Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/parenting/src/world.cc @ 3360

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

orxonox/branches/parenting: Importer: moved object.h → model.h and renamed it everywhere.

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