Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: moved protoclass to folder proto
added protosingleton
added resourceManager
modiefied some stuff to work better

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