GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
grem::application::Application Class Reference

Main application base class. More...

#include <GREM/application/Application.hpp>

Public Member Functions

 Application (const ApplicationOptions &options={})
 Construct the base of the main application.
virtual ~Application ()=default
 Virtual destructor which must be overridden by the concrete application class in order to perform any application-specific cleanup before shutdown.
void run ()
 Start the main loop of the application and keep running until the application quits or an unhandled exception is thrown.
virtual void quit () noexcept
 Initiate the shutdown process, meaning that the current frame will be the last to be processed before the main loop ends.
bool isRunning () const noexcept
 Check if the application is currently running, meaning that it is fully initialized, that run() has been called and has started the main loop, and that it is not in the process of shutting down.
bool isQuitting () const noexcept
 Check if the application is in the process of shutting down.
size_t getLastSecondFrameCount () const noexcept
 Get the number of frames displayed during the last measured second of the application's run time, which approximates the average frame rate.
Duration getTickInterval () const noexcept
 Get the tick interval of the application.
Duration getMaxAccumulatedTickTime () const noexcept
 Get the maximum accumulated tick time of the application.
Duration getMinFrameTime () const noexcept
 Get the minimum frame time of the application.
Duration getMaxFrameTime () const noexcept
 Get the maximum frame time of the application.
void resetTickTimer () noexcept
 Reset the internal tick accumulator, causing the next tick to start exactly one tick interval into the future.
void setTickInterval (Duration newTickInterval) noexcept
 Set the tick interval of the application.
void setMaxAccumulatedTickTime (Duration newMaxAccumulatedTickTime) noexcept
 Set the maximum accumulated tick time of the application.
void setMinFrameTime (Duration newMinFrameTime) noexcept
 Set the minimum frame time of the application.
void setMaxFrameTime (Duration newMaxFrameTime) noexcept
 Set the maximum frame time of the application.
void setFrameRateLimiterSleepEnabled (bool newFrameRateLimiterSleepEnabled) noexcept
 Enable or disable frame rate limiter sleep.
TickInfo getLatestTickInfo () const noexcept
 Get the latest available tick information, including any configuration changes since the last tick.
FrameInfo getLatestFrameInfo () const noexcept
 Get the latest available frame information, including any configuration changes since the last frame.

Protected Member Functions

virtual void update (FrameInfo frameInfo)
 Per-frame update callback, called in the main loop once at the beginning of each frame, before processing ticks.
virtual void tick (TickInfo tickInfo)
 Fixed-rate tick callback, called in the main loop 0 or more times during tick processing, which happens on each frame after calling update() and before calling display().
virtual void skipTick (Duration tickInterval)
 Callback for ticks that were skipped due to the accumulated tick time exceeding ApplicationOptions::maxAccumulatedTickTime.
virtual void display (FrameInfo frameInfo)
 Frame rendering callback, called in the main loop once at the end of each frame after processing ticks, in order to render the latest state of the application.

Detailed Description

Main application base class.

Deriving from this class provides a platform-agnostic wrapper for the main loop that includes a built-in frame rate limiter and fixed-interval frame rate-independent tick callbacks.

Constructor & Destructor Documentation

◆ Application()

grem::application::Application::Application ( const ApplicationOptions & options = {})
explicit

Construct the base of the main application.

Parameters
optionsinitial configuration of the application, see ApplicationOptions.

◆ ~Application()

virtual grem::application::Application::~Application ( )
virtualdefault

Virtual destructor which must be overridden by the concrete application class in order to perform any application-specific cleanup before shutdown.

May also be overridden implicitly by the compiler-generated destructor of the derived class.

Member Function Documentation

◆ run()

void grem::application::Application::run ( )

Start the main loop of the application and keep running until the application quits or an unhandled exception is thrown.

