您需要使用SDL_TEXTUREACCESS_STREAMING标志创建纹理,并在能够处理像素数据之前锁定纹理。要确定某个像素在纹理中是否透明,请确保您致电
SDL_SetTextureBlendMode(t, SDL_BLENDMODE_BLEND);
这样纹理就可以识别Alpha通道。
尝试这样:
SDL_Texture *t;
int main()
{
// initialize SDL, window, renderer, texture
int pitch, w, h;
void *pixels;
SDL_SetTextureBlendMode(t, SDL_BLENDMODE_BLEND);
SDL_QueryTexture(t, NULL, &aw, &h);
SDL_LockTexture(t, NULL, &pixels, &pitch);
Uint32 *upixels = (Uint32*) pixels;
// you will need to know the color of the pixel even if it's transparent
Uint32 transparent = SDL_MapRGBA(SDL_GetWindowSurface(window)->format, r, g, b, 0x00);
// manipulate pixels
for (int i = 0; i < w * h; i++)
{
if (upixels[i] == transparent)
// do stuff
}
// replace the old pixels with the new ones
memcpy(pixels, upixels, (pitch/4) * h);
SDL_UnlockTexture(t);
return 0;
}
如果您有任何疑问,请随时提出。虽然我不是这个话题的专家。
欲了解更多信息和教程,请查看http://lazyfoo.net/tutorials/SDL/index.php。教程40专门处理像素操作。
我很抱歉,如果方法名称有任何错误(我把它写在我头上)。
希望这有助于。
查看SDL_LockTexture的文档,看起来这使像素数据仅可用于写入访问。是否有另一种像素数据可以被读取访问的方式? –
@ Julian锁定纹理会将像素保存到传入它的void指针中。所以,如果你只是想读,然后锁定和解锁纹理一次,像素将被保存。 – user2437378
@ user2437378不正式 - https://wiki.libsdl。org/SDL_LockTexture - '作为优化,可用于编辑的像素不一定包含旧纹理数据。这是一个只写操作,如果您需要保留纹理数据的副本,您应该在应用程序级别执行此操作。“很难想到可以实现的方式,但依赖于某些东西他们明确表示不会。依靠这个建议的读者可能有一天会破坏他们的代码风险,到时候这太迟了。 –