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