SLIDE 3 2/7/2012 3
Dragonfly Game Engine
DRAGONFLY
DrawCharacter InsertObject LoadSprite GetKey MoveObject SendEvent
GAME CODE
Saucer: move() Hero: key() Star: onEvent() GameOver: step()
COMPUTER PLATFORM
Allocate memory Clear display File open/close Get keystroke
Dragonfly Classes
Clock Box Sprite Position GameObject GameObjectList GameObjectListIterator Manager
GameManager ResourceManager
LogManager
InputManager GraphicsManager WorldManager
Event EventCollision EventKeyboard EventMouse EventOut EventStep Managers Game objects Support classes
Engine Support Systems - Managers
Support systems that manage crucial tasks
Handling input, Rendering graphics, Logging data …
Many interdependent, so startup order matters
E.g. Log file manager needed first since others log messages E.g. Graphics manager may need memory allocated for sprites, so need Memory manager first.
Often, want only 1 instance of each Manger
E.g. Undefined if two objects managing the graphics
How to enforce only 1 instance in C++?
Managers in C++: Global Variables?
Could make Managers global variables (e.g. outside of main() )
Constructors called before main(), destructors when main() ends
Then, declare global variable:
RenderManager render_manager;
However, order of constructor/destructor unpredictable
E.g. RenderManager r; GraphicsManager g; Could call g::g() before r::r()!
Plus, explicit globals difficult from library
Names could be different in user code
How about static variables inside a function
Managers in C++: Static Variables?
Remember, static variables retain value after method terminates
Static variables inside
method not created until method invoked Use inside Manager class method go “create” manager the Singleton
void stuff() { static int x = 0; cout << x; x++; } main() { stuff(); // prints 0 stuff(); // prints 1 }
Managers: C++ Singletons
Compiler won’t allow
MySingleton s;
Instead:
MySingleton &s= MySingleton::getInstance();
Guarantees only 1 copy of MySingleton will exist
class MySingleton { private: // Private constructor MySingleton(); // Can’t assign or copy MySingleton(MySingleton const& copy); MySingleton& operator=(MySingleton const& copy); public: // return the 1 and only 1 MySingleton static MySingleton& getInstance() { static MySingleton instance; return instance; } };
Use for Dragonfly Managers
However, also want to explicitly control when starts (not at first getInstance()) call) Use startUp() and shutDown() for each