如何在SDL 2.0中绘制像素?

时间:2022-10-23 12:05:49

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 really awesome Pxl-On() command, so easy.

我几乎所有的图形体验都来自于在 上使用JavaScript。其他的大部分来自我的计算器,它有一个非常棒的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。另外,如果有比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.

减少代码行来完成相同的任务。

3 个解决方案

#1


7  

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 aways can specify an area of 1x1. Read the wiki page if 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部分仍在进行中,但是有足够的材料开始学习。

#2


4  

Runnable example

可运行的例子

Draws a diagonal red line pixel by pixel on the screen using SDL_RenderDrawPoint.

使用SDL_RenderDrawPoint在屏幕上按像素绘制一个对角线像素。

如何在SDL 2.0中绘制像素?

#include <stdlib.h>

#include <SDL2/SDL.h>

#define WINDOW_WIDTH 600

int main(void) {
    SDL_Event event;
    SDL_Renderer *renderer;
    SDL_Window *window;
    int i;

    SDL_Init(SDL_INIT_VIDEO);
    SDL_CreateWindowAndRenderer(WINDOW_WIDTH, WINDOW_WIDTH, 0, &window, &renderer);
    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
    SDL_RenderClear(renderer);
    SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
    for (i = 0; i < WINDOW_WIDTH; ++i)
        SDL_RenderDrawPoint(renderer, i, i);
    SDL_RenderPresent(renderer);
    while (1) {
        if (SDL_PollEvent(&event) && event.type == SDL_QUIT)
            break;
    }
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return EXIT_SUCCESS;
}

Compile with:

编译:

gcc -std=c89 -Wextra -pedantic-errors -o 'main.out' 'main.c' -lSDL2

Tested on libsdl 2.0.2, Ubuntu 15.10.

在libsdl 2.0.2、Ubuntu 15.10上测试。

If you want to set a large rectangle of pixels at once, e.g. the entire screen or a sprite, use SDL_Texture + SDL_RenderCopy and possibly SDL_TEXTUREACCESS_STREAMING, as that will be much faster. Examples at:

如果您希望一次性设置一个大的像素矩形,例如整个屏幕或一个sprite,可以使用sdl_纹理+ SDL_RenderCopy和可能的sdl_textureaccess_流,因为那样会更快。例子:

#3


2  

I find Python + PySDL2 more 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 <techtonik@gmail.com>
"""

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

#1


7  

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 aways can specify an area of 1x1. Read the wiki page if 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部分仍在进行中,但是有足够的材料开始学习。

#2


4  

Runnable example

可运行的例子

Draws a diagonal red line pixel by pixel on the screen using SDL_RenderDrawPoint.

使用SDL_RenderDrawPoint在屏幕上按像素绘制一个对角线像素。

如何在SDL 2.0中绘制像素?

#include <stdlib.h>

#include <SDL2/SDL.h>

#define WINDOW_WIDTH 600

int main(void) {
    SDL_Event event;
    SDL_Renderer *renderer;
    SDL_Window *window;
    int i;

    SDL_Init(SDL_INIT_VIDEO);
    SDL_CreateWindowAndRenderer(WINDOW_WIDTH, WINDOW_WIDTH, 0, &window, &renderer);
    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
    SDL_RenderClear(renderer);
    SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
    for (i = 0; i < WINDOW_WIDTH; ++i)
        SDL_RenderDrawPoint(renderer, i, i);
    SDL_RenderPresent(renderer);
    while (1) {
        if (SDL_PollEvent(&event) && event.type == SDL_QUIT)
            break;
    }
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return EXIT_SUCCESS;
}

Compile with:

编译:

gcc -std=c89 -Wextra -pedantic-errors -o 'main.out' 'main.c' -lSDL2

Tested on libsdl 2.0.2, Ubuntu 15.10.

在libsdl 2.0.2、Ubuntu 15.10上测试。

If you want to set a large rectangle of pixels at once, e.g. the entire screen or a sprite, use SDL_Texture + SDL_RenderCopy and possibly SDL_TEXTUREACCESS_STREAMING, as that will be much faster. Examples at:

如果您希望一次性设置一个大的像素矩形,例如整个屏幕或一个sprite,可以使用sdl_纹理+ SDL_RenderCopy和可能的sdl_textureaccess_流,因为那样会更快。例子:

#3


2  

I find Python + PySDL2 more 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 <techtonik@gmail.com>
"""

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