Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/parenting/src/world.cc @ 3346

Last change on this file since 3346 was 3345, checked in by bensch, 20 years ago

orxonox/branches/parenting: :loadscreen: now uses texture-class

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