| [1850] | 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 |  | 
|---|
| [1855] | 20 |  | 
|---|
 | 21 |    ### File Specific: | 
|---|
 | 22 |    main-programmer: Patrick Boenzli | 
|---|
| [2190] | 23 |    co-programmer: Christian Meyer | 
|---|
| [4054] | 24 |    co-programmer: Benjamin Grauer: injected ResourceManager/GraphicsEngine/GUI | 
|---|
| [1850] | 25 | */ | 
|---|
 | 26 |  | 
|---|
| [2190] | 27 | #include "orxonox.h" | 
|---|
| [3610] | 28 |  | 
|---|
| [4054] | 29 | #include "gui.h" | 
|---|
 | 30 |  | 
|---|
| [2036] | 31 | #include "world.h" | 
|---|
| [4091] | 32 | #include "ini_parser.h" | 
|---|
| [2636] | 33 | #include "game_loader.h" | 
|---|
| [3610] | 34 | #include "graphics_engine.h" | 
|---|
| [4504] | 35 | #include "sound_engine.h" | 
|---|
| [3655] | 36 | #include "resource_manager.h" | 
|---|
| [4286] | 37 | #include "object_manager.h" | 
|---|
| [3790] | 38 | #include "text_engine.h" | 
|---|
| [4010] | 39 | #include "factory.h" | 
|---|
| [4131] | 40 | #include "benchmark.h" | 
|---|
| [4388] | 41 | #include "event_handler.h" | 
|---|
| [4408] | 42 | #include "event.h" | 
|---|
| [3610] | 43 |  | 
|---|
| [2190] | 44 | #include <string.h> | 
|---|
| [4032] | 45 |  | 
|---|
| [3966] | 46 | int verbose = 4; | 
|---|
| [2036] | 47 |  | 
|---|
| [1803] | 48 | using namespace std; | 
|---|
 | 49 |  | 
|---|
| [2190] | 50 | /** | 
|---|
| [2636] | 51 |    \brief create a new Orxonox | 
|---|
| [4135] | 52 |  | 
|---|
 | 53 |    In this funcitons only global values are set. The game will not be started here. | 
|---|
| [2190] | 54 | */ | 
|---|
 | 55 | Orxonox::Orxonox () | 
|---|
| [1872] | 56 | { | 
|---|
| [4445] | 57 |   this->setClassID(CL_ORXONOX, "Orxonox"); | 
|---|
| [4059] | 58 |  | 
|---|
| [4286] | 59 |   this->resourceManager = NULL; | 
|---|
 | 60 |   this->objectManager = NULL; | 
|---|
| [4442] | 61 |   this->eventHandler = NULL; | 
|---|
| [4135] | 62 |  | 
|---|
 | 63 |   this->argc = 0; | 
|---|
 | 64 |   this->argv = NULL; | 
|---|
| [1872] | 65 | } | 
|---|
| [1803] | 66 |  | 
|---|
| [2190] | 67 | /** | 
|---|
| [2636] | 68 |    \brief remove Orxonox from memory | 
|---|
| [2190] | 69 | */ | 
|---|
| [1875] | 70 | Orxonox::~Orxonox ()  | 
|---|
| [2190] | 71 | { | 
|---|
| [4054] | 72 |   int i =0; | 
|---|
| [3226] | 73 |   Orxonox::singletonRef = NULL; | 
|---|
| [3611] | 74 |   delete GraphicsEngine::getInstance(); // deleting the Graphics | 
|---|
| [4504] | 75 |   delete TextEngine::getInstance(); | 
|---|
 | 76 |   delete SoundEngine::getInstance(); | 
|---|
| [3660] | 77 |   delete ResourceManager::getInstance(); // deletes the Resource Manager | 
|---|
| [4286] | 78 |   delete ObjectManager::getInstance(); | 
|---|
| [3790] | 79 |   delete TextEngine::getInstance(); | 
|---|
| [4445] | 80 |   delete EventHandler::getInstance(); | 
|---|
| [2190] | 81 | } | 
|---|
| [1850] | 82 |  | 
|---|
| [3449] | 83 | /** \brief this is a singleton class to prevent duplicates */ | 
|---|
| [3226] | 84 | Orxonox* Orxonox::singletonRef = 0; | 
|---|
| [1872] | 85 |  | 
|---|
| [3449] | 86 | /** | 
|---|
 | 87 |    \returns reference or new Object of Orxonox if not existent. | 
|---|
 | 88 | */ | 
