Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: player should be able to load its preferences from an external file
removed the obsolete ObjectManager that has been redesigned as FastFactory.

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 "cd_engine.h"
40#include "text_engine.h"
41#include "event_handler.h"
42#include "garbage_collector.h"
43
44#include "factory.h"
45#include "benchmark.h"
46
47#include "class_list.h"
48#include "substring.h"
49
50#include <string.h>
51
52int verbose = 4;
53
54using namespace std;
55
56/**
57 *  create a new Orxonox
58
59   In this funcitons only global values are set. The game will not be started here.
60*/
61Orxonox::Orxonox ()
62{
63  this->setClassID(CL_ORXONOX, "Orxonox");
64  this->setName("orxonox-main");
65
66  this->iniParser = NULL;
67
68  this->argc = 0;
69  this->argv = NULL;
70
71  this->configFileName = NULL;
72}
73
74/**
75 *  remove Orxonox from memory
76*/
77Orxonox::~Orxonox ()
78{
79  delete this->iniParser;
80  delete this->configFileName;
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 TextEngine::getInstance();
87  delete Factory::getFirst();
88  delete GameLoader::getInstance();
89  delete SoundEngine::getInstance();
90  delete CDEngine::getInstance();
91  delete GarbageCollector::getInstance();
92
93  delete EventHandler::getInstance();
94
95  ClassList::debug();
96
97  PRINT(3)
98      ("===================================================\n" \
99      "Thanks for playing orxonox.\n" \
100      "visit: http://www.orxonox.ethz.ch for new versions.\n" \
101      "===================================================\n" \
102      ORXONOX_LICENSE_SHORT);
103
104  Orxonox::singletonRef = NULL;
105}
106
107/**
108 *  this is a singleton class to prevent duplicates
109 */
110Orxonox* Orxonox::singletonRef = NULL;
111
112/**
113 *  this finds the config file
114 * @returns the new config-fileName
115 * Since the config file varies from user to user and since one may want to specify different config files
116 * for certain occasions or platforms this function finds the right config file for every occasion and stores
117 * it's path and name into configfilename
118*/
119const char* Orxonox::getConfigFile ()
120{
121  this->configFileName = new char[strlen(DEFAULT_CONFIG_FILE)];
122  strcpy (this->configFileName, DEFAULT_CONFIG_FILE);
123  this->iniParser = new IniParser(this->configFileName);
124}
125
126/**
127 * 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();
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 * 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 * 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 * initializes input functions
180 */
181int Orxonox::initInput()
182{
183  PRINT(3)("> Initializing input\n");
184
185  EventHandler::getInstance()->init(this->iniParser);
186  EventHandler::getInstance()->subscribe(GraphicsEngine::getInstance(), ES_ALL, EV_VIDEO_RESIZE);
187
188
189  return 0;
190}
191
192
193/**
194 * initializes network system
195 */
196int Orxonox::initNetworking()
197{
198  PRINT(3)("> Initializing networking\n");
199
200  printf("  ---Not yet implemented-FIXME--\n");
201  return 0;
202}
203
204
205/**
206 * initializes and loads resource files
207 */
208int Orxonox::initResources()
209{
210  PRINTF(3)("> Initializing resources\n");
211
212  PRINT(3)("initializing ResourceManager\n");
213
214  // create parser
215  if( this->iniParser->getSection (CONFIG_SECTION_DATA) == -1)
216  {
217    PRINTF(1)("Could not find Section %s in %s\n", CONFIG_SECTION_DATA, DEFAULT_CONFIG_FILE);
218    return -1;
219  }
220  char namebuf[256];
221  char valuebuf[256];
222  memset (namebuf, 0, 256);
223  memset (valuebuf, 0, 256);
224
225  while( this->iniParser->nextVar (namebuf, valuebuf) != -1)
226  {
227    if (!strcmp(namebuf, CONFIG_NAME_DATADIR))
228    {
229          //  printf("Not yet implemented\n");
230      if (!ResourceManager::getInstance()->setDataDir(valuebuf))
231      {
232        PRINTF(1)("Data Could not be located\n");
233        exit(-1);
234      }
235    }
236
237    memset (namebuf, 0, 256);
238    memset (valuebuf, 0, 256);
239  }
240
241  if (!ResourceManager::getInstance()->checkDataDir(DEFAULT_DATA_DIR_CHECKFILE))
242  {
243    PRINTF(1)("The DataDirectory %s could not be verified\n" \
244              "  Please Change in File %s Section %s Entry %s to a suitable value\n",
245              ResourceManager::getInstance()->getDataDir(),
246              DEFAULT_CONFIG_FILE,
247              CONFIG_SECTION_DATA,
248              CONFIG_NAME_DATADIR);
249    exit(-1);
250  }
251   //! @todo this is a hack and should be loadable
252  ResourceManager::getInstance()->addImageDir(ResourceManager::getInstance()->getFullName("maps/"));
253  ResourceManager::getInstance()->debug();
254
255  PRINT(3)("initializing TextEngine\n");
256  TextEngine::getInstance();
257
258  CDEngine::getInstance();
259
260  return 0;
261}
262
263/**
264 *  starts the orxonox game or menu
265 * here is the central orxonox state manager. There are currently two states
266 * - menu
267 * - game-play
268 * both states manage their states themselfs again.
269*/
270void Orxonox::start()
271{
272
273  this->gameLoader = GameLoader::getInstance();
274  this->gameLoader->loadCampaign("worlds/DefaultCampaign.oxc");
275  //  this->gameLoader->loadDebugCampaign(DEBUG_CAMPAIGN_0);
276  this->gameLoader->init();
277  this->gameLoader->start();
278}
279
280
281/**
282 * handles sprecial events from localinput
283 * @param event: an event not handled by the CommandNode
284 */
285// void Orxonox::graphicsHandler(SDL_Event* event)
286// {
287//   // Handle special events such as reshape, quit, focus changes
288//   switch (event->type)
289//     {
290//     case SDL_VIDEORESIZE:
291//       GraphicsEngine* tmpGEngine = GraphicsEngine::getInstance();
292//       tmpGEngine->resolutionChanged(event->resize);
293//       break;
294//     }
295// }
296
297
298
299
300
301
302bool showGui = false;
303
304
305
306/**********************************
307*** ORXONOX MAIN STARTING POINT ***
308**********************************/
309/**
310 *
311 *  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, or if the lock file is still used
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.