libdonut  2.3.2
Application framework for cross-platform game development in C++20
Application.hpp
Go to the documentation of this file.
1 #ifndef DONUT_APPLICATION_APPLICATION_HPP
2 #define DONUT_APPLICATION_APPLICATION_HPP
3 
6 
7 #include <chrono> // std::chrono::...
8 
9 namespace donut::application {
10 
39  float tickRate = 60.0f;
40 
59  float minFrameRate = 1.0f;
60 
74  float maxFrameRate = 480.0f;
75 
89 
114  std::chrono::steady_clock::duration frameRateLimiterSleepBias = std::chrono::microseconds{100};
115 };
116 
131 class Application {
132 public:
139  explicit Application(const ApplicationOptions& options = {});
140 
149  virtual ~Application() = default;
150 
175  void run();
176 
188  virtual void quit();
189 
197  [[nodiscard]] bool isRunning() const noexcept {
198  return running;
199  }
200 
217  [[nodiscard]] unsigned getLastSecondFrameCount() const noexcept {
218  return lastSecondFrameCount;
219  }
220 
238  void setFrameRateParameters(float tickRate, float minFrameRate, float maxFrameRate);
239 
250  void setFrameRateLimiterSleepEnabled(bool frameRateLimiterSleepEnabled);
251 
261  void setFrameRateLimiterSleepBias(std::chrono::steady_clock::duration frameRateLimiterSleepBias);
262 
268  [[nodiscard]] TickInfo getLatestTickInfo() const noexcept {
269  return tickInfo;
270  }
271 
277  [[nodiscard]] FrameInfo getLatestFrameInfo() const noexcept {
278  return frameInfo;
279  }
280 
281 protected:
304  virtual void update(FrameInfo frameInfo) {
305  (void)frameInfo;
306  }
307 
328  virtual void tick(TickInfo tickInfo) {
329  (void)tickInfo;
330  }
331 
354  virtual void display(TickInfo tickInfo, FrameInfo frameInfo) {
355  (void)tickInfo;
356  (void)frameInfo;
357  }
358 
359 private:
360  void runFrame();
361 
362  using Clock = std::chrono::steady_clock;
363 
364  Clock::duration tickInterval{};
365  Clock::duration minFrameInterval{};
366  Clock::rep maxTicksPerFrame{};
367  Clock::time_point startTime{};
368  Clock::time_point latestFrameTime{};
369  Clock::time_point latestTickProcessingEndTime{};
370  Clock::time_point latestFrameCountTime{};
371  unsigned lastSecondFrameCount = 0u;
372  unsigned frameCounter = 0u;
373  TickInfo tickInfo{};
374  FrameInfo frameInfo{};
375  Clock::duration frameRateLimiterSleepBias{};
376  bool frameRateLimiterSleepEnabled = false;
377  bool running = false;
378 };
379 
380 } // namespace donut::application
381 
382 #endif
Main application base class.
Definition: Application.hpp:131
virtual ~Application()=default
Virtual destructor which must be overridden by the concrete application class in order to perform any...
unsigned getLastSecondFrameCount() const noexcept
Get the number of frames displayed during the last measured second of the application's run time,...
Definition: Application.hpp:217
virtual void quit()
Initiate the shutdown process, meaning that the current frame will be the last to be processed and di...
void setFrameRateLimiterSleepEnabled(bool frameRateLimiterSleepEnabled)
Enable or disable frame rate limiter sleep.
virtual void update(FrameInfo frameInfo)
Per-frame update callback, called in the main loop once at the beginning of each frame,...
Definition: Application.hpp:304
FrameInfo getLatestFrameInfo() const noexcept
Get information about the latest frame.
Definition: Application.hpp:277
void setFrameRateParameters(float tickRate, float minFrameRate, float maxFrameRate)
Set the frame rate parameters of the application.
TickInfo getLatestTickInfo() const noexcept
Get information about the latest tick.
Definition: Application.hpp:268
virtual void tick(TickInfo tickInfo)
Fixed-rate tick callback, called in the main loop 0 or more times during tick processing,...
Definition: Application.hpp:328
void setFrameRateLimiterSleepBias(std::chrono::steady_clock::duration frameRateLimiterSleepBias)
Set the frame rate limiter sleep bias.
virtual void display(TickInfo tickInfo, FrameInfo frameInfo)
Frame rendering callback, called in the main loop once at the end of each frame after processing tick...
Definition: Application.hpp:354
void run()
Start the main loop of the application and keep running until the application quits or an unhandled e...
bool isRunning() const noexcept
Check if the application is currently running, meaning that it is fully initialized,...
Definition: Application.hpp:197
Application(const ApplicationOptions &options={})
Construct the base of the main application.
Definition: Application.hpp:9
Configuration options for an Application.
Definition: Application.hpp:14
float maxFrameRate
Maximum frame rate of the application, in hertz (frames per second), before frames are delayed.
Definition: Application.hpp:74
float minFrameRate
Minimum frame rate of the application, in hertz (frames per second), before tick slowdown occurs.
Definition: Application.hpp:59
float tickRate
The tick rate of the application, in hertz (ticks per second).
Definition: Application.hpp:39
std::chrono::steady_clock::duration frameRateLimiterSleepBias
The duration offset to subtract from the requested wake-up time when frame rate limiter sleep is enab...
Definition: Application.hpp:114
bool frameRateLimiterSleepEnabled
Put the thread that is running the application to sleep until the next frame is supposed to begin if ...
Definition: Application.hpp:88
Transient information about the current frame of an Application.
Definition: FrameInfo.hpp:11
Transient information about the current tick of an Application.
Definition: TickInfo.hpp:13