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