Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/terrain/src/orxonox.cc @ 8465

Last change on this file since 8465 was 8308, checked in by ponder, 18 years ago
  • orxonox crashed on osx when a call to the opengl subsystem was done before the window was created. I changed the

order of initialisation in graphics_engine.cc.

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