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