Recommended Posts

I'm doing a cave adventure game and I'm in a need of help. What I want to do, is to only show some of the screen and have the rest filled with black. Like if the player is carrying a candle or some other lightsource, he can see a circle kind of area around him, but nothing else. How would I do this? The game is going to have Zelda style camera. I'm using C++ and SDL. I'm not too familiar with SDL so I would appreciate code examples and possible detailed commenting. Thanks in advance.

0

Share this post

Link to post

Share on other sites

Are you using SDL's software blitting functionality for your graphics?

I'm just speculating there, but here's what first comes to mind. First, clear the entire screen to black (or, you could use some sort of 'dirty rect' system to clear areas of the screen only as needed). Then, render the parts of the world that are potentially visible; if the game is tile based, for example, you would render only the tiles that intersect the 'light circle'. Finally, render a black circle with an inverse alpha gradient (255 at the edges moving towards 0 at the center) using alpha blending, centered at the player's position. Note that you may have to do some tweaking to make sure the parts of the world that are rendered don't 'stick out from under' the 'shadow circle'.

0

Share this post

Link to post

Share on other sites

I did this a while back with SDL and what I did was have an off screen surface the same size as the screen that I used as a light overlay.

I started by using SDL_FillRect to fill the light overlay with black. I then had light surfaces (of varying size) that were a color key color dithered out in a circle that I would blit onto the light overlay wherever a light should be on the screen (or slightly off). Once all of the sprites were rendered I would blit the light overlay on top.

This worked for me because there were usually more than one light in the room and the resolutions were relatively low - nothing above 1024x768. I also avoided alpha blending which saved me a bunch of CPU and it came out acceptable for me although alpha blending does make it look nicer and can give you the halos you see around light in most games.