Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3651 was 3651, checked in by patrick, 19 years ago

orxonox/trunk: now got the trick with inline functions. they have to be placed at the same place as the header files are. modified BaseObject to be more performant, now isFinalized needs only 0.5 cycles! :) next stop, vector class

File size: 13.1 KB
Line 
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
20
21   ### File Specific:
22   main-programmer: Patrick Boenzli
23   co-programmer: Christian Meyer
24*/
25
26#include "orxonox.h"
27
28#include "world.h"
29#include "data_tank.h"
30#include "command_node.h"
31#include "game_loader.h"
32#include "graphics_engine.h"
33
34#include <string.h>
35int verbose = 4;
36
37using namespace std;
38
39/**
40   \brief create a new Orxonox
41*/
42Orxonox::Orxonox ()
43{
44  pause = false;
45}
46
47/**
48   \brief remove Orxonox from memory
49*/
50Orxonox::~Orxonox () 
51{
52  Orxonox::singletonRef = NULL;
53  if( world != NULL) delete world;
54  if( localinput != NULL) delete world;
55  if( resources != NULL) delete resources;
56  delete GraphicsEngine::getInstance(); // deleting the Graphics
57}
58
59/** \brief this is a singleton class to prevent duplicates */
60Orxonox* Orxonox::singletonRef = 0;
61
62/**
63   \returns reference or new Object of Orxonox if not existent.
64*/
65Orxonox* Orxonox::getInstance (void)
66{
67  if (singletonRef == NULL)
68    singletonRef = new Orxonox();
69  return singletonRef;
70}
71
72/**
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
78*/
79void Orxonox::getConfigFile (int argc, char** argv)
80{
81  strcpy (configfilename, "orxonox.conf");
82}
83
84/**
85   \brief initialize Orxonox with command line
86*/
87int Orxonox::init (int argc, char** argv)
88{
89  // parse command line
90  // config file
91 
92  getConfigFile (argc, argv);
93  SDL_Init (SDL_INIT_TIMER);
94  // initialize everything
95  if( initVideo() == -1) return -1;
96  if( initSound() == -1) return -1;
97  printf("> Initializing input\n");
98  if( initInput() == -1) return -1;
99  printf("> Initializing networking\n");
100  if( initNetworking () == -1) return -1;
101  printf("> Initializing resources\n");
102  if( initResources () == -1) return -1;
103  //printf("> Initializing world\n");
104  //if( init_world () == -1) return -1; PB: world will be initialized when started
105 
106  return 0;
107}
108
109/**
110   \brief initializes SDL and OpenGL
111*/
112int Orxonox::initVideo() 
113{
114  PRINTF(3)("> Initializing video\n");
115 
116  GraphicsEngine::getInstance();
117 
118  return 0;
119}
120
121
122/**
123   \brief initializes the sound engine
124*/
125int Orxonox::initSound() 
126{
127  printf("> Initializing sound\n");
128  // SDL_Init(SDL_INIT_AUDIO);
129  printf("Not yet implemented\n");
130  return 0;
131}
132
133
134/**
135   \brief initializes input functions
136*/
137int Orxonox::initInput() 
138{
139  // create localinput
140  localinput = new CommandNode( configfilename);
141 
142  return 0;
143}
144
145
146/**
147   \brief initializes network system
148*/
149int Orxonox::initNetworking() 
150{
151  printf("Not yet implemented\n");
152  return 0;
153}
154
155
156/**
157   \brief initializes and loads resource files
158*/
159int Orxonox::initResources() 
160{
161  printf("Not yet implemented\n");
162  return 0;
163}
164
165
166/**
167   \brief initializes the world
168*/
169int Orxonox::initWorld() 
170{
171  //world = new World();
172 
173  // TO DO: replace this with a menu/intro
174  //world->load_debug_level();
175 
176  return 0;
177}
178
179
180/**
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.
187*/
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
197
198/**
199   \brief exits Orxonox
200*/
201void Orxonox::quitGame() 
202{
203  bQuitOrxonox = true;
204}
205
206
207
208/**
209   \brief handles sprecial events from localinput
210   \param event: an event not handled by the CommandNode
211*/
212void Orxonox::eventHandler(SDL_Event* event)
213{
214  // Handle special events such as reshape, quit, focus changes
215  switch (event->type)
216    {
217    case SDL_VIDEORESIZE:
218      GraphicsEngine* tmpGEngine = GraphicsEngine::getInstance();
219      tmpGEngine->resolutionChanged(&event->resize);
220      break;
221    }
222}
223 
224
225/**
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
229*/
230bool Orxonox::systemCommand(Command* cmd)
231{
232  /*
233  if( !strcmp( cmd->cmd, "quit"))
234    {
235      if( !cmd->bUp) this->gameLoader->stop();
236      return true;
237    }
238  return false;
239  */
240  return false;
241}
242
243/**
244   \brief retrieve a pointer to the local CommandNode
245   \return a pointer to localinput
246*/
247CommandNode* Orxonox::getLocalInput()
248{
249  return localinput;
250}
251
252
253/**
254   \brief retrieve a pointer to the local World
255   \return a pointer to world
256*/
257World* Orxonox::getWorld()
258{
259  return world;
260}
261
262/**
263   \return The reference of the SDL-screen of orxonox
264*/
265SDL_Surface* Orxonox::getScreen ()
266{
267  return this->screen;
268}
269
270
271
272/**
273   \brief main function
274
275   here the journey begins
276*/
277int main(int argc, char** argv) 
278{ 
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
313
314int startOrxonox(int argc, char** argv)
315{
316  printf(">>> Starting Orxonox <<<\n");
317  Orxonox *orx = Orxonox::getInstance();
318 
319  if((*orx).init(argc, argv) == -1)
320    {
321      printf("! Orxonox initialization failed\n");
322      return -1;
323    }
324 
325  orx->start();
326 
327  //delete orx;
328 
329}
330
331
332#include "list.h"
333#include "world_entity.h"
334#include "vector.h"
335#include "player.h"
336#include "base_object.h"
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
347int startBenchmarks()
348{
349
350  printf("===========================================================\n");
351  printf("=                      BENCHMARKS                         =\n");
352  printf("===========================================================\n");
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");
357
358  // first measure the time overhead:
359  unsigned long ini, end, dt, tmp;
360  rdtscl(ini); rdtscl(end);
361  dt = end - ini;
362
363  int type = -1; 
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*/
372
373      printf("Generating Objects:\t\t\t\t\t%i\n", ITERATIONS);
374      /* ************WorldEntity class test************** */
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);
386          delete w;
387          mittel += (end - ini - dt);
388        }
389      float mi = mittel / (float)ITERATIONS;
390      printf(" Generate a WorldEntity object:\t\t%11.2f\n", mi);
391     
392      mittel = 0;
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;
404      printf(" Generate a Primitive  object:\t\t%11.2f\n", mi);
405
406
407      mittel = 0;
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;
441      w = new WorldEntity();
442      for(i = 0; i < ITERATIONS; ++i)
443        {
444          rdtscl(ini);
445         
446          w->tick(0.0f);
447
448          rdtscl(end);
449          mittel += (end - ini - dt);
450          }
451      //delete w;
452      mi = mittel / (float)ITERATIONS;
453      printf(" Virt funct tick() of WE: \t\t%11.2f\n", mi);
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);
463           
464          rdtscl(end);
465          mittel += (end - ini - dt);
466          }
467      //delete w;
468      mi = mittel / (float)ITERATIONS;
469      printf(" Inl virt funct tick() of WE v2: \t%11.2f\n", mi);
470
471     
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     
488      tList<WorldEntity>* list = new tList<WorldEntity>();
489
490     
491      /* ************Primitvie class test************** */
492      list = new tList<WorldEntity>();
493 
494     
495     
496      mittel = 0;
497      w = new Primitive(P_SPHERE);
498      for(i = 0; i < ITERATIONS; ++i)
499        {
500          rdtscl(ini);
501         
502          w->tick(0.0f);
503           
504          rdtscl(end);
505          mittel += (end - ini - dt);
506          }
507      mi = mittel / (float)ITERATIONS;
508      printf(" Call function tick() of Prim:\t\t%11.2f\n", mi);
509
510
511      }
512  if(type == 1 || type == -1)
513    {
514      printf("\nDoing some simple vector operations: \t\t\t%i\n", VECTOR_MAX);
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     
520      unsigned long mittel, ini, end;
521      float mi;
522      int i = 0;
523      // addition
524      mittel = 0;
525      for(i = 0; i < VECTOR_MAX; ++i)
526        {
527          rdtscl(ini);
528         
529          *c = *a + *b;
530           
531          rdtscl(end);
532          mittel += (end - ini - dt);
533        }
534      mi = mittel / (float)VECTOR_MAX;
535      printf(" Addition of two vectors:\t\t%11.2f\n", mi);
536     
537      // multiplikation
538
539      mittel = 0;
540      for(i = 0; i < VECTOR_MAX; ++i)
541        {
542          rdtscl(ini);
543         
544          *c = a->cross( *b);
545           
546          rdtscl(end);
547          mittel += (end - ini - dt);
548        }
549      mi = mittel / (float)VECTOR_MAX;
550      printf(" CrossMult of two vectors:\t\t%11.2f\n", mi);
551
552      if(type == 1 || type == -1)
553        {
554          /* quaternion test */
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;
564          // quaternion generieren mit spez konstruktor
565          mittel = 0;
566          Vector* qa = new Vector(4.6, 9.3, 0.4);
567          Vector* qb = new Vector(3.5, 6.1, 4.3);
568          for(i = 0; i < VECTOR_MAX; ++i)
569            {
570              rdtscl(ini);
571             
572              Quaternion* qu = new Quaternion(*qa, *qb);
573             
574              rdtscl(end);
575              delete qu;
576              mittel += (end - ini - dt);
577            }
578          delete a;
579          delete b;
580          mi = mittel / (float)VECTOR_MAX;
581          printf(" Gen. quatern. betw. two vectors:\t%11.2f\n", mi);
582
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;
596          printf(" Multiplying two quat.(=rot): a * b\t%11.2f\n", mi);
597         
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);
613         
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);
630         
631         
632
633         
634
635        }
636     
637
638    }
639}
Note: See TracBrowser for help on using the repository browser.