| 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 | |
|---|
| 21 | #include "debug.h" |
|---|
| 22 | #include "text_engine.h" |
|---|
| 23 | |
|---|
| 24 | #include "ini_parser.h" |
|---|
| 25 | #include "substring.h" |
|---|
| 26 | |
|---|
| 27 | using namespace std; |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | /** |
|---|
| 31 | \brief standard constructor |
|---|
| 32 | \todo this constructor is not jet implemented - do it |
|---|
| 33 | */ |
|---|
| 34 | GraphicsEngine::GraphicsEngine () |
|---|
| 35 | { |
|---|
| 36 | this->setClassID(CL_GRAPHICS_ENGINE, "GraphicsEngine"); |
|---|
| 37 | this->setName("GraphicsEngine"); |
|---|
| 38 | this->bDisplayFPS = false; |
|---|
| 39 | this->minFPS = 9999; |
|---|
| 40 | this->maxFPS = 0; |
|---|
| 41 | |
|---|
| 42 | this->fullscreenFlag = 0; |
|---|
| 43 | |
|---|
| 44 | this->initVideo(); |
|---|
| 45 | |
|---|
| 46 | this->listModes(); |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | /** |
|---|
| 50 | \brief The Pointer to this GraphicsEngine |
|---|
| 51 | */ |
|---|
| 52 | GraphicsEngine* GraphicsEngine::singletonRef = NULL; |
|---|
| 53 | |
|---|
| 54 | /** |
|---|
| 55 | \brief destructs the graphicsEngine. |
|---|
| 56 | */ |
|---|
| 57 | GraphicsEngine::~GraphicsEngine () |
|---|
| 58 | { |
|---|
| 59 | // delete what has to be deleted here |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | /** |
|---|
| 63 | \brief initializes the Video for openGL. |
|---|
| 64 | |
|---|
| 65 | This has to be done only once when starting orxonox. |
|---|
| 66 | */ |
|---|
| 67 | int GraphicsEngine::initVideo() |
|---|
| 68 | { |
|---|
| 69 | // initialize SDL_VIDEO |
|---|
| 70 | if (SDL_Init(SDL_INIT_VIDEO) == -1) |
|---|
| 71 | { |
|---|
| 72 | PRINTF(1)("could not initialize SDL Video\n"); |
|---|
| 73 | // return -1; |
|---|
| 74 | } |
|---|
| 75 | // initialize SDL_GL-settings |
|---|
| 76 | this->setGLattribs(); |
|---|
| 77 | |
|---|
| 78 | // setting the Video Flags. |
|---|
| 79 | this->videoFlags = SDL_OPENGL | SDL_HWPALETTE | SDL_RESIZABLE | SDL_DOUBLEBUF; |
|---|
| 80 | |
|---|
| 81 | /* query SDL for information about our video hardware */ |
|---|
| 82 | const SDL_VideoInfo* videoInfo = SDL_GetVideoInfo (); |
|---|
| 83 | if( videoInfo == NULL) |
|---|
| 84 | { |
|---|
| 85 | PRINTF(1)("Failed getting Video Info :%s\n", SDL_GetError()); |
|---|
| 86 | SDL_Quit (); |
|---|
| 87 | } |
|---|
| 88 | if( videoInfo->hw_available) |
|---|
| 89 | this->videoFlags |= SDL_HWSURFACE; |
|---|
| 90 | else |
|---|
| 91 | this->videoFlags |= SDL_SWSURFACE; |
|---|
| 92 | /* |
|---|
| 93 | if(VideoInfo -> blit_hw) |
|---|
| 94 | VideoFlags |= SDL_HWACCEL; |
|---|
| 95 | */ |
|---|
| 96 | // setting up the Resolution |
|---|
| 97 | this->setResolution(640, 480, 16); |
|---|
| 98 | |
|---|
| 99 | // TO DO: Create a cool icon and use it here |
|---|
| 100 | char* loadPic = new char[strlen(ResourceManager::getInstance()->getDataDir())+ 100]; |
|---|
| 101 | sprintf(loadPic, "%s%s", ResourceManager::getInstance()->getDataDir(), "pictures/orxonox-icon32x32.bmp"); |
|---|
| 102 | SDL_WM_SetIcon(SDL_LoadBMP(loadPic), NULL); |
|---|
| 103 | delete loadPic; |
|---|
| 104 | // Enable default GL stuff |
|---|
| 105 | glEnable(GL_DEPTH_TEST); |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | /** |
|---|
| 109 | * loads the GraphicsEngine's settings from a given ini-file and section |
|---|
| 110 | * @param iniParser the iniParser to load from |
|---|
| 111 | * @param section the Section in the ini-file to load from |
|---|
| 112 | * @returns nothing usefull |
|---|
| 113 | */ |
|---|
| 114 | int GraphicsEngine::loadFromIniFile(IniParser* iniParser) |
|---|
| 115 | { |
|---|
| 116 | // searching for a usefull resolution |
|---|
| 117 | SubString resolution(iniParser->getVar(CONFIG_NAME_RESOLUTION, CONFIG_SECTION_VIDEO, "640x480"), 'x'); |
|---|
| 118 | this->setResolution(atoi(resolution.getString(0)), atoi(resolution.getString(1)), 16); |
|---|
| 119 | |
|---|
| 120 | // looking if we are in fullscreen-mode |
|---|
| 121 | const char* fullscreen = iniParser->getVar(CONFIG_NAME_FULLSCREEN, CONFIG_SECTION_VIDEO, "0"); |
|---|
| 122 | if (strchr(fullscreen, '1')) |
|---|
| 123 | this->setFullscreen(true); |
|---|
| 124 | |
|---|
| 125 | // looking if we are in fullscreen-mode |
|---|
| 126 | const char* textures = iniParser->getVar(CONFIG_NAME_TEXTURES, CONFIG_SECTION_VIDEO_ADVANCED, "0"); |
|---|
| 127 | if (strchr(textures, '1')) |
|---|
| 128 | this->texturesEnabled = true; |
|---|
| 129 | else |
|---|
| 130 | this->texturesEnabled = false; |
|---|
| 131 | |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | /** |
|---|
| 137 | * sets the Window Captions and the Name of the icon. |
|---|
| 138 | * @param windowName The name of the Window |
|---|
| 139 | * @param icon The name of the Icon on the Disc |
|---|
| 140 | */ |
|---|
| 141 | void GraphicsEngine::setWindowName(const char* windowName, const char* icon) |
|---|
| 142 | { |
|---|
| 143 | // Set window labeling |
|---|
| 144 | SDL_WM_SetCaption (windowName, icon); |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | /** |
|---|
| 149 | \brief Sets the GL-attributes |
|---|
| 150 | */ |
|---|
| 151 | int GraphicsEngine::setGLattribs() |
|---|
| 152 | { |
|---|
| 153 | // Set video mode |
|---|
| 154 | // TO DO: parse arguments for settings |
|---|
| 155 | //SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5); |
|---|
| 156 | //SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5); |
|---|
| 157 | //SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5); |
|---|
| 158 | //SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); |
|---|
| 162 | SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16); |
|---|
| 163 | SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, 0); |
|---|
| 164 | SDL_GL_SetAttribute( SDL_GL_ACCUM_RED_SIZE, 0); |
|---|
| 165 | SDL_GL_SetAttribute( SDL_GL_ACCUM_GREEN_SIZE, 0); |
|---|
| 166 | SDL_GL_SetAttribute( SDL_GL_ACCUM_BLUE_SIZE, 0); |
|---|
| 167 | SDL_GL_SetAttribute( SDL_GL_ACCUM_ALPHA_SIZE, 0); |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | /** |
|---|
| 171 | \brief sets the Resolution of the Screen to display the Graphics to. |
|---|
| 172 | \param width The width of the window |
|---|
| 173 | \param height The height of the window |
|---|
| 174 | \param bpp bits per pixel |
|---|
| 175 | */ |
|---|
| 176 | int GraphicsEngine::setResolution(int width, int height, int bpp) |
|---|
| 177 | { |
|---|
| 178 | this->resolutionX = width; |
|---|
| 179 | this->resolutionY = height; |
|---|
| 180 | this->bitsPerPixel = bpp; |
|---|
| 181 | |
|---|
| 182 | if((this->screen = SDL_SetVideoMode(this->resolutionX, this->resolutionY, this->bitsPerPixel, this->videoFlags | this->fullscreenFlag)) == NULL) |
|---|
| 183 | { |
|---|
| 184 | PRINTF(1)("Could not SDL_SetVideoMode(%d, %d, %d, %d): %s\n", this->resolutionX, this->resolutionY, this->bitsPerPixel, this->videoFlags, SDL_GetError()); |
|---|
| 185 | SDL_Quit(); |
|---|
| 186 | // return -1; |
|---|
| 187 | } |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | /** |
|---|
| 191 | \brief sets Fullscreen mode |
|---|
| 192 | \param fullscreen true if fullscreen, false if windowed |
|---|
| 193 | */ |
|---|
| 194 | void GraphicsEngine::setFullscreen(bool fullscreen) |
|---|
| 195 | { |
|---|
| 196 | if (fullscreen) |
|---|
| 197 | fullscreenFlag = SDL_FULLSCREEN; |
|---|
| 198 | else |
|---|
| 199 | fullscreenFlag = 0; |
|---|
| 200 | this->setResolution(this->resolutionX, this->resolutionY, this->bitsPerPixel); |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | /** |
|---|
| 204 | \brief sets the background color |
|---|
| 205 | \param red the red part of the background |
|---|
| 206 | \param blue the blue part of the background |
|---|
| 207 | \param green the green part of the background |
|---|
| 208 | \param alpha the alpha part of the background |
|---|
| 209 | */ |
|---|
| 210 | void GraphicsEngine::setBackgroundColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha) |
|---|
| 211 | { |
|---|
| 212 | glClearColor(red, green, blue, alpha); |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | /** |
|---|
| 217 | \brief Signalhandler, for when the resolution has changed |
|---|
| 218 | \param resizeInfo SDL information about the size of the new screen size |
|---|
| 219 | */ |
|---|
| 220 | int GraphicsEngine::resolutionChanged(SDL_ResizeEvent* resizeInfo) |
|---|
| 221 | { |
|---|
| 222 | this->setResolution(resizeInfo->w, resizeInfo->h, this->bitsPerPixel); |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | /** |
|---|
| 226 | \brief if Textures should be enabled |
|---|
| 227 | */ |
|---|
| 228 | bool GraphicsEngine::texturesEnabled = true; |
|---|
| 229 | |
|---|
| 230 | |
|---|
| 231 | |
|---|
| 232 | /** |
|---|
| 233 | \brief entering 2D Mode |
|---|
| 234 | |
|---|
| 235 | this is a GL-Projection-mode, that is orthogonal, for placing the font in fron of everything else |
|---|
| 236 | */ |
|---|
| 237 | void GraphicsEngine::enter2DMode() |
|---|
| 238 | { |
|---|
| 239 | GraphicsEngine::storeMatrices(); |
|---|
| 240 | SDL_Surface *screen = SDL_GetVideoSurface(); |
|---|
| 241 | |
|---|
| 242 | /* Note, there may be other things you need to change, |
|---|
| 243 | depending on how you have your OpenGL state set up. |
|---|
| 244 | */ |
|---|
| 245 | glPushAttrib(GL_ENABLE_BIT); |
|---|
| 246 | glDisable(GL_DEPTH_TEST); |
|---|
| 247 | glDisable(GL_CULL_FACE); |
|---|
| 248 | glDisable(GL_LIGHTING); // will be set back when leaving 2D-mode |
|---|
| 249 | glEnable(GL_TEXTURE_2D); |
|---|
| 250 | |
|---|
| 251 | /* This allows alpha blending of 2D textures with the scene */ |
|---|
| 252 | glEnable(GL_BLEND); |
|---|
| 253 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
|---|
| 254 | |
|---|
| 255 | glViewport(0, 0, screen->w, screen->h); |
|---|
| 256 | |
|---|
| 257 | glMatrixMode(GL_PROJECTION); |
|---|
| 258 | glPushMatrix(); |
|---|
| 259 | glLoadIdentity(); |
|---|
| 260 | |
|---|
| 261 | glOrtho(0.0, (GLdouble)screen->w, (GLdouble)screen->h, 0.0, 0.0, 1.0); |
|---|
| 262 | |
|---|
| 263 | glMatrixMode(GL_MODELVIEW); |
|---|
| 264 | glPushMatrix(); |
|---|
| 265 | glLoadIdentity(); |
|---|
| 266 | |
|---|
| 267 | glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); |
|---|
| 268 | } |
|---|
| 269 | |
|---|
| 270 | /** |
|---|
| 271 | \brief leaves the 2DMode again also \see Font::enter2DMode() |
|---|
| 272 | */ |
|---|
| 273 | void GraphicsEngine::leave2DMode() |
|---|
| 274 | { |
|---|
| 275 | glMatrixMode(GL_MODELVIEW); |
|---|
| 276 | glPopMatrix(); |
|---|
| 277 | |
|---|
| 278 | glMatrixMode(GL_PROJECTION); |
|---|
| 279 | glPopMatrix(); |
|---|
| 280 | |
|---|
| 281 | glPopAttrib(); |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | /** |
|---|
| 285 | \brief stores the GL_matrices |
|---|
| 286 | */ |
|---|
| 287 | void GraphicsEngine::storeMatrices() |
|---|
| 288 | { |
|---|
| 289 | glGetDoublev(GL_PROJECTION_MATRIX, GraphicsEngine::projMat); |
|---|
| 290 | glGetDoublev(GL_MODELVIEW_MATRIX, GraphicsEngine::modMat); |
|---|
| 291 | glGetIntegerv(GL_VIEWPORT, GraphicsEngine::viewPort); |
|---|
| 292 | } |
|---|
| 293 | |
|---|
| 294 | //! the stored ModelView Matrix. |
|---|
| 295 | GLdouble GraphicsEngine::modMat[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; |
|---|
| 296 | //! the stored Projection Matrix |
|---|
| 297 | GLdouble GraphicsEngine::projMat[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; |
|---|
| 298 | //! The ViewPort |
|---|
| 299 | GLint GraphicsEngine::viewPort[4] = {0,0,0,0}; |
|---|
| 300 | |
|---|
| 301 | |
|---|
| 302 | |
|---|
| 303 | /** |
|---|
| 304 | \brief outputs all the Fullscreen modes. |
|---|
| 305 | */ |
|---|
| 306 | void GraphicsEngine::listModes() |
|---|
| 307 | { |
|---|
| 308 | /* Get available fullscreen/hardware modes */ |
|---|
| 309 | this->videoModes=SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_HWSURFACE); |
|---|
| 310 | |
|---|
| 311 | /* Check is there are any modes available */ |
|---|
| 312 | if(this->videoModes == (SDL_Rect **)0){ |
|---|
| 313 | PRINTF(1)("No modes available!\n"); |
|---|
| 314 | exit(-1); |
|---|
| 315 | } |
|---|
| 316 | |
|---|
| 317 | /* Check if our resolution is restricted */ |
|---|
| 318 | if(this->videoModes == (SDL_Rect **)-1){ |
|---|
| 319 | PRINTF(2)("All resolutions available.\n"); |
|---|
| 320 | } |
|---|
| 321 | else{ |
|---|
| 322 | /* Print valid modes */ |
|---|
| 323 | PRINT(0)("Available Resoulution Modes are\n"); |
|---|
| 324 | for(int i = 0; this->videoModes[i]; ++i) |
|---|
| 325 | PRINT(4)(" | %d x %d\n", this->videoModes[i]->w, this->videoModes[i]->h); |
|---|
| 326 | } |
|---|
| 327 | } |
|---|
| 328 | |
|---|
| 329 | /** |
|---|
| 330 | \brief ticks the Text |
|---|
| 331 | \param dt the time passed |
|---|
| 332 | */ |
|---|
| 333 | void GraphicsEngine::tick(float dt) |
|---|
| 334 | { |
|---|
| 335 | if( unlikely(this->bDisplayFPS)) |
|---|
| 336 | { |
|---|
| 337 | this->currentFPS = 1.0/dt; |
|---|
| 338 | if( unlikely(this->currentFPS > this->maxFPS)) this->maxFPS = this->currentFPS; |
|---|
| 339 | if( unlikely(this->currentFPS < this->minFPS)) this->minFPS = this->currentFPS; |
|---|
| 340 | |
|---|
| 341 | #ifndef NO_TEXT |
|---|
| 342 | char tmpChar1[20]; |
|---|
| 343 | sprintf(tmpChar1, "Current: %4.0f", this->currentFPS); |
|---|
| 344 | this->geTextCFPS->setText(tmpChar1); |
|---|
| 345 | char tmpChar2[20]; |
|---|
| 346 | sprintf(tmpChar2, "Max: %4.0f", this->maxFPS); |
|---|
| 347 | this->geTextMaxFPS->setText(tmpChar2); |
|---|
| 348 | char tmpChar3[20]; |
|---|
| 349 | sprintf(tmpChar3, "Min: %4.0f", this->minFPS); |
|---|
| 350 | this->geTextMinFPS->setText(tmpChar3); |
|---|
| 351 | #endif /* NO_TEXT */ |
|---|
| 352 | } |
|---|
| 353 | } |
|---|
| 354 | |
|---|
| 355 | /** |
|---|
| 356 | \brief displays the Frames per second |
|---|
| 357 | \param display if the text should be displayed |
|---|
| 358 | |
|---|
| 359 | \todo this is dangerous |
|---|
| 360 | */ |
|---|
| 361 | void GraphicsEngine::displayFPS(bool display) |
|---|
| 362 | { |
|---|
| 363 | if( display) |
|---|
| 364 | { |
|---|
| 365 | #ifndef NO_TEXT |
|---|
| 366 | this->geTextCFPS = TextEngine::getInstance()->createText("fonts/arial_black.ttf", 15, TEXT_DYNAMIC, 0, 255, 0); |
|---|
| 367 | this->geTextCFPS->setAlignment(TEXT_ALIGN_LEFT); |
|---|
| 368 | this->geTextCFPS->setPosition(5, 500); |
|---|
| 369 | this->geTextMaxFPS = TextEngine::getInstance()->createText("fonts/arial_black.ttf", 15, TEXT_DYNAMIC, 0, 255, 0); |
|---|
| 370 | this->geTextMaxFPS->setAlignment(TEXT_ALIGN_LEFT); |
|---|
| 371 | this->geTextMaxFPS->setPosition(5, 530); |
|---|
| 372 | this->geTextMinFPS = TextEngine::getInstance()->createText("fonts/arial_black.ttf", 35, TEXT_DYNAMIC, 0, 255, 0); |
|---|
| 373 | this->geTextMinFPS->setAlignment(TEXT_ALIGN_LEFT); |
|---|
| 374 | this->geTextMinFPS->setPosition(5, 560); |
|---|
| 375 | #endif /* NO_TEXT */ |
|---|
| 376 | } |
|---|
| 377 | this->bDisplayFPS = display; |
|---|
| 378 | } |
|---|
| 379 | |
|---|
| 380 | |
|---|