Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/levelloader/src/story_entities/world.cc @ 3525

Last change on this file since 3525 was 3525, checked in by chris, 19 years ago

orxonox/branches/levelloader: removed excess class usage adn messed with makefile definitions

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