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

Author Topic: Can I reload an image to an sf::Image object?  (Read 2698 times)

0 Members and 1 Guest are viewing this topic.

fun2code

  • Newbie
  • *
  • Posts: 14
    • View Profile
Can I reload an image to an sf::Image object?
« on: February 27, 2011, 08:26:00 am »
I am working on a 2D game where the world area is large. I am thinking of using a 2D array of sf:Image objects to store portions of the background to display (tiles?).

I want to load enough images to cover the screen + an extra row above and below and an extra column to the left and right. When the view is scrolled into these bounding rows of images I would like to load another row into the Image objects left behind (then cycle the sprite positions to the front). Can I simply call LoadImageFromFile() again? The tiles are always the same size (128x128 pixel jpg images).

I find that this works but is the memory taken by the image previously loaded released when I do this?

What is a good method for maintaining an "island" of images in memory within the vicinity of the current view?

I recently started using SFML and am impressed with its ease of use, but I could not find these topics addressed in the tutorials.

Thanks.
-fun2code

XDest

  • Newbie
  • *
  • Posts: 8
    • View Profile
Can I reload an image to an sf::Image object?
« Reply #1 on: February 27, 2011, 11:10:53 am »
LoadFromFile is costly, and should never be done more than once for the same image (unless it's no longer in memory), and certainly not every frame.

What you want is to do is pass around references/pointers of sf::Image objects. Create an image manager class to check for duplicates of filenames and the such, and return a reference based on that to use with sf::Sprite. And if it's the first time you're asking for an sf::Image of that filename, return a reference to a new one. Therefore, things are reused automatically and quickly, they're still in memory after all, until you choose to get rid of them. A std::map makes things like this pretty easy to implement I think.

As well, you can optimize things further by just prerendering your tiles into one big sf::Image using sf::RenderImage (in 2.0) before the game loop, because drawing one object every frame is more efficient than drawing several hundred/thousand.
http://www.sfml-dev.org/documentation/2.0/classsf_1_1RenderImage.htm

fun2code

  • Newbie
  • *
  • Posts: 14
    • View Profile
Can I reload an image to an sf::Image object?
« Reply #2 on: February 28, 2011, 06:49:36 am »
Thanks for the reply XDest. Those are some good ideas to go with.
I've started on an image manager class and will keep it simple as I experiment with it.

I'll have to stick with methods available in v1.6 since that's what I installed. I'll learn the basics with that before moving to v2.0
-fun2code