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

Author Topic: Questions about blending  (Read 3097 times)

0 Members and 1 Guest are viewing this topic.

Ace (SWE)

  • Newbie
  • *
  • Posts: 5
    • View Profile
Questions about blending
« on: November 04, 2012, 05:19:52 pm »
So I've been working on a project, a 2D game with a minimap. And after using a simple square for a while I decided to fix it up to work as a circle instead.
My first tries was simply using sprite blending and I never managed to get that working the way it should've, all I ended up with was a darker version of the minimap that was outside of the circle.
In the end I chose to have a rendertexture for the minimap, where I draw the circle first as white on top of a transparent background and then blend the minimap sprite with that. And while it works, it also takes up well over 50% of each frame. Even implementing a simple caching system that only redraws it when needed still makes it a slow solution.

I was hoping to have the ability to clear the alpha mask after drawing the main scene and then set up the minimap using alpha blending instead to speed it up a bit, so far I've yet to find a way of doing that on SFML 2 though.
Is there a way to do this or should I just bite the bullet and keep using the render texture method, even though it's slow and didn't even work on my laptop up until very recently. (Intel graphics)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: Questions about blending
« Reply #1 on: November 05, 2012, 01:06:46 am »
Could you describe a bit more detailed what you actually want to achieve, because the current description is a bit cryptic to me and when you talk about blending, you have to specify what kind of blending.
If you want the the map to be semi-transparent, then you could simply set a color of the sprite with a alpha channel smaller than 255, while keeping the rest to full white (i.e. sf::Color(255, 255, 255, 127)).
If you want to 'replace' the part underneath the sprite/circle you should use sf::BlendNone...
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Ace (SWE)

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Questions about blending
« Reply #2 on: November 05, 2012, 03:11:39 am »
So, I have a minimap, which looks like this at the moment:


It's made out of three other images; the level itself, the mask defining which part of it should draw, and the border.


Currently it's being drawn inside of a render texture every time it changes, the result of which is then used to draw the minimap itself. The drawing looks something like this:
void UI::drawMap(sf::RenderTarget& target, const sf::IntRect& pos, bool redraw) const
{
    if (redraw)
    {
        tex.clear(sf::Color::Transparent);

        sf::Sprite mask(mapMask);
        tex.draw(mask, sf::RenderStates(sf::BlendAdd));
   
        sf::Sprite mini(levelTexture, pos);
        mini.setScale(175.f/pos.width, 175.f/pos.height);
        tex.draw(mini, sf::RenderStates(sf::BlendMultiply));

        tex.display();
    }

    sf::Sprite map(tex.getTexture());

    map.setPosition(target.getView().getSize().x - tex.getSize().x - 12.f, 12.f);

    target.draw(map);
}

This works for now. Though seeing as I have done similar things inside of OpenGL applications without having to use a render texture, I'd rather change the rendering into something that is a bit friendlier to computers that don't take them so well.
Like the laptop I'm developing it on. (Right now, around 8-12ms is spent redrawing the minimap every time it changes. And more ui parts that need blending in a way like this are coming)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Questions about blending
« Reply #3 on: November 05, 2012, 08:05:03 am »
Quote
Right now, around 8-12ms is spent redrawing the minimap every time it changes.
But what is slow exactly? Drawing the map, or drawing it to a render-texture?
Normally, using a render-texture should only have a small impact on performances.

Anyway, for a more direct solution, see tracker issue #1.
Laurent Gomila - SFML developer

Ace (SWE)

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Questions about blending
« Reply #4 on: November 05, 2012, 09:25:22 am »
Quote
Right now, around 8-12ms is spent redrawing the minimap every time it changes.
But what is slow exactly? Drawing the map, or drawing it to a render-texture?
Normally, using a render-texture should only have a small impact on performances.

Anyway, for a more direct solution, see tracker issue #1.

Seeing as the 8-12ms only happen when the redraw bool is true, I'm going to say drawing it to the render-texture is what takes the time. Which honestly doesn't surprise me much, the laptop doesn't have hardware support for them or anything at all really. (I don't think it even has hardware T&L, come to think of it)

And that issue does sound exactly like what I want, though it isn't completed and the last comment was two years ago. For now I think I'll just try my hand at using pure OpenGL to solve the issue.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Questions about blending
« Reply #5 on: November 05, 2012, 09:31:25 am »
Quote
Seeing as the 8-12ms only happen when the redraw bool is true, I'm going to say drawing it to the render-texture is what takes the time.
So what takes time is redrawing the minimap, not doing it specifically to a render-texture. So I don't understand what your problem is, because you'll have to draw it anyway, whatever technique you use on top of it to create the circle mask. So it will always be that slow.

Quote
Which honestly doesn't surprise me much, the laptop doesn't have hardware support for them or anything at all really
But it works, so it somehow supports it ;)
And if it's supported, it should be as fast as drawing directly to a window.

And yes, render masks are not implemented yet, they are planned for SFML 2.1 or later. Using OpenGL will probably work, but be careful when you mis it with SFML.
Laurent Gomila - SFML developer

Ace (SWE)

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Questions about blending
« Reply #6 on: November 05, 2012, 11:09:54 pm »
So what takes time is redrawing the minimap, not doing it specifically to a render-texture. So I don't understand what your problem is, because you'll have to draw it anyway, whatever technique you use on top of it to create the circle mask. So it will always be that slow.

Well, normally I would agree with you, but when I remove the render-texture part of the code and draw the exact same things directly to the screen I don't lose a single ms in the function. Though it does lose a bit of aesthetics, read 'horribly ugly'. (Don't expect you to be able to do much about it though, the 945GM isn't exactly powerful)

At least it's a good reason to optimize my code, otherwise it won't even be playable on my own laptop.
I'll just have a look at doing it the OpenGL way, stuffing a few push/popGLStates in there, and seeing if the rendering time improves.