Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

branches/shadows:code erweitert fuer terrain

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