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