Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/trackManager: c2-continuity-patch (mathematically correct, but does not look to good.)

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