Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4448 was 4446, checked in by patrick, 19 years ago

orxonox/trunk: finished cleaning up orxonox.cc

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