Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: merged back the trackManager to the trunk.
merged with command:
svn merge -r 3369:HEAD branches/trackManager trunk
resolved conflicts in world.cc additive differences.

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