Exceptions
anyunhandled exception which was thrown during the execution of the main loop, unless running under emscripten, in which case exceptions are simply printed before shutting down.
Note
Under emscripten-based WebAssembly builds, this function will never return. Instead, it explicitly calls the virtual application destructor in order to perform any necessary cleanup when the main loop has ended. It is therefore expected that any application- specific cleanup or shutdown code be called from the overriden destructor, rather than being called from main after run() has finished.
If the application has already been shut down, it will not enter the running state again. To start a new main loop, a new application must be constructed.
Warning
The result of calling this function while the application is already running is undefined.
Remarks
The intended usage of this function is to call it once at the end of the main function of the program as the last code to be executed, save for any catch blocks that are specific to non-emscripten builds.

◆ quit()

virtual void grem::application::Application::quit ( )
virtualnoexcept

Initiate the shutdown process, meaning that the current frame will be the last to be processed before the main loop ends.

If called during update(), no ticks will be processed afterwards, and the final frame will not be displayed.

This method may be overridden by the concrete application to intercept requests to quit and perform application-specific processing before deciding whether to actually quit or not by either calling the base implementation or choosing to ignore the request.

Exceptions
anyexception thrown by the concrete implementation.
Note
This function will be called automatically if an unhandled exception is thrown from the main loop.

◆ isRunning()

bool grem::application::Application::isRunning ( ) const
inlinenodiscardnoexcept

Check if the application is currently running, meaning that it is fully initialized, that run() has been called and has started the main loop, and that it is not in the process of shutting down.

Returns
true if the application is currently running, false otherwise.

◆ isQuitting()

bool grem::application::Application::isQuitting ( ) const
inlinenodiscardnoexcept

Check if the application is in the process of shutting down.

Returns
true if quit() has been called, false otherwise.

◆ getLastSecondFrameCount()

size_t grem::application::Application::getLastSecondFrameCount ( ) const
inlinenodiscardnoexcept

Get the number of frames displayed during the last measured second of the application's run time, which approximates the average frame rate.

This is measured automatically by counting the number of frames displayed between each second that passes while the application is running.

Returns
the number of frames displayed during the last second that was measured, or 0 if less than one full second has passed since the start of the application.
Note
This approximation of the frame rate does not update frequently enough to be used as an accurate time delta between frames. Use the values that are supplied in the FrameInfo struct to each relevant callback for this purpose instead.

◆ getTickInterval()

Duration grem::application::Application::getTickInterval ( ) const
inlinenodiscardnoexcept

Get the tick interval of the application.

Returns
the tick interval of the application.
See also
ApplicationOptions::tickInterval
setTickInterval()

◆ getMaxAccumulatedTickTime()

Duration grem::application::Application::getMaxAccumulatedTickTime ( ) const
inlinenodiscardnoexcept

Get the maximum accumulated tick time of the application.

Returns
the maximum time to process accumulated ticks for before starting to skip ticks.
See also
ApplicationOptions::maxAccumulatedTickTime
setMaxAccumulatedTickTime()

◆ getMinFrameTime()

Duration grem::application::Application::getMinFrameTime ( ) const
inlinenodiscardnoexcept

Get the minimum frame time of the application.

Returns
the minimum frame time of the application before frames are delayed.
See also
ApplicationOptions::minFrameTime
setMinFrameTime()

◆ getMaxFrameTime()

Duration grem::application::Application::getMaxFrameTime ( ) const
inlinenodiscardnoexcept

Get the maximum frame time of the application.

Returns
the maximum frame time of the application before tick slowdown occurs.
See also
ApplicationOptions::maxFrameTime
setMaxFrameTime()

◆ resetTickTimer()

void grem::application::Application::resetTickTimer ( )
noexcept

Reset the internal tick accumulator, causing the next tick to start exactly one tick interval into the future.

◆ setTickInterval()

void grem::application::Application::setTickInterval ( Duration newTickInterval)
noexcept

Set the tick interval of the application.

Parameters
newTickIntervaldesired tick interval of the application. Set to zero or lower for no ticks.
See also
ApplicationOptions::tickInterval
getTickInterval()
resetTickTimer()

◆ setMaxAccumulatedTickTime()

void grem::application::Application::setMaxAccumulatedTickTime ( Duration newMaxAccumulatedTickTime)
noexcept

Set the maximum accumulated tick time of the application.

