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