How to create a game - GalaxyZ/Eclipse GitHub Wiki

The IGame Interface

The IGame class provides an interface for any class that want to implement a game. It is a noncopyable class since it clearly has an entity semantic.

IGame virtual methods

IGame defines 5 methods, that are 5 basic features a game should provide.

getTitle

virtual std::string getTitle() const = 0

This method should return the Title of the game. This might seem useless but if we think more about it any game has a name and this featured is required by IGame users to, as an example, correctly set the title of the window encapsulating the game.

init

virtual void init(GameContainer &gc) = 0;

This method is called when a game is about to start. Its implementation should turn the Game derived object in a proper initial state.

update

virtual void update(GameContainer &gc, int delta) = 0;

Updates the game logic. It will be called whenever the gc object wants to refresh the game logic. Since the time between different successive calls to update can differ, a delta variable is passed as an argument, that is the time in ms that passed since the last call to update.

This method is non coupled to the render method, and many calls to update can occur between two successive calls to render, or none, depending on the UpdateAndRender algorithm the gc object uses.

render

virtual void render(GameContainer &gc, Graphics &graph) = 0;

Render a frame of the game. It will be called whenever the game loop in the gc object wants to display a frame, at a frequency set in the gc object. The graph object represents the window where elements should be drawn.

closeRequested

virtual bool closeRequested() = 0;

This method is called by the GameContainer object to signal the game it should ends, which returns true if it is stopping or false if it refuses. But beware, the GameContainer calling object can still call the Game destructor even if closeRequested returned false, depending on how the GC is set.