-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModeManager.cpp
More file actions
157 lines (138 loc) · 2.63 KB
/
ModeManager.cpp
File metadata and controls
157 lines (138 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <apollo/Modes/ModeManager.h>
#include <SDL.h>
#include <apollo/Input.h>
#include <apollo/Logging.h>
#include <apollo/Graphics/Graphics.h>
#include <apollo/Sound/Sound.h>
static AppMode* mode = NULL;
static AppMode* nextMode = NULL;
int reference = 0;
class LuaMode : public AppMode
{
private:
LuaScript* script;
std::string name;
public:
LuaMode ( const std::string& _name )
: name(_name)
{
script = new LuaScript("Modes/" + _name);
}
virtual ~LuaMode ()
{
delete script;
}
virtual std::string Name ()
{
return name;
}
virtual void Init ()
{
if (reference == 0)
{
script->InvokeSubroutine("init");
}
else
{
script->InvokeSubroutine("init", reference);
}
}
virtual void Shutdown ()
{
script->InvokeSubroutine("shutdown");
}
virtual void Update ()
{
script->InvokeSubroutine("update");
}
virtual void Render ()
{
script->InvokeSubroutine("render");
}
virtual void HandleEvent ( const Input::Event& event )
{
vec2 mouseMapped = Graphics::MapPoint(event.mouse);
switch (event.type)
{
case Input::Event::KEYDOWN:
script->InvokeSubroutine("key", event.object);
break;
case Input::Event::KEYUP:
script->InvokeSubroutine("keyup", event.object);
break;
case Input::Event::CLICK:
script->InvokeSubroutine("mouse", event.object, mouseMapped.X(), mouseMapped.Y());
break;
case Input::Event::RELEASE:
script->InvokeSubroutine("mouse_up", event.object, mouseMapped.X(), mouseMapped.Y());
break;
case Input::Event::QUIT:
// TODO: interpret this better: maybe just handle it like an ESC?
QuitEngine();
break;
default: break;
}
}
};
AppMode* ActiveMode ()
{
return mode;
}
void InitModeManager ()
{
}
void UpdateModeManager ()
{
// usleep(125000); // debug line
if (nextMode != NULL)
{
if (mode)
{
mode->Shutdown();
delete mode;
}
LOG("ModeManager", LOG_MESSAGE, "Switching to mode %s", nextMode->Name().c_str());
mode = nextMode;
nextMode = NULL;
if (mode)
{
mode->Init();
}
}
if (mode)
{
mode->Update();
Input::Pump();
while (Input::Event* event = Input::Next())
{
mode->HandleEvent(*event);
}
mode->Render();
}
}
const char* QueryMode()
{
return ActiveMode()->Name().c_str();
}
void SwitchMode ( const std::string& newmode )
{
LuaMode* newMode = new LuaMode ( newmode );
SwitchMode ( newMode );
}
void SwitchMode ( const std::string& newmode, int ref )
{
reference = ref;
LuaMode* newMode = new LuaMode ( newmode );
SwitchMode ( newMode );
}
void SwitchMode ( AppMode* newmode )
{
if (nextMode) delete nextMode;
nextMode = newmode;
}
void QuitEngine()
{
Sound::Quit();
exit(0);
}
//