Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: implemented attenuation

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