Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/orxonox.cc @ 8470

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

orxonox/network: signal

File size: 14.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
[5819]30#include "globals.h"
31
[8145]32#include "gui/qt/qt_gui.h"
[4054]33
[7661]34#include "file.h"
[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"
60
[6695]61#include "state.h"
[7256]62#include "lib/parser/preferences/cmd_line_prefs_reader.h"
63#include "lib/parser/preferences/ini_file_prefs_reader.h"
[2190]64#include <string.h>
[4032]65
[7711]66int verbose = 5;
[2036]67
[1803]68using namespace std;
69
[5207]70SHELL_COMMAND(restart, Orxonox, restart);
71
[7258]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");
[7256]78
[7258]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
[7261]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
[7440]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
[7954]94REGISTER_ARG_ARG(  t, telnetport,  "network","telnetport",        "Port to use for network debug output",               "port" );
95REGISTER_ARG_ARG(  _, write_dict,  "compression", "writedict",    "write packets to DATA/dicts/newdict",               "numBytes" );
96
97
[2190]98/**
[4836]99 *  create a new Orxonox
[4135]100
101   In this funcitons only global values are set. The game will not be started here.
[2190]102*/
103Orxonox::Orxonox ()
[1872]104{
[4445]105  this->setClassID(CL_ORXONOX, "Orxonox");
[4766]106  this->setName("orxonox-main");
[4059]107
[4135]108  this->argc = 0;
109  this->argv = NULL;
[4782]110
[5996]111  /* this way, there is no network enabled: */
[7256]112  this->serverName = "";
[5996]113  this->port = -1;
114
[7221]115  this->configFileName = "";
[1872]116}
[1803]117
[2190]118/**
[4836]119 *  remove Orxonox from memory
[2190]120*/
[4556]121Orxonox::~Orxonox ()
[2190]122{
[5285]123  // game-specific
[4815]124  delete GameLoader::getInstance();
[5285]125
126  // class-less services/factories
[5982]127  Factory::deleteFactories();
[4980]128  FastFactory::deleteAll();
[7374]129  OrxShell::ShellCommandClass::unregisterAllCommands();
[5332]130
[5226]131  LoadClassDescription::deleteAllDescriptions();
132
[7427]133  // handlers
134  delete ResourceManager::getInstance(); // deletes the Resource Manager
135
[5285]136  // engines
137  delete CDEngine::getInstance();
[7460]138  delete OrxSound::SoundEngine::getInstance();
[5285]139  delete GraphicsEngine::getInstance(); // deleting the Graphics
[4817]140  delete EventHandler::getInstance();
[5285]141
142  // output-buffer
[7374]143  delete OrxShell::ShellBuffer::getInstance();
[5285]144
[5225]145  SDL_QuitSubSystem(SDL_INIT_TIMER);
[7126]146  ClassList::debug();
[1850]147
[4833]148  PRINT(3)
[5996]149  (
150    "===================================================\n" \
151    "Thanks for playing orxonox.\n" \
152    "visit: http://www.orxonox.net for new versions.\n" \
153    "===================================================\n" \
154    ORXONOX_LICENSE_SHORT
155  );
[1872]156
[4766]157  Orxonox::singletonRef = NULL;
[1850]158}
159
[2190]160/**
[4836]161 *  this is a singleton class to prevent duplicates
[4766]162 */
163Orxonox* Orxonox::singletonRef = NULL;
[4556]164
[5207]165// DANGEROUS
166void Orxonox::restart()
167{
[5996]168  //   int argc = this->argc;
169  //   char** argv = this->argv;
170  //
171  //   Orxonox *orx = Orxonox::getInstance();
172  //
173  //   delete orx;
174  //
175  //   orx = Orxonox::getInstance();
176  //
177  //   if((*orx).init(argc, argv) == -1)
178  //   {
179  //     PRINTF(1)("! Orxonox initialization failed\n");
180  //     return;
181  //   }
182  //
183  //   printf("finished inizialisation\n");
184  //   orx->start();
[5207]185}
186
[4766]187/**
[7221]188 * @brief this finds the config file
[4766]189 * @returns the new config-fileName
190 * Since the config file varies from user to user and since one may want to specify different config files
191 * for certain occasions or platforms this function finds the right config file for every occasion and stores
192 * it's path and name into configfilename
[2190]193*/
[7221]194const std::string& Orxonox::getConfigFile ()
[1850]195{
[7661]196  File orxConfFile("orxonox.conf");
197  if (orxConfFile.isFile())
[5424]198  {
[7221]199    this->configFileName =  "orxonox.conf";
[5424]200  }
201  else
[7661]202    this->configFileName = File(DEFAULT_CONFIG_FILE).name();
[7256]203
[7677]204  PRINTF(3)("Parsed Config File: '%s'\n", this->configFileName.c_str());
[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);
[5996]229  if( initResources () == -1)
230    return -1;
231  if( initVideo() == -1)
232    return -1;
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
[7221]257  std::string iconName = ResourceManager::getFullName("pictures/fighter-top-32x32.bmp");
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
[7661]288  EventHandler::getInstance()->
[7256]289  EventHandler::getInstance()->init();
[4833]290  EventHandler::getInstance()->subscribe(GraphicsEngine::getInstance(), ES_ALL, EV_VIDEO_RESIZE);
[4556]291
[2636]292  return 0;
[1803]293}
294
[3214]295
[2190]296/**
[4833]297 * initializes network system
298 */
[4556]299int Orxonox::initNetworking()
[1897]300{
[4766]301  PRINT(3)("> Initializing networking\n");
302
[7256]303  if( this->serverName != "") // we are a client
[6695]304  {
305    State::setOnline(true);
[5996]306    NetworkManager::getInstance()->establishConnection(this->serverName, port);
[6695]307  }
[7714]308  else if( this->port > 0)
309  {    // we are a server
[6695]310    State::setOnline(true);
[5996]311    NetworkManager::getInstance()->createServer(port);
[6139]312  }
[2636]313  return 0;
[1897]314}
315
[7355]316//#include "util/loading/dynamic_loader.h"
[3214]317
[2190]318/**
[4833]319 * initializes and loads resource files
[4766]320 */
[4833]321int Orxonox::initResources()
[1858]322{
[4766]323  PRINTF(3)("> Initializing resources\n");
[4091]324
[4766]325  PRINT(3)("initializing ResourceManager\n");
326
[5488]327  // init the resource manager
[7221]328  std::string dataPath;
[7661]329  if ((dataPath = Preferences::getInstance()->getString(CONFIG_SECTION_GENERAL, CONFIG_NAME_DATADIR, ""))!= "")
[4766]330  {
[5480]331    if (!ResourceManager::getInstance()->setDataDir(dataPath) &&
[5996]332        !ResourceManager::getInstance()->verifyDataDir(DEFAULT_DATA_DIR_CHECKFILE))
[4766]333    {
[7221]334      PRINTF(1)("Data Could not be located in %s\n", dataPath.c_str());
[4766]335    }
336  }
[4556]337
[5480]338  if (!ResourceManager::getInstance()->verifyDataDir(DEFAULT_DATA_DIR_CHECKFILE))
[4766]339  {
[5510]340    PRINTF(1)("The DataDirectory %s could not be verified\n\nh" \
341              "!!!  Please Change in File %s Section %s Entry %s to a suitable value !!!\n",
[7714]342              ResourceManager::getInstance()->getDataDir().c_str(),
343              this->configFileName.c_str(),
[7661]344              CONFIG_SECTION_GENERAL,
[5510]345              CONFIG_NAME_DATADIR );
[7661]346    OrxGui::Gui* gui = new OrxGui::QtGui(argc, argv);
[5479]347    gui->startGui();
348    delete gui;
[4766]349    exit(-1);
350  }
[5996]351  //! @todo this is a hack and should be loadable
[7221]352  std::string imageDir = ResourceManager::getInstance()->getFullName("maps");
[5216]353  ResourceManager::getInstance()->addImageDir(imageDir);
[7067]354  imageDir = ResourceManager::getInstance()->getFullName("pictures");
355  ResourceManager::getInstance()->addImageDir(imageDir);
[4009]356
[7355]357  //  DynamicLoader::loadDyLib("libtest.so");
[7167]358
[5488]359  // start the collision detection engine
[4766]360  CDEngine::getInstance();
[5074]361  return 0;
362}
363
364/**
365 * initializes miscelaneous features
366 * @return -1 on failure
367 */
368int Orxonox::initMisc()
369{
[7374]370  OrxShell::ShellBuffer::getInstance();
[4766]371  return 0;
[1858]372}
[1849]373
[2190]374/**
[4836]375 *  starts the orxonox game or menu
[4833]376 * here is the central orxonox state manager. There are currently two states
377 * - menu
378 * - game-play
379 * both states manage their states themselfs again.
[2190]380*/
[2636]381void Orxonox::start()
382{
[4556]383
[2636]384  this->gameLoader = GameLoader::getInstance();
[5996]385
[6139]386  if( this->port != -1)
387    this->gameLoader->loadNetworkCampaign("worlds/DefaultNetworkCampaign.oxc");
[5996]388  else
[6139]389    this->gameLoader->loadCampaign("worlds/DefaultCampaign.oxc");                       /* start orxonox in single player mode */
[5996]390
[4010]391  //  this->gameLoader->loadDebugCampaign(DEBUG_CAMPAIGN_0);
[2636]392  this->gameLoader->init();
393  this->gameLoader->start();
394}
395
[3214]396
[2636]397/**
[4833]398 * handles sprecial events from localinput
399 * @param event: an event not handled by the CommandNode
400 */
[4817]401// void Orxonox::graphicsHandler(SDL_Event* event)
402// {
403//   // Handle special events such as reshape, quit, focus changes
404//   switch (event->type)
405//     {
406//     case SDL_VIDEORESIZE:
407//       GraphicsEngine* tmpGEngine = GraphicsEngine::getInstance();
408//       tmpGEngine->resolutionChanged(event->resize);
409//       break;
410//     }
411// }
[1875]412
[4556]413
[4408]414
[1803]415
[3214]416
[4782]417
[4059]418bool showGui = false;
[3648]419
[4766]420/**********************************
421*** ORXONOX MAIN STARTING POINT ***
422**********************************/
[3449]423/**
[4833]424 *
[4836]425 *  main function
[4833]426 *
427 * here the journey begins
[3449]428*/
[4556]429int main(int argc, char** argv)
430{
[7256]431  CmdLinePrefsReader prefs;
[7374]432
[7661]433  IniFilePrefsReader ini(File(DEFAULT_CONFIG_FILE).name());
434  Preferences::getInstance()->setUserIni(File(DEFAULT_CONFIG_FILE).name());
[7374]435
[7256]436  prefs.parse(argc, argv);
[7374]437
[7256]438  if ( Preferences::getInstance()->getString("misc", "showLicenseAndExit", "") == "1" )
[5996]439  {
[7256]440    PRINT(0)(ORXONOX_LICENSE_SHORT);
441    return 0;
[5996]442  }
[7374]443
[7440]444  if ( Preferences::getInstance()->getString("misc", "start-gdb", "0") == "1" )
445  {
446    SignalHandler::getInstance()->doCatch( argv[0], GDB_RUN_IN_FOREGROUND );
[8470]447    SignalHandler::getInstance()->registerCallback(EventHandler::releaseMouse, NULL);
[7440]448  }
449  else if ( Preferences::getInstance()->getString("misc", "bt-to-file", "1") == "1" )
[7714]450  {
451    SignalHandler::getInstance()->doCatch( argv[0], GDB_RUN_WRITE_TO_FILE );
[7460]452
[7440]453  }
[7460]454
[7256]455  if( Preferences::getInstance()->getString("game", "showGui", "") == "1" )
456    showGui = true;
457  else if( Preferences::getInstance()->getString( "game", "gameType", "" ) == "multiplayer_server" ||
458           Preferences::getInstance()->getString( "game", "gameType", "" ) == "multiplayer_client" )
459    return startNetworkOrxonox(argc, argv);
[7374]460
[7256]461  return startOrxonox(argc, argv, "", -1);
462  return 0;
[3648]463}
464
465
466
[5996]467/**
468 * starts orxonox in network mode
469 * @param argc parameters count given to orxonox
470 * @param argv parameters given to orxonox
471 */
472int startNetworkOrxonox(int argc, char** argv)
473{
474
[7256]475  std::string gameType = Preferences::getInstance()->getString( "game", "gameType", "" );
[7374]476
[7256]477  if ( gameType == "multiplayer_client" )
[4132]478  {
[7256]479    int port = Preferences::getInstance()->getInt( "game", "port", DEFAULT_ORXONOX_PORT );
480    std::string host = Preferences::getInstance()->getString( "game", "host", "" );
[7374]481
[7256]482    if ( host == "" )
[5996]483    {
[7256]484      printf("You need to specify a host to connect to ( -H <host> )\n");
485      return 1;
[5996]486    }
[7374]487
[7256]488    printf("Starting Orxonox as client: connecting to %s, on port %i\n", host.c_str(), port);
[7374]489
[7256]490    startOrxonox(argc, argv, host.c_str(), port);
[4132]491  }
[7256]492  else if ( gameType == "multiplayer_server" )
493  {
494    int port = Preferences::getInstance()->getInt( "game", "port", DEFAULT_ORXONOX_PORT );
[7374]495
[7256]496    printf("Starting Orxonox as server: listening on port %i\n", port);
[7374]497
[7256]498    startOrxonox(argc, argv, "", port);
499  }
[3648]500}
501
[3649]502
[4766]503
504/**
505 * starts orxonox
506 * @param argc parameters count given to orxonox
507 * @param argv parameters given to orxonox
508 */
[7256]509int startOrxonox(int argc, char** argv, const std::string & name, int port)
[3648]510{
[4830]511  // checking for existence of the configuration-files, or if the lock file is still used
[7661]512  if (showGui || (!File("./orxonox.conf").isFile() &&
[7714]513                  !File(DEFAULT_CONFIG_FILE).isFile())
[7729]514#if DEBUG_LEVEL <= 3 // developers do not need to see the GUI, when orxonox fails
[5996]515      || ResourceManager::isFile(DEFAULT_LOCK_FILE)
[4981]516#endif
517     )
[5996]518  {
[7661]519    File lockFile(DEFAULT_LOCK_FILE);
520    if (lockFile.isFile())
521      lockFile.remove();
[4556]522
[5996]523    // starting the GUI
[7661]524    OrxGui::QtGui gui(argc, argv);
525    gui.startGui();
[4132]526
[7661]527    if (gui.getState() & OrxGui::Gui::Quitting)
[5996]528      return 0;
[4556]529
[5996]530  }
[4556]531
[4032]532  PRINT(0)(">>> Starting Orxonox <<<\n");
[4033]533
[7661]534  File(DEFAULT_LOCK_FILE).touch();
[4033]535
[1850]536  Orxonox *orx = Orxonox::getInstance();
[4556]537
[5996]538  if( orx->init(argc, argv, name, port) == -1)
539  {
540    PRINTF(1)("! Orxonox initialization failed\n");
541    return -1;
542  }
[4556]543
[5996]544  printf("finished inizialisation\n");
[2636]545  orx->start();
[4556]546
[3676]547  delete orx;
[7661]548  File("~/.orxonox/orxonox.lock").remove();
[1803]549}
Note: See TracBrowser for help on using the repository browser.