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