Pioneer
Application.h
Go to the documentation of this file.
1 // Copyright © 2008-2023 Pioneer Developers. See AUTHORS.txt for details
2 // Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3 
4 #pragma once
5 
6 #include "RefCounted.h"
7 
8 #include <memory>
9 #include <queue>
10 #include <string>
11 
12 class JobQueue;
13 class SyncJobQueue;
14 class TaskGraph;
15 
16 class Application {
17 public:
18  Application();
19  virtual ~Application();
20 
21  class Lifecycle : public RefCounted {
22  public:
23  Lifecycle(){};
24  Lifecycle(bool profilerAccumulate) :
25  m_profilerAccumulate(profilerAccumulate){};
26  virtual ~Lifecycle(){};
27 
28  // Once called, the lifecycle is terminated at the end of the current update.
29  void RequestEndLifecycle() { m_endLifecycle = true; }
30 
31  protected:
32  friend class Application;
33 
34  // Called when the Lifecycle begins execution
35  virtual void Start(){};
36 
37  // Called in a continual loop and passed the time since the last invocation.
38  virtual void Update(float deltaTime) = 0;
39 
40  // Called when the lifecycle is leaving execution
41  // If a valid Lifecycle pointer is returned, it is run before any queued lifecycles.
42  virtual void End() {}
43 
44  // Set a lifecycle that should begin immediately after this lifecycle has finished execution
46  {
47  m_nextLifecycle = l;
48  }
49 
50  bool GetProfilerAccumulate() const { return m_profilerAccumulate; }
51 
52  void SetProfilerAccumulate(bool enable)
53  {
54  m_profilerAccumulate = enable;
55  }
56 
57  private:
58  // set to true when you want to accumulate all updates in a lifecycle into a single profile frame
59  bool m_profilerAccumulate = false;
60  bool m_endLifecycle = false;
61  RefCountedPtr<Lifecycle> m_nextLifecycle;
62  Application *m_application;
63  };
64 
65  // Add a lifecycle object to the queue of pending lifecycles
66  // You must add at least one lifecycle before calling Run()
68 
69  // Use this function very sparingly (e.g. when the application is shutting down)
70  void ClearQueuedLifecycles();
71 
72  // Runs the application as long as there is a valid lifecycle object
73  void Run();
74 
75  // Get the time between the start of the last update and the current one
76  float DeltaTime() { return m_deltaTime; }
77 
78  double GetTime() { return m_totalTime; }
79 
80  TaskGraph *GetTaskGraph() { return m_taskGraph.get(); }
81 
84 
85  void RequestProfileFrame(const std::string &path = "");
86 
87 protected:
88  // Hooks for inheriting classes to add their own behaviors to.
89 
90  // Runs before the main loop begins
91  virtual void Startup();
92 
93  // Runs after the main loop ends
94  virtual void Shutdown();
95 
96  // Handle running pinned tasks and processing queued jobs
97  virtual void HandleJobs();
98 
99  // Runs at the top of each frame.
100  virtual void BeginFrame() {}
101 
102  // Runs before each Update() call
103  virtual void PreUpdate() {}
104 
105  // Runs after each Update() call
106  virtual void PostUpdate() {}
107 
108  // Runs at the bottom of each frame.
109  virtual void EndFrame() {}
110 
111  // Request the application quit immediately at the end of the update,
112  // ignoring all queued lifecycles
113  void RequestQuit() { m_applicationRunning = false; }
114 
115  Lifecycle *GetActiveLifecycle() { return m_activeLifecycle.Get(); }
116  void SetProfilerPath(const std::string &);
117  void SetProfileSlowFrames(bool enabled) { m_doSlowProfile = enabled; }
118  void SetProfileZones(bool enabled) { m_profileZones = enabled; }
119  void SetProfileTrace(bool enabled) { m_profileTrace = enabled; }
120 
121 private:
122  bool StartLifecycle();
123  void EndLifecycle();
124 
125  bool m_applicationRunning = false;
126  bool m_doTempProfile = false;
127  bool m_doSlowProfile = false;
128  bool m_profileZones = false;
129  bool m_profileTrace = false;
130  float m_deltaTime = 0.f;
131  double m_totalTime = 0.f;
132 
133  std::string m_profilerPath;
134  std::string m_tempProfilePath;
135 
136  // The lifecycle we're actually running right now
137  RefCountedPtr<Lifecycle> m_activeLifecycle;
138 
139  // A lifecycle that should be run next before the rest of the queue.
140  RefCountedPtr<Lifecycle> m_priorityLifecycle;
141 
142  // Lifecycles queued by QueueLifecycle()
143  std::queue<RefCountedPtr<Lifecycle>> m_queuedLifecycles;
144 
145  std::unique_ptr<SyncJobQueue> m_syncJobQueue;
146  std::unique_ptr<TaskGraph> m_taskGraph;
147 };
Definition: Application.h:21
void RequestEndLifecycle()
Definition: Application.h:29
Lifecycle(bool profilerAccumulate)
Definition: Application.h:24
virtual void Start()
Definition: Application.h:35
bool GetProfilerAccumulate() const
Definition: Application.h:50
Lifecycle()
Definition: Application.h:23
void SetNextLifecycle(RefCountedPtr< Lifecycle > l)
Definition: Application.h:45
virtual ~Lifecycle()
Definition: Application.h:26
virtual void Update(float deltaTime)=0
void SetProfilerAccumulate(bool enable)
Definition: Application.h:52
virtual void End()
Definition: Application.h:42
Definition: Application.h:16
void SetProfilerPath(const std::string &)
Definition: Application.cpp:74
void RequestProfileFrame(const std::string &path="")
Definition: Application.cpp:61
void SetProfileTrace(bool enabled)
Definition: Application.h:119
void QueueLifecycle(RefCountedPtr< Lifecycle > cycle)
Definition: Application.cpp:23
TaskGraph * GetTaskGraph()
Definition: Application.h:80
void SetProfileSlowFrames(bool enabled)
Definition: Application.h:117
Lifecycle * GetActiveLifecycle()
Definition: Application.h:115
virtual void BeginFrame()
Definition: Application.h:100
void SetProfileZones(bool enabled)
Definition: Application.h:118
virtual void PostUpdate()
Definition: Application.h:106
virtual void HandleJobs()
Definition: Application.cpp:147
virtual ~Application()
Definition: Application.cpp:21
void ClearQueuedLifecycles()
Definition: Application.cpp:141
virtual void Shutdown()
Definition: Application.cpp:52
double GetTime()
Definition: Application.h:78
void Run()
Definition: Application.cpp:158
virtual void Startup()
Definition: Application.cpp:31
void RequestQuit()
Definition: Application.h:113
float DeltaTime()
Definition: Application.h:76
virtual void EndFrame()
Definition: Application.h:109
virtual void PreUpdate()
Definition: Application.h:103
JobQueue * GetSyncJobQueue()
Definition: Application.cpp:85
JobQueue * GetAsyncJobQueue()
Definition: Application.cpp:90
Application()
Definition: Application.cpp:20
Definition: JobQueue.h:108
Definition: RefCounted.h:11
T * Get() const
Definition: SmartPtr.h:37
Definition: JobQueue.h:131
Definition: TaskGraph.h:139