Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6678 was 6658, checked in by patrick, 19 years ago

network: debug function for NetworkStream and connection

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