Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3431 was 3429, checked in by bensch, 20 years ago

orxonox/trunk: skysphere now produces no more segfault while level changes

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