Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/shadows/src/world.cc @ 3726

Last change on this file since 3726 was 3706, checked in by dave, 20 years ago

branches/shadows: Ok, das Abbild vom Raumschiff funktioniert schon mal, sieht recht cool aus:)

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