[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" |
---|
[1853] | 20 | |
---|
[3611] | 21 | #include "debug.h" |
---|
[4245] | 22 | #include "text_engine.h" |
---|
[3611] | 23 | |
---|
[4770] | 24 | #include "ini_parser.h" |
---|
| 25 | #include "substring.h" |
---|
| 26 | |
---|
[1856] | 27 | using namespace std; |
---|
[1853] | 28 | |
---|
[1856] | 29 | |
---|
[3245] | 30 | /** |
---|
| 31 | \brief standard constructor |
---|
| 32 | \todo this constructor is not jet implemented - do it |
---|
| 33 | */ |
---|
[4597] | 34 | GraphicsEngine::GraphicsEngine () |
---|
[3365] | 35 | { |
---|
[4597] | 36 | this->setClassID(CL_GRAPHICS_ENGINE, "GraphicsEngine"); |
---|
| 37 | this->setName("GraphicsEngine"); |
---|
[4245] | 38 | this->bDisplayFPS = false; |
---|
| 39 | this->minFPS = 9999; |
---|
| 40 | this->maxFPS = 0; |
---|
[4135] | 41 | |
---|
[4768] | 42 | this->fullscreenFlag = 0; |
---|
[4135] | 43 | |
---|
[3611] | 44 | this->initVideo(); |
---|
[3610] | 45 | |
---|
[3617] | 46 | this->listModes(); |
---|
[3611] | 47 | } |
---|
[3610] | 48 | |
---|
[3621] | 49 | /** |
---|
| 50 | \brief The Pointer to this GraphicsEngine |
---|
| 51 | */ |
---|
[3611] | 52 | GraphicsEngine* GraphicsEngine::singletonRef = NULL; |
---|
[3610] | 53 | |
---|
[3621] | 54 | /** |
---|
[3611] | 55 | \brief destructs the graphicsEngine. |
---|
| 56 | */ |
---|
[4597] | 57 | GraphicsEngine::~GraphicsEngine () |
---|
[3611] | 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 | { |
---|
[3617] | 69 | // initialize SDL_VIDEO |
---|
[3610] | 70 | if (SDL_Init(SDL_INIT_VIDEO) == -1) |
---|
| 71 | { |
---|
[3617] | 72 | PRINTF(1)("could not initialize SDL Video\n"); |
---|
[3610] | 73 | // return -1; |
---|
| 74 | } |
---|
[3617] | 75 | // initialize SDL_GL-settings |
---|
| 76 | this->setGLattribs(); |
---|
[3610] | 77 | |
---|
[3617] | 78 | // setting the Video Flags. |
---|
[3619] | 79 | this->videoFlags = SDL_OPENGL | SDL_HWPALETTE | SDL_RESIZABLE | SDL_DOUBLEBUF; |
---|
[3610] | 80 | |
---|
| 81 | /* query SDL for information about our video hardware */ |
---|
| 82 | const SDL_VideoInfo* videoInfo = SDL_GetVideoInfo (); |
---|
| 83 | if( videoInfo == NULL) |
---|
| 84 | { |
---|
[4597] | 85 | PRINTF(1)("Failed getting Video Info :%s\n", SDL_GetError()); |
---|
[3610] | 86 | SDL_Quit (); |
---|
| 87 | } |
---|
| 88 | if( videoInfo->hw_available) |
---|
[3611] | 89 | this->videoFlags |= SDL_HWSURFACE; |
---|
[4597] | 90 | else |
---|
[3611] | 91 | this->videoFlags |= SDL_SWSURFACE; |
---|
[3610] | 92 | /* |
---|
[3619] | 93 | if(VideoInfo -> blit_hw) |
---|
[3610] | 94 | VideoFlags |= SDL_HWACCEL; |
---|
| 95 | */ |
---|
[4739] | 96 | // setting up the Resolution |
---|
[4768] | 97 | this->setResolution(640, 480, 16); |
---|
[3611] | 98 | |
---|
[3610] | 99 | // TO DO: Create a cool icon and use it here |
---|
[4094] | 100 | char* loadPic = new char[strlen(ResourceManager::getInstance()->getDataDir())+ 100]; |
---|
| 101 | sprintf(loadPic, "%s%s", ResourceManager::getInstance()->getDataDir(), "pictures/orxonox-icon32x32.bmp"); |
---|
[4597] | 102 | SDL_WM_SetIcon(SDL_LoadBMP(loadPic), NULL); |
---|
[4094] | 103 | delete loadPic; |
---|
[3621] | 104 | // Enable default GL stuff |
---|
| 105 | glEnable(GL_DEPTH_TEST); |
---|
[3365] | 106 | } |
---|
[1853] | 107 | |
---|
[4770] | 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 | */ |
---|
[4777] | 114 | int GraphicsEngine::loadFromIniFile(IniParser* iniParser) |
---|
[4770] | 115 | { |
---|
| 116 | // searching for a usefull resolution |
---|
[4777] | 117 | SubString resolution(iniParser->getVar(CONFIG_NAME_RESOLUTION, CONFIG_SECTION_VIDEO, "640x480"), 'x'); |
---|
[4770] | 118 | this->setResolution(atoi(resolution.getString(0)), atoi(resolution.getString(1)), 16); |
---|
[4619] | 119 | |
---|
[4770] | 120 | // looking if we are in fullscreen-mode |
---|
[4777] | 121 | const char* fullscreen = iniParser->getVar(CONFIG_NAME_FULLSCREEN, CONFIG_SECTION_VIDEO, "0"); |
---|
[4770] | 122 | if (strchr(fullscreen, '1')) |
---|
| 123 | this->setFullscreen(true); |
---|
[4777] | 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 | |
---|
[4770] | 132 | } |
---|
| 133 | |
---|
| 134 | |
---|
| 135 | |
---|
[3621] | 136 | /** |
---|
[4619] | 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 | /** |
---|
[3621] | 149 | \brief Sets the GL-attributes |
---|
| 150 | */ |
---|
[4746] | 151 | int GraphicsEngine::setGLattribs() |
---|
[3617] | 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 | |
---|
[4597] | 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); |
---|
[3617] | 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 | |
---|
[3621] | 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 | */ |
---|
[3619] | 176 | int GraphicsEngine::setResolution(int width, int height, int bpp) |
---|
[3610] | 177 | { |
---|
[3611] | 178 | this->resolutionX = width; |
---|
| 179 | this->resolutionY = height; |
---|
| 180 | this->bitsPerPixel = bpp; |
---|
[4739] | 181 | |
---|
[4769] | 182 | if((this->screen = SDL_SetVideoMode(this->resolutionX, this->resolutionY, this->bitsPerPixel, this->videoFlags | this->fullscreenFlag)) == NULL) |
---|
[3611] | 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 | } |
---|
[4135] | 188 | } |
---|
[3610] | 189 | |
---|
[4458] | 190 | /** |
---|
| 191 | \brief sets Fullscreen mode |
---|
| 192 | \param fullscreen true if fullscreen, false if windowed |
---|
| 193 | */ |
---|
[4135] | 194 | void GraphicsEngine::setFullscreen(bool fullscreen) |
---|
| 195 | { |
---|
[4768] | 196 | if (fullscreen) |
---|
| 197 | fullscreenFlag = SDL_FULLSCREEN; |
---|
| 198 | else |
---|
| 199 | fullscreenFlag = 0; |
---|
[4135] | 200 | this->setResolution(this->resolutionX, this->resolutionY, this->bitsPerPixel); |
---|
[3543] | 201 | } |
---|
[3617] | 202 | |
---|
[4458] | 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 | */ |
---|
[4374] | 210 | void GraphicsEngine::setBackgroundColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha) |
---|
| 211 | { |
---|
| 212 | glClearColor(red, green, blue, alpha); |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | |
---|
[3621] | 216 | /** |
---|
| 217 | \brief Signalhandler, for when the resolution has changed |
---|
| 218 | \param resizeInfo SDL information about the size of the new screen size |
---|
| 219 | */ |
---|
[3619] | 220 | int GraphicsEngine::resolutionChanged(SDL_ResizeEvent* resizeInfo) |
---|
| 221 | { |
---|
| 222 | this->setResolution(resizeInfo->w, resizeInfo->h, this->bitsPerPixel); |
---|
| 223 | } |
---|
[3617] | 224 | |
---|
[3622] | 225 | /** |
---|
| 226 | \brief if Textures should be enabled |
---|
| 227 | */ |
---|
| 228 | bool GraphicsEngine::texturesEnabled = true; |
---|
[3617] | 229 | |
---|
| 230 | |
---|
| 231 | |
---|
[3790] | 232 | /** |
---|
| 233 | \brief entering 2D Mode |
---|
[4597] | 234 | |
---|
[3790] | 235 | this is a GL-Projection-mode, that is orthogonal, for placing the font in fron of everything else |
---|
| 236 | */ |
---|
[4746] | 237 | void GraphicsEngine::enter2DMode() |
---|
[3790] | 238 | { |
---|
[3844] | 239 | GraphicsEngine::storeMatrices(); |
---|
[3790] | 240 | SDL_Surface *screen = SDL_GetVideoSurface(); |
---|
[4597] | 241 | |
---|
[3790] | 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); |
---|
[3617] | 250 | |
---|
[3790] | 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); |
---|
[4597] | 254 | |
---|
[3790] | 255 | glViewport(0, 0, screen->w, screen->h); |
---|
[4597] | 256 | |
---|
[3790] | 257 | glMatrixMode(GL_PROJECTION); |
---|
| 258 | glPushMatrix(); |
---|
| 259 | glLoadIdentity(); |
---|
[4597] | 260 | |
---|
[3790] | 261 | glOrtho(0.0, (GLdouble)screen->w, (GLdouble)screen->h, 0.0, 0.0, 1.0); |
---|
[4597] | 262 | |
---|
[3790] | 263 | glMatrixMode(GL_MODELVIEW); |
---|
| 264 | glPushMatrix(); |
---|
| 265 | glLoadIdentity(); |
---|
[4597] | 266 | |
---|
[3790] | 267 | glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); |
---|
| 268 | } |
---|
[3617] | 269 | |
---|
[3790] | 270 | /** |
---|
[4746] | 271 | \brief leaves the 2DMode again also \see Font::enter2DMode() |
---|
[3790] | 272 | */ |
---|
[4746] | 273 | void GraphicsEngine::leave2DMode() |
---|
[3790] | 274 | { |
---|
| 275 | glMatrixMode(GL_MODELVIEW); |
---|
| 276 | glPopMatrix(); |
---|
[4597] | 277 | |
---|
[3790] | 278 | glMatrixMode(GL_PROJECTION); |
---|
| 279 | glPopMatrix(); |
---|
[4597] | 280 | |
---|
[3790] | 281 | glPopAttrib(); |
---|
| 282 | } |
---|
[3622] | 283 | |
---|
[3844] | 284 | /** |
---|
[4597] | 285 | \brief stores the GL_matrices |
---|
[3844] | 286 | */ |
---|
[4746] | 287 | void GraphicsEngine::storeMatrices() |
---|
[3844] | 288 | { |
---|
| 289 | glGetDoublev(GL_PROJECTION_MATRIX, GraphicsEngine::projMat); |
---|
| 290 | glGetDoublev(GL_MODELVIEW_MATRIX, GraphicsEngine::modMat); |
---|
| 291 | glGetIntegerv(GL_VIEWPORT, GraphicsEngine::viewPort); |
---|
| 292 | } |
---|
[3622] | 293 | |
---|
[3844] | 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}; |
---|
[3790] | 300 | |
---|
| 301 | |
---|
[3844] | 302 | |
---|
[3617] | 303 | /** |
---|
| 304 | \brief outputs all the Fullscreen modes. |
---|
| 305 | */ |
---|
[4746] | 306 | void GraphicsEngine::listModes() |
---|
[3617] | 307 | { |
---|
| 308 | /* Get available fullscreen/hardware modes */ |
---|
| 309 | this->videoModes=SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_HWSURFACE); |
---|
[4597] | 310 | |
---|
[3617] | 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 | } |
---|
[4597] | 316 | |
---|
[3617] | 317 | /* Check if our resolution is restricted */ |
---|
| 318 | if(this->videoModes == (SDL_Rect **)-1){ |
---|
[4135] | 319 | PRINTF(2)("All resolutions available.\n"); |
---|
[3617] | 320 | } |
---|
| 321 | else{ |
---|
| 322 | /* Print valid modes */ |
---|
| 323 | PRINT(0)("Available Resoulution Modes are\n"); |
---|
| 324 | for(int i = 0; this->videoModes[i]; ++i) |
---|
[4135] | 325 | PRINT(4)(" | %d x %d\n", this->videoModes[i]->w, this->videoModes[i]->h); |
---|
[3617] | 326 | } |
---|
[4245] | 327 | } |
---|
| 328 | |
---|
[4458] | 329 | /** |
---|
| 330 | \brief ticks the Text |
---|
| 331 | \param dt the time passed |
---|
| 332 | */ |
---|
[4245] | 333 | void GraphicsEngine::tick(float dt) |
---|
| 334 | { |
---|
[4458] | 335 | if( unlikely(this->bDisplayFPS)) |
---|
| 336 | { |
---|
| 337 | this->currentFPS = 1.0/dt; |
---|
[4245] | 338 | if( unlikely(this->currentFPS > this->maxFPS)) this->maxFPS = this->currentFPS; |
---|
[4458] | 339 | if( unlikely(this->currentFPS < this->minFPS)) this->minFPS = this->currentFPS; |
---|
[4597] | 340 | |
---|
[4536] | 341 | #ifndef NO_TEXT |
---|
[4458] | 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); |
---|
[4536] | 351 | #endif /* NO_TEXT */ |
---|
[4458] | 352 | } |
---|
[4245] | 353 | } |
---|
[4597] | 354 | |
---|
[4458] | 355 | /** |
---|
| 356 | \brief displays the Frames per second |
---|
| 357 | \param display if the text should be displayed |
---|
| 358 | |
---|
| 359 | \todo this is dangerous |
---|
| 360 | */ |
---|
[4245] | 361 | void GraphicsEngine::displayFPS(bool display) |
---|
| 362 | { |
---|
[4458] | 363 | if( display) |
---|
| 364 | { |
---|
[4536] | 365 | #ifndef NO_TEXT |
---|
[4681] | 366 | this->geTextCFPS = TextEngine::getInstance()->createText("fonts/arial_black.ttf", 15, TEXT_DYNAMIC, 0, 255, 0); |
---|
[4458] | 367 | this->geTextCFPS->setAlignment(TEXT_ALIGN_LEFT); |
---|
| 368 | this->geTextCFPS->setPosition(5, 500); |
---|
[4681] | 369 | this->geTextMaxFPS = TextEngine::getInstance()->createText("fonts/arial_black.ttf", 15, TEXT_DYNAMIC, 0, 255, 0); |
---|
[4458] | 370 | this->geTextMaxFPS->setAlignment(TEXT_ALIGN_LEFT); |
---|
| 371 | this->geTextMaxFPS->setPosition(5, 530); |
---|
[4681] | 372 | this->geTextMinFPS = TextEngine::getInstance()->createText("fonts/arial_black.ttf", 35, TEXT_DYNAMIC, 0, 255, 0); |
---|
[4458] | 373 | this->geTextMinFPS->setAlignment(TEXT_ALIGN_LEFT); |
---|
[4597] | 374 | this->geTextMinFPS->setPosition(5, 560); |
---|
[4536] | 375 | #endif /* NO_TEXT */ |
---|
[4458] | 376 | } |
---|
| 377 | this->bDisplayFPS = display; |
---|
[3617] | 378 | } |
---|
[4245] | 379 | |
---|
[4597] | 380 | |
---|