Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: merged the SoundEngine into the Trunk.
merged file by file, but merged all the files in favor of the new trunk

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