Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/openAL/src/orxonox.cc @ 4209

Last change on this file since 4209 was 4209, checked in by bensch, 19 years ago

orxonox/branches/openAL: initAudio saver now

File size: 9.2 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 "data_tank.h"
33#include "command_node.h"
34#include "ini_parser.h"
35#include "game_loader.h"
36#include "graphics_engine.h"
37#include "sound_engine.h"
38#include "resource_manager.h"
39#include "text_engine.h"
40#include "factory.h"
41#include "benchmark.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->resources = NULL;
60  this->localinput = NULL;
61
62  this->argc = 0;
63  this->argv = NULL;
64}
65
66/**
67   \brief remove Orxonox from memory
68*/
69Orxonox::~Orxonox () 
70{
71  int i =0;
72  Orxonox::singletonRef = NULL;
73  if( world != NULL) delete world;
74  if( localinput != NULL) delete localinput;
75  if( resources != NULL) delete resources;
76  delete GraphicsEngine::getInstance(); // deleting the Graphics
77  delete TextEngine::getInstance();
78  delete SoundEngine::getInstance();
79  delete ResourceManager::getInstance(); // deletes the Resource Manager
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  PRINT(3)("> Initializing input\n");
126  if( initInput() == -1) return -1;
127  PRINT(3)("> 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  PRINT(3)("> Initializing sound\n");
154  // SDL_Init(SDL_INIT_AUDIO);
155  SoundEngine::getInstance()->initAudio();
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  return 0;
169}
170
171
172/**
173   \brief initializes network system
174*/
175int Orxonox::initNetworking() 
176{
177  printf("Not yet implemented\n");
178  return 0;
179}
180
181
182/**
183   \brief initializes and loads resource files
184*/
185int Orxonox::initResources() 
186{
187  PRINT(3)("initializing ResourceManager\n");
188  resourceManager = ResourceManager::getInstance();
189
190  // create parser
191  IniParser parser (DEFAULT_CONFIG_FILE);
192  if( parser.getSection (CONFIG_SECTION_DATA) == -1)
193    {
194      PRINTF(1)("Could not find Section %s in %s\n", CONFIG_SECTION_DATA, DEFAULT_CONFIG_FILE);
195      return -1;
196    }
197  char namebuf[256];
198  char valuebuf[256];
199  memset (namebuf, 0, 256);
200  memset (valuebuf, 0, 256);
201 
202  while( parser.nextVar (namebuf, valuebuf) != -1)
203    {
204      if (!strcmp(namebuf, CONFIG_NAME_DATADIR))
205        {
206          //  printf("Not yet implemented\n");
207          if (!resourceManager->setDataDir(valuebuf))
208            {
209              PRINTF(1)("Data Could not be located\n");
210              exit(-1);
211            }
212        }
213     
214      memset (namebuf, 0, 256);
215      memset (valuebuf, 0, 256);
216    }
217 
218  if (!resourceManager->checkDataDir(DEFAULT_DATA_DIR_CHECKFILE))
219    {
220      PRINTF(1)("The DataDirectory %s could not be verified\nPlease Change in File %s Section %s Entry %s to a suitable value\n",
221                resourceManager->getDataDir(),
222                DEFAULT_CONFIG_FILE,
223                CONFIG_SECTION_DATA,
224                CONFIG_NAME_DATADIR);
225      exit(-1);
226    }
227
228
229  PRINT(3)("initializing TextEngine\n");
230  TextEngine::getInstance();
231
232  return 0;
233}
234
235
236/**
237   \brief initializes the world
238*/
239int Orxonox::initWorld() 
240{
241  //world = new World();
242 
243  // TO DO: replace this with a menu/intro
244  //world->load_debug_level();
245 
246  return 0;
247}
248
249
250/**
251   \brief starts the orxonox game or menu
252
253   here is the central orxonox state manager. There are currently two states
254   - menu
255   - game-play
256   both states manage their states themselfs again.
257*/
258void Orxonox::start()
259{
260 
261  this->gameLoader = GameLoader::getInstance();
262  this->gameLoader->loadCampaign("worlds/DefaultCampaign.oxc");
263  //  this->gameLoader->loadDebugCampaign(DEBUG_CAMPAIGN_0);
264  this->gameLoader->init();
265  this->gameLoader->start();
266}
267
268
269/**
270   \brief exits Orxonox
271*/
272void Orxonox::quitGame() 
273{
274  bQuitOrxonox = true;
275}
276
277
278
279/**
280   \brief handles sprecial events from localinput
281   \param event: an event not handled by the CommandNode
282*/
283void Orxonox::eventHandler(SDL_Event* event)
284{
285  // Handle special events such as reshape, quit, focus changes
286  switch (event->type)
287    {
288    case SDL_VIDEORESIZE:
289      GraphicsEngine* tmpGEngine = GraphicsEngine::getInstance();
290      tmpGEngine->resolutionChanged(&event->resize);
291      break;
292    }
293}
294 
295
296/**
297   \brief handle keyboard commands that are not meant for WorldEntities
298   \param cmd: the command to handle
299   \return true if the command was handled by the system or false if it may be passed to the WorldEntities
300*/
301bool Orxonox::systemCommand(Command* cmd)
302{
303  /*
304  if( !strcmp( cmd->cmd, "quit"))
305    {
306      if( !cmd->bUp) this->gameLoader->stop();
307      return true;
308    }
309  return false;
310  */
311  return false;
312}
313
314/**
315   \brief retrieve a pointer to the local CommandNode
316   \return a pointer to localinput
317*/
318CommandNode* Orxonox::getLocalInput()
319{
320  return localinput;
321}
322
323
324/**
325   \brief retrieve a pointer to the local World
326   \return a pointer to world
327*/
328World* Orxonox::getWorld()
329{
330  return world;
331}
332
333/**
334   \return The reference of the SDL-screen of orxonox
335*/
336SDL_Surface* Orxonox::getScreen ()
337{
338  return this->screen;
339}
340
341
342bool showGui = false;
343
344/**
345   \brief main function
346
347   here the journey begins
348*/
349int main(int argc, char** argv) 
350{ 
351
352  // here the pre-arguments are loaded, these are needed to go either to orxonx itself, Help, or Benchmark.
353  int i;
354  for(i = 1; i < argc; ++i)
355    {
356      if(! strcmp( "--help", argv[i]) || !strcmp("-h", argv[i])) return startHelp(argc, argv);
357      else if(!strcmp( "--benchmark", argv[i]) || !strcmp("-b", argv[i])) return startBenchmarks();
358      else if(!strcmp( "--gui", argv[i]) || !strcmp("-g", argv[i])) showGui = true;
359      //      else PRINTF(2)("Orxonox does not understand the arguments %s\n", argv[i]);
360    }
361
362  return startOrxonox(argc, argv);
363}
364
365
366
367int startHelp(int argc, char** argv)
368{
369  PRINT(0)("orxonox: starts the orxonox game - rules\n");
370  PRINT(0)("usage: orxonox [arg [arg...]]\n\n");
371  PRINT(0)("valid options:\n");
372  {
373    Gui* gui = new Gui(argc, argv);
374    gui->printHelp();
375    delete gui;
376  }
377  PRINT(0)(" -b|--benchmark:\t\tstarts the orxonox benchmark\n");
378  PRINT(0)(" -h|--help:\t\t\tshows this help\n");
379}
380
381
382int startOrxonox(int argc, char** argv)
383{
384  // checking for existence of the configuration-files
385  if (showGui ||
386      !ResourceManager::isFile("~/.orxonox/orxonox.conf") ||
387      ResourceManager::isFile("~/.orxonox/orxonox.lock"))
388    {
389      if (ResourceManager::isFile("~/.orxonox/orxonox.lock"))
390        ResourceManager::deleteFile("~/.orxonox/orxonox.lock");
391     
392      // starting the GUI
393      Gui* gui = new Gui(argc, argv);
394      gui->startGui();
395
396      if (! gui->startOrxonox)
397        return 0;
398     
399      delete gui;
400    }
401 
402  PRINT(0)(">>> Starting Orxonox <<<\n");
403
404  ResourceManager::touchFile("~/.orxonox/orxonox.lock");
405
406  Orxonox *orx = Orxonox::getInstance();
407 
408  if((*orx).init(argc, argv) == -1)
409    {
410      PRINTF(1)("! Orxonox initialization failed\n");
411      return -1;
412    }
413 
414  orx->start();
415 
416  delete orx;
417  ResourceManager::deleteFile("~/.orxonox/orxonox.lock");
418 
419}
Note: See TracBrowser for help on using the repository browser.