Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4431 was 4408, checked in by patrick, 19 years ago

orxonox/trunk: integrating the event handler into orxonox mainclass

File size: 9.4 KB
Line 
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
20
21   ### File Specific:
22   main-programmer: Patrick Boenzli
23   co-programmer: Christian Meyer
24   co-programmer: Benjamin Grauer: injected ResourceManager/GraphicsEngine/GUI
25*/
26
27#include "orxonox.h"
28
29#include "gui.h"
30
31#include "world.h"
32#include "command_node.h"
33#include "ini_parser.h"
34#include "game_loader.h"
35#include "graphics_engine.h"
36#include "resource_manager.h"
37#include "object_manager.h"
38#include "text_engine.h"
39#include "factory.h"
40#include "benchmark.h"
41#include "event_handler.h"
42#include "event.h"
43
44#include <string.h>
45
46int verbose = 4;
47
48using namespace std;
49
50/**
51   \brief create a new Orxonox
52
53   In this funcitons only global values are set. The game will not be started here.
54*/
55Orxonox::Orxonox ()
56{
57  this->pause = false;
58
59  this->world = NULL;
60  this->localinput = NULL;
61  this->resourceManager = NULL;
62  this->objectManager = NULL;
63
64  this->argc = 0;
65  this->argv = NULL;
66}
67
68/**
69   \brief remove Orxonox from memory
70*/
71Orxonox::~Orxonox () 
72{
73  int i =0;
74  Orxonox::singletonRef = NULL;
75  if( world != NULL) delete world;
76  if( localinput != NULL) delete localinput;
77  delete GraphicsEngine::getInstance(); // deleting the Graphics
78  delete ResourceManager::getInstance(); // deletes the Resource Manager
79  delete ObjectManager::getInstance();
80  delete TextEngine::getInstance();
81}
82
83/** \brief this is a singleton class to prevent duplicates */
84Orxonox* Orxonox::singletonRef = 0;
85
86/**
87   \returns reference or new Object of Orxonox if not existent.
88*/
89Orxonox* Orxonox::getInstance (void)
90{
91  if (singletonRef == NULL)
92    singletonRef = new Orxonox();
93  return singletonRef;
94}
95
96/**
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
102*/
103void Orxonox::getConfigFile (int argc, char** argv)
104{
105  strcpy (configfilename, "~/.orxonox/orxonox.conf");
106}
107
108/**
109   \brief initialize Orxonox with command line
110*/
111int Orxonox::init (int argc, char** argv)
112{
113  this->argc = argc;
114  this->argv = argv;
115  // parse command line
116  // config file
117 
118  getConfigFile (argc, argv);
119  SDL_Init (SDL_INIT_TIMER);
120  // initialize everything
121  printf("> Initializing resources\n");
122  if( initResources () == -1) return -1;
123
124  if( initVideo() == -1) return -1;
125  if( initSound() == -1) return -1;
126  printf("> Initializing input\n");
127  if( initInput() == -1) return -1;
128  printf("> Initializing networking\n");
129  if( initNetworking () == -1) return -1;
130  //printf("> Initializing world\n");
131  //if( init_world () == -1) return -1; PB: world will be initialized when started
132 
133  return 0;
134}
135
136/**
137   \brief initializes SDL and OpenGL
138*/
139int Orxonox::initVideo() 
140{
141  PRINTF(3)("> Initializing video\n");
142 
143  GraphicsEngine::getInstance();
144   
145  return 0;
146}
147
148
149/**
150   \brief initializes the sound engine
151*/
152int Orxonox::initSound() 
153{
154  printf("> Initializing sound\n");
155  // SDL_Init(SDL_INIT_AUDIO);
156  printf("Not yet implemented\n");
157  return 0;
158}
159
160
161/**
162   \brief initializes input functions
163*/
164int Orxonox::initInput() 
165{
166  // create localinput
167  localinput = new CommandNode(configfilename);
168
169  this->eventHandler = EventHandler::getInstance();
170  this->eventHandler->init();
171 
172  return 0;
173}
174
175
176/**
177   \brief initializes network system
178*/
179int Orxonox::initNetworking() 
180{
181  printf("Not yet implemented\n");
182  return 0;
183}
184
185
186/**
187   \brief initializes and loads resource files
188*/
189int Orxonox::initResources() 
190{
191  PRINT(3)("initializing ResourceManager\n");
192  resourceManager = ResourceManager::getInstance();
193
194  // create parser
195  IniParser parser (DEFAULT_CONFIG_FILE);
196  if( parser.getSection (CONFIG_SECTION_DATA) == -1)
197    {
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);
229      exit(-1);
230    }
231
232
233  PRINT(3)("initializing TextEngine\n");
234  TextEngine::getInstance();
235
236  PRINT(3)("initializing ObjectManager\n");
237  this->objectManager = ObjectManager::getInstance();
238
239  return 0;
240}
241
242
243/**
244   \brief initializes the world
245*/
246int Orxonox::initWorld() 
247{
248  //world = new World();
249 
250  // TO DO: replace this with a menu/intro
251  //world->load_debug_level();
252 
253  return 0;
254}
255
256
257/**
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.
264*/
265void Orxonox::start()
266{
267 
268  this->gameLoader = GameLoader::getInstance();
269  this->gameLoader->loadCampaign("worlds/DefaultCampaign.oxc");
270  //  this->gameLoader->loadDebugCampaign(DEBUG_CAMPAIGN_0);
271  this->gameLoader->init();
272  this->gameLoader->start();
273}
274
275
276/**
277   \brief exits Orxonox
278*/
279void Orxonox::quitGame() 
280{
281  bQuitOrxonox = true;
282}
283
284
285
286/**
287   \brief handles sprecial events from localinput
288   \param event: an event not handled by the CommandNode
289*/
290void Orxonox::graphicsHandler(SDL_Event* event)
291{
292  // Handle special events such as reshape, quit, focus changes
293  switch (event->type)
294    {
295    case SDL_VIDEORESIZE:
296      GraphicsEngine* tmpGEngine = GraphicsEngine::getInstance();
297      tmpGEngine->resolutionChanged(&event->resize);
298      break;
299    }
300}
301 
302
303/**
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
307*/
308bool Orxonox::systemCommand(Command* cmd)
309{
310  /*
311  if( !strcmp( cmd->cmd, "quit"))
312    {
313      if( !cmd->bUp) this->gameLoader->stop();
314      return true;
315    }
316  return false;
317  */
318  return false;
319}
320
321
322void Orxonox::process(const Event &event)
323{}
324
325/**
326   \brief retrieve a pointer to the local CommandNode
327   \return a pointer to localinput
328*/
329CommandNode* Orxonox::getLocalInput()
330{
331  return localinput;
332}
333
334
335/**
336   \brief retrieve a pointer to the local World
337   \return a pointer to world
338*/
339World* Orxonox::getWorld()
340{
341  return world;
342}
343
344/**
345   \return The reference of the SDL-screen of orxonox
346*/
347SDL_Surface* Orxonox::getScreen ()
348{
349  return this->screen;
350}
351
352
353bool showGui = false;
354
355/**
356   \brief main function
357
358   here the journey begins
359*/
360int main(int argc, char** argv) 
361{ 
362
363  // here the pre-arguments are loaded, these are needed to go either to orxonx itself, Help, or Benchmark.
364  int i;
365  for(i = 1; i < argc; ++i)
366    {
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]);
371    }
372
373  return startOrxonox(argc, argv);
374}
375
376
377
378int startHelp(int argc, char** argv)
379{
380  PRINT(0)("orxonox: starts the orxonox game - rules\n");
381  PRINT(0)("usage: orxonox [arg [arg...]]\n\n");
382  PRINT(0)("valid options:\n");
383  {
384    Gui* gui = new Gui(argc, argv);
385    gui->printHelp();
386    delete gui;
387  }
388  PRINT(0)(" -b|--benchmark:\t\tstarts the orxonox benchmark\n");
389  PRINT(0)(" -h|--help:\t\t\tshows this help\n");
390}
391
392
393int startOrxonox(int argc, char** argv)
394{
395  // checking for existence of the configuration-files
396  if (showGui ||
397      !ResourceManager::isFile("~/.orxonox/orxonox.conf") ||
398      ResourceManager::isFile("~/.orxonox/orxonox.lock"))
399    {
400      if (ResourceManager::isFile("~/.orxonox/orxonox.lock"))
401        ResourceManager::deleteFile("~/.orxonox/orxonox.lock");
402     
403      // starting the GUI
404      Gui* gui = new Gui(argc, argv);
405      gui->startGui();
406
407      if (! gui->startOrxonox)
408        return 0;
409     
410      delete gui;
411    }
412 
413  PRINT(0)(">>> Starting Orxonox <<<\n");
414
415  ResourceManager::touchFile("~/.orxonox/orxonox.lock");
416
417  Orxonox *orx = Orxonox::getInstance();
418 
419  if((*orx).init(argc, argv) == -1)
420    {
421      PRINTF(1)("! Orxonox initialization failed\n");
422      return -1;
423    }
424 
425  orx->start();
426 
427  delete orx;
428  ResourceManager::deleteFile("~/.orxonox/orxonox.lock");
429 
430}
Note: See TracBrowser for help on using the repository browser.