| 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 | this->initVideo(); |
|---|
| 34 | |
|---|
| 35 | this->listModes(); |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | /** |
|---|
| 39 | \brief The Pointer to this GraphicsEngine |
|---|
| 40 | */ |
|---|
| 41 | GraphicsEngine* GraphicsEngine::singletonRef = NULL; |
|---|
| 42 | |
|---|
| 43 | /** |
|---|
| 44 | \returns A pointer to this GraphicsEngine |
|---|
| 45 | */ |
|---|
| 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 | { |
|---|
| 69 | // initialize SDL_VIDEO |
|---|
| 70 | if (SDL_Init(SDL_INIT_VIDEO) == -1) |
|---|
| 71 | { |
|---|
| 72 | PRINTF(1)("could not initialize SDL Video\n"); |
|---|
| 73 | // return -1; |
|---|
| 74 | } |
|---|
| 75 | // initialize SDL_GL-settings |
|---|
| 76 | this->setGLattribs(); |
|---|
| 77 | |
|---|
| 78 | // setting the Video Flags. |
|---|
| 79 | this->videoFlags = SDL_OPENGL | SDL_HWPALETTE | SDL_RESIZABLE | SDL_DOUBLEBUF; |
|---|
| 80 | |
|---|
| 81 | /* query SDL for information about our video hardware */ |
|---|
| 82 | const SDL_VideoInfo* videoInfo = SDL_GetVideoInfo (); |
|---|
| 83 | if( videoInfo == NULL) |
|---|
| 84 | { |
|---|
| 85 | PRINTF(1)("Failed getting Video Info :%s\n", SDL_GetError()); |
|---|
| 86 | SDL_Quit (); |
|---|
| 87 | } |
|---|
| 88 | if( videoInfo->hw_available) |
|---|
| 89 | this->videoFlags |= SDL_HWSURFACE; |
|---|
| 90 | else |
|---|
| 91 | this->videoFlags |= SDL_SWSURFACE; |
|---|
| 92 | /* |
|---|
| 93 | if(VideoInfo -> blit_hw) |
|---|
| 94 | VideoFlags |= SDL_HWACCEL; |
|---|
| 95 | */ |
|---|
| 96 | |
|---|
| 97 | // setting up the Resolution |
|---|
| 98 | this->setResolution(800, 600, 16); |
|---|
| 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 |
|---|
| 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; |
|---|
| 108 | // Enable default GL stuff |
|---|
| 109 | glEnable(GL_DEPTH_TEST); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | /** |
|---|
| 113 | \brief Sets the GL-attributes |
|---|
| 114 | */ |
|---|
| 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 | |
|---|
| 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 | */ |
|---|
| 140 | int GraphicsEngine::setResolution(int width, int height, int bpp) |
|---|
| 141 | { |
|---|
| 142 | this->resolutionX = width; |
|---|
| 143 | this->resolutionY = height; |
|---|
| 144 | this->bitsPerPixel = bpp; |
|---|
| 145 | |
|---|
| 146 | printf ("ok\n"); |
|---|
| 147 | if((this->screen = SDL_SetVideoMode(this->resolutionX, this->resolutionY, this->bitsPerPixel, this->videoFlags)) == NULL) |
|---|
| 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 | } |
|---|
| 153 | |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | /** |
|---|
| 157 | \brief Signalhandler, for when the resolution has changed |
|---|
| 158 | \param resizeInfo SDL information about the size of the new screen size |
|---|
| 159 | */ |
|---|
| 160 | int GraphicsEngine::resolutionChanged(SDL_ResizeEvent* resizeInfo) |
|---|
| 161 | { |
|---|
| 162 | this->setResolution(resizeInfo->w, resizeInfo->h, this->bitsPerPixel); |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | /** |
|---|
| 166 | \brief if Textures should be enabled |
|---|
| 167 | */ |
|---|
| 168 | bool GraphicsEngine::texturesEnabled = true; |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 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 | { |
|---|
| 179 | GraphicsEngine::storeMatrices(); |
|---|
| 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); |
|---|
| 190 | |
|---|
| 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 | } |
|---|
| 209 | |
|---|
| 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 | } |
|---|
| 223 | |
|---|
| 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 | } |
|---|
| 233 | |
|---|
| 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}; |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 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 | } |
|---|