Quote Originally Posted by sarvsav View Post
HI Alpo,

I have a doubt here, as I am loading all images and sounds in Game class, so, what is the need of extra class for Image, Sound.? Because no other class is going to use that class Image, which is responsible for loading/rendering images, and same for sound?

There are 2 cases here:

1) Game class load everything: player image, map image, game sounds.
2) Game class call Player class, Player class then load player image/sound using Image/Sound class, Enemy class load enemy image/sound.

2nd case is undoubtedly good as per structure, but will take lots of extra effort. and as per performance, is first case better than second? because there is a single inheritance, whereas in second case, every class need to inherit Sound and Image class? Kindly help.
Hello, and sorry it has taken so long to reply, I'm still pretty busy with my engine.

To clarify, I was suggesting making a sort of manager class for images (I'll just stick with images, but it applies to other media, and fonts as well).

The way I have mine set up, the Game class only loads a state (The main menu state) in it's initialization function (after initializing SDL and all that). A state is responsible for loading (and keeping track of) it's own media, which it does using the "Texture" class.

In general terms, my system works like this:

1. Game class is created in main, then the Game::Init function is called with all the parameters needed to create the window and renderer.

2. At the end of that function, a class called MainMenu is created and pushed into a vector of GameStates (a base class for all the states in the game). When a state is created, it executes it "Enter()" method.

3. In the GameState::Enter() a class called Parser is created which uses tinyXML to get parameters from an XML file to create the images. The Parser will use the Texture singleton to store SDL_Textures in a std::map in the Texture class. The map is then accessed through it's key, which is stored in the character classes (or whatever class needs rendering).

The reason I prefer (at least so far to use a separate manager class is that not only does it isolate a lot of SDL specific code in one place, it also gives you a greater range of how you will use those SDL Functions, and takes some of the responsibility for keeping track of things.

Maybe a good example is special effects (like getting hit with a spell). When a character is hit with a spell, the spell class will pass it's "type" to one of the collision functions in that characters class. This collision function will then (among other things) start a timer for the effect, then pass the "type" on to the Texture class, which will then automatically apply the appropriate effect, and reset the SDL_BlendMode and render Color.

I suppose you could just write all the stuff my Texture class does in the base class of your inheritance chart for characters, but trust me, those classes will probably grow big and gnarly enough. And if you have classes that aren't in the character hierarchy and need to be rendered (possibly with special needs), you wouldn't have access to the code without copying into the new class.

It's up to you, you'll find your preferred way after a while (hopefully without to many headaches). Good luck!