Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/terrain/src/orxonox.cc @ 9336

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

merged back

File size: 14.9 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"
[5819]29#include "globals.h"
30
[8145]31#include "gui/qt/qt_gui.h"
[8749]32#include "gui/qt/qt_gui_datadir_fallback.h"
[4054]33
[5944]34#include "parser/ini_parser/ini_parser.h"
[7193]35#include "util/loading/game_loader.h"
[7440]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
[7711]65int verbose = 5;
[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
[7440]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
[7954]93REGISTER_ARG_ARG(  t, telnetport,  "network","telnetport",        "Port to use for network debug output",               "port" );
94REGISTER_ARG_ARG(  _, write_dict,  "compression", "writedict",    "write packets to DATA/dicts/newdict",               "numBytes" );
95
96
[2190]97/**
[4836]98 *  create a new Orxonox
[4135]99
100   In this funcitons only global values are set. The game will not be started here.
[2190]101*/
102Orxonox::Orxonox ()
[1872]103{
[4445]104  this->setClassID(CL_ORXONOX, "Orxonox");
[4766]105  this->setName("orxonox-main");
[4059]106
[4135]107  this->argc = 0;
108  this->argv = NULL;
[4782]109
[5996]110  /* this way, there is no network enabled: */
[7256]111  this->serverName = "";
[5996]112  this->port = -1;
113
[7221]114  this->configFileName = "";
[1872]115}
[1803]116
[2190]117/**
[4836]118 *  remove Orxonox from memory
[2190]119*/
[4556]120Orxonox::~Orxonox ()
[2190]121{
[5285]122  // game-specific
[4815]123  delete GameLoader::getInstance();
[5285]124
125  // class-less services/factories
[5982]126  Factory::deleteFactories();
[4980]127  FastFactory::deleteAll();
[7374]128  OrxShell::ShellCommandClass::unregisterAllCommands();
[5332]129
[5226]130  LoadClassDescription::deleteAllDescriptions();
131
[7427]132  // handlers
133  delete ResourceManager::getInstance(); // deletes the Resource Manager
134
[5285]135  // engines
136  delete CDEngine::getInstance();
[7460]137  delete OrxSound::SoundEngine::getInstance();
[5285]138  delete GraphicsEngine::getInstance(); // deleting the Graphics
[4817]139  delete EventHandler::getInstance();
[5285]140
141  // output-buffer
[7374]142  delete OrxShell::ShellBuffer::getInstance();
[5285]143
[5225]144  SDL_QuitSubSystem(SDL_INIT_TIMER);
[7126]145  ClassList::debug();
[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);
[8749]229  if( initVideo() == -1)
230    return -1;
[5996]231  if( initResources () == -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
[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");
301
[7256]302  if( this->serverName != "") // we are a client
[6695]303  {
304    State::setOnline(true);
[5996]305    NetworkManager::getInstance()->establishConnection(this->serverName, port);
[6695]306  }
[7714]307  else if( this->port > 0)
308  {    // we are a server
[6695]309    State::setOnline(true);
[5996]310    NetworkManager::getInstance()->createServer(port);
[6139]311  }
[2636]312  return 0;
[1897]313}
314
[7355]315//#include "util/loading/dynamic_loader.h"
[3214]316
[2190]317/**
[4833]318 * initializes and loads resource files
[4766]319 */
[4833]320int Orxonox::initResources()
[1858]321{
[4766]322  PRINTF(3)("> Initializing resources\n");
[4091]323
[4766]324  PRINT(3)("initializing ResourceManager\n");
325
[5488]326  // init the resource manager
[7221]327  std::string dataPath;
[7661]328  if ((dataPath = Preferences::getInstance()->getString(CONFIG_SECTION_GENERAL, CONFIG_NAME_DATADIR, ""))!= "")
[4766]329  {
[5480]330    if (!ResourceManager::getInstance()->setDataDir(dataPath) &&
[5996]331        !ResourceManager::getInstance()->verifyDataDir(DEFAULT_DATA_DIR_CHECKFILE))
[4766]332    {
[7221]333      PRINTF(1)("Data Could not be located in %s\n", dataPath.c_str());
[4766]334    }
335  }
[4556]336
[8749]337
338  while (!ResourceManager::getInstance()->verifyDataDir(DEFAULT_DATA_DIR_CHECKFILE))
[4766]339  {
[8749]340
[5510]341    PRINTF(1)("The DataDirectory %s could not be verified\n\nh" \
342              "!!!  Please Change in File %s Section %s Entry %s to a suitable value !!!\n",
[7714]343              ResourceManager::getInstance()->getDataDir().c_str(),
344              this->configFileName.c_str(),
[7661]345              CONFIG_SECTION_GENERAL,
[5510]346              CONFIG_NAME_DATADIR );
[8749]347    OrxGui::Gui* gui = new OrxGui::QtGuiDataDirFallback(argc, argv);
[5479]348    gui->startGui();
[8750]349    if (gui->getState() == OrxGui::Gui::Quitting)
350      return(-1);
[5479]351    delete gui;
[8749]352    ResourceManager::getInstance()->setDataDir(Preferences::getInstance()->getString(CONFIG_SECTION_GENERAL, CONFIG_NAME_DATADIR, ""));
353
[4766]354  }
[8749]355
356
[5996]357  //! @todo this is a hack and should be loadable
[7221]358  std::string imageDir = ResourceManager::getInstance()->getFullName("maps");
[5216]359  ResourceManager::getInstance()->addImageDir(imageDir);
[7067]360  imageDir = ResourceManager::getInstance()->getFullName("pictures");
361  ResourceManager::getInstance()->addImageDir(imageDir);
[4009]362
[7355]363  //  DynamicLoader::loadDyLib("libtest.so");
[5074]364  return 0;
365}
366
367/**
368 * initializes miscelaneous features
369 * @return -1 on failure
370 */
371int Orxonox::initMisc()
372{
[7374]373  OrxShell::ShellBuffer::getInstance();
[8749]374
375  // start the collision detection engine
376  CDEngine::getInstance();
377
[4766]378  return 0;
[1858]379}
[1849]380
[2190]381/**
[4836]382 *  starts the orxonox game or menu
[4833]383 * here is the central orxonox state manager. There are currently two states
384 * - menu
385 * - game-play
386 * both states manage their states themselfs again.
[2190]387*/
[2636]388void Orxonox::start()
389{
[4556]390
[2636]391  this->gameLoader = GameLoader::getInstance();
[5996]392
[6139]393  if( this->port != -1)
394    this->gameLoader->loadNetworkCampaign("worlds/DefaultNetworkCampaign.oxc");
[5996]395  else
[6139]396    this->gameLoader->loadCampaign("worlds/DefaultCampaign.oxc");                       /* start orxonox in single player mode */
[5996]397
[4010]398  //  this->gameLoader->loadDebugCampaign(DEBUG_CAMPAIGN_0);
[2636]399  this->gameLoader->init();
400  this->gameLoader->start();
401}
402
[3214]403
[2636]404/**
[4833]405 * handles sprecial events from localinput
406 * @param event: an event not handled by the CommandNode
407 */
[4817]408// void Orxonox::graphicsHandler(SDL_Event* event)
409// {
410//   // Handle special events such as reshape, quit, focus changes
411//   switch (event->type)
412//     {
413//     case SDL_VIDEORESIZE:
414//       GraphicsEngine* tmpGEngine = GraphicsEngine::getInstance();
415//       tmpGEngine->resolutionChanged(event->resize);
416//       break;
417//     }
418// }
[1875]419
[4556]420
[4408]421
[1803]422
[3214]423
[4782]424
[4059]425bool showGui = false;
[3648]426
[4766]427/**********************************
428*** ORXONOX MAIN STARTING POINT ***
429**********************************/
[3449]430/**
[4833]431 *
[4836]432 *  main function
[4833]433 *
434 * here the journey begins
[3449]435*/
[4556]436int main(int argc, char** argv)
437{
[7256]438  CmdLinePrefsReader prefs;
[7374]439
[7661]440  IniFilePrefsReader ini(File(DEFAULT_CONFIG_FILE).name());
441  Preferences::getInstance()->setUserIni(File(DEFAULT_CONFIG_FILE).name());
[7374]442
[7256]443  prefs.parse(argc, argv);
[7374]444
[7256]445  if ( Preferences::getInstance()->getString("misc", "showLicenseAndExit", "") == "1" )
[5996]446  {
[7256]447    PRINT(0)(ORXONOX_LICENSE_SHORT);
448    return 0;
[5996]449  }
[7374]450
[7440]451  if ( Preferences::getInstance()->getString("misc", "start-gdb", "0") == "1" )
452  {
453    SignalHandler::getInstance()->doCatch( argv[0], GDB_RUN_IN_FOREGROUND );
[8623]454    SignalHandler::getInstance()->registerCallback(EventHandler::releaseMouse, NULL);
[7440]455  }
456  else if ( Preferences::getInstance()->getString("misc", "bt-to-file", "1") == "1" )
[7714]457  {
458    SignalHandler::getInstance()->doCatch( argv[0], GDB_RUN_WRITE_TO_FILE );
[7460]459
[7440]460  }
[7460]461
[7256]462  if( Preferences::getInstance()->getString("game", "showGui", "") == "1" )
463    showGui = true;
464  else if( Preferences::getInstance()->getString( "game", "gameType", "" ) == "multiplayer_server" ||
465           Preferences::getInstance()->getString( "game", "gameType", "" ) == "multiplayer_client" )
466    return startNetworkOrxonox(argc, argv);
[7374]467
[7256]468  return startOrxonox(argc, argv, "", -1);
[3648]469}
470
471
472
[5996]473/**
474 * starts orxonox in network mode
475 * @param argc parameters count given to orxonox
476 * @param argv parameters given to orxonox
477 */
478int startNetworkOrxonox(int argc, char** argv)
479{
480
[7256]481  std::string gameType = Preferences::getInstance()->getString( "game", "gameType", "" );
[7374]482
[7256]483  if ( gameType == "multiplayer_client" )
[4132]484  {
[7256]485    int port = Preferences::getInstance()->getInt( "game", "port", DEFAULT_ORXONOX_PORT );
486    std::string host = Preferences::getInstance()->getString( "game", "host", "" );
[7374]487
[7256]488    if ( host == "" )
[5996]489    {
[7256]490      printf("You need to specify a host to connect to ( -H <host> )\n");
491      return 1;
[5996]492    }
[7374]493
[7256]494    printf("Starting Orxonox as client: connecting to %s, on port %i\n", host.c_str(), port);
[7374]495
[7256]496    startOrxonox(argc, argv, host.c_str(), port);
[4132]497  }
[7256]498  else if ( gameType == "multiplayer_server" )
499  {
500    int port = Preferences::getInstance()->getInt( "game", "port", DEFAULT_ORXONOX_PORT );
[7374]501
[7256]502    printf("Starting Orxonox as server: listening on port %i\n", port);
[7374]503
[7256]504    startOrxonox(argc, argv, "", port);
505  }
[8316]506  return 1;
[3648]507}
508
[3649]509
[4766]510
511/**
512 * starts orxonox
513 * @param argc parameters count given to orxonox
514 * @param argv parameters given to orxonox
515 */
[7256]516int startOrxonox(int argc, char** argv, const std::string & name, int port)
[3648]517{
[4830]518  // checking for existence of the configuration-files, or if the lock file is still used
[7661]519  if (showGui || (!File("./orxonox.conf").isFile() &&
[9140]520                  !File(DEFAULT_CONFIG_FILE).isFile() ) )
[5996]521  {
[7661]522    File lockFile(DEFAULT_LOCK_FILE);
523    if (lockFile.isFile())
524      lockFile.remove();
[4556]525
[5996]526    // starting the GUI
[7661]527    OrxGui::QtGui gui(argc, argv);
528    gui.startGui();
[4132]529
[7661]530    if (gui.getState() & OrxGui::Gui::Quitting)
[5996]531      return 0;
[4556]532
[5996]533  }
[4556]534
[4032]535  PRINT(0)(">>> Starting Orxonox <<<\n");
[4033]536
[7661]537  File(DEFAULT_LOCK_FILE).touch();
[4033]538
[1850]539  Orxonox *orx = Orxonox::getInstance();
[4556]540
[5996]541  if( orx->init(argc, argv, name, port) == -1)
542  {
543    PRINTF(1)("! Orxonox initialization failed\n");
544    return -1;
545  }
[4556]546
[5996]547  printf("finished inizialisation\n");
[2636]548  orx->start();
[4556]549
[3676]550  delete orx;
[7661]551  File("~/.orxonox/orxonox.lock").remove();
[8363]552  return 0;
[1803]553}
Note: See TracBrowser for help on using the repository browser.