I am struggling to convert a simple pong game from SDL to SDL2 because I could not find a valid value for the pixel format argument (second parameter 'Uint32 format') when calling SDL_CreateTexture(). Finally I found success by using SDL_PIXELFORMAT_ARGB8888, and I also found that SDL_PIXELFORMAT_UNKNOWN works too. However, after reading a few articles on system endianness and determining byte-order, I'm still unsure how a valid pixel format can be found for a given system without guessing, using unknown, or running some separate shell scripts.
Is it safe to just rely on SDL_PIXELFORMAT_UNKNOWN? Could anyone provide some information (or a link) to help me better understand these concepts and implementing them? I'd like to understand the related concepts better for future graphics programming.
Here's what the code originally looked like:
screen = SDL_CreateRGBSurfaceWithFormat(0, width, height, 32,
SDL_PIXELTYPE_RGBA32);
screen_texture = SDL_CreateTextureFromSurface(renderer, screen);
Run Code Online (Sandbox Code Playgroud)
And what I used to make it work:
screen = SDL_CreateRGBSurface(0, 640, 480, 32,
0x00FF0000,
0x0000FF00,
0x000000FF,
0xFF000000);
screen_texture = SDL_CreateTexture(renderer,
SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_TARGET,
640, 480); // SDL_PIXELFORMAT_UNKNOWN also works
Run Code Online (Sandbox Code Playgroud)
一个给定SDL_Renderer将支持至少一个SDL_PixelFormatEnum;其他格式也可以使用,但在创建和/或使用时可能会产生转换损失。
用于SDL_GetRendererInfo()填充 a SDL_RendererInfo& 使用其中一种格式SDL_RendererInfo::texture_formats(来自此处):
SDL_RendererInfo info;
SDL_GetRendererInfo( renderer, &info );
cout << "Renderer name: " << info.name << endl;
cout << "Texture formats: " << endl;
for( Uint32 i = 0; i < info.num_texture_formats; i++ )
{
cout << SDL_GetPixelFormatName( info.texture_formats[i] ) << endl;
}
Run Code Online (Sandbox Code Playgroud)
当我运行 DirectX 和 OpenGL 后端时都支持SDL_PIXELFORMAT_ARGB8888。
仔细检查 SDL 代码,git grep -A 25 SDL_RenderDriver我认为第一个texture_formats是“首选”格式:
SDL_RendererInfo info;
SDL_GetRendererInfo( renderer, &info );
cout << "Renderer name: " << info.name << endl;
cout << "Texture formats: " << endl;
for( Uint32 i = 0; i < info.num_texture_formats; i++ )
{
cout << SDL_GetPixelFormatName( info.texture_formats[i] ) << endl;
}
Run Code Online (Sandbox Code Playgroud)