2009-07-11 20 views
0

我寫了這個簡單的C程序來測試基本的SDL 1.3功能。一切正常,只有一個小問題。該colorkey不會被轉換。我正在加載一個8位PNG sprite文件,其中調色板索引#0是背景顏色。我希望只看到精靈顯示,但我得到的是整個圖像,包括背景顏色。爲什麼我無法使用SDI紋理的colorkey?

任何想法發生了什麼或如何解決它?這是用Visual C++ 2005

// SDL test.c : Defines the entry point for the console application. 
// 

#include "stdafx.h" 
#include "sdl.h" 
#include "sdl_image.h" 
#include <stdio.h> 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    int windowID; 
    int textureID; 
    SDL_Surface* surface; 
    char* dummy = ""; 
    SDL_Color color; 

    SDL_Init(SDL_INIT_VIDEO); 

    windowID = SDL_CreateWindow("SDL Test", 400, 400, 320, 240, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN); 
    if (windowID == 0) 
    { 
     printf("Unable to create window: %s\n", SDL_GetError()); 
    } 
    else printf("Window created: %d\n", windowID); 

    if (SDL_CreateRenderer(windowID, -1, SDL_RENDERER_PRESENTFLIP2) != 0) 
    { 
     printf("Unable to create renderer: %s\n", SDL_GetError()); 
    } 
    else printf("Renderer created successfully.\n"); 

    if (SDL_SelectRenderer(windowID) != 0) 
    { 
     printf("Unable to select renderer: %s\n", SDL_GetError()); 
    } 
    else printf("Renderer selected successfully\n"); 

    SDL_RenderPresent(); 

    surface = IMG_Load("<INSERT FILENAME HERE>"); 
    if (!surface) 
    { 
     printf("Unable to load image!\n"); 
    } 
    else printf("Image Loaded\n"); 

    color = surface->format->palette->colors[0]; 
    SDL_SetColorKey(surface, 1, SDL_MapRGB(surface->format, color.r, color.g, color.b)); 

    textureID = SDL_CreateTextureFromSurface(0, surface); 
    if (textureID == 0) 
    { 
     printf("Unable to create texture: %s\n", SDL_GetError()); 
    } 
    else printf("Texture created: %d\n", textureID); 

    SDL_FreeSurface(surface); 

    if (SDL_RenderCopy(textureID, NULL, NULL) != 0) 
    { 
     printf("Unable to render texture: %s", SDL_GetError()); 
    } 

    SDL_RenderPresent(); 

    scanf_s("%s", dummy); 
    return 0; 
} 

編輯:事實證明,這是由於SDL_CreateTextureFromSurface了一個問題,我結束了在提交一個補丁。

+0

你爲什麼要試用1.3版本? – Zoomulator 2009-08-12 17:18:13

+0

因爲1.3增加了我需要的一些新功能。 – 2009-08-12 21:43:23

回答

0

您應該在使用前將表面轉換爲顯示格式。

... 
surface = IMG_Load("<INSERT FILENAME HERE>"); 
SDL_DisplayFormat(surface); 
SDL_SetColorKey(surface, 1, SDL_MapRGB(surface->format, color.r, color.g, color.b)); 
... 

總是一個好主意,這樣做,所以你絕對相信,所有圖像使用SDL功能時具有相同的格式,並給出正確的結果。

儘管我只熟悉1.2版本,並且沒有線索1.3中已更改的內容。

2

以下是可能對您有所幫助的兩個示例。他們使用SDL1.3爲我完美工作。

if (color == white) 
{ 
    SDL_SetColorKey(bmp_surface, 1, 
        SDL_MapRGB(bmp_surface->format, 255, 255, 255)); 
} 

if (color == blue) 
{ 
    SDL_SetColorKey(bmp_surface, 1, 
        SDL_MapRGB(bmp_surface->format, 0, 0, 254)); 
} 
相關問題