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