Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: resource now also gets loaded if the data-directory is not where it is supposed to be… this will be better in the future (i hope)

File size: 16.7 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   co-programmer: Benjamin Grauer: injected ResourceManager/GraphicsEngine
25*/
26
27#include "orxonox.h"
28
29#include "world.h"
30#include "data_tank.h"
31#include "command_node.h"
32#include "game_loader.h"
33#include "graphics_engine.h"
34#include "resource_manager.h"
35#include "text_engine.h"
36
37#include <string.h>
38int verbose = 4;
39
40using namespace std;
41
42/**
43   \brief create a new Orxonox
44*/
45Orxonox::Orxonox ()
46{
47  pause = false;
48}
49
50/**
51   \brief remove Orxonox from memory
52*/
53Orxonox::~Orxonox () 
54{
55  Orxonox::singletonRef = NULL;
56  if( world != NULL) delete world;
57  if( localinput != NULL) delete world;
58  if( resources != NULL) delete resources;
59  delete GraphicsEngine::getInstance(); // deleting the Graphics
60  delete ResourceManager::getInstance(); // deletes the Resource Manager
61  delete TextEngine::getInstance();
62}
63
64/** \brief this is a singleton class to prevent duplicates */
65Orxonox* Orxonox::singletonRef = 0;
66
67/**
68   \returns reference or new Object of Orxonox if not existent.
69*/
70Orxonox* Orxonox::getInstance (void)
71{
72  if (singletonRef == NULL)
73    singletonRef = new Orxonox();
74  return singletonRef;
75}
76
77/**
78   \brief this finds the config file
79   
80   Since the config file varies from user to user and since one may want to specify different config files
81   for certain occasions or platforms this function finds the right config file for every occasion and stores
82   it's path and name into configfilename
83*/
84void Orxonox::getConfigFile (int argc, char** argv)
85{
86  strcpy (configfilename, "orxonox.conf");
87}
88
89/**
90   \brief initialize Orxonox with command line
91*/
92int Orxonox::init (int argc, char** argv)
93{
94  // parse command line
95  // config file
96 
97  getConfigFile (argc, argv);
98  SDL_Init (SDL_INIT_TIMER);
99  // initialize everything
100  if( initVideo() == -1) return -1;
101  if( initSound() == -1) return -1;
102  printf("> Initializing input\n");
103  if( initInput() == -1) return -1;
104  printf("> Initializing networking\n");
105  if( initNetworking () == -1) return -1;
106  printf("> Initializing resources\n");
107  if( initResources () == -1) return -1;
108  //printf("> Initializing world\n");
109  //if( init_world () == -1) return -1; PB: world will be initialized when started
110 
111  return 0;
112}
113
114/**
115   \brief initializes SDL and OpenGL
116*/
117int Orxonox::initVideo() 
118{
119  PRINTF(3)("> Initializing video\n");
120 
121  GraphicsEngine::getInstance();
122 
123  return 0;
124}
125
126
127/**
128   \brief initializes the sound engine
129*/
130int Orxonox::initSound() 
131{
132  printf("> Initializing sound\n");
133  // SDL_Init(SDL_INIT_AUDIO);
134  printf("Not yet implemented\n");
135  return 0;
136}
137
138
139/**
140   \brief initializes input functions
141*/
142int Orxonox::initInput() 
143{
144  // create localinput
145  localinput = new CommandNode( configfilename);
146 
147  return 0;
148}
149
150
151/**
152   \brief initializes network system
153*/
154int Orxonox::initNetworking() 
155{
156  printf("Not yet implemented\n");
157  return 0;
158}
159
160
161/**
162   \brief initializes and loads resource files
163*/
164int Orxonox::initResources() 
165{
166  //  printf("Not yet implemented\n");
167  PRINT(3)("initializing ResourceManager\n");
168  resourceManager = ResourceManager::getInstance();
169  if (!resourceManager->setDataDir("data/"))
170    if (!resourceManager->setDataDir("../data/"))
171      if (!resourceManager->setDataDir("../../data/"))
172        resourceManager->setDataDir("../../../data/");
173  return 0;
174  PRINT(3)("initializing TextEngine\n");
175  TextEngine::getInstance();
176}
177
178
179/**
180   \brief initializes the world
181*/
182int Orxonox::initWorld() 
183{
184  //world = new World();
185 
186  // TO DO: replace this with a menu/intro
187  //world->load_debug_level();
188 
189  return 0;
190}
191
192
193/**
194   \brief starts the orxonox game or menu
195
196   here is the central orxonox state manager. There are currently two states
197   - menu
198   - game-play
199   both states manage their states themselfs again.
200*/
201void Orxonox::start()
202{
203 
204  this->gameLoader = GameLoader::getInstance();
205  this->gameLoader->loadDebugCampaign(DEBUG_CAMPAIGN_0);
206  this->gameLoader->init();
207  this->gameLoader->start();
208}
209
210
211/**
212   \brief exits Orxonox
213*/
214void Orxonox::quitGame() 
215{
216  bQuitOrxonox = true;
217}
218
219
220
221/**
222   \brief handles sprecial events from localinput
223   \param event: an event not handled by the CommandNode
224*/
225void Orxonox::eventHandler(SDL_Event* event)
226{
227  // Handle special events such as reshape, quit, focus changes
228  switch (event->type)
229    {
230    case SDL_VIDEORESIZE:
231      GraphicsEngine* tmpGEngine = GraphicsEngine::getInstance();
232      tmpGEngine->resolutionChanged(&event->resize);
233      break;
234    }
235}
236 
237
238/**
239   \brief handle keyboard commands that are not meant for WorldEntities
240   \param cmd: the command to handle
241   \return true if the command was handled by the system or false if it may be passed to the WorldEntities
242*/
243bool Orxonox::systemCommand(Command* cmd)
244{
245  /*
246  if( !strcmp( cmd->cmd, "quit"))
247    {
248      if( !cmd->bUp) this->gameLoader->stop();
249      return true;
250    }
251  return false;
252  */
253  return false;
254}
255
256/**
257   \brief retrieve a pointer to the local CommandNode
258   \return a pointer to localinput
259*/
260CommandNode* Orxonox::getLocalInput()
261{
262  return localinput;
263}
264
265
266/**
267   \brief retrieve a pointer to the local World
268   \return a pointer to world
269*/
270World* Orxonox::getWorld()
271{
272  return world;
273}
274
275/**
276   \return The reference of the SDL-screen of orxonox
277*/
278SDL_Surface* Orxonox::getScreen ()
279{
280  return this->screen;
281}
282
283
284
285/**
286   \brief main function
287
288   here the journey begins
289*/
290int main(int argc, char** argv) 
291{ 
292 
293  /* reading arguments
294     
295     currently supported arguments are:
296     <no args>                   ::    just starts orxonox
297     --benchmark                 ::    start the benchmark without starting orxonox
298     
299     this is a preselection: it matches to one of the start* functions, the
300     finetuning is made in those functions.
301  */
302
303
304  int i;
305  for(i = 0; i < argc; ++i)
306    {
307      if(! strcmp( "--help", argv[i])) return startHelp();
308      else if(! strcmp( "--benchmark", argv[i])) return startBenchmarks();
309    }
310
311  PRINTF(2)("Orxonox does not understand the arguments");
312  return startOrxonox(argc, argv);
313}
314
315
316
317int startHelp()
318{
319  printf("orxonox: starts the orxonox game - rules\n");
320  printf("usage: orxonox [arg]\n\n");
321  printf("valid options:\n");
322  printf(" --benchmark\tstarts the orxonox benchmark\n");
323  printf(" --help \tshows this menu\n");
324}
325
326
327int startOrxonox(int argc, char** argv)
328{
329  printf(">>> Starting Orxonox <<<\n");
330  Orxonox *orx = Orxonox::getInstance();
331 
332  if((*orx).init(argc, argv) == -1)
333    {
334      printf("! Orxonox initialization failed\n");
335      return -1;
336    }
337 
338  orx->start();
339 
340  delete orx;
341 
342}
343
344#if defined __linux__
345
346#include "list.h"
347#include "world_entity.h"
348#include "vector.h"
349#include "player.h"
350#include "base_object.h"
351#include <asm/msr.h>
352#include <linux/timex.h>
353
354
355#define LIST_MAX 1000
356#define VECTOR_MAX 1000000
357#define ITERATIONS 10000
358
359
360int startBenchmarks()
361{
362
363  printf("===========================================================\n");
364  printf("=                      BENCHMARKS                         =\n");
365  printf("===========================================================\n");
366  printf(" the author is not paying any attention to cacheing effects\n");
367  printf(" of the CPU.\n\n");
368  printf("[title]\t\t\t\t\t     [cycles]\t[loops]\n\n");
369  //  printf("------------------------------------------------------------\n\n");
370
371  // first measure the time overhead:
372  unsigned long ini, end, dt, tmp;
373  rdtscl(ini); rdtscl(end);
374  dt = end - ini;
375
376  int type = -1; 
377  /* type   -1 == all
378     type    0 == framework
379     type    1 == vector
380     type    2 == quaternion
381     type    3 == lists
382  */
383  if(type == 0 || type == -1)
384    {
385      /* framework test*/
386     
387      printf("Generating Objects:\t\t\t\t\t%i\n", ITERATIONS);
388      /* ************WorldEntity class test************** */
389      WorldEntity* w = NULL;
390      int i = 0;
391      unsigned long mittel = 0;
392     
393      for(i = 0; i < ITERATIONS; ++i)
394        {
395          rdtscl(ini);
396         
397          WorldEntity* w = new WorldEntity();
398         
399          rdtscl(end);
400          delete w;
401          mittel += (end - ini - dt);
402        }
403      float mi = mittel / (float)ITERATIONS;
404      printf(" Generate a WorldEntity 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        WorldEntity* w = new Primitive(P_SPHERE);
413       
414        rdtscl(end);
415        delete w;
416        mittel += (end - ini - dt);
417        }
418        mi = mittel / (float)ITERATIONS;
419        printf(" Generate a Primitive  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          Vector* v = new Vector();
428         
429          rdtscl(end);
430          delete v;
431          mittel += (end - ini - dt);
432        }
433      mi = mittel / (float)ITERATIONS;
434      printf(" Generate a Vector object:\t\t%11.2f\n", mi);
435
436
437     mittel = 0;
438      for(i = 0; i < ITERATIONS; ++i)
439        {
440          rdtscl(ini);
441         
442          Quaternion* q = new Quaternion();
443         
444          rdtscl(end);
445          delete q;
446          mittel += (end - ini - dt);
447        }
448      mi = mittel / (float)ITERATIONS;
449      printf(" Generate a Quaternion object:\t\t%11.2f\n", mi);
450
451
452
453
454      printf("\nCalling function inline &| virtual, \t\t\t%i\n", ITERATIONS);
455      mittel = 0;
456      w = new WorldEntity();
457      for(i = 0; i < ITERATIONS; ++i)
458        {
459          rdtscl(ini);
460         
461          w->tick(0.0f);
462
463          rdtscl(end);
464          mittel += (end - ini - dt);
465          }
466      //delete w;
467      mi = mittel / (float)ITERATIONS;
468      printf(" Virt funct tick() of WE: \t\t%11.2f\n", mi);
469
470
471      mittel = 0;
472      WorldEntity wo;
473      for(i = 0; i < ITERATIONS; ++i)
474        {
475          rdtscl(ini);
476         
477          wo.tick(0.0f);
478           
479          rdtscl(end);
480          mittel += (end - ini - dt);
481          }
482      //delete w;
483      mi = mittel / (float)ITERATIONS;
484      printf(" Inl virt funct tick() of WE v2: \t%11.2f\n", mi);
485
486     
487      mittel = 0;
488      BaseObject* bo = new BaseObject();
489      for(i = 0; i < ITERATIONS; ++i)
490        {
491          rdtscl(ini);
492         
493          bo->isFinalized();
494           
495          rdtscl(end);
496          mittel += (end - ini - dt);
497          }
498      //delete w;
499      mi = mittel / (float)ITERATIONS;
500      printf(" Inl funct BaseObject::isFinazlized(): \t%11.2f\n", mi);
501
502     
503      tList<WorldEntity>* list = new tList<WorldEntity>();
504
505     
506      /* ************Primitvie class test************** */
507      list = new tList<WorldEntity>();
508 
509     
510      /*
511        mittel = 0;
512        w = new Primitive(P_SPHERE);
513        for(i = 0; i < ITERATIONS; ++i)
514        {
515        rdtscl(ini);
516       
517        w->tick(0.0f);
518       
519        rdtscl(end);
520        mittel += (end - ini - dt);
521        }
522        mi = mittel / (float)ITERATIONS;
523        printf(" Call function tick() of Prim:\t\t%11.2f\n", mi);
524      */
525     
526    }
527 
528  if(type == 1 || type == -1)
529    {
530      printf("\nDoing some simple vector operations: \t\t\t%i\n", VECTOR_MAX);
531      /* vector test */
532      Vector* a = new Vector(1.3, 5.3, 4.1);
533      Vector* b = new Vector(0.4, 2.5, 6.2);
534      Vector* c = new Vector();
535     
536      unsigned long mittel, ini, end;
537      float mi;
538      int i = 0;
539      // addition
540      mittel = 0;
541      for(i = 0; i < VECTOR_MAX; ++i)
542        {
543          rdtscl(ini);
544         
545          *c = *a + *b;
546           
547          rdtscl(end);
548          mittel += (end - ini - dt);
549        }
550      mi = mittel / (float)VECTOR_MAX;
551      printf(" Addition of two vectors:\t\t%11.2f\n", mi);
552     
553      // multiplikation
554
555      mittel = 0;
556      for(i = 0; i < VECTOR_MAX; ++i)
557        {
558          rdtscl(ini);
559         
560          *c = a->cross( *b);
561           
562          rdtscl(end);
563          mittel += (end - ini - dt);
564        }
565      mi = mittel / (float)VECTOR_MAX;
566      printf(" CrossMult of two vectors:\t\t%11.2f\n", mi);
567
568    }
569  if( type == 2 || type == -1)
570    {
571      /* quaternion test */
572      printf("\nDoing some simple quaternion operations: \t\t%i\n", VECTOR_MAX);
573      /* vector test */
574      Quaternion* a = new Quaternion();
575      Quaternion* b = new Quaternion();
576      Quaternion* c = new Quaternion();
577     
578      unsigned long mittel, ini, end;
579      float mi;
580      int i = 0;
581      // quaternion generieren mit spez konstruktor
582      mittel = 0;
583      Vector* qa = new Vector(4.6, 9.3, 0.4);
584      Vector* qb = new Vector(3.5, 6.1, 4.3);
585      for(i = 0; i < VECTOR_MAX; ++i)
586        {
587          rdtscl(ini);
588         
589          Quaternion* qu = new Quaternion(*qa, *qb);
590         
591          rdtscl(end);
592          delete qu;
593          mittel += (end - ini - dt);
594        }
595      delete a;
596      delete b;
597      mi = mittel / (float)VECTOR_MAX;
598      printf(" Gen. quatern. betw. two vectors:\t%11.2f\n", mi);
599     
600     
601      // multiplication
602      mittel = 0;
603      for(i = 0; i < VECTOR_MAX; ++i)
604        {
605          rdtscl(ini);
606         
607          *c = *a * *b;
608         
609          rdtscl(end);
610          mittel += (end - ini - dt);
611        }
612      mi = mittel / (float)VECTOR_MAX;
613      printf(" Multiplying two quat.(=rot): a * b\t%11.2f\n", mi);
614     
615     
616     
617      // rotating a vector by a quaternion
618      mittel = 0;
619      for(i = 0; i < VECTOR_MAX; ++i)
620        {
621          rdtscl(ini);
622         
623          *qa = a->apply(*qb);
624         
625          rdtscl(end);
626          mittel += (end - ini - dt);
627        }
628      mi = mittel / (float)VECTOR_MAX;
629      printf(" Rot a vec by a quat: q->apply(v)\t%11.2f\n", mi);
630     
631     
632     
633      // generate rotation matrix
634      mittel = 0;
635      float matrix[4][4];
636      for(i = 0; i < VECTOR_MAX; ++i)
637        {
638          rdtscl(ini);
639         
640          a->matrix(matrix);
641         
642          rdtscl(end);
643          mittel += (end - ini - dt);
644        }
645      mi = mittel / (float)VECTOR_MAX;
646      printf(" Generate rot matrix: q->matrix(m)\t%11.2f\n", mi);
647    }
648  if( type == 3 || type == -1)
649    {
650      /* list tests*/
651      printf("\nList operations tests: \t\t\t\t\t%i\n", LIST_MAX);
652      tList<char>* list = new tList<char>();
653      char* name;
654     
655      printf(" Adding[1..10] elements to list, found:\n");
656      list->add("1");
657      list->add("2");
658      list->add("3");
659      list->add("4");
660      list->add("5");
661      list->add("6");
662      list->add("7");
663      list->add("8");
664      list->add("9");
665      list->add("10");
666     
667      /*give list out */
668      tIterator<char>* iterator = list->getIterator();
669      name = iterator->nextElement();
670      printf("  List Elements: \t\t");
671      while( name != NULL)
672        {
673          printf("%s,", name);
674          name = iterator->nextElement();
675        }
676      delete iterator;
677      printf("\n");
678     
679     
680      /*removing some elements from the list*/
681      printf(" Removing elements [2,3,6,8,10], adding [11] now found:\n");
682      list->remove("2");
683      list->remove("3");
684      list->remove("6");
685      list->remove("8");
686      list->remove("10");
687      list->add("11");
688      /*give list out */
689      iterator = list->getIterator();
690      name = iterator->nextElement();
691      printf("  List Elements: \t\t");
692      while( name != NULL)
693        {
694          printf("%s,", name);
695          name = iterator->nextElement();
696        }
697      delete iterator;
698      printf("\n");
699     
700      delete list;
701      printf("\nChecking list performance:\t\t\t\t%i\n", LIST_MAX);
702     
703      tList<int>* plist = new tList<int>();
704      unsigned long mittel, ini, end;
705      float mi;
706      int i = 0;
707      mittel = 0;
708      for(i = 0; i < LIST_MAX; ++i)
709        {
710          rdtscl(ini);
711         
712          plist->add(&i);
713         
714          rdtscl(end);
715          mittel += (end - ini - dt);
716        }
717      mi = mittel / (float)LIST_MAX;
718      printf(" Adding reference to list:\t\t%11.2f\n", mi);
719     
720      mittel = 0;
721      for(i = 0; i < LIST_MAX; ++i)
722        {
723          rdtscl(ini);
724         
725          plist->remove(&i);
726         
727          rdtscl(end);
728          mittel += (end - ini - dt);
729        }
730      mi = mittel / (float)LIST_MAX;
731      printf(" Removing 1st reference from list:\t%11.2f\n", mi);
732     
733
734      printf("\nList operations tests: \t\t\t\t\t%i\n", LIST_MAX);
735      list = new tList<char>();
736      printf(" Adding[1..10] elements to list, found:\n");
737      list->add("1");
738      list->add("2");
739      list->add("3");
740      list->add("4");
741      list->add("5");
742      list->add("6");
743      list->add("7");
744      list->add("8");
745      list->add("9");
746      list->add("10");
747     
748      /*give list out */
749      iterator = list->getIterator();
750      name = iterator->nextElement();
751      printf("  List Elements: \t\t");
752      while( name != NULL)
753        {
754          printf("%s,", name);
755          name = iterator->nextElement();
756        }
757      delete iterator;
758      printf("\n");
759     
760     
761      int c = 0;
762      printf(" Going trough list with nextElement(el) func: ");
763      name = list->firstElement();
764      while(c < 20)
765        {
766          printf("%s,", name);
767          name = list->nextElement(name);
768          c++;
769        }
770      printf("\n");
771     
772
773     
774    }
775 
776}
777
778#else
779
780int startBenchmarks()
781{
782  PRINTF(1)("Benchmark is not implemented in this system\n");
783}
784
785#endif
Note: See TracBrowser for help on using the repository browser.