Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/std/src/orxonox.cc @ 7203

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

orxonox/trunk: compiles again, BUT well…. i do not expect it to run anymore

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