jeudi 13 août 2015

casting a SDL_PixelFormatEnum from 'unit32' to 'const SDL_PixelFormat'

I've been trying to wrap my head around the basics of SDL and I'm stomped 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' and I can't find any way of converting it for use with SDL_MapRGB().

There's probably a an easier way then 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.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire