Public Member Functions | List of all members
ime::Engine Class Reference

Runs the main game loop. More...

#include <Engine.h>

Public Member Functions

 Engine (const std::string &gameTitle, const std::string &settingsFilename="default")
 Constructor. More...
 
 Engine (const std::string &gameName, const PrefContainer &settings)
 Constructor. More...
 
 Engine (const Engine &)=delete
 Copy constructor. More...
 
Engineoperator= (const Engine &)=delete
 Copy assignment operator. More...
 
 Engine (Engine &&)=delete
 Move constructor. More...
 
Engineoperator= (Engine &&)=delete
 Move Assignment operator. More...
 
void initialize ()
 Initialize the engine. More...
 
void run ()
 Start the main loop. More...
 
bool isRunning () const
 Check if the engine is running or not. More...
 
void quit ()
 Stop the engine. More...
 
void setPause (bool pause)
 Pause or resume the engine. More...
 
bool isPaused () const
 Check if then engine is paused or not. More...
 
void setPhysicsUpdateFrameRate (unsigned int frameRate)
 Set the physics update frame rate. More...
 
unsigned int getPhysicsUpdateFrameRate () const
 Get the fixed update frame rate. More...
 
PrefContainergetConfigs ()
 Get the engines settings. More...
 
const PrefContainergetConfigs () const
 
PropertyContainergetCache ()
 Get the engine level persistent data. More...
 
const PropertyContainergetCache () const
 
PrefContainergetSavableCache ()
 Get the engine level savable persistent data. More...
 
const PrefContainergetSavableCache () const
 
const std::string & getGameName () const
 Get the name of the game run by the engine. More...
 
void pushScene (Scene::Ptr scene)
 Add a Scene to the engine. More...
 
bool pushCachedScene (const std::string &name)
 Add a cached scene to the engine. More...
 
void popScene (int numScenes=1)
 Remove scenes from the engine. More...
 
void cacheScene (const std::string &name, Scene::Ptr scene)
 Add a scene to the cache list. More...
 
bool uncacheScene (const std::string &name)
 Remove a scene from the cache list. More...
 
bool isSceneCached (const std::string &name) const
 Check if a scene with a given name is cached or not. More...
 
void removeAllScenesExceptActive ()
 Remove all the scenes from the engine except the current active scene. More...
 
void removeAllScenes ()
 Remove all the scenes from the engine. More...
 
std::size_t getSceneCount () const
 Get the number of scenes in the engine. More...
 
ScenegetActiveScene ()
 Get the active scene. More...
 
const ScenegetActiveScene () const
 
ScenegetPreviousScene ()
 Get the scene that was previously active. More...
 
const ScenegetPreviousScene () const
 
ScenegetCachedScene (const std::string &name)
 Get access to a scene in the cache list. More...
 
const ScenegetCachedScene (const std::string &name) const
 
Time getElapsedTime () const
 Get the time passed since the engine was started. More...
 
WindowgetWindow ()
 Get the engines game window. More...
 
const WindowgetWindow () const
 
ui::GuiContainergetGui ()
 Get the engine level gui. More...
 
const ui::GuiContainergetGui () const
 
audio::AudioManagergetAudioManager ()
 Get the engine level audio manager. More...
 
const audio::AudioManagergetAudioManager () const
 
input::InputManagergetInputManager ()
 Get the engine level input manager. More...
 
const input::InputManagergetInputManager () const
 
TimerManagergetTimer ()
 Get the engine level timer manager. More...
 
const TimerManagergetTimer () const
 
void suspendedEventListener (int id, bool suspend)
 Pause or resume execution of an event listener. More...
 
bool isEventListenerSuspended (int id) const
 Check if an event listener is suspended or not. More...
 
bool removeEventListener (int id)
 Remove an event listener from an event. More...
 
int onInit (const Callback<> &callback)
 Add an event listener to an initialize event. More...
 
int onSceneActivate (const Callback< Scene * > &callback, bool oneTime=false)
 Add an event listener to a scene activation event. More...
 
