1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Benjamin Grauer |
---|
13 | co-programmer: ... |
---|
14 | */ |
---|
15 | |
---|
16 | #include "framework.h" |
---|
17 | |
---|
18 | |
---|
19 | #include "p_node.h" |
---|
20 | #include "null_parent.h" |
---|
21 | #include "state.h" |
---|
22 | #include "debug.h" |
---|
23 | #include "graphics_engine.h" |
---|
24 | #include "light.h" |
---|
25 | #include "resource_manager.h" |
---|
26 | #include "camera.h" |
---|
27 | |
---|
28 | |
---|
29 | int verbose; |
---|
30 | |
---|
31 | void* Framework::mainLoop(void* tmp) |
---|
32 | { |
---|
33 | Framework* framework = Framework::getInstance(); |
---|
34 | while(!framework->isFinished) |
---|
35 | { |
---|
36 | #ifdef GUI_MODULE |
---|
37 | while(gtk_events_pending()) |
---|
38 | gtk_main_iteration(); |
---|
39 | #endif |
---|
40 | // keyhandler returns false if sdl gets quit by some event |
---|
41 | framework->eventHandler(); |
---|
42 | |
---|
43 | // tick the scene |
---|
44 | float dt = framework->tick(); |
---|
45 | |
---|
46 | NullParent::getInstance()->update(dt); |
---|
47 | |
---|
48 | // Draw the scene |
---|
49 | framework->draw(dt); |
---|
50 | |
---|
51 | } |
---|
52 | } |
---|
53 | |
---|
54 | bool Framework::draw(float dt) |
---|
55 | { |
---|
56 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); |
---|
57 | glLoadIdentity(); // Reset the view |
---|
58 | |
---|
59 | this->moduleDraw(dt); |
---|
60 | |
---|
61 | camera->apply(); |
---|
62 | |
---|
63 | SDL_GL_SwapBuffers(); // Swap the buffers |
---|
64 | } |
---|
65 | |
---|
66 | |
---|
67 | float Framework::tick() |
---|
68 | { |
---|
69 | currFrame = SDL_GetTicks(); |
---|
70 | float dt = (float)(currFrame - lastFrame) / 1000.0; |
---|
71 | lastFrame = currFrame; |
---|
72 | |
---|
73 | this->moduleTick(dt); |
---|
74 | |
---|
75 | return dt; |
---|
76 | } |
---|
77 | |
---|
78 | |
---|
79 | bool Framework::eventHandler() |
---|
80 | { |
---|
81 | // This is the main loop for the entire program and it will run until done==TRUE |
---|
82 | { |
---|
83 | // And poll for events |
---|
84 | SDL_Event event; |
---|
85 | while(SDL_PollEvent(&event)) |
---|
86 | { |
---|
87 | moduleEventHandler(&event); |
---|
88 | |
---|
89 | switch (event.type) { |
---|
90 | case SDL_MOUSEMOTION: |
---|
91 | { |
---|
92 | Vector view = camera->getTarget()->getAbsCoor() - camera->getAbsCoor(); |
---|
93 | Vector up = Vector(0, 1, 0); |
---|
94 | up = camera->getAbsDir().apply(up); |
---|
95 | Vector h = up.cross(view); |
---|
96 | Vector v = h.cross(view); |
---|
97 | h.normalize(); |
---|
98 | v.normalize(); |
---|
99 | |
---|
100 | if (mouseDown[1]) |
---|
101 | camera->setRelCoor(camera->getRelCoor()+ h * event.motion.xrel *.05 - v * event.motion.yrel * .05); |
---|
102 | if (mouseDown[3]) |
---|
103 | camera->getTarget()->setRelCoor(camera->getTarget()->getRelCoor()+ h * event.motion.xrel *.05 - v * event.motion.yrel * .05); |
---|
104 | |
---|
105 | } |
---|
106 | break; |
---|
107 | case SDL_MOUSEBUTTONDOWN: |
---|
108 | switch (event.button.button) |
---|
109 | { |
---|
110 | case 4: |
---|
111 | PRINTF(4)("MouseWheel up\n"); |
---|
112 | camera->setRelCoor(camera->getRelCoor() + (camera->getTarget()->getAbsCoor() - camera->getAbsCoor())*.1); |
---|
113 | break; |
---|
114 | case 5: |
---|
115 | PRINTF(4)("MouseWheel down\n"); |
---|
116 | camera->setRelCoor(camera->getRelCoor() - (camera->getTarget()->getAbsCoor() - camera->getAbsCoor())*.1); |
---|
117 | break; |
---|
118 | case 1: |
---|
119 | case 2: |
---|
120 | case 3: |
---|
121 | mouseDown[event.button.button] = true; |
---|
122 | break; |
---|
123 | } |
---|
124 | |
---|
125 | break; |
---|
126 | case SDL_MOUSEBUTTONUP: |
---|
127 | switch (event.button.button) |
---|
128 | { |
---|
129 | case 1: |
---|
130 | case 2: |
---|
131 | case 3: |
---|
132 | mouseDown[event.button.button] = false; |
---|
133 | break; |
---|
134 | } |
---|
135 | break; |
---|
136 | case SDL_VIDEORESIZE: |
---|
137 | GraphicsEngine::getInstance()->resolutionChanged(&event.resize); |
---|
138 | break; |
---|
139 | case SDL_KEYDOWN: |
---|
140 | switch (event.key.keysym.sym) |
---|
141 | { |
---|
142 | case SDLK_q: |
---|
143 | case SDLK_ESCAPE: |
---|
144 | #ifdef GUI_MODULE |
---|
145 | quitGui(NULL, NULL); |
---|
146 | #else |
---|
147 | this->quit(); |
---|
148 | #endif |
---|
149 | break; |
---|
150 | case SDLK_a: |
---|
151 | camera->setRelCoor(camera->getRelCoor() + (camera->getTarget()->getAbsCoor() - camera->getAbsCoor())*.1); |
---|
152 | break; |
---|
153 | case SDLK_z: |
---|
154 | camera->setRelCoor(camera->getRelCoor() - (camera->getTarget()->getAbsCoor() - camera->getAbsCoor())*.1); |
---|
155 | break; |
---|
156 | case SDLK_h: |
---|
157 | this->printHelp(); |
---|
158 | break; |
---|
159 | } |
---|
160 | break; |
---|
161 | |
---|
162 | // If a quit event was recieved |
---|
163 | case SDL_QUIT: |
---|
164 | // then we're done and we'll end this program |
---|
165 | #ifdef GUI_MODULE |
---|
166 | quitGui(NULL, NULL); |
---|
167 | #else |
---|
168 | this->quit(); |
---|
169 | #endif |
---|
170 | break; |
---|
171 | default: |
---|
172 | break; |
---|
173 | } |
---|
174 | |
---|
175 | } |
---|
176 | |
---|
177 | // Get the state of the keyboard keys |
---|
178 | keys = SDL_GetKeyState(NULL); |
---|
179 | |
---|
180 | // and check if ESCAPE has been pressed. If so then quit |
---|
181 | if(keys[SDLK_ESCAPE]) return false; |
---|
182 | } |
---|
183 | return true; |
---|
184 | } |
---|
185 | |
---|
186 | void Framework::quit(void) |
---|
187 | { |
---|
188 | this->isFinished = true; |
---|
189 | } |
---|
190 | |
---|
191 | Framework* Framework::singletonRef = NULL; |
---|
192 | |
---|
193 | Framework* Framework::getInstance(void) |
---|
194 | { |
---|
195 | if (Framework::singletonRef == NULL) |
---|
196 | Framework::singletonRef = new Framework(); |
---|
197 | return Framework::singletonRef; |
---|
198 | } |
---|
199 | |
---|
200 | Framework::Framework() |
---|
201 | { |
---|
202 | this->isFinished = false; |
---|
203 | |
---|
204 | this->lastFrame = 0; |
---|
205 | // Create a new OpenGL window with the title "Cone3D Basecode" at |
---|
206 | // 640x480x32, fullscreen and check for errors along the way |
---|
207 | GraphicsEngine::getInstance(); |
---|
208 | |
---|
209 | LightManager::getInstance(); |
---|
210 | glEnable(GL_TEXTURE_2D); |
---|
211 | |
---|
212 | // Build the font from a TGA image font.tga in the data directory |
---|
213 | // Hide the mouse cursor |
---|
214 | SDL_ShowCursor(2); |
---|
215 | |
---|
216 | for (int i = 0; i <MOUSE_BUTTON_COUNT; i++) |
---|
217 | mouseDown[i] = false; |
---|
218 | |
---|
219 | ResourceManager::getInstance()->setDataDir(DATA_DIRECTORY); |
---|
220 | |
---|
221 | camera = new Camera(); |
---|
222 | |
---|
223 | State::getInstance()->setCamera(camera, camera->getTarget()); |
---|
224 | |
---|
225 | camera->setAbsCoor(Vector(10, 10, 0)); |
---|
226 | } |
---|
227 | |
---|
228 | Framework::~Framework() |
---|
229 | { |
---|
230 | delete GraphicsEngine::getInstance(); |
---|
231 | |
---|
232 | } |
---|
233 | |
---|
234 | |
---|
235 | |
---|
236 | void Framework::printHelp(void) const |
---|
237 | { |
---|
238 | PRINT(0)(" Help for the frameWork\n"); |
---|
239 | PRINT(0)("========================\n"); |
---|
240 | PRINT(0)("h - print thisHelp\n"); |
---|
241 | |
---|
242 | this->moduleHelp(); |
---|
243 | |
---|
244 | } |
---|
245 | |
---|
246 | #ifdef GUI_MODULE |
---|
247 | int quitGui(GtkWidget* widget, void* data) |
---|
248 | { |
---|
249 | #ifdef HAVE_GTK2 |
---|
250 | while(gtk_events_pending()) gtk_main_iteration(); |
---|
251 | Framework::getInstance()->quit(); |
---|
252 | #endif /* HAVE_GTK2 */ |
---|
253 | } |
---|
254 | #endif |
---|
255 | |
---|
256 | int main(int argc, char *argv[]) |
---|
257 | { |
---|
258 | verbose = 3; |
---|
259 | |
---|
260 | Framework* framework = Framework::getInstance(); |
---|
261 | |
---|
262 | framework->moduleInit(argc, argv); |
---|
263 | #ifdef GUI_MODULE |
---|
264 | framework->moduleInitGui(argc, argv); |
---|
265 | #endif |
---|
266 | framework->mainLoop(NULL); |
---|
267 | |
---|
268 | delete framework; |
---|
269 | // Kill the GL & SDL screens |
---|
270 | // And quit |
---|
271 | return 0; |
---|
272 | } |
---|