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

Author Topic: Best way to load the same image multiple times  (Read 11682 times)

0 Members and 1 Guest are viewing this topic.

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Best way to load the same image multiple times
« Reply #15 on: November 22, 2010, 05:17:28 pm »
Quote from: "Contadotempo"
Just one quick question, sorry for bumping the topic again:
If I wanted to destroy the manager completely, would using map::clean() on the destructor be enough? I'm very afraid of memory leaks.


The map destructs it self when the destructor finishes. But if you have the resources in std::map HEAP allocated with new then you'll have to loop trough the map and free them yourselves with delete. Though if you followed my small example then you shouldn't need to worry about it. Since like I said, std::map will clean itself up when it's destructor is called which is called when your destructor finishes.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Best way to load the same image multiple times
« Reply #16 on: November 22, 2010, 08:36:42 pm »
As Groovy said, if you use new use delete before emptying the map. When you have deleted the value for a certain key you can call myMap.erase(key). Or you can deleted all the resources and call myMap.clear();
SFML / OS X developer

Contadotempo

  • Full Member
  • ***
  • Posts: 167
  • Firelink Shrine
    • View Profile
Best way to load the same image multiple times
« Reply #17 on: November 22, 2010, 09:26:09 pm »
That's actually something I've been wondering.
What's the difference between allocating an image with new on "map storage<string, sf::Image*> or allocating it automatically on "map storage<string, sf::Image>"?
Sorry, I know it's a beginner's question  :(

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Best way to load the same image multiple times
« Reply #18 on: November 22, 2010, 09:55:51 pm »
My recommendation is pretty simple : avoid pointer as often as possible.

Anyway. We use T* if we want to store some T or some derivate of T. That way we can use full OO programming power : polymorphism.

Here, with sf::Image, you don't need to store pointers and use new/delete. It would have no benefit.
SFML / OS X developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Best way to load the same image multiple times
« Reply #19 on: November 22, 2010, 10:06:05 pm »
Quote from: "Hiura"
My recommendation is pretty simple : avoid pointer as often as possible.
I wouldn't say pointers, rather manual memory management. This automatically reduces the need to use pointers at every corner. ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Contadotempo

  • Full Member
  • ***
  • Posts: 167
  • Firelink Shrine
    • View Profile
Best way to load the same image multiple times
« Reply #20 on: November 22, 2010, 10:14:09 pm »
Ah I understand.
I don't work with polymorphism very well yet so I'll avoid pointers for now, and keep practising.

Thanks for all the help good sirs  :wink:

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Best way to load the same image multiple times
« Reply #21 on: November 22, 2010, 10:45:56 pm »
@Contadotempo, I strongly recommend you to read some stuff about it : you'll really enjoy it! :)

@Nexus, well I also recommend to use as rarely as possible raw pointer (with/without manual memory management). References are easier to use. But I think I teach you nothing here.  :wink:
SFML / OS X developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Best way to load the same image multiple times
« Reply #22 on: November 22, 2010, 11:01:29 pm »
Quote from: "Hiura"
@Nexus, well I also recommend to use as rarely as possible raw pointer (with/without manual memory management). References are easier to use.
Yes, but references have their limitations (e.g. they are constant and can't be NULL). I also use references when appropriate, but often, I need pointers. I don't see a fundamental fault in doing so. I mean, pointers are mainly called "dangerous" because a lot of beginners don't get how they work and do scary things with them... :)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything