Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4977 was 4975, checked in by bensch, 20 years ago

orxonox/trunk: player should be able to load its preferences from an external file
removed the obsolete ObjectManager that has been redesigned as FastFactory.

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