本文介绍了SDL_SetColorKey的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用SDL创建一个透明的sprite。我使用SDL_SetColorKey在洋红色(0xff00ff)背景的位图(它是100%洋红色,我检查它与GIMP :))调用SDL_SetColorKey看起来像这样:



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



调用SDL_SetColorKey显然返回0,但是没有透明度。任何人都可以告诉我我在这里失踪了什么?



这里是有问题的代码,以防任何人想测试它:

  #includeSDL / 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());



//我们要绘制精灵到
的部分屏幕SDL_Rect目的地;
destination.x = 100
destination.y = 100;
destination.w = 65;
destination.h = 44;

SDL_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
需要它,这里是位图:

解决方案

问题是你的形象,我使用了一个由我生成的,



您的映像是32位的,似乎SDL_SetColorKey不喜欢它,转换为24位,它应该工作。 / p>



您可以使用Gimp将其转换为BMP。 href =http://dl.dropbox.com/u/11552226/ship2.bmp =nofollow>这一个转换为24位。


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

解决方案

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.

这篇关于SDL_SetColorKey的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 16:47