|---|
| [1850] | 89 | Orxonox* Orxonox::getInstance (void) | 
|---|
| [1803] | 90 | { | 
|---|
| [3226] | 91 |   if (singletonRef == NULL) | 
|---|
 | 92 |     singletonRef = new Orxonox(); | 
|---|
 | 93 |   return singletonRef; | 
|---|
| [1850] | 94 | } | 
|---|
 | 95 |  | 
|---|
| [2190] | 96 | /** | 
|---|
| [2636] | 97 |    \brief this finds the config file | 
|---|
 | 98 |     | 
|---|
 | 99 |    Since the config file varies from user to user and since one may want to specify different config files | 
|---|
 | 100 |    for certain occasions or platforms this function finds the right config file for every occasion and stores | 
|---|
 | 101 |    it's path and name into configfilename | 
|---|
| [2190] | 102 | */ | 
|---|
| [3226] | 103 | void Orxonox::getConfigFile (int argc, char** argv) | 
|---|
| [1850] | 104 | { | 
|---|
| [4084] | 105 |   strcpy (configfilename, "~/.orxonox/orxonox.conf"); | 
|---|
| [1803] | 106 | } | 
|---|
 | 107 |  | 
|---|
| [2190] | 108 | /** | 
|---|
| [2636] | 109 |    \brief initialize Orxonox with command line | 
|---|
| [2190] | 110 | */ | 
|---|
 | 111 | int Orxonox::init (int argc, char** argv) | 
|---|
| [1803] | 112 | { | 
|---|
| [4135] | 113 |   this->argc = argc; | 
|---|
 | 114 |   this->argv = argv; | 
|---|
| [2636] | 115 |   // parse command line | 
|---|
 | 116 |   // config file | 
|---|
 | 117 |    | 
|---|
| [3226] | 118 |   getConfigFile (argc, argv); | 
|---|
| [3174] | 119 |   SDL_Init (SDL_INIT_TIMER); | 
|---|
| [2636] | 120 |   // initialize everything | 
|---|
| [4113] | 121 |   printf("> Initializing resources\n"); | 
|---|
 | 122 |   if( initResources () == -1) return -1; | 
|---|
 | 123 |  | 
|---|
| [3226] | 124 |   if( initVideo() == -1) return -1; | 
|---|
 | 125 |   if( initSound() == -1) return -1; | 
|---|
| [4504] | 126 |   PRINT(3)("> Initializing input\n"); | 
|---|
| [3226] | 127 |   if( initInput() == -1) return -1; | 
|---|
| [4504] | 128 |   PRINT(3)("> Initializing networking\n"); | 
|---|
| [3226] | 129 |   if( initNetworking () == -1) return -1; | 
|---|
| [2636] | 130 |   //printf("> Initializing world\n"); | 
|---|
 | 131 |   //if( init_world () == -1) return -1; PB: world will be initialized when started | 
|---|
 | 132 |    | 
|---|
 | 133 |   return 0; | 
|---|
| [1850] | 134 | } | 
|---|
| [1849] | 135 |  | 
|---|
| [2190] | 136 | /** | 
|---|
| [2636] | 137 |    \brief initializes SDL and OpenGL | 
|---|
| [2190] | 138 | */ | 
|---|
| [3226] | 139 | int Orxonox::initVideo()  | 
|---|
| [2190] | 140 | { | 
|---|
| [3611] | 141 |   PRINTF(3)("> Initializing video\n"); | 
|---|
| [2190] | 142 |    | 
|---|
| [3610] | 143 |   GraphicsEngine::getInstance(); | 
|---|
| [4136] | 144 |      | 
|---|
| [2190] | 145 |   return 0; | 
|---|
 | 146 | } | 
