Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7927 was 7729, checked in by bensch, 19 years ago

orxonox/trunk: better debug names
As DEBUG and ERROR are already given to windows.h and other files:
DEBUG is renamed to DEBUG_LEVEL
and all
NO, ERR, WARN, INFO, DEBUG, vDEBUG
are renamed to
ORX_NONE, ORX_ERR, ORX_WARN, ORX_INFO, ORX_DEBUG, ORX_vDEBUG

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