Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: some nicer debug information, and doxygen tags

File size: 8.8 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#include "cd_engine.h"
44
45#include "class_list.h"
46
47#include <string.h>
48
49int verbose = 4;
50
51using namespace std;
52
53/**
54   \brief create a new Orxonox
55
56   In this funcitons only global values are set. The game will not be started here.
57*/
58Orxonox::Orxonox ()
59{
60  this->setClassID(CL_ORXONOX, "Orxonox");
61  this->setName("orxonox");
62
63  this->resourceManager = NULL;
64  this->objectManager = NULL;
65  this->eventHandler = NULL;
66
67  this->argc = 0;
68  this->argv = NULL;
69}
70
71/**
72   \brief remove Orxonox from memory
73*/
74Orxonox::~Orxonox ()
75{
76  int i =0;
77  Orxonox::singletonRef = NULL;
78  delete GraphicsEngine::getInstance(); // deleting the Graphics
79  delete TextEngine::getInstance();
80  delete SoundEngine::getInstance();
81  delete ResourceManager::getInstance(); // deletes the Resource Manager
82  delete ObjectManager::getInstance();
83  delete TextEngine::getInstance();
84  delete EventHandler::getInstance();
85
86  ClassList::debug();
87}
88
89/** \brief this is a singleton class to prevent duplicates */
90Orxonox* Orxonox::singletonRef = 0;
91
92/**
93   \returns reference or new Object of Orxonox if not existent.
94*/
95Orxonox* Orxonox::getInstance ()
96{
97  if (singletonRef == NULL)
98    singletonRef = new Orxonox();
99  return singletonRef;
100}
101
102/**
103   \brief this finds the config file
104
105   Since the config file varies from user to user and since one may want to specify different config files
106   for certain occasions or platforms this function finds the right config file for every occasion and stores
107   it's path and name into configfilename
108*/
109void Orxonox::getConfigFile (int argc, char** argv)
110{
111  strcpy (configfilename, "~/.orxonox/orxonox.conf");
112}
113
114/**
115   \brief initialize Orxonox with command line
116*/
117int Orxonox::init (int argc, char** argv)
118{
119  this->argc = argc;
120  this->argv = argv;
121  // parse command line
122  // config file
123
124  getConfigFile (argc, argv);
125  SDL_Init (SDL_INIT_TIMER);
126  // initialize everything
127  printf("> Initializing resources\n");
128  if( initResources () == -1) return -1;
129
130  if( initVideo() == -1) return -1;
131  if( initSound() == -1) return -1;
132  PRINT(3)("> Initializing input\n");
133  if( initInput() == -1) return -1;
134  PRINT(3)("> Initializing networking\n");
135  if( initNetworking () == -1) return -1;
136  //printf("> Initializing world\n");
137  //if( init_world () == -1) return -1; PB: world will be initialized when started
138
139  return 0;
140}
141
142/**
143   \brief initializes SDL and OpenGL
144*/
145int Orxonox::initVideo()
146{
147  PRINTF(3)("> Initializing video\n");
148
149  GraphicsEngine::getInstance();
150  GraphicsEngine::getInstance()->setWindowName("orxonox " PACKAGE_VERSION, "orxonox " PACKAGE_VERSION);
151
152  return 0;
153}
154
155
156/**
157   \brief initializes the sound engine
158*/
159int Orxonox::initSound()
160{
161  PRINT(3)("> Initializing sound\n");
162  // SDL_Init(SDL_INIT_AUDIO);
163  SoundEngine::getInstance()->initAudio();
164  return 0;
165}
166
167
168/**
169   \brief initializes input functions
170*/
171int Orxonox::initInput()
172{
173  this->eventHandler = EventHandler::getInstance();
174  this->eventHandler->init();
175
176  return 0;
177}
178
179
180/**
181   \brief initializes network system
182*/
183int Orxonox::initNetworking()
184{
185  printf("Not yet implemented\n");
186  return 0;
187}
188
189
190/**
191   \brief initializes and loads resource files
192*/
193int Orxonox::initResources()
194{
195   PRINT(3)("initializing ResourceManager\n");
196   resourceManager = ResourceManager::getInstance();
197
198  // create parser
199   IniParser parser (DEFAULT_CONFIG_FILE);
200   if( parser.getSection (CONFIG_SECTION_DATA) == -1)
201   {
202     PRINTF(1)("Could not find Section %s in %s\n", CONFIG_SECTION_DATA, DEFAULT_CONFIG_FILE);
203     return -1;
204   }
205   char namebuf[256];
206   char valuebuf[256];
207   memset (namebuf, 0, 256);
208   memset (valuebuf, 0, 256);
209
210   while( parser.nextVar (namebuf, valuebuf) != -1)
211   {
212     if (!strcmp(namebuf, CONFIG_NAME_DATADIR))
213     {
214          //  printf("Not yet implemented\n");
215       if (!resourceManager->setDataDir(valuebuf))
216       {
217         PRINTF(1)("Data Could not be located\n");
218         exit(-1);
219       }
220     }
221
222     memset (namebuf, 0, 256);
223     memset (valuebuf, 0, 256);
224   }
225
226   if (!resourceManager->checkDataDir(DEFAULT_DATA_DIR_CHECKFILE))
227   {
228     PRINTF(1)("The DataDirectory %s could not be verified\nPlease Change in File %s Section %s Entry %s to a suitable value\n",
229     resourceManager->getDataDir(),
230     DEFAULT_CONFIG_FILE,
231     CONFIG_SECTION_DATA,
232     CONFIG_NAME_DATADIR);
233     exit(-1);
234   }
235   //! \todo this is a hack and should be loadable
236   resourceManager->addImageDir(ResourceManager::getInstance()->getFullName("maps/"));
237   resourceManager->debug();
238
239   PRINT(3)("initializing TextEngine\n");
240   TextEngine::getInstance();
241
242   PRINT(3)("initializing ObjectManager\n");
243   this->objectManager = ObjectManager::getInstance();
244
245   CDEngine::getInstance();
246
247   return 0;
248}
249
250
251
252
253/**
254   \brief starts the orxonox game or menu
255
256   here is the central orxonox state manager. There are currently two states
257   - menu
258   - game-play
259   both states manage their states themselfs again.
260*/
261void Orxonox::start()
262{
263
264  this->gameLoader = GameLoader::getInstance();
265  this->gameLoader->loadCampaign("worlds/DefaultCampaign.oxc");
266  //  this->gameLoader->loadDebugCampaign(DEBUG_CAMPAIGN_0);
267  this->gameLoader->init();
268  this->gameLoader->start();
269}
270
271
272/**
273   \brief handles sprecial events from localinput
274   \param event: an event not handled by the CommandNode
275*/
276void Orxonox::graphicsHandler(SDL_Event* event)
277{
278  // Handle special events such as reshape, quit, focus changes
279  switch (event->type)
280    {
281    case SDL_VIDEORESIZE:
282      GraphicsEngine* tmpGEngine = GraphicsEngine::getInstance();
283      tmpGEngine->resolutionChanged(&event->resize);
284      break;
285    }
286}
287
288
289/**
290  \brief processes the events for orxonox main class
291  \param the event to handle
292*/
293void Orxonox::process(const Event &event)
294{}
295
296
297
298bool showGui = false;
299
300/**
301   \brief main function
302
303   here the journey begins
304*/
305int main(int argc, char** argv)
306{
307
308  // here the pre-arguments are loaded, these are needed to go either to orxonx itself, Help, or Benchmark.
309  int i;
310  for(i = 1; i < argc; ++i)
311    {
312      if(! strcmp( "--help", argv[i]) || !strcmp("-h", argv[i])) return startHelp(argc, argv);
313      else if(!strcmp( "--benchmark", argv[i]) || !strcmp("-b", argv[i])) return startBenchmarks();
314      else if(!strcmp( "--gui", argv[i]) || !strcmp("-g", argv[i])) showGui = true;
315      //      else PRINTF(2)("Orxonox does not understand the arguments %s\n", argv[i]);
316    }
317
318  return startOrxonox(argc, argv);
319}
320
321
322
323int startHelp(int argc, char** argv)
324{
325  PRINT(0)("orxonox: starts the orxonox game - rules\n");
326  PRINT(0)("usage: orxonox [arg [arg...]]\n\n");
327  PRINT(0)("valid options:\n");
328  {
329    Gui* gui = new Gui(argc, argv);
330    gui->printHelp();
331    delete gui;
332  }
333  PRINT(0)(" -b|--benchmark:\t\tstarts the orxonox benchmark\n");
334  PRINT(0)(" -h|--help:\t\t\tshows this help\n");
335}
336
337
338int startOrxonox(int argc, char** argv)
339{
340  // checking for existence of the configuration-files
341  if (showGui ||
342      !ResourceManager::isFile("~/.orxonox/orxonox.conf") ||
343      ResourceManager::isFile("~/.orxonox/orxonox.lock"))
344    {
345      if (ResourceManager::isFile("~/.orxonox/orxonox.lock"))
346        ResourceManager::deleteFile("~/.orxonox/orxonox.lock");
347
348      // starting the GUI
349      Gui* gui = new Gui(argc, argv);
350      gui->startGui();
351
352      if (! gui->startOrxonox)
353        return 0;
354
355      delete gui;
356    }
357
358  PRINT(0)(">>> Starting Orxonox <<<\n");
359
360  ResourceManager::touchFile("~/.orxonox/orxonox.lock");
361
362  Orxonox *orx = Orxonox::getInstance();
363
364  if((*orx).init(argc, argv) == -1)
365    {
366      PRINTF(1)("! Orxonox initialization failed\n");
367      return -1;
368    }
369
370  orx->start();
371
372  delete orx;
373  ResourceManager::deleteFile("~/.orxonox/orxonox.lock");
374
375}
Note: See TracBrowser for help on using the repository browser.