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