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