[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" |
---|
[4817] | 20 | #include "event_handler.h" |
---|
[6441] | 21 | #include "state.h" |
---|
[1853] | 22 | |
---|
[6142] | 23 | #include "world_entity.h" |
---|
| 24 | |
---|
[4849] | 25 | #include "render_2d.h" |
---|
[5347] | 26 | #include "text_engine.h" |
---|
[4850] | 27 | #include "light.h" |
---|
[5347] | 28 | #include "shader.h" |
---|
[3611] | 29 | #include "debug.h" |
---|
[5347] | 30 | |
---|
[7256] | 31 | #include "util/preferences.h" |
---|
[4770] | 32 | #include "substring.h" |
---|
[5344] | 33 | #include "text.h" |
---|
[4770] | 34 | |
---|
[5819] | 35 | #include "globals.h" |
---|
[5857] | 36 | #include "texture.h" |
---|
[5755] | 37 | |
---|
[6753] | 38 | #include "effects/graphics_effect.h" |
---|
[6772] | 39 | #include "effects/fog_effect.h" |
---|
[6884] | 40 | #include "effects/lense_flare.h" |
---|
[6753] | 41 | |
---|
[6522] | 42 | #include "shell_command.h" |
---|
| 43 | |
---|
[6815] | 44 | |
---|
| 45 | #include "parser/tinyxml/tinyxml.h" |
---|
[7193] | 46 | #include "util/loading/load_param.h" |
---|
| 47 | #include "util/loading/factory.h" |
---|
[6979] | 48 | #include "class_list.h" |
---|
[6815] | 49 | |
---|
[5755] | 50 | #ifdef __WIN32__ |
---|
[6162] | 51 | #include "static_model.h" |
---|
[5755] | 52 | #endif |
---|
[1856] | 53 | using namespace std; |
---|
[1853] | 54 | |
---|
[6522] | 55 | SHELL_COMMAND(wireframe, GraphicsEngine, wireframe); |
---|
| 56 | |
---|
[6976] | 57 | |
---|
[3245] | 58 | /** |
---|
[4836] | 59 | * standard constructor |
---|
[5262] | 60 | */ |
---|
[4597] | 61 | GraphicsEngine::GraphicsEngine () |
---|
[3365] | 62 | { |
---|
[4597] | 63 | this->setClassID(CL_GRAPHICS_ENGINE, "GraphicsEngine"); |
---|
| 64 | this->setName("GraphicsEngine"); |
---|
[4784] | 65 | |
---|
| 66 | this->isInit = false; |
---|
| 67 | |
---|
[4245] | 68 | this->bDisplayFPS = false; |
---|
| 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 | /** |
---|
[4836] | 87 | * The Pointer to this GraphicsEngine |
---|
[3621] | 88 | */ |
---|
[3611] | 89 | GraphicsEngine* GraphicsEngine::singletonRef = NULL; |
---|
[3610] | 90 | |
---|
[3621] | 91 | /** |
---|
[4836] | 92 | * 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 | /** |
---|
[6815] | 113 | * loads the GraphicsEngine Specific Parameters. |
---|
| 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 | /** |
---|
[4830] | 141 | * initializes the GraphicsEngine with default settings. |
---|
| 142 | */ |
---|
[4784] | 143 | int GraphicsEngine::init() |
---|
| 144 | { |
---|
[4830] | 145 | if (this->isInit) |
---|
| 146 | return -1; |
---|
| 147 | this->initVideo(640, 480, 16); |
---|
[4784] | 148 | } |
---|
| 149 | |
---|
[3611] | 150 | /** |
---|
[4830] | 151 | * loads the GraphicsEngine's settings from a given ini-file and section |
---|
| 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"); |
---|
| 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); |
---|
| 170 | 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 | /** |
---|
[4836] | 194 | * 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"); |
---|
[6979] | 206 | // return -1; |
---|
[5024] | 207 | } |
---|
[3617] | 208 | // initialize SDL_GL-settings |
---|
| 209 | this->setGLattribs(); |
---|
[3610] | 210 | |
---|
[3617] | 211 | // setting the Video Flags. |
---|
[5225] | 212 | this->videoFlags = SDL_OPENGL | SDL_HWPALETTE | SDL_RESIZABLE | SDL_DOUBLEBUF | SDL_GL_DOUBLEBUFFER; |
---|
[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; |
---|
[3365] | 241 | } |
---|
[1853] | 242 | |
---|
[4770] | 243 | /** |
---|
[4619] | 244 | * sets the Window Captions and the Name of the icon. |
---|
| 245 | * @param windowName The name of the Window |
---|
| 246 | * @param icon The name of the Icon on the Disc |
---|
| 247 | */ |
---|
[7221] | 248 | void GraphicsEngine::setWindowName(const std::string& windowName, const std::string& icon) |
---|
[4619] | 249 | { |
---|
[7221] | 250 | SDL_Surface* iconSurf = SDL_LoadBMP(icon.c_str()); |
---|
[5240] | 251 | if (iconSurf != NULL) |
---|
| 252 | { |
---|
[5241] | 253 | Uint32 colorkey = SDL_MapRGB(iconSurf->format, 0, 0, 0); |
---|
[5240] | 254 | SDL_SetColorKey(iconSurf, SDL_SRCCOLORKEY, colorkey); |
---|
[7221] | 255 | SDL_WM_SetIcon(iconSurf, NULL); |
---|
[5240] | 256 | SDL_FreeSurface(iconSurf); |
---|
| 257 | } |
---|
[5024] | 258 | |
---|
[7221] | 259 | SDL_WM_SetCaption (windowName.c_str(), icon.c_str()); |
---|
[4619] | 260 | } |
---|
| 261 | |
---|
| 262 | |
---|
| 263 | /** |
---|
[4836] | 264 | * Sets the GL-attributes |
---|
[5346] | 265 | */ |
---|
[4746] | 266 | int GraphicsEngine::setGLattribs() |
---|
[3617] | 267 | { |
---|
| 268 | // Set video mode |
---|
| 269 | // TO DO: parse arguments for settings |
---|
| 270 | //SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5); |
---|
| 271 | //SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5); |
---|
| 272 | //SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5); |
---|
| 273 | //SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); |
---|
| 274 | |
---|
[4597] | 275 | |
---|
| 276 | SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); |
---|
| 277 | SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16); |
---|
| 278 | SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, 0); |
---|
[3617] | 279 | SDL_GL_SetAttribute( SDL_GL_ACCUM_RED_SIZE, 0); |
---|
| 280 | SDL_GL_SetAttribute( SDL_GL_ACCUM_GREEN_SIZE, 0); |
---|
| 281 | SDL_GL_SetAttribute( SDL_GL_ACCUM_BLUE_SIZE, 0); |
---|
| 282 | SDL_GL_SetAttribute( SDL_GL_ACCUM_ALPHA_SIZE, 0); |
---|
| 283 | } |
---|
| 284 | |
---|
[5261] | 285 | /** |
---|
| 286 | * grabs the Hardware Specifics |
---|
| 287 | * checks for all the different HW-types |
---|
| 288 | */ |
---|
[5260] | 289 | void GraphicsEngine::grabHardwareSettings() |
---|
| 290 | { |
---|
| 291 | const char* renderer = (const char*) glGetString(GL_RENDERER); |
---|
| 292 | const char* vendor = (const char*) glGetString(GL_VENDOR); |
---|
| 293 | const char* version = (const char*) glGetString(GL_VERSION); |
---|
| 294 | const char* extensions = (const char*) glGetString(GL_EXTENSIONS); |
---|
| 295 | |
---|
[6979] | 296 | // printf("%s %s %s\n %s", renderer, vendor, version, extensions); |
---|
[5260] | 297 | |
---|
[7221] | 298 | if (renderer != NULL) |
---|
[5260] | 299 | { |
---|
[7221] | 300 | this->hwRenderer == renderer; |
---|
[5260] | 301 | } |
---|
[7221] | 302 | if (vendor != NULL) |
---|
[5260] | 303 | { |
---|
[7221] | 304 | this->hwVendor == vendor; |
---|
[5260] | 305 | } |
---|
[7221] | 306 | if (version != NULL) |
---|
[5260] | 307 | { |
---|
[7221] | 308 | this->hwVersion == version; |
---|
[5260] | 309 | } |
---|
| 310 | |
---|
[7221] | 311 | if (extensions != NULL) |
---|
| 312 | this->hwExtensions.split(extensions, " \n\t,"); |
---|
[5260] | 313 | |
---|
[6634] | 314 | PRINT(4)("Running on : vendor: %s, renderer: %s, version:%s\n", vendor, renderer, version); |
---|
[5260] | 315 | PRINT(4)("Extensions:\n"); |
---|
[7319] | 316 | for (unsigned int i = 0; i < this->hwExtensions.size(); i++) |
---|
| 317 | PRINT(4)("%d: %s\n", i, this->hwExtensions[i].c_str()); |
---|
[5263] | 318 | |
---|
| 319 | |
---|
| 320 | // inizializing GLEW |
---|
| 321 | GLenum err = glewInit(); |
---|
| 322 | if (GLEW_OK != err) |
---|
| 323 | { |
---|
| 324 | /* Problem: glewInit failed, something is seriously wrong. */ |
---|
| 325 | PRINTF(1)("%s\n", glewGetErrorString(err)); |
---|
| 326 | } |
---|
| 327 | PRINTF(4)("Status: Using GLEW %s\n", glewGetString(GLEW_VERSION)); |
---|
[5260] | 328 | } |
---|
| 329 | |
---|
[6162] | 330 | |
---|
[3621] | 331 | /** |
---|
[4836] | 332 | * sets the Resolution of the Screen to display the Graphics to. |
---|
| 333 | * @param width The width of the window |
---|
| 334 | * @param height The height of the window |
---|
| 335 | * @param bpp bits per pixel |
---|
[5346] | 336 | */ |
---|
[3619] | 337 | int GraphicsEngine::setResolution(int width, int height, int bpp) |
---|
[3610] | 338 | { |
---|
[3611] | 339 | this->resolutionX = width; |
---|
| 340 | this->resolutionY = height; |
---|
| 341 | this->bitsPerPixel = bpp; |
---|
[6441] | 342 | State::setResolution( width, height); |
---|
[4739] | 343 | |
---|
[5255] | 344 | if (this->screen != NULL) |
---|
[5237] | 345 | SDL_FreeSurface(screen); |
---|
[4769] | 346 | if((this->screen = SDL_SetVideoMode(this->resolutionX, this->resolutionY, this->bitsPerPixel, this->videoFlags | this->fullscreenFlag)) == NULL) |
---|
[6979] | 347 | { |
---|
| 348 | PRINTF(1)("Could not SDL_SetVideoMode(%d, %d, %d, %d): %s\n", this->resolutionX, this->resolutionY, this->bitsPerPixel, this->videoFlags, SDL_GetError()); |
---|
| 349 | // SDL_Quit(); |
---|
| 350 | // return -1; |
---|
| 351 | } |
---|
| 352 | glViewport(0, 0, width, height); // Reset The Current Viewport |
---|
[5755] | 353 | |
---|
[5790] | 354 | #ifdef __WIN32__ |
---|
[6979] | 355 | // REBUILDING TEXTURES (ON WINDOWS CONTEXT SWITCH) |
---|
| 356 | const std::list<BaseObject*>* texList = ClassList::getList(CL_TEXTURE); |
---|
| 357 | if (texList != NULL) |
---|
| 358 | { |
---|
| 359 | std::list<BaseObject*>::const_iterator reTex; |
---|
| 360 | for (reTex = texList->begin(); reTex != texList->end(); reTex++) |
---|
| 361 | dynamic_cast<Texture*>(*reTex)->rebuild(); |
---|
| 362 | } |
---|
| 363 | // REBUILDING MODELS |
---|
| 364 | const std::list<BaseObject*>* modelList = ClassList::getList(CL_STATIC_MODEL); |
---|
| 365 | if (texList != NULL) |
---|
| 366 | { |
---|
| 367 | std::list<BaseObject*>::const_iterator reModel; |
---|
| 368 | for (reModel = modelList->begin(); reModel != modelList->end(); reModel++) |
---|
| 369 | dynamic_cast<StaticModel*>(*reModel)->rebuild(); |
---|
| 370 | } |
---|
[5755] | 371 | #endif /* __WIN32__ */ |
---|
[4135] | 372 | } |
---|
[3610] | 373 | |
---|
[4458] | 374 | /** |
---|
[4836] | 375 | * sets Fullscreen mode |
---|
| 376 | * @param fullscreen true if fullscreen, false if windowed |
---|
[4458] | 377 | */ |
---|
[4135] | 378 | void GraphicsEngine::setFullscreen(bool fullscreen) |
---|
| 379 | { |
---|
[4768] | 380 | if (fullscreen) |
---|
[5789] | 381 | this->fullscreenFlag = SDL_FULLSCREEN; |
---|
[4768] | 382 | else |
---|
[5789] | 383 | this->fullscreenFlag = 0; |
---|
[4135] | 384 | this->setResolution(this->resolutionX, this->resolutionY, this->bitsPerPixel); |
---|
[3543] | 385 | } |
---|
[3617] | 386 | |
---|
[4458] | 387 | /** |
---|
[4836] | 388 | * sets the background color |
---|
| 389 | * @param red the red part of the background |
---|
| 390 | * @param blue the blue part of the background |
---|
| 391 | * @param green the green part of the background |
---|
| 392 | * @param alpha the alpha part of the background |
---|
[5346] | 393 | */ |
---|
[4374] | 394 | void GraphicsEngine::setBackgroundColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha) |
---|
| 395 | { |
---|
| 396 | glClearColor(red, green, blue, alpha); |
---|
| 397 | } |
---|
| 398 | |
---|
[3621] | 399 | /** |
---|
[4836] | 400 | * Signalhandler, for when the resolution has changed |
---|
| 401 | * @param resizeInfo SDL information about the size of the new screen size |
---|
[5346] | 402 | */ |
---|
[4782] | 403 | int GraphicsEngine::resolutionChanged(const SDL_ResizeEvent& resizeInfo) |
---|
[3619] | 404 | { |
---|
[4782] | 405 | this->setResolution(resizeInfo.w, resizeInfo.h, this->bitsPerPixel); |
---|
[3619] | 406 | } |
---|
[3617] | 407 | |
---|
[3622] | 408 | /** |
---|
[4833] | 409 | |
---|
| 410 | /** |
---|
[4836] | 411 | * entering 2D Mode |
---|
[4597] | 412 | |
---|
[3790] | 413 | this is a GL-Projection-mode, that is orthogonal, for placing the font in fron of everything else |
---|
| 414 | */ |
---|
[4746] | 415 | void GraphicsEngine::enter2DMode() |
---|
[3790] | 416 | { |
---|
[4955] | 417 | //GraphicsEngine::storeMatrices(); |
---|
[3790] | 418 | SDL_Surface *screen = SDL_GetVideoSurface(); |
---|
[4597] | 419 | |
---|
[3790] | 420 | /* Note, there may be other things you need to change, |
---|
| 421 | depending on how you have your OpenGL state set up. |
---|
| 422 | */ |
---|
| 423 | glPushAttrib(GL_ENABLE_BIT); |
---|
| 424 | glDisable(GL_DEPTH_TEST); |
---|
| 425 | glDisable(GL_CULL_FACE); |
---|
| 426 | glDisable(GL_LIGHTING); // will be set back when leaving 2D-mode |
---|
[3617] | 427 | |
---|
[3790] | 428 | glMatrixMode(GL_PROJECTION); |
---|
| 429 | glPushMatrix(); |
---|
| 430 | glLoadIdentity(); |
---|
| 431 | glOrtho(0.0, (GLdouble)screen->w, (GLdouble)screen->h, 0.0, 0.0, 1.0); |
---|
[4597] | 432 | |
---|
[3790] | 433 | glMatrixMode(GL_MODELVIEW); |
---|
| 434 | glPushMatrix(); |
---|
| 435 | glLoadIdentity(); |
---|
| 436 | } |
---|
[3617] | 437 | |
---|
[3790] | 438 | /** |
---|
[5346] | 439 | * leaves the 2DMode again also @see Font::enter2DMode() |
---|
| 440 | */ |
---|
[4746] | 441 | void GraphicsEngine::leave2DMode() |
---|
[3790] | 442 | { |
---|
[4597] | 443 | |
---|
[4834] | 444 | glMatrixMode(GL_MODELVIEW); |
---|
[3790] | 445 | glPopMatrix(); |
---|
[4597] | 446 | |
---|
[6780] | 447 | glMatrixMode(GL_PROJECTION); |
---|
| 448 | glPopMatrix(); |
---|
| 449 | |
---|
[3790] | 450 | glPopAttrib(); |
---|
| 451 | } |
---|
[3622] | 452 | |
---|
[6522] | 453 | void GraphicsEngine::wireframe() |
---|
| 454 | { |
---|
[6523] | 455 | glPolygonMode(GL_FRONT, GL_LINE); |
---|
[6522] | 456 | } |
---|
| 457 | |
---|
[3844] | 458 | /** |
---|
[4836] | 459 | * stores the GL_matrices |
---|
[5346] | 460 | */ |
---|
[4746] | 461 | void GraphicsEngine::storeMatrices() |
---|
[3844] | 462 | { |
---|
| 463 | glGetDoublev(GL_PROJECTION_MATRIX, GraphicsEngine::projMat); |
---|
| 464 | glGetDoublev(GL_MODELVIEW_MATRIX, GraphicsEngine::modMat); |
---|
| 465 | glGetIntegerv(GL_VIEWPORT, GraphicsEngine::viewPort); |
---|
| 466 | } |
---|
[3622] | 467 | |
---|
[3844] | 468 | //! the stored ModelView Matrix. |
---|
| 469 | GLdouble GraphicsEngine::modMat[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; |
---|
| 470 | //! the stored Projection Matrix |
---|
| 471 | GLdouble GraphicsEngine::projMat[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; |
---|
| 472 | //! The ViewPort |
---|
| 473 | GLint GraphicsEngine::viewPort[4] = {0,0,0,0}; |
---|
[3790] | 474 | |
---|
| 475 | |
---|
[3844] | 476 | |
---|
[3617] | 477 | /** |
---|
[4836] | 478 | * outputs all the Fullscreen modes. |
---|
[5346] | 479 | */ |
---|
[4746] | 480 | void GraphicsEngine::listModes() |
---|
[3617] | 481 | { |
---|
| 482 | /* Get available fullscreen/hardware modes */ |
---|
| 483 | this->videoModes=SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_HWSURFACE); |
---|
[4597] | 484 | |
---|
[3617] | 485 | /* Check is there are any modes available */ |
---|
[6979] | 486 | if(this->videoModes == (SDL_Rect **)0) |
---|
| 487 | { |
---|
[3617] | 488 | PRINTF(1)("No modes available!\n"); |
---|
| 489 | exit(-1); |
---|
| 490 | } |
---|
[4597] | 491 | |
---|
[3617] | 492 | /* Check if our resolution is restricted */ |
---|
[6979] | 493 | if(this->videoModes == (SDL_Rect **)-1) |
---|
| 494 | { |
---|
[4135] | 495 | PRINTF(2)("All resolutions available.\n"); |
---|
[3617] | 496 | } |
---|
[6979] | 497 | else |
---|
| 498 | { |
---|
[3617] | 499 | /* Print valid modes */ |
---|
| 500 | PRINT(0)("Available Resoulution Modes are\n"); |
---|
| 501 | for(int i = 0; this->videoModes[i]; ++i) |
---|
[4135] | 502 | PRINT(4)(" | %d x %d\n", this->videoModes[i]->w, this->videoModes[i]->h); |
---|
[3617] | 503 | } |
---|
[4245] | 504 | } |
---|
| 505 | |
---|
[4458] | 506 | /** |
---|
[5261] | 507 | * checks wether a certain extension is availiable |
---|
| 508 | * @param extension the Extension to check for (ex. GL_ARB_texture_env_dot3) |
---|
| 509 | * @return true if it is, false otherwise |
---|
| 510 | */ |
---|
[7221] | 511 | bool GraphicsEngine::hwSupportsEXT(const std::string& extension) |
---|
[5261] | 512 | { |
---|
[7319] | 513 | for (unsigned int i = 0; i < this->hwExtensions.size(); i++) |
---|
[7221] | 514 | if ( this->hwExtensions.getString(i) == extension) |
---|
| 515 | return true; |
---|
[5261] | 516 | return false; |
---|
| 517 | } |
---|
| 518 | |
---|
| 519 | /** |
---|
[5084] | 520 | * updates everything that is to be updated in the GraphicsEngine |
---|
| 521 | */ |
---|
| 522 | void GraphicsEngine::update(float dt) |
---|
| 523 | { |
---|
[5406] | 524 | Render2D::getInstance()->update(dt); |
---|
[5084] | 525 | } |
---|
| 526 | |
---|
| 527 | |
---|
| 528 | /** |
---|
[4836] | 529 | * ticks the Text |
---|
| 530 | * @param dt the time passed |
---|
[5346] | 531 | */ |
---|
| 532 | void GraphicsEngine::tick(float dt) |
---|
[4245] | 533 | { |
---|
[4458] | 534 | if( unlikely(this->bDisplayFPS)) |
---|
[4849] | 535 | { |
---|
| 536 | this->currentFPS = 1.0/dt; |
---|
| 537 | if( unlikely(this->currentFPS > this->maxFPS)) this->maxFPS = this->currentFPS; |
---|
| 538 | if( unlikely(this->currentFPS < this->minFPS)) this->minFPS = this->currentFPS; |
---|
[4597] | 539 | |
---|
[4536] | 540 | #ifndef NO_TEXT |
---|
[6979] | 541 | char tmpChar1[20]; |
---|
| 542 | sprintf(tmpChar1, "Current: %4.0f", this->currentFPS); |
---|
| 543 | this->geTextCFPS->setText(tmpChar1); |
---|
| 544 | char tmpChar2[20]; |
---|
| 545 | sprintf(tmpChar2, "Max: %4.0f", this->maxFPS); |
---|
| 546 | this->geTextMaxFPS->setText(tmpChar2); |
---|
| 547 | char tmpChar3[20]; |
---|
| 548 | sprintf(tmpChar3, "Min: %4.0f", this->minFPS); |
---|
| 549 | this->geTextMinFPS->setText(tmpChar3); |
---|
[4536] | 550 | #endif /* NO_TEXT */ |
---|
[4849] | 551 | |
---|
[6815] | 552 | } |
---|
[4849] | 553 | |
---|
| 554 | Render2D::getInstance()->tick(dt); |
---|
[6815] | 555 | |
---|
| 556 | // tick the graphics effects |
---|
[6979] | 557 | if (this->graphicsEffects != NULL || (this->graphicsEffects = ClassList::getList(CL_GRAPHICS_EFFECT)) != NULL) |
---|
| 558 | { |
---|
| 559 | std::list<BaseObject*>::const_iterator it; |
---|
| 560 | for (it = this->graphicsEffects->begin(); it != this->graphicsEffects->end(); it++) |
---|
| 561 | dynamic_cast<GraphicsEffect*>(*it)->tick(dt); |
---|
| 562 | } |
---|
[4245] | 563 | } |
---|
[4597] | 564 | |
---|
[4849] | 565 | |
---|
| 566 | void GraphicsEngine::draw() const |
---|
| 567 | { |
---|
[7221] | 568 | // LightManager::getInstance()->draw(); |
---|
[6778] | 569 | |
---|
[6979] | 570 | if (this->graphicsEffects != NULL) |
---|
| 571 | { |
---|
| 572 | //draw the graphics effects |
---|
| 573 | list<BaseObject*>::const_iterator it; |
---|
| 574 | for (it = this->graphicsEffects->begin(); it != this->graphicsEffects->end(); it++) |
---|
| 575 | dynamic_cast<GraphicsEffect*>(*it)->draw(); |
---|
| 576 | } |
---|
[7428] | 577 | GraphicsEngine::storeMatrices(); |
---|
| 578 | Shader::suspendShader(); |
---|
| 579 | |
---|
| 580 | Render2D::getInstance()->draw(E2D_LAYER_ALL); |
---|
| 581 | Shader::restoreShader(); |
---|
[4849] | 582 | } |
---|
| 583 | |
---|
[6142] | 584 | |
---|
[4458] | 585 | /** |
---|
[5346] | 586 | * displays the Frames per second |
---|
[4836] | 587 | * @param display if the text should be displayed |
---|
[4458] | 588 | */ |
---|
[4245] | 589 | void GraphicsEngine::displayFPS(bool display) |
---|
| 590 | { |
---|
[4536] | 591 | #ifndef NO_TEXT |
---|
[5346] | 592 | if( display ) |
---|
| 593 | { |
---|
[6979] | 594 | if (this->geTextCFPS == NULL) |
---|
| 595 | { |
---|
| 596 | this->geTextCFPS = new Text("fonts/arial_black.ttf", 15); |
---|
| 597 | this->geTextCFPS->setName("curFPS"); |
---|
| 598 | this->geTextCFPS->setAlignment(TEXT_ALIGN_LEFT); |
---|
| 599 | this->geTextCFPS->setAbsCoor2D(5, 0); |
---|
| 600 | } |
---|
| 601 | if (this->geTextMaxFPS == NULL) |
---|
| 602 | { |
---|
| 603 | this->geTextMaxFPS = new Text("fonts/arial_black.ttf", 15); |
---|
| 604 | this->geTextMaxFPS->setName("MaxFPS"); |
---|
| 605 | this->geTextMaxFPS->setAlignment(TEXT_ALIGN_LEFT); |
---|
| 606 | this->geTextMaxFPS->setAbsCoor2D(5, 20); |
---|
| 607 | } |
---|
| 608 | if (this->geTextMinFPS == NULL) |
---|
| 609 | { |
---|
| 610 | this->geTextMinFPS = new Text("fonts/arial_black.ttf", 15); |
---|
| 611 | this->geTextMinFPS->setName("MinFPS"); |
---|
| 612 | this->geTextMinFPS->setAlignment(TEXT_ALIGN_LEFT); |
---|
| 613 | this->geTextMinFPS->setAbsCoor2D(5, 40); |
---|
| 614 | } |
---|
[5346] | 615 | } |
---|
[6979] | 616 | else |
---|
[5346] | 617 | { |
---|
[6979] | 618 | delete this->geTextCFPS; |
---|
| 619 | this->geTextCFPS = NULL; |
---|
| 620 | delete this->geTextMaxFPS; |
---|
| 621 | this->geTextMaxFPS = NULL; |
---|
| 622 | delete this->geTextMinFPS; |
---|
| 623 | this->geTextMinFPS = NULL; |
---|
[5346] | 624 | } |
---|
| 625 | this->bDisplayFPS = display; |
---|
| 626 | #else |
---|
| 627 | this->bDisplayFPS = false; |
---|
[4536] | 628 | #endif /* NO_TEXT */ |
---|
[3617] | 629 | } |
---|
[4245] | 630 | |
---|
[4597] | 631 | |
---|
[4817] | 632 | /** |
---|
[5346] | 633 | processes the events for the GraphicsEngine class |
---|
[4836] | 634 | * @param the event to handle |
---|
[4817] | 635 | */ |
---|
| 636 | void GraphicsEngine::process(const Event &event) |
---|
| 637 | { |
---|
| 638 | switch (event.type) |
---|
| 639 | { |
---|
[7221] | 640 | case EV_VIDEO_RESIZE: |
---|
| 641 | this->resolutionChanged(event.resize); |
---|
| 642 | break; |
---|
[4817] | 643 | } |
---|
[6753] | 644 | } |
---|