Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/signals/src/orxonox.cc @ 7362

Last change on this file since 7362 was 7361, checked in by rennerc, 19 years ago

added signalhandling class to orx

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