Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: splitted Texture and TextureData into two files.
Also fixed the Creator-Function for Textures with empty textures with size

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