SFML community forums

Help => Graphics => Topic started by: Pikmeir on October 05, 2012, 03:40:08 am

Title: Drawing darkness around player
Post by: Pikmeir on October 05, 2012, 03:40:08 am
(http://i.imgur.com/DNRHe.jpg)

This is what I'm working on in my game. I want to have the player only able to see a square or so in front of them.

How I'm doing it is I just have a sprite that's all black except for the center, and I have it move along with the player's position. It works, until the sprite boundaries hit the window boundaries (since this sprite has to be rather large to stay black no matter where the player is on the map), and then the sprite doesn't display in this case.

This is all framed within the rest of the screen, so everything else is going on top of it to hide the edges of the "blackness."

I could only think of one option to fix this:

1) Make 4 differently shaped "blackness" sprites, and change between which is displayed depending on where the player is on the map (divide it into 4 quadrants). This way would work, but it would be a pain in the butt to do and take a long time to implement.

I wish there was some way I could just fill the map area of the screen with black, then remove a hole in the area of the sprite, and have that hole move around. Is there someway I can do that?

Or, is there a better way you think I could do this?

Thank you for your help.
Title: Re: Drawing darkness around player
Post by: Laurent on October 05, 2012, 07:39:47 am
Quote
I wish there was some way I could just fill the map area of the screen with black, then remove a hole in the area of the sprite, and have that hole move around. Is there someway I can do that?
Yes. Use a sf::RenderTexture (SFML 2), clear it with black, draw a transparent sprite that contains the "visibility" pattern with sf::BlendNone blending mode, and finaly draw the render-texture on top of the scene with a sprite.
Title: Re: Drawing darkness around player
Post by: Pikmeir on October 05, 2012, 07:54:32 am
Thank you! That's exactly what I was hoping for!
Title: Re: Drawing darkness around player
Post by: eXpl0it3r on October 05, 2012, 08:41:10 am
If you need an example you can take a look at the one I wrote a few days ago on GitHub (https://github.com/eXpl0it3r/Examples/blob/master/SFML/Flashlight.cpp).
Title: Re: Drawing darkness around player
Post by: Pikmeir on October 05, 2012, 09:04:38 am
Lol, I was just about to ask for a link to an example of that. Thanks I'll have a look at this.
Title: Re: Drawing darkness around player
Post by: Pikmeir on October 06, 2012, 02:03:15 am
(http://i.imgur.com/ArwEp.jpg)

Thank you, it works perfectly now. Your example code really helped also.