Using SDL to replace colors using SDL Color Keys

Posted by Shawn B on Stack Overflow See other posts from Stack Overflow or by Shawn B
Published on 2010-03-29T19:41:12Z Indexed on 2010/03/29 19:43 UTC
Read the original article Hit count: 699

Filed under:
|
|
|
|

Hey,

I am working an a simple Roguelike game, and using SDL as the display. The Graphics for the game is an image of Codepage 437, with the background being black, and the font white. Instead of using many seperate image files that are already colored, I want to use one image file, and replace the colors when it is being loaded into memory.

The code to split the codepage into a sprite sheet works properly, but when attempting to print in color, everything comes out in white. I had it working the past, but somehow I broke the code when changing it from change the color at print, to change the color on load. Here is the code to load the image:

SDL_Surface *Screen,*Font[2];
SDL_Rect Character[256];

Uint8 ScreenY,ScreenX;
Uint16 PrintX,PrintY,ScreenSizeY,ScreenSizeX;
Uint32 Color[2];

void InitDisplay()
{
    if(SDL_Init(SDL_INIT_VIDEO) == -1) { printf("SDL Init failed\n"); return; }

    ScreenSizeY = 600;
    ScreenSizeX = 800;
    Screen = SDL_SetVideoMode(ScreenSizeX,ScreenSizeY,32,SDL_HWSURFACE | SDL_RESIZABLE);
    SDL_WM_SetCaption("Alpha",NULL);

    SDL_Surface *Load;
    Load = IMG_Load("resource/font.png");
    Font[0] = SDL_DisplayFormat(Load);
    SDL_FreeSurface(Load);

    Color[0] = SDL_MapRGB(Font[0]->format,255,255,255);
    Color[1] = SDL_MapRGB(Font[0]->format,255,0,0);

    Uint8 i,j,k = 0;

    PrintX = 0;
    PrintY = 0;

    for(i = 0; i < 16; i++) { for(j = 0; j < 16; j++)
    {
        Character[k].x = PrintX;
        Character[k].y = PrintY;
        Character[k].w = 8;
        Character[k].h = 12;
        k++;
        PrintX += 8;
    } PrintX = 0; PrintY += 12; }

    PrintX = 0;
    PrintY = 0;

    for(i = 1; i < 2; i++)
    {
        Font[i] = SDL_DisplayFormat(Font[0]);
        SDL_SetColorKey(Font[i],SDL_SRCCOLORKEY,Color[i]);
        SDL_FillRect(Font[i],&Font[i]->clip_rect,Color[i]);
        SDL_BlitSurface(Font[0],NULL,Font[i],NULL);
        SDL_SetColorKey(Font[0],0,Color[0]);
    }
}

The problem is with the last for loop above. I can't figure out why it isn't working. Any help is greatly appreciated!

© Stack Overflow or respective owner

Related posts about c++

Related posts about sdl