SFML community forums

Help => Graphics => Topic started by: pighead10 on October 15, 2012, 09:58:25 pm

Title: Drawing darkness around player
Post by: pighead10 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.
Title: Re: Drawing darkness around player
Post by: eXpl0it3r 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 (https://github.com/eXpl0it3r/Examples/blob/master/SFML/Flashlight.cpp).

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.
Title: Re: Drawing darkness around player
Post by: pighead10 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 (https://github.com/eXpl0it3r/Examples/blob/master/SFML/Flashlight.cpp).

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.
Title: Re: Drawing darkness around player
Post by: pighead10 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?
Title: Re: Drawing darkness around player
Post by: eXpl0it3r 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...
Title: Re: Drawing darkness around player
Post by: pighead10 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.
Title: Re: Drawing darkness around player
Post by: Vovosunt on October 18, 2012, 01:19:53 pm
Set Blendmode to BlendNone
Title: Re: Drawing darkness around player
Post by: pighead10 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);
Title: Re: Drawing darkness around player
Post by: Vovosunt 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.