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 | #include "physics_engine.h" |
---|
19 | #include "particle_engine.h" |
---|
20 | |
---|
21 | #include "p_node.h" |
---|
22 | #include "null_parent.h" |
---|
23 | #include "state.h" |
---|
24 | #include "debug.h" |
---|
25 | #include "graphics_engine.h" |
---|
26 | #include "light.h" |
---|
27 | #include "resource_manager.h" |
---|
28 | #include "camera.h" |
---|
29 | |
---|
30 | int verbose; |
---|
31 | |
---|
32 | void Framework::initModule() |
---|
33 | { |
---|
34 | // Creating a Test Particle System |
---|
35 | ParticleSystem* system = new ParticleSystem(100000, PARTICLE_SPRITE); |
---|
36 | system->setLifeSpan(5); |
---|
37 | system->setConserve(.99); |
---|
38 | system->setRadius(4, 3, 1, 2); |
---|
39 | system->setColor(.5,0,0,.5, 1,1,0,1, 0,0,0,0); |
---|
40 | |
---|
41 | // Creating a Test Particle Emitter |
---|
42 | ParticleEmitter* emitter = new ParticleEmitter(Vector(0 , 1, 0), M_PI_4, 20, .1); |
---|
43 | emitter->setType(EMITTER_DOT ); |
---|
44 | emitter->setSize(0); |
---|
45 | emitter->setRelCoor(Vector(0,0,0)); |
---|
46 | |
---|
47 | // Add the Flow from the Emitter into the System |
---|
48 | ParticleEngine::getInstance()->addConnection(emitter, system); |
---|
49 | } |
---|
50 | |
---|
51 | bool Framework::mainLoop() |
---|
52 | { |
---|
53 | while(true) |
---|
54 | { |
---|
55 | // keyhandler returns false if sdl gets quit by some event |
---|
56 | if (!this->keyHandler()) |
---|
57 | return false; |
---|
58 | |
---|
59 | // tick the scene |
---|
60 | float dt = this->tick(); |
---|
61 | |
---|
62 | // Draw the scene |
---|
63 | this->draw(dt); |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | |
---|
68 | bool Framework::draw(float dt) |
---|
69 | { |
---|
70 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); |
---|
71 | glLoadIdentity(); // Reset the view |
---|
72 | |
---|
73 | ParticleEngine::getInstance()->draw(dt); |
---|
74 | |
---|
75 | camera->apply(); |
---|
76 | |
---|
77 | SDL_GL_SwapBuffers(); // Swap the buffers |
---|
78 | } |
---|
79 | |
---|
80 | |
---|
81 | float Framework::tick() |
---|
82 | { |
---|
83 | currFrame = SDL_GetTicks(); |
---|
84 | float dt = (float)(currFrame - lastFrame) / 1000.0; |
---|
85 | lastFrame = currFrame; |
---|
86 | |
---|
87 | ParticleEngine::getInstance()->tick(dt); |
---|
88 | |
---|
89 | NullParent::getInstance()->update(dt); |
---|
90 | return dt; |
---|
91 | } |
---|
92 | |
---|
93 | |
---|
94 | bool Framework::keyHandler() |
---|
95 | { |
---|
96 | // This is the main loop for the entire program and it will run until done==TRUE |
---|
97 | { |
---|
98 | // And poll for events |
---|
99 | SDL_Event event; |
---|
100 | while(SDL_PollEvent(&event)) |
---|
101 | { |
---|
102 | switch (event.type) { |
---|
103 | case SDL_MOUSEMOTION: |
---|
104 | PRINTF(4)("Mouse motion about %d,%d Pixels to (%d,%d).\n", |
---|
105 | event.motion.xrel, event.motion.yrel, |
---|
106 | event.motion.x, event.motion.y); |
---|
107 | // TRACKBALL |
---|
108 | if (mouse1Down) |
---|
109 | { |
---|
110 | /* |
---|
111 | int mX = event.button.x; |
---|
112 | int mY = event.button.y; |
---|
113 | int wH = GraphicsEngine::getInstance()->getResolutionY(); |
---|
114 | int wW = GraphicsEngine::getInstance()->getResolutionX(); |
---|
115 | Vector tmpV (mX, mY, sqrt ( (float) abs(wH * wH/4 - (wW/2-mX) * (wW/2-mX) - (wH/2-mY) * (wH/2-mY)) )); |
---|
116 | // PRINTF(0)("tmpV: %f, %f, %f\n", tmpV.x, tmpV.y, tmpV.z); |
---|
117 | p2 = tmpV-M; |
---|
118 | p2.y = -p2.y; |
---|
119 | rotAxis = p1.cross(p2); |
---|
120 | // PRINTF(0)("rotAxis: %f, %f, %f\n", rotAxis.x, rotAxis.y, rotAxis.z); |
---|
121 | |
---|
122 | // in case that there is no rotation-axis defined |
---|
123 | if (rotAxis.x != 0 || rotAxis.y != 0 || rotAxis.z != 0) |
---|
124 | { |
---|
125 | rotAxis.normalize(); |
---|
126 | // PRINTF(0)("rotAxis: %f, %f, %f\n", rotAxis.x, rotAxis.y, rotAxis.z, rotAngle); |
---|
127 | |
---|
128 | rotAngle = angleRad (p1, p2); |
---|
129 | rotQ = Quaternion (rotAngle, rotAxis); |
---|
130 | rotQ = rotQ * rotQlast; |
---|
131 | rotQ.matrix (matQ); |
---|
132 | // dir = rotQ.apply(dir); |
---|
133 | // dir.normalize(); |
---|
134 | // PRINTF(0)("rotAxis: %f, %f, %f, %f\n", dir.x, dir.y, dir.z, rotAngle); |
---|
135 | } |
---|
136 | rotQlast = rotQ; |
---|
137 | p1 = p2; |
---|
138 | */ |
---|
139 | |
---|
140 | |
---|
141 | } |
---|
142 | break; |
---|
143 | case SDL_MOUSEBUTTONDOWN: |
---|
144 | if (event.button.button == 4) |
---|
145 | { |
---|
146 | PRINTF(4)("MouseWheel up\n"); |
---|
147 | // zoomTo *= .5; |
---|
148 | } |
---|
149 | else if (event.button.button == 5) |
---|
150 | { |
---|
151 | PRINTF(4)("MouseWheel down\n"); |
---|
152 | // zoomTo *= 2.0; |
---|
153 | } |
---|
154 | else if (event.button.button == 1) |
---|
155 | { |
---|
156 | mouse1Down = true; |
---|
157 | movement[0] = event.button.x; |
---|
158 | movement[1] = event.button.y; |
---|
159 | movement[2] = 0; |
---|
160 | movement[3] = 0; |
---|
161 | printf("test %d %d\n", movement[0], movement[1]); |
---|
162 | /* |
---|
163 | int mX = event.button.x; |
---|
164 | int mY = event.button.y; |
---|
165 | int wH = GraphicsEngine::getInstance()->getResolutionY(); |
---|
166 | int wW = GraphicsEngine::getInstance()->getResolutionX(); |
---|
167 | Vector tmpV (mX, mY, sqrt ( (float) abs(wH * wH/4 - (wW/2-mX) * (wW/2-mX) - (wH/2-mY) * (wH/2-mY)) )); |
---|
168 | p1 = tmpV-M; |
---|
169 | p1.y = -p1.y; |
---|
170 | */ |
---|
171 | } |
---|
172 | else |
---|
173 | { |
---|
174 | PRINTF(0)("MouseButton %d pressed at (%d,%d).\n", |
---|
175 | event.button.button, event.button.x, event.button.y); |
---|
176 | } |
---|
177 | |
---|
178 | break; |
---|
179 | case SDL_MOUSEBUTTONUP: |
---|
180 | if (event.button.button == 4); |
---|
181 | else if (event.button.button == 5); |
---|
182 | else if (event.button.button == 1) |
---|
183 | mouse1Down =false; |
---|
184 | else |
---|
185 | { |
---|
186 | PRINTF(4)("MouseButton %d released at (%d,%d).\n", |
---|
187 | event.button.button, event.button.x, event.button.y); |
---|
188 | } |
---|
189 | break; |
---|
190 | |
---|
191 | case SDL_VIDEORESIZE: |
---|
192 | GraphicsEngine::getInstance()->resolutionChanged(&event.resize); |
---|
193 | break; |
---|
194 | case SDL_KEYDOWN: |
---|
195 | switch (event.key.keysym.sym) |
---|
196 | { |
---|
197 | case SDLK_x: |
---|
198 | |
---|
199 | break; |
---|
200 | case SDLK_c: |
---|
201 | |
---|
202 | break; |
---|
203 | case SDLK_i: |
---|
204 | ParticleEngine::getInstance()->debug(); |
---|
205 | break; |
---|
206 | case SDLK_a: |
---|
207 | //zoomTo /=2; |
---|
208 | break; |
---|
209 | case SDLK_z: |
---|
210 | //zoomTo *=2; |
---|
211 | break; |
---|
212 | } |
---|
213 | break; |
---|
214 | |
---|
215 | // If a quit event was recieved |
---|
216 | case SDL_QUIT: |
---|
217 | // then we're done and we'll end this program |
---|
218 | return false; |
---|
219 | break; |
---|
220 | default: |
---|
221 | break; |
---|
222 | } |
---|
223 | |
---|
224 | } |
---|
225 | |
---|
226 | // Get the state of the keyboard keys |
---|
227 | keys = SDL_GetKeyState(NULL); |
---|
228 | |
---|
229 | // and check if ESCAPE has been pressed. If so then quit |
---|
230 | if(keys[SDLK_ESCAPE]) return false; |
---|
231 | } |
---|
232 | return true; |
---|
233 | } |
---|
234 | |
---|
235 | |
---|
236 | Framework::Framework() |
---|
237 | { |
---|
238 | this->lastFrame = 0; |
---|
239 | // Create a new OpenGL window with the title "Cone3D Basecode" at |
---|
240 | // 640x480x32, fullscreen and check for errors along the way |
---|
241 | GraphicsEngine::getInstance(); |
---|
242 | |
---|
243 | LightManager::getInstance(); |
---|
244 | glEnable(GL_TEXTURE_2D); |
---|
245 | |
---|
246 | // Build the font from a TGA image font.tga in the data directory |
---|
247 | // Hide the mouse cursor |
---|
248 | SDL_ShowCursor(2); |
---|
249 | mouse1Down = false; |
---|
250 | |
---|
251 | ResourceManager::getInstance()->setDataDir(DATA_DIRECTORY); |
---|
252 | |
---|
253 | |
---|
254 | initModule(); |
---|
255 | |
---|
256 | camera = new Camera(); |
---|
257 | |
---|
258 | State::getInstance()->setCamera(camera, camera->getTarget()); |
---|
259 | |
---|
260 | camera->setAbsCoor(Vector(10, 10, 0)); |
---|
261 | |
---|
262 | } |
---|
263 | |
---|
264 | Framework::~Framework() |
---|
265 | { |
---|
266 | delete GraphicsEngine::getInstance(); |
---|
267 | |
---|
268 | } |
---|
269 | |
---|
270 | int main(int argc, char *argv[]) |
---|
271 | { |
---|
272 | verbose = 3; |
---|
273 | Framework* framework = new Framework(); |
---|
274 | framework->mainLoop(); |
---|
275 | |
---|
276 | delete framework; |
---|
277 | // Kill the GL & SDL screens |
---|
278 | // And quit |
---|
279 | return 0; |
---|
280 | } |
---|