Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: cone3D-fonts added, but they will still need some work till they totally adapted to orxonox.

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