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

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

0 Members and 1 Guest are viewing this topic.

pighead10

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
    • Hog Pog
Drawing darkness around player
« on: October 15, 2012, 09:58:25 pm »
I have read a thread about this: http://en.sfml-dev.org/forums/index.php?topic=9320.0 but, even after looking at the example, I am unable to add this into my game.

I want a circle of just transparency - no white borders, or any pretty effects - to follow the player. The darkness layer will not always be present, as it needs to slowly fade in/out, so the rectangle which the circle is drawn on needs to be black and the same opacity as the darkness layer.

Here is what I have so far:

//class Darkness
sf::RenderTexture m_Layer;
        sf::RenderTexture m_lighttexture;
        sf::Sprite m_Sprite;
        sf::Sprite m_light;

/////
m_Layer.create(view->getSize().x,view->getSize().y);
        init_light();

void Darkness::init_light(){
        m_lighttexture.create(100,100);

        m_lighttexture.clear();

        sf::CircleShape temp(50);
        temp.setOrigin(50,50);
        temp.setFillColor(sf::Color(255,255,255,0));
        temp.setPosition(sf::Vector2f(50.f, 50.f));
        m_lighttexture.draw(temp, sf::BlendNone);

        m_light.setTexture(m_lighttexture.getTexture(),true);
        m_light.setOrigin(50,50);
}

and

void Darkness::render(){
        m_Layer.clear(sf::Color(0,0,0,opacity));
        if(displaylight){
                m_Layer.draw(m_light,sf::BlendNone);
        }
        m_Layer.display();

        m_Sprite.setTexture(m_Layer.getTexture());
        Game::window->draw(m_Sprite);
}

What happens is there is a black rectangle on top of the player at all times - not transparent.
« Last Edit: October 15, 2012, 10:40:33 pm by pighead10 »
Immortui - Zombie puzzle game: http://immortui.hogpog.co.uk

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: Drawing darkness around player
« Reply #1 on: October 15, 2012, 10:18:26 pm »
If you don't want the nice effect, just draw one single sf::CircleShape onto the sf::RenderTexture instead of many, i.e. replace the generateSpot() function from my example.

If you have trouble you should tell use where exactly and not just post the code and let us search where things go wrong... ;)

Btw your code-tag formatting is broken.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

pighead10

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
    • Hog Pog
Re: Drawing darkness around player
« Reply #2 on: October 15, 2012, 10:42:56 pm »
If you don't want the nice effect, just draw one single sf::CircleShape onto the sf::RenderTexture instead of many, i.e. replace the generateSpot() function from my example.

If you have trouble you should tell use where exactly and not just post the code and let us search where things go wrong... ;)

Btw your code-tag formatting is broken.

Formatting fixed. Sorry, I thought made it clear - in the init_light function, I drew the single circle like you suggested but it comes out black like I said.
Immortui - Zombie puzzle game: http://immortui.hogpog.co.uk

pighead10

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
    • Hog Pog
Re: Drawing darkness around player
« Reply #3 on: October 16, 2012, 07:56:32 pm »
Hmm. For some reason changing opacity from 0 to 1 fixes the problem... However, I still have an issue: when the game is not dark but the player-light is there, there are black parts where the rectangle surrounds the circle, which is an issue as i'm moving around. Is there any way of fixing this?
Immortui - Zombie puzzle game: http://immortui.hogpog.co.uk

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: Drawing darkness around player
« Reply #4 on: October 16, 2012, 10:38:02 pm »
Hmm. For some reason changing opacity from 0 to 1 fixes the problem...
Yeah that's a bug in older SFML 2 version, Laurent fixed it and with the newest source you'd be able to use full transparency.

However, I still have an issue: when the game is not dark but the player-light is there, there are black parts where the rectangle surrounds the circle, which is an issue as i'm moving around. Is there any way of fixing this?
I don't understand the issue... "black parts where the rectangle surrounds the circle" what? ???
Make sure you draw in the right order...
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

pighead10

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
    • Hog Pog
Re: Drawing darkness around player
« Reply #5 on: October 16, 2012, 11:40:57 pm »
I'm drawing my circle to be transparent, and the texture I am drawing is filled in black. So the circle is transparenct by the areas outside of the circle but still inside the texture is black.
Immortui - Zombie puzzle game: http://immortui.hogpog.co.uk

Vovosunt

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Re: Drawing darkness around player
« Reply #6 on: October 18, 2012, 01:19:53 pm »
Set Blendmode to BlendNone

pighead10

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
    • Hog Pog
Re: Drawing darkness around player
« Reply #7 on: October 19, 2012, 06:58:32 pm »
Set Blendmode to BlendNone

I have done

m_lighttexture.create(200,200);

        m_lighttexture.clear();

        sf::CircleShape temp(100);
        temp.setOrigin(100,100);
        temp.setFillColor(sf::Color(255,255,255,1));
        temp.setPosition(sf::Vector2f(100.f, 100.f));
        m_lighttexture.draw(temp, sf::BlendNone);
Immortui - Zombie puzzle game: http://immortui.hogpog.co.uk

Vovosunt

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Re: Drawing darkness around player
« Reply #8 on: October 19, 2012, 07:49:15 pm »
Oh, sorry, didn't notice.
I'm pretty sure fully Transparent shapes don't render.
Try using an already prepared sprite of alternatively very low alpha (0,0,0,1) or a vertexArray.

 

anything