Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/story_entities/world.cc @ 3521

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

orxonox/trunk: parenting revisited. fixed some bugs some missunderstandings. made it a little more secure to use. still dev

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