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