Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7259 was 7258, checked in by rennerc, 20 years ago

added some command line arguments

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