Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4432 was 4408, checked in by patrick, 20 years ago

orxonox/trunk: integrating the event handler into orxonox mainclass

File size: 9.4 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"
[2190]32#include "command_node.h"
[4091]33#include "ini_parser.h"
[2636]34#include "game_loader.h"
[3610]35#include "graphics_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{
[4059]57  this->pause = false;
58
[4054]59  this->world = NULL;
60  this->localinput = NULL;
[4286]61  this->resourceManager = NULL;
62  this->objectManager = 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*/
[1875]71Orxonox::~Orxonox () 
[2190]72{
[4054]73  int i =0;
[3226]74  Orxonox::singletonRef = NULL;
[2636]75  if( world != NULL) delete world;
[4054]76  if( localinput != NULL) delete localinput;
[3611]77  delete GraphicsEngine::getInstance(); // deleting the Graphics
[3660]78  delete ResourceManager::getInstance(); // deletes the Resource Manager
[4286]79  delete ObjectManager::getInstance();
[3790]80  delete TextEngine::getInstance();
[2190]81}
[1850]82
[3449]83/** \brief this is a singleton class to prevent duplicates */
[3226]84Orxonox* Orxonox::singletonRef = 0;
[1872]85
[3449]86/**
87   \returns reference or new Object of Orxonox if not existent.
88*/
[1850]89Orxonox* Orxonox::getInstance (void)
[1803]90{
[3226]91  if (singletonRef == NULL)
92    singletonRef = new Orxonox();
93  return singletonRef;
[1850]94}
95
[2190]96/**
[2636]97   \brief this finds the config file
98   
99   Since the config file varies from user to user and since one may want to specify different config files
100   for certain occasions or platforms this function finds the right config file for every occasion and stores
101   it's path and name into configfilename
[2190]102*/
[3226]103void Orxonox::getConfigFile (int argc, char** argv)
[1850]104{
[4084]105  strcpy (configfilename, "~/.orxonox/orxonox.conf");
[1803]106}
107
[2190]108/**
[2636]109   \brief initialize Orxonox with command line
[2190]110*/
111int Orxonox::init (int argc, char** argv)
[1803]112{
[4135]113  this->argc = argc;
114  this->argv = argv;
[2636]115  // parse command line
116  // config file
117 
[3226]118  getConfigFile (argc, argv);
[3174]119  SDL_Init (SDL_INIT_TIMER);
[2636]120  // initialize everything
[4113]121  printf("> Initializing resources\n");
122  if( initResources () == -1) return -1;
123
[3226]124  if( initVideo() == -1) return -1;
125  if( initSound() == -1) return -1;
[2190]126  printf("> Initializing input\n");
[3226]127  if( initInput() == -1) return -1;
[2190]128  printf("> Initializing networking\n");
[3226]129  if( initNetworking () == -1) return -1;
[2636]130  //printf("> Initializing world\n");
131  //if( init_world () == -1) return -1; PB: world will be initialized when started
132 
133  return 0;
[1850]134}
[1849]135
[2190]136/**
[2636]137   \brief initializes SDL and OpenGL
[2190]138*/
[3226]139int Orxonox::initVideo() 
[2190]140{
[3611]141  PRINTF(3)("> Initializing video\n");
[2190]142 
[3610]143  GraphicsEngine::getInstance();
[4136]144   
[2190]145  return 0;
146}
[1850]147
[3214]148
[2190]149/**
[2636]150   \brief initializes the sound engine
[2190]151*/
[3226]152int Orxonox::initSound() 
[2190]153{
[3174]154  printf("> Initializing sound\n");
[3226]155  // SDL_Init(SDL_INIT_AUDIO);
[2636]156  printf("Not yet implemented\n");
157  return 0;
[2190]158}
[1900]159
[3214]160
[2190]161/**
[2636]162   \brief initializes input functions
[2190]163*/
[3226]164int Orxonox::initInput() 
[2190]165{
[2636]166  // create localinput
[4084]167  localinput = new CommandNode(configfilename);
[4388]168
[4408]169  this->eventHandler = EventHandler::getInstance();
170  this->eventHandler->init();
[2636]171 
172  return 0;
[1803]173}
174
[3214]175
[2190]176/**
[2636]177   \brief initializes network system
[2190]178*/
[3226]179int Orxonox::initNetworking() 
[1897]180{
[2636]181  printf("Not yet implemented\n");
182  return 0;
[1897]183}
184
[3214]185
[2190]186/**
[2636]187   \brief initializes and loads resource files
[2190]188*/
[3226]189int Orxonox::initResources() 
[1858]190{
[3655]191  PRINT(3)("initializing ResourceManager\n");
192  resourceManager = ResourceManager::getInstance();
[4091]193
194  // create parser
195  IniParser parser (DEFAULT_CONFIG_FILE);
196  if( parser.getSection (CONFIG_SECTION_DATA) == -1)
[4042]197    {
[4091]198      PRINTF(1)("Could not find Section %s in %s\n", CONFIG_SECTION_DATA, DEFAULT_CONFIG_FILE);
199      return -1;
200    }
201  char namebuf[256];
202  char valuebuf[256];
203  memset (namebuf, 0, 256);
204  memset (valuebuf, 0, 256);
205 
206  while( parser.nextVar (namebuf, valuebuf) != -1)
207    {
208      if (!strcmp(namebuf, CONFIG_NAME_DATADIR))
209        {
210          //  printf("Not yet implemented\n");
211          if (!resourceManager->setDataDir(valuebuf))
212            {
213              PRINTF(1)("Data Could not be located\n");
214              exit(-1);
215            }
216        }
217     
218      memset (namebuf, 0, 256);
219      memset (valuebuf, 0, 256);
220    }
221 
222  if (!resourceManager->checkDataDir(DEFAULT_DATA_DIR_CHECKFILE))
223    {
224      PRINTF(1)("The DataDirectory %s could not be verified\nPlease Change in File %s Section %s Entry %s to a suitable value\n",
225                resourceManager->getDataDir(),
226                DEFAULT_CONFIG_FILE,
227                CONFIG_SECTION_DATA,
228                CONFIG_NAME_DATADIR);
[4054]229      exit(-1);
[4042]230    }
[4009]231
[4091]232
[3790]233  PRINT(3)("initializing TextEngine\n");
234  TextEngine::getInstance();
[4132]235
[4286]236  PRINT(3)("initializing ObjectManager\n");
237  this->objectManager = ObjectManager::getInstance();
238
[4132]239  return 0;
[1858]240}
[1849]241
[3214]242
[2190]243/**
[2636]244   \brief initializes the world
[2190]245*/
[3226]246int Orxonox::initWorld() 
[1896]247{
[2636]248  //world = new World();
249 
250  // TO DO: replace this with a menu/intro
251  //world->load_debug_level();
252 
253  return 0;
[1896]254}
255
[2636]256
[2190]257/**
[2636]258   \brief starts the orxonox game or menu
259
260   here is the central orxonox state manager. There are currently two states
261   - menu
262   - game-play
263   both states manage their states themselfs again.
[2190]264*/
[2636]265void Orxonox::start()
266{
267 
268  this->gameLoader = GameLoader::getInstance();
[4094]269  this->gameLoader->loadCampaign("worlds/DefaultCampaign.oxc");
[4010]270  //  this->gameLoader->loadDebugCampaign(DEBUG_CAMPAIGN_0);
[2636]271  this->gameLoader->init();
272  this->gameLoader->start();
273}
274
[3214]275
[2636]276/**
277   \brief exits Orxonox
278*/
[1875]279void Orxonox::quitGame() 
280{
[2636]281  bQuitOrxonox = true;
[1875]282}
283
284
[3214]285
[2190]286/**
[2636]287   \brief handles sprecial events from localinput
288   \param event: an event not handled by the CommandNode
[2190]289*/
[4408]290void Orxonox::graphicsHandler(SDL_Event* event)
[2190]291{
[2636]292  // Handle special events such as reshape, quit, focus changes
[3619]293  switch (event->type)
294    {
295    case SDL_VIDEORESIZE:
296      GraphicsEngine* tmpGEngine = GraphicsEngine::getInstance();
297      tmpGEngine->resolutionChanged(&event->resize);
298      break;
299    }
[2190]300}
[3214]301 
[1875]302
[2190]303/**
[2636]304   \brief handle keyboard commands that are not meant for WorldEntities
305   \param cmd: the command to handle
306   \return true if the command was handled by the system or false if it may be passed to the WorldEntities
[2190]307*/
[3226]308bool Orxonox::systemCommand(Command* cmd)
[2190]309{
[3220]310  /*
[2636]311  if( !strcmp( cmd->cmd, "quit"))
312    {
313      if( !cmd->bUp) this->gameLoader->stop();
314      return true;
315    }
316  return false;
[3220]317  */
318  return false;
[2190]319}
[1803]320
[4408]321
322void Orxonox::process(const Event &event)
323{}
324
[2190]325/**
[2636]326   \brief retrieve a pointer to the local CommandNode
327   \return a pointer to localinput
[2190]328*/
[3226]329CommandNode* Orxonox::getLocalInput()
[1850]330{
[2636]331  return localinput;
[1803]332}
333
[3214]334
[2190]335/**
[2636]336   \brief retrieve a pointer to the local World
337   \return a pointer to world
[2190]338*/
[3226]339World* Orxonox::getWorld()
[1872]340{
[2636]341  return world;
[1872]342}
[1850]343
[3449]344/**
345   \return The reference of the SDL-screen of orxonox
346*/
[3365]347SDL_Surface* Orxonox::getScreen ()
348{
349  return this->screen;
350}
[3214]351
[3648]352
[4059]353bool showGui = false;
[3648]354
[3449]355/**
356   \brief main function
[3214]357
[3449]358   here the journey begins
359*/
[3226]360int main(int argc, char** argv) 
[4135]361{ 
[3648]362
[4135]363  // here the pre-arguments are loaded, these are needed to go either to orxonx itself, Help, or Benchmark.
[3648]364  int i;
[4032]365  for(i = 1; i < argc; ++i)
[3648]366    {
[4135]367      if(! strcmp( "--help", argv[i]) || !strcmp("-h", argv[i])) return startHelp(argc, argv);
368      else if(!strcmp( "--benchmark", argv[i]) || !strcmp("-b", argv[i])) return startBenchmarks();
369      else if(!strcmp( "--gui", argv[i]) || !strcmp("-g", argv[i])) showGui = true;
370      //      else PRINTF(2)("Orxonox does not understand the arguments %s\n", argv[i]);
[3648]371    }
372
373  return startOrxonox(argc, argv);
374}
375
376
377
[4132]378int startHelp(int argc, char** argv)
[3648]379{
[4032]380  PRINT(0)("orxonox: starts the orxonox game - rules\n");
[4134]381  PRINT(0)("usage: orxonox [arg [arg...]]\n\n");
[4032]382  PRINT(0)("valid options:\n");
[4132]383  {
384    Gui* gui = new Gui(argc, argv);
385    gui->printHelp();
386    delete gui;
387  }
[4135]388  PRINT(0)(" -b|--benchmark:\t\tstarts the orxonox benchmark\n");
389  PRINT(0)(" -h|--help:\t\t\tshows this help\n");
[3648]390}
391
[3649]392
[3648]393int startOrxonox(int argc, char** argv)
394{
[4032]395  // checking for existence of the configuration-files
[4059]396  if (showGui ||
397      !ResourceManager::isFile("~/.orxonox/orxonox.conf") ||
398      ResourceManager::isFile("~/.orxonox/orxonox.lock"))
[4032]399    {
400      if (ResourceManager::isFile("~/.orxonox/orxonox.lock"))
401        ResourceManager::deleteFile("~/.orxonox/orxonox.lock");
[4132]402     
403      // starting the GUI
[4056]404      Gui* gui = new Gui(argc, argv);
[4132]405      gui->startGui();
406
[4054]407      if (! gui->startOrxonox)
408        return 0;
409     
410      delete gui;
[4032]411    }
412 
413  PRINT(0)(">>> Starting Orxonox <<<\n");
[4033]414
415  ResourceManager::touchFile("~/.orxonox/orxonox.lock");
416
[1850]417  Orxonox *orx = Orxonox::getInstance();
[2190]418 
[3226]419  if((*orx).init(argc, argv) == -1)
[2636]420    {
[4032]421      PRINTF(1)("! Orxonox initialization failed\n");
[2636]422      return -1;
423    }
424 
425  orx->start();
426 
[3676]427  delete orx;
[4033]428  ResourceManager::deleteFile("~/.orxonox/orxonox.lock");
[2190]429 
[1803]430}
Note: See TracBrowser for help on using the repository browser.