1 | |
---|
2 | /* |
---|
3 | orxonox - the future of 3D-vertical-scrollers |
---|
4 | |
---|
5 | Copyright (C) 2004 orx |
---|
6 | |
---|
7 | This program is free software; you can redistribute it and/or modify |
---|
8 | it under the terms of the GNU General Public License as published by |
---|
9 | the Free Software Foundation; either version 2, or (at your option) |
---|
10 | any later version. |
---|
11 | |
---|
12 | ### File Specific: |
---|
13 | main-programmer: Patrick Boenzli |
---|
14 | co-programmer: Christian Meyer |
---|
15 | */ |
---|
16 | |
---|
17 | #include "world.h" |
---|
18 | #include "world_entity.h" |
---|
19 | #include "track_manager.h" |
---|
20 | #include "player.h" |
---|
21 | #include "command_node.h" |
---|
22 | #include "camera.h" |
---|
23 | #include "environment.h" |
---|
24 | #include "primitive.h" |
---|
25 | #include "p_node.h" |
---|
26 | #include "null_parent.h" |
---|
27 | #include "helper_parent.h" |
---|
28 | #include "glmenu_imagescreen.h" |
---|
29 | #include "skysphere.h" |
---|
30 | #include "light.h" |
---|
31 | #include "fontset.h" |
---|
32 | #include "track_node.h" |
---|
33 | |
---|
34 | using namespace std; |
---|
35 | |
---|
36 | |
---|
37 | /** |
---|
38 | \brief create a new World |
---|
39 | |
---|
40 | This creates a new empty world! |
---|
41 | */ |
---|
42 | World::World (char* name) |
---|
43 | { |
---|
44 | this->init(name, -1); |
---|
45 | printf("World::World - generating Nullparent\n"); |
---|
46 | NullParent* np = NullParent::getInstance(); |
---|
47 | } |
---|
48 | |
---|
49 | /** |
---|
50 | \brief creates a new World... |
---|
51 | \param worldID with this ID |
---|
52 | */ |
---|
53 | World::World (int worldID) |
---|
54 | { |
---|
55 | this->init(NULL, worldID); |
---|
56 | } |
---|
57 | |
---|
58 | /** |
---|
59 | \brief remove the World from memory |
---|
60 | |
---|
61 | delete everything explicitly, that isn't contained in the parenting tree! |
---|
62 | things contained in the tree are deleted automaticaly |
---|
63 | */ |
---|
64 | World::~World () |
---|
65 | { |
---|
66 | printf("World::~World() - deleting current world\n"); |
---|
67 | CommandNode* cn = Orxonox::getInstance()->getLocalInput(); |
---|
68 | cn->unbind(this->localPlayer); |
---|
69 | cn->reset(); |
---|
70 | |
---|
71 | this->localCamera->destroy(); |
---|
72 | this->nullParent->destroy(); |
---|
73 | delete this->nullParent; |
---|
74 | //delete this->skySphere; |
---|
75 | |
---|
76 | //delete this->trackManager; |
---|
77 | |
---|
78 | /* |
---|
79 | WorldEntity* entity = entities->enumerate(); |
---|
80 | while( entity != NULL ) |
---|
81 | { |
---|
82 | entity->destroy(); |
---|
83 | entity = entities->nextElement(); |
---|
84 | } |
---|
85 | this->entities->destroy(); |
---|
86 | */ |
---|
87 | |
---|
88 | /* FIX the parent list has to be cleared - not possible if we got the old list also*/ |
---|
89 | |
---|
90 | |
---|
91 | //delete this->entities; |
---|
92 | //delete this->localCamera; |
---|
93 | /* this->localPlayer hasn't to be deleted explicitly, it is |
---|
94 | contained in entities*/ |
---|
95 | } |
---|
96 | |
---|
97 | /** |
---|
98 | \brief initializes a new World |
---|
99 | */ |
---|
100 | void World::init(char* name, int worldID) |
---|
101 | { |
---|
102 | this->setClassName ("World"); |
---|
103 | |
---|
104 | this->worldName = name; |
---|
105 | this->debugWorldNr = worldID; |
---|
106 | this->entities = new tList<WorldEntity>(); |
---|
107 | |
---|
108 | // Enable default GL stuff |
---|
109 | glEnable(GL_DEPTH_TEST); |
---|
110 | |
---|
111 | } |
---|
112 | |
---|
113 | |
---|
114 | /** |
---|
115 | \brief loads the World by initializing all resources, and set their default values. |
---|
116 | */ |
---|
117 | ErrorMessage World::load() |
---|
118 | { |
---|
119 | // BezierCurve* tmpCurve = new BezierCurve(); |
---|
120 | if(this->debugWorldNr != -1) |
---|
121 | { |
---|
122 | // initializing Font |
---|
123 | testFont = new FontSet(); |
---|
124 | testFont->buildFont("../data/pictures/font.tga"); |
---|
125 | |
---|
126 | // initializing the TrackManager |
---|
127 | trackManager = TrackManager::getInstance(); |
---|
128 | trackManager->addPoint(Vector(0,-5,0)); |
---|
129 | trackManager->addPoint(Vector(10,0,5)); |
---|
130 | trackManager->addPoint(Vector(20,0,-5)); |
---|
131 | trackManager->addPoint(Vector(30,0,5)); |
---|
132 | trackManager->addPoint(Vector(40,0,5)); |
---|
133 | trackManager->setDuration(4); |
---|
134 | int fork11, fork12, fork13; |
---|
135 | trackManager->fork(3, &fork11, &fork12, &fork13); |
---|
136 | trackManager->workOn(fork11); |
---|
137 | trackManager->addPoint(Vector(70, 0, -10)); |
---|
138 | trackManager->addPoint(Vector(100, 0, -15)); |
---|
139 | trackManager->addPoint(Vector(300, 0, -15)); |
---|
140 | trackManager->setDuration(10); |
---|
141 | trackManager->workOn(fork12); |
---|
142 | trackManager->addPoint(Vector(70, 0, 0)); |
---|
143 | trackManager->addPoint(Vector(100, 0, 0)); |
---|
144 | trackManager->addPoint(Vector(120,10, 0)); |
---|
145 | trackManager->addPoint(Vector(150,10, 0)); |
---|
146 | trackManager->addPoint(Vector(180,15, 0)); |
---|
147 | trackManager->addPoint(Vector(200,10, 0)); |
---|
148 | trackManager->setDuration(7); |
---|
149 | trackManager->workOn(fork13); |
---|
150 | trackManager->addPoint(Vector(70, 0, 10)); |
---|
151 | trackManager->addPoint(Vector(100, 0, 30)); |
---|
152 | trackManager->addPoint(Vector(120,-10, 30)); |
---|
153 | trackManager->addPoint(Vector(150,-10, 30)); |
---|
154 | trackManager->setDuration(10); |
---|
155 | trackManager->join(2, fork12, fork13); |
---|
156 | |
---|
157 | trackManager->workOn(5); |
---|
158 | trackManager->addPoint(Vector(250, 20, 10)); |
---|
159 | trackManager->addPoint(Vector(290, 20, 10)); |
---|
160 | trackManager->setDuration(5); |
---|
161 | trackManager->setSavePoint(); |
---|
162 | trackManager->addPoint(Vector(350, 20, 10)); |
---|
163 | trackManager->addPoint(Vector(360, 20, 10)); |
---|
164 | trackManager->addPoint(Vector(370, 20, 10)); |
---|
165 | trackManager->setDuration(5); |
---|
166 | |
---|
167 | trackManager->join(2, 6, fork11); |
---|
168 | |
---|
169 | trackManager->finalize(); |
---|
170 | |
---|
171 | |
---|
172 | /*monitor progress*/ |
---|
173 | this->glmis->step(); |
---|
174 | |
---|
175 | /* |
---|
176 | tmpCurve->addNode(Vector(10, -1, -1)); |
---|
177 | tmpCurve->addNode(Vector(10, -2, 2)); |
---|
178 | tmpCurve->addNode(Vector(10, 3, 3)); |
---|
179 | tmpCurve->addNode(Vector(10, 4, -4), 0); |
---|
180 | tmpCurve->addNode(Vector(10, -1, -1)); |
---|
181 | tmpCurve->addNode(Vector(10, -2, 2)); |
---|
182 | tmpCurve->addNode(Vector(10, 3, 3)); |
---|
183 | tmpCurve->addNode(Vector(10, 4, -4), 0); |
---|
184 | tmpCurve->debug(); |
---|
185 | */ |
---|
186 | switch(this->debugWorldNr) |
---|
187 | { |
---|
188 | /* |
---|
189 | this loads the hard-coded debug world. this only for simplicity and will be |
---|
190 | removed by a reald world-loader, which interprets a world-file. |
---|
191 | if you want to add an own debug world, just add a case DEBUG_WORLD_[nr] and |
---|
192 | make whatever you want... |
---|
193 | */ |
---|
194 | case DEBUG_WORLD_0: |
---|
195 | { |
---|
196 | this->nullParent = NullParent::getInstance (); |
---|
197 | this->nullParent->setName ("NullParent"); |
---|
198 | |
---|
199 | // !\todo old track-system has to be removed |
---|
200 | |
---|
201 | //create helper for player |
---|
202 | HelperParent* hp = new HelperParent (); |
---|
203 | /* the player has to be added to this helper */ |
---|
204 | |
---|
205 | // create a player |
---|
206 | WorldEntity* myPlayer = new Player (); |
---|
207 | myPlayer->setName ("player"); |
---|
208 | this->spawn (myPlayer); |
---|
209 | this->localPlayer = myPlayer; |
---|
210 | /*monitor progress*/ |
---|
211 | this->glmis->step(); |
---|
212 | |
---|
213 | // bind input |
---|
214 | Orxonox *orx = Orxonox::getInstance (); |
---|
215 | orx->getLocalInput()->bind (myPlayer); |
---|
216 | |
---|
217 | // bind camera |
---|
218 | this->localCamera = new Camera(this); |
---|
219 | this->localCamera->setName ("camera"); |
---|
220 | this->localCamera->bind (myPlayer); |
---|
221 | /*monitor progress*/ |
---|
222 | this->glmis->step(); |
---|
223 | |
---|
224 | // Create SkySphere |
---|
225 | skySphere = new Skysphere("../data/pictures/sky-replace.jpg"); |
---|
226 | this->localCamera->addChild(this->skySphere); |
---|
227 | this->skySphere->setMode(MOVEMENT); |
---|
228 | |
---|
229 | /*monitor progress*/ |
---|
230 | this->glmis->step(); |
---|
231 | |
---|
232 | |
---|
233 | WorldEntity* env = new Environment(); |
---|
234 | env->setName ("env"); |
---|
235 | this->spawn(env); |
---|
236 | |
---|
237 | /* |
---|
238 | Vector* es = new Vector (50, 2, 0); |
---|
239 | Quaternion* qs = new Quaternion (); |
---|
240 | WorldEntity* pr = new Primitive(PSPHERE); |
---|
241 | pr->setName("primitive"); |
---|
242 | this->spawn(pr, this->localPlayer, es, qs, ROTATION); |
---|
243 | */ |
---|
244 | |
---|
245 | |
---|
246 | /*monitor progress*/ |
---|
247 | this->glmis->step(); |
---|
248 | |
---|
249 | // trackManager->setBindSlave(env); |
---|
250 | TrackNode* tn = TrackNode::getInstance(); |
---|
251 | tn->addChild(myPlayer); |
---|
252 | |
---|
253 | //Vector v(1.0, 0.0, 0.0); |
---|
254 | //Quaternion* q = new Quaternion(90.0, v); |
---|
255 | //this->relDirection = this->relDirection * q; |
---|
256 | |
---|
257 | //tn->setAbsDir(q); |
---|
258 | |
---|
259 | //localCamera->setParent(TrackNode::getInstance()); |
---|
260 | this->localPlayer->addChild (this->localCamera); |
---|
261 | //Vector* cameraOffset = new Vector (0, 5, -10); |
---|
262 | Vector* cameraOffset = new Vector (-10, 5, 0); |
---|
263 | this->localCamera->setRelCoor (cameraOffset); |
---|
264 | trackManager->condition(1, NEAREST, myPlayer); |
---|
265 | |
---|
266 | break; |
---|
267 | } |
---|
268 | case DEBUG_WORLD_1: |
---|
269 | { |
---|
270 | this->nullParent = NullParent::getInstance (); |
---|
271 | this->nullParent->setName ("NullParent"); |
---|
272 | |
---|
273 | // create a player |
---|
274 | WorldEntity* myPlayer = new Player(); |
---|
275 | myPlayer->setName ("player"); |
---|
276 | this->spawn(myPlayer); |
---|
277 | this->localPlayer = myPlayer; |
---|
278 | |
---|
279 | // bind input |
---|
280 | Orxonox *orx = Orxonox::getInstance(); |
---|
281 | orx->getLocalInput()->bind (myPlayer); |
---|
282 | |
---|
283 | // bind camera |
---|
284 | this->localCamera = new Camera (this); |
---|
285 | this->localCamera->setName ("camera"); |
---|
286 | this->localCamera->bind (myPlayer); |
---|
287 | this->localPlayer->addChild (this->localCamera); |
---|
288 | |
---|
289 | // Create SkySphere |
---|
290 | skySphere = new Skysphere("../data/pictures/sky-replace.jpg"); |
---|
291 | this->localPlayer->addChild(this->skySphere); |
---|
292 | |
---|
293 | break; |
---|
294 | |
---|
295 | |
---|
296 | } |
---|
297 | default: |
---|
298 | printf("World::load() - no world with ID %i found", this->debugWorldNr ); |
---|
299 | } |
---|
300 | } |
---|
301 | else if(this->worldName != NULL) |
---|
302 | { |
---|
303 | |
---|
304 | } |
---|
305 | |
---|
306 | // initialize debug coord system |
---|
307 | objectList = glGenLists(1); |
---|
308 | glNewList (objectList, GL_COMPILE); |
---|
309 | |
---|
310 | glColor3f(1.0,0,0); |
---|
311 | |
---|
312 | int sizeX = 100; |
---|
313 | int sizeZ = 80; |
---|
314 | float length = 1000; |
---|
315 | float width = 200; |
---|
316 | float widthX = float (length /sizeX); |
---|
317 | float widthZ = float (width /sizeZ); |
---|
318 | |
---|
319 | float height [sizeX][sizeZ]; |
---|
320 | Vector normal_vectors[sizeX][sizeZ]; |
---|
321 | |
---|
322 | |
---|
323 | for ( int i = 0; i<sizeX-1; i+=1) |
---|
324 | for (int j = 0; j<sizeZ-1;j+=1) |
---|
325 | //height[i][j] = rand()/20046 + (j-25)*(j-25)/30; |
---|
326 | #ifdef __WIN32__ |
---|
327 | height[i][j]=(sin((float)j/3)*rand()*i/182400)*.5; |
---|
328 | #else |
---|
329 | height[i][j]=(sin((float)j/3)*rand()*(long)i/6282450500.0)*.5; |
---|
330 | #endif |
---|
331 | |
---|
332 | //Die Huegel ein wenig glaetten |
---|
333 | for (int h=1; h<2;h++) |
---|
334 | for (int i=1;i<sizeX-2 ;i+=1 ) |
---|
335 | for(int j=1;j<sizeZ-2;j+=1) |
---|
336 | height[i][j]=(height[i+1][j]+height[i][j+1]+height[i-1][j]+height[i][j-1])/4; |
---|
337 | |
---|
338 | //Berechnung von normalen Vektoren |
---|
339 | for(int i=1;i<sizeX-2;i+=1) |
---|
340 | for(int j=1;j<sizeZ-2 ;j+=1) |
---|
341 | { |
---|
342 | Vector v1 = Vector (widthX*(1), height[i][j], widthZ*(j) ); |
---|
343 | Vector v2 = Vector (widthX*(i-1), height[i-1][j], widthZ*(j)); |
---|
344 | Vector v3 = Vector (widthX*(i), height[i][j+1], widthZ*(j+1)); |
---|
345 | Vector v4 = Vector (widthX*(i+1), height[i+1][j], widthZ*(j)); |
---|
346 | Vector v5 = Vector (widthX*(i), height[i][j-1], widthZ*(j-1)); |
---|
347 | |
---|
348 | Vector c1 = v2 - v1; |
---|
349 | Vector c2 = v3 - v1; |
---|
350 | Vector c3= v4 - v1; |
---|
351 | Vector c4 = v5 - v1; |
---|
352 | Vector zero = Vector (0,0,0); |
---|
353 | normal_vectors[i][j]=c1.cross(v3-v5)+c2.cross(v4-v2)+c3.cross(v5-v3)+c4.cross(v2-v4); |
---|
354 | normal_vectors[i][j].normalize(); |
---|
355 | } |
---|
356 | |
---|
357 | glBegin(GL_QUADS); |
---|
358 | int snowheight=3; |
---|
359 | for ( int i = 0; i<sizeX; i+=1) |
---|
360 | for (int j = 0; j<sizeZ;j+=1) |
---|
361 | { |
---|
362 | Vector v1 = Vector (widthX*(i), height[i][j]-20, widthZ*(j) -width/2); |
---|
363 | Vector v2 = Vector (widthX*(i+1), height[i+1][j]-20, widthZ*(j) -width/2); |
---|
364 | Vector v3 = Vector (widthX*(i+1), height[i+1][j+1]-20, widthZ*(j+1)-width/2); |
---|
365 | Vector v4 = Vector (widthX*(i), height[i][j+1]-20, widthZ*(j+1)-width/2); |
---|
366 | float a[3]; |
---|
367 | if(height[i][j]<snowheight){ |
---|
368 | a[0]=0; |
---|
369 | a[1]=1.0-height[i][j]/10-.3; |
---|
370 | a[2]=0; |
---|
371 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
372 | } |
---|
373 | else{ |
---|
374 | a[0]=1.0; |
---|
375 | a[1]=1.0; |
---|
376 | a[2]=1.0; |
---|
377 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
378 | |
---|
379 | } |
---|
380 | glNormal3f(normal_vectors[i][j].x, normal_vectors[i][j].y, normal_vectors[i][j].z); |
---|
381 | glVertex3f(v1.x, v1.y, v1.z); |
---|
382 | if(height[i+1][j]<snowheight){ |
---|
383 | a[0]=0; |
---|
384 | a[1] =1.0-height[i+1][j]/10-.3; |
---|
385 | a[2]=0; |
---|
386 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
387 | } |
---|
388 | else{ |
---|
389 | a[0]=1.0; |
---|
390 | a[1]=1.0; |
---|
391 | a[2]=1.0; |
---|
392 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
393 | |
---|
394 | } |
---|
395 | glNormal3f(normal_vectors[i+1][j].x, normal_vectors[i+1][j].y, normal_vectors[i+1][j].z); |
---|
396 | glVertex3f(v2.x, v2.y, v2.z); |
---|
397 | if(height[i+1][j+1]<snowheight){ |
---|
398 | a[0]=0; |
---|
399 | a[1] =1.0-height[i+1][j+1]/10-.3; |
---|
400 | a[2]=0; |
---|
401 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
402 | } |
---|
403 | else{ |
---|
404 | a[0]=1.0; |
---|
405 | a[1]=1.0; |
---|
406 | a[2]=1.0; |
---|
407 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
408 | |
---|
409 | |
---|
410 | } |
---|
411 | glNormal3f(normal_vectors[i+1][j+1].x, normal_vectors[i+1][j+1].y, normal_vectors[i+1][j+1].z); |
---|
412 | glVertex3f(v3.x, v3.y, v3.z); |
---|
413 | if(height[i][j+1]<snowheight){ |
---|
414 | a[0]=0; |
---|
415 | a[1] =1.0-height[i+1][j+1]/10-.3; |
---|
416 | a[2]=0; |
---|
417 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
418 | } |
---|
419 | else{ |
---|
420 | a[0]=1.0; |
---|
421 | a[1]=1.0; |
---|
422 | a[2]=1.0; |
---|
423 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
424 | } |
---|
425 | glNormal3f(normal_vectors[i][j+1].x, normal_vectors[i][j+1].y, normal_vectors[i][j+1].z); |
---|
426 | glVertex3f(v4.x, v4.y, v4.z); |
---|
427 | |
---|
428 | } |
---|
429 | glEnd(); |
---|
430 | |
---|
431 | trackManager->drawGraph(.01); |
---|
432 | trackManager->debug(2); |
---|
433 | glEndList(); |
---|
434 | |
---|
435 | |
---|
436 | // LIGHT initialisation |
---|
437 | light = Light::getInstance(); |
---|
438 | light->addLight(0); |
---|
439 | light->setAttenuation(QUADRATIC, 1.0); |
---|
440 | light->setAttenuation(CONSTANT, 2.0); |
---|
441 | light->setAttenuation(QUADRATIC, 1.0); |
---|
442 | light->setPosition(10.0, 10.0, 50.0); |
---|
443 | light->setDiffuseColor(1,1,1); |
---|
444 | // light->addLight(1); |
---|
445 | // light->setPosition(20, 10, -20); |
---|
446 | // light->setDiffuseColor(0,0,0); |
---|
447 | light->debug(); |
---|
448 | |
---|
449 | |
---|
450 | } |
---|
451 | |
---|
452 | /** |
---|
453 | \brief initializes a new World |
---|
454 | */ |
---|
455 | ErrorMessage World::init() |
---|
456 | { |
---|
457 | this->bPause = false; |
---|
458 | CommandNode* cn = Orxonox::getInstance()->getLocalInput(); |
---|
459 | cn->addToWorld(this); |
---|
460 | cn->enable(true); |
---|
461 | |
---|
462 | //glMap1f (GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4, &ctrlpoints[0][0]); |
---|
463 | //glEnable (GL_MAP1_VERTEX_3); |
---|
464 | |
---|
465 | //theNurb = gluNewNurbsRenderer (); |
---|
466 | //gluNurbsProperty (theNurb, GLU_NURBS_MODE, GLU_NURBS_TESSELLATOR); |
---|
467 | //gluNurbsProperty (theNurb, GLU_NURBS_VERTEX, vertexCallback ); |
---|
468 | |
---|
469 | } |
---|
470 | |
---|
471 | |
---|
472 | /** |
---|
473 | \brief starts the World |
---|
474 | */ |
---|
475 | ErrorMessage World::start() |
---|
476 | { |
---|
477 | printf("World::start() - starting current World: nr %i\n", this->debugWorldNr); |
---|
478 | this->bQuitOrxonox = false; |
---|
479 | this->bQuitCurrentGame = false; |
---|
480 | this->mainLoop(); |
---|
481 | } |
---|
482 | |
---|
483 | /** |
---|
484 | \brief stops the world. |
---|
485 | |
---|
486 | This happens, when the player decides to end the Level. |
---|
487 | */ |
---|
488 | ErrorMessage World::stop() |
---|
489 | { |
---|
490 | printf("World::stop() - got stop signal\n"); |
---|
491 | this->bQuitCurrentGame = true; |
---|
492 | } |
---|
493 | |
---|
494 | /** |
---|
495 | \brief pauses the Game |
---|
496 | */ |
---|
497 | ErrorMessage World::pause() |
---|
498 | { |
---|
499 | this->isPaused = true; |
---|
500 | } |
---|
501 | |
---|
502 | /** |
---|
503 | \brief ends the pause Phase |
---|
504 | */ |
---|
505 | ErrorMessage World::resume() |
---|
506 | { |
---|
507 | this->isPaused = false; |
---|
508 | } |
---|
509 | |
---|
510 | /** |
---|
511 | \brief destroys the World |
---|
512 | */ |
---|
513 | ErrorMessage World::destroy() |
---|
514 | { |
---|
515 | delete trackManager; |
---|
516 | } |
---|
517 | |
---|
518 | /** |
---|
519 | \brief shows the loading screen |
---|
520 | */ |
---|
521 | void World::displayLoadScreen () |
---|
522 | { |
---|
523 | printf ("World::displayLoadScreen - start\n"); |
---|
524 | |
---|
525 | //GLMenuImageScreen* |
---|
526 | this->glmis = GLMenuImageScreen::getInstance(); |
---|
527 | this->glmis->init(); |
---|
528 | this->glmis->setMaximum(10); |
---|
529 | this->glmis->draw(); |
---|
530 | |
---|
531 | printf ("World::displayLoadScreen - end\n"); |
---|
532 | } |
---|
533 | |
---|
534 | /** |
---|
535 | \brief removes the loadscreen, and changes over to the game |
---|
536 | |
---|
537 | \todo take out the delay |
---|
538 | */ |
---|
539 | void World::releaseLoadScreen () |
---|
540 | { |
---|
541 | printf ("World::releaseLoadScreen - start\n"); |
---|
542 | this->glmis->setValue(this->glmis->getMaximum()); |
---|
543 | SDL_Delay(500); |
---|
544 | printf ("World::releaseLoadScreen - end\n"); |
---|
545 | } |
---|
546 | |
---|
547 | |
---|
548 | /** |
---|
549 | \brief checks for collisions |
---|
550 | |
---|
551 | This method runs through all WorldEntities known to the world and checks for collisions |
---|
552 | between them. In case of collisions the collide() method of the corresponding entities |
---|
553 | is called. |
---|
554 | */ |
---|
555 | void World::collide () |
---|
556 | { |
---|
557 | /* |
---|
558 | List *a, *b; |
---|
559 | WorldEntity *aobj, *bobj; |
---|
560 | |
---|
561 | a = entities; |
---|
562 | |
---|
563 | while( a != NULL) |
---|
564 | { |
---|
565 | aobj = a->nextElement(); |
---|
566 | if( aobj->bCollide && aobj->collisioncluster != NULL) |
---|
567 | { |
---|
568 | b = a->nextElement(); |
---|
569 | while( b != NULL ) |
---|
570 | { |
---|
571 | bobj = b->nextElement(); |
---|
572 | if( bobj->bCollide && bobj->collisioncluster != NULL ) |
---|
573 | { |
---|
574 | unsigned long ahitflg, bhitflg; |
---|
575 | if( check_collision ( &aobj->place, aobj->collisioncluster, |
---|
576 | &ahitflg, &bobj->place, bobj->collisioncluster, |
---|
577 | &bhitflg) ); |
---|
578 | { |
---|
579 | aobj->collide (bobj, ahitflg, bhitflg); |
---|
580 | bobj->collide (aobj, bhitflg, ahitflg); |
---|
581 | } |
---|
582 | } |
---|
583 | b = b->nextElement(); |
---|
584 | } |
---|
585 | } |
---|
586 | a = a->enumerate(); |
---|
587 | } |
---|
588 | */ |
---|
589 | } |
---|
590 | |
---|
591 | /** |
---|
592 | \brief runs through all entities calling their draw() methods |
---|
593 | */ |
---|
594 | void World::draw () |
---|
595 | { |
---|
596 | /* draw entities */ |
---|
597 | WorldEntity* entity; |
---|
598 | glLoadIdentity(); |
---|
599 | |
---|
600 | entity = this->entities->enumerate(); |
---|
601 | while( entity != NULL ) |
---|
602 | { |
---|
603 | if( entity->bDraw ) entity->draw(); |
---|
604 | entity = this->entities->nextElement(); |
---|
605 | } |
---|
606 | |
---|
607 | glCallList (objectList); |
---|
608 | //! \todo skysphere is a WorldEntity and should be inside of the world-entity-list. |
---|
609 | skySphere->draw(); |
---|
610 | |
---|
611 | testFont->printText(0, 0, 1, "orxonox_" PACKAGE_VERSION); |
---|
612 | |
---|
613 | } |
---|
614 | |
---|
615 | |
---|
616 | /** |
---|
617 | \brief function to put your own debug stuff into it. it can display informations about |
---|
618 | the current class/procedure |
---|
619 | */ |
---|
620 | void World::debug() |
---|
621 | { |
---|
622 | printf ("World::debug() - starting debug\n"); |
---|
623 | PNode* p1 = NullParent::getInstance (); |
---|
624 | PNode* p2 = new PNode (new Vector(2, 2, 2), p1); |
---|
625 | PNode* p3 = new PNode (new Vector(4, 4, 4), p1); |
---|
626 | PNode* p4 = new PNode (new Vector(6, 6, 6), p2); |
---|
627 | |
---|
628 | p1->debug (); |
---|
629 | p2->debug (); |
---|
630 | p3->debug (); |
---|
631 | p4->debug (); |
---|
632 | |
---|
633 | p1->shiftCoor (new Vector(-1, -1, -1)); |
---|
634 | |
---|
635 | printf("World::debug() - shift\n"); |
---|
636 | p1->debug (); |
---|
637 | p2->debug (); |
---|
638 | p3->debug (); |
---|
639 | p4->debug (); |
---|
640 | |
---|
641 | p1->update (1); |
---|
642 | |
---|
643 | printf ("World::debug() - update\n"); |
---|
644 | p1->debug (); |
---|
645 | p2->debug (); |
---|
646 | p3->debug (); |
---|
647 | p4->debug (); |
---|
648 | |
---|
649 | p2->shiftCoor (new Vector(-1, -1, -1)); |
---|
650 | p1->update (2); |
---|
651 | |
---|
652 | p1->debug (); |
---|
653 | p2->debug (); |
---|
654 | p3->debug (); |
---|
655 | p4->debug (); |
---|
656 | |
---|
657 | p2->setAbsCoor (new Vector(1,2,3)); |
---|
658 | |
---|
659 | |
---|
660 | p1->update (2); |
---|
661 | |
---|
662 | p1->debug (); |
---|
663 | p2->debug (); |
---|
664 | p3->debug (); |
---|
665 | p4->debug (); |
---|
666 | |
---|
667 | p1->destroy (); |
---|
668 | |
---|
669 | |
---|
670 | /* |
---|
671 | WorldEntity* entity; |
---|
672 | printf("counting all entities\n"); |
---|
673 | printf("World::debug() - enumerate()\n"); |
---|
674 | entity = entities->enumerate(); |
---|
675 | while( entity != NULL ) |
---|
676 | { |
---|
677 | if( entity->bDraw ) printf("got an entity\n"); |
---|
678 | entity = entities->nextElement(); |
---|
679 | } |
---|
680 | */ |
---|
681 | } |
---|
682 | |
---|
683 | |
---|
684 | /** |
---|
685 | \brief main loop of the world: executing all world relevant function |
---|
686 | |
---|
687 | in this loop we synchronize (if networked), handle input events, give the heart-beat to |
---|
688 | all other member-entities of the world (tick to player, enemies etc.), checking for |
---|
689 | collisions drawing everything to the screen. |
---|
690 | */ |
---|
691 | void World::mainLoop() |
---|
692 | { |
---|
693 | this->lastFrame = SDL_GetTicks (); |
---|
694 | printf("World::mainLoop() - Entering main loop\n"); |
---|
695 | while( !this->bQuitOrxonox && !this->bQuitCurrentGame) /* \todo implement pause */ |
---|
696 | { |
---|
697 | // Network |
---|
698 | this->synchronize (); |
---|
699 | // Process input |
---|
700 | this->handleInput (); |
---|
701 | if( this->bQuitCurrentGame || this->bQuitOrxonox) |
---|
702 | { |
---|
703 | printf("World::mainLoop() - leaving loop earlier...\n"); |
---|
704 | break; |
---|
705 | } |
---|
706 | // Process time |
---|
707 | this->timeSlice (); |
---|
708 | // Process collision |
---|
709 | this->collide (); |
---|
710 | // Draw |
---|
711 | this->display (); |
---|
712 | |
---|
713 | for( int i = 0; i < 5000000; i++) {} |
---|
714 | /* \todo this is to slow down the program for openGl Software emulator computers, reimplement*/ |
---|
715 | } |
---|
716 | printf("World::mainLoop() - Exiting the main loop\n"); |
---|
717 | } |
---|
718 | |
---|
719 | |
---|
720 | /** |
---|
721 | \brief synchronize local data with remote data |
---|
722 | */ |
---|
723 | void World::synchronize () |
---|
724 | { |
---|
725 | // Get remote input |
---|
726 | // Update synchronizables |
---|
727 | } |
---|
728 | |
---|
729 | |
---|
730 | /** |
---|
731 | \brief run all input processing |
---|
732 | |
---|
733 | the command node is the central input event dispatcher. the node uses the even-queue from |
---|
734 | sdl and has its own event-passing-queue. |
---|
735 | */ |
---|
736 | void World::handleInput () |
---|
737 | { |
---|
738 | // localinput |
---|
739 | CommandNode* cn = Orxonox::getInstance()->getLocalInput(); |
---|
740 | cn->process(); |
---|
741 | // remoteinput |
---|
742 | } |
---|
743 | |
---|
744 | |
---|
745 | /** |
---|
746 | \brief advance the timeline |
---|
747 | |
---|
748 | this calculates the time used to process one frame (with all input handling, drawing, etc) |
---|
749 | the time is mesured in ms and passed to all world-entities and other classes that need |
---|
750 | a heart-beat. |
---|
751 | */ |
---|
752 | void World::timeSlice () |
---|
753 | { |
---|
754 | Uint32 currentFrame = SDL_GetTicks(); |
---|
755 | if(!this->bPause) |
---|
756 | { |
---|
757 | Uint32 dt = currentFrame - this->lastFrame; |
---|
758 | |
---|
759 | if(dt > 0) |
---|
760 | { |
---|
761 | float fps = 1000/dt; |
---|
762 | printf("fps = %f\n", fps); |
---|
763 | } |
---|
764 | else |
---|
765 | { |
---|
766 | /* the frame-rate is limited to 100 frames per second, all other things are for |
---|
767 | nothing. |
---|
768 | */ |
---|
769 | printf("fps = 1000 - frame rate is adjusted\n"); |
---|
770 | SDL_Delay(10); |
---|
771 | dt = 10; |
---|
772 | } |
---|
773 | //this->timeSlice (dt); |
---|
774 | |
---|
775 | /* function to let all entities tick (iterate through list) */ |
---|
776 | WorldEntity* entity; |
---|
777 | float seconds = dt / 1000.0; |
---|
778 | this->nullParent->update (seconds); |
---|
779 | entity = entities->enumerate(); |
---|
780 | while( entity != NULL) |
---|
781 | { |
---|
782 | entity->tick (seconds); |
---|
783 | entity = entities->nextElement(); |
---|
784 | } |
---|
785 | //skySphere->updatePosition(localCamera->absCoordinate); |
---|
786 | |
---|
787 | /* update tick the rest */ |
---|
788 | this->localCamera->timeSlice(dt); |
---|
789 | this->trackManager->tick(dt); |
---|
790 | } |
---|
791 | this->lastFrame = currentFrame; |
---|
792 | } |
---|
793 | |
---|
794 | |
---|
795 | /** |
---|
796 | \brief render the current frame |
---|
797 | |
---|
798 | clear all buffers and draw the world |
---|
799 | */ |
---|
800 | void World::display () |
---|
801 | { |
---|
802 | // clear buffer |
---|
803 | glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); |
---|
804 | // set camera |
---|
805 | this->localCamera->apply (); |
---|
806 | // draw world |
---|
807 | this->draw(); |
---|
808 | // draw HUD |
---|
809 | /* \todo draw HUD */ |
---|
810 | // flip buffers |
---|
811 | SDL_GL_SwapBuffers(); |
---|
812 | //SDL_Surface* screen = Orxonox::getInstance()->getScreen (); |
---|
813 | //SDL_Flip (screen); |
---|
814 | } |
---|
815 | |
---|
816 | |
---|
817 | /** |
---|
818 | \brief add and spawn a new entity to this world |
---|
819 | \param entity to be added |
---|
820 | */ |
---|
821 | void World::spawn(WorldEntity* entity) |
---|
822 | { |
---|
823 | this->nullParent->addChild (entity); |
---|
824 | this->entities->add (entity); |
---|
825 | entity->postSpawn (); |
---|
826 | } |
---|
827 | |
---|
828 | |
---|
829 | /** |
---|
830 | \brief add and spawn a new entity to this world |
---|
831 | \param entity to be added |
---|
832 | \param absCoor At what coordinates to add this entity. |
---|
833 | \param absDir In which direction should it look. |
---|
834 | */ |
---|
835 | void World::spawn(WorldEntity* entity, Vector* absCoor, Quaternion* absDir) |
---|
836 | { |
---|
837 | this->nullParent->addChild (entity); |
---|
838 | this->entities->add (entity); |
---|
839 | |
---|
840 | entity->setAbsCoor (absCoor); |
---|
841 | entity->setAbsDir (absDir); |
---|
842 | |
---|
843 | entity->postSpawn (); |
---|
844 | } |
---|
845 | |
---|
846 | |
---|
847 | /** |
---|
848 | \brief add and spawn a new entity to this world |
---|
849 | \param entity to be added |
---|
850 | \param entity to be added to (PNode) |
---|
851 | \param At what relative coordinates to add this entity. |
---|
852 | \param In which relative direction should it look. |
---|
853 | */ |
---|
854 | void World::spawn(WorldEntity* entity, PNode* parentNode, |
---|
855 | Vector* relCoor, Quaternion* relDir, |
---|
856 | parentingMode mode) |
---|
857 | { |
---|
858 | |
---|
859 | if( parentNode != NULL) |
---|
860 | { |
---|
861 | parentNode->addChild (entity); |
---|
862 | |
---|
863 | entity->setRelCoor (relCoor); |
---|
864 | entity->setRelDir (relDir); |
---|
865 | |
---|
866 | this->entities->add (entity); |
---|
867 | |
---|
868 | entity->postSpawn (); |
---|
869 | } |
---|
870 | } |
---|
871 | |
---|
872 | |
---|
873 | |
---|
874 | /** |
---|
875 | \brief commands that the world must catch |
---|
876 | \returns false if not used by the world |
---|
877 | */ |
---|
878 | bool World::command(Command* cmd) |
---|
879 | { |
---|
880 | return false; |
---|
881 | } |
---|
882 | |
---|