Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7793 was 7749, checked in by rennerc, 19 years ago

added commandline argument to open network log console

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