Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/shared_lib/src/orxonox.cc @ 7182

Last change on this file since 7182 was 7181, checked in by bensch, 19 years ago

sync

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