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