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

Author Topic: Need advice on code structure  (Read 1597 times)

0 Members and 1 Guest are viewing this topic.

Kalnos

  • Newbie
  • *
  • Posts: 8
    • View Profile
Need advice on code structure
« on: June 20, 2010, 03:36:42 am »
Hey guys, I'm pretty new to the C++ / SFML scene and was wanting some advice from more experienced users on how to handle this.  I'm starting by writing a simple Tetris like game, and one thing i'm particularly stumped on is the way to handle images.  I've set up a class that manages images so that they're not loaded more than once by storing them in a map.  The problem, though, lies in where I should declare the instance of this imagecache in my code.

An example I saw on this site was a Singleton pattern like:

Code: [Select]

static ImageManager &get()
{
static ImageManager imgManager;
return imgManager;
}


I don't really want to use a method like this, but I don't really have any good idea of where the cache should be placed.  

I've thought of placing it in a generic Entity class in which the Blocks and Player, etc is derived from, but this seems like it would alienate the manager from things like background images and things that aren't derived from this class.  Declaring it in the main loop just seems hectic as it requires me to pass a reference to the manager like Game --> Board --> Block --> etc just to set a sprites image, which seems like poor structure.

My question to you guys then, is how would you suggest I handle creating an instance of my imagecache class.  I don't care if you link me sample code, a tutorial, or anything, I'm just lost as to where I should put it so that it's efficient and not cryptic.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Need advice on code structure
« Reply #1 on: June 20, 2010, 10:07:21 am »
The question is, what are the reasons why don't you want to use a global access for your image manager (singleton, global function or global variable)?

If you don't want to pass the instance to the constructor of all your variables, you will need such a global access at some point.
Laurent Gomila - SFML developer

Spidyy

  • Sr. Member
  • ****
  • Posts: 493
    • View Profile
Need advice on code structure
« Reply #2 on: June 20, 2010, 10:15:14 am »
Quote
Declaring it in the main loop just seems hectic as it requires me to pass a reference to the manager like Game --> Board --> Block --> etc just to set a sprites image, which seems like poor structure.


If you want to keep a global access to your manager but want to instantiate it in your main() (to control the order of your constructor calls), you can use a global pointer pointing to your ImageManager and use this pointer to access it.

I did a singleton class instantiated in global, containing only pointers of the different objects I want to access anywhere, such as ImageManager, VideoSystem, SoundSystem, etc.

 

anything