Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/guiMerge/src/orxonox.cc @ 4045

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

orxonox/branches/guiMerge: little patch, (1 segfaults less)

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