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