Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: merged the proxy back

merged with commandsvn merge -r9346:HEAD https://svn.orxonox.net/orxonox/branches/proxy .

no conflicts

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