int onFrameStart (const Callback<> &callback, bool oneTime=false)
 Add an event listener to a current frame start event. More...
 
int onFrameEnd (const Callback<> &callback, bool oneTime=false)
 Add an event listener to a current frame end event. More...
 
int onStart (const Callback<> &callback)
 Add an event listener to an engine start event. More...
 
int onShutDown (const Callback<> &callback)
 Execute a callback when the engine is about to shut down. More...
 
void onShutdownComplete (const Callback<> &callback)
 Add an event listener to a shutdown complete event. More...
 
priv::RenderTarget & getRenderTarget ()
 
 ~Engine ()
 Destructor. More...
 

Detailed Description

Runs the main game loop.

ime::Engine is the entry point of Infinite Motion Engine.

It is responsible for running, updating and rendering the game world. External code interacts with the engine by pushing and popping ime::Scene at appropriate times

Usage example:

// The engine needs at least one scene in order to run
class TestScene : public ime::Scene {
public:
void onEnter() override {}
};
ime::Engine engine{"My awesome game"};
engine.initialize();
engine.pushScene(std::make_unique<TestScene>());
engine.run();
Runs the main game loop.
Definition: Engine.h:55
void initialize()
Initialize the engine.
A base class for game scenes.
Definition: Scene.h:63

Definition at line 55 of file Engine.h.

Constructor & Destructor Documentation

◆ Engine() [1/4]

ime::Engine::Engine ( const std::string &  gameTitle,
const std::string &  settingsFilename = "default" 
)
explicit

Constructor.

Parameters
gameTitleThe name of the game run by the engine
settingsFilenameThe name of the file that contains the engine settings

If the settingsFilename argument is left unspecified the engine will be constructed with default settings

◆ Engine() [2/4]

ime::Engine::Engine ( const std::string &  gameName,
const PrefContainer settings 
)

Constructor.

Parameters
gameNameThe name of the game run by the engine
settingsSettings to construct the engine with

◆ Engine() [3/4]

ime::Engine::Engine ( const Engine )
delete

Copy constructor.

◆ Engine() [4/4]

ime::Engine::Engine ( Engine &&  )
delete

Move constructor.

◆ ~Engine()

ime::Engine::~Engine ( )

Destructor.

Member Function Documentation

◆ cacheScene()

void ime::Engine::cacheScene ( const std::string &  name,
Scene::Ptr  scene 
)

Add a scene to the cache list.

Parameters
nameThe unique name of the scene for later access
sceneThe scene to be added

A cached scene is not destroyed when popped from the Engine but rather saved for reuse. This means that you can have a single instance that you return to. For example, instead of instantiating a new main menu scene every time the user wants to go to the main menu, you can instantiate it once, cache it and use pushCachedScene()

Unlike ime::Scene::setCached, this function will cache the scene immediately

See also
pushCachedScene, uncacheScene, ime::Scene::setCached

◆ getActiveScene()

Scene * ime::Engine::getActiveScene ( )

Get the active scene.

Returns
A pointer to the active scene if it exists, otherwise a nullptr
See also
getPreviousScene

◆ getAudioManager()

audio::AudioManager & ime::Engine::getAudioManager ( )

Get the engine level audio manager.

Returns
The engine level audio manager

Unlike ime::Scene::getAudio, this audio manager is not destroyed when the scene is destroyed, but rather when the engine is shutdown

◆ getCache()

PropertyContainer & ime::Engine::getCache ( )

Get the engine level persistent data.

Returns
The engine level persistent data

Data stored in this object persists from scene to scene. This means that the data is preserved during a scene push or a scene pop operation. The data stored inside the cache can be of any type.

For example, the code below saves the current level of the game in the cache such that it can be accessed in any ime::Scene

// Saving data to the cache
engine.getCache().addProperty({"Current_Level", 1});
// Accessing the cache within a ime::Scene
auto level = getEngine().getCache().getValue<int>("Current_Level");
Note
Data in the cache is destroyed when the engine is shutdown, see quit()
See also
getSavableCache

◆ getCachedScene()

