Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/ll2trunktemp/src/orxonox.cc @ 3989

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

orxonox/branches/ll2trunktemp: some more headers, now the level really gets loaded

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