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