首页 文章

如何从SDL_PixelFormatEnum或SDL_Texture获取SDL_PixelFormat?

提问于
浏览
3

我一直在试图围绕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;
}

在你说之前,我知道这只是一个白色的屏幕 .

1 回答

  • 4

    所以, 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文档通常有点不稳定,但是我的goto信息放置在我不确定某些SDL的东西例如,首先是these页面,然后只是查看SDL2 Headers 本身,然后可能谷歌它并希望它在论坛帖子或其他东西 .

    希望这有帮助 . (注意,我没有尝试在这里编译任何东西)

相关问题