2012-11-01 40 views
0

以下代碼旨在在黑色背景上顯示綠色正方形。它執行,但綠色方塊不顯示。但是,如果我將SDL_DisplayFormatAlpha更改爲SDL_DisplayFormat,則正方形顯示正確。SDL_DisplayFormat工作,但不是SDL_DisplayFormatAlpha

那我有什麼不明白的?在我看來,我使用alpha蒙版創建了*surface,並且我使用SDL_MapRGBA來映射我的綠色,因此使用SDL_DisplayFormatAlpha也是一致的。

(我刪除了錯誤檢查的清晰度,但沒有SDL API調用在這個例子中失敗。)

#include <SDL.h> 

int main(int argc, const char *argv[]) 
{ 
    SDL_Init(SDL_INIT_EVERYTHING); 

    SDL_Surface *screen = SDL_SetVideoMode(
     640, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF 
    ); 

    SDL_Surface *temp = SDL_CreateRGBSurface(
     SDL_HWSURFACE, 100, 100, 32, 0, 0, 0, 
     (SDL_BYTEORDER == SDL_BIG_ENDIAN ? 0x000000ff : 0xff000000) 
    ); 

    SDL_Surface *surface = SDL_DisplayFormatAlpha(temp); 

    SDL_FreeSurface(temp); 

    SDL_FillRect(
     surface, &surface->clip_rect, SDL_MapRGBA(
      screen->format, 0x00, 0xff, 0x00, 0xff 
     ) 
    ); 

    SDL_Rect r; 
    r.x = 50; 
    r.y = 50; 

    SDL_BlitSurface(surface, NULL, screen, &r); 

    SDL_Flip(screen); 

    SDL_Delay(1000); 

    SDL_Quit(); 

    return 0; 
} 

回答

0

我用了SDL_MapRGBA的格式不正確。本來應該

SDL_FillRect(
    surface, NULL, SDL_MapRGBA(
     surface->format, 0xff, 0xff, 0x00, 0xff 
    ) 
); 

(的surface->format代替screen->format)。我認爲這兩個是等價的。他們在致電SDL_DisplayFormatAlpha()後致電SDL_DisplayFormat(),但不是。屏幕表面沒有alpha通道,因此兩者的格式不同。

(交叉發佈從gamedev.stackexchange.com