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