Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3358 was 3358, checked in by patrick, 19 years ago

orxonox/branches/parenting: imageloader works. but not yet fully implemented

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