]> git.localhorst.tv Git - l2e.git/blob - src/app/State.h
58538faf99c70f33dd6b16c5b66dfb6a63cb3845
[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 protected:
43         /// Get a handle to the application this state is running on.
44         /// Do not call this while the state is off the stack (e.g. in c'tor/d'tor)
45         /// or you'll get a std::domain_error (potentially evil in d'tor)!
46         Application &Ctrl();
47         const Application &Ctrl() const;
48
49 private:
50         /// Do some setup that needs an application and/or screen handle and thus
51         /// can not be done by the constructor.
52         /// Called when the state first enters the stack.
53         /// @param ctrl the Application running the state
54         virtual void OnEnterState(SDL_Surface *screen) = 0;
55         /// Do some cleanup.
56         /// Called when the state is popped from the stack.
57         virtual void OnExitState(SDL_Surface *screen) = 0;
58         /// Called when the state becomes the active one.
59         virtual void OnResumeState(SDL_Surface *screen) = 0;
60         /// Called when the state becomes inactive.
61         virtual void OnPauseState(SDL_Surface *screen) = 0;
62
63         /// Adapt the state's graphics to given dimensions.
64         /// NOTE: currently, this is only called for the stack top and not
65         ///       propagated on stack changes.
66         ///       Will be fixed soom ;).
67         virtual void OnResize(int width, int height) = 0;
68
69 public:
70         /// Timers handle intended for graphics, sync'ed with render 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> &GraphicsTimers() { return graphicsTimers; }
74         /// Timers handle intended for graphics, sync'ed with world time.
75         /// These timers are only updated for the stack top and thus appear paused
76         /// when the state is visible (roughly).
77         Timers<Uint32> &PhysicsTimers() { return physicsTimers; }
78
79 private:
80         Application *ctrl;
81         Timers<Uint32> graphicsTimers;
82         Timers<Uint32> physicsTimers;
83
84 };
85
86 }
87
88 #endif /* APP_STATE_H_ */