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