Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/trackManager: merged trunk back to trackManager
merged with command
svn merge -r 3369:HEAD trunk/ branches/trackManager
resoloved conflicts in favor of the trunk.

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