Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/trackManager: patched:
problem with timeTable solved
self-producing start ofChildren in initChildren.

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