如何从SDL_PixelFormatEnum或SDL_Texture中获得SDL_PixelFormat
How to get an SDL_PixelFormat from an SDL_PixelFormatEnum or SDL_Texture?
我一直在努力了解SDL的基本知识,但我被看似简单的东西难住了。
SDL_MapRGB()
需要const SDL_PixelFormat*
,我使用SDL_PixelFormatEnum
在我的项目中创建纹理,这是unit32
。但我找不到任何方法将其转换为SDL_MapRGB()
使用。
可能有一个比使用SDL_MapRGB()
更简单的方法,但这个问题仍然会让我困惑,因为你可以很容易地转换成另一种方式。
不相关,但如果你想知道剩下的代码,那就去吧。
#include <SDL.h>
SDL_Window *sdlWindow;
SDL_Renderer *sdlRenderer;
int main( int argc, char *args[] )
{
int w = 640;
int h = 480;
Uint32 format = SDL_PIXELFORMAT_RGB888;
SDL_CreateWindowAndRenderer(w, h, 0, &sdlWindow, &sdlRenderer);
SDL_Texture *sdlTexture = SDL_CreateTexture(sdlRenderer, format, SDL_TEXTUREACCESS_STREAMING, w, h);
extern uint32_t *pixels;
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
pixels[x + y * w] = SDL_MapRGB(format, 255, 255, 255);
}
}
SDL_UpdateTexture(sdlTexture, NULL, pixels, 640 * sizeof (Uint32));
SDL_RenderClear(sdlRenderer);
SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, NULL);
SDL_RenderPresent(sdlRenderer);
SDL_Delay(5000);
SDL_Quit();
return 0;
}
在你说之前,我知道这只是一个白屏幕。
所以,SDL_PixelFormat
和SDL_PixelFormatEnum
是完全不同的类型,您不会在它们之间进行强制转换。您可以要求SDL查找与您提到的Uint32
对应的SDL_PixelFormat
:
/**
* Create an SDL_PixelFormat structure corresponding to a pixel format.
*
* Returned structure may come from a shared global cache (i.e. not newly
* allocated), and hence should not be modified, especially the palette. Weird
* errors such as `Blit combination not supported` may occur.
*
* param pixel_format one of the SDL_PixelFormatEnum values
* returns the new SDL_PixelFormat structure or NULL on failure; call
* SDL_GetError() for more information.
*
* since This function is available since SDL 2.0.0.
*
* sa SDL_FreeFormat
*/
extern DECLSPEC SDL_PixelFormat * SDLCALL SDL_AllocFormat(Uint32 pixel_format);
来源:SDL2 header
SDL文档通常有点参差不齐,但是当我不确定一些SDL的事情时,我的goto info的地方是,例如,这些页面首先,然后只是去看看SDL2头本身,然后也许谷歌它,希望它在论坛帖子或其他地方被提及。
希望这有帮助。(注意,我没有尝试在这里编译任何东西)
相关文章:
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- Mix_Init和Mix_OpenAudio SDL之间的区别是什么
- SDL 窗口不会弹出
- SDL 操纵杆按钮按下/C++
- VSCode IntelliSense无法识别SDL框架的SDL_image扩展库
- C++/SDL "initial value of reference to a non-const must be an lvalue"
- SDL 映像:无法打开映像,仅显示错误消息
- 如何在 3D OpenGL 场景上绘制 SDL 2D 矩形?
- "Undefined symbols for architecture x86_64" 在 SDL 2 (macOS) 中
- 为什么 SDL 在 Mac 上比 Linux 慢得多?
- 在 OSX 上打开 SDL 窗口时不要打开终端窗口
- SDL + OpenGL ES 2.0 使用着色器显示纹理
- SDL GPU 为什么将两个图像分成两个单独的循环更快?
- 在 cmake 中找不到 SDL 库
- SDL/C++: 如何平滑但随机地移动矩形?
- 如何与 SDL 库链接?
- 为什么我的C++代码不呈现 SDL 矩形?
- 为什么要在初始化SDL之前在SDL中声明指针
- C++/SDL 如何覆盖数组?
- 在 SDL 中水平减小静态图像进度条