- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在努力将一个简单的乒乓球游戏从 SDL 转换为 SDL2,因为我找不到像素格式参数的有效值(第二个参数“Uint32
格式”) 当调用 SDL_CreateTexture()
时。最后,我通过使用 SDL_PIXELFORMAT_ARGB8888
获得了成功,而且我还发现 SDL_PIXELFORMAT_UNKNOWN
也能正常工作。然而,在阅读了几篇关于系统字节顺序和确定字节顺序的文章后,我仍然不确定如何在不猜测、使用未知或运行一些单独的 shell 脚本的情况下为给定系统找到有效的像素格式。
仅依赖 SDL_PIXELFORMAT_UNKNOWN
是否安全?谁能提供一些信息(或链接)来帮助我更好地理解这些概念并实现它们?为了以后的图形编程,我想更好地理解相关概念。
这是代码最初的样子:
screen = SDL_CreateRGBSurfaceWithFormat(0, width, height, 32,
SDL_PIXELTYPE_RGBA32);
screen_texture = SDL_CreateTextureFromSurface(renderer, screen);
以及我用来让它工作的东西:
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
最佳答案
给定的 SDL_Renderer
将支持至少一个 SDL_PixelFormatEnum
;其他格式也可以,但可能会在创建和/或使用时产生转换损失。
使用 SDL_GetRendererInfo()
填充 SDL_RendererInfo
并使用 SDL_RendererInfo::texture_formats
中的一种格式(来自 here ):
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;
}
当我运行时,DirectX 和 OpenGL 后端都支持 SDL_PIXELFORMAT_ARGB8888
。
通过 git grep -A 25 SDL_RenderDriver
仔细检查 SDL 代码我认为第一个 texture_formats
是“首选”格式:
SDL_render_d3d.c:SDL_RenderDriver D3D_RenderDriver = {
SDL_render_d3d.c- D3D_CreateRenderer,
SDL_render_d3d.c- {
SDL_render_d3d.c- "direct3d",
SDL_render_d3d.c- (SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE),
SDL_render_d3d.c- 1,
SDL_render_d3d.c- {SDL_PIXELFORMAT_ARGB8888},
SDL_render_d3d.c- 0,
SDL_render_d3d.c- 0}
--
SDL_render_d3d11.c:SDL_RenderDriver D3D11_RenderDriver = {
SDL_render_d3d11.c- D3D11_CreateRenderer,
SDL_render_d3d11.c- {
SDL_render_d3d11.c- "direct3d11",
SDL_render_d3d11.c- (
SDL_render_d3d11.c- SDL_RENDERER_ACCELERATED |
SDL_render_d3d11.c- SDL_RENDERER_PRESENTVSYNC |
SDL_render_d3d11.c- SDL_RENDERER_TARGETTEXTURE
SDL_render_d3d11.c- ), /* flags. see SDL_RendererFlags */
SDL_render_d3d11.c- 6, /* num_texture_formats */
SDL_render_d3d11.c- { /* texture_formats */
SDL_render_d3d11.c- SDL_PIXELFORMAT_ARGB8888,
SDL_render_d3d11.c- SDL_PIXELFORMAT_RGB888,
SDL_render_d3d11.c- SDL_PIXELFORMAT_YV12,
SDL_render_d3d11.c- SDL_PIXELFORMAT_IYUV,
SDL_render_d3d11.c- SDL_PIXELFORMAT_NV12,
SDL_render_d3d11.c- SDL_PIXELFORMAT_NV21
SDL_render_d3d11.c- },
SDL_render_d3d11.c- 0, /* max_texture_width: will be filled in later */
SDL_render_d3d11.c- 0 /* max_texture_height: will be filled in later */
SDL_render_d3d11.c- }
SDL_render_d3d11.c-};
--
SDL_render_metal.m:SDL_RenderDriver METAL_RenderDriver = {
SDL_render_metal.m- METAL_CreateRenderer,
SDL_render_metal.m- {
SDL_render_metal.m- "metal",
SDL_render_metal.m- (SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE),
SDL_render_metal.m- 6,
SDL_render_metal.m- {
SDL_render_metal.m- SDL_PIXELFORMAT_ARGB8888,
SDL_render_metal.m- SDL_PIXELFORMAT_ABGR8888,
SDL_render_metal.m- SDL_PIXELFORMAT_YV12,
SDL_render_metal.m- SDL_PIXELFORMAT_IYUV,
SDL_render_metal.m- SDL_PIXELFORMAT_NV12,
SDL_render_metal.m- SDL_PIXELFORMAT_NV21
SDL_render_metal.m- },
SDL_render_metal.m- 0, 0,
SDL_render_metal.m- }
SDL_render_metal.m-};
--
SDL_render_gl.c:SDL_RenderDriver GL_RenderDriver = {
SDL_render_gl.c- GL_CreateRenderer,
SDL_render_gl.c- {
SDL_render_gl.c- "opengl",
SDL_render_gl.c- (SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE),
SDL_render_gl.c- 1,
SDL_render_gl.c- {SDL_PIXELFORMAT_ARGB8888},
SDL_render_gl.c- 0,
SDL_render_gl.c- 0}
SDL_render_gl.c-};
--
SDL_render_gles.c:SDL_RenderDriver GLES_RenderDriver = {
SDL_render_gles.c- GLES_CreateRenderer,
SDL_render_gles.c- {
SDL_render_gles.c- "opengles",
SDL_render_gles.c- (SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC),
SDL_render_gles.c- 1,
SDL_render_gles.c- {SDL_PIXELFORMAT_ABGR8888},
SDL_render_gles.c- 0,
SDL_render_gles.c- 0
SDL_render_gles.c- }
SDL_render_gles.c-};
--
SDL_render_gles2.c:SDL_RenderDriver GLES2_RenderDriver = {
SDL_render_gles2.c- GLES2_CreateRenderer,
SDL_render_gles2.c- {
SDL_render_gles2.c- "opengles2",
SDL_render_gles2.c- (SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE),
SDL_render_gles2.c- 4,
SDL_render_gles2.c- {
SDL_render_gles2.c- SDL_PIXELFORMAT_ARGB8888,
SDL_render_gles2.c- SDL_PIXELFORMAT_ABGR8888,
SDL_render_gles2.c- SDL_PIXELFORMAT_RGB888,
SDL_render_gles2.c- SDL_PIXELFORMAT_BGR888
SDL_render_gles2.c- },
SDL_render_gles2.c- 0,
SDL_render_gles2.c- 0
SDL_render_gles2.c- }
SDL_render_gles2.c-};
--
SDL_render_psp.c:SDL_RenderDriver PSP_RenderDriver = {
SDL_render_psp.c- .CreateRenderer = PSP_CreateRenderer,
SDL_render_psp.c- .info = {
SDL_render_psp.c- .name = "PSP",
SDL_render_psp.c- .flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE,
SDL_render_psp.c- .num_texture_formats = 4,
SDL_render_psp.c- .texture_formats = { [0] = SDL_PIXELFORMAT_BGR565,
SDL_render_psp.c- [1] = SDL_PIXELFORMAT_ABGR1555,
SDL_render_psp.c- [2] = SDL_PIXELFORMAT_ABGR4444,
SDL_render_psp.c- [3] = SDL_PIXELFORMAT_ABGR8888,
SDL_render_psp.c- },
SDL_render_psp.c- .max_texture_width = 512,
SDL_render_psp.c- .max_texture_height = 512,
SDL_render_psp.c- }
SDL_render_psp.c-};
--
SDL_render_sw.c:SDL_RenderDriver SW_RenderDriver = {
SDL_render_sw.c- SW_CreateRenderer,
SDL_render_sw.c- {
SDL_render_sw.c- "software",
SDL_render_sw.c- SDL_RENDERER_SOFTWARE | SDL_RENDERER_TARGETTEXTURE,
SDL_render_sw.c- 8,
SDL_render_sw.c- {
SDL_render_sw.c- SDL_PIXELFORMAT_ARGB8888,
SDL_render_sw.c- SDL_PIXELFORMAT_ABGR8888,
SDL_render_sw.c- SDL_PIXELFORMAT_RGBA8888,
SDL_render_sw.c- SDL_PIXELFORMAT_BGRA8888,
SDL_render_sw.c- SDL_PIXELFORMAT_RGB888,
SDL_render_sw.c- SDL_PIXELFORMAT_BGR888,
SDL_render_sw.c- SDL_PIXELFORMAT_RGB565,
SDL_render_sw.c- SDL_PIXELFORMAT_RGB555
SDL_render_sw.c- },
SDL_render_sw.c- 0,
SDL_render_sw.c- 0}
SDL_render_sw.c-};
--
SDL_DirectFB_render.c:SDL_RenderDriver DirectFB_RenderDriver = {
SDL_DirectFB_render.c- DirectFB_CreateRenderer,
SDL_DirectFB_render.c- {
SDL_DirectFB_render.c- "directfb",
SDL_DirectFB_render.c- (SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED),
SDL_DirectFB_render.c- /* (SDL_TEXTUREMODULATE_NONE | SDL_TEXTUREMODULATE_COLOR |
SDL_DirectFB_render.c- SDL_TEXTUREMODULATE_ALPHA),
SDL_DirectFB_render.c- (SDL_BLENDMODE_NONE | SDL_BLENDMODE_MASK | SDL_BLENDMODE_BLEND |
SDL_DirectFB_render.c- SDL_BLENDMODE_ADD | SDL_BLENDMODE_MOD),
SDL_DirectFB_render.c- (SDL_SCALEMODE_NONE | SDL_SCALEMODE_FAST |
SDL_DirectFB_render.c- SDL_SCALEMODE_SLOW | SDL_SCALEMODE_BEST), */
SDL_DirectFB_render.c- 0,
SDL_DirectFB_render.c- {
SDL_DirectFB_render.c- /* formats filled in later */
SDL_DirectFB_render.c- },
SDL_DirectFB_render.c- 0,
SDL_DirectFB_render.c- 0}
SDL_DirectFB_render.c-};
关于c - 确定 SDL_CreateTexture() 的正确像素格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56143991/
我想知道如何使用SDL_CreateTexture功能。 我只想创建纹理,给它一种颜色并将其绘制在屏幕上的某个位置。我不想加载任何图片。 我以为我可以按此顺序使用 SDL_CreateTexture、
我正在努力将一个简单的乒乓球游戏从 SDL 转换为 SDL2,因为我找不到像素格式参数的有效值(第二个参数“Uint32 格式”) 当调用 SDL_CreateTexture() 时。最后,我通过使用
我是一名优秀的程序员,十分优秀!