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