Scene * ime::Engine::getCachedScene ( const std::string &  name)

Get access to a scene in the cache list.

Parameters
nameThe name of the scene
Returns
A pointer to the scene is it exists, otherwise a nullptr

◆ getConfigs()

PrefContainer & ime::Engine::getConfigs ( )

Get the engines settings.

Returns
The engines settings
Exceptions
AccessViolationExceptionIf this function is called before the engine is initialized

Note that this function returns the settings that were used during the initialization of the engine. Modifying then after the engine has been initialized has no effect

See also
initialize

◆ getElapsedTime()

Time ime::Engine::getElapsedTime ( ) const

Get the time passed since the engine was started.

Returns
The time passed since the engine was started

The elapsed time starts counting after run() is called.

Note
The elapsed time will reset to zero after the engine is shutdown

◆ getGameName()

const std::string & ime::Engine::getGameName ( ) const

Get the name of the game run by the engine.

Returns
The name of the game run by the engine

This name is provided as an argument to the engines constructor(s)

◆ getGui()

ui::GuiContainer & ime::Engine::getGui ( )

Get the engine level gui.

Returns
The engine level gui
Exceptions
AccessViolationExceptionIf this function is called before the engine is initialized

Unlike ime::Scene::getGui, this gui is not destroyed when the scene is destroyed, but rather when the engine is shutdown. It may be useful when the same UI is required across multiple scenes. Note that this gui is rendered on top of the scene level gui

See also
initialize

◆ getInputManager()

input::InputManager & ime::Engine::getInputManager ( )

Get the engine level input manager.

Returns
The engine level input manager

Unlike ime::Scene::getInput, event listeners registered to this input manager are executed regardless of which scene is active and are only destroyed when the engine is shutdown

◆ getPhysicsUpdateFrameRate()

unsigned int ime::Engine::getPhysicsUpdateFrameRate ( ) const

Get the fixed update frame rate.

Returns
The fixed update frame rate
See also
setPhysicsUpdateFrameRate

◆ getPreviousScene()

Scene * ime::Engine::getPreviousScene ( )

Get the scene that was previously active.

Returns
A pointer to the previously active scene if it exists, otherwise a nullptr

Note that after a push operation, the previously active scene is the scene that was pushed over and after a pop operation, it is the scene that will be active is the current active scene is popped

See also
getActiveScene

◆ getSavableCache()

PrefContainer & ime::Engine::getSavableCache ( )

Get the engine level savable persistent data.

Returns
The engine level savable persistent data

The savable cache behaves exactly the same way as the normal cahce (see getCache()). The difference between them is that the savable cache can only store data of a known type and can be saved/read to/from the disk. Its main purpose is to save player preferences. For example, you can use it to save the players desired audio levels

Warning
The data is destroyed when the engine is shutdown
See also
getCache

◆ getSceneCount()

std::size_t ime::Engine::getSceneCount ( ) const

Get the number of scenes in the engine.

Returns
The number of scenes in the engine

◆ getTimer()

TimerManager & ime::Engine::getTimer ( )

Get the engine level timer manager.

Returns
The engine level timer manager

Unlike ime::Scene::getTimer, event listeners registered to this timer manager are executed regardless of which scene is active and are destroyed when the engine is shutdown

◆ getWindow()

Window & ime::Engine::getWindow ( )

Get the engines game window.

Returns
The engines game window
Exceptions
AccessViolationExceptionIf this function is called before the engine is initialized
See also
initialize

◆ initialize()

void ime::Engine::initialize ( )

Initialize the engine.

Warning
This function must be called before the engine is run, otherwise undefined behaviour
See also
run

◆ isEventListenerSuspended()

bool ime::Engine::isEventListenerSuspended ( int  id) const

Check if an event listener is suspended or not.

Parameters
idThe identification number of the listener to be checked
Returns
True if suspended, otherwise false

This function also returns false if the specified event listener does not exist

See also
suspendedEventListener

◆ isPaused()

bool ime::Engine::isPaused ( ) const

Check if then engine is paused or not.

