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