Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/trackManager/src/world.cc @ 3463

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

orxonox/branches/trackManager: backloop-check created

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