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