Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/orxonox.cc @ 6383

Last change on this file since 6383 was 6375, checked in by rennerc, 19 years ago

fixed a bug in orxonox.cc

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