SDL_SetColorKey 的问题

发布于 2024-10-16 19:05:47 字数 1594 浏览 3 评论 0原文

我正在尝试使用 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

I'm trying to create a transparent sprite with SDL. I'm using SDL_SetColorKey on a bitmap with magenta (0xff00ff) background (it's 100% magenta, I checked it with the GIMP :)) The call to SDL_SetColorKey looks like this:

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

The call to SDL_SetColorKey apparently returns 0, however there is no transparency. Can anyone tell me what I am missing here?

Here is the problematic code in case anyone wants to test it:

#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;
}

UPDATE:
In case anyone needs it, here is the bitmap: http://dl.dropbox.com/u/8936880/ship.bmp

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぽ尐不点ル 2024-10-23 19:05:47

问题出在你的图像上,我使用了我生成的图像,它可以与你的代码一起开箱即用。

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

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

尝试将这个转换为24位。

The problem is with your image, i used one generated by me and it works out of the box with your code.

Your image is in 32 bits and it seems that SDL_SetColorKey doesn't like it, convert it to 24 bits and it should work.

You can convert it with Gimp when you save it to BMP from the advanced settings.

Try with this one converted to 24 bits.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文