SDL_SetColorKey的问题

时间:2011-02-11 20:11:14

标签: 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;
}

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

1 个答案:

答案 0 :(得分:5)

问题在于您的图片,我使用的是我生成的图片,它与您的代码一起开箱即用。

您的图像是32位,似乎SDL_SetColorKey不喜欢它,将其转换为24位,它应该可以工作。

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

尝试将this one转换为24位。