Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: changed Skysphere to WorldEntity

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