SFML community forums

Help => Graphics => Topic started by: NPS on January 18, 2013, 02:17:27 pm

Title: Parallax background with a couple of images
Post by: NPS on January 18, 2013, 02:17:27 pm
I've implemented parallax background which arbitrary number of images that slide at different speeds and loop. I'm using setTextureRect() for that of course and since when the images loop I have to draw 2 parts of the same image I also use setPosition().

Everything works great but I thought it'd be nice to be able to rotate and scale my parallax. When I do this right now everything breaks and gets put apart. Of course, I could implement calculating new positions for different angles and scales but it would be much simpler and cleaner if I could create the parallax frame (with scale 1.0 and no rotation), copy it to a new image and then rotate and scale that image.

My 2 questions - would it be fast (pixel operations like to be very slow) and how to do that (although I might probably google that so I'm most interested in the first question)?
Title: Re: Parallax background with a couple of images
Post by: eXpl0it3r on January 18, 2013, 02:30:15 pm
With SFML 2 you can simply draw you background to a RenderTexture, extract the texture and draw it with any transformation you want. ;)
Title: Re: Parallax background with a couple of images
Post by: NPS on January 18, 2013, 02:32:17 pm
I'm assuming it's fast?

Btw finally a way to render to texture in an easy and simple way. :D
Title: Re: Parallax background with a couple of images
Post by: eXpl0it3r on January 18, 2013, 02:41:32 pm
'Fast' is an extremely relative term. It all depends on what you're actually doing.
In theory it's not very different than drawing to a window, thus if you draw to the render texture every frame and then draw the rendered texture again you're obviously slower than when directly drawing the objects to the window. But if you now would need to do some heavy calculations just to get the same result with direct drawing and the overhead of the calculations would kill the performance, it's probably better/easier to draw to the render texture.

Btw: If you care about performance you should start using sf::VertexArray (or std::vector<sf::Vertex>) instead of sprites. The boost you get is incredible, but the scenery might be a bit hard to work with.
Title: Re: Parallax background with a couple of images
Post by: masskiller on January 18, 2013, 03:00:44 pm
Quote
Btw: If you care about performance you should start using sf::VertexArray (or std::vector<sf::Vertex>) instead of sprites. The boost you get is incredible, but the scenery might be a bit hard to work with.

Depending on what you may need you can use a class in the wiki for that, mainly the tile-map classes if I understood well what you want.
Title: Re: Parallax background with a couple of images
Post by: NPS on January 18, 2013, 03:10:40 pm
Well, in my case I just need a few (tens?) images and some particles to run on pc at 60 fps, so I hope (and think) sf::Sprite will be enough. Thx.
Title: Re: Parallax background with a couple of images
Post by: masskiller on January 18, 2013, 03:14:15 pm
Then you'll have no problems, FPS tends to lower once the amount of sprites goes beyond thousands (depending on your specs of course).
Title: Re: Parallax background with a couple of images
Post by: NPS on January 20, 2013, 12:23:00 am
Ok, I have a follow up question. It doesn't concern parallax background as it's usually rectangle but what if I want to draw transparent stuff to sf::RenderTexture and then that in window? Doesn't sf::RenderTexture require me to clear it with a fixed color? Is there a way not to clear it/clear it with "transparent color"? :P

(I see there's something called sf::Color::Transparent but it's description says "transparent (black)" so it is really transparent and does it apply here?)

Edit: Ok, tests suggest sf::Color::Transparent is what I need. As for "transparent (black)", my guess is it's always transparent unless something can't be transparent (like window), then it's black. Can anyone confirm?
Title: Re: Parallax background with a couple of images
Post by: eXpl0it3r on January 20, 2013, 12:52:55 am
There is no such things as a transparent-only color, but every color has an alpha channel that defines it's transparency. sf::Color::Transparent returns sf::Color(0, 0, 0, 0), thus it's a black color with full transparency. If the alpha channels gets ignored (e.g. with the window), then it will just be black.
Title: Re: Parallax background with a couple of images
Post by: NPS on January 20, 2013, 12:58:51 am
I know, that's what I mean, that was a mental leap. :P Thx for confirmation anyway.