Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3505 was 3505, checked in by bensch, 19 years ago

orxonox/branches/trackManager: added trackNode, but there is a segfault while deleting the pNodes

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