Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: setClassID implemented in all files

File size: 8.5 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 "ini_parser.h"
33#include "game_loader.h"
34#include "graphics_engine.h"
35#include "sound_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->setClassID(CL_ORXONOX, "Orxonox");
58  this->setName("orxonox");
59
60  this->resourceManager = NULL;
61  this->objectManager = NULL;
62  this->eventHandler = 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  delete GraphicsEngine::getInstance(); // deleting the Graphics
76  delete TextEngine::getInstance();
77  delete SoundEngine::getInstance();
78  delete ResourceManager::getInstance(); // deletes the Resource Manager
79  delete ObjectManager::getInstance();
80  delete TextEngine::getInstance();
81  delete EventHandler::getInstance();
82}
83
84/** \brief this is a singleton class to prevent duplicates */
85Orxonox* Orxonox::singletonRef = 0;
86
87/**
88   \returns reference or new Object of Orxonox if not existent.
89*/
90Orxonox* Orxonox::getInstance (void)
91{
92  if (singletonRef == NULL)
93    singletonRef = new Orxonox();
94  return singletonRef;
95}
96
97/**
98   \brief this finds the config file
99
100   Since the config file varies from user to user and since one may want to specify different config files
101   for certain occasions or platforms this function finds the right config file for every occasion and stores
102   it's path and name into configfilename
103*/
104void Orxonox::getConfigFile (int argc, char** argv)
105{
106  strcpy (configfilename, "~/.orxonox/orxonox.conf");
107}
108
109/**
110   \brief initialize Orxonox with command line
111*/
112int Orxonox::init (int argc, char** argv)
113{
114  this->argc = argc;
115  this->argv = argv;
116  // parse command line
117  // config file
118
119  getConfigFile (argc, argv);
120  SDL_Init (SDL_INIT_TIMER);
121  // initialize everything
122  printf("> Initializing resources\n");
123  if( initResources () == -1) return -1;
124
125  if( initVideo() == -1) return -1;
126  if( initSound() == -1) return -1;
127  PRINT(3)("> Initializing input\n");
128  if( initInput() == -1) return -1;
129  PRINT(3)("> Initializing networking\n");
130  if( initNetworking () == -1) return -1;
131  //printf("> Initializing world\n");
132  //if( init_world () == -1) return -1; PB: world will be initialized when started
133
134  return 0;
135}
136
137/**
138   \brief initializes SDL and OpenGL
139*/
140int Orxonox::initVideo()
141{
142  PRINTF(3)("> Initializing video\n");
143
144  GraphicsEngine::getInstance();
145
146  return 0;
147}
148
149
150/**
151   \brief initializes the sound engine
152*/
153int Orxonox::initSound()
154{
155  PRINT(3)("> Initializing sound\n");
156  // SDL_Init(SDL_INIT_AUDIO);
157  SoundEngine::getInstance()->initAudio();
158  return 0;
159}
160
161
162/**
163   \brief initializes input functions
164*/
165int Orxonox::initInput()
166{
167  this->eventHandler = EventHandler::getInstance();
168  this->eventHandler->init();
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
243/**
244   \brief starts the orxonox game or menu
245
246   here is the central orxonox state manager. There are currently two states
247   - menu
248   - game-play
249   both states manage their states themselfs again.
250*/
251void Orxonox::start()
252{
253
254  this->gameLoader = GameLoader::getInstance();
255  this->gameLoader->loadCampaign("worlds/DefaultCampaign.oxc");
256  //  this->gameLoader->loadDebugCampaign(DEBUG_CAMPAIGN_0);
257  this->gameLoader->init();
258  this->gameLoader->start();
259}
260
261
262/**
263   \brief handles sprecial events from localinput
264   \param event: an event not handled by the CommandNode
265*/
266void Orxonox::graphicsHandler(SDL_Event* event)
267{
268  // Handle special events such as reshape, quit, focus changes
269  switch (event->type)
270    {
271    case SDL_VIDEORESIZE:
272      GraphicsEngine* tmpGEngine = GraphicsEngine::getInstance();
273      tmpGEngine->resolutionChanged(&event->resize);
274      break;
275    }
276}
277
278
279/**
280  \brief processes the events for orxonox main class
281  \param the event to handle
282*/
283void Orxonox::process(const Event &event)
284{}
285
286
287
288bool showGui = false;
289
290/**
291   \brief main function
292
293   here the journey begins
294*/
295int main(int argc, char** argv)
296{
297
298  // here the pre-arguments are loaded, these are needed to go either to orxonx itself, Help, or Benchmark.
299  int i;
300  for(i = 1; i < argc; ++i)
301    {
302      if(! strcmp( "--help", argv[i]) || !strcmp("-h", argv[i])) return startHelp(argc, argv);
303      else if(!strcmp( "--benchmark", argv[i]) || !strcmp("-b", argv[i])) return startBenchmarks();
304      else if(!strcmp( "--gui", argv[i]) || !strcmp("-g", argv[i])) showGui = true;
305      //      else PRINTF(2)("Orxonox does not understand the arguments %s\n", argv[i]);
306    }
307
308  return startOrxonox(argc, argv);
309}
310
311
312
313int startHelp(int argc, char** argv)
314{
315  PRINT(0)("orxonox: starts the orxonox game - rules\n");
316  PRINT(0)("usage: orxonox [arg [arg...]]\n\n");
317  PRINT(0)("valid options:\n");
318  {
319    Gui* gui = new Gui(argc, argv);
320    gui->printHelp();
321    delete gui;
322  }
323  PRINT(0)(" -b|--benchmark:\t\tstarts the orxonox benchmark\n");
324  PRINT(0)(" -h|--help:\t\t\tshows this help\n");
325}
326
327
328int startOrxonox(int argc, char** argv)
329{
330  // checking for existence of the configuration-files
331  if (showGui ||
332      !ResourceManager::isFile("~/.orxonox/orxonox.conf") ||
333      ResourceManager::isFile("~/.orxonox/orxonox.lock"))
334    {
335      if (ResourceManager::isFile("~/.orxonox/orxonox.lock"))
336        ResourceManager::deleteFile("~/.orxonox/orxonox.lock");
337
338      // starting the GUI
339      Gui* gui = new Gui(argc, argv);
340      gui->startGui();
341
342      if (! gui->startOrxonox)
343        return 0;
344
345      delete gui;
346    }
347
348  PRINT(0)(">>> Starting Orxonox <<<\n");
349
350  ResourceManager::touchFile("~/.orxonox/orxonox.lock");
351
352  Orxonox *orx = Orxonox::getInstance();
353
354  if((*orx).init(argc, argv) == -1)
355    {
356      PRINTF(1)("! Orxonox initialization failed\n");
357      return -1;
358    }
359
360  orx->start();
361
362  delete orx;
363  ResourceManager::deleteFile("~/.orxonox/orxonox.lock");
364
365}
Note: See TracBrowser for help on using the repository browser.