Returns
True if paused, otherwise false
See also
setPause

◆ isRunning()

bool ime::Engine::isRunning ( ) const

Check if the engine is running or not.

Returns
True if the engine is running, otherwise false

◆ isSceneCached()

bool ime::Engine::isSceneCached ( const std::string &  name) const

Check if a scene with a given name is cached or not.

Parameters
nameThe name of the scene to be checked
Returns
True if the scene is cached, otherwise false
See also
cacheScene

◆ onFrameEnd()

int ime::Engine::onFrameEnd ( const Callback<> &  callback,
bool  oneTime = false 
)

Add an event listener to a current frame end event.

Parameters
callbackFunction to be executed when the current frame ends
oneTimeTrue to execute the callback one-time or false to execute it every time the event is triggered
Returns
The event listener unique identification number

You can add any number of event listeners to this event

See also
onFrameStart

◆ onFrameStart()

int ime::Engine::onFrameStart ( const Callback<> &  callback,
bool  oneTime = false 
)

Add an event listener to a current frame start event.

Parameters
callbackFunction to executed when the current frame starts
oneTimeTrue to execute the callback one-time or false to execute it every time the event is triggered
Returns
The event listener unique identification number

You can add any number of event listeners to this event

See also
onFrameEnd

◆ onInit()

int ime::Engine::onInit ( const Callback<> &  callback)

Add an event listener to an initialize event.

Parameters
callbackThe function to be executed after the engine is initialized
Returns
The event listener unique identification number

You can add any number of event listeners to this event

See also
removeEventListener

◆ onSceneActivate()

int ime::Engine::onSceneActivate ( const Callback< Scene * > &  callback,
bool  oneTime = false 
)

Add an event listener to a scene activation event.

Parameters
callbackFunction to be executed when a scene becomes active
oneTimeTrue to execute the callback one-time or false to execute it every time the event is triggered
Returns
The event listener unique identification number

This event is emitted when a scene becomes active. The callback is passed a pointer to the scene that was activated.

You can add any number of event listeners to this event

◆ onShutDown()

int ime::Engine::onShutDown ( const Callback<> &  callback)

Execute a callback when the engine is about to shut down.

Parameters
callbackThe function to be executed when the engine is starts to shut down
Returns
The event listener unique identification number

This event is emitted when the engine is about to begin its shutdown sequence. To perform an action after the engine has completed its shutdown sequence, use onShutdownComplete()

You can add any number of event listeners to this event

See also
onShutdownComplete

◆ onShutdownComplete()

void ime::Engine::onShutdownComplete ( const Callback<> &  callback)

Add an event listener to a shutdown complete event.

Parameters
callbackThe function to be executed when the engine complete its shutdown sequence
Warning
When this event is emitted, the engine will be in an uninitialized state (see initialize())
Note
Only one event listener may registered to this event. Pass nullptr to remove the callback
See also
onShutDown

◆ onStart()

int ime::Engine::onStart ( const Callback<> &  callback)

Add an event listener to an engine start event.

Parameters
callbackFunction to be executed when the engine starts running
Returns
The event listener unique identification number

You can add any number of event listeners to this event

See also
onShutdown

◆ operator=() [1/2]

Engine & ime::Engine::operator= ( const Engine )
delete

Copy assignment operator.

◆ operator=() [2/2]

Engine & ime::Engine::operator= ( Engine &&  )
delete

Move Assignment operator.

◆ popScene()

void ime::Engine::popScene ( int  numScenes = 1)

Remove scenes from the engine.

Parameters
numScenesThe number of scenes to remove

If the engine is not running, the scene(s) will be removed immediately, otherwise they will be removed at the end of the current frame.

You can remove multiple scenes in the same frame by calling this function as many times as the number of scenes to be removed or by specifying the number of scenes to be removed. For example the code below removes all scenes from the engine:

engine.popScene(engine.getSceneCount());
// OR
while (engine.getSceneCount() > 0)
engine.popScene();

Note that calling this function when the engine has no scenes has no effect

