CUGL 2.5
Cornell University Game Library
|
#include <CUApplication.h>
Public Types | |
enum class | State : unsigned int { NONE = 0 , STARTUP = 1 , FOREGROUND = 2 , BACKGROUND = 3 , SHUTDOWN = 4 } |
Public Member Functions | |
Application () | |
~Application () | |
virtual void | dispose () |
bool | init () |
virtual void | onStartup () |
virtual void | onShutdown () |
virtual void | onLowMemory () |
virtual void | onSuspend () |
virtual void | onResume () |
virtual void | update (float dt) |
virtual void | preUpdate (float dt) |
virtual void | fixedUpdate () |
virtual void | postUpdate (float dt) |
virtual void | draw () |
bool | getInput () |
bool | step () |
void | quit () |
Uint32 | schedule (std::function< bool()> callback, Uint32 time=0) |
Uint32 | schedule (std::function< bool()> callback, Uint32 time, Uint32 period) |
void | unschedule (Uint32 id) |
void | setDisplaySize (int width, int height) |
int | getDisplayWidth () const |
int | getDisplayHeight () const |
Size | getDisplaySize () const |
Rect | getDisplayBounds () const |
Rect | getSafeBounds () const |
void | setFullscreen (bool value) |
bool | isFullscreen () const |
void | setHighDPI (bool highDPI) |
bool | isHighDPI () const |
void | setMultiSampled (bool flag) |
bool | isMultiSampled () const |
void | setName (const char *name) |
void | setName (const std::string name) |
const std::string | getName () const |
void | setOrganization (const char *name) |
void | setOrganization (const std::string name) |
const std::string | getOrganization () const |
bool | getVSync () const |
void | setVSync (bool vsync) |
void | setFPS (float fps) |
float | getFPS () const |
float | getAverageFPS () const |
void | setFixedStep (Uint64 step) |
long | getFixedStep () const |
void | setDeterministic (bool value) |
bool | isDeterministic () |
Uint64 | getFixedCount () const |
Uint32 | getFixedRemainder () const |
void | resetFixedRemainder () |
void | setClearColor (Color4 color) |
Color4 | getClearColor () const |
Uint64 | getEllapsedMicros () const |
State | getState () const |
const std::string | getOpenGLDescription () const |
std::string | getAssetDirectory () |
std::string | getSaveDirectory () |
Static Public Member Functions | |
static Application * | get () |
Protected Attributes | |
std::string | _name |
std::string | _org |
std::string | _assetdir |
std::string | _savesdir |
State | _state |
Rect | _display |
Rect | _safearea |
bool | _fullscreen |
bool | _highdpi |
bool | _multisamp |
float | _fps |
Uint64 | _fixstep |
bool | _vsync |
bool | _fixed |
Color4f | _clearColor |
Static Protected Attributes | |
static Application * | _theapp |
This class represents a basic CUGL application
The application does not assume 2d or 3d. This application can be used with any type of graphics.
This class is not intended to be passed around as a pointer, as it is the (subclass of the) root class. Hence we only have a stack-based initializer for this class.
With that said, we do allow access to the application through static method get()
. This allows other parts of the application to get important information like the display size or orientation.
|
strong |
The current state of the application.
This value is used by SDL to invoke the correct update method at each frame
Enumerator | |
---|---|
NONE | The application is not yet initialized. This state indicates that there is no OpenGL context. It is unsafe to make OpenGL calls while in this state. |
STARTUP | The application is initialized, but has not yet started This state indicates there is an OpenGL context, and OpenGL calls are now safe. This is the state for initializing the application with user-defined attributes. |
FOREGROUND | The application is currently running in the foreground The update-draw loop will be invoked while the application is in this state (and only in this state). |
BACKGROUND | The application is currently suspended The update-draw loop will not be invoked while the application is in this state. However, no assets will be deleted unless manually deleted by the programmer. |
SHUTDOWN | The application is shutting down. While in this state, the programmer should delete all custom data in the application. The OpenGL context will soon be deleted, and the application will shift back to start |
cugl::Application::Application | ( | ) |
Creates a degnerate application with no OpenGL context
You must initialize the application to use it. However, you may set any of the attributes before initialization.
|
inline |
Deletes this application, disposing of all resources
|
virtual |
Disposes all of the resources used by this application.
A disposed Application has no OpenGL context, and cannot be used.
However, it can be safely reinitialized.
|
inlinevirtual |
The method called to draw the application to the screen.
This is your core loop and should be replaced with your custom implementation. This method should OpenGL and related drawing calls.
When overriding this method, you do not need to call the parent method at all. The default implmentation does nothing.
|
inlinevirtual |
The method called to provide a deterministic application loop.
This method provides an application loop that runs at a guaranteed fixed timestep. This method is (logically) invoked every getFixedStep
microseconds. By that we mean if the method draw
is called at time T, then this method is guaranteed to have been called exactly floor(T/s) times this session, where s is the fixed time step.
This method is always invoked in-between a call to preUpdate
and postUpdate
. However, to guarantee determinism, it is possible that this method is called multiple times between those two calls. Depending on the value of getFixedStep
, it can also (periodically) be called zero times, particularly if getFPS
is much faster.
As such, this method should only be used for portions of the application that must be deterministic, such as the physics simulation. It should not be used to process user input (as no user input is recorded between preUpdate
and postUpdate
) or to animate models.
|
inlinestatic |
Returns the current running application
There can only be one application running a time. While this should never happen, this method will return nullptr if there is no application.
std::string cugl::Application::getAssetDirectory | ( | ) |
Returns the base directory for all assets (e.g. the assets folder).
The assets folder is a READ-ONLY folder for providing assets for the game. Its path depends on the platform involved. Android uses this to refer to the dedicated assets folder, while MacOS/iOS refers to the resource bundle. On Windows, this is the working directory.
The value returned is an absolute path in UTF-8 encoding, and has the appropriate path separator for the given platform ('\' on Windows, '/' most other places). In addition, it is guaranteed to end with a path separator, so that you can append a file name to the path.
It is possible that the the string is empty. For example, the assets directory for Android is not a proper directory (unlike the save directory) and should not be treated as such.
Asset loaders use this directory by default.
float cugl::Application::getAverageFPS | ( | ) | const |
Returns the average frames per second over the last 10 frames.
The method provides a way of computing the curren frames per second that smooths out any one-frame anomolies. The FPS is averages over the exact rate of the past 10 frames.
|
inline |
Returns the clear color of this application
This color is the default background color. The window will be cleared using this color before draw() is called.
|
inline |
Returns the screen bounds of this application.
If the application is running in windowed mode on a desktop, the bounds origin is the position of the bottom left corner of the window. Otherwise the origin is (0,0).
This value is in pixels, representing the view port size of the OpenGL context. It is never changed, even if the orientation of the device changes. For display information that takes into account the current device orientation, see Display
.
|
inline |
Returns the screen height of this application.
This value is in pixels, representing the safe view port size of the OpenGL context. It is never changed, even if the orientation of the device changes. For display information that takes into account the current device orientation, see Display
.
|
inline |
Returns the screen size of this application.
This value is in pixels, representing the safe view port size of the OpenGL context. It is never changed, even if the orientation of the device changes. For display information that takes into account the current device orientation, see Display
.
|
inline |
Returns the screen width of this application.
This value is in pixels, representing the safe view port size of the OpenGL context. It is never changed, even if the orientation of the device changes. For display information that takes into account the current device orientation, see Display
.
|
inline |
Returns the number of total microseconds that have ellapsed
This is value is measured from the call to init
to the current time step. The value is undefined if the application has not been initialized.
|
inline |
Returns the number of times fixedUpdate
has been called.
This value is reset to 0 if setDeterministic
is set to false.
fixedUpdate
has been called.
|
inline |
Returns the time "left over" after the call to fixedUpdate
.
If the FPS and the simulation timestep do not perfectly match, the draw
method will be invoked with some extra time after the last call to fixedUpdate
. It is useful to know this amount of time for the purposes of interpolation. The value returned is in microseconds.
This value is always guaranteed to be less than getFixedStep
.
fixedUpdate
.
|
inline |
Returns the simulation timestep of this application.
The value defines the rate at which fixedUpdate
is called. The rate is a logical value, not a wall-clock value. That is, if draw
is called at time T, then the method fixedUpdate
will have been called T/s times, where s is the simulation timestep.
This timestep is set in microseconds for the purposes of precision. Note that this value does nothing if setDeterministic
is set to false. In that case, fixedUpdate
is never called, and update
is called instead.
This method may be safely changed at any time while the application is running. By default, this value will match the initial FPS of the application.
|
inline |
Returns the target frames per second of this application.
The application does not guarantee that the fps target will always be met. In particular, if the update
and draw
methods are expensive, it may run slower. In addition, if vsync is enabled, it may be ignored entirely (especially if the refresh rate is faster).
Note that the FPS is distinct from setFixedStep
, which sets the timestep of a deterministic loop. The FPS sets the draw speed, not the simulation speed.
This method may be safely changed at any time while the application is running. By default, this value will match the refresh rate of the display, even if vsync is disabled.
bool cugl::Application::getInput | ( | ) |
Gathers SDL input and distributes it to the event handlers.
Input is gathered at the state of the animation frame, before update is called. As it sends all of its information to the appropriate handlers, you should never need to override this method.
|
inline |
Returns the name of this application
On a desktop, the name will be displayed at the top of the window. The name also defines the preferences directory – the place where it is safe to write save files.
const std::string cugl::Application::getOpenGLDescription | ( | ) | const |
Returns the OpenGL description for this application
|
inline |
Returns the organization name for this application
This name defines the preferences directory – the place where it is safe to write save files. Applications of the same organization will save in the same location.
|
inline |
Returns the safe area of this application.
The safe area is a subset of getDisplayBounds()
that is safe for UI and interactive elements. For phones with notches or rounded corners, it removes those areas that may be hidden.
This value is in pixels, representing the safe view port size of the OpenGL context. It is never changed, even if the orientation of the device changes. For display information that takes into account the current device orientation, see Display
.
std::string cugl::Application::getSaveDirectory | ( | ) |
Returns the base directory for writing save files and preferences.
The save folder is a READ-WRITE folder for storing saved games and preferences. The folder is unique to the current user. On desktop platforms, it is typically in the user's home directory. You must use this folder (and not the asset folder) if you are writing any files.
The value returned is an absolute path in UTF-8 encoding, and has the appropriate path separator for the given platform ('\' on Windows, '/' most other places). In addition, it is guaranteed to end with a path separator, so that you can append a file name to the path.
I/O classes (both readers and writers) use this directory by default.
However, if you are want to use this directory in an asset loader (e.g. for a saved game file), you you may want to refer to the path directly.
|
inline |
|
inline |
Returns true if this application obeys the display refresh rate
A vsync-enabled application will always match the refresh rate of the display. Otherwise, the application will attempt to match the value of getFPS()
, which could be faster than the refresh rate.
Note that some platforms (notably macOS) will always use vsync no matter the settings. In that case, setting this value to false will actually hurt the performance of your application. As a general rule, it is best to set this value to true, and perform any simulations that must be done at a faster rate in a separate thread.
bool cugl::Application::init | ( | ) |
Initializes this application, creating an OpenGL context.
The initialization will use the current value of all of the attributes, like application name, orientation, and size. These values should be set before calling init().
CUGL only supports one application running at a time. This method will fail if there is another application object.
You should not override this method to initialize user-defined attributes. Use the method onStartup() instead.
|
inline |
Returns whether the application uses the deterministic loop.
If this value is set to false, then the application will use the simple structure of alternating between update
and draw
. However, if it is set to true, it will use a more complicated loop in place of update
, consisting of a call to preUpdate
, followed by zero or more calls to fixedUpdate
.
This method may be safely changed at any time while the application is running. By default, this value is false.
|
inline |
Returns true if this application is running fullscreen
Mobile devices must always run fullscreen, and can never be windowed.
On desktop/laptop platforms, going fullscreen will hide the mouse. The mouse cursor is only visible in windowed mode.
|
inline |
Returns true if this application supports high dpi resolution.
For devices that have high dpi screens (e.g. a pixel ration greater than 1), this will enable that feature. Otherwise, this value will do nothing.
Setting high dpi to true is highly recommended for devides that support it (e.g. iPhones). It makes the edges of textures much smoother. However, rendering is slightly slower as it effectively doubles (and in some cases triples) the resolution.
|
inline |
Returns true if this application supports graphics multisampling.
Multisampling adds anti-aliasing to OpenGL so that polygon edges are not so hard and jagged. This does add some extra overhead, and is not really necessary on Retina or high DPI displays. However, it is pretty much a must in Windows and normal displays.
By default, this is false on any platform other than Windows.
|
inlinevirtual |
The method called when you are running out of memory.
When this method is called, you should immediately free memory or cause the application to quit. Texture memory is generally the biggest candidate for freeing memory; delete all textures you are not using.
When overriding this method, you do not need to call the parent method at all. The default implmentation does nothing.
|
inlinevirtual |
The method called when the application resumes and put in the foreground.
If you saved any state before going into the background, now is the time to restore it. This guarantees that the application looks the same as when it was suspended.
If you are using audio, you should use this method to resume any audio paused before app suspension.
|
virtual |
The method called when the application is ready to quit.
This is the method to dispose of all resources allocated by this application. As a rule of thumb, everything created in onStartup() should be deleted here.
When overriding this method, you should call the parent method as the very last line. This ensures that the state will transition to NONE, causing the application to be deleted.
|
virtual |
The method called after OpenGL is initialized, but before running the application.
This is the method in which all user-defined program intialization should take place. You should not create a new init() method.
When overriding this method, you should call the parent method as the very last line. This ensures that the state will transition to FOREGROUND, causing the application to run.
|
inlinevirtual |
The method called when the application is suspended and put in the background.
When this method is called, you should store any state that you do not want to be lost. There is no guarantee that an application will return from the background; it may be terminated instead.
If you are using audio, it is critical that you pause it on suspension. Otherwise, the audio thread may persist while the application is in the background.
|
inlinevirtual |
The method called to indicate the end of a deterministic loop.
This method is used instead of update
if setDeterministic
is set to true. It marks the end of the core application loop, which was begun with a call to preUpdate
.
This method is the final portion of the update loop called before any drawing occurs. As such, it should be used to implement any final animation in response to the simulation provided by fixedUpdate
. In particular, it should be used to interpolate any visual differences between the the simulation timestep and the FPS.
This method should not be used to process user input, as no new input will have been recorded since preUpdate
was called.
Note that the time passed as a parameter is the time measured from the start of the previous frame to the start of the current frame. It is measured before any input or callbacks are processed. It agrees with the value sent to preUpdate
this animation frame.
dt | The amount of time (in seconds) since the last frame |
|
inlinevirtual |
The method called to indicate the start of a deterministic loop.
This method is used instead of update
if setDeterministic
is set to true. It marks the beginning of the core application loop, which is concluded with a call to postUpdate
.
This method should be used to process any events that happen early in the application loop, such as user input or events created by the schedule
method. In particular, no new user input will be recorded between the time this method is called and postUpdate
is invoked.
Note that the time passed as a parameter is the time measured from the start of the previous frame to the start of the current frame. It is measured before any input or callbacks are processed. It agrees with the value sent to postUpdate
this animation frame.
dt | The amount of time (in seconds) since the last frame |
void cugl::Application::quit | ( | ) |
Cleanly shuts down the application.
This method will shutdown the application in a way that is guaranteed to call onShutdown() for clean-up. You should use this method instead of a general C++ exit() function.
This method uses the SDL event system. Therefore, the application will quit at the start of the next animation frame, when all events are processed.
|
inline |
Resets the time "left over" for fixedUpdate
to 0.
This method is for when you need to reset a simulation back to its initial state.
Uint32 cugl::Application::schedule | ( | std::function< bool()> | callback, |
Uint32 | time, | ||
Uint32 | period | ||
) |
Schedules a reoccuring callback function time milliseconds in the future.
This method allows the user to delay an operation until a certain length of time has passed. If time is 0, it will be called the next animation frame. Otherwise, it will be called the first animation frame equal to or more than time steps in the future (so there is no guarantee that the callback will be invoked at exactly time milliseconds in the future).
The callback will only be executed on a regular basis. Once it is called, the timer will be reset and it will not be called for another period milliseconds. Hence it is possible to delay the callback for a long time, but then have it execute every frame. If the callback started late, that extra time waited will be credited to the next call.
The callback is guaranteed to be executed in the main thread, so it is safe to access the drawing context or any low-level SDL operations. It will be executed after the input has been processed, but before either update
or preUpdate
are invoked.
callback | The callback function |
time | The number of milliseconds to delay the callback. |
period | The delay until the callback is executed again. |
Uint32 cugl::Application::schedule | ( | std::function< bool()> | callback, |
Uint32 | time = 0 |
||
) |
Schedules a reoccuring callback function time milliseconds in the future.
This method allows the user to delay an operation until a certain length of time has passed. If time is 0, it will be called the next animation frame. Otherwise, it will be called the first animation frame equal to or more than time steps in the future (so there is no guarantee that the callback will be invoked at exactly time milliseconds in the future).
The callback will only be executed on a regular basis. Once it is called, the timer will be reset and it will not be called for another time milliseconds. If the callback started late, that extra time waited will be credited to the next call.
The callback is guaranteed to be executed in the main thread, so it is safe to access the drawing context or any low-level SDL operations. It will be executed after the input has been processed, but before either update
or preUpdate
are invoked.
callback | The callback function |
time | The number of milliseconds to delay the callback. |
|
inline |
Sets the clear color of this application
This color is the default background color. The window will be cleared using this color before draw() is called.
This method may be safely changed at any time while the application is running.
color | The clear color of this application |
void cugl::Application::setDeterministic | ( | bool | value | ) |
Instructs the application to use the deterministic loop.
If this value is set to false, then the application will use the simple structure of alternating between update
and draw
. However, if it is set to true, it will use a more complicated loop in place of update
, consisting of a call to preUpdate
, followed by zero or more calls to fixedUpdate
.
This method may be safely changed at any time while the application is running. By default, this value is false.
value | Whether to use the deterministic loop |
void cugl::Application::setDisplaySize | ( | int | width, |
int | height | ||
) |
Sets the screen size of this application.
If the application is set to be full screen, this value will be ignored. Instead, the application size will be the same as the Display
.
This method may only be safely called before the application is initialized. Once the application is initialized; this value may not be changed.
width | The screen width |
height | The screen height |
void cugl::Application::setFixedStep | ( | Uint64 | step | ) |
Sets the simulation timestep of this application.
The value defines the rate at which fixedUpdate
is called. The rate is a logical value, not a wall-clock value. That is, if draw
is called at time T, then the method fixedUpdate
will have been called T/s times, where s is the simulation timestep.
This timestep is set in microseconds for the purposes of precision. Note that this value does nothing if setDeterministic
is set to false. In that case, fixedUpdate
is never called, and update
is called instead.
This method may be safely changed at any time while the application is running. By default, this value will match the initial FPS of the application.
step | The simulation timestep |
void cugl::Application::setFPS | ( | float | fps | ) |
Sets the target frames per second of this application.
The application does not guarantee that the fps target will always be met. In particular, if the update
and draw
methods are expensive, it may run slower. In addition, if vsync is enabled, it may be ignored entirely (especially if the refresh rate is faster).
Note that the FPS is distinct from setFixedStep
, which sets the timestep of a deterministic loop. The FPS sets the draw speed, not the simulation speed.
This method may be safely changed at any time while the application is running. By default, this value will match the refresh rate of the display, even if vsync is disabled.
fps | The target frames per second |
void cugl::Application::setFullscreen | ( | bool | value | ) |
Sets whether this application is running fullscreen
Mobile devices must always run fullscreen, and can never be windowed. In addition, this method may only be safely called before the application is initialized. Once the application is initialized; this value may not be changed.
On desktop/laptop platforms, going fullscreen will hide the mouse. The mouse cursor is only visible in windowed mode.
value | Whether this application is running fullscreen |
void cugl::Application::setHighDPI | ( | bool | highDPI | ) |
Sets whether this application supports high dpi resolution.
For devices that have high dpi screens (e.g. a pixel ration greater than 1), this will enable that feature. Otherwise, this value will do nothing.
Setting high dpi to true is highly recommended for devides that support it (e.g. iPhones). It makes the edges of textures much smoother. However, rendering is slightly slower as it effectively doubles (and in some cases triples) the resolution.
This method may only be safely called before the application is initialized. Once the application is initialized; this value may not be changed.
highDPI | Whether to enable high dpi |
void cugl::Application::setMultiSampled | ( | bool | flag | ) |
Sets whether this application supports graphics multisampling.
Multisampling adds anti-aliasing to OpenGL so that polygon edges are not so hard and jagged. This does add some extra overhead, and is not really necessary on Retina or high DPI displays. However, it is pretty much a must in Windows and normal displays.
By default, this is false on any platform other than Windows.
flag | Whether this application should support graphics multisampling. |
void cugl::Application::setName | ( | const char * | name | ) |
Sets the name of this application
On a desktop, the name will be displayed at the top of the window. The name also defines the preferences directory – the place where it is safe to write save files.
This method may be safely changed at any time while the application is running.
name | The name of this application |
void cugl::Application::setName | ( | const std::string | name | ) |
Sets the name of this application
On a desktop, the name will be displayed at the top of the window. The name also defines the preferences directory – the place where it is safe to write save files.
This method may be safely changed at any time while the application is running.
name | The name of this application |
void cugl::Application::setOrganization | ( | const char * | name | ) |
Sets the organization name for this application
This name defines the preferences directory – the place where it is safe to write save files. Applications of the same organization will save in the same location.
This method may be safely changed at any time while the application is running.
name | The organization name for this application |
void cugl::Application::setOrganization | ( | const std::string | name | ) |
Sets the organization name for this application
This name defines the preferences directory – the place where it is safe to write save files. Applications of the same organization will save in the same location.
This method may be safely changed at any time while the application is running.
name | The organization name for this application |
void cugl::Application::setVSync | ( | bool | vsync | ) |
Sets whether this application obeys the display refresh rate
A vsync-enabled application will always match the refresh rate of the display. Otherwise, the application will attempt to match the value of getFPS()
, which could be faster than the refresh rate.
Note that some platforms (notably macOS) will always use vsync no matter the settings. In that case, setting this value to false will actually hurt the performance of your application. As a general rule, it is best to set this value to true, and perform any simulations that must be done at a faster rate in a separate thread.
vsync | Whether this application obeys the display refresh rate |
bool cugl::Application::step | ( | ) |
Processes a single animation frame.
This method processes the input, calls the update method, and then draws it. It also updates any running statics, like the average FPS.
void cugl::Application::unschedule | ( | Uint32 | id | ) |
Stops a callback function from being executed.
This method may be used to disable a reoccuring callback. If called soon enough, it can also disable a one-time callback that is yet to be executed. Once unscheduled, a callback must be re-scheduled in order to be activated again.
The callback is identified by the unique identifier returned by the appropriate schedule function. Hence this value should be saved if you ever wish to unschedule a callback.
id | The callback identifier |
|
inlinevirtual |
The method called to update the application during a non-deterministic loop.
This is method is provided your core application loop, provided that setDeterministic
is false. This method should be replaced with your custom implementation to define your application. This method should contain any code that is not an explicit drawing call. When overriding this method, you do not need to call the parent method at all. The default implmentation does nothing.
This method is not invoked if setDeterministic
is set to true. In that case, the application uses preUpdate
, fixedUpdate
, and postUpdate
instead.
Note that the time passed as a parameter is the time measured from the start of the previous frame to the start of the current frame. It is measured before any input or callbacks are processed.
dt | The amount of time (in seconds) since the last frame |
|
protected |
The asset directory of this application
|
protected |
The default background color of this application
|
protected |
The display bounds of this application
|
protected |
Whether to use a fixed timestep
|
protected |
The time step for the fixed loop
|
protected |
The target FPS of this application
|
protected |
Whether this application is running in fullscreen
|
protected |
Whether this application supports high dpi resolution
|
protected |
Whether this application supports multisampling
|
protected |
The name of this application
|
protected |
The organization name (company) of this application
|
protected |
The SAFE display bounds of this application
|
protected |
The save directory of this application
|
protected |
The current state of this application
|
staticprotected |
A weak pointer to the single application that is running
|
protected |
Whether to respect the display vsync