Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/orxonox.cc @ 9715

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

renamed newclassid to classid and newobjectlist to objectlist

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