Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/world.cc @ 3423

Last change on this file since 3423 was 3419, checked in by bensch, 20 years ago

orxonox/trunk: copied dave's SkySphere into the Trunk
done it with svn copy, and implemented the Class into the world.cc
it works perfectly

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