Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: now there is the ability to Fly in fullscreen-mode

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