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