我寫了這個簡單的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了一個問題,我結束了在提交一個補丁。
你爲什麼要試用1.3版本? – Zoomulator 2009-08-12 17:18:13
因爲1.3增加了我需要的一些新功能。 – 2009-08-12 21:43:23