|---|
| [1850] | 147 |  | 
|---|
| [3214] | 148 |  | 
|---|
| [2190] | 149 | /** | 
|---|
| [2636] | 150 |    \brief initializes the sound engine | 
|---|
| [2190] | 151 | */ | 
|---|
| [3226] | 152 | int Orxonox::initSound()  | 
|---|
| [2190] | 153 | { | 
|---|
| [4504] | 154 |   PRINT(3)("> Initializing sound\n"); | 
|---|
| [3226] | 155 |   // SDL_Init(SDL_INIT_AUDIO); | 
|---|
| [4504] | 156 |   SoundEngine::getInstance()->initAudio(); | 
|---|
| [2636] | 157 |   return 0; | 
|---|
| [2190] | 158 | } | 
|---|
| [1900] | 159 |  | 
|---|
| [3214] | 160 |  | 
|---|
| [2190] | 161 | /** | 
|---|
| [2636] | 162 |    \brief initializes input functions | 
|---|
| [2190] | 163 | */ | 
|---|
| [3226] | 164 | int Orxonox::initInput()  | 
|---|
| [2190] | 165 | { | 
|---|
| [4408] | 166 |   this->eventHandler = EventHandler::getInstance(); | 
|---|
 | 167 |   this->eventHandler->init(); | 
|---|
| [2636] | 168 |    | 
|---|
 | 169 |   return 0; | 
|---|
| [1803] | 170 | } | 
|---|
 | 171 |  | 
|---|
| [3214] | 172 |  | 
|---|
| [2190] | 173 | /** | 
|---|
| [2636] | 174 |    \brief initializes network system | 
|---|
| [2190] | 175 | */ | 
|---|
| [3226] | 176 | int Orxonox::initNetworking()  | 
|---|
| [1897] | 177 | { | 
|---|
| [2636] | 178 |   printf("Not yet implemented\n"); | 
|---|
 | 179 |   return 0; | 
|---|
| [1897] | 180 | } | 
|---|
 | 181 |  | 
|---|
| [3214] | 182 |  | 
|---|
| [2190] | 183 | /** | 
|---|
| [2636] | 184 |    \brief initializes and loads resource files | 
|---|
| [2190] | 185 | */ | 
|---|
| [3226] | 186 | int Orxonox::initResources()  | 
|---|
| [1858] | 187 | { | 
|---|
| [3655] | 188 |   PRINT(3)("initializing ResourceManager\n"); | 
|---|
 | 189 |   resourceManager = ResourceManager::getInstance(); | 
|---|
| [4091] | 190 |  | 
|---|
 | 191 |   // create parser | 
|---|
 | 192 |   IniParser parser (DEFAULT_CONFIG_FILE); | 
|---|
 | 193 |   if( parser.getSection (CONFIG_SECTION_DATA) == -1) | 
|---|
| [4042] | 194 |     { | 
|---|
| [4091] | 195 |       PRINTF(1)("Could not find Section %s in %s\n", CONFIG_SECTION_DATA, DEFAULT_CONFIG_FILE); | 
|---|
 | 196 |       return -1; | 
|---|
 | 197 |     } | 
|---|
 | 198 |   char namebuf[256]; | 
|---|
 | 199 |   char valuebuf[256]; | 
|---|
 | 200 |   memset (namebuf, 0, 256); | 
|---|
 | 201 |   memset (valuebuf, 0, 256); | 
|---|
 | 202 |    | 
|---|
 | 203 |   while( parser.nextVar (namebuf, valuebuf) != -1) | 
|---|
 | 204 |     { | 
|---|
 | 205 |       if (!strcmp(namebuf, CONFIG_NAME_DATADIR)) | 
|---|
 | 206 |         { | 
|---|
 | 207 |           //  printf("Not yet implemented\n"); | 
|---|
 | 208 |           if (!resourceManager->setDataDir(valuebuf)) | 
|---|
 | 209 |             { | 
|---|
 | 210 |               PRINTF(1)("Data Could not be located\n"); | 
|---|
 | 211 |               exit(-1); | 
|---|
 | 212 |             } | 
|---|
 | 213 |         } | 
|---|
 | 214 |        | 
|---|
 | 215 |       memset (namebuf, 0, 256); | 
|---|
 | 216 |       memset (valuebuf, 0, 256); | 
|---|
 | 217 |     } | 
|---|
 | 218 |    | 
|---|
 | 219 |   if (!resourceManager->checkDataDir(DEFAULT_DATA_DIR_CHECKFILE)) | 
|---|
 | 220 |     { | 
|---|
 | 221 |       PRINTF(1)("The DataDirectory %s could not be verified\nPlease Change in File %s Section %s Entry %s to a suitable value\n", | 
|---|
 | 222 |                 resourceManager->getDataDir(), | 
|---|
 | 223 |                 DEFAULT_CONFIG_FILE, | 
|---|
 | 224 |                 CONFIG_SECTION_DATA, | 
|---|
 | 225 |                 CONFIG_NAME_DATADIR); | 
|---|
| [4054] | 226 |       exit(-1); | 
|---|
| [4042] | 227 |     } | 
|---|
| [4009] | 228 |  | 
|---|
| [4091] | 229 |  | 
|---|
| [3790] | 230 |   PRINT(3)("initializing TextEngine\n"); | 
|---|
 | 231 |   TextEngine::getInstance(); | 
|---|
| [4132] | 232 |  | 
|---|
| [4286] | 233 |   PRINT(3)("initializing ObjectManager\n"); | 
|---|
 | 234 |   this->objectManager = ObjectManager::getInstance(); | 
|---|
 | 235 |  | 
|---|
| [4132] | 236 |   return 0; | 
|---|
| [1858] | 237 | } | 
|---|
| [1849] | 238 |  | 
|---|
| [3214] | 239 |  | 
|---|
| [1896] | 240 |  | 
|---|
| [2636] | 241 |  | 
|---|
| [2190] | 242 | /** | 
|---|
| [2636] | 243 |    \brief starts the orxonox game or menu | 
|---|
 | 244 |  | 
|---|
 | 245 |    here is the central orxonox state manager. There are currently two states | 
|---|
 | 246 |    - menu | 
|---|
 | 247 |    - game-play | 
|---|
 | 248 |    both states manage their states themselfs again. | 
|---|
| [2190] | 249 | */ | 
|---|
| [2636] | 250 | void Orxonox::start() | 
|---|
 | 251 | { | 
|---|
 | 252 |    | 
|---|
 | 253 |   this->gameLoader = GameLoader::getInstance(); | 
|---|
| [4094] | 254 |   this->gameLoader->loadCampaign("worlds/DefaultCampaign.oxc"); | 
|---|
| [4010] | 255 |   //  this->gameLoader->loadDebugCampaign(DEBUG_CAMPAIGN_0); | 
|---|
| [2636] | 256 |   this->gameLoader->init(); | 
|---|
 | 257 |   this->gameLoader->start(); | 
|---|
 | 258 | } | 
