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

Author Topic: My small Sprite Manager  (Read 7720 times)

0 Members and 1 Guest are viewing this topic.

lfnunley

  • Newbie
  • *
  • Posts: 12
    • View Profile
My small Sprite Manager
« on: March 27, 2013, 08:28:10 pm »
I've been trying to make a lightweight template for myself using sprites and eventually physics. For now, I created this Sprite Manager:
class SpriteManager{
public:
  SpriteManager();
  ~SpriteManager();

  void         addImage(std::string file);
  sf::Image*   getImage(std::string file) { return Images.find(file)->second; }
  void         addSprite(std::string file, std::string id); // returns a pointer to created sprite
  sf::Sprite*  getSprite(std::string id)  { return Sprites.find(id)->second; };

  void update();

private:
  std::map<std::string, sf::Sprite*> Sprites; // id, sprite
  std::map<std::string, sf::Image*> Images; // file name, image
};

SpriteManager::SpriteManager(){
}

SpriteManager::~SpriteManager(){
  std::map<std::string, sf::Sprite*>::iterator it1 = Sprites.begin();
  while(it1 != Sprites.end())
    Sprites.erase(it1++);

  std::map<std::string, sf::Image*>::iterator it2 = Images.begin();
  while(it2 != Images.end())
    Images.erase(it2++);

}

void SpriteManager::addImage(std::string file){
        if(Images.find(file) == Images.end()){ // If image is not in map already.
                std::cout << "Adding image " << file << std::endl;
                sf::Image* image = new sf::Image();
                if(image->LoadFromFile(file))
                        Images.insert(std::pair<std::string, sf::Image*>(file, image));
                else
                        std::cout << "Image was unable to be loaded" << std::endl;
        }
        else{
                std::cout << "Image " << file << " was already loaded" << std::endl;
        }
}

void SpriteManager::addSprite(std::string file, std::string id){
  addImage(file);
  sf::Sprite* sprite = new sf::Sprite(*getImage(file));
  Sprites.insert(std::pair<std::string, sf::Sprite*>(id, sprite));
}

void SpriteManager::update(){
  for(std::map<std::string, sf::Sprite*>::iterator it = Sprites.begin();
      it != Sprites.end(); it++)
    g_App->Draw(*(*it).second);
}
 

This can be used by creating sprites like so:
  // Initialize the Sprite Manager
  g_SpriteManager = new SpriteManager();

  // Create the sprites
  g_SpriteManager->addImage("./res/Harlem.png");
  g_SpriteManager->addSprite("./res/Harlem.png", "Harlem");

  g_SpriteManager->addImage("./res/dot.png");
  g_SpriteManager->addSprite("./res/dot.png", "dot1");
  sf::Sprite* testSprite = g_SpriteManager->getSprite("dot1");
  testSprite->SetPosition(30.f, 53.f);

And then you call it by using:
g_SpriteManager->update();

I'd like to know what all of you think of this. Let me know if you have any critiques of my code  :D

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: My small Sprite Manager
« Reply #1 on: March 27, 2013, 08:35:33 pm »
Are you a Java programmer?
Oh, and 1.6 is outdated.
« Last Edit: March 27, 2013, 09:14:45 pm by FRex »
Back to C++ gamedev with SFML in May 2023

Gobbles

  • Full Member
  • ***
  • Posts: 132
    • View Profile
    • Email
Re: My small Sprite Manager
« Reply #2 on: March 27, 2013, 08:42:28 pm »
Are you a Java programmer?

I thought the exact same thing "He must be a Java programmer" :P

