Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: added new Class light, that handles lights (needed for Bump-mapping).
Because Light is very important in 3D, I have created this Class

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