Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Light faster getPosition algorythm

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