lfnunley

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: My small Sprite Manager
« Reply #3 on: March 27, 2013, 10:27:58 pm »
Nope, I hate java actually, why do you ask? And I know 1.6 is outdated, but I'm going to keep using it for a while.

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: My small Sprite Manager
« Reply #4 on: March 27, 2013, 10:33:41 pm »
Because you use new(like in Java) but never use delete(like in Java) so your class is leaking memory(in Java there's gc for that but not in c++).
Back to C++ gamedev with SFML in May 2023

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: My small Sprite Manager
« Reply #5 on: March 27, 2013, 10:33:58 pm »
why do you ask?
You use new all the time, without using delete. Apart from being wrong (memory leaks!), using new/delete is often unnecessary. Please take a look at this thread, there I mention the alternatives to manual memory management and give a lot of arguments explaining why they are better.

And choose the correct forum next time (Help/Graphics).
« Last Edit: March 27, 2013, 10:35:35 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

lfnunley

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: My small Sprite Manager
« Reply #6 on: March 27, 2013, 11:06:41 pm »
I use
new
to initialize many of the objects because they must be initialized before being used. If I try to do this:

sf::Image* image;// = new sf::Image();
                if(image->LoadFromFile(file))
                        Images.insert(std::pair<std::string, sf::Image*>(file, image));

The program crashes. Also, I don't delete pointers like that because they are being added to a map. Deleting them would cause the program to crash. The destructor for SpriteManager deletes all of the pointers in the maps. The SpriteManager and RenderWindow are deleted upon program close. If you don't believe me, I can upload the project file  :P.

Quote
And choose the correct forum next time (Help/Graphics).
Sorry, I thought that since this was a discussion-type post that it would go in General.

lfnunley

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: My small Sprite Manager
« Reply #7 on: March 27, 2013, 11:13:06 pm »
using new/delete is often unnecessary. Please take a look at this thread, there I mention the alternatives to manual memory management and give a lot of arguments explaining why they are better.

Would you suggest I look at Boost smart pointers? I've heard many good things about them. I'm just used to handling my pointers manually, I feel more in control.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11035
    • View Profile
    • development blog
    • Email
Re: My small Sprite Manager
« Reply #8 on: March 27, 2013, 11:18:31 pm »
Would you suggest I look at Boost smart pointers? I've heard many good things about them. I'm just used to handling my pointers manually, I feel more in control.
Or even better C++11 smart pointers. Just get a recent compiler (VS 2012 or GCC 4.7). ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: My small Sprite Manager
« Reply #9 on: March 27, 2013, 11:23:40 pm »
Or don't use pointers and manual memory management at all. Use automatic variables as much as possible.

sf::Image image;
image.LoadFromFile("...");
...

And you should really read a good book, this stuff is extremely important to know before writing any decent C++ program ;)
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: My small Sprite Manager
« Reply #10 on: March 27, 2013, 11:27:03 pm »
I'm just used to handling my pointers manually, I feel more in control.
Yes, and that's wrong. Especially for such simple use cases, you have the same amount of control with smart pointers, however they're safe.

But why do you need pointers at all? Why don't you store the sf::Sprite directly?


Or even better C++11 smart pointers. Just get a recent compiler (VS 2012 or GCC 4.7).
Indeed, std::unique_ptr covers most cases. However, VS 2010 or g++ 4.6 are enough.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11035
    • View Profile
    • development blog
    • Email
Re: My small Sprite Manager
« Reply #11 on: March 28, 2013, 12:06:06 am »
Indeed, std::unique_ptr covers most cases. However, VS 2010 or g++ 4.6 are enough.
Yes and no, it's mostly enough, but std::unique_ptr is broken in VS10 in certain cases. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: My small Sprite Manager
« Reply #12 on: March 28, 2013, 01:05:20 am »
std::unique_ptr is broken in VS10 in certain cases. ;)
Are these important cases? I don't exclude minor bugs, but I'm working with VS 2010 and std::unique_ptr all the time, and I have never had any problems.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: My small Sprite Manager
« Reply #13 on: March 28, 2013, 01:18:15 am »
A pointer is not 'an object' that needs 'initialization'.
Also,
SpriteManager::~SpriteManager(){
  std::map<std::string, sf::Sprite*>::iterator it1 = Sprites.begin();
  while(it1 != Sprites.end())
    Sprites.erase(it1++);

  std::map<std::string, sf::Image*>::iterator it2 = Images.begin();
  while(it2 != Images.end())
    Images.erase(it2++);

}
this just looks stupid and doesn't delete anything.
Back to C++ gamedev with SFML in May 2023

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11035
    • View Profile
    • development blog
    • Email
Re: My small Sprite Manager
« Reply #14 on: March 28, 2013, 01:55:51 am »
Are these important cases? I don't exclude minor bugs, but I'm working with VS 2010 and std::unique_ptr all the time, and I have never had any problems.
Completely depends on what you're doing, for me it was a reason to abandon VS 10. The following won't compile with VS10:

#include <memory> // unique_ptr
#include <vector>

class foo
{
        std::unique_ptr<int> bar;
};

int main()
{
        std::vector<foo> fail(1);
}

So basically you can't put a class/struct with a unique_ptr into a (STL) container...  :-\
« Last Edit: March 28, 2013, 01:57:56 am by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/