Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get an SDL_PixelFormat from an SDL_PixelFormatEnum or SDL_Texture?

Tags:

c++

sdl

sdl-2

I've been trying to wrap my head around the basics of SDL, and I'm stumped by what should seem simple.

SDL_MapRGB() requires const SDL_PixelFormat*, and I use a SDL_PixelFormatEnum for creating textures in my project which is unit32. But I can't find any way of converting it for use with SDL_MapRGB().

There's probably an easier way than using SDL_MapRGB(), but this problem would still confuse me, as you can easily convert it the other way.

Irrelevant, but if you wish to know the rest of the code, then their you go.

#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;
}

Before you say it, I know this just makes a white screen.

like image 496
Dead_S Avatar asked Aug 13 '15 23:08

Dead_S


People also ask

What is pixel format in SDL?

An SDL_PixelFormat describes the format of the pixel data stored at the pixels field of an SDL_Surface. Every surface stores an SDL_PixelFormat in the format field. If you wish to do pixel level modifications on a surface, then understanding how SDL stores its color information is essential.

What is SDL_QueryTexture?

The function SDL_QueryTexture() is used to retrieve the basic settings of a texture, including the format, access, width, and height. int w, h, access; unsigned int format; SDL_QueryTexture( texture, &format, &access, &width, &height );


1 Answers

So, SDL_PixelFormat and SDL_PixelFormatEnum are simply completely different types, you don't cast between them. You can ask SDL to lookup the SDL_PixelFormat corresponding to the Uint32 you mentioned though:

/**
 * 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);

Source: SDL2 header

SDL docs are often a bit spotty, but my goto info places when I'm not sure about some SDL thing are for example, these pages first, then just go look in the SDL2 headers themselves, then maybe Google it and hope it's mentioned in a forum post or something.

Hope this helped. (Note that I didn't try to compile anything here)

like image 77
Chris Beck Avatar answered Sep 28 '22 15:09

Chris Beck