Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: derivation of superclasses is more clear now. lets see, if this will hold out for some time (about 115 classes long)

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