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