Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: moved the keynames strucure to a place closer to util/event, some small tests

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