Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/spaceshipcontrol/src/orxonox.cc @ 6036

Last change on this file since 6036 was 5996, checked in by patrick, 19 years ago

orxonox/trunk: merged network branche into trunk with command svn merge -r 5824:HEAD

File size: 13.1 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#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ORXONOX
28#include "orxonox.h"
29
30#include "globals.h"
31
32#include "gui.h"
33
34#include "world.h"
35#include "parser/ini_parser/ini_parser.h"
36#include "game_loader.h"
37
38//ENGINES
39#include "graphics_engine.h"
40#include "sound_engine.h"
41#include "resource_manager.h"
42#include "cd_engine.h"
43#include "text_engine.h"
44#include "event_handler.h"
45#include "garbage_collector.h"
46
47#include "factory.h"
48#include "fast_factory.h"
49
50#include "benchmark.h"
51
52#include "class_list.h"
53#include "shell_command_class.h"
54#include "shell_command.h"
55#include "shell_buffer.h"
56
57#include "load_param_description.h"
58
59#include "network_manager.h"
60
61#include <string.h>
62
63int verbose = 4;
64
65using namespace std;
66
67SHELL_COMMAND(restart, Orxonox, restart);
68
69/**
70 *  create a new Orxonox
71
72   In this funcitons only global values are set. The game will not be started here.
73*/
74Orxonox::Orxonox ()
75{
76  this->setClassID(CL_ORXONOX, "Orxonox");
77  this->setName("orxonox-main");
78
79  this->iniParser = NULL;
80
81  this->argc = 0;
82  this->argv = NULL;
83
84  /* this way, there is no network enabled: */
85  this->serverName = NULL;
86  this->port = -1;
87
88  this->configFileName = NULL;
89}
90
91/**
92 *  remove Orxonox from memory
93*/
94Orxonox::~Orxonox ()
95{
96  // game-specific
97  delete GameLoader::getInstance();
98  delete GarbageCollector::getInstance();
99
100  // class-less services/factories
101  Factory::deleteFactories();
102  FastFactory::deleteAll();
103  ShellCommandClass::unregisterAllCommands();
104
105  LoadClassDescription::deleteAllDescriptions();
106
107  // engines
108  delete CDEngine::getInstance();
109  delete SoundEngine::getInstance();
110  delete GraphicsEngine::getInstance(); // deleting the Graphics
111  delete EventHandler::getInstance();
112
113  // handlers
114  delete ResourceManager::getInstance(); // deletes the Resource Manager
115  // output-buffer
116  delete ShellBuffer::getInstance();
117
118  // orxonox class-stuff
119  delete this->iniParser;
120  delete[] this->configFileName;
121
122  SDL_QuitSubSystem(SDL_INIT_TIMER);
123  ClassList::debug();
124
125  PRINT(3)
126  (
127    "===================================================\n" \
128    "Thanks for playing orxonox.\n" \
129    "visit: http://www.orxonox.net for new versions.\n" \
130    "===================================================\n" \
131    ORXONOX_LICENSE_SHORT
132  );
133
134  Orxonox::singletonRef = NULL;
135}
136
137/**
138 *  this is a singleton class to prevent duplicates
139 */
140Orxonox* Orxonox::singletonRef = NULL;
141
142// DANGEROUS
143void Orxonox::restart()
144{
145  //   int argc = this->argc;
146  //   char** argv = this->argv;
147  //
148  //   Orxonox *orx = Orxonox::getInstance();
149  //
150  //   delete orx;
151  //
152  //   orx = Orxonox::getInstance();
153  //
154  //   if((*orx).init(argc, argv) == -1)
155  //   {
156  //     PRINTF(1)("! Orxonox initialization failed\n");
157  //     return;
158  //   }
159  //
160  //   printf("finished inizialisation\n");
161  //   orx->start();
162}
163
164/**
165 *  this finds the config file
166 * @returns the new config-fileName
167 * Since the config file varies from user to user and since one may want to specify different config files
168 * for certain occasions or platforms this function finds the right config file for every occasion and stores
169 * it's path and name into configfilename
170*/
171const char* Orxonox::getConfigFile ()
172{
173  if (ResourceManager::isFile("orxonox.conf"))
174  {
175    this->configFileName = new char[strlen("orxonox.conf")+1];
176    strcpy(this->configFileName, "orxonox.conf");
177  }
178  else
179    this->configFileName = ResourceManager::homeDirCheck(DEFAULT_CONFIG_FILE);
180  this->iniParser = new IniParser(this->configFileName);
181  PRINTF(3)("Parsed Config File: '%s'\n", this->configFileName);
182}
183
184/**
185 * initialize Orxonox with command line
186 */
187int Orxonox::init (int argc, char** argv, const char* name, int port)
188{
189  this->argc = argc;
190  this->argv = argv;
191
192  this->serverName = name;
193  this->port = port;
194
195  // initialize the Config-file
196  this->getConfigFile();
197
198  // windows must not write into stdout.txt and stderr.txt
199#ifdef __WIN32__
200  freopen( "CON", "w", stdout );
201  freopen( "CON", "w", stderr );
202#endif
203
204  // initialize everything
205  SDL_Init(SDL_INIT_TIMER);
206  if( initResources () == -1)
207    return -1;
208  if( initVideo() == -1)
209    return -1;
210  if( initSound() == -1)
211    return -1;
212  if( initInput() == -1)
213    return -1;
214  if( initNetworking () == -1)
215    return -1;
216  if( initMisc () == -1)
217    return -1;
218
219  return 0;
220}
221
222
223/**
224 * initializes SDL and OpenGL
225 */
226int Orxonox::initVideo()
227{
228  PRINTF(3)("> Initializing video\n");
229
230  GraphicsEngine::getInstance();
231
232  GraphicsEngine::getInstance()->initFromIniFile(this->iniParser);
233
234  char* iconName = ResourceManager::getFullName("pictures/fighter-top-32x32.bmp");
235  if (iconName != NULL)
236  {
237    GraphicsEngine::getInstance()->setWindowName(PACKAGE_NAME " " PACKAGE_VERSION, iconName);
238    delete[] iconName;
239  }
240  return 0;
241}
242
243
244/**
245 * initializes the sound engine
246 */
247int Orxonox::initSound()
248{
249  PRINT(3)("> Initializing sound\n");
250  // SDL_InitSubSystem(SDL_INIT_AUDIO);
251  SoundEngine::getInstance()->initAudio();
252  SoundEngine::getInstance()->allocateSources(1);
253
254  SoundEngine::getInstance()->loadSettings(this->iniParser);
255  return 0;
256}
257
258
259/**
260 * initializes input functions
261 */
262int Orxonox::initInput()
263{
264  PRINT(3)("> Initializing input\n");
265
266  EventHandler::getInstance()->init(this->iniParser);
267  EventHandler::getInstance()->subscribe(GraphicsEngine::getInstance(), ES_ALL, EV_VIDEO_RESIZE);
268
269  return 0;
270}
271
272
273/**
274 * initializes network system
275 */
276int Orxonox::initNetworking()
277{
278  PRINT(3)("> Initializing networking\n");
279
280  if( this->serverName != NULL) // we are a client
281    NetworkManager::getInstance()->establishConnection(this->serverName, port);
282  else if( this->port > 0)      // we are a server
283    NetworkManager::getInstance()->createServer(port);
284
285  return 0;
286}
287
288
289/**
290 * initializes and loads resource files
291 */
292int Orxonox::initResources()
293{
294  PRINTF(3)("> Initializing resources\n");
295
296  PRINT(3)("initializing ResourceManager\n");
297
298  // init the resource manager
299  const char* dataPath;
300  if ((dataPath = this->iniParser->getVar(CONFIG_NAME_DATADIR, CONFIG_SECTION_DATA))!= NULL)
301  {
302    if (!ResourceManager::getInstance()->setDataDir(dataPath) &&
303        !ResourceManager::getInstance()->verifyDataDir(DEFAULT_DATA_DIR_CHECKFILE))
304    {
305      PRINTF(1)("Data Could not be located in %s\n", dataPath);
306    }
307  }
308
309  if (!ResourceManager::getInstance()->verifyDataDir(DEFAULT_DATA_DIR_CHECKFILE))
310  {
311    PRINTF(1)("The DataDirectory %s could not be verified\n\nh" \
312              "!!!  Please Change in File %s Section %s Entry %s to a suitable value !!!\n",
313              ResourceManager::getInstance()->getDataDir(),
314              this->configFileName,
315              CONFIG_SECTION_DATA,
316              CONFIG_NAME_DATADIR );
317    Gui* gui = new Gui(argc, argv);
318    gui->startGui();
319    delete gui;
320    exit(-1);
321  }
322  //! @todo this is a hack and should be loadable
323  char* imageDir = ResourceManager::getInstance()->getFullName("maps");
324  ResourceManager::getInstance()->addImageDir(imageDir);
325  delete[] imageDir;
326
327  // start the collision detection engine
328  CDEngine::getInstance();
329  return 0;
330}
331
332/**
333 * initializes miscelaneous features
334 * @return -1 on failure
335 */
336int Orxonox::initMisc()
337{
338  ShellBuffer::getInstance();
339  return 0;
340}
341
342/**
343 *  starts the orxonox game or menu
344 * here is the central orxonox state manager. There are currently two states
345 * - menu
346 * - game-play
347 * both states manage their states themselfs again.
348*/
349void Orxonox::start()
350{
351
352  this->gameLoader = GameLoader::getInstance();
353
354  if( this->port != -1 || this->serverName != NULL)
355    this->gameLoader->loadCampaign("worlds/DefaultCampaign.oxc"); /* actualy loadNetworkCampaign*/
356  else
357    this->gameLoader->loadCampaign("worlds/DefaultCampaign.oxc");
358
359  //  this->gameLoader->loadDebugCampaign(DEBUG_CAMPAIGN_0);
360  this->gameLoader->init();
361  this->gameLoader->start();
362}
363
364
365/**
366 * handles sprecial events from localinput
367 * @param event: an event not handled by the CommandNode
368 */
369// void Orxonox::graphicsHandler(SDL_Event* event)
370// {
371//   // Handle special events such as reshape, quit, focus changes
372//   switch (event->type)
373//     {
374//     case SDL_VIDEORESIZE:
375//       GraphicsEngine* tmpGEngine = GraphicsEngine::getInstance();
376//       tmpGEngine->resolutionChanged(event->resize);
377//       break;
378//     }
379// }
380
381
382
383
384
385
386bool showGui = false;
387
388
389
390/**********************************
391*** ORXONOX MAIN STARTING POINT ***
392**********************************/
393/**
394 *
395 *  main function
396 *
397 * here the journey begins
398*/
399int main(int argc, char** argv)
400{
401  int i;
402  for(i = 1; i < argc; ++i)
403  {
404    if(     !strcmp( "--help", argv[i])     || !strcmp("-h", argv[i]))
405      return showHelp(argc, argv);
406    else if(!strcmp( "--gui", argv[i])      || !strcmp("-g", argv[i]))
407      showGui = true;
408    else if(!strcmp( "--client", argv[i])   || !strcmp("-c", argv[i]))
409      return startNetworkOrxonox(argc, argv);
410    else if(!strcmp( "--server", argv[i])   || !strcmp("-s", argv[i]))
411      return startNetworkOrxonox(argc, argv);
412    else if(!strcmp( "--license", argv[i])  || !strcmp("-s", argv[i]))
413      return PRINT(0)(ORXONOX_LICENSE_SHORT);
414  }
415
416  return startOrxonox(argc, argv, NULL, -1);
417}
418
419
420
421int showHelp(int argc, char** argv)
422{
423  PRINT(0)("Orxonox Version %s\n", PACKAGE_VERSION);
424  PRINT(0)(" Starts Orxonox - The most furious 3D Action Game :)\n");
425  PRINT(0)("\n");
426  PRINT(0)("Common options:\n");
427  PRINT(0)(" -g, --gui                        starts the orxonox with the configuration GUI \n");
428  PRINT(0)(" -h, --help                       shows this help\n");
429  PRINT(0)("\n");
430  PRINT(0)("Network options:\n");
431  PRINT(0)(" -s, --server [port]              starts Orxonox and listens on the [port] for players\n");
432  PRINT(0)(" -c, --client [hostname] [port]   starts Orxonox as a Client\n");
433  PRINT(0)(" -c, --client [ip address] [port] starts Orxonox as a Client\n");
434  PRINT(0)("\n");
435  PRINT(0)("Other options:\n");
436  PRINT(0)("     --license     prints the licence and exit\n\n");
437  PRINT(0)("\n");
438
439  //   {
440  //     Gui* gui = new Gui(argc, argv);
441  //     gui->printHelp();
442  //     delete gui;
443  //   }
444}
445
446
447
448
449/**
450 * starts orxonox in network mode
451 * @param argc parameters count given to orxonox
452 * @param argv parameters given to orxonox
453 */
454int startNetworkOrxonox(int argc, char** argv)
455{
456
457  int i;
458  for(i = 0; i < argc; ++i )
459  {
460    if( !strcmp( "--client", argv[i]) || !strcmp("-c", argv[i]))
461    {
462      if( argc <= (i+2))
463      {
464        printf(" Wrong arguments try following notations:\n");
465        printf("   --client [server ip address] [port number]\n");
466        printf("   --client [dns name] [port number]\n");
467        return 0;
468      }
469
470      const char* name = argv[i+1];
471      int port = atoi(argv[i+2]);
472      printf("Starting Orxonox as client: connecting to %s, on port %i\n", name, port);
473
474      startOrxonox(argc, argv, name, port);
475    }
476    else if( !strcmp( "--server", argv[i]) || !strcmp("-s", argv[i]))
477    {
478      if( argc <= (i+1))
479      {
480        printf(" Wrong arguments try following notations:\n");
481        printf("   --server [port number]\n");
482        return 0;
483      }
484
485      int port = atoi(argv[i+1]);
486      printf("Starting Orxonox as server, listening on port %i\n", port);
487
488      startOrxonox(argc, argv, NULL, port);
489    }
490  }
491}
492
493
494
495/**
496 * starts orxonox
497 * @param argc parameters count given to orxonox
498 * @param argv parameters given to orxonox
499 */
500int startOrxonox(int argc, char** argv, const char* name, int port)
501{
502  // checking for existence of the configuration-files, or if the lock file is still used
503  if (showGui || (!ResourceManager::isFile("./orxonox.conf") &&
504                  !ResourceManager::isFile(DEFAULT_CONFIG_FILE))
505#if DEBUG < 3 // developers do not need to see the GUI, when orxonox fails
506      || ResourceManager::isFile(DEFAULT_LOCK_FILE)
507#endif
508     )
509  {
510    if (ResourceManager::isFile(DEFAULT_LOCK_FILE))
511      ResourceManager::deleteFile(DEFAULT_LOCK_FILE);
512
513    // starting the GUI
514    Gui* gui = new Gui(argc, argv);
515    gui->startGui();
516
517    if (! gui->startOrxonox)
518      return 0;
519
520    delete gui;
521  }
522
523  PRINT(0)(">>> Starting Orxonox <<<\n");
524
525  ResourceManager::touchFile(DEFAULT_LOCK_FILE);
526
527  Orxonox *orx = Orxonox::getInstance();
528
529  if( orx->init(argc, argv, name, port) == -1)
530  {
531    PRINTF(1)("! Orxonox initialization failed\n");
532    return -1;
533  }
534
535  printf("finished inizialisation\n");
536  orx->start();
537
538  delete orx;
539  ResourceManager::deleteFile("~/.orxonox/orxonox.lock");
540}
Note: See TracBrowser for help on using the repository browser.