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

Author Topic: Parallax background with a couple of images  (Read 4925 times)

0 Members and 1 Guest are viewing this topic.

NPS

  • Newbie
  • *
  • Posts: 31
    • View Profile
Parallax background with a couple of images
« 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)?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Parallax background with a couple of images
« Reply #1 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. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

NPS

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: Parallax background with a couple of images
« Reply #2 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

eXpl0it3r

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

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: Parallax background with a couple of images
« Reply #4 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.
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

NPS

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: Parallax background with a couple of images
« Reply #5 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.

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: Parallax background with a couple of images
« Reply #6 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).
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

NPS

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: Parallax background with a couple of images
« Reply #7 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?
« Last Edit: January 20, 2013, 12:48:08 am by NPS »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Parallax background with a couple of images
« Reply #8 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

NPS

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: Parallax background with a couple of images
« Reply #9 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.