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