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