【问题标题】:How to get an SDL_PixelFormat from an SDL_PixelFormatEnum or SDL_Texture?如何从 SDL_PixelFormatEnum 或 SDL_Texture 获取 SDL_PixelFormat?
【发布时间】:2015-08-13 23:13:36
【问题描述】:

我一直在尝试深入了解 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;
}

在你说之前,我知道这只会产生白屏。

【问题讨论】:

    标签: c++ sdl sdl-2


    【解决方案1】:

    所以,SDL_PixelFormatSDL_PixelFormatEnum 是完全不同的类型,你不能在它们之间进行转换。您可以要求 SDL 查找与您提到的 Uint32 对应的 SDL_PixelFormat

    /**
     *  \brief Create an SDL_PixelFormat structure from a pixel format enum.
     */
    extern DECLSPEC SDL_PixelFormat * SDLCALL SDL_AllocFormat(Uint32 pixel_format);
    

    来自SDL2 header

    SDL 文档通常有点参差不齐,但是当我不确定某些 SDL 内容时,我的 goto 信息位置例如,首先是 these 页面,然后只需查看 SDL2 标题本身,然后也许谷歌它并希望它在论坛帖子或其他内容中被提及。

    希望这会有所帮助。 (注意我这里没有编译任何东西)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      相关资源
      最近更新 更多