Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/orxonox.cc @ 6294

Last change on this file since 6294 was 6142, checked in by bensch, 19 years ago

orxonox/trunk: merge the ObjectManager to the trunk
merged with command:
svn merge -r6082:HEAD objectmanager/ ../trunk/

conflicts resolution was easy this time :)
but specially merged the world to network_world

File size: 13.0 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
[2036]34#include "world.h"
[5944]35#include "parser/ini_parser/ini_parser.h"
[5165]36#include "game_loader.h"
[4786]37
38//ENGINES
[3610]39#include "graphics_engine.h"
[4504]40#include "sound_engine.h"
[3655]41#include "resource_manager.h"
[4786]42#include "cd_engine.h"
[3790]43#include "text_engine.h"
[4786]44#include "event_handler.h"
45
[4010]46#include "factory.h"
[4980]47#include "fast_factory.h"
48
[4131]49#include "benchmark.h"
[3610]50
[4786]51#include "class_list.h"
[5641]52#include "shell_command_class.h"
[5165]53#include "shell_command.h"
[5175]54#include "shell_buffer.h"
[4748]55
[5546]56#include "load_param_description.h"
[5226]57
[5996]58#include "network_manager.h"
59
[2190]60#include <string.h>
[4032]61
[4885]62int verbose = 4;
[2036]63
[1803]64using namespace std;
65
[5207]66SHELL_COMMAND(restart, Orxonox, restart);
67
[2190]68/**
[4836]69 *  create a new Orxonox
[4135]70
71   In this funcitons only global values are set. The game will not be started here.
[2190]72*/
73Orxonox::Orxonox ()
[1872]74{
[4445]75  this->setClassID(CL_ORXONOX, "Orxonox");
[4766]76  this->setName("orxonox-main");
[4059]77
[4766]78  this->iniParser = NULL;
[4135]79
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
[4830]87  this->configFileName = NULL;
[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
116  // orxonox class-stuff
[5078]117  delete this->iniParser;
[5210]118  delete[] this->configFileName;
[4817]119
[5225]120  SDL_QuitSubSystem(SDL_INIT_TIMER);
[4942]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/**
[4836]163 *  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*/
[4830]169const char* Orxonox::getConfigFile ()
[1850]170{
[5424]171  if (ResourceManager::isFile("orxonox.conf"))
172  {
173    this->configFileName = new char[strlen("orxonox.conf")+1];
174    strcpy(this->configFileName, "orxonox.conf");
175  }
176  else
177    this->configFileName = ResourceManager::homeDirCheck(DEFAULT_CONFIG_FILE);
[4766]178  this->iniParser = new IniParser(this->configFileName);
[5424]179  PRINTF(3)("Parsed Config File: '%s'\n", this->configFileName);
[1803]180}
181
[2190]182/**
[4833]183 * initialize Orxonox with command line
184 */
[5996]185int Orxonox::init (int argc, char** argv, const char* name, int port)
[1803]186{
[4135]187  this->argc = argc;
188  this->argv = argv;
[4556]189
[5996]190  this->serverName = name;
191  this->port = port;
192
[4766]193  // initialize the Config-file
[4830]194  this->getConfigFile();
[4766]195
[5788]196  // windows must not write into stdout.txt and stderr.txt
197#ifdef __WIN32__
198  freopen( "CON", "w", stdout );
199  freopen( "CON", "w", stderr );
200#endif
201
[5996]202  // initialize everything
[6079]203  SDL_Init(0);
[5996]204  if( initResources () == -1)
205    return -1;
206  if( initVideo() == -1)
207    return -1;
208  if( initSound() == -1)
209    return -1;
210  if( initInput() == -1)
211    return -1;
212  if( initNetworking () == -1)
213    return -1;
214  if( initMisc () == -1)
215    return -1;
[4556]216
[2636]217  return 0;
[1850]218}
[1849]219
[5996]220
[2190]221/**
[4833]222 * initializes SDL and OpenGL
[5996]223 */
[4556]224int Orxonox::initVideo()
[2190]225{
[3611]226  PRINTF(3)("> Initializing video\n");
[4556]227
[3610]228  GraphicsEngine::getInstance();
[4556]229
[4784]230  GraphicsEngine::getInstance()->initFromIniFile(this->iniParser);
[4766]231
[5219]232  char* iconName = ResourceManager::getFullName("pictures/fighter-top-32x32.bmp");
[5225]233  if (iconName != NULL)
234  {
235    GraphicsEngine::getInstance()->setWindowName(PACKAGE_NAME " " PACKAGE_VERSION, iconName);
236    delete[] iconName;
237  }
[2190]238  return 0;
239}
[1850]240
[5996]241
[2190]242/**
[4833]243 * initializes the sound engine
244 */
[4556]245int Orxonox::initSound()
[2190]246{
[4504]247  PRINT(3)("> Initializing sound\n");
[5225]248  // SDL_InitSubSystem(SDL_INIT_AUDIO);
[4504]249  SoundEngine::getInstance()->initAudio();
[5930]250  SoundEngine::getInstance()->allocateSources(1);
[4985]251
252  SoundEngine::getInstance()->loadSettings(this->iniParser);
[2636]253  return 0;
[2190]254}
[1900]255
[3214]256
[2190]257/**
[4833]258 * initializes input functions
259 */
[4556]260int Orxonox::initInput()
[2190]261{
[4766]262  PRINT(3)("> Initializing input\n");
263
[4866]264  EventHandler::getInstance()->init(this->iniParser);
[4833]265  EventHandler::getInstance()->subscribe(GraphicsEngine::getInstance(), ES_ALL, EV_VIDEO_RESIZE);
[4556]266
[2636]267  return 0;
[1803]268}
269
[3214]270
[2190]271/**
[4833]272 * initializes network system
273 */
[4556]274int Orxonox::initNetworking()
[1897]275{
[4766]276  PRINT(3)("> Initializing networking\n");
277
[5996]278  if( this->serverName != NULL) // we are a client
279    NetworkManager::getInstance()->establishConnection(this->serverName, port);
[6139]280  else if( this->port > 0) {    // we are a server
[5996]281    NetworkManager::getInstance()->createServer(port);
[6139]282  }
[5996]283
[2636]284  return 0;
[1897]285}
286
[3214]287
[2190]288/**
[4833]289 * initializes and loads resource files
[4766]290 */
[4833]291int Orxonox::initResources()
[1858]292{
[4766]293  PRINTF(3)("> Initializing resources\n");
[4091]294
[4766]295  PRINT(3)("initializing ResourceManager\n");
296
[5488]297  // init the resource manager
[5014]298  const char* dataPath;
299  if ((dataPath = this->iniParser->getVar(CONFIG_NAME_DATADIR, CONFIG_SECTION_DATA))!= NULL)
[4766]300  {
[5480]301    if (!ResourceManager::getInstance()->setDataDir(dataPath) &&
[5996]302        !ResourceManager::getInstance()->verifyDataDir(DEFAULT_DATA_DIR_CHECKFILE))
[4766]303    {
[5423]304      PRINTF(1)("Data Could not be located in %s\n", dataPath);
[4766]305    }
306  }
[4556]307
[5480]308  if (!ResourceManager::getInstance()->verifyDataDir(DEFAULT_DATA_DIR_CHECKFILE))
[4766]309  {
[5510]310    PRINTF(1)("The DataDirectory %s could not be verified\n\nh" \
311              "!!!  Please Change in File %s Section %s Entry %s to a suitable value !!!\n",
[4822]312              ResourceManager::getInstance()->getDataDir(),
[5424]313              this->configFileName,
[4766]314              CONFIG_SECTION_DATA,
[5510]315              CONFIG_NAME_DATADIR );
[5479]316    Gui* gui = new Gui(argc, argv);
317    gui->startGui();
318    delete gui;
[4766]319    exit(-1);
320  }
[5996]321  //! @todo this is a hack and should be loadable
[5335]322  char* imageDir = ResourceManager::getInstance()->getFullName("maps");
[5216]323  ResourceManager::getInstance()->addImageDir(imageDir);
324  delete[] imageDir;
[4009]325
[5488]326  // start the collision detection engine
[4766]327  CDEngine::getInstance();
[5074]328  return 0;
329}
330
331/**
332 * initializes miscelaneous features
333 * @return -1 on failure
334 */
335int Orxonox::initMisc()
336{
[5183]337  ShellBuffer::getInstance();
[4766]338  return 0;
[1858]339}
[1849]340
[2190]341/**
[4836]342 *  starts the orxonox game or menu
[4833]343 * here is the central orxonox state manager. There are currently two states
344 * - menu
345 * - game-play
346 * both states manage their states themselfs again.
[2190]347*/
[2636]348void Orxonox::start()
349{
[4556]350
[2636]351  this->gameLoader = GameLoader::getInstance();
[5996]352
[6139]353  if( this->port != -1)
354    this->gameLoader->loadNetworkCampaign("worlds/DefaultNetworkCampaign.oxc");
[5996]355  else
[6139]356    this->gameLoader->loadCampaign("worlds/DefaultCampaign.oxc");                       /* start orxonox in single player mode */
[5996]357
[4010]358  //  this->gameLoader->loadDebugCampaign(DEBUG_CAMPAIGN_0);
[2636]359  this->gameLoader->init();
360  this->gameLoader->start();
361}
362
[3214]363
[2636]364/**
[4833]365 * handles sprecial events from localinput
366 * @param event: an event not handled by the CommandNode
367 */
[4817]368// void Orxonox::graphicsHandler(SDL_Event* event)
369// {
370//   // Handle special events such as reshape, quit, focus changes
371//   switch (event->type)
372//     {
373//     case SDL_VIDEORESIZE:
374//       GraphicsEngine* tmpGEngine = GraphicsEngine::getInstance();
375//       tmpGEngine->resolutionChanged(event->resize);
376//       break;
377//     }
378// }
[1875]379
[4556]380
[4408]381
[1803]382
[3214]383
[4782]384
[4059]385bool showGui = false;
[3648]386
[4766]387
388
389/**********************************
390*** ORXONOX MAIN STARTING POINT ***
391**********************************/
[3449]392/**
[4833]393 *
[4836]394 *  main function
[4833]395 *
396 * here the journey begins
[3449]397*/
[4556]398int main(int argc, char** argv)
399{
[3648]400  int i;
[4032]401  for(i = 1; i < argc; ++i)
[5996]402  {
403    if(     !strcmp( "--help", argv[i])     || !strcmp("-h", argv[i]))
404      return showHelp(argc, argv);
405    else if(!strcmp( "--gui", argv[i])      || !strcmp("-g", argv[i]))
406      showGui = true;
407    else if(!strcmp( "--client", argv[i])   || !strcmp("-c", argv[i]))
408      return startNetworkOrxonox(argc, argv);
409    else if(!strcmp( "--server", argv[i])   || !strcmp("-s", argv[i]))
410      return startNetworkOrxonox(argc, argv);
411    else if(!strcmp( "--license", argv[i])  || !strcmp("-s", argv[i]))
412      return PRINT(0)(ORXONOX_LICENSE_SHORT);
413  }
[3648]414
[5996]415  return startOrxonox(argc, argv, NULL, -1);
[3648]416}
417
418
419
[5996]420int showHelp(int argc, char** argv)
[3648]421{
[5996]422  PRINT(0)("Orxonox Version %s\n", PACKAGE_VERSION);
423  PRINT(0)(" Starts Orxonox - The most furious 3D Action Game :)\n");
424  PRINT(0)("\n");
425  PRINT(0)("Common options:\n");
426  PRINT(0)(" -g, --gui                        starts the orxonox with the configuration GUI \n");
427  PRINT(0)(" -h, --help                       shows this help\n");
428  PRINT(0)("\n");
429  PRINT(0)("Network options:\n");
430  PRINT(0)(" -s, --server [port]              starts Orxonox and listens on the [port] for players\n");
431  PRINT(0)(" -c, --client [hostname] [port]   starts Orxonox as a Client\n");
432  PRINT(0)(" -c, --client [ip address] [port] starts Orxonox as a Client\n");
433  PRINT(0)("\n");
434  PRINT(0)("Other options:\n");
435  PRINT(0)("     --license     prints the licence and exit\n\n");
436  PRINT(0)("\n");
437
438  //   {
439  //     Gui* gui = new Gui(argc, argv);
440  //     gui->printHelp();
441  //     delete gui;
442  //   }
443}
444
445
446
447
448/**
449 * starts orxonox in network mode
450 * @param argc parameters count given to orxonox
451 * @param argv parameters given to orxonox
452 */
453int startNetworkOrxonox(int argc, char** argv)
454{
455
456  int i;
457  for(i = 0; i < argc; ++i )
[4132]458  {
[5996]459    if( !strcmp( "--client", argv[i]) || !strcmp("-c", argv[i]))
460    {
461      if( argc <= (i+2))
462      {
463        printf(" Wrong arguments try following notations:\n");
464        printf("   --client [server ip address] [port number]\n");
465        printf("   --client [dns name] [port number]\n");
466        return 0;
467      }
468
469      const char* name = argv[i+1];
470      int port = atoi(argv[i+2]);
471      printf("Starting Orxonox as client: connecting to %s, on port %i\n", name, port);
472
473      startOrxonox(argc, argv, name, port);
474    }
475    else if( !strcmp( "--server", argv[i]) || !strcmp("-s", argv[i]))
476    {
477      if( argc <= (i+1))
478      {
479        printf(" Wrong arguments try following notations:\n");
480        printf("   --server [port number]\n");
481        return 0;
482      }
483
484      int port = atoi(argv[i+1]);
485      printf("Starting Orxonox as server, listening on port %i\n", port);
486
487      startOrxonox(argc, argv, NULL, port);
488    }
[4132]489  }
[3648]490}
491
[3649]492
[4766]493
494/**
495 * starts orxonox
496 * @param argc parameters count given to orxonox
497 * @param argv parameters given to orxonox
498 */
[5996]499int startOrxonox(int argc, char** argv, const char* name, int port)
[3648]500{
[4830]501  // checking for existence of the configuration-files, or if the lock file is still used
[5424]502  if (showGui || (!ResourceManager::isFile("./orxonox.conf") &&
[5996]503                  !ResourceManager::isFile(DEFAULT_CONFIG_FILE))
[5424]504#if DEBUG < 3 // developers do not need to see the GUI, when orxonox fails
[5996]505      || ResourceManager::isFile(DEFAULT_LOCK_FILE)
[4981]506#endif
507     )
[5996]508  {
509    if (ResourceManager::isFile(DEFAULT_LOCK_FILE))
510      ResourceManager::deleteFile(DEFAULT_LOCK_FILE);
[4556]511
[5996]512    // starting the GUI
513    Gui* gui = new Gui(argc, argv);
514    gui->startGui();
[4132]515
[5996]516    if (! gui->startOrxonox)
517      return 0;
[4556]518
[5996]519    delete gui;
520  }
[4556]521
[4032]522  PRINT(0)(">>> Starting Orxonox <<<\n");
[4033]523
[4766]524  ResourceManager::touchFile(DEFAULT_LOCK_FILE);
[4033]525
[1850]526  Orxonox *orx = Orxonox::getInstance();
[4556]527
[5996]528  if( orx->init(argc, argv, name, port) == -1)
529  {
530    PRINTF(1)("! Orxonox initialization failed\n");
531    return -1;
532  }
[4556]533
[5996]534  printf("finished inizialisation\n");
[2636]535  orx->start();
[4556]536
[3676]537  delete orx;
[4033]538  ResourceManager::deleteFile("~/.orxonox/orxonox.lock");
[1803]539}
Note: See TracBrowser for help on using the repository browser.