Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

removed some more iniParsers

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