|---|
 | 259 |  | 
|---|
| [3214] | 260 |  | 
|---|
| [2636] | 261 | /** | 
|---|
 | 262 |    \brief handles sprecial events from localinput | 
|---|
 | 263 |    \param event: an event not handled by the CommandNode  | 
|---|
| [2190] | 264 | */ | 
|---|
| [4408] | 265 | void Orxonox::graphicsHandler(SDL_Event* event) | 
|---|
| [2190] | 266 | { | 
|---|
| [2636] | 267 |   // Handle special events such as reshape, quit, focus changes | 
|---|
| [3619] | 268 |   switch (event->type) | 
|---|
 | 269 |     { | 
|---|
 | 270 |     case SDL_VIDEORESIZE: | 
|---|
 | 271 |       GraphicsEngine* tmpGEngine = GraphicsEngine::getInstance(); | 
|---|
 | 272 |       tmpGEngine->resolutionChanged(&event->resize); | 
|---|
 | 273 |       break; | 
|---|
 | 274 |     } | 
|---|
| [2190] | 275 | } | 
|---|
| [3214] | 276 |   | 
|---|
| [1875] | 277 |  | 
|---|
| [4445] | 278 | /** | 
|---|
 | 279 |   \brief processes the events for orxonox main class | 
|---|
 | 280 |   \param the event to handle | 
|---|
 | 281 | */ | 
|---|
| [4408] | 282 | void Orxonox::process(const Event &event) | 
|---|
 | 283 | {} | 
|---|
 | 284 |  | 
|---|
| [1803] | 285 |  | 
|---|
| [3214] | 286 |  | 
|---|
| [4059] | 287 | bool showGui = false; | 
|---|
| [3648] | 288 |  | 
|---|
| [3449] | 289 | /** | 
|---|
 | 290 |    \brief main function | 
|---|
| [3214] | 291 |  | 
|---|
| [3449] | 292 |    here the journey begins | 
|---|
 | 293 | */ | 