Parameters
newMaxAccumulatedTickTimemaximum time to process accumulated ticks for before starting to skip ticks.
See also
ApplicationOptions::maxAccumulatedTickTime
getMaxAccumulatedTickTime()
setMaxFrameTime()

◆ setMinFrameTime()

void grem::application::Application::setMinFrameTime ( Duration newMinFrameTime)
noexcept

Set the minimum frame time of the application.

Parameters
newMinFrameTimeminimum frame time of the application before frames are delayed. Set to zero or lower for no frame rate limit.
See also
ApplicationOptions::minFrameTime
getMinFrameTime()

◆ setMaxFrameTime()

void grem::application::Application::setMaxFrameTime ( Duration newMaxFrameTime)
noexcept

Set the maximum frame time of the application.

Parameters
newMaxFrameTimemaximum frame time of the application before tick slowdown occurs.
See also
ApplicationOptions::maxFrameTime
getMaxFrameTime()
setMaxAccumulatedTickTime()

◆ setFrameRateLimiterSleepEnabled()

void grem::application::Application::setFrameRateLimiterSleepEnabled ( bool newFrameRateLimiterSleepEnabled)
noexcept

Enable or disable frame rate limiter sleep.

Parameters
newFrameRateLimiterSleepEnabledtrue to enable frame rate limiter sleep, false to disable.
See also
ApplicationOptions::frameRateLimiterSleepEnabled

◆ getLatestTickInfo()

TickInfo grem::application::Application::getLatestTickInfo ( ) const
inlinenodiscardnoexcept

Get the latest available tick information, including any configuration changes since the last tick.

Returns
the latest tick information.

◆ getLatestFrameInfo()

FrameInfo grem::application::Application::getLatestFrameInfo ( ) const
inlinenodiscardnoexcept

Get the latest available frame information, including any configuration changes since the last frame.

Returns
the latest frame information.

◆ update()

virtual void grem::application::Application::update ( FrameInfo frameInfo)
inlineprotectedvirtual

Per-frame update callback, called in the main loop once at the beginning of each frame, before processing ticks.

This is the best time to poll events using an EventPump and apply any changes to interactive application state that depends on user input and is used by tick(), since it minimizes the average latency between processing an input event and it affecting the result of a subsequent tick.

Parameters
frameInfoinformation about the current frame, see FrameInfo.
Note
Any exception that is thrown out of this function will percolate up to run() and cause the main loop to stop.
The default implementation of this function does nothing.
See also
tick()
display()
getLatestTickInfo()

◆ tick()

virtual void grem::application::Application::tick ( TickInfo tickInfo)
inlineprotectedvirtual

Fixed-rate tick callback, called in the main loop 0 or more times during tick processing, which happens on each frame after calling update() and before calling display().

See the documentation of TickInfo::tickInterval for an explanation of what this function may be useful for.

Parameters
tickInfoinformation about the current tick, see TickInfo.
Note
Any exception that is thrown out of this function will percolate up to run() and cause the main loop to stop.
The default implementation of this function does nothing.
See also
update()
skipTick()
display()
getLatestFrameInfo()

◆ skipTick()

virtual void grem::application::Application::skipTick ( Duration tickInterval)
inlineprotectedvirtual

Callback for ticks that were skipped due to the accumulated tick time exceeding ApplicationOptions::maxAccumulatedTickTime.

Parameters
tickIntervalthe average time that would have elapsed for this tick if it hadn't been skipped.
Note
Any exception that is thrown out of this function will percolate up to run() and cause the main loop to stop.
The default implementation of this function does nothing.
See also
tick()

◆ display()

virtual void grem::application::Application::display ( FrameInfo frameInfo)
inlineprotectedvirtual

Frame rendering callback, called in the main loop once at the end of each frame after processing ticks, in order to render the latest state of the application.

Before rendering, this is also the best time to apply any final cosmetic changes to the state of the application that is about to be presented, such as interpolation of data that is updated in tick().

Parameters
frameInfoinformation about the current frame, see FrameInfo.
Note
Any exception that is thrown out of this function will percolate up to run() and cause the main loop to stop.
The default implementation of this function does nothing.
See also
update()
tick()
getLatestTickInfo()

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