Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/md2_loader/src/orxonox.cc @ 4072

Last change on this file since 4072 was 4072, checked in by patrick, 19 years ago

orxonox/branches/md2_loader: implemented some data formats used by the md2 loader, altered debug levels, and commented out all the debug() functions. This branche won't run on you computer unless you have the fonts and models installed i use temporary for testing purposes

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