HoverRace  2.0
Scene.h
Go to the documentation of this file.
1 
2 // Scene.h
3 //
4 // Copyright (c) 2010, 2013-2015 Michael Imamura.
5 //
6 // Licensed under GrokkSoft HoverRace SourceCode License v1.0(the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // A copy of the license should have been attached to the package from which
10 // you have taken this file. If you can not find the license you can not use
11 // this file.
12 //
13 //
14 // The author makes no representations about the suitability of
15 // this software for any purpose. It is provided "as is" "AS IS",
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
17 // implied.
18 //
19 // See the License for the specific language governing permissions
20 // and limitations under the License.
21 
22 #pragma once
23 
24 #include "../../engine/Util/OS.h"
25 #include "../../engine/Util/Profiler.h"
26 
27 namespace HoverRace {
28  namespace Control {
29  class InputEventController;
30  }
31 }
32 
33 namespace HoverRace {
34 namespace Client {
35 
43 class Scene
44 {
45 public:
46  enum class Phase
47  {
48  INITIALIZING,
49  STARTING,
50  RUNNING,
51  STOPPING,
52  STOPPED,
53  };
54  enum class State
55  {
56  INITIALIZING,
57  BACKGROUND,
58  RAISING,
59  FOREGROUND,
60  LOWERING,
61  };
62 
63 protected:
69  struct ConnList
70  {
71  public:
73  {
74  Clear();
75  }
76 
77  template<class T>
78  ConnList &operator<<(T &&conn)
79  {
80  conns.emplace_front(std::forward<decltype(conn)>(conn));
81  return *this;
82  }
83 
84  // This can be noexcept since we check for empty().
85  void Clear() noexcept
86  {
87  // Disconnect the connections in reverse order of add.
88  while (!conns.empty()) {
89  conns.pop_front();
90  }
91  }
92 
93  private:
94  std::forward_list<boost::signals2::scoped_connection> conns;
95  };
96 
97 public:
98  Scene(const std::string &name = "Unnamed Scene");
99  virtual ~Scene() { }
100 
101  Scene &operator=(const Scene&) = delete;
102 
103 public:
110  const std::string &GetName() const { return name; }
111 
118  virtual std::ostream &OutputDebugText(std::ostream &oss) const { return oss; }
119 
128  void AttachInput(Control::InputEventController &controller);
129 
136  void DetachInput(Control::InputEventController &controller);
137 
138 protected:
151  virtual void AttachController(Control::InputEventController &controller,
152  ConnList &conns) = 0;
153 
161  virtual void DetachController(Control::InputEventController &controller,
162  ConnList &conns) = 0;
163 
164 public:
180  virtual void OnScenePushed() { }
181 
187  virtual bool IsMouseCursorEnabled() const = 0;
188 
189 private:
191  {
192  return (tick >= prevTick) ?
193  Util::OS::TimeDiff(tick, prevTick) :
194  0;
195  }
196 
197 public:
198  Phase GetPhase() const { return phase; }
199  bool SetPhase(Phase phase);
200 
201  State GetState() const { return state; }
202  bool MoveToForeground();
203  bool MoveToBackground();
204 private:
205  bool SetState(State state);
206 
207 protected:
224  {
225  phaseTransitionDuration = static_cast<double>(ms);
226  }
227 
236  {
237  return (curTime >= phaseTs) ?
238  Util::OS::TimeDiff(curTime, phaseTs) :
239  0;
240  }
241 
252  {
253  return (phase == Phase::STARTING) ?
254  GetPhaseDuration() :
255  startingPhaseTime;
256  }
257 
266  {
267  return (curTime >= stateTs) ?
268  Util::OS::TimeDiff(curTime, stateTs) :
269  0;
270  }
271 
288  {
289  stateTransitionDuration = ms;
290  stateTransitionVelocity =
291  (ms == 0) ? 1.0 : (1.0 / static_cast<double>(ms));
292  }
293 
294 protected:
300  virtual void OnPhaseChanged(Phase oldPhase) { HR_UNUSED(oldPhase); }
301 
307  virtual void OnStateChanged(State oldState) { HR_UNUSED(oldState); }
308 
316  virtual void OnPhaseTransition(double progress) { HR_UNUSED(progress); }
317 
325  virtual void OnStateTransition(double progress) { HR_UNUSED(progress); }
326 
327 public:
329  {
330  Util::Profiler::Sampler rootSampler(*rootProfiler);
331  Util::Profiler::Sampler sampler(*advanceProfiler);
332  Advance(tick);
333  }
334 
336  {
337  Util::Profiler::Sampler rootSampler(*rootProfiler);
338  Util::Profiler::Sampler sampler(*prepareProfiler);
339  PrepareRender();
340  }
341 
342  void RenderScene()
343  {
344  Util::Profiler::Sampler rootSampler(*rootProfiler);
345  Util::Profiler::Sampler sampler(*renderProfiler);
346  Render();
347  }
348 
349 protected:
350  virtual void Advance(Util::OS::timestamp_t tick);
351  virtual void PrepareRender() { }
352  virtual void Render() = 0;
353 
354 private:
355  std::string name;
367 protected:
368  std::shared_ptr<Util::Profiler> rootProfiler;
369  std::shared_ptr<Util::Profiler> advanceProfiler;
370  std::shared_ptr<Util::Profiler> prepareProfiler;
371  std::shared_ptr<Util::Profiler> renderProfiler;
372 };
373 
374 } // namespace HoverScript
375 } // namespace Client
constexpr timestamp_t TimeDiff(timestamp_t laterTs, timestamp_t earlierTs) noexcept
Calculate the difference between two timestamps.
Definition: OS.h:82
Util::OS::timestamp_t TimeSincePrevTick(Util::OS::timestamp_t tick)
Definition: Scene.h:190
Util::OS::timestamp_t GetStartingPhaseTime()
Return how long the starting phase lasted.
Definition: Scene.h:251
Util::OS::timestamp_t phaseTs
When current phase was started.
Definition: Scene.h:359
void RenderScene()
Definition: Scene.h:342
std::forward_list< boost::signals2::scoped_connection > conns
Definition: Scene.h:94
void Clear() noexcept
Definition: Scene.h:85
ConnList & operator<<(T &&conn)
Definition: Scene.h:78
void AdvanceScene(Util::OS::timestamp_t tick)
Definition: Scene.h:328
std::shared_ptr< Util::Profiler > renderProfiler
Definition: Scene.h:371
Phase GetPhase() const
Definition: Scene.h:198
void PrepareScene()
Definition: Scene.h:335
std::string name
Definition: Scene.h:355
void SetPhaseTransitionDuration(Util::OS::timestamp_t ms)
Set the maximum duration of the starting or stopping phases.
Definition: Scene.h:223
virtual ~Scene()
Definition: Scene.h:99
Util::OS::timestamp_t prevTick
Definition: Scene.h:356
Util::OS::timestamp_t GetStateDuration(Util::OS::timestamp_t curTime=Util::OS::Time())
Calculate how much time we&#39;ve spent in the current state.
Definition: Scene.h:264
Definition: Profiler.h:53
std::array< boost::signals2::scoped_connection, 2 > conns
Definition: ProfileEditScene.cpp:152
Util::OS::timestamp_t stateTs
When current state was started.
Definition: Scene.h:363
Util::OS::timestamp_t stateTransitionDuration
Definition: Scene.h:362
State
Definition: Scene.h:54
double phaseTransitionDuration
Definition: Scene.h:358
~ConnList()
Definition: Scene.h:72
virtual void OnStateTransition(double progress)
Fired during the raising and lowering states, if SetStateTransitionDuration() was set...
Definition: Scene.h:325
timestamp_t Time()
Retrieve a timestamp.
Definition: OS.cpp:217
virtual void OnScenePushed()
Called when the scene is actually pushed to the stage.
Definition: Scene.h:180
virtual void OnPhaseTransition(double progress)
Fired during the starting and stopping phases, if SetPhaseTransitionDuration() was set...
Definition: Scene.h:316
State GetState() const
Definition: Scene.h:201
virtual void OnPhaseChanged(Phase oldPhase)
Fired immediately after entering a new phase.
Definition: Scene.h:300
std::shared_ptr< Util::Profiler > prepareProfiler
Definition: Scene.h:370
double statePosition
Definition: Scene.h:365
Scenes are analogous to fullscreen windows that are layered on top of each other. ...
Definition: Scene.h:43
virtual std::ostream & OutputDebugText(std::ostream &oss) const
Output a stream of debug information describing this scene.
Definition: Scene.h:118
virtual void PrepareRender()
Definition: Scene.h:351
const std::string & GetName() const
Retrieve the name of the scene.
Definition: Scene.h:110
State state
Definition: Scene.h:361
Tracks connections as a group.
Definition: Scene.h:69
Phase
Definition: Scene.h:46
Definition: Announcement.h:24
std::shared_ptr< Util::Profiler > advanceProfiler
Definition: Scene.h:369
double stateTransitionVelocity
Definition: Scene.h:364
Util::OS::timestamp_t startingPhaseTime
Definition: Scene.h:360
virtual void OnStateChanged(State oldState)
Fired immediately after entering a new state.
Definition: Scene.h:307
std::shared_ptr< Util::Profiler > rootProfiler
Definition: Scene.h:368
void SetStateTransitionDuration(Util::OS::timestamp_t ms)
Set the maximum duration of the raising or lowering state.
Definition: Scene.h:287
Util::OS::timestamp_t GetPhaseDuration(Util::OS::timestamp_t curTime=Util::OS::Time())
Calculate how much time we&#39;ve spent in the current phase.
Definition: Scene.h:234
ConnList attachConns
Connected signals while controller is attached.
Definition: Scene.h:366
Phase phase
Definition: Scene.h:357
Translates input events into actions.
Definition: Controller.h:106
MR_Int64 timestamp_t
Definition: OS.h:55