SDL_SetColorKey的问题

0 c++ graphics transparency sdl

我正在尝试使用SDL创建透明精灵.我在带有品红色(0xff00ff)背景的位图上使用SDL_SetColorKey(它是100%品红色,我用GIMP检查了:))对SDL_SetColorKey的调用如下所示:

SDL_SetColorKey( bitmap, SDL_SRCCOLORKEY, SDL_MapRGB(bitmap->format, 255, 0, 255) );

对SDL_SetColorKey的调用显然返回0,但是没有透明度.谁能告诉我这里缺少什么?

如果有人想测试它,这是有问题的代码:

#include "SDL/SDL.h"

const int WINDOW_WIDTH = 640;
const int WINDOW_HEIGHT = 480;
const char* WINDOW_TITLE = "SDL Start";

int main(int argc, char **argv)
{
   SDL_Init( SDL_INIT_VIDEO );

   SDL_Surface* screen = SDL_SetVideoMode( WINDOW_WIDTH, WINDOW_HEIGHT, 0, 
      SDL_HWSURFACE | SDL_DOUBLEBUF );
   SDL_WM_SetCaption( WINDOW_TITLE, 0 );

   SDL_Surface* bitmap = SDL_LoadBMP("resources/ship.bmp");
   if(SDL_SetColorKey( bitmap, SDL_SRCCOLORKEY, SDL_MapRGB(bitmap->format, 255, 0, 255) )) printf("aaaaa %s", SDL_GetError());



   // Part of the screen we want to draw the sprite to
   SDL_Rect destination;
   destination.x = 100;
   destination.y = 100;
   destination.w = 65;
   destination.h = 44;

   SDL_Event event;
   bool gameRunning = true;

   while (gameRunning)
   {
      if (SDL_PollEvent(&event))
      { 
         if (event.type == SDL_QUIT)
         {
            gameRunning = false;
         }
      }

      SDL_BlitSurface(bitmap, NULL, screen, &destination);

      SDL_Flip(screen);
   }

   SDL_FreeSurface(bitmap);

   SDL_Quit();

   return 0;
}
Run Code Online (Sandbox Code Playgroud)

更新:如果有人需要它,这里是位图:http://dl.dropbox.com/u/8936880/ship.bmp

Luc*_* S. 5

问题在于您的图像,我使用了由我生成的图像,它与您的代码开箱即用.

你的图像是32位,似乎SDL_SetColorKey不喜欢它,将它转换为24位,它应该工作.

当您从高级设置将其保存到BMP时,可以使用Gimp进行转换.

尝试将此转换为24位.