Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7715 was 7715, checked in by bensch, 18 years ago

trunk: mighty templates

File size: 19.9 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#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ORXONOX
28#include "orxonox.h"
29
30#include "globals.h"
31
32#include "gui/qt_gui/qt_gui.h"
33
34#include "file.h"
35#include "parser/ini_parser/ini_parser.h"
36#include "util/loading/game_loader.h"
37#include "util/signal_handler.h"
38
39//ENGINES
40#include "graphics_engine.h"
41#include "sound_engine.h"
42#include "util/loading/resource_manager.h"
43#include "cd_engine.h"
44#include "text_engine.h"
45#include "event_handler.h"
46
47#include "util/loading/factory.h"
48#include "fast_factory.h"
49
50#include "benchmark.h"
51
52#include "class_list.h"
53#include "shell_command_class.h"
54#include "shell_command.h"
55#include "shell_buffer.h"
56
57#include "util/loading/load_param_description.h"
58
59#include "network_manager.h"
60
61#include "state.h"
62#include "lib/parser/preferences/cmd_line_prefs_reader.h"
63#include "lib/parser/preferences/ini_file_prefs_reader.h"
64#include <string.h>
65
66int verbose = 5;
67
68using namespace std;
69
70SHELL_COMMAND(restart, Orxonox, restart);
71
72REGISTER_ARG_FLAG( l, license,    "misc",  "showLicenseAndExit", "Prints the license and exit",      "1" );
73REGISTER_ARG_FLAG( c, client,     "game",  "gameType",           "Connect to Server (-H)",           "multiplayer_client" );
74REGISTER_ARG_FLAG( s, server,     "game",  "gameType",           "Start Orxonox as Game Server",     "multiplayer_server" );
75REGISTER_ARG_ARG(  H, host,       "game",  "host",               "Host to connect to",               "host");
76REGISTER_ARG_ARG(  p, port,       "game",  "port",               "Port to use",                      "port" );
77REGISTER_ARG_FLAG( g, gui,        "game",  "showGui",            "starts the orxonox with the configuration GUI", "1");
78
79REGISTER_ARG_FLAG( f, fullscreen, "video", "Fullscreen-mode",    "start Orxonox in fullscreen mode", "1");
80REGISTER_ARG_FLAG( w, windowed,   "video", "Fullscreen-mode",    "start Orxonox in windowed mode",   "0");
81REGISTER_ARG_ARG(  r, resolution, "video", "Resolution",         "Sets resolution / window size",    "res");
82
83REGISTER_ARG_FLAG( a, audio,      "audio", "Disable-Audio",      "Enable audio",                     "0" );
84REGISTER_ARG_FLAG( m, mute ,      "audio", "Disable-Audio",      "Disable audio",                    "1" );
85REGISTER_ARG_ARG(  _, audio_channels, "audio", "Audio-Channels", "Sets # audio channels", "num" );
86REGISTER_ARG_ARG(  _, music_volume, "audio", "Music-Volume", "Sets music volume", "vol" );
87REGISTER_ARG_ARG(  _, effects_volume, "audio", "Effects-Volume", "Sets effects volume", "vol" );
88
89#ifndef __WIN32__
90REGISTER_ARG_FLAG( _, start_gdb_on_signal, "misc", "start-gdb", "Start gdb on signal", "1");
91REGISTER_ARG_FLAG( _, write_bt_to_file, "misc", "bt-to-file", "Write backtrace to file", "1");
92#endif
93
94/**
95 *  create a new Orxonox
96
97   In this funcitons only global values are set. The game will not be started here.
98*/
99Orxonox::Orxonox ()
100{
101  this->setClassID(CL_ORXONOX, "Orxonox");
102  this->setName("orxonox-main");
103
104  this->argc = 0;
105  this->argv = NULL;
106
107  /* this way, there is no network enabled: */
108  this->serverName = "";
109  this->port = -1;
110
111  this->configFileName = "";
112}
113
114/**
115 *  remove Orxonox from memory
116*/
117Orxonox::~Orxonox ()
118{
119  // game-specific
120  delete GameLoader::getInstance();
121
122  // class-less services/factories
123  Factory::deleteFactories();
124  FastFactory::deleteAll();
125  OrxShell::ShellCommandClass::unregisterAllCommands();
126
127  LoadClassDescription::deleteAllDescriptions();
128
129  // handlers
130  delete ResourceManager::getInstance(); // deletes the Resource Manager
131
132  // engines
133  delete CDEngine::getInstance();
134  delete OrxSound::SoundEngine::getInstance();
135  delete GraphicsEngine::getInstance(); // deleting the Graphics
136  delete EventHandler::getInstance();
137
138  // output-buffer
139  delete OrxShell::ShellBuffer::getInstance();
140
141  SDL_QuitSubSystem(SDL_INIT_TIMER);
142  ClassList::debug();
143
144  PRINT(3)
145  (
146    "===================================================\n" \
147    "Thanks for playing orxonox.\n" \
148    "visit: http://www.orxonox.net for new versions.\n" \
149    "===================================================\n" \
150    ORXONOX_LICENSE_SHORT
151  );
152
153  Orxonox::singletonRef = NULL;
154}
155
156/**
157 *  this is a singleton class to prevent duplicates
158 */
159Orxonox* Orxonox::singletonRef = NULL;
160
161// DANGEROUS
162void Orxonox::restart()
163{
164  //   int argc = this->argc;
165  //   char** argv = this->argv;
166  //
167  //   Orxonox *orx = Orxonox::getInstance();
168  //
169  //   delete orx;
170  //
171  //   orx = Orxonox::getInstance();
172  //
173  //   if((*orx).init(argc, argv) == -1)
174  //   {
175  //     PRINTF(1)("! Orxonox initialization failed\n");
176  //     return;
177  //   }
178  //
179  //   printf("finished inizialisation\n");
180  //   orx->start();
181}
182
183/**
184 * @brief this finds the config file
185 * @returns the new config-fileName
186 * Since the config file varies from user to user and since one may want to specify different config files
187 * for certain occasions or platforms this function finds the right config file for every occasion and stores
188 * it's path and name into configfilename
189*/
190const std::string& Orxonox::getConfigFile ()
191{
192  File orxConfFile("orxonox.conf");
193  if (orxConfFile.isFile())
194  {
195    this->configFileName =  "orxonox.conf";
196  }
197  else
198    this->configFileName = File(DEFAULT_CONFIG_FILE).name();
199
200  PRINTF(3)("Parsed Config File: '%s'\n", this->configFileName.c_str());
201}
202
203/**
204 * initialize Orxonox with command line
205 */
206int Orxonox::init (int argc, char** argv, const std::string & name, int port)
207{
208  this->argc = argc;
209  this->argv = argv;
210
211  this->serverName = name;
212  this->port = port;
213
214  // initialize the Config-file
215  this->getConfigFile();
216
217  // windows must not write into stdout.txt and stderr.txt
218  /*#ifdef __WIN32__
219  freopen( "CON", "w", stdout );
220  freopen( "CON", "w", stderr );
221  #endif*/
222
223  // initialize everything
224  SDL_Init(0);
225  if( initResources () == -1)
226    return -1;
227  if( initVideo() == -1)
228    return -1;
229  if( initSound() == -1)
230    return -1;
231  if( initInput() == -1)
232    return -1;
233  if( initNetworking () == -1)
234    return -1;
235  if( initMisc () == -1)
236    return -1;
237
238  return 0;
239}
240
241
242/**
243 * initializes SDL and OpenGL
244 */
245int Orxonox::initVideo()
246{
247  PRINTF(3)("> Initializing video\n");
248
249  GraphicsEngine::getInstance();
250
251  GraphicsEngine::getInstance()->initFromPreferences();
252
253  std::string iconName = ResourceManager::getFullName("pictures/fighter-top-32x32.bmp");
254  if (!iconName.empty())
255  {
256    GraphicsEngine::getInstance()->setWindowName(PACKAGE_NAME " " PACKAGE_VERSION, iconName);
257  }
258  return 0;
259}
260
261
262/**
263 * initializes the sound engine
264 */
265int Orxonox::initSound()
266{
267  PRINT(3)("> Initializing sound\n");
268  // SDL_InitSubSystem(SDL_INIT_AUDIO);
269  OrxSound::SoundEngine::getInstance();
270
271  OrxSound::SoundEngine::getInstance()->loadSettings();
272  OrxSound::SoundEngine::getInstance()->initAudio();
273  return 0;
274}
275
276
277/**
278 * initializes input functions
279 */
280int Orxonox::initInput()
281{
282  PRINT(3)("> Initializing input\n");
283
284  EventHandler::getInstance()->
285  EventHandler::getInstance()->init();
286  EventHandler::getInstance()->subscribe(GraphicsEngine::getInstance(), ES_ALL, EV_VIDEO_RESIZE);
287
288  return 0;
289}
290
291
292/**
293 * initializes network system
294 */
295int Orxonox::initNetworking()
296{
297  PRINT(3)("> Initializing networking\n");
298
299  if( this->serverName != "") // we are a client
300  {
301    State::setOnline(true);
302    NetworkManager::getInstance()->establishConnection(this->serverName, port);
303  }
304  else if( this->port > 0)
305  {    // we are a server
306    State::setOnline(true);
307    NetworkManager::getInstance()->createServer(port);
308  }
309  return 0;
310}
311
312//#include "util/loading/dynamic_loader.h"
313
314/**
315 * initializes and loads resource files
316 */
317int Orxonox::initResources()
318{
319  PRINTF(3)("> Initializing resources\n");
320
321  PRINT(3)("initializing ResourceManager\n");
322
323  // init the resource manager
324  std::string dataPath;
325  if ((dataPath = Preferences::getInstance()->getString(CONFIG_SECTION_GENERAL, CONFIG_NAME_DATADIR, ""))!= "")
326  {
327    if (!ResourceManager::getInstance()->setDataDir(dataPath) &&
328        !ResourceManager::getInstance()->verifyDataDir(DEFAULT_DATA_DIR_CHECKFILE))
329    {
330      PRINTF(1)("Data Could not be located in %s\n", dataPath.c_str());
331    }
332  }
333
334  if (!ResourceManager::getInstance()->verifyDataDir(DEFAULT_DATA_DIR_CHECKFILE))
335  {
336    PRINTF(1)("The DataDirectory %s could not be verified\n\nh" \
337              "!!!  Please Change in File %s Section %s Entry %s to a suitable value !!!\n",
338              ResourceManager::getInstance()->getDataDir().c_str(),
339              this->configFileName.c_str(),
340              CONFIG_SECTION_GENERAL,
341              CONFIG_NAME_DATADIR );
342    OrxGui::Gui* gui = new OrxGui::QtGui(argc, argv);
343    gui->startGui();
344    delete gui;
345    exit(-1);
346  }
347  //! @todo this is a hack and should be loadable
348  std::string imageDir = ResourceManager::getInstance()->getFullName("maps");
349  ResourceManager::getInstance()->addImageDir(imageDir);
350  imageDir = ResourceManager::getInstance()->getFullName("pictures");
351  ResourceManager::getInstance()->addImageDir(imageDir);
352
353  //  DynamicLoader::loadDyLib("libtest.so");
354
355  // start the collision detection engine
356  CDEngine::getInstance();
357  return 0;
358}
359
360/**
361 * initializes miscelaneous features
362 * @return -1 on failure
363 */
364int Orxonox::initMisc()
365{
366  OrxShell::ShellBuffer::getInstance();
367  return 0;
368}
369
370/**
371 *  starts the orxonox game or menu
372 * here is the central orxonox state manager. There are currently two states
373 * - menu
374 * - game-play
375 * both states manage their states themselfs again.
376*/
377void Orxonox::start()
378{
379
380  this->gameLoader = GameLoader::getInstance();
381
382  if( this->port != -1)
383    this->gameLoader->loadNetworkCampaign("worlds/DefaultNetworkCampaign.oxc");
384  else
385    this->gameLoader->loadCampaign("worlds/DefaultCampaign.oxc");                       /* start orxonox in single player mode */
386
387  //  this->gameLoader->loadDebugCampaign(DEBUG_CAMPAIGN_0);
388  this->gameLoader->init();
389  this->gameLoader->start();
390}
391
392
393/**
394 * handles sprecial events from localinput
395 * @param event: an event not handled by the CommandNode
396 */
397// void Orxonox::graphicsHandler(SDL_Event* event)
398// {
399//   // Handle special events such as reshape, quit, focus changes
400//   switch (event->type)
401//     {
402//     case SDL_VIDEORESIZE:
403//       GraphicsEngine* tmpGEngine = GraphicsEngine::getInstance();
404//       tmpGEngine->resolutionChanged(event->resize);
405//       break;
406//     }
407// }
408
409
410
411
412
413
414bool showGui = false;
415
416/// HACK HACK TEST
417
418
419template<typename type> MT_Type ExecutorParamType() { return MT_EXT1; };
420template<> MT_Type ExecutorParamType<bool>() { return MT_EXT1; };
421template<> MT_Type ExecutorParamType<int>() { return MT_INT; };
422template<> MT_Type ExecutorParamType<unsigned int>() { return MT_UINT; };
423template<> MT_Type ExecutorParamType<float>() { return MT_FLOAT; };
424template<> MT_Type ExecutorParamType<char>() { return MT_CHAR; };
425template<> MT_Type ExecutorParamType<const std::string&>() { return MT_STRING; };
426
427template<typename type> type fromString(const std::string& input, type defaultValue) {return defaultValue; };
428template<> bool fromString<bool>(const std::string& input, bool defaultValue) { return isBool(input, defaultValue); };
429template<> int fromString<int>(const std::string& input, int defaultValue) { return isInt(input, defaultValue); };
430template<> unsigned int fromString<unsigned int>(const std::string& input, unsigned int defaultValue) { return isInt(input, defaultValue); };
431template<> float fromString<float>(const std::string& input, float defaultValue) { return isFloat(input, defaultValue); };
432template<> char fromString<char>(const std::string& input, char defaultValue) { return isInt(input, defaultValue); };
433template<> const std::string& fromString<const std::string&>(const std::string& input, const std::string& defaultValue) { return isString(input, defaultValue); };
434
435template<typename type> type getDefault(const MultiType* const defaultValues, unsigned int i) { return (type)0; };
436template<> bool getDefault<bool>(const MultiType* const defaultValues, unsigned int i) { return defaultValues[i].getBool(); };
437template<> int getDefault<int>(const MultiType* const defaultValues, unsigned int i) { return defaultValues[i].getInt(); };
438template<> unsigned int getDefault<unsigned int>(const MultiType* const defaultValues, unsigned int i) { return defaultValues[i].getInt(); };
439template<> float getDefault<float>(const MultiType* const defaultValues, unsigned int i) { return defaultValues[i].getFloat(); };
440template<> char getDefault<char>(const MultiType* const defaultValues, unsigned int i) { return defaultValues[i].getChar(); };
441template<> std::string getDefault<std::string>(const MultiType* const defaultValues, unsigned int i) { return defaultValues[i].getString(); };
442
443
444template<class T> class Executor0Params : public Executor
445{
446private:
447  void (T::*functionPointer)();
448
449public:
450  Executor0Params (void (T::*functionPointer)())
451      : Executor()
452  {
453    this->functorType = Executor_Objective;
454    this->functionPointer = functionPointer;
455  }
456  virtual void operator()(BaseObject* object, const SubString& sub = SubString()) const
457  {
458    (dynamic_cast<T*>(object)->*functionPointer)();
459  };
460
461  Executor* clone() const {};
462};
463
464
465template<class T, typename type0> class Executor1Params : public Executor
466{
467private:
468  void (T::*functionPointer)(type0);
469
470public:
471  Executor1Params (void (T::*functionPointer)(type0))
472      : Executor(ExecutorParamType<type0>())
473  {
474    this->functorType = Executor_Objective;
475    this->functionPointer = functionPointer;
476  }
477  virtual void operator()(BaseObject* object, const SubString& sub = SubString()) const
478  {
479
480    /* // THE VERY COOL DEBUG
481    printf("SUB[0] : %s\n", sub[0].c_str());
482    printf("getDefault<type0>(this->defaultValue, 0):::: %d\n", getDefault<type0>(this->defaultValue, 0));
483    printf("VALUE: %d\n", fromString<type0>(sub[0], getDefault<type0>(this->defaultValue, 0)));
484    */
485    (dynamic_cast<T*>(object)->*functionPointer)( fromString<type0>(sub[0], getDefault<type0>(this->defaultValue, 0)) );
486  };
487
488  virtual Executor* clone() const {};
489};
490
491/// DOUBLE PENETRATION
492template<class T, typename type0, typename type1> class Executor2Params : public Executor
493{
494private:
495  void (T::*functionPointer)(type0, type1);
496
497public:
498  Executor2Params (void (T::*functionPointer)(type0, type1))
499      : Executor(ExecutorParamType<type0>(), ExecutorParamType<type1>())
500  {
501    this->functorType = Executor_Objective;
502    this->functionPointer = functionPointer;
503  }
504  virtual void operator()(BaseObject* object, const SubString& sub = SubString()) const
505  {
506    (dynamic_cast<T*>(object)->*functionPointer)(
507      fromString<type0>(sub[0], getDefault<type0>(this->defaultValue, 0)),
508      fromString<type1>(sub[1], getDefault<type1>(this->defaultValue, 1)));
509  };
510
511  virtual Executor* clone() const {};
512};
513
514
515template<class T> Executor* createExecutor(void (T::*functionPointer)())
516{
517  return new Executor0Params<T>(functionPointer);
518}
519template<class T> Executor* createExecutor(void (T::*functionPointer)(bool))
520{
521  return new Executor1Params<T, bool>(functionPointer);
522}
523template<class T> Executor* createExecutor(void (T::*functionPointer)(int))
524{
525  return new Executor1Params<T, int>(functionPointer);
526}
527template<class T> Executor* createExecutor(void (T::*functionPointer)(bool, int))
528{
529  return new Executor2Params<T, bool, int>(functionPointer);
530}
531
532
533class TestClass : public BaseObject
534{
535public:
536  TestClass() {};
537
538  void printTest() { printf ("TEST\n"); };
539  void printTestInt(int i) { printf ("%d\n", i); };
540  void printTestBool(bool b) { printf("%d\n", (int)b); };
541  void printTwoVals(bool b, int i) { printf ("%d %d\n", b, i); };
542};
543
544void TEST()
545{
546  TestClass test;
547  SubString testStrings("1, 2, 3", ",", SubString::WhiteSpaces, false);
548  (*createExecutor(&TestClass::printTest))(&test, testStrings);
549  (*createExecutor(&TestClass::printTestInt))(&test, testStrings);
550  (*createExecutor(&TestClass::printTestBool))(&test, testStrings);
551  (*createExecutor(&TestClass::printTwoVals))(&test, testStrings);
552
553}
554
555//// HACK HACK
556
557/**********************************
558*** ORXONOX MAIN STARTING POINT ***
559**********************************/
560/**
561 *
562 *  main function
563 *
564 * here the journey begins
565*/
566int main(int argc, char** argv)
567{
568  TEST();
569  CmdLinePrefsReader prefs;
570
571  IniFilePrefsReader ini(File(DEFAULT_CONFIG_FILE).name());
572  Preferences::getInstance()->setUserIni(File(DEFAULT_CONFIG_FILE).name());
573
574  prefs.parse(argc, argv);
575
576  if ( Preferences::getInstance()->getString("misc", "showLicenseAndExit", "") == "1" )
577  {
578    PRINT(0)(ORXONOX_LICENSE_SHORT);
579    return 0;
580  }
581
582  if ( Preferences::getInstance()->getString("misc", "start-gdb", "0") == "1" )
583  {
584    SignalHandler::getInstance()->doCatch( argv[0], GDB_RUN_IN_FOREGROUND );
585  }
586  else if ( Preferences::getInstance()->getString("misc", "bt-to-file", "1") == "1" )
587  {
588    SignalHandler::getInstance()->doCatch( argv[0], GDB_RUN_WRITE_TO_FILE );
589
590  }
591
592  if( Preferences::getInstance()->getString("game", "showGui", "") == "1" )
593    showGui = true;
594  else if( Preferences::getInstance()->getString( "game", "gameType", "" ) == "multiplayer_server" ||
595           Preferences::getInstance()->getString( "game", "gameType", "" ) == "multiplayer_client" )
596    return startNetworkOrxonox(argc, argv);
597
598  return startOrxonox(argc, argv, "", -1);
599  return 0;
600}
601
602
603
604/**
605 * starts orxonox in network mode
606 * @param argc parameters count given to orxonox
607 * @param argv parameters given to orxonox
608 */
609int startNetworkOrxonox(int argc, char** argv)
610{
611
612  std::string gameType = Preferences::getInstance()->getString( "game", "gameType", "" );
613
614  if ( gameType == "multiplayer_client" )
615  {
616    int port = Preferences::getInstance()->getInt( "game", "port", DEFAULT_ORXONOX_PORT );
617    std::string host = Preferences::getInstance()->getString( "game", "host", "" );
618
619    if ( host == "" )
620    {
621      printf("You need to specify a host to connect to ( -H <host> )\n");
622      return 1;
623    }
624
625    printf("Starting Orxonox as client: connecting to %s, on port %i\n", host.c_str(), port);
626
627    startOrxonox(argc, argv, host.c_str(), port);
628  }
629  else if ( gameType == "multiplayer_server" )
630  {
631    int port = Preferences::getInstance()->getInt( "game", "port", DEFAULT_ORXONOX_PORT );
632
633    printf("Starting Orxonox as server: listening on port %i\n", port);
634
635    startOrxonox(argc, argv, "", port);
636  }
637}
638
639
640
641/**
642 * starts orxonox
643 * @param argc parameters count given to orxonox
644 * @param argv parameters given to orxonox
645 */
646int startOrxonox(int argc, char** argv, const std::string & name, int port)
647{
648  // checking for existence of the configuration-files, or if the lock file is still used
649  if (showGui || (!File("./orxonox.conf").isFile() &&
650                  !File(DEFAULT_CONFIG_FILE).isFile())
651#if DEBUG < 3 // developers do not need to see the GUI, when orxonox fails
652      || ResourceManager::isFile(DEFAULT_LOCK_FILE)
653#endif
654     )
655  {
656    File lockFile(DEFAULT_LOCK_FILE);
657    if (lockFile.isFile())
658      lockFile.remove();
659
660    // starting the GUI
661    OrxGui::QtGui gui(argc, argv);
662    gui.startGui();
663
664    if (gui.getState() & OrxGui::Gui::Quitting)
665      return 0;
666
667  }
668
669  PRINT(0)(">>> Starting Orxonox <<<\n");
670
671  File(DEFAULT_LOCK_FILE).touch();
672
673  Orxonox *orx = Orxonox::getInstance();
674
675  if( orx->init(argc, argv, name, port) == -1)
676  {
677    PRINTF(1)("! Orxonox initialization failed\n");
678    return -1;
679  }
680
681  printf("finished inizialisation\n");
682  orx->start();
683
684  delete orx;
685  File("~/.orxonox/orxonox.lock").remove();
686}
Note: See TracBrowser for help on using the repository browser.