Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: minor

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