]> git.localhorst.tv Git - l2e.git/blob - src/app/State.h
added option for sped up integration
[l2e.git] / src / app / State.h
1 #ifndef APP_APPLICATIONSTATE_H_
2 #define APP_APPLICATIONSTATE_H_
3
4 namespace app {
5         class Application;
6         class Input;
7 }
8
9 #include "Timer.h"
10
11 #include <SDL.h>
12
13 namespace app {
14
15 /// Application state base class for use with app::Application.
16 class State {
17
18 public:
19         State();
20         virtual ~State();
21
22 public:
23         /// Called by Application when pushing this state.
24         void EnterState(Application &ctrl, SDL_Surface *screen);
25         /// Called by Application when popping this state.
26         void ExitState(Application &ctrl, SDL_Surface *screen);
27         /// Called by Application when this state becomes the top state.
28         void ResumeState(SDL_Surface *screen);
29         /// Called by Application when this state no longer is the top state.
30         void PauseState(SDL_Surface *screen);
31
32         /// Called by Application on SDL window resize events.
33         void Resize(int width, int height);
34
35         /// Handle interactive events such as input and timers.
36         virtual void HandleEvents(const Input &) = 0;
37         /// Update the time-dependant world representation.
38         virtual void UpdateWorld(Uint32 deltaMs) = 0;
39         /// Draw a picture of the world.
40         virtual void Render(SDL_Surface *) = 0;
41
42         /// Number of milliseconds per simulation frame.
43         Uint32 Timestep() const { return timestep; }
44
45 protected:
46         /// Get a handle to the application this state is running on.
47         /// Do not call this while the state is off the stack (e.g. in c'tor/d'tor)
48         /// or you'll get a std::domain_error (potentially evil in d'tor)!
49         Application &Ctrl();
50         const Application &Ctrl() const;
51
52         /// Set the number of milliseconds per simulation frame.
53         void Timestep(Uint32 t) { timestep = t; }
54
55 private:
56         /// Do some setup that needs an application and/or screen handle and thus
57         /// can not be done by the constructor.
58         /// Called when the state first enters the stack.
59         /// @param ctrl the Application running the state
60         virtual void OnEnterState(SDL_Surface *screen) = 0;
61         /// Do some cleanup.
62         /// Called when the state is popped from the stack.
63         virtual void OnExitState(SDL_Surface *screen) = 0;
64         /// Called when the state becomes the active one.
65         virtual void OnResumeState(SDL_Surface *screen) = 0;
66         /// Called when the state becomes inactive.
67         virtual void OnPauseState(SDL_Surface *screen) = 0;
68
69         /// Adapt the state's graphics to given dimensions.
70         /// NOTE: currently, this is only called for the stack top and not
71         ///       propagated on stack changes.
72         ///       Will be fixed sometime ;).
73         virtual void OnResize(int width, int height) = 0;
74
75 public:
76         /// Timers handle intended for graphics, sync'ed with render time.
77         /// These timers are only updated for the stack top and thus appear paused
78         /// when the state is invisible (roughly).
79         Timers<Uint32> &GraphicsTimers() { return graphicsTimers; }
80         /// Timers handle intended for graphics, sync'ed with world time.
81         /// These timers are only updated for the stack top and thus appear paused
82         /// when the state is invisible (roughly).
83         Timers<Uint32> &PhysicsTimers() { return physicsTimers; }
84
85 private:
86         Application *ctrl;
87         Timers<Uint32> graphicsTimers;
88         Timers<Uint32> physicsTimers;
89         Uint32 timestep;
90
91 };
92
93 }
94
95 #endif