Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: state is no more singleton, but static class.
This is faster, and easier to handle, because one can just say State::getBLA() and not State::getInstance()→getBLA(); ——→>> less redundant

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 "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
[4766]68  this->iniParser = NULL;
[4135]69
70  this->argc = 0;
71  this->argv = NULL;
[4782]72
[1872]73}
[1803]74
[2190]75/**
[2636]76   \brief remove Orxonox from memory
[2190]77*/
[4556]78Orxonox::~Orxonox ()
[2190]79{
[4766]80  delete this->iniParser;
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
[4286]86  delete ObjectManager::getInstance();
[3790]87  delete TextEngine::getInstance();
[4749]88  delete Factory::getFirst();
[4815]89  delete GameLoader::getInstance();
[4766]90  delete SoundEngine::getInstance();
[4815]91  delete CDEngine::getInstance();
92  delete GarbageCollector::getInstance();
[4748]93
[4817]94  delete EventHandler::getInstance();
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
[2636]146  return 0;
[1850]147}
[1849]148
[2190]149/**
[2636]150   \brief initializes SDL and OpenGL
[2190]151*/
[4556]152int Orxonox::initVideo()
[2190]153{
[3611]154  PRINTF(3)("> Initializing video\n");
[4556]155
[3610]156  GraphicsEngine::getInstance();
[4766]157  GraphicsEngine::getInstance()->setWindowName(PACKAGE_NAME " " PACKAGE_VERSION, PACKAGE_NAME " " PACKAGE_VERSION);
[4556]158
[4784]159  GraphicsEngine::getInstance()->initFromIniFile(this->iniParser);
[4766]160
[2190]161  return 0;
162}
[1850]163
[2190]164/**
[2636]165   \brief initializes the sound engine
[2190]166*/
[4556]167int Orxonox::initSound()
[2190]168{
[4504]169  PRINT(3)("> Initializing sound\n");
[3226]170  // SDL_Init(SDL_INIT_AUDIO);
[4504]171  SoundEngine::getInstance()->initAudio();
[2636]172  return 0;
[2190]173}
[1900]174
[3214]175
[2190]176/**
[4766]177 * @brief initializes input functions
[2190]178*/
[4556]179int Orxonox::initInput()
[2190]180{
[4766]181  PRINT(3)("> Initializing input\n");
182
[4822]183  EventHandler::getInstance()->init();
[4556]184
[2636]185  return 0;
[1803]186}
187
[3214]188
[2190]189/**
[4766]190* @brief initializes network system
[2190]191*/
[4556]192int Orxonox::initNetworking()
[1897]193{
[4766]194  PRINT(3)("> Initializing networking\n");
195
196  printf("  ---Not yet implemented-FIXME--\n");
[2636]197  return 0;
[1897]198}
199
[3214]200
[2190]201/**
[4766]202 * @brief initializes and loads resource files
203 */
204 int Orxonox::initResources()
[1858]205{
[4766]206  PRINTF(3)("> Initializing resources\n");
[4091]207
[4766]208  PRINT(3)("initializing ResourceManager\n");
209
[4091]210  // create parser
[4766]211  if( this->iniParser->getSection (CONFIG_SECTION_DATA) == -1)
212  {
213    PRINTF(1)("Could not find Section %s in %s\n", CONFIG_SECTION_DATA, DEFAULT_CONFIG_FILE);
214    return -1;
215  }
216  char namebuf[256];
217  char valuebuf[256];
218  memset (namebuf, 0, 256);
219  memset (valuebuf, 0, 256);
[4556]220
[4766]221  while( this->iniParser->nextVar (namebuf, valuebuf) != -1)
222  {
223    if (!strcmp(namebuf, CONFIG_NAME_DATADIR))
224    {
[4556]225          //  printf("Not yet implemented\n");
[4822]226      if (!ResourceManager::getInstance()->setDataDir(valuebuf))
[4766]227      {
228        PRINTF(1)("Data Could not be located\n");
229        exit(-1);
230      }
231    }
[4556]232
[4766]233    memset (namebuf, 0, 256);
234    memset (valuebuf, 0, 256);
235  }
[4556]236
[4822]237  if (!ResourceManager::getInstance()->checkDataDir(DEFAULT_DATA_DIR_CHECKFILE))
[4766]238  {
239    PRINTF(1)("The DataDirectory %s could not be verified\n" \
240              "  Please Change in File %s Section %s Entry %s to a suitable value\n",
[4822]241              ResourceManager::getInstance()->getDataDir(),
[4766]242              DEFAULT_CONFIG_FILE,
243              CONFIG_SECTION_DATA,
244              CONFIG_NAME_DATADIR);
245    exit(-1);
246  }
[4606]247   //! \todo this is a hack and should be loadable
[4822]248  ResourceManager::getInstance()->addImageDir(ResourceManager::getInstance()->getFullName("maps/"));
249  ResourceManager::getInstance()->debug();
[4009]250
[4766]251  PRINT(3)("initializing TextEngine\n");
252  TextEngine::getInstance();
[4091]253
[4766]254  PRINT(3)("initializing ObjectManager\n");
[4822]255  ObjectManager::getInstance();
[4766]256  CDEngine::getInstance();
[4616]257
[4766]258  return 0;
[1858]259}
[1849]260
[2190]261/**
[2636]262   \brief starts the orxonox game or menu
263
264   here is the central orxonox state manager. There are currently two states
265   - menu
266   - game-play
267   both states manage their states themselfs again.
[2190]268*/
[2636]269void Orxonox::start()
270{
[4556]271
[2636]272  this->gameLoader = GameLoader::getInstance();
[4094]273  this->gameLoader->loadCampaign("worlds/DefaultCampaign.oxc");
[4010]274  //  this->gameLoader->loadDebugCampaign(DEBUG_CAMPAIGN_0);
[2636]275  this->gameLoader->init();
276  this->gameLoader->start();
277}
278
[3214]279
[2636]280/**
281   \brief handles sprecial events from localinput
[4556]282   \param event: an event not handled by the CommandNode
[2190]283*/
[4817]284// void Orxonox::graphicsHandler(SDL_Event* event)
285// {
286//   // Handle special events such as reshape, quit, focus changes
287//   switch (event->type)
288//     {
289//     case SDL_VIDEORESIZE:
290//       GraphicsEngine* tmpGEngine = GraphicsEngine::getInstance();
291//       tmpGEngine->resolutionChanged(event->resize);
292//       break;
293//     }
294// }
[1875]295
[4556]296
[4408]297
[1803]298
[3214]299
[4782]300
[4059]301bool showGui = false;
[3648]302
[4766]303
304
305/**********************************
306*** ORXONOX MAIN STARTING POINT ***
307**********************************/
[3449]308/**
309   \brief main function
[3214]310
[3449]311   here the journey begins
312*/
[4556]313int main(int argc, char** argv)
314{
[4135]315  // here the pre-arguments are loaded, these are needed to go either to orxonx itself, Help, or Benchmark.
[3648]316  int i;
[4032]317  for(i = 1; i < argc; ++i)
[3648]318    {
[4135]319      if(! strcmp( "--help", argv[i]) || !strcmp("-h", argv[i])) return startHelp(argc, argv);
320      else if(!strcmp( "--benchmark", argv[i]) || !strcmp("-b", argv[i])) return startBenchmarks();
321      else if(!strcmp( "--gui", argv[i]) || !strcmp("-g", argv[i])) showGui = true;
322      //      else PRINTF(2)("Orxonox does not understand the arguments %s\n", argv[i]);
[3648]323    }
324
325  return startOrxonox(argc, argv);
326}
327
328
329
[4132]330int startHelp(int argc, char** argv)
[3648]331{
[4032]332  PRINT(0)("orxonox: starts the orxonox game - rules\n");
[4134]333  PRINT(0)("usage: orxonox [arg [arg...]]\n\n");
[4032]334  PRINT(0)("valid options:\n");
[4132]335  {
336    Gui* gui = new Gui(argc, argv);
337    gui->printHelp();
338    delete gui;
339  }
[4135]340  PRINT(0)(" -b|--benchmark:\t\tstarts the orxonox benchmark\n");
341  PRINT(0)(" -h|--help:\t\t\tshows this help\n");
[3648]342}
343
[3649]344
[4766]345
346/**
347 * starts orxonox
348 * @param argc parameters count given to orxonox
349 * @param argv parameters given to orxonox
350 */
[3648]351int startOrxonox(int argc, char** argv)
352{
[4032]353  // checking for existence of the configuration-files
[4059]354  if (showGui ||
[4766]355      !ResourceManager::isFile(DEFAULT_CONFIG_FILE) ||
356      ResourceManager::isFile(DEFAULT_LOCK_FILE))
[4032]357    {
[4766]358      if (ResourceManager::isFile(DEFAULT_LOCK_FILE))
359        ResourceManager::deleteFile(DEFAULT_LOCK_FILE);
[4556]360
[4132]361      // starting the GUI
[4056]362      Gui* gui = new Gui(argc, argv);
[4132]363      gui->startGui();
364
[4054]365      if (! gui->startOrxonox)
[4556]366        return 0;
367
[4054]368      delete gui;
[4032]369    }
[4556]370
[4032]371  PRINT(0)(">>> Starting Orxonox <<<\n");
[4033]372
[4766]373  ResourceManager::touchFile(DEFAULT_LOCK_FILE);
[4033]374
[1850]375  Orxonox *orx = Orxonox::getInstance();
[4556]376
[3226]377  if((*orx).init(argc, argv) == -1)
[2636]378    {
[4032]379      PRINTF(1)("! Orxonox initialization failed\n");
[2636]380      return -1;
381    }
[4556]382
[2636]383  orx->start();
[4556]384
[3676]385  delete orx;
[4033]386  ResourceManager::deleteFile("~/.orxonox/orxonox.lock");
[4556]387
[1803]388}
Note: See TracBrowser for help on using the repository browser.