Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: default value can now be supplied as aditional arg of the SHELL_COMMAND-macro

File size: 9.8 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
[5158]23co-programmer: Christian Meyer
[4054]24   co-programmer: Benjamin Grauer: injected ResourceManager/GraphicsEngine/GUI
[1850]25*/
26
[2190]27#include "orxonox.h"
[3610]28
[4054]29#include "gui.h"
30
[2036]31#include "world.h"
[4091]32#include "ini_parser.h"
[5165]33#include "game_loader.h"
[4786]34
35//ENGINES
[3610]36#include "graphics_engine.h"
[4504]37#include "sound_engine.h"
[3655]38#include "resource_manager.h"
[4786]39#include "cd_engine.h"
[3790]40#include "text_engine.h"
[4786]41#include "event_handler.h"
[4815]42#include "garbage_collector.h"
[4786]43
[4010]44#include "factory.h"
[4980]45#include "fast_factory.h"
46
[4131]47#include "benchmark.h"
[3610]48
[4786]49#include "class_list.h"
[4766]50#include "substring.h"
[5072]51#include "shell.h"
[5165]52#include "shell_command.h"
[5175]53#include "shell_buffer.h"
[4748]54
[2190]55#include <string.h>
[4032]56
[4885]57int verbose = 4;
[2036]58
[1803]59using namespace std;
60
[5207]61SHELL_COMMAND(restart, Orxonox, restart);
62
[2190]63/**
[4836]64 *  create a new Orxonox
[4135]65
66   In this funcitons only global values are set. The game will not be started here.
[2190]67*/
68Orxonox::Orxonox ()
[1872]69{
[4445]70  this->setClassID(CL_ORXONOX, "Orxonox");
[4766]71  this->setName("orxonox-main");
[4059]72
[4766]73  this->iniParser = NULL;
[4135]74
75  this->argc = 0;
76  this->argv = NULL;
[4782]77
[4830]78  this->configFileName = NULL;
[1872]79}
[1803]80
[2190]81/**
[4836]82 *  remove Orxonox from memory
[2190]83*/
[4556]84Orxonox::~Orxonox ()
[2190]85{
[3611]86  delete GraphicsEngine::getInstance(); // deleting the Graphics
[4504]87  delete TextEngine::getInstance();
88  delete SoundEngine::getInstance();
[3660]89  delete ResourceManager::getInstance(); // deletes the Resource Manager
[3790]90  delete TextEngine::getInstance();
[4749]91  delete Factory::getFirst();
[4815]92  delete GameLoader::getInstance();
[4766]93  delete SoundEngine::getInstance();
[4815]94  delete CDEngine::getInstance();
95  delete GarbageCollector::getInstance();
[4980]96  FastFactory::deleteAll();
[5170]97  ShellCommandBase::debug();
[5171]98  ShellCommandClass::unregisterAllCommands();
99  ShellCommandBase::debug();
[5177]100  delete ShellBuffer::getInstance();
[4748]101
[4817]102  delete EventHandler::getInstance();
[5078]103  delete this->iniParser;
104  delete this->configFileName;
[4817]105
[4942]106  ClassList::debug();
[1850]107
[4833]108  PRINT(3)
[4981]109      (
110      "===================================================\n" \
[4766]111      "Thanks for playing orxonox.\n" \
[5037]112      "visit: http://www.orxonox.net for new versions.\n" \
[4946]113      "===================================================\n" \
[4981]114      ORXONOX_LICENSE_SHORT
115      );
[1872]116
[4766]117  Orxonox::singletonRef = NULL;
[1850]118}
119
[2190]120/**
[4836]121 *  this is a singleton class to prevent duplicates
[4766]122 */
123Orxonox* Orxonox::singletonRef = NULL;
[4556]124
[5207]125// DANGEROUS
126void Orxonox::restart()
127{
128//   int argc = this->argc;
129//   char** argv = this->argv;
130//
131//   Orxonox *orx = Orxonox::getInstance();
132//
133//   delete orx;
134//
135//   orx = Orxonox::getInstance();
136//
137//   if((*orx).init(argc, argv) == -1)
138//   {
139//     PRINTF(1)("! Orxonox initialization failed\n");
140//     return;
141//   }
142//
143//   printf("finished inizialisation\n");
144//   orx->start();
145}
146
[4766]147/**
[4836]148 *  this finds the config file
[4766]149 * @returns the new config-fileName
150 * Since the config file varies from user to user and since one may want to specify different config files
151 * for certain occasions or platforms this function finds the right config file for every occasion and stores
152 * it's path and name into configfilename
[2190]153*/
[4830]154const char* Orxonox::getConfigFile ()
[1850]155{
[5015]156  this->configFileName = ResourceManager::homeDirCheck(DEFAULT_CONFIG_FILE);
[4766]157  this->iniParser = new IniParser(this->configFileName);
[1803]158}
159
[2190]160/**
[4833]161 * initialize Orxonox with command line
162 */
[2190]163int Orxonox::init (int argc, char** argv)
[1803]164{
[4135]165  this->argc = argc;
166  this->argv = argv;
[2636]167  // parse command line
168  // config file
[4556]169
[4766]170  // initialize the Config-file
[4830]171  this->getConfigFile();
[4766]172
[4782]173  // initialize everything
[3174]174  SDL_Init (SDL_INIT_TIMER);
[4113]175  if( initResources () == -1) return -1;
[3226]176  if( initVideo() == -1) return -1;
177  if( initSound() == -1) return -1;
178  if( initInput() == -1) return -1;
179  if( initNetworking () == -1) return -1;
[5074]180  if( initMisc () == -1) return -1;
[4556]181
[2636]182  return 0;
[1850]183}
[1849]184
[2190]185/**
[4833]186 * initializes SDL and OpenGL
[2190]187*/
[4556]188int Orxonox::initVideo()
[2190]189{
[3611]190  PRINTF(3)("> Initializing video\n");
[4556]191
[3610]192  GraphicsEngine::getInstance();
[4556]193
[4784]194  GraphicsEngine::getInstance()->initFromIniFile(this->iniParser);
[4766]195
[5024]196  char* icon = ResourceManager::getFullName("pictures/fighter-top-32x32.bmp");
197  GraphicsEngine::getInstance()->setWindowName(PACKAGE_NAME " " PACKAGE_VERSION, icon);
198  delete icon;
[2190]199  return 0;
200}
[1850]201
[2190]202/**
[4833]203 * initializes the sound engine
204 */
[4556]205int Orxonox::initSound()
[2190]206{
[4504]207  PRINT(3)("> Initializing sound\n");
[3226]208  // SDL_Init(SDL_INIT_AUDIO);
[4504]209  SoundEngine::getInstance()->initAudio();
[4985]210
211  SoundEngine::getInstance()->loadSettings(this->iniParser);
[2636]212  return 0;
[2190]213}
[1900]214
[3214]215
[2190]216/**
[4833]217 * initializes input functions
218 */
[4556]219int Orxonox::initInput()
[2190]220{
[4766]221  PRINT(3)("> Initializing input\n");
222
[4866]223  EventHandler::getInstance()->init(this->iniParser);
[4833]224  EventHandler::getInstance()->subscribe(GraphicsEngine::getInstance(), ES_ALL, EV_VIDEO_RESIZE);
[4556]225
[4833]226
[2636]227  return 0;
[1803]228}
229
[3214]230
[2190]231/**
[4833]232 * initializes network system
233 */
[4556]234int Orxonox::initNetworking()
[1897]235{
[4766]236  PRINT(3)("> Initializing networking\n");
237
238  printf("  ---Not yet implemented-FIXME--\n");
[2636]239  return 0;
[1897]240}
241
[3214]242
[2190]243/**
[4833]244 * initializes and loads resource files
[4766]245 */
[4833]246int Orxonox::initResources()
[1858]247{
[4766]248  PRINTF(3)("> Initializing resources\n");
[4091]249
[4766]250  PRINT(3)("initializing ResourceManager\n");
251
[5014]252  const char* dataPath;
253  if ((dataPath = this->iniParser->getVar(CONFIG_NAME_DATADIR, CONFIG_SECTION_DATA))!= NULL)
[4766]254  {
[5014]255    if (!ResourceManager::getInstance()->setDataDir(dataPath))
[4766]256    {
[5014]257      PRINTF(1)("Data Could not be located\n");
258      exit(-1);
[4766]259    }
260  }
[4556]261
[4822]262  if (!ResourceManager::getInstance()->checkDataDir(DEFAULT_DATA_DIR_CHECKFILE))
[4766]263  {
264    PRINTF(1)("The DataDirectory %s could not be verified\n" \
265              "  Please Change in File %s Section %s Entry %s to a suitable value\n",
[4822]266              ResourceManager::getInstance()->getDataDir(),
[4766]267              DEFAULT_CONFIG_FILE,
268              CONFIG_SECTION_DATA,
269              CONFIG_NAME_DATADIR);
270    exit(-1);
271  }
[4836]272   //! @todo this is a hack and should be loadable
[4822]273  ResourceManager::getInstance()->addImageDir(ResourceManager::getInstance()->getFullName("maps/"));
274  ResourceManager::getInstance()->debug();
[4009]275
[4766]276  PRINT(3)("initializing TextEngine\n");
277  TextEngine::getInstance();
[4091]278
[4766]279  CDEngine::getInstance();
[5074]280  return 0;
281}
282
283/**
284 * initializes miscelaneous features
285 * @return -1 on failure
286 */
287int Orxonox::initMisc()
288{
[5183]289  ShellBuffer::getInstance();
[4766]290  return 0;
[1858]291}
[1849]292
[2190]293/**
[4836]294 *  starts the orxonox game or menu
[4833]295 * here is the central orxonox state manager. There are currently two states
296 * - menu
297 * - game-play
298 * both states manage their states themselfs again.
[2190]299*/
[2636]300void Orxonox::start()
301{
[4556]302
[2636]303  this->gameLoader = GameLoader::getInstance();
[4094]304  this->gameLoader->loadCampaign("worlds/DefaultCampaign.oxc");
[4010]305  //  this->gameLoader->loadDebugCampaign(DEBUG_CAMPAIGN_0);
[2636]306  this->gameLoader->init();
307  this->gameLoader->start();
308}
309
[3214]310
[2636]311/**
[4833]312 * handles sprecial events from localinput
313 * @param event: an event not handled by the CommandNode
314 */
[4817]315// void Orxonox::graphicsHandler(SDL_Event* event)
316// {
317//   // Handle special events such as reshape, quit, focus changes
318//   switch (event->type)
319//     {
320//     case SDL_VIDEORESIZE:
321//       GraphicsEngine* tmpGEngine = GraphicsEngine::getInstance();
322//       tmpGEngine->resolutionChanged(event->resize);
323//       break;
324//     }
325// }
[1875]326
[4556]327
[4408]328
[1803]329
[3214]330
[4782]331
[4059]332bool showGui = false;
[3648]333
[4766]334
335
336/**********************************
337*** ORXONOX MAIN STARTING POINT ***
338**********************************/
[3449]339/**
[4833]340 *
[4836]341 *  main function
[4833]342 *
343 * here the journey begins
[3449]344*/
[4556]345int main(int argc, char** argv)
346{
[4135]347  // here the pre-arguments are loaded, these are needed to go either to orxonx itself, Help, or Benchmark.
[3648]348  int i;
[4032]349  for(i = 1; i < argc; ++i)
[3648]350    {
[4135]351      if(! strcmp( "--help", argv[i]) || !strcmp("-h", argv[i])) return startHelp(argc, argv);
352      else if(!strcmp( "--benchmark", argv[i]) || !strcmp("-b", argv[i])) return startBenchmarks();
353      else if(!strcmp( "--gui", argv[i]) || !strcmp("-g", argv[i])) showGui = true;
354      //      else PRINTF(2)("Orxonox does not understand the arguments %s\n", argv[i]);
[3648]355    }
356
357  return startOrxonox(argc, argv);
358}
359
360
361
[4132]362int startHelp(int argc, char** argv)
[3648]363{
[4032]364  PRINT(0)("orxonox: starts the orxonox game - rules\n");
[4134]365  PRINT(0)("usage: orxonox [arg [arg...]]\n\n");
[4032]366  PRINT(0)("valid options:\n");
[4132]367  {
368    Gui* gui = new Gui(argc, argv);
369    gui->printHelp();
370    delete gui;
371  }
[4135]372  PRINT(0)(" -b|--benchmark:\t\tstarts the orxonox benchmark\n");
373  PRINT(0)(" -h|--help:\t\t\tshows this help\n");
[3648]374}
375
[3649]376
[4766]377
378/**
379 * starts orxonox
380 * @param argc parameters count given to orxonox
381 * @param argv parameters given to orxonox
382 */
[3648]383int startOrxonox(int argc, char** argv)
384{
[4830]385  // checking for existence of the configuration-files, or if the lock file is still used
[4059]386  if (showGui ||
[4981]387      !ResourceManager::isFile(DEFAULT_CONFIG_FILE)
388#if DEBUG < 3
389       || ResourceManager::isFile(DEFAULT_LOCK_FILE)
390#endif
391     )
[4032]392    {
[4766]393      if (ResourceManager::isFile(DEFAULT_LOCK_FILE))
394        ResourceManager::deleteFile(DEFAULT_LOCK_FILE);
[4556]395
[4132]396      // starting the GUI
[4056]397      Gui* gui = new Gui(argc, argv);
[4132]398      gui->startGui();
399
[4054]400      if (! gui->startOrxonox)
[4556]401        return 0;
402
[4054]403      delete gui;
[4032]404    }
[4556]405
[4032]406  PRINT(0)(">>> Starting Orxonox <<<\n");
[4033]407
[4766]408  ResourceManager::touchFile(DEFAULT_LOCK_FILE);
[4033]409
[1850]410  Orxonox *orx = Orxonox::getInstance();
[4556]411
[3226]412  if((*orx).init(argc, argv) == -1)
[2636]413    {
[4032]414      PRINTF(1)("! Orxonox initialization failed\n");
[2636]415      return -1;
416    }
[4556]417
[5018]418    printf("finished inizialisation\n");
[2636]419  orx->start();
[4556]420
[3676]421  delete orx;
[4033]422  ResourceManager::deleteFile("~/.orxonox/orxonox.lock");
[4556]423
[1803]424}
Note: See TracBrowser for help on using the repository browser.