Skip to main content
gamedev.net
🔒 Locked

SDL - transparency?

Started by RobotGymnast 3 replies 1.8k views
Original Post
RobotGymnast
RobotGymnast
I've tried setting up a color key, but it doesn't seem to work. I'm not using sprites of any kind, just drawing rectangles to a surface. Here's what I have so far:

Set-up code
Screen::Screen(unsigned int _width, unsigned int _height) :	width(_width), height(_height), bgColor(0, 0, 0){	// "screen" is of type SDL_Surface*	screen = SDL_SetVideoMode(width, height, 0, SDL_ANYFORMAT | SDL_SWSURFACE);	if(screen == NULL)		logger.Fatal(boost::format("Error creating screen: %1%") % SDL_GetError());	if(SDL_SetColorKey(screen, SDL_SRCCOLORKEY, transparent.GetRGBMap(screen)) != 0)		logger.Fatal("Error setting color key");}


The constructor for the Screen object initializes the SDL_Surface pointer, and attempts to make transparency work, logging any errors in the process.

Drawing code:
void WorldObject::Draw(Screen& target) const{	SDL_Surface* surface = target.GetSurface();	SDL_Rect rect;	rect.w = bounds.max.x - bounds.min.x;	rect.h = bounds.max.y - bounds.min.y;	rect.x = bounds.min.x;	rect.y = bounds.min.y;	if(SDL_FillRect(surface, ▭, color.GetRGBMap(surface)) != 0)	{		string error = "Error drawing to screen: ";		error += SDL_GetError();		logger.Fatal(error.c_str());	}}


It gets the SDL_Surface from the Screen object, then creates a rectangle to draw, and draws it (logging an error if things don't work).

Any ideas?

Thanks/
RobotGymnast
RobotGymnast
I'm only drawing rectangles - they're not loaded as surfaces.
Can I not make simple rectangles transparent?
Kylotan
Kylotan
A colourkey specifies which parts of a multi-coloured rectangular image you don't want to blit. By definition that can't apply to a single-coloured rectangle.

Could you describe precisely what you're hoping to achieve? (Also, what does 'color.GetRGBMap' do?)
RobotGymnast
RobotGymnast
Color::GetRGBMap just calls SDL_MapRGB.

I'm just trying to draw a transparent rectangle.
I realize now that that only makes sense in my current design, which implies my design is ridiculous.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.