首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从SDL_PixelFormat或SDL_PixelFormatEnum获得SDL_Texture?

如何从SDL_PixelFormat或SDL_PixelFormatEnum获得SDL_Texture?
EN

Stack Overflow用户
提问于 2015-08-13 23:13:36
回答 1查看 3.1K关注 0票数 5

我一直在试图了解SDL的基本知识,我对应该看起来很简单的东西感到困惑。

SDL_MapRGB()需要const SDL_PixelFormat*,我在我的项目unit32中使用SDL_PixelFormatEnum创建纹理。但我找不到任何方法将其转换为与SDL_MapRGB()一起使用。

可能有一种比使用SDL_MapRGB()更简单的方法,但这个问题仍然会使我感到困惑,因为您可以轻松地将它转换为另一种方式。

不相干,但如果你想知道其余的代码,那么他们的你去。

代码语言:javascript
复制
#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;
}

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-14 00:54:00

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

代码语言:javascript
复制
/**
 * 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报头

SDL文档通常有点不稳定,但是当我不确定一些SDL的时候,我的goto信息的位置是:比如,这些页面,然后自己去查看SDL2头,然后搜索它,并希望在论坛帖子中提到它。

希望这能帮上忙。(请注意,我没有试图在这里编译任何东西)

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31999935

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档