Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: merged the guiMerge-branche back into the trunk
merged with command:
svn merge -r 4043:HEAD guiMerge/ ../trunk/
no conflicts, only updates and moves :)

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