Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: light: added debug-method.
There is something strange about adding multiple lights. I have to read about this some more.

File size: 23.5 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
48World::World (int worldID)
49{
50  this->debugWorldNr = worldID;
51  this->worldName = NULL;
52  this->entities = new tList<WorldEntity>();
53}
54
55/**
56    \brief remove the World from memory
57   
58    delete everything explicitly, that isn't contained in the parenting tree!
59    things contained in the tree are deleted automaticaly
60*/
61World::~World ()
62{
63  printf("World::~World() - deleting current world\n");
64  CommandNode* cn = Orxonox::getInstance()->getLocalInput();
65  cn->unbind(this->localPlayer);
66  cn->reset();
67  this->localCamera->destroy();
68
69  this->nullParent->destroy();
70 
71  delete this->skySphere;
72
73  //delete this->trackManager;
74
75  /*
76  WorldEntity* entity = entities->enumerate(); 
77  while( entity != NULL )
78    {
79      entity->destroy();
80      entity = entities->nextElement();
81    }
82  this->entities->destroy();
83  */
84
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;
90  /* this->localPlayer hasn't to be deleted explicitly, it is
91     contained in entities*/
92}
93
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}};
97
98
99ErrorMessage World::init()
100{
101  this->bPause = false;
102  CommandNode* cn = Orxonox::getInstance()->getLocalInput();
103  cn->addToWorld(this);
104  cn->enable(true);
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
113}
114
115
116
117ErrorMessage World::start()
118{
119  printf("World::start() - starting current World: nr %i\n", this->debugWorldNr);
120  this->bQuitOrxonox = false;
121  this->bQuitCurrentGame = false;
122  this->mainLoop();
123}
124
125ErrorMessage World::stop()
126{
127  printf("World::stop() - got stop signal\n");
128  this->bQuitCurrentGame = true;
129}
130
131ErrorMessage World::pause()
132{
133  this->isPaused = true;
134}
135
136
137ErrorMessage World::resume()
138{
139  this->isPaused = false;
140}
141
142
143void World::destroy()
144{
145  delete trackManager;
146}
147
148
149void World::displayLoadScreen ()
150{
151  printf ("World::displayLoadScreen - start\n"); 
152 
153  //GLMenuImageScreen*
154  this->glmis = GLMenuImageScreen::getInstance();
155  this->glmis->init();
156  this->glmis->setMaximum(10);
157  this->glmis->draw();
158 
159  printf ("World::displayLoadScreen - end\n"); 
160}
161
162
163void World::releaseLoadScreen ()
164{
165  printf ("World::releaseLoadScreen - start\n"); 
166  this->glmis->setValue(this->glmis->getMaximum());
167  SDL_Delay(500);
168  printf ("World::releaseLoadScreen - end\n"); 
169}
170
171
172void World::load()
173{
174  // LIGHT initialisation
175  light = Light::getInstance();
176  light->addLight(0);
177  light->setAttenuation(QUADRATIC, 1.0);
178  light->setPosition(20.0, 10.0, 20.0);
179  light->setDiffuseColor(1,1,1);
180  //  light->addLight(1);
181  //  light->setPosition(20, 10, -20);
182  //  light->setDiffuseColor(0,0,0);
183  light->debug();
184
185  //  BezierCurve* tmpCurve = new BezierCurve();
186  if(this->debugWorldNr != -1)
187    {
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));
194      trackManager->setDuration(2);
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));
201      trackManager->setDuration(5);
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));
208      trackManager->setDuration(3);
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));
214      trackManager->setDuration(3);
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));
220      trackManager->setDuration(7);
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));
229      trackManager->setDuration(3);
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));
236      trackManager->setDuration(6);
237      trackManager->workOn(fork13);
238      trackManager->addPoint(Vector(200,-10,5));
239      trackManager->addPoint(Vector(250,-10,5));
240      trackManager->setDuration(3);
241      trackManager->workOn(fork14);
242      trackManager->addPoint(Vector(200,15,0));
243      trackManager->addPoint(Vector(210,0,10));
244      trackManager->setDuration(1);
245      trackManager->join(4, fork21, fork22, fork13, fork14);
246      trackManager->workOn(10);
247      trackManager->addPoint(Vector(250,-10,5));
248      trackManager->addPoint(Vector(260,-10,5));
249      trackManager->finalize();
250     
251      /*monitor progress*/
252      this->glmis->step();
253
254      /*
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();
264      */
265      switch(this->debugWorldNr)
266        {
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           */
273        case DEBUG_WORLD_0:
274          {
275            this->nullParent = NullParent::getInstance ();
276            this->nullParent->setName ("NullParent");
277
278            // !\todo old track-system has to be removed
279
280            //create helper for player
281            HelperParent* hp = new HelperParent ();
282            /* the player has to be added to this helper */
283
284            // create a player
285            WorldEntity* myPlayer = new Player ();
286            myPlayer->setName ("player");
287            this->spawn (myPlayer);
288            this->localPlayer = myPlayer;
289            /*monitor progress*/
290            this->glmis->step();           
291
292            // bind input
293            Orxonox *orx = Orxonox::getInstance ();
294            orx->getLocalInput()->bind (myPlayer);
295           
296            // bind camera
297            this->localCamera = new Camera(this);
298            this->localCamera->setName ("camera");
299            this->getCamera()->bind (myPlayer);
300            this->localPlayer->addChild (this->localCamera);
301
302            // Create SkySphere
303            skySphere = new Skysphere("../data/pictures/sky-replace.jpg");
304
305            /*monitor progress*/
306            this->glmis->step();
307
308            Vector* es = new Vector (50, 2, 0);
309            Quaternion* qs = new Quaternion ();
310            WorldEntity* env = new Environment();
311            env->setName ("env");
312            this->spawn(env, es, qs);
313           
314            /*monitor progress*/
315            this->glmis->step();
316
317            trackManager->setBindSlave(env);
318
319            break;
320          }
321        case DEBUG_WORLD_1:
322          {
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
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           
345
346
347
348            // create a player
349            WorldEntity* myPlayer = new Player();
350            myPlayer->setName ("player");
351            this->spawn(myPlayer);
352            this->localPlayer = myPlayer;           
353           
354            // bind input
355            Orxonox *orx = Orxonox::getInstance();
356            orx->getLocalInput()->bind (myPlayer);
357           
358            // bind camera
359            this->localCamera = new Camera (this);
360            this->localCamera->setName ("camera");
361            this->getCamera()->bind (myPlayer); 
362            this->localPlayer->addChild (this->localCamera);
363
364            // Create SkySphere
365            skySphere = new Skysphere("../data/pictures/sky-replace.jpg");
366
367            break;
368
369
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    }
379
380  // initialize debug coord system
381  objectList = glGenLists(1);
382  glNewList (objectList, GL_COMPILE);
383  glLoadIdentity();
384  glColor3f(1.0,0,0);
385  glBegin(GL_QUADS);
386
387  int sizeX = 100;
388  int sizeZ = 80;
389  float length = 1000;
390  float width = 200;
391  float widthX = float (length /sizeX);
392  float widthZ = float (width /sizeZ);
393 
394  float height [sizeX][sizeZ];
395  Vector normal_vectors[sizeX][sizeZ];
396 
397 
398  for ( int i = 0; i<sizeX-1; i+=1)
399    for (int j = 0; j<sizeZ-1;j+=1)
400      //height[i][j] = rand()/20046 + (j-25)*(j-25)/30;
401#ifdef __WIN32__
402      height[i][j]=(sin((float)j/3)*rand()*i/182400)*.5;
403#else
404      height[i][j]=(sin((float)j/3)*rand()*(long)i/6282450500.0)*.5;
405#endif
406
407  //Die Huegel ein wenig glaetten
408  for (int h=1; h<2;h++)
409    for (int i=1;i<sizeX-2 ;i+=1 )
410      for(int j=1;j<sizeZ-2;j+=1)
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
414  for(int i=1;i<sizeX-2;i+=1)
415    for(int j=1;j<sizeZ-2 ;j+=1)
416      {
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));
422       
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);
428        normal_vectors[i][j]=c1.cross(v3-v5)+c2.cross(v4-v2)+c3.cross(v5-v3)+c4.cross(v2-v4);
429        normal_vectors[i][j].normalize();
430      }
431
432  int snowheight=3;
433  for ( int i = 0; i<sizeX; i+=1)
434    for (int j = 0; j<sizeZ;j+=1)
435      {   
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);
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);
446        }
447        else{
448            a[0]=1.0;
449            a[1]=1.0;
450            a[2]=1.0;
451            glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
452           
453        }
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      }
503  glEnd();
504  /* 
505  glBegin(GL_LINES);
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    }
543  */ 
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);
556  /* 
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  */
566  glEndList();
567}
568
569
570/**
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.
576*/
577void World::collide ()
578{
579  /*
580  List *a, *b;
581  WorldEntity *aobj, *bobj;
582   
583  a = entities;
584 
585  while( a != NULL)
586    {
587      aobj = a->nextElement();
588      if( aobj->bCollide && aobj->collisioncluster != NULL)
589        {
590          b = a->nextElement();
591          while( b != NULL )
592            {
593              bobj = b->nextElement();
594              if( bobj->bCollide && bobj->collisioncluster != NULL )
595                {
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                  }
604                }
605              b = b->nextElement();
606            }
607        }
608      a = a->enumerate();
609    }
610  */
611}
612
613/**
614    \brief runs through all entities calling their draw() methods
615*/
616void World::draw ()
617{
618  // draw entities
619  WorldEntity* entity;
620  entity = this->entities->enumerate();
621  while( entity != NULL ) 
622    { 
623      if( entity->bDraw ) entity->draw();
624      entity = this->entities->nextElement();
625    } 
626 
627  //glmis = new GLMenuImageScreen();
628  ///glmis->init();
629
630  // draw debug coord system
631  glCallList (objectList);
632
633  //! \todo skysphere is a WorldEntity and should be inside of the world-entity-list.
634  skySphere->draw();
635
636}
637
638/**
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.
645*/
646void World::update ()
647{
648  /*
649  //List<WorldEntity> *l;
650  WorldEntity* entity;
651  Location* loc;
652  Placement* plc;
653  Uint32 t;
654 
655  //  l = entities->enumerate();
656  entity = this->entities->enumerate();
657  while( entity != NULL )
658    {
659
660     
661      if( !entity->isFree() )
662        {
663          loc = entity->getLocation();
664          plc = entity->getPlacement();
665          t = loc->part;
666         
667          if( t >= traclen )
668            {
669              printf("An entity is out of the game area\n");
670              entity->leftWorld ();
671            }
672          else
673            {
674              while( track[t].mapCoords( loc, plc) )
675                {
676                  track[t].postLeave (entity);
677                  if( loc->part >= tracklen )
678                    {
679                      printf("An entity has left the game area\n");
680                      entity->leftWorld ();
681                      break;
682                    }
683                  track[loc->part].postEnter (entity);
684                }
685            }
686        }
687      else
688        {
689        }
690     
691      entity = entities->nextElement();
692    }
693  */ 
694}
695
696/**
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
699*/
700void World::timeSlice (Uint32 deltaT)
701{
702  //List<WorldEntity> *l;
703  WorldEntity* entity;
704  float seconds = deltaT / 1000.0;
705 
706  this->nullParent->update (seconds);
707  //this->nullParent->processTick (seconds);
708
709  entity = entities->enumerate(); 
710  while( entity != NULL) 
711    { 
712      entity->tick (seconds);
713      entity = entities->nextElement();
714    }
715
716  skySphere->updatePosition(localCamera->absCoordinate);
717  //for( int i = 0; i < tracklen; i++) track[i].tick (seconds);
718}
719
720/**
721   \brief removes level data from memory
722*/
723void World::unload()
724{
725
726}
727
728
729/**
730   \brief function to put your own debug stuff into it. it can display informations about
731   the current class/procedure
732*/
733void World::debug()
734{
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  /*
784  WorldEntity* entity;
785  printf("counting all entities\n");
786  printf("World::debug() - enumerate()\n");
787  entity = entities->enumerate(); 
788  while( entity != NULL )
789    {
790      if( entity->bDraw ) printf("got an entity\n");
791      entity = entities->nextElement();
792    }
793  */
794}
795
796
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*/
804void World::mainLoop()
805{
806  this->lastFrame = SDL_GetTicks ();
807  printf("World::mainLoop() - Entering main loop\n");
808  while( !this->bQuitOrxonox && !this->bQuitCurrentGame) /* \todo implement pause */
809    {
810      // Network
811      this->synchronize ();
812      // Process input
813      this->handleInput ();
814      if( this->bQuitCurrentGame || this->bQuitOrxonox)
815        {
816          printf("World::mainLoop() - leaving loop earlier...\n");
817          break;
818        }
819      // Process time
820      this->timeSlice ();
821      // Process collision
822      this->collision ();
823      // Draw
824      this->display ();
825 
826      for( int i = 0; i < 5000000; i++) {}
827      /* \todo this is to slow down the program for openGl Software emulator computers, reimplement*/
828    }
829  printf("World::mainLoop() - Exiting the main loop\n");
830}
831
832/**
833   \brief synchronize local data with remote data
834*/
835void World::synchronize ()
836{
837  // Get remote input
838  // Update synchronizables
839}
840
841/**
842   \brief run all input processing
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.
846*/
847void World::handleInput ()
848{
849  // localinput
850  CommandNode* cn = Orxonox::getInstance()->getLocalInput();
851  cn->process();
852  // remoteinput
853}
854
855/**
856   \brief advance the timeline
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.
861*/
862void World::timeSlice ()
863{
864  Uint32 currentFrame = SDL_GetTicks();
865  if(!this->bPause)
866    {
867      Uint32 dt = currentFrame - this->lastFrame;
868     
869      if(dt > 0)
870        {
871          float fps = 1000/dt;
872          printf("fps = %f\n", fps);
873        }
874      else
875        {
876          /* the frame-rate is limited to 100 frames per second, all other things are for
877             nothing.
878          */
879          printf("fps = 1000 - frame rate is adjusted\n");
880          SDL_Delay(10);
881          dt = 10;
882        }
883      this->timeSlice (dt);
884      this->update ();
885      this->localCamera->timeSlice(dt);
886      this->trackManager->tick(dt);
887    }
888  this->lastFrame = currentFrame;
889}
890
891
892/**
893   \brief compute collision detection
894*/
895void World::collision ()
896{
897  this->collide ();
898}
899
900
901/**
902   \brief render the current frame
903   
904   clear all buffers and draw the world
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
915  /* \todo draw HUD */
916  // flip buffers
917  SDL_GL_SwapBuffers();
918  //SDL_Surface* screen = Orxonox::getInstance()->getScreen ();
919  //SDL_Flip (screen);
920}
921
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*/
928Camera* World::getCamera()
929{
930  return this->localCamera;
931}
932
933
934/**
935   \brief add and spawn a new entity to this world
936   \param entity to be added
937*/
938void World::spawn(WorldEntity* entity)
939{
940  if( this->nullParent != NULL && entity->parent == NULL)
941    this->nullParent->addChild (entity);
942
943  this->entities->add (entity);
944
945  entity->postSpawn ();
946}
947
948
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*/
954void World::spawn(WorldEntity* entity, Vector* absCoor, Quaternion* absDir)
955{
956  entity->setAbsCoor (absCoor);
957  entity->setAbsDir (absDir);
958 
959  if( this->nullParent != NULL && entity->parent == NULL)
960    this->nullParent->addChild (entity);
961
962  this->entities->add (entity);
963
964  entity->postSpawn ();
965}
966
967
968
969/*
970  \brief commands that the world must catch
971  \returns false if not used by the world
972*/
973bool World::command(Command* cmd)
974{
975  return false;
976}
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.