Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/trackManager: now curve-points can be inserted wherever you want.

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