Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/preferences/src/orxonox.cc @ 7255

Last change on this file since 7255 was 7255, checked in by rennerc, 18 years ago

one char* removed

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