Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

branches/dave/src:so bensch, hab das ganze umbenannt: skysphere.cc und skysphere.h

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