Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

branches/shadow/src:der Boden hat nicht mehr so komische Farben

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