Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/orxonox.cc @ 7355

Last change on this file since 7355 was 7355, checked in by bensch, 18 years ago

orxonox/trunk: works on windows too

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