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