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