Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/orxonox.cc @ 3652

Last change on this file since 3652 was 3652, checked in by patrick, 21 years ago

orxonox/trunk: added list iterator template. this is a fix in the list framework, that should I should have made long time ago. modified the list to make it more performant — benchmarking rulez

File size: 13.1 KB
RevLine 
[1850]1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   This program is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with this program; if not, write to the Free Software Foundation,
18   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
19
[1855]20
21   ### File Specific:
22   main-programmer: Patrick Boenzli
[2190]23   co-programmer: Christian Meyer
[1850]24*/
25
[2190]26#include "orxonox.h"
[3610]27
[2036]28#include "world.h"
29#include "data_tank.h"
[2190]30#include "command_node.h"
[2636]31#include "game_loader.h"
[3610]32#include "graphics_engine.h"
33
[2190]34#include <string.h>
[3592]35int verbose = 4;
[2036]36
[1803]37using namespace std;
38
[2190]39/**
[2636]40   \brief create a new Orxonox
[2190]41*/
42Orxonox::Orxonox ()
[1872]43{
44  pause = false;
45}
[1803]46
[2190]47/**
[2636]48   \brief remove Orxonox from memory
[2190]49*/
[1875]50Orxonox::~Orxonox () 
[2190]51{
[3226]52  Orxonox::singletonRef = NULL;
[2636]53  if( world != NULL) delete world;
54  if( localinput != NULL) delete world;
55  if( resources != NULL) delete resources;
[3611]56  delete GraphicsEngine::getInstance(); // deleting the Graphics
[2190]57}
[1850]58
[3449]59/** \brief this is a singleton class to prevent duplicates */
[3226]60Orxonox* Orxonox::singletonRef = 0;
[1872]61
[3449]62/**
63   \returns reference or new Object of Orxonox if not existent.
64*/
[1850]65Orxonox* Orxonox::getInstance (void)
[1803]66{
[3226]67  if (singletonRef == NULL)
68    singletonRef = new Orxonox();
69  return singletonRef;
[1850]70}
71
[2190]72/**
[2636]73   \brief this finds the config file
74   
75   Since the config file varies from user to user and since one may want to specify different config files
76   for certain occasions or platforms this function finds the right config file for every occasion and stores
77   it's path and name into configfilename
[2190]78*/
[3226]79void Orxonox::getConfigFile (int argc, char** argv)
[1850]80{
[2636]81  strcpy (configfilename, "orxonox.conf");
[1803]82}
83
[2190]84/**
[2636]85   \brief initialize Orxonox with command line
[2190]86*/
87int Orxonox::init (int argc, char** argv)
[1803]88{
[2636]89  // parse command line
90  // config file
91 
[3226]92  getConfigFile (argc, argv);
[3174]93  SDL_Init (SDL_INIT_TIMER);
[2636]94  // initialize everything
[3226]95  if( initVideo() == -1) return -1;
96  if( initSound() == -1) return -1;
[2190]97  printf("> Initializing input\n");
[3226]98  if( initInput() == -1) return -1;
[2190]99  printf("> Initializing networking\n");
[3226]100  if( initNetworking () == -1) return -1;
[2190]101  printf("> Initializing resources\n");
[3226]102  if( initResources () == -1) return -1;
[2636]103  //printf("> Initializing world\n");
104  //if( init_world () == -1) return -1; PB: world will be initialized when started
105 
106  return 0;
[1850]107}
[1849]108
[2190]109/**
[2636]110   \brief initializes SDL and OpenGL
[2190]111*/
[3226]112int Orxonox::initVideo() 
[2190]113{
[3611]114  PRINTF(3)("> Initializing video\n");
[2190]115 
[3610]116  GraphicsEngine::getInstance();
[2190]117 
118  return 0;
119}
[1850]120
[3214]121
[2190]122/**
[2636]123   \brief initializes the sound engine
[2190]124*/
[3226]125int Orxonox::initSound() 
[2190]126{
[3174]127  printf("> Initializing sound\n");
[3226]128  // SDL_Init(SDL_INIT_AUDIO);
[2636]129  printf("Not yet implemented\n");
130  return 0;
[2190]131}
[1900]132
[3214]133
[2190]134/**
[2636]135   \brief initializes input functions
[2190]136*/
[3226]137int Orxonox::initInput() 
[2190]138{
[2636]139  // create localinput
140  localinput = new CommandNode( configfilename);
141 
142  return 0;
[1803]143}
144
[3214]145
[2190]146/**
[2636]147   \brief initializes network system
[2190]148*/
[3226]149int Orxonox::initNetworking() 
[1897]150{
[2636]151  printf("Not yet implemented\n");
152  return 0;
[1897]153}
154
[3214]155
[2190]156/**
[2636]157   \brief initializes and loads resource files
[2190]158*/
[3226]159int Orxonox::initResources() 
[1858]160{
[2636]161  printf("Not yet implemented\n");
162  return 0;
[1858]163}
[1849]164
[3214]165
[2190]166/**
[2636]167   \brief initializes the world
[2190]168*/
[3226]169int Orxonox::initWorld() 
[1896]170{
[2636]171  //world = new World();
172 
173  // TO DO: replace this with a menu/intro
174  //world->load_debug_level();
175 
176  return 0;
[1896]177}
178
[2636]179
[2190]180/**
[2636]181   \brief starts the orxonox game or menu
182
183   here is the central orxonox state manager. There are currently two states
184   - menu
185   - game-play
186   both states manage their states themselfs again.
[2190]187*/
[2636]188void Orxonox::start()
189{
190 
191  this->gameLoader = GameLoader::getInstance();
192  this->gameLoader->loadDebugCampaign(DEBUG_CAMPAIGN_0);
193  this->gameLoader->init();
194  this->gameLoader->start();
195}
196
[3214]197
[2636]198/**
199   \brief exits Orxonox
200*/
[1875]201void Orxonox::quitGame() 
202{
[2636]203  bQuitOrxonox = true;
[1875]204}
205
206
[3214]207
[2190]208/**
[2636]209   \brief handles sprecial events from localinput
210   \param event: an event not handled by the CommandNode
[2190]211*/
[3226]212void Orxonox::eventHandler(SDL_Event* event)
[2190]213{
[2636]214  // Handle special events such as reshape, quit, focus changes
[3619]215  switch (event->type)
216    {
217    case SDL_VIDEORESIZE:
218      GraphicsEngine* tmpGEngine = GraphicsEngine::getInstance();
219      tmpGEngine->resolutionChanged(&event->resize);
220      break;
221    }
[2190]222}
[3214]223 
[1875]224
[2190]225/**
[2636]226   \brief handle keyboard commands that are not meant for WorldEntities
227   \param cmd: the command to handle
228   \return true if the command was handled by the system or false if it may be passed to the WorldEntities
[2190]229*/
[3226]230bool Orxonox::systemCommand(Command* cmd)
[2190]231{
[3220]232  /*
[2636]233  if( !strcmp( cmd->cmd, "quit"))
234    {
235      if( !cmd->bUp) this->gameLoader->stop();
236      return true;
237    }
238  return false;
[3220]239  */
240  return false;
[2190]241}
[1803]242
[2190]243/**
[2636]244   \brief retrieve a pointer to the local CommandNode
245   \return a pointer to localinput
[2190]246*/
[3226]247CommandNode* Orxonox::getLocalInput()
[1850]248{
[2636]249  return localinput;
[1803]250}
251
[3214]252
[2190]253/**
[2636]254   \brief retrieve a pointer to the local World
255   \return a pointer to world
[2190]256*/
[3226]257World* Orxonox::getWorld()
[1872]258{
[2636]259  return world;
[1872]260}
[1850]261
[3449]262/**
263   \return The reference of the SDL-screen of orxonox
264*/
[3365]265SDL_Surface* Orxonox::getScreen ()
266{
267  return this->screen;
268}
[3214]269
[3648]270
271
[3449]272/**
273   \brief main function
[3214]274
[3449]275   here the journey begins
276*/
[3226]277int main(int argc, char** argv) 
[1803]278{ 
[3648]279 
280  /* reading arguments
281     
282     currently supported arguments are:
283     <no args>                   ::    just starts orxonox
284     --benchmark                 ::    start the benchmark without starting orxonox
285     
286     this is a preselection: it matches to one of the start* functions, the
287     finetuning is made in those functions.
288  */
289
290
291  int i;
292  for(i = 0; i < argc; ++i)
293    {
294      if(! strcmp( "--help", argv[i])) return startHelp();
295      else if(! strcmp( "--benchmark", argv[i])) return startBenchmarks();
296    }
297
298  PRINTF(2)("Orxonox does not understand the arguments");
299  return startOrxonox(argc, argv);
300}
301
302
303
304int startHelp()
305{
306  printf("orxonox: starts the orxonox game - rules\n");
307  printf("usage: orxonox [arg]\n\n");
308  printf("valid options:\n");
309  printf(" --benchmark\tstarts the orxonox benchmark\n");
310  printf(" --help \tshows this menu\n");
311}
312
[3649]313
[3648]314int startOrxonox(int argc, char** argv)
315{
[2636]316  printf(">>> Starting Orxonox <<<\n");
[1850]317  Orxonox *orx = Orxonox::getInstance();
[2190]318 
[3226]319  if((*orx).init(argc, argv) == -1)
[2636]320    {
321      printf("! Orxonox initialization failed\n");
322      return -1;
323    }
324 
325  orx->start();
326 
[2190]327  //delete orx;
328 
[1803]329}
[3648]330
331
[3649]332#include "list.h"
333#include "world_entity.h"
334#include "vector.h"
335#include "player.h"
[3651]336#include "base_object.h"
[3649]337#include "primitive.h"
338#include <asm/msr.h>
339#include <linux/timex.h>
340
341
342#define LIST_MAX 10000
343#define VECTOR_MAX 1000000
344#define ITERATIONS 10000
345
346
[3648]347int startBenchmarks()
348{
349
350  printf("===========================================================\n");
351  printf("=                      BENCHMARKS                         =\n");
352  printf("===========================================================\n");
[3650]353  printf(" the author is not paying any attention to cacheing effects\n");
354  printf(" of the CPU.\n\n");
355  printf("[title]\t\t\t\t\t     [cycles]\t[loops]\n\n");
356  //  printf("------------------------------------------------------------\n\n");
[3648]357
358  // first measure the time overhead:
359  unsigned long ini, end, dt, tmp;
360  rdtscl(ini); rdtscl(end);
361  dt = end - ini;
362
[3649]363  int type = -1; 
[3648]364  /* type   -1 == all
365     type    0 == framework
366     type    1 == vector
367     type    2 == quaternion
368  */
369  if(type == 0 || type == -1)
370    {
371      /* framework test*/
[3649]372
[3650]373      printf("Generating Objects:\t\t\t\t\t%i\n", ITERATIONS);
[3649]374      /* ************WorldEntity class test************** */
[3648]375      WorldEntity* w = NULL;
376      int i = 0;
377      unsigned long mittel = 0;
378     
379      for(i = 0; i < ITERATIONS; ++i)
380        {
381          rdtscl(ini);
382         
383          WorldEntity* w = new WorldEntity();
384         
385          rdtscl(end);
[3649]386          delete w;
[3648]387          mittel += (end - ini - dt);
388        }
389      float mi = mittel / (float)ITERATIONS;
[3650]390      printf(" Generate a WorldEntity object:\t\t%11.2f\n", mi);
[3648]391     
392      mittel = 0;
[3649]393      for(i = 0; i < ITERATIONS; ++i)
394        {
395          rdtscl(ini);
396         
397          WorldEntity* w = new Primitive(P_SPHERE);
398         
399          rdtscl(end);
400          delete w;
401          mittel += (end - ini - dt);
402        }
403      mi = mittel / (float)ITERATIONS;
[3650]404      printf(" Generate a Primitive  object:\t\t%11.2f\n", mi);
[3649]405
[3650]406
[3649]407      mittel = 0;
[3650]408      for(i = 0; i < ITERATIONS; ++i)
409        {
410          rdtscl(ini);
411         
412          Vector* v = new Vector();
413         
414          rdtscl(end);
415          delete v;
416          mittel += (end - ini - dt);
417        }
418      mi = mittel / (float)ITERATIONS;
419      printf(" Generate a Vector object:\t\t%11.2f\n", mi);
420
421
422     mittel = 0;
423      for(i = 0; i < ITERATIONS; ++i)
424        {
425          rdtscl(ini);
426         
427          Quaternion* q = new Quaternion();
428         
429          rdtscl(end);
430          delete q;
431          mittel += (end - ini - dt);
432        }
433      mi = mittel / (float)ITERATIONS;
434      printf(" Generate a Quaternion object:\t\t%11.2f\n", mi);
435
436
437
438
439      printf("\nCalling function inline &| virtual, \t\t\t%i\n", ITERATIONS);
440      mittel = 0;
[3648]441      w = new WorldEntity();
442      for(i = 0; i < ITERATIONS; ++i)
443        {
444          rdtscl(ini);
445         
446          w->tick(0.0f);
[3649]447
448          rdtscl(end);
449          mittel += (end - ini - dt);
450          }
451      //delete w;
452      mi = mittel / (float)ITERATIONS;
[3650]453      printf(" Virt funct tick() of WE: \t\t%11.2f\n", mi);
[3649]454
455
456      mittel = 0;
457      WorldEntity wo;
458      for(i = 0; i < ITERATIONS; ++i)
459        {
460          rdtscl(ini);
461         
462          wo.tick(0.0f);
[3648]463           
464          rdtscl(end);
465          mittel += (end - ini - dt);
466          }
[3649]467      //delete w;
[3648]468      mi = mittel / (float)ITERATIONS;
[3650]469      printf(" Inl virt funct tick() of WE v2: \t%11.2f\n", mi);
[3649]470
[3648]471     
[3651]472      mittel = 0;
473      BaseObject* bo = new BaseObject();
474      for(i = 0; i < ITERATIONS; ++i)
475        {
476          rdtscl(ini);
477         
478          bo->isFinalized();
479           
480          rdtscl(end);
481          mittel += (end - ini - dt);
482          }
483      //delete w;
484      mi = mittel / (float)ITERATIONS;
485      printf(" Inl funct BaseObject::isFinazlized(): \t%11.2f\n", mi);
486
487     
[3648]488      tList<WorldEntity>* list = new tList<WorldEntity>();
[3649]489
[3648]490     
[3649]491      /* ************Primitvie class test************** */
492      list = new tList<WorldEntity>();
493 
[3648]494     
495     
[3649]496      mittel = 0;
497      w = new Primitive(P_SPHERE);
498      for(i = 0; i < ITERATIONS; ++i)
[3648]499        {
[3649]500          rdtscl(ini);
501         
[3648]502          w->tick(0.0f);
[3649]503           
504          rdtscl(end);
505          mittel += (end - ini - dt);
506          }
507      mi = mittel / (float)ITERATIONS;
[3650]508      printf(" Call function tick() of Prim:\t\t%11.2f\n", mi);
[3648]509
[3649]510
[3648]511      }
512  if(type == 1 || type == -1)
513    {
[3650]514      printf("\nDoing some simple vector operations: \t\t\t%i\n", VECTOR_MAX);
[3648]515      /* vector test */
516      Vector* a = new Vector(1.3, 5.3, 4.1);
517      Vector* b = new Vector(0.4, 2.5, 6.2);
518      Vector* c = new Vector();
519     
[3650]520      unsigned long mittel, ini, end;
521      float mi;
[3648]522      int i = 0;
523      // addition
[3650]524      mittel = 0;
[3648]525      for(i = 0; i < VECTOR_MAX; ++i)
526        {
[3650]527          rdtscl(ini);
528         
[3648]529          *c = *a + *b;
[3650]530           
531          rdtscl(end);
532          mittel += (end - ini - dt);
[3648]533        }
[3650]534      mi = mittel / (float)VECTOR_MAX;
535      printf(" Addition of two vectors:\t\t%11.2f\n", mi);
[3648]536     
537      // multiplikation
[3650]538
539      mittel = 0;
[3648]540      for(i = 0; i < VECTOR_MAX; ++i)
541        {
[3650]542          rdtscl(ini);
543         
[3648]544          *c = a->cross( *b);
[3650]545           
546          rdtscl(end);
547          mittel += (end - ini - dt);
[3648]548        }
[3650]549      mi = mittel / (float)VECTOR_MAX;
550      printf(" CrossMult of two vectors:\t\t%11.2f\n", mi);
551
[3648]552      if(type == 1 || type == -1)
553        {
554          /* quaternion test */
[3650]555          printf("\nDoing some simple quaternion operations: \t\t%i\n", VECTOR_MAX);
556          /* vector test */
557          Quaternion* a = new Quaternion();
558          Quaternion* b = new Quaternion();
559          Quaternion* c = new Quaternion();
560         
561          unsigned long mittel, ini, end;
562          float mi;
563          int i = 0;
[3651]564          // quaternion generieren mit spez konstruktor
[3650]565          mittel = 0;
[3651]566          Vector* qa = new Vector(4.6, 9.3, 0.4);
567          Vector* qb = new Vector(3.5, 6.1, 4.3);
[3650]568          for(i = 0; i < VECTOR_MAX; ++i)
569            {
570              rdtscl(ini);
571             
[3651]572              Quaternion* qu = new Quaternion(*qa, *qb);
[3650]573             
574              rdtscl(end);
[3651]575              delete qu;
[3650]576              mittel += (end - ini - dt);
577            }
[3651]578          delete a;
579          delete b;
[3650]580          mi = mittel / (float)VECTOR_MAX;
[3651]581          printf(" Gen. quatern. betw. two vectors:\t%11.2f\n", mi);
[3648]582
[3650]583
584          // multiplication
585          mittel = 0;
586          for(i = 0; i < VECTOR_MAX; ++i)
587            {
588              rdtscl(ini);
589             
590              *c = *a * *b;
591             
592              rdtscl(end);
593              mittel += (end - ini - dt);
594            }
595          mi = mittel / (float)VECTOR_MAX;
[3651]596          printf(" Multiplying two quat.(=rot): a * b\t%11.2f\n", mi);
[3650]597         
[3651]598
599
600          // rotating a vector by a quaternion
601          mittel = 0;
602          for(i = 0; i < VECTOR_MAX; ++i)
603            {
604              rdtscl(ini);
605             
606              *qa = a->apply(*qb);
607             
608              rdtscl(end);
609              mittel += (end - ini - dt);
610            }
611          mi = mittel / (float)VECTOR_MAX;
612          printf(" Rot a vec by a quat: q->apply(v)\t%11.2f\n", mi);
[3650]613         
[3651]614         
615
616          // generate rotation matrix
617          mittel = 0;
618          float matrix[4][4];
619          for(i = 0; i < VECTOR_MAX; ++i)
620            {
621              rdtscl(ini);
622             
623              a->matrix(matrix);
624             
625              rdtscl(end);
626              mittel += (end - ini - dt);
627            }
628          mi = mittel / (float)VECTOR_MAX;
629          printf(" Generate rot matrix: q->matrix(m)\t%11.2f\n", mi);
[3648]630        }
631    }
632}
Note: See TracBrowser for help on using the repository browser.