GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
Application.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_APPLICATION_APPLICATION_HPP
5#define GREM_APPLICATION_APPLICATION_HPP
6
8
14#include <GREM/core/time.hpp>
15
16#include <cstdlib> // EXIT_SUCCESS, EXIT_FAILURE
17
19
53 Duration tickInterval = duration_cast<Duration>(FloatSeconds{1.0f}) / 60; // 60 Hz
54
70
86#ifdef __EMSCRIPTEN__
87 {}
88#else
89 duration_cast<Duration>(FloatSeconds{1.0f}) / 480 // 480 Hz
90#endif
91 ;
92
113 Duration maxFrameTime = duration_cast<Duration>(FloatSeconds{1.0f}) / 5; // 5 Hz
114
127#ifdef __EMSCRIPTEN__
128 false
129#else
130 true
131#endif
132 ;
133};
134
143public:
150 GREM_API(application) explicit Application(const ApplicationOptions& options = {});
151
160 virtual ~Application() = default;
161
189 GREM_API(application) void run();
190
208 GREM_API(application) virtual void quit() noexcept;
209
217 [[nodiscard]] bool isRunning() const noexcept {
218 return running;
219 }
220
226 [[nodiscard]] bool isQuitting() const noexcept {
227 return quitting;
228 }
229
246 [[nodiscard]] size_t getLastSecondFrameCount() const noexcept {
247 return lastSecondFrameCount;
248 }
249
258 [[nodiscard]] Duration getTickInterval() const noexcept {
259 return latestTickInfo.tickInterval;
260 }
261
271 [[nodiscard]] Duration getMaxAccumulatedTickTime() const noexcept {
272 return maxAccumulatedTickTime;
273 }
274
284 [[nodiscard]] Duration getMinFrameTime() const noexcept {
285 return minFrameTime;
286 }
287
297 [[nodiscard]] Duration getMaxFrameTime() const noexcept {
298 return maxFrameTime;
299 }
300
305 GREM_API(application) void resetTickTimer() noexcept;
306
317 GREM_API(application) void setTickInterval(Duration newTickInterval) noexcept;
318
329 GREM_API(application) void setMaxAccumulatedTickTime(Duration newMaxAccumulatedTickTime) noexcept;
330
340 GREM_API(application) void setMinFrameTime(Duration newMinFrameTime) noexcept;
341
352 GREM_API(application) void setMaxFrameTime(Duration newMaxFrameTime) noexcept;
353
362 GREM_API(application) void setFrameRateLimiterSleepEnabled(bool newFrameRateLimiterSleepEnabled) noexcept;
363
370 [[nodiscard]] TickInfo getLatestTickInfo() const noexcept {
371 return latestTickInfo;
372 }
373
380 [[nodiscard]] FrameInfo getLatestFrameInfo() const noexcept {
381 return latestFrameInfo;
382 }
383
384protected:
405 virtual void update(FrameInfo frameInfo) {
406 (void)frameInfo;
407 }
408
428 virtual void tick(TickInfo tickInfo) {
429 (void)tickInfo;
430 }
431
445 virtual void skipTick(Duration tickInterval) {
446 (void)tickInterval;
447 }
448
468 virtual void display(FrameInfo frameInfo) {
469 (void)frameInfo;
470 }
471
472private:
473 GREM_API(application) void updateMaxTicksPerFrame() noexcept;
474
475 GREM_API(application) void runFrame();
476
477 Duration minFrameTime{};
478 Duration maxFrameTime{};
479 Duration maxAccumulatedTickTime{};
480 Clock::rep maxTicksPerFrame{};
481 TimePoint startTime{};
482 TimePoint latestFrameBeginTime{};
483 TimePoint latestTickProcessingEndTime{};
484 TimePoint latestFrameCountTime{};
485 ExponentialMovingAverage<FloatMilliseconds> frameRateLimiterSleepError{};
486 size_t lastSecondFrameCount = 0;
487 size_t frameCounter = 0;
488 TickInfo latestTickInfo{};
489 FrameInfo latestFrameInfo{};
490 bool frameRateLimiterSleepEnabled = false;
491 bool running = false;
492 bool quitting = false;
493};
494
498class ExitCode {
499public:
500 static const ExitCode SUCCESS;
501 static const ExitCode FAILURE;
502
511 constexpr explicit ExitCode(int value) noexcept
512 : value(value) {}
513
519 constexpr operator int() const noexcept {
520 return value;
521 }
522
523private:
524 int value;
525};
526
527inline constexpr ExitCode ExitCode::SUCCESS{EXIT_SUCCESS};
528inline constexpr ExitCode ExitCode::FAILURE{EXIT_FAILURE};
529
530} // namespace grem::application
531
532#endif
void setMaxAccumulatedTickTime(Duration newMaxAccumulatedTickTime) noexcept
Set the maximum accumulated tick time of the application.
virtual void tick(TickInfo tickInfo)
Fixed-rate tick callback, called in the main loop 0 or more times during tick processing,...
Definition Application.hpp:428
virtual void display(FrameInfo frameInfo)
Frame rendering callback, called in the main loop once at the end of each frame after processing tick...
Definition Application.hpp:468
void run()
Start the main loop of the application and keep running until the application quits or an unhandled e...
Duration getMinFrameTime() const noexcept
Get the minimum frame time of the application.
Definition Application.hpp:284
FrameInfo getLatestFrameInfo() const noexcept
Get the latest available frame information, including any configuration changes since the last frame.
Definition Application.hpp:380
bool isRunning() const noexcept
Check if the application is currently running, meaning that it is fully initialized,...
Definition Application.hpp:217
virtual void quit() noexcept
Initiate the shutdown process, meaning that the current frame will be the last to be processed before...
bool isQuitting() const noexcept
Check if the application is in the process of shutting down.
Definition Application.hpp:226
Application(const ApplicationOptions &options={})
Construct the base of the main application.
void setTickInterval(Duration newTickInterval) noexcept
Set the tick interval of the application.
virtual void skipTick(Duration tickInterval)
Callback for ticks that were skipped due to the accumulated tick time exceeding ApplicationOptions::m...
Definition Application.hpp:445
virtual void update(FrameInfo frameInfo)
Per-frame update callback, called in the main loop once at the beginning of each frame,...
Definition Application.hpp:405
Duration getMaxAccumulatedTickTime() const noexcept
Get the maximum accumulated tick time of the application.
Definition Application.hpp:271
Duration getMaxFrameTime() const noexcept
Get the maximum frame time of the application.
Definition Application.hpp:297
size_t getLastSecondFrameCount() const noexcept
Get the number of frames displayed during the last measured second of the application's run time,...
Definition Application.hpp:246
void setMaxFrameTime(Duration newMaxFrameTime) noexcept
Set the maximum frame time of the application.
TickInfo getLatestTickInfo() const noexcept
Get the latest available tick information, including any configuration changes since the last tick.
Definition Application.hpp:370
virtual ~Application()=default
Virtual destructor which must be overridden by the concrete application class in order to perform any...
void resetTickTimer() noexcept
Reset the internal tick accumulator, causing the next tick to start exactly one tick interval into th...
void setFrameRateLimiterSleepEnabled(bool newFrameRateLimiterSleepEnabled) noexcept
Enable or disable frame rate limiter sleep.
void setMinFrameTime(Duration newMinFrameTime) noexcept
Set the minimum frame time of the application.
Duration getTickInterval() const noexcept
Get the tick interval of the application.
Definition Application.hpp:258
static const ExitCode SUCCESS
Standard exit code value for indicating that the program completed successfully.
Definition Application.hpp:500
constexpr ExitCode(int value) noexcept
Construct an exit code.
Definition Application.hpp:511
static const ExitCode FAILURE
Standard exit code value for indicating that the program encountered an error.
Definition Application.hpp:501
Definition Application.hpp:18
Clock::duration Duration
Definition Clock.hpp:25
Clock::time_point TimePoint
Definition Clock.hpp:26
DurationBase< float, Ratio< 1, 1 > > FloatSeconds
Definition time.hpp:60
DurationBase< int64_t, Ratio< 1, 1 > > Seconds
Definition time.hpp:53
Configuration options for an Application.
Definition Application.hpp:23
Duration tickInterval
Desired average time between ticks, i.e. the reciprocal of the application's desired tick rate.
Definition Application.hpp:53
Duration minFrameTime
Minimum frame time before frames are delayed, i.e. the reciprocal of the application's maximum frame ...
Definition Application.hpp:85
Duration maxFrameTime
Maximum frame time before tick time is accumulated and deferred to later frames, i....
Definition Application.hpp:113
Duration maxAccumulatedTickTime
Maximum time to process accumulated ticks for before starting to skip ticks.
Definition Application.hpp:69
bool frameRateLimiterSleepEnabled
Put the thread that is running the application to sleep until the next frame is supposed to begin if ...
Definition Application.hpp:126
Transient information about the current frame of an Application.
Definition FrameInfo.hpp:16
Transient information about the current tick of an Application.
Definition TickInfo.hpp:17