Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/sound_engine/src/orxonox.cc @ 3887

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

orxonox/trunk: added some classes to handle Audio

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 "sound_engine.h"
35#include "resource_manager.h"
36#include "text_engine.h"
37
38#include <string.h>
39int verbose = 3;
40
41using namespace std;
42
43/**
44   \brief create a new Orxonox
45*/
46Orxonox::Orxonox ()
47{
48  pause = false;
49}
50
51/**
52   \brief remove Orxonox from memory
53*/
54Orxonox::~Orxonox () 
55{
56  Orxonox::singletonRef = NULL;
57  if( world != NULL) delete world;
58  if( localinput != NULL) delete world;
59  if( resources != NULL) delete resources;
60  delete GraphicsEngine::getInstance(); // delets the Graphics Engine
61  delete SoundEngine::getInstance();    // deletes the Sound Engine
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  SoundEngine::getInstance();
136  printf("implementing\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  resourceManager->setDataDir("../data/");
172  return 0;
173  PRINT(3)("initializing TextEngine\n");
174  TextEngine::getInstance();
175}
176
177
178/**
179   \brief initializes the world
180*/
181int Orxonox::initWorld() 
182{
183  //world = new World();
184 
185  // TO DO: replace this with a menu/intro
186  //world->load_debug_level();
187 
188  return 0;
189}
190
191
192/**
193   \brief starts the orxonox game or menu
194
195   here is the central orxonox state manager. There are currently two states
196   - menu
197   - game-play
198   both states manage their states themselfs again.
199*/
200void Orxonox::start()
201{
202 
203  this->gameLoader = GameLoader::getInstance();
204  this->gameLoader->loadDebugCampaign(DEBUG_CAMPAIGN_0);
205  this->gameLoader->init();
206  this->gameLoader->start();
207}
208
209
210/**
211   \brief exits Orxonox
212*/
213void Orxonox::quitGame() 
214{
215  bQuitOrxonox = true;
216}
217
218
219
220/**
221   \brief handles sprecial events from localinput
222   \param event: an event not handled by the CommandNode
223*/
224void Orxonox::eventHandler(SDL_Event* event)
225{
226  // Handle special events such as reshape, quit, focus changes
227  switch (event->type)
228    {
229    case SDL_VIDEORESIZE:
230      GraphicsEngine* tmpGEngine = GraphicsEngine::getInstance();
231      tmpGEngine->resolutionChanged(&event->resize);
232      break;
233    }
234}
235 
236
237/**
238   \brief handle keyboard commands that are not meant for WorldEntities
239   \param cmd: the command to handle
240   \return true if the command was handled by the system or false if it may be passed to the WorldEntities
241*/
242bool Orxonox::systemCommand(Command* cmd)
243{
244  /*
245  if( !strcmp( cmd->cmd, "quit"))
246    {
247      if( !cmd->bUp) this->gameLoader->stop();
248      return true;
249    }
250  return false;
251  */
252  return false;
253}
254
255/**
256   \brief retrieve a pointer to the local CommandNode
257   \return a pointer to localinput
258*/
259CommandNode* Orxonox::getLocalInput()
260{
261  return localinput;
262}
263
264
265/**
266   \brief retrieve a pointer to the local World
267   \return a pointer to world
268*/
269World* Orxonox::getWorld()
270{
271  return world;
272}
273
274/**
275   \return The reference of the SDL-screen of orxonox
276*/
277SDL_Surface* Orxonox::getScreen ()
278{
279  return this->screen;
280}
281
282
283
284/**
285   \brief main function
286
287   here the journey begins
288*/
289int main(int argc, char** argv) 
290{ 
291 
292  /* reading arguments
293     
294     currently supported arguments are:
295     <no args>                   ::    just starts orxonox
296     --benchmark                 ::    start the benchmark without starting orxonox
297     
298     this is a preselection: it matches to one of the start* functions, the
299     finetuning is made in those functions.
300  */
301
302
303  int i;
304  for(i = 0; i < argc; ++i)
305    {
306      if(! strcmp( "--help", argv[i])) return startHelp();
307      else if(! strcmp( "--benchmark", argv[i])) return startBenchmarks();
308    }
309
310  PRINTF(2)("Orxonox does not understand the arguments");
311  return startOrxonox(argc, argv);
312}
313
314
315
316int startHelp()
317{
318  printf("orxonox: starts the orxonox game - rules\n");
319  printf("usage: orxonox [arg]\n\n");
320  printf("valid options:\n");
321  printf(" --benchmark\tstarts the orxonox benchmark\n");
322  printf(" --help \tshows this menu\n");
323}
324
325
326int startOrxonox(int argc, char** argv)
327{
328  printf(">>> Starting Orxonox <<<\n");
329  Orxonox *orx = Orxonox::getInstance();
330 
331  if((*orx).init(argc, argv) == -1)
332    {
333      printf("! Orxonox initialization failed\n");
334      return -1;
335    }
336 
337  orx->start();
338 
339  delete orx;
340 
341}
342
343#if defined __linux__
344
345#include "list.h"
346#include "world_entity.h"
347#include "vector.h"
348#include "player.h"
349#include "base_object.h"
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.