Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: orxonox-events registered, solved an error in ClassList

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