我编写了这个简单的 C 程序来测试基本的 SDL 1.3 功能。一切正常,只有一个小问题。colorkey 不会被转换。我正在加载一个 8 位 PNG 精灵文件,其中调色板索引 #0 是背景颜色。我希望只看到显示的精灵,但我得到的是整个图像,包括背景颜色。
知道发生了什么或如何解决它吗?这是用 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 中的一个错误,我最终提交了一个补丁。