Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

#ActualBeerNutts

Posted 21 August 2012 - 12:25 PM

IMO, the easiest way to solve this is to don't even worry about classes.  You can use basic C-style functions to load your images and return the pointers to them in a generic Utility.cpp file, like this:

// in Utility.h define this
SDL_Surface GetImage(std::string imageName);

// in Utility.cpp, do this
std::map<std::string, SDL_Surface*> ImageMap;

SDL_Surface *GetImage(std::string imageName)
{
  // check if this image is in the map
  if (ImageMap.find(imageName) == std::map::end) {
	// load it once into the map
	ImageMap[imageName] = IMG_Load(imageName);
  }
  return ImageMap[imageName];
}

// Then, when you want to load an image, #include "Utility.h" and call
SDL_Surface *PlayerImage = GetImage("player.png");
You can put Utility into a Utility namespace if you want, but that's just another option.
Good Luck.

#1BeerNutts

Posted 21 August 2012 - 12:23 PM

IMO, the easiest way to solve this is to don't even worry about classes.  You can use basic C-style functions to load your images and return the pointers to them in a generic Utility.cpp file, like this:

// in Utility.h define this
SDL_Surface GetImage(std::string imageName);

// in Utility.cpp, do this
std::map<std::string, SDL_Surface*> ImageMap;

SDL_Surface *GetImage(std::string imageName)
{
  // check if this image is in the map
  if (ImageMap.find(imageName) == std::map::end) {
    // load it once into the map
    ImageMap[imageName] = IMG_Load(imageName);
  }
  return ImageMap[imageName];
}

Good Luck.

PARTNERS