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

Author Topic: Creae a simple image manager  (Read 6136 times)

0 Members and 1 Guest are viewing this topic.

Fred_FS

  • Newbie
  • *
  • Posts: 48
    • View Profile
Creae a simple image manager
« on: April 22, 2011, 07:44:19 pm »
Hello,
I decided to create a little tutorial on handling images, because often images and sprites are used together in the same object-class and so the image is often reloaded for all objects of the same type, what shouldn't be necessary.
You can read it here.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6272
  • Thor Developer
    • View Profile
    • Bromeon
Creae a simple image manager
« Reply #1 on: April 23, 2011, 07:20:00 pm »
Good tutorial! :)

I have written something similar (ResourceManager) in my library, but it doesn't provide much functionality specific to sf::Image. In your code, I like the possibility to specify multiple directories. I think an option to disable the debug outputs to std::cout would be handy, as well as the possibility to check whether image loading was successful. But you can also leave this as an exercise to the reader ;)

By the way, a mistake I came across: The following code doesn't compile because std::map::erase() doesn't return any iterator.
Code: [Select]
it = images_.erase( it );
Instead, you could write the code below (which is okay because map iterators remain valid for all operations except removal), however I think in that case you may also exit the function.
Code: [Select]
images_.erase(it++);
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Fred_FS

  • Newbie
  • *
  • Posts: 48
    • View Profile
Creae a simple image manager
« Reply #2 on: April 23, 2011, 08:23:29 pm »
Thank you :).
I had a look on your Resource Manager yesterday. I think, this would be the next step. But my tutorial is for beginners and a look at your code tells me that it is not "beginner's code" ;).
Those options, you mentioned, are not implemented, because I think that the reader will be also a programmer and he might be able to set some "//" int front of the debug output(or just don't copy it^^). It isn't a real debug output anyway. It just shows you, that the image is not loaded again and again.

A strange mistake. This[/cpp] also tells me that there is no erase-function, which returns an iterator, but my Visual C++ compiler has one ;). So it works for me.
But of course I fixed it. And you're right. It won't be necesseray to search the whole map, if I find and appropriate entry, and I should leave it.
So it should be enough to use this, shouldn't it?
Code: [Select]

images_.erase(it)
return;

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6272
  • Thor Developer
    • View Profile
    • Bromeon
Creae a simple image manager
« Reply #3 on: April 23, 2011, 08:29:27 pm »
Quote from: "Fred_FS"
I had a look on your Resource Manager yesterday. I think, this would be the next step. But my tutorial is for beginners and a look at your code tells me that it is not "beginner's code" ;).
In fact, my code sometimes looks more complicated than necessary, especially the metaprogramming stuff ;)

But I think our goals are different anyway: You teach people how to write a smart image managing class, while I try to provide something more or less ready to use for a specific purpose.


Quote from: "Fred_FS"
So it should be enough to use this, shouldn't it?
Code: [Select]

images_.erase(it)
return;
Yes. Now you can also drop the else branch and move the ++it to the loop header.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Fouf

  • Newbie
  • *
  • Posts: 23
    • View Profile
Creae a simple image manager
« Reply #4 on: June 02, 2011, 03:49:42 pm »
You can use the map.find function map.find msdn instead of looping through with an iterator.

guess I can't do links like that :/ oh well.

http://msdn.microsoft.com/en-us/library/92cwhskb(VS.80).aspx
- Fouf

denismr

  • Newbie
  • *
  • Posts: 13
    • View Profile
Creae a simple image manager
« Reply #5 on: August 26, 2011, 03:02:17 am »
Quote from: "Fouf"
You can use the map.find function map.find msdn instead of looping through with an iterator.

guess I can't do links like that :/ oh well.

http://msdn.microsoft.com/en-us/library/92cwhskb(VS.80).aspx


It's very recommended to use the find function instead of looping through with an iterator because the map is an implementation of a RedBlack Tree or an AVL Tree.
It means that a find operation is O( log2 (N) ) while passing over the entire map is O(N), N = number of items in the map.

Good job!   :)

 

anything