| 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. |
|---|
| 10 | |
|---|
| 11 | ### File Specific: |
|---|
| 12 | main-programmer: Benjamin Grauer |
|---|
| 13 | co-programmer: ... |
|---|
| 14 | */ |
|---|
| 15 | |
|---|
| 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GRAPHICS |
|---|
| 17 | |
|---|
| 18 | #include "graphics_engine.h" |
|---|
| 19 | #include "resource_manager.h" |
|---|
| 20 | |
|---|
| 21 | #include "debug.h" |
|---|
| 22 | |
|---|
| 23 | using namespace std; |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | /** |
|---|
| 27 | \brief standard constructor |
|---|
| 28 | \todo this constructor is not jet implemented - do it |
|---|
| 29 | */ |
|---|
| 30 | GraphicsEngine::GraphicsEngine () |
|---|
| 31 | { |
|---|
| 32 | this->setClassName ("GraphicsEngine"); |
|---|
| 33 | |
|---|
| 34 | this->fullscreen = false; |
|---|
| 35 | |
|---|
| 36 | this->initVideo(); |
|---|
| 37 | |
|---|
| 38 | this->listModes(); |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | /** |
|---|
| 42 | \brief The Pointer to this GraphicsEngine |
|---|
| 43 | */ |
|---|
| 44 | GraphicsEngine* GraphicsEngine::singletonRef = NULL; |
|---|
| 45 | |
|---|
| 46 | /** |
|---|
| 47 | \returns A pointer to this GraphicsEngine |
|---|
| 48 | */ |
|---|
| 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 | { |
|---|
| 72 | // initialize SDL_VIDEO |
|---|
| 73 | if (SDL_Init(SDL_INIT_VIDEO) == -1) |
|---|
| 74 | { |
|---|
| 75 | PRINTF(1)("could not initialize SDL Video\n"); |
|---|
| 76 | // return -1; |
|---|
| 77 | } |
|---|
| 78 | // initialize SDL_GL-settings |
|---|
| 79 | this->setGLattribs(); |
|---|
| 80 | |
|---|
| 81 | // setting the Video Flags. |
|---|
| 82 | this->videoFlags = SDL_OPENGL | SDL_HWPALETTE | SDL_RESIZABLE | SDL_DOUBLEBUF; |
|---|
| 83 | |
|---|
| 84 | /* query SDL for information about our video hardware */ |
|---|
| 85 | const SDL_VideoInfo* videoInfo = SDL_GetVideoInfo (); |
|---|
| 86 | if( videoInfo == NULL) |
|---|
| 87 | { |
|---|
| 88 | PRINTF(1)("Failed getting Video Info :%s\n", SDL_GetError()); |
|---|
| 89 | SDL_Quit (); |
|---|
| 90 | } |
|---|
| 91 | if( videoInfo->hw_available) |
|---|
| 92 | this->videoFlags |= SDL_HWSURFACE; |
|---|
| 93 | else |
|---|
| 94 | this->videoFlags |= SDL_SWSURFACE; |
|---|
| 95 | /* |
|---|
| 96 | if(VideoInfo -> blit_hw) |
|---|
| 97 | VideoFlags |= SDL_HWACCEL; |
|---|
| 98 | */ |
|---|
| 99 | |
|---|
| 100 | // setting up the Resolution |
|---|
| 101 | this->setResolution(800, 600, 16); |
|---|
| 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 |
|---|
| 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; |
|---|
| 111 | // Enable default GL stuff |
|---|
| 112 | glEnable(GL_DEPTH_TEST); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | /** |
|---|
| 116 | \brief Sets the GL-attributes |
|---|
| 117 | */ |
|---|
| 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 | |
|---|
| 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 | */ |
|---|
| 143 | int GraphicsEngine::setResolution(int width, int height, int bpp) |
|---|
| 144 | { |
|---|
| 145 | Uint32 fullscreenFlag; |
|---|
| 146 | this->resolutionX = width; |
|---|
| 147 | this->resolutionY = height; |
|---|
| 148 | this->bitsPerPixel = bpp; |
|---|
| 149 | if (this->fullscreen) |
|---|
| 150 | fullscreenFlag = SDL_FULLSCREEN; |
|---|
| 151 | else |
|---|
| 152 | fullscreenFlag = 0; |
|---|
| 153 | |
|---|
| 154 | printf ("ok\n"); |
|---|
| 155 | if((this->screen = SDL_SetVideoMode(this->resolutionX, this->resolutionY, this->bitsPerPixel, this->videoFlags | fullscreenFlag)) == NULL) |
|---|
| 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 | } |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | void GraphicsEngine::setFullscreen(bool fullscreen) |
|---|
| 164 | { |
|---|
| 165 | this->fullscreen = fullscreen; |
|---|
| 166 | this->setResolution(this->resolutionX, this->resolutionY, this->bitsPerPixel); |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | /** |
|---|
| 170 | \brief Signalhandler, for when the resolution has changed |
|---|
| 171 | \param resizeInfo SDL information about the size of the new screen size |
|---|
| 172 | */ |
|---|
| 173 | int GraphicsEngine::resolutionChanged(SDL_ResizeEvent* resizeInfo) |
|---|
| 174 | { |
|---|
| 175 | this->setResolution(resizeInfo->w, resizeInfo->h, this->bitsPerPixel); |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | /** |
|---|
| 179 | \brief if Textures should be enabled |
|---|
| 180 | */ |
|---|
| 181 | bool GraphicsEngine::texturesEnabled = true; |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 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 | { |
|---|
| 192 | GraphicsEngine::storeMatrices(); |
|---|
| 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); |
|---|
| 203 | |
|---|
| 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 | } |
|---|
| 222 | |
|---|
| 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 | } |
|---|
| 236 | |
|---|
| 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 | } |
|---|
| 246 | |
|---|
| 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}; |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | |
|---|
| 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){ |
|---|
| 272 | PRINTF(2)("All resolutions available.\n"); |
|---|
| 273 | } |
|---|
| 274 | else{ |
|---|
| 275 | /* Print valid modes */ |
|---|
| 276 | PRINT(0)("Available Resoulution Modes are\n"); |
|---|
| 277 | for(int i = 0; this->videoModes[i]; ++i) |
|---|
| 278 | PRINT(4)(" | %d x %d\n", this->videoModes[i]->w, this->videoModes[i]->h); |
|---|
| 279 | } |
|---|
| 280 | |
|---|
| 281 | } |
|---|