]> git.localhorst.tv Git - l2e.git/blob - src/app/Application.h
e29bc63feb885e613efea2fd998565d48803c144
[l2e.git] / src / app / Application.h
1 #ifndef APP_APPLICATION_H_
2 #define APP_APPLICATION_H_
3
4 namespace app {
5         class State;
6 }
7 namespace sdl {
8         class InitScreen;
9 }
10
11 #include "Input.h"
12 #include "Timer.h"
13
14 #include <stack>
15 #include <queue>
16 #include <SDL.h>
17
18
19 namespace app {
20
21 /// Application controller class.
22 /// Operates on a state stack that can be modified via ChangeState, PushState,
23 /// and PopState.
24 /// All state changes are delayed until the looping mechanism gets control again
25 /// (i.e. after a top level state function returns, not during).
26 /// SDL key events are preprocessed, see app::Input.
27 /// The quit event (typically window closed or signal received) is caught and
28 /// results in immediate (that is, after the next input loop) termination.
29 /// Popped states will be deleted via the plain delete operator on an app::State
30 /// pointer.
31 /// Timers created by GlobalTimers() operate on actual application time and are
32 /// not paused when the current state is paused (as are the timers started by
33 /// the app::State members).
34 class Application {
35
36 public:
37         Application(sdl::InitScreen &screen, State *initialState);
38         ~Application();
39 private:
40         Application(const Application &);
41         Application &operator =(const Application &);
42
43 public:
44         void Run();
45         void Loop();
46
47 public:
48         void ChangeState(State *);
49         void PushState(State *);
50         void PopState();
51         void Quit();
52         Input &Buttons() { return input; }
53         const Input &Buttons() const { return input; }
54         Timers<Uint32> &GlobalTimers() { return globalTimers; }
55
56 private:
57         struct StateCommand {
58                 enum Type {
59                         PUSH, POP, CHANGE
60                 } type;
61                 State *state;
62         };
63
64 private:
65         State *CurrentState();
66         bool StateChangePending() const { return !stateChanges.empty(); }
67         void UpdateState();
68         void RealChangeState(State *);
69         void RealPushState(State *);
70         void RealPopState();
71         void PopAllStates();
72
73 private:
74         void HandleEvents();
75         void UpdateWorld(Uint32 deltaT);
76         void Render();
77
78 private:
79         sdl::InitScreen &screen;
80         std::stack<State *> states;
81         std::queue<StateCommand> stateChanges;
82         Timers<Uint32> globalTimers;
83         Input input;
84         Uint32 last;
85         bool inStateChage;
86
87 };
88
89 }
90
91 #endif /* APP_APPLICATION_H_ */