Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4827 was 4827, checked in by bensch, 20 years ago

orxonox/trunk: state is no more singleton, but static class.
This is faster, and easier to handle, because one can just say State::getBLA() and not State::getInstance()→getBLA(); ——→>> less redundant

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