Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Singleton-Classes are now refered to as such in World and Orxonox

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"
[4286]39#include "object_manager.h"
[4786]40#include "cd_engine.h"
[3790]41#include "text_engine.h"
[4786]42#include "event_handler.h"
[4815]43#include "garbage_collector.h"
[4786]44
45#include "state.h"
46#include "event.h"
[4010]47#include "factory.h"
[4131]48#include "benchmark.h"
[3610]49
[4786]50#include "class_list.h"
[4766]51#include "substring.h"
[4748]52
[2190]53#include <string.h>
[4032]54
[3966]55int verbose = 4;
[2036]56
[1803]57using namespace std;
58
[2190]59/**
[2636]60   \brief create a new Orxonox
[4135]61
62   In this funcitons only global values are set. The game will not be started here.
[2190]63*/
64Orxonox::Orxonox ()
[1872]65{
[4445]66  this->setClassID(CL_ORXONOX, "Orxonox");
[4766]67  this->setName("orxonox-main");
[4059]68
[4766]69  this->iniParser = NULL;
[4135]70
71  this->argc = 0;
72  this->argv = NULL;
[4782]73
[1872]74}
[1803]75
[2190]76/**
[2636]77   \brief remove Orxonox from memory
[2190]78*/
[4556]79Orxonox::~Orxonox ()
[2190]80{
[4766]81  delete this->iniParser;
82
[3611]83  delete GraphicsEngine::getInstance(); // deleting the Graphics
[4504]84  delete TextEngine::getInstance();
85  delete SoundEngine::getInstance();
[3660]86  delete ResourceManager::getInstance(); // deletes the Resource Manager
[4286]87  delete ObjectManager::getInstance();
[3790]88  delete TextEngine::getInstance();
[4749]89  delete Factory::getFirst();
[4815]90  delete GameLoader::getInstance();
[4766]91  delete SoundEngine::getInstance();
[4786]92  delete State::getInstance();
[4815]93  delete CDEngine::getInstance();
94  delete GarbageCollector::getInstance();
[4748]95
[4817]96  delete EventHandler::getInstance();
97
[4753]98  ClassList::debug(0);
[1850]99
[4766]100  PRINT(3)("===================================================\n" \
101      "Thanks for playing orxonox.\n" \
102      "visit: http://www.orxonox.ethz.ch for new versions.\n" \
103      "===================================================\n");
[1872]104
[4766]105  Orxonox::singletonRef = NULL;
[1850]106}
107
[2190]108/**
[4766]109 * @brief this is a singleton class to prevent duplicates
110 */
111Orxonox* Orxonox::singletonRef = NULL;
[4556]112
[4766]113/**
114 * @brief this finds the config file
115 * @returns the new config-fileName
116 * Since the config file varies from user to user and since one may want to specify different config files
117 * for certain occasions or platforms this function finds the right config file for every occasion and stores
118 * it's path and name into configfilename
[2190]119*/
[4766]120const char* Orxonox::getConfigFile (int argc, char** argv)
[1850]121{
[4766]122  strcpy (this->configFileName, DEFAULT_CONFIG_FILE);
123  this->iniParser = new IniParser(this->configFileName);
[1803]124}
125
[2190]126/**
[2636]127   \brief initialize Orxonox with command line
[2190]128*/
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
137  this->getConfigFile(argc, argv);
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/**
[2636]152   \brief 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/**
[2636]167   \brief initializes the sound engine
[2190]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/**
[4766]179 * @brief initializes input functions
[2190]180*/
[4556]181int Orxonox::initInput()
[2190]182{
[4766]183  PRINT(3)("> Initializing input\n");
184
[4822]185  EventHandler::getInstance()->init();
[4556]186
[2636]187  return 0;
[1803]188}
189
[3214]190
[2190]191/**
[4766]192* @brief initializes network system
[2190]193*/
[4556]194int Orxonox::initNetworking()
[1897]195{
[4766]196  PRINT(3)("> Initializing networking\n");
197
198  printf("  ---Not yet implemented-FIXME--\n");
[2636]199  return 0;
[1897]200}
201
[3214]202
[2190]203/**
[4766]204 * @brief initializes and loads resource files
205 */
206 int Orxonox::initResources()
[1858]207{
[4766]208  PRINTF(3)("> Initializing resources\n");
[4091]209
[4766]210  PRINT(3)("initializing ResourceManager\n");
211
[4091]212  // create parser
[4766]213  if( this->iniParser->getSection (CONFIG_SECTION_DATA) == -1)
214  {
215    PRINTF(1)("Could not find Section %s in %s\n", CONFIG_SECTION_DATA, DEFAULT_CONFIG_FILE);
216    return -1;
217  }
218  char namebuf[256];
219  char valuebuf[256];
220  memset (namebuf, 0, 256);
221  memset (valuebuf, 0, 256);
[4556]222
[4766]223  while( this->iniParser->nextVar (namebuf, valuebuf) != -1)
224  {
225    if (!strcmp(namebuf, CONFIG_NAME_DATADIR))
226    {
[4556]227          //  printf("Not yet implemented\n");
[4822]228      if (!ResourceManager::getInstance()->setDataDir(valuebuf))
[4766]229      {
230        PRINTF(1)("Data Could not be located\n");
231        exit(-1);
232      }
233    }
[4556]234
[4766]235    memset (namebuf, 0, 256);
236    memset (valuebuf, 0, 256);
237  }
[4556]238
[4822]239  if (!ResourceManager::getInstance()->checkDataDir(DEFAULT_DATA_DIR_CHECKFILE))
[4766]240  {
241    PRINTF(1)("The DataDirectory %s could not be verified\n" \
242              "  Please Change in File %s Section %s Entry %s to a suitable value\n",
[4822]243              ResourceManager::getInstance()->getDataDir(),
[4766]244              DEFAULT_CONFIG_FILE,
245              CONFIG_SECTION_DATA,
246              CONFIG_NAME_DATADIR);
247    exit(-1);
248  }
[4606]249   //! \todo this is a hack and should be loadable
[4822]250  ResourceManager::getInstance()->addImageDir(ResourceManager::getInstance()->getFullName("maps/"));
251  ResourceManager::getInstance()->debug();
[4009]252
[4766]253  PRINT(3)("initializing TextEngine\n");
254  TextEngine::getInstance();
[4091]255
[4766]256  PRINT(3)("initializing ObjectManager\n");
[4822]257  ObjectManager::getInstance();
[4766]258  CDEngine::getInstance();
[4616]259
[4766]260  return 0;
[1858]261}
[1849]262
[2190]263/**
[2636]264   \brief starts the orxonox game or menu
265
266   here is the central orxonox state manager. There are currently two states
267   - menu
268   - game-play
269   both states manage their states themselfs again.
[2190]270*/
[2636]271void Orxonox::start()
272{
[4556]273
[2636]274  this->gameLoader = GameLoader::getInstance();
[4094]275  this->gameLoader->loadCampaign("worlds/DefaultCampaign.oxc");
[4010]276  //  this->gameLoader->loadDebugCampaign(DEBUG_CAMPAIGN_0);
[2636]277  this->gameLoader->init();
278  this->gameLoader->start();
279}
280
[3214]281
[2636]282/**
283   \brief handles sprecial events from localinput
[4556]284   \param event: an event not handled by the CommandNode
[2190]285*/
[4817]286// void Orxonox::graphicsHandler(SDL_Event* event)
287// {
288//   // Handle special events such as reshape, quit, focus changes
289//   switch (event->type)
290//     {
291//     case SDL_VIDEORESIZE:
292//       GraphicsEngine* tmpGEngine = GraphicsEngine::getInstance();
293//       tmpGEngine->resolutionChanged(event->resize);
294//       break;
295//     }
296// }
[1875]297
[4556]298
[4408]299
[1803]300
[3214]301
[4782]302
[4059]303bool showGui = false;
[3648]304
[4766]305
306
307/**********************************
308*** ORXONOX MAIN STARTING POINT ***
309**********************************/
[3449]310/**
311   \brief main function
[3214]312
[3449]313   here the journey begins
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{
[4032]355  // checking for existence of the configuration-files
[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.