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