Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: documented orxonox.cc/h world.cc/h vector.cc/h

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