Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3770 was 3760, checked in by dave, 21 years ago

branches/shadows: chaos, aber kommt der loesung naeher

File size: 23.4 KB
RevLine 
[1853]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.
[1855]11
12   ### File Specific:
13   main-programmer: Patrick Boenzli
[2190]14   co-programmer: Christian Meyer
[1853]15*/
16
[2190]17#include "world.h"
18#include "world_entity.h"
19#include "collision.h"
[3365]20#include "track_manager.h"
[2190]21#include "track.h"
[2036]22#include "player.h"
[2190]23#include "command_node.h"
24#include "camera.h"
[2816]25#include "environment.h"
[3365]26#include "p_node.h"
27#include "null_parent.h"
28#include "helper_parent.h"
29#include "glmenu_imagescreen.h"
[3698]30#include "importer/material.h"
[2036]31
[1856]32using namespace std;
[1853]33
34
[1858]35/**
[2551]36    \brief create a new World
37   
38    This creates a new empty world!
[1858]39*/
[2636]40World::World (char* name)
[1855]41{
[3365]42  this->setClassName ("World");
[2636]43  this->worldName = name;
44  this->debugWorldNr = -1;
[2822]45  this->entities = new tList<WorldEntity>();
[3728]46 
[1855]47}
48
[2636]49World::World (int worldID)
50{
51  this->debugWorldNr = worldID;
52  this->worldName = NULL;
[2822]53  this->entities = new tList<WorldEntity>();
[2636]54}
55
[1858]56/**
[2551]57    \brief remove the World from memory
[3365]58   
59    delete everything explicitly, that isn't contained in the parenting tree!
60    things contained in the tree are deleted automaticaly
[1858]61*/
[2190]62World::~World ()
[1872]63{
[3220]64  printf("World::~World() - deleting current world\n");
[3226]65  CommandNode* cn = Orxonox::getInstance()->getLocalInput();
[3220]66  cn->unbind(this->localPlayer);
67  cn->reset();
68  this->localCamera->destroy();
69
[3365]70  this->nullParent->destroy ();
71
72  //delete this->trackManager;
73
74  /*
[3220]75  WorldEntity* entity = entities->enumerate(); 
76  while( entity != NULL )
77    {
78      entity->destroy();
79      entity = entities->nextElement();
80    }
81  this->entities->destroy();
[3365]82  */
[3220]83
[3365]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;
[3220]89  /* this->localPlayer hasn't to be deleted explicitly, it is
90     contained in entities*/
[1872]91}
[1858]92
[3365]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}};
[2636]96
[3365]97
[3698]98
99
[3222]100ErrorMessage World::init()
[2636]101{
102  this->bPause = false;
[3226]103  CommandNode* cn = Orxonox::getInstance()->getLocalInput();
[3216]104  cn->addToWorld(this);
105  cn->enable(true);
[3365]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
[2636]114}
115
[3365]116
117
[3222]118ErrorMessage World::start()
[2636]119{
[3220]120  printf("World::start() - starting current World: nr %i\n", this->debugWorldNr);
121  this->bQuitOrxonox = false;
122  this->bQuitCurrentGame = false;
[2636]123  this->mainLoop();
124}
125
[3222]126ErrorMessage World::stop()
[2636]127{
[3220]128  printf("World::stop() - got stop signal\n");
[2636]129  this->bQuitCurrentGame = true;
130}
131
[3222]132ErrorMessage World::pause()
[2636]133{
134  this->isPaused = true;
135}
136
[3365]137
[3222]138ErrorMessage World::resume()
[2636]139{
140  this->isPaused = false;
141}
142
[3365]143
[3221]144void World::destroy()
145{
[3365]146}
[3221]147
[3365]148
149void World::displayLoadScreen ()
150{
151  printf ("World::displayLoadScreen - start\n"); 
152 
153  //GLMenuImageScreen*
[3368]154  this->glmis = new GLMenuImageScreen();
155  this->glmis->init();
156  this->glmis->setMaximum(10);
157  this->glmis->draw();
[3365]158 
159  printf ("World::displayLoadScreen - end\n"); 
[3221]160}
161
[3365]162
163void World::releaseLoadScreen ()
164{
165  printf ("World::releaseLoadScreen - start\n"); 
[3368]166  this->glmis->setValue(this->glmis->getMaximum());
[3370]167  SDL_Delay(500);
[3365]168  printf ("World::releaseLoadScreen - end\n"); 
169}
170
171
[2636]172void World::load()
173{
[3365]174  //  BezierCurve* tmpCurve = new BezierCurve();
[2636]175  if(this->debugWorldNr != -1)
176    {
[3365]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
[3368]234      /*monitor progress*/
235      this->glmis->step();
236
[3365]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      */
[2636]244      switch(this->debugWorldNr)
245        {
[3225]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           */
[2636]252        case DEBUG_WORLD_0:
253          {
[3365]254            this->nullParent = NullParent::getInstance ();
255            this->nullParent->setName ("NullParent");
256
[2636]257            // create some path nodes
258            this->pathnodes = new Vector[6];
259            this->pathnodes[0] = Vector(0, 0, 0);
[2792]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);
[2636]265           
266            // create the tracks
[2816]267            this->tracklen = 2;
268            this->track = new Track[2];
[2636]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              }
[3194]273            // !\todo old track-system has to be removed
274
[3365]275            //create helper for player
276            HelperParent* hp = new HelperParent ();
277            /* the player has to be added to this helper */
278
[2636]279            // create a player
[3365]280            WorldEntity* myPlayer = new Player ();
281            myPlayer->setName ("player");
282            this->spawn (myPlayer);
[3368]283            this->localPlayer = myPlayer;
[2640]284
[3368]285            /*monitor progress*/
286            this->glmis->step();           
287
[2636]288            // bind input
[3365]289            Orxonox *orx = Orxonox::getInstance ();
[3226]290            orx->getLocalInput()->bind (myPlayer);
[2636]291           
[3698]292           
[3706]293            //create shadow
[3728]294           
295            this->shadow =new Shadow(myPlayer->model,(Player*)myPlayer);           
[3760]296            //shadow->init();     
297            first=true;             
[3706]298           
299            //create skysphere
[3698]300            this->skysphere =new Skysphere();
[3706]301           
302           
[2636]303            // bind camera
304            this->localCamera = new Camera(this);
[3365]305            this->localCamera->setName ("camera");
306            this->getCamera()->bind (myPlayer);
307            this->localPlayer->addChild (this->localCamera);
[2816]308
[3368]309            /*monitor progress*/
310            this->glmis->step();
311
[3365]312            Vector* es = new Vector (50, 2, 0);
313            Quaternion* qs = new Quaternion ();
[2816]314            WorldEntity* env = new Environment();
[3365]315            env->setName ("env");
316            this->spawn(env, es, qs);
[3368]317           
318            /*monitor progress*/
319            this->glmis->step();
[2816]320
[2636]321            break;
322          }
323        case DEBUG_WORLD_1:
324          {
[3365]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
[2636]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           
[3698]347           
[3365]348
349
[2636]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              }
[3194]357
[2636]358            // create a player
[2644]359            WorldEntity* myPlayer = new Player();
[3365]360            myPlayer->setName ("player");
[2644]361            this->spawn(myPlayer);
[3194]362            this->localPlayer = myPlayer;           
[2636]363           
364            // bind input
365            Orxonox *orx = Orxonox::getInstance();
[3226]366            orx->getLocalInput()->bind (myPlayer);
[2636]367           
368            // bind camera
[3365]369            this->localCamera = new Camera (this);
370            this->localCamera->setName ("camera");
[2636]371            this->getCamera()->bind (myPlayer); 
[3365]372            this->localPlayer->addChild (this->localCamera);
[2636]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    }
[2731]383
384  // initialize debug coord system
[3698]385 
[3760]386        objectList = glGenLists(1);
387        glNewList (objectList, GL_COMPILE);
388        glLoadIdentity();
389        //glColor3f(1.0,0,0);
[3698]390 
[3760]391        // Enabling Textures
392        glEnable(GL_TEXTURE_2D);
[3698]393 
[3760]394        // Selecting the new Ground Material.
395       
[3698]396   
[3760]397 
398 
399   
[3698]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();
[3200]407  int sizeX = 100;
[3365]408  int sizeZ = 80;
[3200]409  float length = 1000;
[3698]410  float width = 300;
[3200]411  float widthX = float (length /sizeX);
[3365]412  float widthZ = float (width /sizeZ);
[3199]413 
[3760]414  float 
415 
416  height [sizeX][sizeZ];
[3365]417  Vector normal_vectors[sizeX][sizeZ];
[3728]418  Vector vertexes[sizeX][sizeZ];
[3199]419 
[3728]420  glBegin(GL_QUADS);
[3200]421 
[3728]422 
[3200]423  for ( int i = 0; i<sizeX-1; i+=1)
[3365]424    for (int j = 0; j<sizeZ-1;j+=1)
[3200]425      //height[i][j] = rand()/20046 + (j-25)*(j-25)/30;
[3199]426#ifdef __WIN32__
[3200]427      height[i][j]=(sin((float)j/3)*rand()*i/182400)*.5;
[3199]428#else
[3200]429      height[i][j]=(sin((float)j/3)*rand()*(long)i/6282450500.0)*.5;
[3199]430#endif
[3200]431
[3365]432  //Die Huegel ein wenig glaetten
[3199]433  for (int h=1; h<2;h++)
[3200]434    for (int i=1;i<sizeX-2 ;i+=1 )
[3365]435      for(int j=1;j<sizeZ-2;j+=1)
[3199]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
[3200]439  for(int i=1;i<sizeX-2;i+=1)
[3365]440    for(int j=1;j<sizeZ-2 ;j+=1)
[2792]441      {
[3365]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));
[3199]447       
[3200]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);
[3365]453        normal_vectors[i][j]=c1.cross(v3-v5)+c2.cross(v4-v2)+c3.cross(v5-v3)+c4.cross(v2-v4);
[3199]454        normal_vectors[i][j].normalize();
[3728]455        shadow->setNormal(normal_vectors[i][j],i,j);
[3200]456      }
[3760]457     
458     
459       
[3200]460  for ( int i = 0; i<sizeX; i+=1)
[3365]461    for (int j = 0; j<sizeZ;j+=1)
[3200]462      {   
[3760]463        Vector v1 = Vector (widthX*(i),      height[i][j]-20,       widthZ*(j)  -width/2);
[3365]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);
[3200]467        float a[3];
[3760]468        a[0]=.8;
469        a[1]=.8;
470        a[2]=.8;
[3698]471        glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
[3728]472        shadow->setVertexNum(v1,i,j,0);
473        shadow->setVertexNum(v2,i,j,1);
474        shadow->setVertexNum(v3,i,j,2);
475        shadow->setVertexNum(v4,i,j,3);
[3698]476       
[3760]477                       
[3728]478       
[3200]479        glNormal3f(normal_vectors[i][j].x, normal_vectors[i][j].y, normal_vectors[i][j].z);
[3698]480        glTexCoord2f(0.0f,0.0f);
481        //glTexCoord2f((float)i/(float)sizeX,(float)j/(float)sizeZ);
[3200]482        glVertex3f(v1.x, v1.y, v1.z);
[3698]483       
[3200]484        glNormal3f(normal_vectors[i+1][j].x, normal_vectors[i+1][j].y, normal_vectors[i+1][j].z);
[3698]485        glTexCoord2f(1.0f,0.0f);
486        //glTexCoord2f((float)(i+1.0)/(float)sizeX,(float)j/(float)sizeZ);
[3200]487        glVertex3f(v2.x, v2.y, v2.z);
[3698]488       
[3200]489        glNormal3f(normal_vectors[i+1][j+1].x, normal_vectors[i+1][j+1].y, normal_vectors[i+1][j+1].z);
[3698]490        glTexCoord2f(1.0f,1.0f);
491        //glTexCoord2f((float)(i+1.0)/(float)sizeX,(float)(j+1.0)/(float)sizeZ);
[3200]492        glVertex3f(v3.x, v3.y, v3.z);
[3698]493       
[3200]494        glNormal3f(normal_vectors[i][j+1].x, normal_vectors[i][j+1].y, normal_vectors[i][j+1].z);
[3698]495        glTexCoord2f(0.0f,1.0f);
496        //glTexCoord2f((float)(i)/(float)sizeX,(float)(j+1.0)/(float)sizeZ);
[3200]497        glVertex3f(v4.x, v4.y, v4.z);
498       
499      }
[3760]500      if(first){
501        shadow->createGroundList();
502        first=false;
503        }
504       
[3199]505  glEnd();
[3698]506 
[3737]507 
[3698]508  // Disable Textures Again, for Performance reasons.
509  //glLoadIdentity();
510  glDisable(GL_TEXTURE_2D);
511
512 
[2731]513  //draw track
[2792]514  glBegin(GL_LINES);
[3365]515  glColor3f(0.0, 1.0, 1.0);
[2731]516  for( int i = 0; i < tracklen; i++)
517    {
518      glVertex3f(pathnodes[i].x,pathnodes[i].y,pathnodes[i].z);
519      glVertex3f(pathnodes[(i+1)%tracklen].x,pathnodes[(i+1)%tracklen].y,pathnodes[(i+1)%tracklen].z);
520    }
521  glEnd();
[3365]522 
523
[3698]524
[3365]525  trackManager->drawGraph(.01);
526  trackManager->debug(2);
527  delete trackManager;
[3698]528   
[3760]529  glEndList();
530 shadow->init(); 
531 
[2636]532}
533
534
535/**
[2551]536    \brief checks for collisions
537   
538    This method runs through all WorldEntities known to the world and checks for collisions
539    between them. In case of collisions the collide() method of the corresponding entities
540    is called.
[1858]541*/
[2190]542void World::collide ()
[1858]543{
[2816]544  /*
545  List *a, *b;
[2551]546  WorldEntity *aobj, *bobj;
[2816]547   
548  a = entities;
[2551]549 
550  while( a != NULL)
551    {
[2816]552      aobj = a->nextElement();
[2551]553      if( aobj->bCollide && aobj->collisioncluster != NULL)
[2190]554        {
[2816]555          b = a->nextElement();
[2551]556          while( b != NULL )
557            {
[2816]558              bobj = b->nextElement();
[2551]559              if( bobj->bCollide && bobj->collisioncluster != NULL )
[2190]560                {
[2551]561                  unsigned long ahitflg, bhitflg;
562                  if( check_collision ( &aobj->place, aobj->collisioncluster,
563                                        &ahitflg, &bobj->place, bobj->collisioncluster,
564                                        &bhitflg) );
565                  {
566                    aobj->collide (bobj, ahitflg, bhitflg);
567                    bobj->collide (aobj, bhitflg, ahitflg);
568                  }
[2190]569                }
[2816]570              b = b->nextElement();
[2551]571            }
[2190]572        }
[2816]573      a = a->enumerate();
[2551]574    }
[2816]575  */
[1858]576}
577
578/**
[2551]579    \brief runs through all entities calling their draw() methods
[1931]580*/
[2190]581void World::draw ()
[2077]582{
[2551]583  // draw entities
[3737]584 
585 
[2551]586  WorldEntity* entity;
[2822]587  entity = this->entities->enumerate();
[2816]588  while( entity != NULL ) 
[2551]589    { 
[2822]590      if( entity->bDraw ) entity->draw();
591      entity = this->entities->nextElement();
[3365]592    } 
[2551]593 
[3365]594  //glmis = new GLMenuImageScreen();
595  ///glmis->init();
596
[2551]597  // draw debug coord system
[3698]598  //glLoadIdentity();
599 
[2731]600  glCallList (objectList);
[3737]601 
[2551]602
[3698]603  //After calling the list, draw the skysphere
[3737]604 
[3698]605  glEnable(GL_TEXTURE_2D);
606  skysphere->draw();
607 
[3706]608  shadow->draw();
609 
610 
[3737]611 
[1931]612}
613
614/**
[2551]615    \brief updates Placements and notifies entities when they left the
616    world
617   
618    This runs trough all WorldEntities and maps Locations to Placements
619    if they are bound, checks whether they left the level boundaries
620    and calls appropriate functions.
[1883]621*/
[2190]622void World::update ()
[1883]623{
[3698]624   
[3365]625  /*
[2816]626  //List<WorldEntity> *l;
[2551]627  WorldEntity* entity;
628  Location* loc;
629  Placement* plc;
630  Uint32 t;
631 
[2816]632  //  l = entities->enumerate();
633  entity = this->entities->enumerate();
634  while( entity != NULL )
[2551]635    {
[2816]636
[2551]637     
638      if( !entity->isFree() )
639        {
[3233]640          loc = entity->getLocation();
641          plc = entity->getPlacement();
[2551]642          t = loc->part;
643         
644          if( t >= tracklen )
645            {
646              printf("An entity is out of the game area\n");
[3233]647              entity->leftWorld ();
[2551]648            }
649          else
650            {
[3233]651              while( track[t].mapCoords( loc, plc) )
[2190]652                {
[3233]653                  track[t].postLeave (entity);
[2551]654                  if( loc->part >= tracklen )
655                    {
656                      printf("An entity has left the game area\n");
[3233]657                      entity->leftWorld ();
[2551]658                      break;
659                    }
[3233]660                  track[loc->part].postEnter (entity);
[2190]661                }
[2551]662            }
[2190]663        }
[2551]664      else
665        {
666        }
667     
[2816]668      entity = entities->nextElement();
[2551]669    }
[3365]670  */ 
[1883]671}
672
[2077]673/**
[2551]674    \brief relays the passed time since the last frame to entities and Track parts
675    \param deltaT: the time passed since the last frame in milliseconds
[2077]676*/
[3225]677void World::timeSlice (Uint32 deltaT)
[2077]678{
[2816]679  //List<WorldEntity> *l;
[2551]680  WorldEntity* entity;
[3175]681  float seconds = deltaT / 1000.0;
[2551]682 
[3365]683  this->nullParent->update (seconds);
684  //this->nullParent->processTick (seconds);
685
[2816]686  entity = entities->enumerate(); 
687  while( entity != NULL) 
[2551]688    { 
689      entity->tick (seconds);
[2816]690      entity = entities->nextElement();
[2551]691    }
[3698]692 
693  skysphere->updatePosition(localPlayer->absCoordinate.x,localPlayer->absCoordinate.y,localPlayer->absCoordinate.z);
[3706]694  shadow->updatePosition(localPlayer->absCoordinate.x,localPlayer->absCoordinate.y,localPlayer->absCoordinate.z);
[3209]695  //for( int i = 0; i < tracklen; i++) track[i].tick (seconds);
[2077]696}
[1883]697
[2190]698/**
[2551]699   \brief removes level data from memory
[1858]700*/
[2190]701void World::unload()
[1858]702{
[2551]703  if( pathnodes) delete []pathnodes;
704  if( track) delete []pathnodes;
[1883]705}
[1879]706
[2636]707
708void World::setTrackLen(Uint32 len)
709{
710  this->tracklen = len;
711}
712
713int World::getTrackLen()
714{
715  return this->tracklen;
716}
717
[3225]718
719
720/**
721   \brief function to put your own debug stuff into it. it can display informations about
722   the current class/procedure
723*/
[2640]724void World::debug()
725{
[3365]726  printf ("World::debug() - starting debug\n");
727  PNode* p1 = NullParent::getInstance ();
728  PNode* p2 = new PNode (new Vector(2, 2, 2), p1);
729  PNode* p3 = new PNode (new Vector(4, 4, 4), p1);
730  PNode* p4 = new PNode (new Vector(6, 6, 6), p2);
731
732  p1->debug ();
733  p2->debug ();
734  p3->debug ();
735  p4->debug ();
736
737  p1->shiftCoor (new Vector(-1, -1, -1));
738
739  printf("World::debug() - shift\n");
740  p1->debug ();
741  p2->debug ();
742  p3->debug ();
743  p4->debug ();
744 
745  p1->update (1);
746
747  printf ("World::debug() - update\n");
748  p1->debug ();
749  p2->debug ();
750  p3->debug ();
751  p4->debug ();
752
753  p2->shiftCoor (new Vector(-1, -1, -1));
754  p1->update (2);
755
756  p1->debug ();
757  p2->debug ();
758  p3->debug ();
759  p4->debug ();
760
761  p2->setAbsCoor (new Vector(1,2,3));
762
763
764 p1->update (2);
765
766  p1->debug ();
767  p2->debug ();
768  p3->debug ();
769  p4->debug ();
770
771  p1->destroy ();
772 
773 
774  /*
[2640]775  WorldEntity* entity;
776  printf("counting all entities\n");
[2816]777  printf("World::debug() - enumerate()\n");
778  entity = entities->enumerate(); 
779  while( entity != NULL )
[2640]780    {
781      if( entity->bDraw ) printf("got an entity\n");
[2816]782      entity = entities->nextElement();
[2640]783    }
[3365]784  */
[2640]785}
[2636]786
[2640]787
[3225]788/*
789  \brief main loop of the world: executing all world relevant function
790
791  in this loop we synchronize (if networked), handle input events, give the heart-beat to
792  all other member-entities of the world (tick to player, enemies etc.), checking for
793  collisions drawing everything to the screen.
794*/
[2636]795void World::mainLoop()
796{
[3365]797  this->lastFrame = SDL_GetTicks ();
[3220]798  printf("World::mainLoop() - Entering main loop\n");
[3215]799  while( !this->bQuitOrxonox && !this->bQuitCurrentGame) /* \todo implement pause */
[2551]800    {
[2636]801      // Network
[3365]802      this->synchronize ();
[2636]803      // Process input
[3365]804      this->handleInput ();
[3215]805      if( this->bQuitCurrentGame || this->bQuitOrxonox)
806        {
807          printf("World::mainLoop() - leaving loop earlier...\n");
808          break;
809        }
[2636]810      // Process time
[3365]811      this->timeSlice ();
[2636]812      // Process collision
[3365]813      this->collision ();
[2636]814      // Draw
[3365]815      this->display ();
[2816]816 
[3365]817      for( int i = 0; i < 5000000; i++) {}
818      /* \todo this is to slow down the program for openGl Software emulator computers, reimplement*/
[2551]819    }
[3215]820  printf("World::mainLoop() - Exiting the main loop\n");
[1899]821}
822
[2190]823/**
[2636]824   \brief synchronize local data with remote data
[1855]825*/
[2636]826void World::synchronize ()
[1855]827{
[2636]828  // Get remote input
829  // Update synchronizables
[1855]830}
[2636]831
832/**
833   \brief run all input processing
[3225]834
835   the command node is the central input event dispatcher. the node uses the even-queue from
836   sdl and has its own event-passing-queue.
[2636]837*/
[3225]838void World::handleInput ()
[2636]839{
840  // localinput
[3225]841  CommandNode* cn = Orxonox::getInstance()->getLocalInput();
[3216]842  cn->process();
[2636]843  // remoteinput
844}
845
846/**
847   \brief advance the timeline
[3225]848
849   this calculates the time used to process one frame (with all input handling, drawing, etc)
850   the time is mesured in ms and passed to all world-entities and other classes that need
851   a heart-beat.
[2636]852*/
[3225]853void World::timeSlice ()
[2636]854{
855  Uint32 currentFrame = SDL_GetTicks();
856  if(!this->bPause)
857    {
858      Uint32 dt = currentFrame - this->lastFrame;
[2816]859     
[2636]860      if(dt > 0)
861        {
862          float fps = 1000/dt;
863          printf("fps = %f\n", fps);
864        }
865      else
866        {
[3225]867          /* the frame-rate is limited to 100 frames per second, all other things are for
868             nothing.
869          */
[3194]870          printf("fps = 1000 - frame rate is adjusted\n");
871          SDL_Delay(10);
872          dt = 10;
[2636]873        }
[3225]874      this->timeSlice (dt);
[2636]875      this->update ();
[3225]876      this->localCamera->timeSlice(dt);
[2636]877    }
878  this->lastFrame = currentFrame;
879}
880
[3216]881
[2636]882/**
883   \brief compute collision detection
884*/
885void World::collision ()
886{
887  this->collide ();
888}
889
890
891/**
[3225]892   \brief render the current frame
893   
894   clear all buffers and draw the world
[2636]895*/
896void World::display ()
897{
898  // clear buffer
899  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
900  // set camera
901  this->localCamera->apply ();
902  // draw world
903  this->draw();
904  // draw HUD
[3365]905  /* \todo draw HUD */
[2636]906  // flip buffers
907  SDL_GL_SwapBuffers();
[3365]908  //SDL_Surface* screen = Orxonox::getInstance()->getScreen ();
909  //SDL_Flip (screen);
[2636]910}
911
[3225]912/**
913   \brief give back active camera
914   
915   this passes back the actualy active camera
916   \todo ability to define more than one camera or camera-places
917*/
[2636]918Camera* World::getCamera()
919{
920  return this->localCamera;
921}
[2644]922
923
[3225]924/**
925   \brief add and spawn a new entity to this world
926   \param entity to be added
927*/
[2644]928void World::spawn(WorldEntity* entity)
929{
[3365]930  if( this->nullParent != NULL && entity->parent == NULL)
931    this->nullParent->addChild (entity);
[2816]932
[3365]933  this->entities->add (entity);
934
[3233]935  entity->postSpawn ();
[2816]936}
937
938
[3225]939/**
940   \brief add and spawn a new entity to this world
941   \param entity to be added
942   \param location where to add
943*/
[3365]944void World::spawn(WorldEntity* entity, Vector* absCoor, Quaternion* absDir)
[2816]945{
[3365]946  entity->setAbsCoor (absCoor);
947  entity->setAbsDir (absDir);
948 
949  if( this->nullParent != NULL && entity->parent == NULL)
950    this->nullParent->addChild (entity);
951
[2816]952  this->entities->add (entity);
[3365]953
[3233]954  entity->postSpawn ();
[2644]955}
[2816]956
957
[3216]958
[3225]959/*
960  \brief commands that the world must catch
961  \returns false if not used by the world
962*/
[3216]963bool World::command(Command* cmd)
964{
965  return false;
966}
[3365]967
968
969
970
971void World::swap (unsigned char &a, unsigned char &b)
972{
973  unsigned char temp;
974  temp = a;
975  a    = b;
976  b    = temp;
977}
Note: See TracBrowser for help on using the repository browser.