Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: deleting state

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