Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3405 was 3405, checked in by bensch, 19 years ago

orxonox/branches/dave: Fixed issue with texture-class

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