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, 19 years ago

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

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