Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Singleton-Classes are now refered to as such in World and Orxonox

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