|---|
| [3226] | 294 | int main(int argc, char** argv)  | 
|---|
| [4135] | 295 | {  | 
|---|
| [3648] | 296 |  | 
|---|
| [4135] | 297 |   // here the pre-arguments are loaded, these are needed to go either to orxonx itself, Help, or Benchmark. | 
|---|
| [3648] | 298 |   int i; | 
|---|
| [4032] | 299 |   for(i = 1; i < argc; ++i) | 
|---|
| [3648] | 300 |     { | 
|---|
| [4135] | 301 |       if(! strcmp( "--help", argv[i]) || !strcmp("-h", argv[i])) return startHelp(argc, argv); | 
|---|
 | 302 |       else if(!strcmp( "--benchmark", argv[i]) || !strcmp("-b", argv[i])) return startBenchmarks(); | 
|---|
 | 303 |       else if(!strcmp( "--gui", argv[i]) || !strcmp("-g", argv[i])) showGui = true; | 
|---|
 | 304 |       //      else PRINTF(2)("Orxonox does not understand the arguments %s\n", argv[i]); | 
|---|
| [3648] | 305 |     } | 
|---|
 | 306 |  | 
|---|
 | 307 |   return startOrxonox(argc, argv); | 
|---|
 | 308 | } | 
|---|
 | 309 |  | 
|---|
 | 310 |  | 
|---|
 | 311 |  | 
|---|
| [4132] | 312 | int startHelp(int argc, char** argv) | 
|---|
| [3648] | 313 | { | 
|---|
| [4032] | 314 |   PRINT(0)("orxonox: starts the orxonox game - rules\n"); | 
|---|
| [4134] | 315 |   PRINT(0)("usage: orxonox [arg [arg...]]\n\n"); | 
|---|
| [4032] | 316 |   PRINT(0)("valid options:\n"); | 
|---|
| [4132] | 317 |   { | 
|---|
 | 318 |     Gui* gui = new Gui(argc, argv); | 
|---|
 | 319 |     gui->printHelp(); | 
|---|
 | 320 |     delete gui; | 
|---|
 | 321 |   } | 
|---|
| [4135] | 322 |   PRINT(0)(" -b|--benchmark:\t\tstarts the orxonox benchmark\n"); | 
|---|
 | 323 |   PRINT(0)(" -h|--help:\t\t\tshows this help\n"); | 
|---|
| [3648] | 324 | } | 
|---|
 | 325 |  | 
|---|
| [3649] | 326 |  | 
|---|
| [3648] | 327 | int startOrxonox(int argc, char** argv) | 
|---|
 | 328 | { | 
|---|
| [4032] | 329 |   // checking for existence of the configuration-files | 
|---|
| [4059] | 330 |   if (showGui || | 
|---|
 | 331 |       !ResourceManager::isFile("~/.orxonox/orxonox.conf") || | 
|---|
 | 332 |       ResourceManager::isFile("~/.orxonox/orxonox.lock")) | 
|---|
| [4032] | 333 |     { | 
|---|
 | 334 |       if (ResourceManager::isFile("~/.orxonox/orxonox.lock")) | 
|---|
 | 335 |         ResourceManager::deleteFile("~/.orxonox/orxonox.lock"); | 
|---|
| [4132] | 336 |        | 
|---|
 | 337 |       // starting the GUI | 
|---|
| [4056] | 338 |       Gui* gui = new Gui(argc, argv); | 
|---|
| [4132] | 339 |       gui->startGui(); | 
|---|
 | 340 |  | 
|---|
| [4054] | 341 |       if (! gui->startOrxonox) | 
|---|
 | 342 |         return 0; | 
|---|
 | 343 |        | 
|---|
 | 344 |       delete gui; | 
|---|
| [4032] | 345 |     } | 
|---|
 | 346 |    | 
|---|
 | 347 |   PRINT(0)(">>> Starting Orxonox <<<\n"); | 
|---|
| [4033] | 348 |  | 
|---|
 | 349 |   ResourceManager::touchFile("~/.orxonox/orxonox.lock"); | 
|---|
 | 350 |  | 
|---|
| [1850] | 351 |   Orxonox *orx = Orxonox::getInstance(); | 
|---|
| [2190] | 352 |    | 
|---|
| [3226] | 353 |   if((*orx).init(argc, argv) == -1) | 
|---|
| [2636] | 354 |     { | 
|---|
| [4032] | 355 |       PRINTF(1)("! Orxonox initialization failed\n"); | 
|---|
| [2636] | 356 |       return -1; | 
|---|
 | 357 |     } | 
|---|
 | 358 |    | 
|---|
 | 359 |   orx->start(); | 
|---|
 | 360 |    | 
|---|
| [3676] | 361 |   delete orx; | 
|---|
| [4033] | 362 |   ResourceManager::deleteFile("~/.orxonox/orxonox.lock"); | 
|---|
| [2190] | 363 |    | 
|---|
| [1803] | 364 | } | 
|---|