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