Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/dave/src/world.cc @ 3409

Last change on this file since 3409 was 3409, checked in by dave, 19 years ago

branches/dave/src: Ok, fehler behoben, jetzt kann man noch eigene Himmel einfuegen, je nach belieben:) enjoy./orxonox

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