Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: light: comments, errormessages, light up again

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