Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: gui: minor niceness-patch

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