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