Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/new_class_id: resourceManager splitted away

File size: 15.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"
[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
[9709]47#include "loading/fast_factory.h"
[4980]48
[4131]49#include "benchmark.h"
[3610]50
[5641]51#include "shell_command_class.h"
[5165]52#include "shell_command.h"
[5175]53#include "shell_buffer.h"
[4748]54
[5996]55#include "network_manager.h"
[9406]56#include "shared_network_data.h"
[5996]57
[6695]58#include "state.h"
[7256]59#include "lib/parser/preferences/cmd_line_prefs_reader.h"
60#include "lib/parser/preferences/ini_file_prefs_reader.h"
[2190]61#include <string.h>
[4032]62
[7711]63int verbose = 5;
[2036]64
[1803]65
[9406]66
[5207]67SHELL_COMMAND(restart, Orxonox, restart);
68
[7258]69REGISTER_ARG_FLAG( l, license,    "misc",  "showLicenseAndExit", "Prints the license and exit",      "1" );
70REGISTER_ARG_FLAG( c, client,     "game",  "gameType",           "Connect to Server (-H)",           "multiplayer_client" );
[9406]71REGISTER_ARG_FLAG( s, server,     "game",  "gameType",           "Start Orxonox as Game Server",     "multiplayer_master_server" );
72REGISTER_ARG_FLAG( x, proxy,      "game",  "gameType",           "Start Orxonox as Proxy Server",    "multiplayer_proxy_server" );
[7258]73REGISTER_ARG_ARG(  H, host,       "game",  "host",               "Host to connect to",               "host");
74REGISTER_ARG_ARG(  p, port,       "game",  "port",               "Port to use",                      "port" );
75REGISTER_ARG_FLAG( g, gui,        "game",  "showGui",            "starts the orxonox with the configuration GUI", "1");
[7256]76
[7258]77REGISTER_ARG_FLAG( f, fullscreen, "video", "Fullscreen-mode",    "start Orxonox in fullscreen mode", "1");
78REGISTER_ARG_FLAG( w, windowed,   "video", "Fullscreen-mode",    "start Orxonox in windowed mode",   "0");
[9406]79REGISTER_ARG_ARG(  r, resolution, "video", "Resolution",         "sets resolution / window size",    "res");
80REGISTER_ARG_FLAG( d, dedicated,  "video", "Norender-mode",      "the scene is not rendered",        "1" );
[7258]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( _, write_bt_to_file, "misc", "bt-to-file", "Write backtrace to file", "1");
90#endif
91
[7954]92REGISTER_ARG_ARG(  t, telnetport,  "network","telnetport",        "Port to use for network debug output",               "port" );
93REGISTER_ARG_ARG(  _, write_dict,  "compression", "writedict",    "write packets to DATA/dicts/newdict",               "numBytes" );
94
[9715]95ObjectListDefinition(Orxonox);
[7954]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{
[9709]104  this->registerObject(this, Orxonox::_objectList);
[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
[4980]126  FastFactory::deleteAll();
[7374]127  OrxShell::ShellCommandClass::unregisterAllCommands();
[5332]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);
[9406]142
[9709]143
[9715]144  ObjectListBase::debugAll(1);
[9709]145
[9235]146  Preferences::getInstance()->save();
[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());
[8316]205  return this->configFileName;
[1803]206}
207
[2190]208/**
[4833]209 * initialize Orxonox with command line
210 */
[7256]211int Orxonox::init (int argc, char** argv, const std::string & name, int port)
[1803]212{
[4135]213  this->argc = argc;
214  this->argv = argv;
[4556]215
[5996]216  this->serverName = name;
217  this->port = port;
218
[4766]219  // initialize the Config-file
[4830]220  this->getConfigFile();
[4766]221
[5788]222  // windows must not write into stdout.txt and stderr.txt
[6833]223  /*#ifdef __WIN32__
[5788]224  freopen( "CON", "w", stdout );
225  freopen( "CON", "w", stderr );
[6833]226  #endif*/
[5788]227
[5996]228  // initialize everything
[6079]229  SDL_Init(0);
[8749]230  if( initVideo() == -1)
231    return -1;
[5996]232  if( initResources () == -1)
233    return -1;
234  if( initSound() == -1)
235    return -1;
236  if( initInput() == -1)
237    return -1;
238  if( initNetworking () == -1)
239    return -1;
240  if( initMisc () == -1)
241    return -1;
[4556]242
[2636]243  return 0;
[1850]244}
[1849]245
[5996]246
[2190]247/**
[4833]248 * initializes SDL and OpenGL
[5996]249 */
[4556]250int Orxonox::initVideo()
[2190]251{
[3611]252  PRINTF(3)("> Initializing video\n");
[4556]253
[3610]254  GraphicsEngine::getInstance();
[4556]255
[7256]256  GraphicsEngine::getInstance()->initFromPreferences();
[4766]257
[7221]258  std::string iconName = ResourceManager::getFullName("pictures/fighter-top-32x32.bmp");
259  if (!iconName.empty())
[5225]260  {
261    GraphicsEngine::getInstance()->setWindowName(PACKAGE_NAME " " PACKAGE_VERSION, iconName);
262  }
[2190]263  return 0;
264}
[1850]265
[5996]266
[2190]267/**
[4833]268 * initializes the sound engine
269 */
[4556]270int Orxonox::initSound()
[2190]271{
[4504]272  PRINT(3)("> Initializing sound\n");
[5225]273  // SDL_InitSubSystem(SDL_INIT_AUDIO);
[7460]274  OrxSound::SoundEngine::getInstance();
[4985]275
[7460]276  OrxSound::SoundEngine::getInstance()->loadSettings();
277  OrxSound::SoundEngine::getInstance()->initAudio();
[2636]278  return 0;
[2190]279}
[1900]280
[3214]281
[2190]282/**
[4833]283 * initializes input functions
284 */
[4556]285int Orxonox::initInput()
[2190]286{
[4766]287  PRINT(3)("> Initializing input\n");
288
[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");
[9406]302  std::string gameType = Preferences::getInstance()->getString( "game", "gameType", "" );
[4766]303
[9406]304  if( gameType == "multiplayer_client")
305  {    // we are a client
[6695]306    State::setOnline(true);
[9406]307    SharedNetworkData::getInstance()->setNodeType(NET_CLIENT);
308    NetworkManager::getInstance()->createClient(this->serverName, port);
[6695]309  }
[9406]310  else if( gameType == "multiplayer_master_server")
311  {    // we are a master server
[6695]312    State::setOnline(true);
[9406]313    SharedNetworkData::getInstance()->setNodeType(NET_MASTER_SERVER);
314
315    NetworkManager::getInstance()->createMasterServer(port);
[6139]316  }
[9406]317  else if( gameType == "multiplayer_proxy_server")
318  {    // we are a proxy server
319    State::setOnline(true);
320    SharedNetworkData::getInstance()->setNodeType(NET_PROXY_SERVER_ACTIVE);
321    NetworkManager::getInstance()->createProxyServer(port);
322  }
323
[2636]324  return 0;
[9406]325
[1897]326}
327
[7355]328//#include "util/loading/dynamic_loader.h"
[9791]329#include "loading/new_resource_manager.h"
[2190]330/**
[4833]331 * initializes and loads resource files
[4766]332 */
[4833]333int Orxonox::initResources()
[1858]334{
[4766]335  PRINTF(3)("> Initializing resources\n");
[4091]336
[4766]337  PRINT(3)("initializing ResourceManager\n");
338
[5488]339  // init the resource manager
[7221]340  std::string dataPath;
[7661]341  if ((dataPath = Preferences::getInstance()->getString(CONFIG_SECTION_GENERAL, CONFIG_NAME_DATADIR, ""))!= "")
[4766]342  {
[5480]343    if (!ResourceManager::getInstance()->setDataDir(dataPath) &&
[5996]344        !ResourceManager::getInstance()->verifyDataDir(DEFAULT_DATA_DIR_CHECKFILE))
[4766]345    {
[7221]346      PRINTF(1)("Data Could not be located in %s\n", dataPath.c_str());
[4766]347    }
348  }
[4556]349
[8749]350
351  while (!ResourceManager::getInstance()->verifyDataDir(DEFAULT_DATA_DIR_CHECKFILE))
[4766]352  {
[8749]353
[5510]354    PRINTF(1)("The DataDirectory %s could not be verified\n\nh" \
355              "!!!  Please Change in File %s Section %s Entry %s to a suitable value !!!\n",
[7714]356              ResourceManager::getInstance()->getDataDir().c_str(),
357              this->configFileName.c_str(),
[7661]358              CONFIG_SECTION_GENERAL,
[5510]359              CONFIG_NAME_DATADIR );
[8749]360    OrxGui::Gui* gui = new OrxGui::QtGuiDataDirFallback(argc, argv);
[5479]361    gui->startGui();
[8750]362    if (gui->getState() == OrxGui::Gui::Quitting)
363      return(-1);
[5479]364    delete gui;
[8749]365    ResourceManager::getInstance()->setDataDir(Preferences::getInstance()->getString(CONFIG_SECTION_GENERAL, CONFIG_NAME_DATADIR, ""));
366
[4766]367  }
[8749]368
[5996]369  //! @todo this is a hack and should be loadable
[9791]370  NewResourceManager::getInstance()->addResourceSubPath("Texture", "maps");
371  NewResourceManager::getInstance()->addResourceSubPath("Texture", "pictures");
[4009]372
[7355]373  //  DynamicLoader::loadDyLib("libtest.so");
[5074]374  return 0;
375}
376
377/**
378 * initializes miscelaneous features
379 * @return -1 on failure
380 */
381int Orxonox::initMisc()
382{
[7374]383  OrxShell::ShellBuffer::getInstance();
[8749]384
385  // start the collision detection engine
386  CDEngine::getInstance();
387
[4766]388  return 0;
[1858]389}
[1849]390
[2190]391/**
[4836]392 *  starts the orxonox game or menu
[4833]393 * here is the central orxonox state manager. There are currently two states
394 * - menu
395 * - game-play
396 * both states manage their states themselfs again.
[2190]397*/
[2636]398void Orxonox::start()
399{
[4556]400
[2636]401  this->gameLoader = GameLoader::getInstance();
[5996]402
[6139]403  if( this->port != -1)
404    this->gameLoader->loadNetworkCampaign("worlds/DefaultNetworkCampaign.oxc");
[5996]405  else
[6139]406    this->gameLoader->loadCampaign("worlds/DefaultCampaign.oxc");                       /* start orxonox in single player mode */
[5996]407
[4010]408  //  this->gameLoader->loadDebugCampaign(DEBUG_CAMPAIGN_0);
[2636]409  this->gameLoader->init();
410  this->gameLoader->start();
411}
412
[3214]413
[2636]414/**
[4833]415 * handles sprecial events from localinput
416 * @param event: an event not handled by the CommandNode
417 */
[4817]418// void Orxonox::graphicsHandler(SDL_Event* event)
419// {
420//   // Handle special events such as reshape, quit, focus changes
421//   switch (event->type)
422//     {
423//     case SDL_VIDEORESIZE:
424//       GraphicsEngine* tmpGEngine = GraphicsEngine::getInstance();
425//       tmpGEngine->resolutionChanged(event->resize);
426//       break;
427//     }
428// }
[1875]429
[4556]430
[4408]431
[1803]432
[3214]433
[4782]434
[4059]435bool showGui = false;
[3648]436
[4766]437/**********************************
438*** ORXONOX MAIN STARTING POINT ***
439**********************************/
[3449]440/**
[4833]441 *
[4836]442 *  main function
[4833]443 *
444 * here the journey begins
[3449]445*/
[4556]446int main(int argc, char** argv)
447{
[7256]448  CmdLinePrefsReader prefs;
[7374]449
[7661]450  IniFilePrefsReader ini(File(DEFAULT_CONFIG_FILE).name());
451  Preferences::getInstance()->setUserIni(File(DEFAULT_CONFIG_FILE).name());
[7374]452
[7256]453  prefs.parse(argc, argv);
[7374]454
[7256]455  if ( Preferences::getInstance()->getString("misc", "showLicenseAndExit", "") == "1" )
[5996]456  {
[7256]457    PRINT(0)(ORXONOX_LICENSE_SHORT);
458    return 0;
[5996]459  }
[7374]460
[9240]461  if ( Preferences::getInstance()->getString("misc", "bt-to-file", "1") == "1" )
[7440]462  {
[9406]463    SignalHandler::getInstance()->doCatch( argv[0], "orxonox.backtrace" );
464    SignalHandler::getInstance()->registerCallback( EventHandler::releaseMouse, NULL );
[7440]465  }
[7460]466
[7256]467  if( Preferences::getInstance()->getString("game", "showGui", "") == "1" )
468    showGui = true;
[9406]469  else if( Preferences::getInstance()->getString( "game", "gameType", "" ) == "multiplayer_master_server" ||
470           Preferences::getInstance()->getString( "game", "gameType", "" ) == "multiplayer_proxy_server" ||
[7256]471           Preferences::getInstance()->getString( "game", "gameType", "" ) == "multiplayer_client" )
472    return startNetworkOrxonox(argc, argv);
[7374]473
[7256]474  return startOrxonox(argc, argv, "", -1);
[3648]475}
476
477
478
[5996]479/**
480 * starts orxonox in network mode
481 * @param argc parameters count given to orxonox
482 * @param argv parameters given to orxonox
483 */
484int startNetworkOrxonox(int argc, char** argv)
485{
486
[7256]487  std::string gameType = Preferences::getInstance()->getString( "game", "gameType", "" );
[7374]488
[7256]489  if ( gameType == "multiplayer_client" )
[4132]490  {
[7256]491    int port = Preferences::getInstance()->getInt( "game", "port", DEFAULT_ORXONOX_PORT );
492    std::string host = Preferences::getInstance()->getString( "game", "host", "" );
[7374]493
[7256]494    if ( host == "" )
[5996]495    {
[7256]496      printf("You need to specify a host to connect to ( -H <host> )\n");
497      return 1;
[5996]498    }
[7374]499
[7256]500    printf("Starting Orxonox as client: connecting to %s, on port %i\n", host.c_str(), port);
[7374]501
[7256]502    startOrxonox(argc, argv, host.c_str(), port);
[4132]503  }
[9406]504  else if ( gameType == "multiplayer_master_server" )
[7256]505  {
506    int port = Preferences::getInstance()->getInt( "game", "port", DEFAULT_ORXONOX_PORT );
[7374]507
[7256]508    printf("Starting Orxonox as server: listening on port %i\n", port);
[7374]509
[7256]510    startOrxonox(argc, argv, "", port);
511  }
[9406]512  else if ( gameType == "multiplayer_proxy_server" )
513  {
514    int port = Preferences::getInstance()->getInt( "game", "port", DEFAULT_ORXONOX_PORT );
515
516    printf("Starting Orxonox as proxy server: listening on port %i\n", port);
517
518    startOrxonox(argc, argv, "", port);
519  }
[8316]520  return 1;
[3648]521}
522
[3649]523
[4766]524
525/**
526 * starts orxonox
527 * @param argc parameters count given to orxonox
528 * @param argv parameters given to orxonox
529 */
[7256]530int startOrxonox(int argc, char** argv, const std::string & name, int port)
[3648]531{
[4830]532  // checking for existence of the configuration-files, or if the lock file is still used
[7661]533  if (showGui || (!File("./orxonox.conf").isFile() &&
[7714]534                  !File(DEFAULT_CONFIG_FILE).isFile())
[7729]535#if DEBUG_LEVEL <= 3 // developers do not need to see the GUI, when orxonox fails
[5996]536      || ResourceManager::isFile(DEFAULT_LOCK_FILE)
[4981]537#endif
538     )
[5996]539  {
[7661]540    File lockFile(DEFAULT_LOCK_FILE);
541    if (lockFile.isFile())
542      lockFile.remove();
[4556]543
[5996]544    // starting the GUI
[7661]545    OrxGui::QtGui gui(argc, argv);
546    gui.startGui();
[4132]547
[7661]548    if (gui.getState() & OrxGui::Gui::Quitting)
[5996]549      return 0;
[4556]550
[5996]551  }
[4556]552
[4032]553  PRINT(0)(">>> Starting Orxonox <<<\n");
[4033]554
[7661]555  File(DEFAULT_LOCK_FILE).touch();
[4033]556
[1850]557  Orxonox *orx = Orxonox::getInstance();
[4556]558
[5996]559  if( orx->init(argc, argv, name, port) == -1)
560  {
561    PRINTF(1)("! Orxonox initialization failed\n");
562    return -1;
563  }
[4556]564
[5996]565  printf("finished inizialisation\n");
[2636]566  orx->start();
[4556]567
[3676]568  delete orx;
[7661]569  File("~/.orxonox/orxonox.lock").remove();
[8363]570  return 0;
[1803]571}
Note: See TracBrowser for help on using the repository browser.