Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/world.cc @ 3431

Last change on this file since 3431 was 3429, checked in by bensch, 20 years ago

orxonox/trunk: skysphere now produces no more segfault while level changes

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