SDL2 修改像素

SDL2 modifying pixels

本文关键字:像素 修改 SDL2      更新时间:2023-10-16

我想用 SDL2 修改单个像素,但我不想用表面修改。

这是我代码的相关部分:

// Create a texture for drawing
SDL_Texture *m_pDrawing = SDL_CreateTexture(m_pRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, 1024, 768);
// Create a pixelbuffer
Uint32 *m_pPixels = (Uint32 *) malloc(1024*768*sizeof(Uint32));
// Set a single pixel to red
m_pPixels[1600] = 0xFF0000FF;
// Update the texture with created pixelbuffer
SDL_UpdateTexture(m_pDrawing, NULL, m_pPixels, 1024*sizeof(Uint32));
// Copy texture to render target
SDL_RenderCopy(m_pRenderer, m_pDrawing, NULL, NULL);

如果渲染,则使用 SDL_RenderPresent(m_pRenderer) 时屏幕上不会显示任何内容。 在这里,他们解释说,你可以使用"surface->pixels,或者malloc()'d缓冲区"。那么,出了什么问题呢?

编辑: 最后,这只是我的m_pRenderer,这是在SDL_CreateTexture电话之后定义的。 现在一切正常,我修复了缓冲区分配中的小错误,所以这段代码应该可以工作了。

我不确定这是否是您的问题,但这是一个错误:

Uint32 *m_pPixels = (Uint32 *) malloc(1024*768);

如果要分配足够的数据来表示曲面中的像素,则需要在其中* sizeof(Uint32)

Uint32 *m_pPixels = (Uint32 *) malloc(1024*768*sizeof(Uint32));

(如果这是C,则不需要演员表。