Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7278 was 7269, checked in by bensch, 19 years ago

creating and linking ltdl… hmm.. lets see if this works

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