See also
pushScene and pushCachedScene

◆ pushCachedScene()

bool ime::Engine::pushCachedScene ( const std::string &  name)

Add a cached scene to the engine.

Parameters
nameThe name of the scene to add
Returns
True if the scene was added or false if the scene with the given name does not exist in the cache list

Note that after the push, the scene is removed from the cache and will be added back once its popped (if its cache state is still true)

See also
cacheScene, ime::Scene::setCached and popScene

◆ pushScene()

void ime::Engine::pushScene ( Scene::Ptr  scene)

Add a Scene to the engine.

Parameters
sceneThe scene to be added

If the engine is running, the scene will not be pushed immediately but rather at the end of the current frame. You can push multiple scenes in the same frame, the last scene to be pushed will become the active scene. All the other scenes will be pushed without being entered

To be notified when a scene becomes active, see onSceneActivate()

See also
popScene

◆ quit()

void ime::Engine::quit ( )

Stop the engine.

This function will remove all scenes that have been added to the engine and reset it's initialization state. Therefore, the engine must be reinitialized before it is re-run

See also
initialize and run

◆ removeAllScenes()

void ime::Engine::removeAllScenes ( )

Remove all the scenes from the engine.

If a scene is not pushed in the same frame as this function call, then the engine will automatically shutdown since it must have at least one scene to run.

This function is an alias to the following code:

engine.popScene(engine.getSceneCount());
See also
removeAllScenesExceptActive

◆ removeAllScenesExceptActive()

void ime::Engine::removeAllScenesExceptActive ( )

Remove all the scenes from the engine except the current active scene.

Note that if this function is called whilst the engine is not running, then all the scenes will be removed from the engine.

Note
ime::Scene::onExit is not invoked on scenes that are removed using this function
See also
removeAllScenes

◆ removeEventListener()

bool ime::Engine::removeEventListener ( int  id)

Remove an event listener from an event.

Parameters
idThe unique identification number of the event listener
Returns
True if the event listener was removed or false if no such event listener exists

◆ run()

void ime::Engine::run ( )

Start the main loop.

Exceptions
AccessViolationExceptionIf this function is called before the engine is initialized or before at least one Scene is pushed to the engine
Warning
The engine must be initialized before this function is called. In addition, there must be at least one scene added to the engine for it to run
See also
initialize and pushScene, pushCachedScene

◆ setPause()

void ime::Engine::setPause ( bool  pause)

Pause or resume the engine.

Parameters
pauseTrue to pause the engine or false to resume it

Pausing the engine suspends all time based updates such as animations, physics, movements, timers. The same effect can be achieved by setting a scenes timescale to 0. The difference between the two is that the latter invokes the scenes update functions with a delta time of ime::Time::Zero whilst the former does not invoke the update functions at all.

By default, the engine is not paused

See also
isPaused

◆ setPhysicsUpdateFrameRate()

void ime::Engine::setPhysicsUpdateFrameRate ( unsigned int  frameRate)

Set the physics update frame rate.

Parameters
frameRateThe new physics update frame rate
Exceptions
InvalidArgumentExceptionIf the specified frame rate is 0

Physics based components are updated using a fixed timestep defined by the formula:

timestep = 1.0f / physicsUpdateFrameRate

By default the physics update frame rate is 60 fps

See also
getPhysicsUpdateFrameRate

◆ suspendedEventListener()

void ime::Engine::suspendedEventListener ( int  id,
bool  suspend 
)

Pause or resume execution of an event listener.

Parameters
idThe event listeners unique identification number
suspendTrue to suspend/pause or false to unsuspend/resume
See also
isEventListenerSuspended

◆ uncacheScene()

bool ime::Engine::uncacheScene ( const std::string &  name)

Remove a scene from the cache list.

Parameters
nameThe name of the scene to be removed
Returns
True if the scene was removed or false if it does not exist

Unlike calling ime::Scene::setCached with a false argument, which destroys the scene after it gets popped, this function will destroy the scene immediately

See also
cacheScene

The documentation for this class was generated from the following file: