Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10666 was 10618, checked in by bknecht, 18 years ago

merged cleanup into trunk (only improvements)

File size: 16.8 KB
RevLine 
[4982]1/*
[1850]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,
[4556]18   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
[1850]19
[1855]20
21   ### File Specific:
22   main-programmer: Patrick Boenzli
[5303]23   co-programmer: Christian Meyer
[4054]24   co-programmer: Benjamin Grauer: injected ResourceManager/GraphicsEngine/GUI
[1850]25*/
26
[5303]27#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ORXONOX
[2190]28#include "orxonox.h"
[3610]29
[10618]30#include "orxonox_globals.h"
[5819]31
[8145]32#include "gui/qt/qt_gui.h"
[8749]33#include "gui/qt/qt_gui_datadir_fallback.h"
[4054]34
[5944]35#include "parser/ini_parser/ini_parser.h"
[7193]36#include "util/loading/game_loader.h"
[7440]37#include "util/signal_handler.h"
[4786]38
39//ENGINES
[3610]40#include "graphics_engine.h"
[4504]41#include "sound_engine.h"
[7193]42#include "util/loading/resource_manager.h"
[9869]43
[4786]44#include "cd_engine.h"
45#include "event_handler.h"
46
[9869]47#include "loading/fast_factory.h"
[4980]48
[5641]49#include "shell_command_class.h"
[5165]50#include "shell_command.h"
[4748]51
[5996]52#include "network_manager.h"
[9406]53#include "shared_network_data.h"
[5996]54
[6695]55#include "state.h"
[7256]56#include "lib/parser/preferences/cmd_line_prefs_reader.h"
57#include "lib/parser/preferences/ini_file_prefs_reader.h"
[2190]58#include <string.h>
[4032]59
[7711]60int verbose = 5;
[2036]61
[1803]62
[9406]63
[5207]64SHELL_COMMAND(restart, Orxonox, restart);
65
[7258]66REGISTER_ARG_FLAG( l, license,    "misc",  "showLicenseAndExit", "Prints the license and exit",      "1" );
67REGISTER_ARG_FLAG( c, client,     "game",  "gameType",           "Connect to Server (-H)",           "multiplayer_client" );
[9406]68REGISTER_ARG_FLAG( s, server,     "game",  "gameType",           "Start Orxonox as Game Server",     "multiplayer_master_server" );
69REGISTER_ARG_FLAG( x, proxy,      "game",  "gameType",           "Start Orxonox as Proxy Server",    "multiplayer_proxy_server" );
[7258]70REGISTER_ARG_ARG(  H, host,       "game",  "host",               "Host to connect to",               "host");
71REGISTER_ARG_ARG(  p, port,       "game",  "port",               "Port to use",                      "port" );
72REGISTER_ARG_FLAG( g, gui,        "game",  "showGui",            "starts the orxonox with the configuration GUI", "1");
[7256]73
[7258]74REGISTER_ARG_FLAG( f, fullscreen, "video", "Fullscreen-mode",    "start Orxonox in fullscreen mode", "1");
75REGISTER_ARG_FLAG( w, windowed,   "video", "Fullscreen-mode",    "start Orxonox in windowed mode",   "0");
[9406]76REGISTER_ARG_ARG(  r, resolution, "video", "Resolution",         "sets resolution / window size",    "res");
77REGISTER_ARG_FLAG( d, dedicated,  "video", "Norender-mode",      "the scene is not rendered",        "1" );
[7258]78
[7261]79REGISTER_ARG_FLAG( a, audio,      "audio", "Disable-Audio",      "Enable audio",                     "0" );
80REGISTER_ARG_FLAG( m, mute ,      "audio", "Disable-Audio",      "Disable audio",                    "1" );
81REGISTER_ARG_ARG(  _, audio_channels, "audio", "Audio-Channels", "Sets # audio channels", "num" );
82REGISTER_ARG_ARG(  _, music_volume, "audio", "Music-Volume", "Sets music volume", "vol" );
83REGISTER_ARG_ARG(  _, effects_volume, "audio", "Effects-Volume", "Sets effects volume", "vol" );
84
[7440]85#ifndef __WIN32__
86REGISTER_ARG_FLAG( _, write_bt_to_file, "misc", "bt-to-file", "Write backtrace to file", "1");
[10114]87REGISTER_ARG_FLAG( _, dont_catch_signals, "misc", "bt-to-file", "Write backtrace to file", "0");
[7440]88#endif
89
[7954]90REGISTER_ARG_ARG(  t, telnetport,  "network","telnetport",        "Port to use for network debug output",               "port" );
91REGISTER_ARG_ARG(  _, write_dict,  "compression", "writedict",    "write packets to DATA/dicts/newdict",               "numBytes" );
92
[9869]93ObjectListDefinition(Orxonox);
[7954]94
[2190]95/**
[4836]96 *  create a new Orxonox
[4135]97
98   In this funcitons only global values are set. The game will not be started here.
[2190]99*/
100Orxonox::Orxonox ()
[1872]101{
[9869]102  this->registerObject(this, Orxonox::_objectList);
[4766]103  this->setName("orxonox-main");
[4059]104
[4135]105  this->argc = 0;
106  this->argv = NULL;
[4782]107
[5996]108  /* this way, there is no network enabled: */
[7256]109  this->serverName = "";
[5996]110  this->port = -1;
111
[7221]112  this->configFileName = "";
[1872]113}
[1803]114
[2190]115/**
[4836]116 *  remove Orxonox from memory
[2190]117*/
[4556]118Orxonox::~Orxonox ()
[2190]119{
[5285]120  // game-specific
[4815]121  delete GameLoader::getInstance();
[5285]122
123  // class-less services/factories
[4980]124  FastFactory::deleteAll();
[7374]125  OrxShell::ShellCommandClass::unregisterAllCommands();
[5332]126
[7427]127  // handlers
[9869]128  Resources::ResourceManager::deleteInstance(); // deletes the Resource Manager
[7427]129
[5285]130  // engines
131  delete CDEngine::getInstance();
[7460]132  delete OrxSound::SoundEngine::getInstance();
[5285]133  delete GraphicsEngine::getInstance(); // deleting the Graphics
[4817]134  delete EventHandler::getInstance();
[5285]135
[9869]136  Resources::ResourceManager::deleteInstance();
[5285]137  // output-buffer
[9869]138  delete DebugBuffer::getInstance();
[5285]139
[5225]140  SDL_QuitSubSystem(SDL_INIT_TIMER);
[9406]141
[9869]142
143  ObjectListBase::debugAll(1);
144
[9235]145  Preferences::getInstance()->save();
[1850]146
[4833]147  PRINT(3)
[5996]148  (
149    "===================================================\n" \
150    "Thanks for playing orxonox.\n" \
151    "visit: http://www.orxonox.net for new versions.\n" \
152    "===================================================\n" \
153    ORXONOX_LICENSE_SHORT
154  );
[1872]155
[4766]156  Orxonox::singletonRef = NULL;
[1850]157}
158
[2190]159/**
[4836]160 *  this is a singleton class to prevent duplicates
[4766]161 */
162Orxonox* Orxonox::singletonRef = NULL;
[4556]163
[5207]164// DANGEROUS
165void Orxonox::restart()
166{
[5996]167  //   int argc = this->argc;
168  //   char** argv = this->argv;
169  //
170  //   Orxonox *orx = Orxonox::getInstance();
171  //
172  //   delete orx;
173  //
174  //   orx = Orxonox::getInstance();
175  //
176  //   if((*orx).init(argc, argv) == -1)
177  //   {
178  //     PRINTF(1)("! Orxonox initialization failed\n");
179  //     return;
180  //   }
181  //
182  //   printf("finished inizialisation\n");
183  //   orx->start();
[5207]184}
185
[4766]186/**
[7221]187 * @brief this finds the config file
[4766]188 * @returns the new config-fileName
189 * Since the config file varies from user to user and since one may want to specify different config files
190 * for certain occasions or platforms this function finds the right config file for every occasion and stores
191 * it's path and name into configfilename
[2190]192*/
[7221]193const std::string& Orxonox::getConfigFile ()
[1850]194{
[7661]195  File orxConfFile("orxonox.conf");
196  if (orxConfFile.isFile())
[5424]197  {
[7221]198    this->configFileName =  "orxonox.conf";
[5424]199  }
200  else
[7661]201    this->configFileName = File(DEFAULT_CONFIG_FILE).name();
[7256]202
[7677]203  PRINTF(3)("Parsed Config File: '%s'\n", this->configFileName.c_str());
[8316]204  return this->configFileName;
[1803]205}
206
[2190]207/**
[4833]208 * initialize Orxonox with command line
209 */
[7256]210int Orxonox::init (int argc, char** argv, const std::string & name, int port)
[1803]211{
[4135]212  this->argc = argc;
213  this->argv = argv;
[4556]214
[5996]215  this->serverName = name;
216  this->port = port;
217
[4766]218  // initialize the Config-file
[4830]219  this->getConfigFile();
[4766]220
[5788]221  // windows must not write into stdout.txt and stderr.txt
[6833]222  /*#ifdef __WIN32__
[5788]223  freopen( "CON", "w", stdout );
224  freopen( "CON", "w", stderr );
[6833]225  #endif*/
[5788]226
[5996]227  // initialize everything
[6079]228  SDL_Init(0);
[10150]229  if( initResources () == -1)
230    return -1;
[8749]231  if( initVideo() == -1)
232    return -1;
[5996]233  if( initSound() == -1)
234    return -1;
235  if( initInput() == -1)
236    return -1;
237  if( initNetworking () == -1)
238    return -1;
239  if( initMisc () == -1)
240    return -1;
[4556]241
[2636]242  return 0;
[1850]243}
[1849]244
[5996]245
[2190]246/**
[4833]247 * initializes SDL and OpenGL
[5996]248 */
[4556]249int Orxonox::initVideo()
[2190]250{
[3611]251  PRINTF(3)("> Initializing video\n");
[4556]252
[3610]253  GraphicsEngine::getInstance();
[4556]254
[7256]255  GraphicsEngine::getInstance()->initFromPreferences();
[4766]256
[10317]257  std::string iconName = Resources::ResourceManager::getInstance()->prependAbsoluteMainPath("textures/fighter-top-32x32.bmp");
[7221]258  if (!iconName.empty())
[5225]259  {
260    GraphicsEngine::getInstance()->setWindowName(PACKAGE_NAME " " PACKAGE_VERSION, iconName);
261  }
[2190]262  return 0;
263}
[1850]264
[5996]265
[2190]266/**
[4833]267 * initializes the sound engine
268 */
[4556]269int Orxonox::initSound()
[2190]270{
[4504]271  PRINT(3)("> Initializing sound\n");
[5225]272  // SDL_InitSubSystem(SDL_INIT_AUDIO);
[7460]273  OrxSound::SoundEngine::getInstance();
[4985]274
[7460]275  OrxSound::SoundEngine::getInstance()->loadSettings();
276  OrxSound::SoundEngine::getInstance()->initAudio();
[2636]277  return 0;
[2190]278}
[1900]279
[3214]280
[2190]281/**
[4833]282 * initializes input functions
283 */
[4556]284int Orxonox::initInput()
[2190]285{
[4766]286  PRINT(3)("> Initializing input\n");
287
[7256]288  EventHandler::getInstance()->init();
[4833]289  EventHandler::getInstance()->subscribe(GraphicsEngine::getInstance(), ES_ALL, EV_VIDEO_RESIZE);
[4556]290
[2636]291  return 0;
[1803]292}
293
[3214]294
[2190]295/**
[4833]296 * initializes network system
297 */
[4556]298int Orxonox::initNetworking()
[1897]299{
[4766]300  PRINT(3)("> Initializing networking\n");
[9406]301  std::string gameType = Preferences::getInstance()->getString( "game", "gameType", "" );
[4766]302
[9406]303  if( gameType == "multiplayer_client")
304  {    // we are a client
[6695]305    State::setOnline(true);
[9406]306    SharedNetworkData::getInstance()->setNodeType(NET_CLIENT);
307    NetworkManager::getInstance()->createClient(this->serverName, port);
[6695]308  }
[9406]309  else if( gameType == "multiplayer_master_server")
310  {    // we are a master server
[6695]311    State::setOnline(true);
[9406]312    SharedNetworkData::getInstance()->setNodeType(NET_MASTER_SERVER);
313
314    NetworkManager::getInstance()->createMasterServer(port);
[6139]315  }
[9406]316  else if( gameType == "multiplayer_proxy_server")
317  {    // we are a proxy server
318    State::setOnline(true);
319    SharedNetworkData::getInstance()->setNodeType(NET_PROXY_SERVER_ACTIVE);
320    NetworkManager::getInstance()->createProxyServer(port);
321  }
322
[2636]323  return 0;
[9406]324
[1897]325}
326
[7355]327//#include "util/loading/dynamic_loader.h"
[2190]328/**
[4833]329 * initializes and loads resource files
[4766]330 */
[4833]331int Orxonox::initResources()
[1858]332{
[4766]333  PRINTF(3)("> Initializing resources\n");
[4091]334
[4766]335  PRINT(3)("initializing ResourceManager\n");
336
[5488]337  // init the resource manager
[9880]338  std::string dataPath = Preferences::getInstance()->getString(CONFIG_SECTION_GENERAL, CONFIG_NAME_DATADIR, "");
339  if (dataPath!= "")
[4766]340  {
[9869]341    Resources::ResourceManager::getInstance()->setMainGlobalPath(dataPath) ;
342    if(!Resources::ResourceManager::getInstance()->checkFileInMainPath(File(DEFAULT_DATA_DIR_CHECKFILE)))
[4766]343    {
[7221]344      PRINTF(1)("Data Could not be located in %s\n", dataPath.c_str());
[4766]345    }
346  }
[9869]347  while (!Resources::ResourceManager::getInstance()->checkFileInMainPath(File(DEFAULT_DATA_DIR_CHECKFILE)))
[4766]348  {
[8749]349
[5510]350    PRINTF(1)("The DataDirectory %s could not be verified\n\nh" \
351              "!!!  Please Change in File %s Section %s Entry %s to a suitable value !!!\n",
[9869]352              Resources::ResourceManager::getInstance()->mainGlobalPath().name().c_str(),
[7714]353              this->configFileName.c_str(),
[7661]354              CONFIG_SECTION_GENERAL,
[5510]355              CONFIG_NAME_DATADIR );
[8749]356    OrxGui::Gui* gui = new OrxGui::QtGuiDataDirFallback(argc, argv);
[5479]357    gui->startGui();
[8750]358    if (gui->getState() == OrxGui::Gui::Quitting)
359      return(-1);
[5479]360    delete gui;
[9869]361    Resources::ResourceManager::getInstance()->setMainGlobalPath(Preferences::getInstance()->getString(CONFIG_SECTION_GENERAL, CONFIG_NAME_DATADIR, ""));
[4766]362  }
[8749]363
[5996]364  //! @todo this is a hack and should be loadable
[10317]365  Resources::ResourceManager::getInstance()->addResourceSubPath("Texture", "textures");
366  //Resources::ResourceManager::getInstance()->addResourceSubPath("Texture", "pictures");
[4009]367
[10317]368  Resources::ResourceManager::getInstance()->addResourceSubPath("SoundBuffer", "sounds");
[9869]369  Resources::ResourceManager::getInstance()->addResourceSubPath("SoundBuffer", "music");
370
371  Resources::ResourceManager::getInstance()->addResourceSubPath("Shader", "shaders");
372  Resources::ResourceManager::getInstance()->addResourceSubPath("OBJ", "models");
373
374  Resources::ResourceManager::getInstance()->addKeepLevelName("Imediately");
375  Resources::ResourceManager::getInstance()->addKeepLevelName("LevelEnd");
376  Resources::ResourceManager::getInstance()->addKeepLevelName("CampaignEnd");
377  Resources::ResourceManager::getInstance()->addKeepLevelName("GameEnd");
378  Resources::ResourceManager::getInstance()->setDefaultKeepLevel(std::string("GameEnd"));
379
[5074]380  return 0;
381}
382
383/**
384 * initializes miscelaneous features
385 * @return -1 on failure
386 */
387int Orxonox::initMisc()
388{
[9869]389  DebugBuffer::getInstance();
[8749]390
391  // start the collision detection engine
392  CDEngine::getInstance();
393
[4766]394  return 0;
[1858]395}
[1849]396
[2190]397/**
[4836]398 *  starts the orxonox game or menu
[4833]399 * here is the central orxonox state manager. There are currently two states
400 * - menu
401 * - game-play
402 * both states manage their states themselfs again.
[2190]403*/
[2636]404void Orxonox::start()
405{
[4556]406
[2636]407  this->gameLoader = GameLoader::getInstance();
[5996]408
[6139]409  if( this->port != -1)
[10317]410    this->gameLoader->loadNetworkCampaign("levels/DefaultNetworkCampaign.oxc");
[5996]411  else
[10317]412    this->gameLoader->loadCampaign("levels/DefaultCampaign.oxc");                       /* start orxonox in single player mode */
[5996]413
[4010]414  //  this->gameLoader->loadDebugCampaign(DEBUG_CAMPAIGN_0);
[2636]415  this->gameLoader->init();
416  this->gameLoader->start();
417}
418
[3214]419
[2636]420/**
[4833]421 * handles sprecial events from localinput
422 * @param event: an event not handled by the CommandNode
423 */
[4817]424// void Orxonox::graphicsHandler(SDL_Event* event)
425// {
426//   // Handle special events such as reshape, quit, focus changes
427//   switch (event->type)
428//     {
429//     case SDL_VIDEORESIZE:
430//       GraphicsEngine* tmpGEngine = GraphicsEngine::getInstance();
431//       tmpGEngine->resolutionChanged(event->resize);
432//       break;
433//     }
434// }
[1875]435
[4556]436
[4408]437
[1803]438
[3214]439
[4782]440
[4059]441bool showGui = false;
[3648]442
[4766]443/**********************************
444*** ORXONOX MAIN STARTING POINT ***
445**********************************/
[3449]446/**
[4833]447 *
[4836]448 *  main function
[4833]449 *
450 * here the journey begins
[3449]451*/
[4556]452int main(int argc, char** argv)
453{
[7256]454  CmdLinePrefsReader prefs;
[7374]455
[10015]456  // create the ~/.orxonox directory if there is not already one
457  Directory home("~/.orxonox");
[10141]458  if (!home.exists())
459    home.create();
[10015]460
[7661]461  IniFilePrefsReader ini(File(DEFAULT_CONFIG_FILE).name());
462  Preferences::getInstance()->setUserIni(File(DEFAULT_CONFIG_FILE).name());
[7374]463
[7256]464  prefs.parse(argc, argv);
[7374]465
[7256]466  if ( Preferences::getInstance()->getString("misc", "showLicenseAndExit", "") == "1" )
[5996]467  {
[7256]468    PRINT(0)(ORXONOX_LICENSE_SHORT);
469    return 0;
[5996]470  }
[7374]471
[9240]472  if ( Preferences::getInstance()->getString("misc", "bt-to-file", "1") == "1" )
[7440]473  {
[9406]474    SignalHandler::getInstance()->doCatch( argv[0], "orxonox.backtrace" );
475    SignalHandler::getInstance()->registerCallback( EventHandler::releaseMouse, NULL );
[7440]476  }
[7460]477
[7256]478  if( Preferences::getInstance()->getString("game", "showGui", "") == "1" )
479    showGui = true;
[9406]480  else if( Preferences::getInstance()->getString( "game", "gameType", "" ) == "multiplayer_master_server" ||
481           Preferences::getInstance()->getString( "game", "gameType", "" ) == "multiplayer_proxy_server" ||
[7256]482           Preferences::getInstance()->getString( "game", "gameType", "" ) == "multiplayer_client" )
483    return startNetworkOrxonox(argc, argv);
[7374]484
[7256]485  return startOrxonox(argc, argv, "", -1);
[3648]486}
487
488
489
[5996]490/**
491 * starts orxonox in network mode
492 * @param argc parameters count given to orxonox
493 * @param argv parameters given to orxonox
494 */
495int startNetworkOrxonox(int argc, char** argv)
496{
497
[7256]498  std::string gameType = Preferences::getInstance()->getString( "game", "gameType", "" );
[7374]499
[7256]500  if ( gameType == "multiplayer_client" )
[4132]501  {
[7256]502    int port = Preferences::getInstance()->getInt( "game", "port", DEFAULT_ORXONOX_PORT );
503    std::string host = Preferences::getInstance()->getString( "game", "host", "" );
[7374]504
[7256]505    if ( host == "" )
[5996]506    {
[7256]507      printf("You need to specify a host to connect to ( -H <host> )\n");
508      return 1;
[5996]509    }
[7374]510
[7256]511    printf("Starting Orxonox as client: connecting to %s, on port %i\n", host.c_str(), port);
[7374]512
[7256]513    startOrxonox(argc, argv, host.c_str(), port);
[4132]514  }
[9406]515  else if ( gameType == "multiplayer_master_server" )
[7256]516  {
517    int port = Preferences::getInstance()->getInt( "game", "port", DEFAULT_ORXONOX_PORT );
[7374]518
[7256]519    printf("Starting Orxonox as server: listening on port %i\n", port);
[7374]520
[7256]521    startOrxonox(argc, argv, "", port);
522  }
[9406]523  else if ( gameType == "multiplayer_proxy_server" )
524  {
525    int port = Preferences::getInstance()->getInt( "game", "port", DEFAULT_ORXONOX_PORT );
526
527    printf("Starting Orxonox as proxy server: listening on port %i\n", port);
528
529    startOrxonox(argc, argv, "", port);
530  }
[8316]531  return 1;
[3648]532}
533
[3649]534
[4766]535
536/**
537 * starts orxonox
538 * @param argc parameters count given to orxonox
539 * @param argv parameters given to orxonox
540 */
[7256]541int startOrxonox(int argc, char** argv, const std::string & name, int port)
[3648]542{
[4830]543  // checking for existence of the configuration-files, or if the lock file is still used
[7661]544  if (showGui || (!File("./orxonox.conf").isFile() &&
[7714]545                  !File(DEFAULT_CONFIG_FILE).isFile())
[7729]546#if DEBUG_LEVEL <= 3 // developers do not need to see the GUI, when orxonox fails
[5996]547      || ResourceManager::isFile(DEFAULT_LOCK_FILE)
[4981]548#endif
549     )
[5996]550  {
[7661]551    File lockFile(DEFAULT_LOCK_FILE);
552    if (lockFile.isFile())
553      lockFile.remove();
[4556]554
[5996]555    // starting the GUI
[7661]556    OrxGui::QtGui gui(argc, argv);
557    gui.startGui();
[4132]558
[7661]559    if (gui.getState() & OrxGui::Gui::Quitting)
[5996]560      return 0;
[4556]561
[5996]562  }
[4556]563
[4032]564  PRINT(0)(">>> Starting Orxonox <<<\n");
[4033]565
[7661]566  File(DEFAULT_LOCK_FILE).touch();
[4033]567
[1850]568  Orxonox *orx = Orxonox::getInstance();
[4556]569
[5996]570  if( orx->init(argc, argv, name, port) == -1)
571  {
572    PRINTF(1)("! Orxonox initialization failed\n");
573    return -1;
574  }
[4556]575
[9869]576  PRINTF(5)("finished inizialisation\n");
[2636]577  orx->start();
[4556]578
[3676]579  delete orx;
[7661]580  File("~/.orxonox/orxonox.lock").remove();
[8363]581  return 0;
[1803]582}
Note: See TracBrowser for help on using the repository browser.