2010-03-29 131 views
1

我正在開發一個簡單的Roguelike遊戲,並使用SDL作爲顯示。遊戲的圖形是Codepage 437的圖像,背景爲黑色,字體爲白色。我不想使用已經着色的許多獨立圖像文件,而是使用一個圖像文件,並在將其加載到內存中時替換顏色。使用SDL使用SDL顏色鍵替換顏色

將代碼頁拆分爲精靈表的代碼工作正常,但嘗試以彩色打印時,所有內容都以白色顯示。過去我曾經工作過,但不知何故,當改變打印時的顏色以改變加載時的顏色時,我破壞了代碼。以下是加載圖像的代碼:

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]); 
    } 
} 

問題出在上面的for循環上面。我無法弄清楚它爲什麼不起作用。任何幫助是極大的讚賞!

+0

在您的font.png中「白色」實際上是全RGB(255,255,255)? – genpfault 2010-04-01 16:41:41

+0

是的。白色是FFFFFF,黑色是000000. – 2010-04-02 02:11:59

+0

您的最後一個for循環僅運行在Font [1]上,是您的意圖嗎? – Steven 2010-07-25 03:27:49

回答

0

我解決了這個問題。這個問題源於錯誤順序的顏色鍵控。這裏是解決的代碼:

uint8_t i; 
uint8_t j; 
uint8_t k = 0; 

SDL_FillRect(Screen,NULL,0x00000000); 

SDL_Rect Offset; 
SDL_Surface *Load; 
SDL_Surface *LoadFont; 

Load = IMG_Load("resource/font.png"); 
LoadFont = SDL_DisplayFormat(Load); 
SDL_FreeSurface(Load); 

Offset.x = 0; 
Offset.y = 0; 

for(i = 0; i < 16; i++) { for(j = 0; j < 16; j++) 
{ 
    Character[k].x = Offset.x; 
    Character[k].y = Offset.y; 
    Character[k].w = XWIDTH; 
    Character[k].h = YHIEGHT; 
    k++; 
    Offset.x += XWIDTH; 
} Offset.x = 0; Offset.y += 12; } 

Color[0][0] = SDL_MapRGB(LoadFont->format,255,255,255); 
Color[0][1] = SDL_MapRGB(LoadFont->format,96,96,96); 
Color[1][0] = SDL_MapRGB(LoadFont->format,255,0,0); 
Color[1][1] = SDL_MapRGB(LoadFont->format,96,0,0); 
Color[2][0] = SDL_MapRGB(LoadFont->format,0,255,0); 
Color[2][1] = SDL_MapRGB(LoadFont->format,0,96,0); 
Color[3][0] = SDL_MapRGB(LoadFont->format,0,0,255); 
Color[3][1] = SDL_MapRGB(LoadFont->format,0,0,96); 
Color[4][0] = SDL_MapRGB(LoadFont->format,255,255,0); 
Color[4][1] = SDL_MapRGB(LoadFont->format,96,96,0); 

SDL_SetColorKey(LoadFont,SDL_SRCCOLORKEY,Color[0][0]); 

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

SDL_FreeSurface(LoadFont);