PDA

View Full Version : SDL and masks


Pancho
2003.12.09, 03:27 AM
Hello everyone,

I am in charge of porting a game from MAc OS to Windows and to Linux. I thought that the easiest way to do this would be rewriting it in SDL. However, I have a smal problem. The graphics for the Macintosh version were given in the Macintosh standard way of doing sprites, i.e. a color stencil, and its black and white mask. From all the examples and code I've found I can see that the way they do sprites in windows and in SDL is by painting the background of the image in a different color.

My question is, is it possible to do sprites in SDL using a colour image and its respective black and white mask ? If so, how would you do it?

Thank you,

Pancho

Entropy
2003.12.09, 07:32 PM
You could just convert your sprites to a format that handles transparency, and let SDL do handle the rest:

Here is a snippet of the SpriteEngine that I am building, using pure SDL for the drawing. CMSprite is a class I built that has a SDL_Surface for a sprite image, along with other data. I also have 2 buffers, one backbuffer that I flip to the main drawing buffer after drawing. SDL handles all the trans of my .png's fine, using SDL_image.

void CMSpriteEngine::renderScene(void)
{
CMSprite* cur_sprite;
std::vector<CMSprite *>::iterator sprite_iter;

t_current = SDL_GetTicks();

SDL_BlitSurface(bgStorageSurface, NULL, backBuffer, NULL);
for (sprite_iter = _spriteVector.begin(); sprite_iter != _spriteVector.end(); sprite_iter++)
{
cur_sprite = *sprite_iter;
SDL_BlitSurface(cur_sprite->getSurface(), cur_sprite->getFrameRect(), backBuffer, cur_sprite->getDstRect());
cur_sprite->setLastRender();
}

SDL_BlitSurface(backBuffer, 0, mainSurface, 0);
SDL_Flip(mainSurface);
t_previous = t_current;
}


Edit: i thought you were porting to OS X. Sorry.

Taxxodium
2003.12.09, 11:31 PM
I thought about this and the only solution I came up is writing your own Mask blitter.

Basically you would have to SDL_Surfaces, one for the color graphic and a second for the black and white mask.

Now, you should check for every pixel in the mask surface and draw every pixel that isn't black (ie, rgb(0, 0, 0))

It's probably a lot of work, but once you've written it you can reuse it.

w_reade
2003.12.10, 05:16 AM
Use the mask to draw a coloured background onto the graphic when it's in memory?

Skorche
2003.12.12, 04:47 AM
I'd go with w_reade's approach, just mask on the transparent color when loading. If possible, just change the original graphics themselves.