C++ 如何在 SDL 2.0 中绘制像素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20579658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:12:54  来源:igfitidea点击:

How to draw pixels in SDL 2.0?

c++csdlsdl-2

提问by Jordan

How does one draw with pixels in SDL2.0?

在 SDL2.0 中如何用像素绘制?

I'm trying to get familiar with C++, but this is very difficult to do without pretty pictures, so I'm trying to get a very basic graphics display thing running. All I really want it to do is give me a window, let me draw rgbα pixels on it, and access information about those pixels. There may be other things I want that I'm not aware of, but that's all that's on my list right now. My research on this has lead me to try using SDL, the current version being 2.0.

我正在尝试熟悉 C++,但是如果没有漂亮的图片,这很难做到,所以我正在尝试运行一个非常基本的图形显示。我真正想让它做的就是给我一个窗口,让我在上面绘制 rgbα 像素,并访问有关这些像素的信息。可能还有其他我不知道的我想要的东西,但这就是我现在清单上的全部内容。我对此的研究使我尝试使用 SDL,当前版本为 2.0。

Almost all my graphics experience comes from using JavaScript on a <canvas>. Most of the other bit comes from my calculator, which has this reallyawesome Pxl-On()command, so easy.

我几乎所有的图形经验都来自在<canvas>. 其他大部分来自我的计算器,它有一个非常棒的Pxl-On()命令,非常简单。

I'm using MinGW for my C++, if it matters. Also, if there's something better** out there than SDL2.0 for what I need, advice welcome.

如果重要的话,我将 MinGW 用于我的 C++。另外,如果有比 SDL2.0 更好的东西** 满足我的需求,欢迎提出建议。



** "better" means "contains what functionality I need, but less total functionality than SDL2.0, and/or has a more intuitive/less complex*** API than SDL2.0."

**“更好”意味着“包含我需要的功能,但总功能比 SDL2.0 少,和/或比 SDL2.0 具有更直观/更简单的 API ***。”

*** Less lines of code to accomplish the same task.

*** 完成相同任务所需的代码行更少。

采纳答案by TalesM

I don't know how your code is structured. Assuming you have a SDL_Window and a SDL_Renderer, you just have to call SDL_RenderDrawPoint(renderer, x, y).

我不知道你的代码是如何构建的。假设你有一个 SDL_Window 和一个 SDL_Renderer,你只需要调用SDL_RenderDrawPoint(renderer, x, y).

If you don't have a renderer nor window, you can create both with SDL_CreateWindowAndRenderer(). For example:

如果您没有渲染器或窗口,则可以使用 SDL_CreateWindowAndRenderer() 创建两者。例如:

SDL_Window *window;
SDL_Renderer *renderer;
SDL_CreateWindowAndRenderer(800, 600, 0, &window, &renderer);

//Probably on a loop
  SDL_RenderDrawPoint(renderer, 400, 300); //Renders on middle of screen.
  SDL_RenderPresent(renderer);

This should draw a pixel on the middle of screen. To read a pixel is a little more complicated. You can use SDL_RenderReadPixels(), it is made for read an area, but you can always specify an area of 1x1. Read the wiki pageif you really need it.

这应该在屏幕中间绘制一个像素。读取像素稍微复杂一些。您可以使用SDL_RenderReadPixels(),它用于读取区域,但您始终可以指定 1x1 的区域。如果您确实需要,请阅读维基页面

If you are having much trouble with SDL2 a recommend you to read the Lazy Foo tutorials. The SDL2 section still a work in progress, but there is enough material to begin learning.

如果您在使用 SDL2 时遇到很多问题,建议您阅读Lazy Foo 教程。SDL2 部分仍在进行中,但有足够的材料可以开始学习。

回答by anatoly techtonik

I find Python + PySDL2more easy to prototype with. Debug is also funny, because it is veeeery slooow for pixel graphics. =) Here is the complete code:

我发现 Python + PySDL2更容易使用原型。调试也很有趣,因为它对于像素图形来说太慢了。=) 这是完整的代码:

"""
The code is placed into public domain
by anatoly techtonik <[email protected]>
"""

import sdl2
import sdl2.ext

sdl2.ext.init()

window = sdl2.ext.Window('', size=(300, 100))
window.show()

renderer = sdl2.ext.Renderer(window)
renderer.draw_point([10,10], sdl2.ext.Color(255,255,255))
renderer.present()

running = True
while running:
  for e in sdl2.ext.get_events():
    if e.type == sdl2.SDL_QUIT:
      running = False
      break
    if e.type == sdl2.SDL_KEYDOWN:
      if e.key.keysym.sym == sdl2.SDLK_ESCAPE:
        running = False
        break