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