Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7895 was 7851, checked in by rennerc, 19 years ago

compression implemented

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