Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: moved around some files, and deleted the obsolete DataTank

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