Foray Library
rapid prototyping framework for crossplatform development of vulkan hardware ray tracing applications
Loading...
Searching...
No Matches
foray_renderloop.hpp
Go to the documentation of this file.
1#pragma once
2#include "../foray_basics.hpp"
3#include <functional>
4#include <limits>
5#include <list>
6#include <vector>
7
8namespace foray::base {
9
11 enum class ELifetimeState
12 {
13 PreInit,
15 Running,
19 };
20
22
25 {
26 public:
28 inline void SetFramesPerSecond(float value) { mSecondsPerFrame = 1.f / value; }
30 inline void SetSecondsPerFrame(float value) { mSecondsPerFrame = value; }
32 inline float GetFramesPerSecond() const { return 1.f / mSecondsPerFrame; }
34 inline float GetSecondsPerFrame() const { return mSecondsPerFrame; }
35
37 inline void Set60Fps() { mSecondsPerFrame = 1.f / 60.f; }
39 inline void DisableFpsLimit() { mSecondsPerFrame = 0; }
40
41 protected:
43 };
44
47 {
48 public:
50 {
53 uint64_t LoopFrameNumber = 0;
55 };
56
58 using InitFunctionPointer = std::function<void()>;
60 using RenderFunctionPointer = std::function<void(RenderInfo&)>;
62 using RenderReadyFunctionPointer = std::function<bool()>;
64 using DestroyFunctionPointer = std::function<void()>;
66 using PollEventsFunctionPointer = std::function<void()>;
69
70 inline RenderLoop() = default;
79 RenderFunctionPointer renderFunc,
80 RenderReadyFunctionPointer renderReadyFunc,
81 DestroyFunctionPointer destroyFunc,
82 PollEventsFunctionPointer pollEventsFunc,
83 OnStateChangedFunctionPointer onStateChangedFunc)
84 : mInitFunc{initFunc}
85 , mRenderFunc{renderFunc}
86 , mRenderReadyFunc{renderReadyFunc}
87 , mDestroyFunc{destroyFunc}
88 , mPollEventsFunc{pollEventsFunc}
89 , mOnStateChangedFunc{onStateChangedFunc} {};
90
93 int32_t Run();
96 void RequestStop(int32_t runResult = 0);
98 bool IsRunning() const;
99
102 {
106 uint32_t Count = 0;
108 fp32_t MinFrameTime = std::numeric_limits<fp32_t>::infinity();
113 };
114
118 void GetFrameTimes(std::vector<fp32_t>& outFrameTimes) const;
119
126
127 FORAY_GETTER_V(State)
128
129 FORAY_GETTER_MR(FrameTiming)
130 FORAY_PROPERTY_V(MaxFrameTimeAge)
131
132 protected:
134
141
143 int32_t mRunResult = 0;
144
147
148
150 {
153 };
154
155 std::list<FrameTime> mFrameTimes;
157 };
158} // namespace foray::base
Controls an apps render scheduling timing.
Definition foray_renderloop.hpp:25
void DisableFpsLimit()
Disables the Fps limit.
Definition foray_renderloop.hpp:39
fp32_t mSecondsPerFrame
Definition foray_renderloop.hpp:42
void SetFramesPerSecond(float value)
Set the target render calls per second.
Definition foray_renderloop.hpp:28
float GetFramesPerSecond() const
Gets target frames per second.
Definition foray_renderloop.hpp:32
void SetSecondsPerFrame(float value)
Set the target time per frame.
Definition foray_renderloop.hpp:30
void Set60Fps()
Sets the target Fps to 60 (0.166667 seconds per frame)
Definition foray_renderloop.hpp:37
float GetSecondsPerFrame() const
Gets target seconds per frame.
Definition foray_renderloop.hpp:34
Manages a single threaded, automatically balancing application lifetime.
Definition foray_renderloop.hpp:47
RenderLoop & SetInitFunc(InitFunctionPointer func)
RenderLoop & SetRenderReadyFunc(RenderReadyFunctionPointer func)
RenderLoop & SetRenderFunc(RenderFunctionPointer func)
DestroyFunctionPointer mDestroyFunc
Definition foray_renderloop.hpp:138
RenderReadyFunctionPointer mRenderReadyFunc
Definition foray_renderloop.hpp:137
std::function< void(RenderInfo &)> RenderFunctionPointer
Function pointer for a single frame render action. Param#0 : Delta time in seconds.
Definition foray_renderloop.hpp:60
FrameTimeAnalysis AnalyseFrameTimes() const
Analyse all stored frame times. Note: MaxFrameTimeAge member determines maximum possible frame time a...
RenderLoop & SetDestroyFunc(DestroyFunctionPointer func)
void GetFrameTimes(std::vector< fp32_t > &outFrameTimes) const
Get all stored frame times (in seconds) by pushing them on the vector.
PollEventsFunctionPointer mPollEventsFunc
Definition foray_renderloop.hpp:139
RenderLoop & SetPollEventsFunc(PollEventsFunctionPointer func)
ELifetimeState mState
Definition foray_renderloop.hpp:142
bool IsRunning() const
Returns true, if the internal state is ELifetimeState::Running; false otherwise.
fp32_t mMaxFrameTimeAge
Definition foray_renderloop.hpp:156
uint64_t mRenderedFrameCount
Definition foray_renderloop.hpp:146
void RequestStop(int32_t runResult=0)
Request the finalization of the application as soon as possible.
InitFunctionPointer mInitFunc
Definition foray_renderloop.hpp:135
std::function< void()> DestroyFunctionPointer
Function pointer for application finalization.
Definition foray_renderloop.hpp:64
int32_t Run()
Runs the application through its full lifetime, invoking all function pointers which have been set.
OnStateChangedFunctionPointer mOnStateChangedFunc
Definition foray_renderloop.hpp:140
int32_t mRunResult
Definition foray_renderloop.hpp:143
std::function< void()> InitFunctionPointer
Function pointer for application initialization.
Definition foray_renderloop.hpp:58
std::function< void()> PollEventsFunctionPointer
Function pointer for system event polling and handling.
Definition foray_renderloop.hpp:66
std::function< void(ELifetimeState, ELifetimeState)> OnStateChangedFunctionPointer
Function pointer for a callback when the renderloops state changes. Param#0: Old state Param#1: Next ...
Definition foray_renderloop.hpp:68
RenderLoop(InitFunctionPointer initFunc, RenderFunctionPointer renderFunc, RenderReadyFunctionPointer renderReadyFunc, DestroyFunctionPointer destroyFunc, PollEventsFunctionPointer pollEventsFunc, OnStateChangedFunctionPointer onStateChangedFunc)
Constructor.
Definition foray_renderloop.hpp:78
std::list< FrameTime > mFrameTimes
Definition foray_renderloop.hpp:155
RenderLoop & SetOnStateChangedFunc(OnStateChangedFunctionPointer func)
std::function< bool()> RenderReadyFunctionPointer
Function pointer for the RenderLoop to check if application is ready to render next frame....
Definition foray_renderloop.hpp:62
RenderFunctionPointer mRenderFunc
Definition foray_renderloop.hpp:136
AppFrameTiming mFrameTiming
Definition foray_renderloop.hpp:145
#define FORAY_GETTER_V(member)
Return value.
Definition foray_basics.hpp:39
#define FORAY_GETTER_MR(member)
Return mutable reference.
Definition foray_basics.hpp:54
#define FORAY_PROPERTY_V(member)
Getter+Setter shorthand for value types.
Definition foray_basics.hpp:81
Definition foray_base_declares.hpp:3
void PrintStateChange(ELifetimeState oldState, ELifetimeState newState)
ELifetimeState
The lifetime states an application progresses through chronologically.
Definition foray_renderloop.hpp:12
float fp32_t
stdint.h style 32 bit floating point type alias (float)
Definition foray_basics.hpp:13
double fp64_t
stdint.h style 64 bit floating point type alias (double)
Definition foray_basics.hpp:15
Analysis.
Definition foray_renderloop.hpp:102
fp32_t MaxFrameTime
Maximum frame time in seconds recorded.
Definition foray_renderloop.hpp:110
fp32_t MinFrameTime
Minimum frame time in seconds recorded.
Definition foray_renderloop.hpp:108
fp32_t TotalTime
Total time in seconds of the analyzed frametimes.
Definition foray_renderloop.hpp:104
uint32_t Count
Count of frametimes analyzed.
Definition foray_renderloop.hpp:106
fp32_t AvgFrameTime
Average frame time in seconds recorded.
Definition foray_renderloop.hpp:112
Definition foray_renderloop.hpp:150
fp64_t Timestamp
Definition foray_renderloop.hpp:152
fp32_t Delta
Definition foray_renderloop.hpp:151
Definition foray_renderloop.hpp:50
fp32_t TargetDelta
Definition foray_renderloop.hpp:52
fp64_t SinceStart
Definition foray_renderloop.hpp:54
fp32_t Delta
Definition foray_renderloop.hpp:51
uint64_t LoopFrameNumber
Definition foray_renderloop.hpp:53