[4597] | 1 | /* |
---|
[1853] | 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. |
---|
[1855] | 10 | |
---|
| 11 | ### File Specific: |
---|
[3610] | 12 | main-programmer: Benjamin Grauer |
---|
[1855] | 13 | co-programmer: ... |
---|
[1853] | 14 | */ |
---|
| 15 | |
---|
[3610] | 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GRAPHICS |
---|
[1853] | 17 | |
---|
[3610] | 18 | #include "graphics_engine.h" |
---|
[7193] | 19 | #include "util/loading/resource_manager.h" |
---|
[6441] | 20 | #include "state.h" |
---|
[1853] | 21 | |
---|
[6142] | 22 | #include "world_entity.h" |
---|
| 23 | |
---|
[4849] | 24 | #include "render_2d.h" |
---|
[5347] | 25 | #include "text_engine.h" |
---|
[4850] | 26 | #include "light.h" |
---|
[5347] | 27 | #include "shader.h" |
---|
[3611] | 28 | #include "debug.h" |
---|
[5347] | 29 | |
---|
[7256] | 30 | #include "util/preferences.h" |
---|
[4770] | 31 | #include "substring.h" |
---|
[5344] | 32 | #include "text.h" |
---|
[4770] | 33 | |
---|
[5819] | 34 | #include "globals.h" |
---|
[5857] | 35 | #include "texture.h" |
---|
[5755] | 36 | |
---|
[6753] | 37 | #include "effects/graphics_effect.h" |
---|
[6772] | 38 | #include "effects/fog_effect.h" |
---|
[6884] | 39 | #include "effects/lense_flare.h" |
---|
[6753] | 40 | |
---|
[6522] | 41 | #include "shell_command.h" |
---|
| 42 | |
---|
[6815] | 43 | |
---|
| 44 | #include "parser/tinyxml/tinyxml.h" |
---|
[7193] | 45 | #include "util/loading/load_param.h" |
---|
| 46 | #include "util/loading/factory.h" |
---|
[6979] | 47 | #include "class_list.h" |
---|
[6815] | 48 | |
---|
[5755] | 49 | #ifdef __WIN32__ |
---|
[6162] | 50 | #include "static_model.h" |
---|
[5755] | 51 | #endif |
---|
[1853] | 52 | |
---|
[6522] | 53 | SHELL_COMMAND(wireframe, GraphicsEngine, wireframe); |
---|
[9351] | 54 | SHELL_COMMAND(fps, GraphicsEngine, toggleFPSdisplay); |
---|
[6522] | 55 | |
---|
[3245] | 56 | /** |
---|
[7871] | 57 | * @brief standard constructor |
---|
[5262] | 58 | */ |
---|
[4597] | 59 | GraphicsEngine::GraphicsEngine () |
---|
[3365] | 60 | { |
---|
[4597] | 61 | this->setClassID(CL_GRAPHICS_ENGINE, "GraphicsEngine"); |
---|
| 62 | this->setName("GraphicsEngine"); |
---|
[4784] | 63 | |
---|
| 64 | this->isInit = false; |
---|
| 65 | |
---|
[4245] | 66 | this->bDisplayFPS = false; |
---|
[8490] | 67 | this->bAntialiasing = false; |
---|
[4245] | 68 | this->minFPS = 9999; |
---|
| 69 | this->maxFPS = 0; |
---|
[4135] | 70 | |
---|
[5079] | 71 | this->geTextCFPS = NULL; |
---|
| 72 | this->geTextMaxFPS = NULL; |
---|
| 73 | this->geTextMinFPS = NULL; |
---|
| 74 | |
---|
[4768] | 75 | this->fullscreenFlag = 0; |
---|
[5225] | 76 | this->videoFlags = 0; |
---|
| 77 | this->screen = NULL; |
---|
[5260] | 78 | |
---|
[6979] | 79 | // initialize the Modules |
---|
[5285] | 80 | TextEngine::getInstance(); |
---|
[6979] | 81 | this->graphicsEffects = NULL; |
---|
| 82 | |
---|
[3611] | 83 | } |
---|
[3610] | 84 | |
---|
[3621] | 85 | /** |
---|
[7871] | 86 | * @brief The Pointer to this GraphicsEngine |
---|
| 87 | */ |
---|
[3611] | 88 | GraphicsEngine* GraphicsEngine::singletonRef = NULL; |
---|
[3610] | 89 | |
---|
[3621] | 90 | /** |
---|
[7871] | 91 | * @brief destructs the graphicsEngine. |
---|
[3611] | 92 | */ |
---|
[4597] | 93 | GraphicsEngine::~GraphicsEngine () |
---|
[3611] | 94 | { |
---|
| 95 | // delete what has to be deleted here |
---|
[7029] | 96 | this->displayFPS( false ); |
---|
[4849] | 97 | |
---|
[5286] | 98 | //TextEngine |
---|
[5285] | 99 | delete TextEngine::getInstance(); |
---|
[5347] | 100 | // render 2D |
---|
| 101 | delete Render2D::getInstance(); |
---|
[5079] | 102 | |
---|
[5225] | 103 | SDL_QuitSubSystem(SDL_INIT_VIDEO); |
---|
[6979] | 104 | // if (this->screen != NULL) |
---|
| 105 | // SDL_FreeSurface(this->screen); |
---|
[5240] | 106 | |
---|
[4849] | 107 | GraphicsEngine::singletonRef = NULL; |
---|
[3611] | 108 | } |
---|
| 109 | |
---|
[6815] | 110 | |
---|
[4830] | 111 | /** |
---|
[7871] | 112 | * @brief loads the GraphicsEngine Specific Parameters. |
---|
[6815] | 113 | * @param root: the XML-Element to load the Data From |
---|
| 114 | */ |
---|
| 115 | void GraphicsEngine::loadParams(const TiXmlElement* root) |
---|
| 116 | { |
---|
[6979] | 117 | LoadParamXML(root, "GraphicsEffect", this, GraphicsEngine, loadGraphicsEffects) |
---|
| 118 | .describe("loads a graphics effect"); |
---|
[6815] | 119 | } |
---|
| 120 | |
---|
| 121 | |
---|
[6980] | 122 | |
---|
| 123 | |
---|
[6815] | 124 | /** |
---|
[6980] | 125 | * @param root The XML-element to load GraphicsEffects from |
---|
| 126 | */ |
---|
| 127 | void GraphicsEngine::loadGraphicsEffects(const TiXmlElement* root) |
---|
| 128 | { |
---|
| 129 | LOAD_PARAM_START_CYCLE(root, element); |
---|
| 130 | { |
---|
[7035] | 131 | PRINTF(4)("element is: %s\n", element->Value()); |
---|
[6980] | 132 | Factory::fabricate(element); |
---|
| 133 | } |
---|
| 134 | LOAD_PARAM_END_CYCLE(element); |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | |
---|
| 138 | |
---|
| 139 | /** |
---|
[7871] | 140 | * @brief initializes the GraphicsEngine with default settings. |
---|
[4830] | 141 | */ |
---|
[4784] | 142 | int GraphicsEngine::init() |
---|
| 143 | { |
---|
[4830] | 144 | if (this->isInit) |
---|
| 145 | return -1; |
---|
| 146 | this->initVideo(640, 480, 16); |
---|
[8316] | 147 | return 1; |
---|
[4784] | 148 | } |
---|
| 149 | |
---|
[3611] | 150 | /** |
---|
[7871] | 151 | * @brief loads the GraphicsEngine's settings from a given ini-file and section |
---|
[4830] | 152 | * @returns nothing usefull |
---|
| 153 | */ |
---|
[7256] | 154 | int GraphicsEngine::initFromPreferences() |
---|
[4830] | 155 | { |
---|
| 156 | // looking if we are in fullscreen-mode |
---|
[7661] | 157 | MultiType fullscreen = Preferences::getInstance()->getString(CONFIG_SECTION_VIDEO, CONFIG_NAME_FULLSCREEN, "0"); |
---|
[7256] | 158 | |
---|
[7661] | 159 | if (fullscreen.getBool()) |
---|
[4830] | 160 | this->fullscreenFlag = SDL_FULLSCREEN; |
---|
| 161 | |
---|
| 162 | // looking if we are in fullscreen-mode |
---|
[7661] | 163 | MultiType textures = Preferences::getInstance()->getString(CONFIG_SECTION_VIDEO_ADVANCED, CONFIG_NAME_TEXTURES, "1"); |
---|
[8316] | 164 | Texture::setTextureEnableState(textures.getBool()); |
---|
[4830] | 165 | |
---|
| 166 | // searching for a usefull resolution |
---|
[7256] | 167 | SubString resolution(Preferences::getInstance()->getString(CONFIG_SECTION_VIDEO, CONFIG_NAME_RESOLUTION, "640x480").c_str(), 'x'); ///FIXME |
---|
[4833] | 168 | //resolution.debug(); |
---|
[7221] | 169 | MultiType x = resolution.getString(0), y = resolution.getString(1); |
---|
[8316] | 170 | return this->initVideo(x.getInt(), y.getInt(), 16); |
---|
[4833] | 171 | |
---|
[6979] | 172 | // GraphicsEffect* fe = new FogEffect(NULL); |
---|
| 173 | // this->loadGraphicsEffect(fe); |
---|
| 174 | // fe->activate(); |
---|
| 175 | // PRINTF(0)("--------------------------------------------------------------\n"); |
---|
[6884] | 176 | |
---|
[6967] | 177 | //LenseFlare* ge = new LenseFlare(); |
---|
| 178 | //this->loadGraphicsEffect(ge); |
---|
[6884] | 179 | |
---|
[6967] | 180 | //ge->addFlare("pictures/lense_flare/sun.png"); //sun |
---|
| 181 | //ge->addFlare("pictures/lense_flare/lens2.png"); //first halo |
---|
| 182 | //ge->addFlare("pictures/lense_flare/lens1.png"); //small birst |
---|
| 183 | //ge->addFlare("pictures/lense_flare/lens3.png"); //second halo |
---|
| 184 | //ge->addFlare("pictures/lense_flare/lens4.png"); |
---|
| 185 | //ge->addFlare("pictures/lense_flare/lens1.png"); |
---|
| 186 | //ge->addFlare("pictures/lense_flare/lens3.png"); |
---|
[6884] | 187 | |
---|
[6966] | 188 | //ge->activate(); |
---|
[4830] | 189 | } |
---|
| 190 | |
---|
| 191 | |
---|
| 192 | |
---|
| 193 | /** |
---|
[7871] | 194 | * @brief initializes the Video for openGL. |
---|
[5346] | 195 | * |
---|
| 196 | * This has to be done only once when starting orxonox. |
---|
| 197 | */ |
---|
[4784] | 198 | int GraphicsEngine::initVideo(unsigned int resX, unsigned int resY, unsigned int bbp) |
---|
[3611] | 199 | { |
---|
[4784] | 200 | if (this->isInit) |
---|
| 201 | return -1; |
---|
[5024] | 202 | // initialize SDL_VIDEO |
---|
[5225] | 203 | if (SDL_InitSubSystem(SDL_INIT_VIDEO) == -1) |
---|
[5024] | 204 | { |
---|
| 205 | PRINTF(1)("could not initialize SDL Video\n"); |
---|
[8316] | 206 | return -1; |
---|
[5024] | 207 | } |
---|
[3617] | 208 | // initialize SDL_GL-settings |
---|
| 209 | this->setGLattribs(); |
---|
[3610] | 210 | |
---|
[3617] | 211 | // setting the Video Flags. |
---|
[7727] | 212 | this->videoFlags = SDL_OPENGL | SDL_HWPALETTE | SDL_RESIZABLE ; |
---|
[3610] | 213 | |
---|
| 214 | /* query SDL for information about our video hardware */ |
---|
| 215 | const SDL_VideoInfo* videoInfo = SDL_GetVideoInfo (); |
---|
| 216 | if( videoInfo == NULL) |
---|
[6979] | 217 | { |
---|
| 218 | PRINTF(1)("Failed getting Video Info :%s\n", SDL_GetError()); |
---|
| 219 | SDL_Quit (); |
---|
| 220 | } |
---|
[3610] | 221 | if( videoInfo->hw_available) |
---|
[3611] | 222 | this->videoFlags |= SDL_HWSURFACE; |
---|
[4597] | 223 | else |
---|
[3611] | 224 | this->videoFlags |= SDL_SWSURFACE; |
---|
[3610] | 225 | /* |
---|
[3619] | 226 | if(VideoInfo -> blit_hw) |
---|
[3610] | 227 | VideoFlags |= SDL_HWACCEL; |
---|
| 228 | */ |
---|
[6979] | 229 | // setting up the Resolution |
---|
[4784] | 230 | this->setResolution(resX, resY, bbp); |
---|
[3611] | 231 | |
---|
[5260] | 232 | // GRABBING ALL GL-extensions |
---|
| 233 | this->grabHardwareSettings(); |
---|
| 234 | |
---|
[3621] | 235 | // Enable default GL stuff |
---|
| 236 | glEnable(GL_DEPTH_TEST); |
---|
[4784] | 237 | |
---|
[4849] | 238 | Render2D::getInstance(); |
---|
[4833] | 239 | |
---|
[4784] | 240 | this->isInit = true; |
---|
[8316] | 241 | return 1; |
---|
[3365] | 242 | } |
---|
[1853] | 243 | |
---|
[4770] | 244 | /** |
---|
[7871] | 245 | * @brief sets the Window Captions and the Name of the icon. |
---|
[4619] | 246 | * @param windowName The name of the Window |
---|
| 247 | * @param icon The name of the Icon on the Disc |
---|
| 248 | */ |
---|
[7221] | 249 | void GraphicsEngine::setWindowName(const std::string& windowName, const std::string& icon) |
---|
[4619] | 250 | { |
---|
[7221] | 251 | SDL_Surface* iconSurf = SDL_LoadBMP(icon.c_str()); |
---|
[5240] | 252 | if (iconSurf != NULL) |
---|
| 253 | { |
---|
[5241] | 254 | Uint32 colorkey = SDL_MapRGB(iconSurf->format, 0, 0, 0); |
---|
[5240] | 255 | SDL_SetColorKey(iconSurf, SDL_SRCCOLORKEY, colorkey); |
---|
[7221] | 256 | SDL_WM_SetIcon(iconSurf, NULL); |
---|
[5240] | 257 | SDL_FreeSurface(iconSurf); |
---|
| 258 | } |
---|
[5024] | 259 | |
---|
[7221] | 260 | SDL_WM_SetCaption (windowName.c_str(), icon.c_str()); |
---|
[4619] | 261 | } |
---|
| 262 | |
---|
| 263 | |
---|
| 264 | /** |
---|
[7871] | 265 | * @brief Sets the GL-attributes |
---|
[5346] | 266 | */ |
---|
[8316] | 267 | void GraphicsEngine::setGLattribs() |
---|
[3617] | 268 | { |
---|
| 269 | // Set video mode |
---|
| 270 | // TO DO: parse arguments for settings |
---|
| 271 | //SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5); |
---|
| 272 | //SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5); |
---|
| 273 | //SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5); |
---|
| 274 | //SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); |
---|
| 275 | |
---|
[4597] | 276 | |
---|
| 277 | SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); |
---|
| 278 | SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16); |
---|
| 279 | SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, 0); |
---|
[3617] | 280 | SDL_GL_SetAttribute( SDL_GL_ACCUM_RED_SIZE, 0); |
---|
| 281 | SDL_GL_SetAttribute( SDL_GL_ACCUM_GREEN_SIZE, 0); |
---|
| 282 | SDL_GL_SetAttribute( SDL_GL_ACCUM_BLUE_SIZE, 0); |
---|
| 283 | SDL_GL_SetAttribute( SDL_GL_ACCUM_ALPHA_SIZE, 0); |
---|
[7727] | 284 | |
---|
[8490] | 285 | SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5); //Use at least 5 bits of Red |
---|
| 286 | SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5); //Use at least 5 bits of Green |
---|
| 287 | SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5); //Use at least 5 bits of Blue |
---|
| 288 | SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); //Use at least 16 bits for the depth buffer |
---|
[7727] | 289 | SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); //Enable double buffering |
---|
[7919] | 290 | |
---|
[8490] | 291 | // enable antialiasing? |
---|
| 292 | if( this->bAntialiasing) |
---|
| 293 | { |
---|
| 294 | SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES,4); |
---|
| 295 | SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS,1); |
---|
| 296 | } |
---|
| 297 | |
---|
[7919] | 298 | glEnable(GL_CULL_FACE); |
---|
| 299 | glCullFace(GL_FRONT); |
---|
[3617] | 300 | } |
---|
| 301 | |
---|
[5261] | 302 | /** |
---|
[7871] | 303 | * @brief grabs the Hardware Specifics |
---|
| 304 | * |
---|
[5261] | 305 | * checks for all the different HW-types |
---|
| 306 | */ |
---|
[5260] | 307 | void GraphicsEngine::grabHardwareSettings() |
---|
| 308 | { |
---|
| 309 | const char* renderer = (const char*) glGetString(GL_RENDERER); |
---|
| 310 | const char* vendor = (const char*) glGetString(GL_VENDOR); |
---|
| 311 | const char* version = (const char*) glGetString(GL_VERSION); |
---|
| 312 | const char* extensions = (const char*) glGetString(GL_EXTENSIONS); |
---|
| 313 | |
---|
[6979] | 314 | // printf("%s %s %s\n %s", renderer, vendor, version, extensions); |
---|
[5260] | 315 | |
---|
[7221] | 316 | if (renderer != NULL) |
---|
[5260] | 317 | { |
---|
[7221] | 318 | this->hwRenderer == renderer; |
---|
[5260] | 319 | } |
---|
[7221] | 320 | if (vendor != NULL) |
---|
[5260] | 321 | { |
---|
[7221] | 322 | this->hwVendor == vendor; |
---|
[5260] | 323 | } |
---|
[7221] | 324 | if (version != NULL) |
---|
[5260] | 325 | { |
---|
[7221] | 326 | this->hwVersion == version; |
---|
[5260] | 327 | } |
---|
| 328 | |
---|
[7221] | 329 | if (extensions != NULL) |
---|
| 330 | this->hwExtensions.split(extensions, " \n\t,"); |
---|
[5260] | 331 | |
---|
[6634] | 332 | PRINT(4)("Running on : vendor: %s, renderer: %s, version:%s\n", vendor, renderer, version); |
---|
[5260] | 333 | PRINT(4)("Extensions:\n"); |
---|
[7319] | 334 | for (unsigned int i = 0; i < this->hwExtensions.size(); i++) |
---|
| 335 | PRINT(4)("%d: %s\n", i, this->hwExtensions[i].c_str()); |
---|
[5263] | 336 | |
---|
| 337 | |
---|
| 338 | // inizializing GLEW |
---|
| 339 | GLenum err = glewInit(); |
---|
| 340 | if (GLEW_OK != err) |
---|
| 341 | { |
---|
| 342 | /* Problem: glewInit failed, something is seriously wrong. */ |
---|
| 343 | PRINTF(1)("%s\n", glewGetErrorString(err)); |
---|
| 344 | } |
---|
| 345 | PRINTF(4)("Status: Using GLEW %s\n", glewGetString(GLEW_VERSION)); |
---|
[5260] | 346 | } |
---|
| 347 | |
---|
[6162] | 348 | |
---|
[3621] | 349 | /** |
---|
[7871] | 350 | * @brief sets the Resolution of the Screen to display the Graphics to. |
---|
[4836] | 351 | * @param width The width of the window |
---|
| 352 | * @param height The height of the window |
---|
| 353 | * @param bpp bits per pixel |
---|
[5346] | 354 | */ |
---|
[3619] | 355 | int GraphicsEngine::setResolution(int width, int height, int bpp) |
---|
[3610] | 356 | { |
---|
[3611] | 357 | this->resolutionX = width; |
---|
| 358 | this->resolutionY = height; |
---|
| 359 | this->bitsPerPixel = bpp; |
---|
[6441] | 360 | State::setResolution( width, height); |
---|
[4739] | 361 | |
---|
[5255] | 362 | if (this->screen != NULL) |
---|
[5237] | 363 | SDL_FreeSurface(screen); |
---|
[4769] | 364 | if((this->screen = SDL_SetVideoMode(this->resolutionX, this->resolutionY, this->bitsPerPixel, this->videoFlags | this->fullscreenFlag)) == NULL) |
---|
[6979] | 365 | { |
---|
| 366 | PRINTF(1)("Could not SDL_SetVideoMode(%d, %d, %d, %d): %s\n", this->resolutionX, this->resolutionY, this->bitsPerPixel, this->videoFlags, SDL_GetError()); |
---|
| 367 | // SDL_Quit(); |
---|
| 368 | // return -1; |
---|
[8316] | 369 | return -1; |
---|
[6979] | 370 | } |
---|
| 371 | glViewport(0, 0, width, height); // Reset The Current Viewport |
---|
[5755] | 372 | |
---|
[5790] | 373 | #ifdef __WIN32__ |
---|
[6979] | 374 | // REBUILDING TEXTURES (ON WINDOWS CONTEXT SWITCH) |
---|
| 375 | const std::list<BaseObject*>* texList = ClassList::getList(CL_TEXTURE); |
---|
| 376 | if (texList != NULL) |
---|
| 377 | { |
---|
| 378 | std::list<BaseObject*>::const_iterator reTex; |
---|
| 379 | for (reTex = texList->begin(); reTex != texList->end(); reTex++) |
---|
| 380 | dynamic_cast<Texture*>(*reTex)->rebuild(); |
---|
| 381 | } |
---|
| 382 | // REBUILDING MODELS |
---|
| 383 | const std::list<BaseObject*>* modelList = ClassList::getList(CL_STATIC_MODEL); |
---|
| 384 | if (texList != NULL) |
---|
| 385 | { |
---|
| 386 | std::list<BaseObject*>::const_iterator reModel; |
---|
| 387 | for (reModel = modelList->begin(); reModel != modelList->end(); reModel++) |
---|
| 388 | dynamic_cast<StaticModel*>(*reModel)->rebuild(); |
---|
| 389 | } |
---|
[5755] | 390 | #endif /* __WIN32__ */ |
---|
[8316] | 391 | return 1; |
---|
[4135] | 392 | } |
---|
[3610] | 393 | |
---|
[4458] | 394 | /** |
---|
[7871] | 395 | * @brief sets Fullscreen mode |
---|
[4836] | 396 | * @param fullscreen true if fullscreen, false if windowed |
---|
[4458] | 397 | */ |
---|
[4135] | 398 | void GraphicsEngine::setFullscreen(bool fullscreen) |
---|
| 399 | { |
---|
[4768] | 400 | if (fullscreen) |
---|
[5789] | 401 | this->fullscreenFlag = SDL_FULLSCREEN; |
---|
[4768] | 402 | else |
---|
[5789] | 403 | this->fullscreenFlag = 0; |
---|
[4135] | 404 | this->setResolution(this->resolutionX, this->resolutionY, this->bitsPerPixel); |
---|
[3543] | 405 | } |
---|
[3617] | 406 | |
---|
[8518] | 407 | void GraphicsEngine::toggleFullscreen() |
---|
| 408 | { |
---|
| 409 | if (this->fullscreenFlag == SDL_FULLSCREEN) |
---|
| 410 | this->fullscreenFlag = 0; |
---|
| 411 | else |
---|
| 412 | this->fullscreenFlag = SDL_FULLSCREEN; |
---|
| 413 | this->setResolution(this->resolutionX, this->resolutionY, this->bitsPerPixel); |
---|
| 414 | } |
---|
| 415 | |
---|
| 416 | |
---|
[4458] | 417 | /** |
---|
[7871] | 418 | * @brief sets the background color |
---|
[4836] | 419 | * @param red the red part of the background |
---|
| 420 | * @param blue the blue part of the background |
---|
| 421 | * @param green the green part of the background |
---|
| 422 | * @param alpha the alpha part of the background |
---|
[5346] | 423 | */ |
---|
[4374] | 424 | void GraphicsEngine::setBackgroundColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha) |
---|
| 425 | { |
---|
| 426 | glClearColor(red, green, blue, alpha); |
---|
| 427 | } |
---|
| 428 | |
---|
[3621] | 429 | /** |
---|
[7871] | 430 | * @brief Signalhandler, for when the resolution has changed |
---|
[4836] | 431 | * @param resizeInfo SDL information about the size of the new screen size |
---|
[5346] | 432 | */ |
---|
[8316] | 433 | void GraphicsEngine::resolutionChanged(const SDL_ResizeEvent& resizeInfo) |
---|
[3619] | 434 | { |
---|
[4782] | 435 | this->setResolution(resizeInfo.w, resizeInfo.h, this->bitsPerPixel); |
---|
[3619] | 436 | } |
---|
[3617] | 437 | |
---|
[3622] | 438 | /** |
---|
[7871] | 439 | * @brief entering 2D Mode |
---|
| 440 | * this is a GL-Projection-mode, that is orthogonal, for placing the font in fron of everything else |
---|
| 441 | */ |
---|
[4746] | 442 | void GraphicsEngine::enter2DMode() |
---|
[3790] | 443 | { |
---|
[4955] | 444 | //GraphicsEngine::storeMatrices(); |
---|
[3790] | 445 | SDL_Surface *screen = SDL_GetVideoSurface(); |
---|
[4597] | 446 | |
---|
[3790] | 447 | /* Note, there may be other things you need to change, |
---|
| 448 | depending on how you have your OpenGL state set up. |
---|
| 449 | */ |
---|
| 450 | glPushAttrib(GL_ENABLE_BIT); |
---|
| 451 | glDisable(GL_DEPTH_TEST); |
---|
| 452 | glDisable(GL_CULL_FACE); |
---|
| 453 | glDisable(GL_LIGHTING); // will be set back when leaving 2D-mode |
---|
[3617] | 454 | |
---|
[3790] | 455 | glMatrixMode(GL_PROJECTION); |
---|
| 456 | glPushMatrix(); |
---|
| 457 | glLoadIdentity(); |
---|
| 458 | glOrtho(0.0, (GLdouble)screen->w, (GLdouble)screen->h, 0.0, 0.0, 1.0); |
---|
[4597] | 459 | |
---|
[3790] | 460 | glMatrixMode(GL_MODELVIEW); |
---|
| 461 | glPushMatrix(); |
---|
| 462 | glLoadIdentity(); |
---|
| 463 | } |
---|
[3617] | 464 | |
---|
[3790] | 465 | /** |
---|
[7871] | 466 | * @brief leaves the 2DMode again also @see Font::enter2DMode() |
---|
[5346] | 467 | */ |
---|
[4746] | 468 | void GraphicsEngine::leave2DMode() |
---|
[3790] | 469 | { |
---|
[4597] | 470 | |
---|
[4834] | 471 | glMatrixMode(GL_MODELVIEW); |
---|
[3790] | 472 | glPopMatrix(); |
---|
[4597] | 473 | |
---|
[6780] | 474 | glMatrixMode(GL_PROJECTION); |
---|
| 475 | glPopMatrix(); |
---|
| 476 | |
---|
[3790] | 477 | glPopAttrib(); |
---|
| 478 | } |
---|
[3622] | 479 | |
---|
[7871] | 480 | /** |
---|
| 481 | * @brief changes to wireframe-mode. |
---|
| 482 | */ |
---|
[6522] | 483 | void GraphicsEngine::wireframe() |
---|
| 484 | { |
---|
[6523] | 485 | glPolygonMode(GL_FRONT, GL_LINE); |
---|
[6522] | 486 | } |
---|
| 487 | |
---|
[3844] | 488 | /** |
---|
[7871] | 489 | * @brief stores the GL_matrices |
---|
[5346] | 490 | */ |
---|
[4746] | 491 | void GraphicsEngine::storeMatrices() |
---|
[3844] | 492 | { |
---|
| 493 | glGetDoublev(GL_PROJECTION_MATRIX, GraphicsEngine::projMat); |
---|
| 494 | glGetDoublev(GL_MODELVIEW_MATRIX, GraphicsEngine::modMat); |
---|
| 495 | glGetIntegerv(GL_VIEWPORT, GraphicsEngine::viewPort); |
---|
| 496 | } |
---|
[3622] | 497 | |
---|
[3844] | 498 | //! the stored ModelView Matrix. |
---|
| 499 | GLdouble GraphicsEngine::modMat[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; |
---|
| 500 | //! the stored Projection Matrix |
---|
| 501 | GLdouble GraphicsEngine::projMat[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; |
---|
| 502 | //! The ViewPort |
---|
| 503 | GLint GraphicsEngine::viewPort[4] = {0,0,0,0}; |
---|
[3790] | 504 | |
---|
| 505 | |
---|
[3844] | 506 | |
---|
[3617] | 507 | /** |
---|
[7871] | 508 | * @brief outputs all the Fullscreen modes. |
---|
[5346] | 509 | */ |
---|
[4746] | 510 | void GraphicsEngine::listModes() |
---|
[3617] | 511 | { |
---|
| 512 | /* Get available fullscreen/hardware modes */ |
---|
| 513 | this->videoModes=SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_HWSURFACE); |
---|
[4597] | 514 | |
---|
[3617] | 515 | /* Check is there are any modes available */ |
---|
[6979] | 516 | if(this->videoModes == (SDL_Rect **)0) |
---|
| 517 | { |
---|
[3617] | 518 | PRINTF(1)("No modes available!\n"); |
---|
| 519 | exit(-1); |
---|
| 520 | } |
---|
[4597] | 521 | |
---|
[3617] | 522 | /* Check if our resolution is restricted */ |
---|
[6979] | 523 | if(this->videoModes == (SDL_Rect **)-1) |
---|
| 524 | { |
---|
[4135] | 525 | PRINTF(2)("All resolutions available.\n"); |
---|
[3617] | 526 | } |
---|
[6979] | 527 | else |
---|
| 528 | { |
---|
[3617] | 529 | /* Print valid modes */ |
---|
| 530 | PRINT(0)("Available Resoulution Modes are\n"); |
---|
| 531 | for(int i = 0; this->videoModes[i]; ++i) |
---|
[4135] | 532 | PRINT(4)(" | %d x %d\n", this->videoModes[i]->w, this->videoModes[i]->h); |
---|
[3617] | 533 | } |
---|
[4245] | 534 | } |
---|
| 535 | |
---|
[4458] | 536 | /** |
---|
[7871] | 537 | * @brief checks wether a certain extension is availiable |
---|
[5261] | 538 | * @param extension the Extension to check for (ex. GL_ARB_texture_env_dot3) |
---|
| 539 | * @return true if it is, false otherwise |
---|
| 540 | */ |
---|
[7221] | 541 | bool GraphicsEngine::hwSupportsEXT(const std::string& extension) |
---|
[5261] | 542 | { |
---|
[7319] | 543 | for (unsigned int i = 0; i < this->hwExtensions.size(); i++) |
---|
[7221] | 544 | if ( this->hwExtensions.getString(i) == extension) |
---|
| 545 | return true; |
---|
[5261] | 546 | return false; |
---|
| 547 | } |
---|
| 548 | |
---|
| 549 | /** |
---|
[7871] | 550 | * @brief updates everything that is to be updated in the GraphicsEngine |
---|
[5084] | 551 | */ |
---|
| 552 | void GraphicsEngine::update(float dt) |
---|
| 553 | { |
---|
[5406] | 554 | Render2D::getInstance()->update(dt); |
---|
[5084] | 555 | } |
---|
| 556 | |
---|
| 557 | |
---|
| 558 | /** |
---|
[7871] | 559 | * @brief ticks the Text |
---|
[4836] | 560 | * @param dt the time passed |
---|
[5346] | 561 | */ |
---|
| 562 | void GraphicsEngine::tick(float dt) |
---|
[4245] | 563 | { |
---|
[4458] | 564 | if( unlikely(this->bDisplayFPS)) |
---|
[4849] | 565 | { |
---|
| 566 | this->currentFPS = 1.0/dt; |
---|
| 567 | if( unlikely(this->currentFPS > this->maxFPS)) this->maxFPS = this->currentFPS; |
---|
| 568 | if( unlikely(this->currentFPS < this->minFPS)) this->minFPS = this->currentFPS; |
---|
[4597] | 569 | |
---|
[4536] | 570 | #ifndef NO_TEXT |
---|
[6979] | 571 | char tmpChar1[20]; |
---|
| 572 | sprintf(tmpChar1, "Current: %4.0f", this->currentFPS); |
---|
| 573 | this->geTextCFPS->setText(tmpChar1); |
---|
| 574 | char tmpChar2[20]; |
---|
| 575 | sprintf(tmpChar2, "Max: %4.0f", this->maxFPS); |
---|
| 576 | this->geTextMaxFPS->setText(tmpChar2); |
---|
| 577 | char tmpChar3[20]; |
---|
| 578 | sprintf(tmpChar3, "Min: %4.0f", this->minFPS); |
---|
| 579 | this->geTextMinFPS->setText(tmpChar3); |
---|
[4536] | 580 | #endif /* NO_TEXT */ |
---|
[4849] | 581 | |
---|
[6815] | 582 | } |
---|
[4849] | 583 | |
---|
| 584 | Render2D::getInstance()->tick(dt); |
---|
[6815] | 585 | |
---|
| 586 | // tick the graphics effects |
---|
[6979] | 587 | if (this->graphicsEffects != NULL || (this->graphicsEffects = ClassList::getList(CL_GRAPHICS_EFFECT)) != NULL) |
---|
| 588 | { |
---|
| 589 | std::list<BaseObject*>::const_iterator it; |
---|
| 590 | for (it = this->graphicsEffects->begin(); it != this->graphicsEffects->end(); it++) |
---|
| 591 | dynamic_cast<GraphicsEffect*>(*it)->tick(dt); |
---|
| 592 | } |
---|
[4245] | 593 | } |
---|
[4597] | 594 | |
---|
[7871] | 595 | /** |
---|
| 596 | * @brief draws all Elements that should be displayed on the Background. |
---|
| 597 | */ |
---|
[7840] | 598 | void GraphicsEngine::drawBackgroundElements() const |
---|
| 599 | { |
---|
[7919] | 600 | GraphicsEngine::storeMatrices(); |
---|
| 601 | |
---|
[7840] | 602 | Render2D::getInstance()->draw(E2D_LAYER_BELOW_ALL, E2D_LAYER_BELOW_ALL); |
---|
| 603 | } |
---|
[4849] | 604 | |
---|
| 605 | void GraphicsEngine::draw() const |
---|
| 606 | { |
---|
[7221] | 607 | // LightManager::getInstance()->draw(); |
---|
[6778] | 608 | |
---|
[6979] | 609 | if (this->graphicsEffects != NULL) |
---|
| 610 | { |
---|
| 611 | //draw the graphics effects |
---|
[9351] | 612 | std::list<BaseObject*>::const_iterator it; |
---|
[6979] | 613 | for (it = this->graphicsEffects->begin(); it != this->graphicsEffects->end(); it++) |
---|
| 614 | dynamic_cast<GraphicsEffect*>(*it)->draw(); |
---|
| 615 | } |
---|
[7428] | 616 | Shader::suspendShader(); |
---|
[7840] | 617 | Render2D::getInstance()->draw(E2D_LAYER_BOTTOM, E2D_LAYER_ABOVE_ALL); |
---|
[7428] | 618 | Shader::restoreShader(); |
---|
[4849] | 619 | } |
---|
| 620 | |
---|
[6142] | 621 | |
---|
[9351] | 622 | void GraphicsEngine::toggleFPSdisplay() |
---|
| 623 | { |
---|
| 624 | this->displayFPS(!this->bDisplayFPS); |
---|
| 625 | } |
---|
| 626 | |
---|
| 627 | |
---|
[4458] | 628 | /** |
---|
[7871] | 629 | * @brief displays the Frames per second |
---|
[4836] | 630 | * @param display if the text should be displayed |
---|
[4458] | 631 | */ |
---|
[4245] | 632 | void GraphicsEngine::displayFPS(bool display) |
---|
| 633 | { |
---|
[4536] | 634 | #ifndef NO_TEXT |
---|
[5346] | 635 | if( display ) |
---|
| 636 | { |
---|
[6979] | 637 | if (this->geTextCFPS == NULL) |
---|
| 638 | { |
---|
| 639 | this->geTextCFPS = new Text("fonts/arial_black.ttf", 15); |
---|
| 640 | this->geTextCFPS->setName("curFPS"); |
---|
| 641 | this->geTextCFPS->setAlignment(TEXT_ALIGN_LEFT); |
---|
| 642 | this->geTextCFPS->setAbsCoor2D(5, 0); |
---|
| 643 | } |
---|
| 644 | if (this->geTextMaxFPS == NULL) |
---|
| 645 | { |
---|
| 646 | this->geTextMaxFPS = new Text("fonts/arial_black.ttf", 15); |
---|
| 647 | this->geTextMaxFPS->setName("MaxFPS"); |
---|
| 648 | this->geTextMaxFPS->setAlignment(TEXT_ALIGN_LEFT); |
---|
| 649 | this->geTextMaxFPS->setAbsCoor2D(5, 20); |
---|
| 650 | } |
---|
| 651 | if (this->geTextMinFPS == NULL) |
---|
| 652 | { |
---|
| 653 | this->geTextMinFPS = new Text("fonts/arial_black.ttf", 15); |
---|
| 654 | this->geTextMinFPS->setName("MinFPS"); |
---|
| 655 | this->geTextMinFPS->setAlignment(TEXT_ALIGN_LEFT); |
---|
| 656 | this->geTextMinFPS->setAbsCoor2D(5, 40); |
---|
| 657 | } |
---|
[5346] | 658 | } |
---|
[6979] | 659 | else |
---|
[5346] | 660 | { |
---|
[6979] | 661 | delete this->geTextCFPS; |
---|
| 662 | this->geTextCFPS = NULL; |
---|
| 663 | delete this->geTextMaxFPS; |
---|
| 664 | this->geTextMaxFPS = NULL; |
---|
| 665 | delete this->geTextMinFPS; |
---|
| 666 | this->geTextMinFPS = NULL; |
---|
[5346] | 667 | } |
---|
| 668 | this->bDisplayFPS = display; |
---|
| 669 | #else |
---|
| 670 | this->bDisplayFPS = false; |
---|
[4536] | 671 | #endif /* NO_TEXT */ |
---|
[3617] | 672 | } |
---|
[4245] | 673 | |
---|
[4597] | 674 | |
---|
[4817] | 675 | /** |
---|
[7871] | 676 | * @brief processes the events for the GraphicsEngine class |
---|
| 677 | * @param the event to handle |
---|
[4817] | 678 | */ |
---|
| 679 | void GraphicsEngine::process(const Event &event) |
---|
| 680 | { |
---|
| 681 | switch (event.type) |
---|
| 682 | { |
---|
[8316] | 683 | case EV_VIDEO_RESIZE: |
---|
| 684 | this->resolutionChanged(event.resize); |
---|
| 685 | break; |
---|
[4817] | 686 | } |
---|
[6753] | 687 | } |
---|