Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Drawing darkness around player  (Read 3070 times)

0 Members and 1 Guest are viewing this topic.

Pikmeir

  • Newbie
  • *
  • Posts: 31
    • View Profile
Drawing darkness around player
« on: October 05, 2012, 03:40:08 am »


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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Drawing darkness around player
« Reply #1 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.
Laurent Gomila - SFML developer

Pikmeir

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: Drawing darkness around player
« Reply #2 on: October 05, 2012, 07:54:32 am »
Thank you! That's exactly what I was hoping for!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Drawing darkness around player
« Reply #3 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Pikmeir

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: Drawing darkness around player
« Reply #4 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.

Pikmeir

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: Drawing darkness around player
« Reply #5 on: October 06, 2012, 02:03:15 am »


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