Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/shared_lib/src/orxonox.cc @ 7190

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

windows sucks

File size: 13.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"
[5165]35#include "game_loader.h"
[4786]36
37//ENGINES
[3610]38#include "graphics_engine.h"
[4504]39#include "sound_engine.h"
[3655]40#include "resource_manager.h"
[4786]41#include "cd_engine.h"
[3790]42#include "text_engine.h"
[4786]43#include "event_handler.h"
44
[4010]45#include "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
[5546]55#include "load_param_description.h"
[7187]56#include "dynamic_loader.h"
[5226]57
[5996]58#include "network_manager.h"
59
[6695]60#include "state.h"
61
[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
[4766]80  this->iniParser = NULL;
[4135]81
82  this->argc = 0;
83  this->argv = NULL;
[4782]84
[5996]85  /* this way, there is no network enabled: */
86  this->serverName = NULL;
87  this->port = -1;
88
[4830]89  this->configFileName = NULL;
[1872]90}
[1803]91
[2190]92/**
[4836]93 *  remove Orxonox from memory
[2190]94*/
[4556]95Orxonox::~Orxonox ()
[2190]96{
[5285]97  // game-specific
[4815]98  delete GameLoader::getInstance();
[5285]99
100  // class-less services/factories
[5982]101  Factory::deleteFactories();
[4980]102  FastFactory::deleteAll();
[5171]103  ShellCommandClass::unregisterAllCommands();
[5332]104
[5226]105  LoadClassDescription::deleteAllDescriptions();
106
[5285]107  // engines
108  delete CDEngine::getInstance();
109  delete SoundEngine::getInstance();
110  delete GraphicsEngine::getInstance(); // deleting the Graphics
[4817]111  delete EventHandler::getInstance();
[5285]112
113  // handlers
114  delete ResourceManager::getInstance(); // deletes the Resource Manager
115  // output-buffer
116  delete ShellBuffer::getInstance();
117
118  // orxonox class-stuff
[5078]119  delete this->iniParser;
[5210]120  delete[] this->configFileName;
[4817]121
[5225]122  SDL_QuitSubSystem(SDL_INIT_TIMER);
[7126]123  ClassList::debug();
[1850]124
[4833]125  PRINT(3)
[5996]126  (
127    "===================================================\n" \
128    "Thanks for playing orxonox.\n" \
129    "visit: http://www.orxonox.net for new versions.\n" \
130    "===================================================\n" \
131    ORXONOX_LICENSE_SHORT
132  );
[1872]133
[4766]134  Orxonox::singletonRef = NULL;
[1850]135}
136
[2190]137/**
[4836]138 *  this is a singleton class to prevent duplicates
[4766]139 */
140Orxonox* Orxonox::singletonRef = NULL;
[4556]141
[5207]142// DANGEROUS
143void Orxonox::restart()
144{
[5996]145  //   int argc = this->argc;
146  //   char** argv = this->argv;
147  //
148  //   Orxonox *orx = Orxonox::getInstance();
149  //
150  //   delete orx;
151  //
152  //   orx = Orxonox::getInstance();
153  //
154  //   if((*orx).init(argc, argv) == -1)
155  //   {
156  //     PRINTF(1)("! Orxonox initialization failed\n");
157  //     return;
158  //   }
159  //
160  //   printf("finished inizialisation\n");
161  //   orx->start();
[5207]162}
163
[4766]164/**
[4836]165 *  this finds the config file
[4766]166 * @returns the new config-fileName
167 * Since the config file varies from user to user and since one may want to specify different config files
168 * for certain occasions or platforms this function finds the right config file for every occasion and stores
169 * it's path and name into configfilename
[2190]170*/
[4830]171const char* Orxonox::getConfigFile ()
[1850]172{
[5424]173  if (ResourceManager::isFile("orxonox.conf"))
174  {
175    this->configFileName = new char[strlen("orxonox.conf")+1];
176    strcpy(this->configFileName, "orxonox.conf");
177  }
178  else
179    this->configFileName = ResourceManager::homeDirCheck(DEFAULT_CONFIG_FILE);
[4766]180  this->iniParser = new IniParser(this->configFileName);
[5424]181  PRINTF(3)("Parsed Config File: '%s'\n", this->configFileName);
[1803]182}
183
[2190]184/**
[4833]185 * initialize Orxonox with command line
186 */
[5996]187int Orxonox::init (int argc, char** argv, const char* 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
[4784]232  GraphicsEngine::getInstance()->initFromIniFile(this->iniParser);
[4766]233
[5219]234  char* iconName = ResourceManager::getFullName("pictures/fighter-top-32x32.bmp");
[5225]235  if (iconName != NULL)
236  {
237    GraphicsEngine::getInstance()->setWindowName(PACKAGE_NAME " " PACKAGE_VERSION, iconName);
238    delete[] iconName;
239  }
[2190]240  return 0;
241}
[1850]242
[5996]243
[2190]244/**
[4833]245 * initializes the sound engine
246 */
[4556]247int Orxonox::initSound()
[2190]248{
[4504]249  PRINT(3)("> Initializing sound\n");
[5225]250  // SDL_InitSubSystem(SDL_INIT_AUDIO);
[6840]251  SoundEngine::getInstance();
[4985]252
253  SoundEngine::getInstance()->loadSettings(this->iniParser);
[6840]254  SoundEngine::getInstance()->initAudio();
[2636]255  return 0;
[2190]256}
[1900]257
[3214]258
[2190]259/**
[4833]260 * initializes input functions
261 */
[4556]262int Orxonox::initInput()
[2190]263{
[4766]264  PRINT(3)("> Initializing input\n");
265
[4866]266  EventHandler::getInstance()->init(this->iniParser);
[4833]267  EventHandler::getInstance()->subscribe(GraphicsEngine::getInstance(), ES_ALL, EV_VIDEO_RESIZE);
[4556]268
[2636]269  return 0;
[1803]270}
271
[3214]272
[2190]273/**
[4833]274 * initializes network system
275 */
[4556]276int Orxonox::initNetworking()
[1897]277{
[4766]278  PRINT(3)("> Initializing networking\n");
279
[5996]280  if( this->serverName != NULL) // we are a client
[6695]281  {
282    State::setOnline(true);
[5996]283    NetworkManager::getInstance()->establishConnection(this->serverName, port);
[6695]284  }
[7177]285  else if( this->port > 0)
286  {    // we are a server
[6695]287    State::setOnline(true);
[5996]288    NetworkManager::getInstance()->createServer(port);
[6139]289  }
[2636]290  return 0;
[1897]291}
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
[5014]303  const char* dataPath;
304  if ((dataPath = this->iniParser->getVar(CONFIG_NAME_DATADIR, CONFIG_SECTION_DATA))!= NULL)
[4766]305  {
[5480]306    if (!ResourceManager::getInstance()->setDataDir(dataPath) &&
[5996]307        !ResourceManager::getInstance()->verifyDataDir(DEFAULT_DATA_DIR_CHECKFILE))
[4766]308    {
[5423]309      PRINTF(1)("Data Could not be located in %s\n", dataPath);
[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",
[4822]317              ResourceManager::getInstance()->getDataDir(),
[5424]318              this->configFileName,
[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
[5335]327  char* imageDir = ResourceManager::getInstance()->getFullName("maps");
[5216]328  ResourceManager::getInstance()->addImageDir(imageDir);
329  delete[] imageDir;
[7067]330  imageDir = ResourceManager::getInstance()->getFullName("pictures");
331  ResourceManager::getInstance()->addImageDir(imageDir);
332  delete[] imageDir;
[4009]333
[7176]334  if (!DynamicLoader::addSearchDirRelative("./world_entities"))
335    if (!DynamicLoader::addSearchDirRelative("./src/world_entities"))
336      DynamicLoader::addSearchDir(PKG_LIBDIR);
337
[7177]338  /// FIXME make this more modular (search for himself for all the libraries
[7176]339  printf("%s\n", DynamicLoader::getSearchDir());
[7190]340  DynamicLoader::loadDyLib("libORXplayables.la");
341  DynamicLoader::loadDyLib("libORXweapons.la");
342  DynamicLoader::loadDyLib("libORXprojectiles.la");
343  DynamicLoader::loadDyLib("libORXpower_ups.la");
[7181]344
[7190]345  DynamicLoader::loadDyLib("libORXenvironmentals.la");
[7181]346
347  DynamicLoader::loadDyLib("libORXnpcs");
348
[7177]349  DynamicLoader::loadDyLib("libORXmisc_entities");
350  printf("%s\n", DynamicLoader::getSearchDir());
351
[5488]352  // start the collision detection engine
[4766]353  CDEngine::getInstance();
[5074]354  return 0;
355}
356
357/**
358 * initializes miscelaneous features
359 * @return -1 on failure
360 */
361int Orxonox::initMisc()
362{
[5183]363  ShellBuffer::getInstance();
[4766]364  return 0;
[1858]365}
[1849]366
[2190]367/**
[4836]368 *  starts the orxonox game or menu
[4833]369 * here is the central orxonox state manager. There are currently two states
370 * - menu
371 * - game-play
372 * both states manage their states themselfs again.
[2190]373*/
[2636]374void Orxonox::start()
375{
[4556]376
[2636]377  this->gameLoader = GameLoader::getInstance();
[5996]378
[6139]379  if( this->port != -1)
380    this->gameLoader->loadNetworkCampaign("worlds/DefaultNetworkCampaign.oxc");
[5996]381  else
[6139]382    this->gameLoader->loadCampaign("worlds/DefaultCampaign.oxc");                       /* start orxonox in single player mode */
[5996]383
[4010]384  //  this->gameLoader->loadDebugCampaign(DEBUG_CAMPAIGN_0);
[2636]385  this->gameLoader->init();
386  this->gameLoader->start();
387}
388
[3214]389
[2636]390/**
[4833]391 * handles sprecial events from localinput
392 * @param event: an event not handled by the CommandNode
393 */
[4817]394// void Orxonox::graphicsHandler(SDL_Event* event)
395// {
396//   // Handle special events such as reshape, quit, focus changes
397//   switch (event->type)
398//     {
399//     case SDL_VIDEORESIZE:
400//       GraphicsEngine* tmpGEngine = GraphicsEngine::getInstance();
401//       tmpGEngine->resolutionChanged(event->resize);
402//       break;
403//     }
404// }
[1875]405
[4556]406
[4408]407
[1803]408
[3214]409
[4782]410
[4059]411bool showGui = false;
[3648]412
[4766]413
414
415/**********************************
416*** ORXONOX MAIN STARTING POINT ***
417**********************************/
[3449]418/**
[4833]419 *
[4836]420 *  main function
[4833]421 *
422 * here the journey begins
[3449]423*/
[4556]424int main(int argc, char** argv)
425{
[3648]426  int i;
[4032]427  for(i = 1; i < argc; ++i)
[5996]428  {
429    if(     !strcmp( "--help", argv[i])     || !strcmp("-h", argv[i]))
430      return showHelp(argc, argv);
431    else if(!strcmp( "--gui", argv[i])      || !strcmp("-g", argv[i]))
432      showGui = true;
433    else if(!strcmp( "--client", argv[i])   || !strcmp("-c", argv[i]))
434      return startNetworkOrxonox(argc, argv);
435    else if(!strcmp( "--server", argv[i])   || !strcmp("-s", argv[i]))
436      return startNetworkOrxonox(argc, argv);
[6424]437    else if(!strcmp( "--license", argv[i])  || !strcmp("-l", argv[i]))
[5996]438      return PRINT(0)(ORXONOX_LICENSE_SHORT);
439  }
[3648]440
[5996]441  return startOrxonox(argc, argv, NULL, -1);
[3648]442}
443
444
445
[5996]446int showHelp(int argc, char** argv)
[3648]447{
[5996]448  PRINT(0)("Orxonox Version %s\n", PACKAGE_VERSION);
449  PRINT(0)(" Starts Orxonox - The most furious 3D Action Game :)\n");
450  PRINT(0)("\n");
451  PRINT(0)("Common options:\n");
452  PRINT(0)(" -g, --gui                        starts the orxonox with the configuration GUI \n");
453  PRINT(0)(" -h, --help                       shows this help\n");
454  PRINT(0)("\n");
455  PRINT(0)("Network options:\n");
456  PRINT(0)(" -s, --server [port]              starts Orxonox and listens on the [port] for players\n");
457  PRINT(0)(" -c, --client [hostname] [port]   starts Orxonox as a Client\n");
458  PRINT(0)(" -c, --client [ip address] [port] starts Orxonox as a Client\n");
459  PRINT(0)("\n");
460  PRINT(0)("Other options:\n");
461  PRINT(0)("     --license     prints the licence and exit\n\n");
462  PRINT(0)("\n");
463
464  //   {
465  //     Gui* gui = new Gui(argc, argv);
466  //     gui->printHelp();
467  //     delete gui;
468  //   }
469}
470
471
472
473
474/**
475 * starts orxonox in network mode
476 * @param argc parameters count given to orxonox
477 * @param argv parameters given to orxonox
478 */
479int startNetworkOrxonox(int argc, char** argv)
480{
481
482  int i;
483  for(i = 0; i < argc; ++i )
[4132]484  {
[5996]485    if( !strcmp( "--client", argv[i]) || !strcmp("-c", argv[i]))
486    {
487      if( argc <= (i+2))
488      {
489        printf(" Wrong arguments try following notations:\n");
490        printf("   --client [server ip address] [port number]\n");
491        printf("   --client [dns name] [port number]\n");
492        return 0;
493      }
494
495      const char* name = argv[i+1];
496      int port = atoi(argv[i+2]);
497      printf("Starting Orxonox as client: connecting to %s, on port %i\n", name, port);
498
499      startOrxonox(argc, argv, name, port);
500    }
501    else if( !strcmp( "--server", argv[i]) || !strcmp("-s", argv[i]))
502    {
503      if( argc <= (i+1))
504      {
505        printf(" Wrong arguments try following notations:\n");
506        printf("   --server [port number]\n");
507        return 0;
508      }
509
510      int port = atoi(argv[i+1]);
511      printf("Starting Orxonox as server, listening on port %i\n", port);
512
513      startOrxonox(argc, argv, NULL, port);
514    }
[4132]515  }
[3648]516}
517
[3649]518
[4766]519
520/**
521 * starts orxonox
522 * @param argc parameters count given to orxonox
523 * @param argv parameters given to orxonox
524 */
[5996]525int startOrxonox(int argc, char** argv, const char* name, int port)
[3648]526{
[4830]527  // checking for existence of the configuration-files, or if the lock file is still used
[5424]528  if (showGui || (!ResourceManager::isFile("./orxonox.conf") &&
[5996]529                  !ResourceManager::isFile(DEFAULT_CONFIG_FILE))
[5424]530#if DEBUG < 3 // developers do not need to see the GUI, when orxonox fails
[5996]531      || ResourceManager::isFile(DEFAULT_LOCK_FILE)
[4981]532#endif
533     )
[5996]534  {
535    if (ResourceManager::isFile(DEFAULT_LOCK_FILE))
536      ResourceManager::deleteFile(DEFAULT_LOCK_FILE);
[4556]537
[5996]538    // starting the GUI
539    Gui* gui = new Gui(argc, argv);
540    gui->startGui();
[4132]541
[5996]542    if (! gui->startOrxonox)
543      return 0;
[4556]544
[5996]545    delete gui;
546  }
[4556]547
[4032]548  PRINT(0)(">>> Starting Orxonox <<<\n");
[4033]549
[4766]550  ResourceManager::touchFile(DEFAULT_LOCK_FILE);
[4033]551
[1850]552  Orxonox *orx = Orxonox::getInstance();
[4556]553
[5996]554  if( orx->init(argc, argv, name, port) == -1)
555  {
556    PRINTF(1)("! Orxonox initialization failed\n");
557    return -1;
558  }
[4556]559
[5996]560  printf("finished inizialisation\n");
[2636]561  orx->start();
[4556]562
[3676]563  delete orx;
[4033]564  ResourceManager::deleteFile("~/.orxonox/orxonox.lock");
[1803]565}
Note: See TracBrowser for help on using the repository browser.