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