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