[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" |
---|
[1853] | 19 | |
---|
[3611] | 20 | #include "debug.h" |
---|
| 21 | |
---|
[1856] | 22 | using namespace std; |
---|
[1853] | 23 | |
---|
[1856] | 24 | |
---|
[3245] | 25 | /** |
---|
| 26 | \brief standard constructor |
---|
| 27 | \todo this constructor is not jet implemented - do it |
---|
| 28 | */ |
---|
[3610] | 29 | GraphicsEngine::GraphicsEngine () |
---|
[3365] | 30 | { |
---|
[3611] | 31 | this->setClassName ("GraphicsEngine"); |
---|
| 32 | this->initVideo(); |
---|
[3610] | 33 | |
---|
[3617] | 34 | this->listModes(); |
---|
[3611] | 35 | } |
---|
[3610] | 36 | |
---|
[3621] | 37 | /** |
---|
| 38 | \brief The Pointer to this GraphicsEngine |
---|
| 39 | */ |
---|
[3611] | 40 | GraphicsEngine* GraphicsEngine::singletonRef = NULL; |
---|
[3610] | 41 | |
---|
[3621] | 42 | /** |
---|
| 43 | \returns A pointer to this GraphicsEngine |
---|
| 44 | */ |
---|
[3611] | 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 | { |
---|
[3617] | 68 | // initialize SDL_VIDEO |
---|
[3610] | 69 | if (SDL_Init(SDL_INIT_VIDEO) == -1) |
---|
| 70 | { |
---|
[3617] | 71 | PRINTF(1)("could not initialize SDL Video\n"); |
---|
[3610] | 72 | // return -1; |
---|
| 73 | } |
---|
[3617] | 74 | // initialize SDL_GL-settings |
---|
| 75 | this->setGLattribs(); |
---|
[3610] | 76 | |
---|
[3617] | 77 | // setting the Video Flags. |
---|
[3619] | 78 | this->videoFlags = SDL_OPENGL | SDL_HWPALETTE | SDL_RESIZABLE | SDL_DOUBLEBUF; |
---|
[3610] | 79 | |
---|
| 80 | /* query SDL for information about our video hardware */ |
---|
| 81 | const SDL_VideoInfo* videoInfo = SDL_GetVideoInfo (); |
---|
| 82 | if( videoInfo == NULL) |
---|
| 83 | { |
---|
[3617] | 84 | PRINTF(1)("Failed getting Video Info :%s\n", SDL_GetError()); |
---|
[3610] | 85 | SDL_Quit (); |
---|
| 86 | } |
---|
| 87 | if( videoInfo->hw_available) |
---|
[3611] | 88 | this->videoFlags |= SDL_HWSURFACE; |
---|
[3610] | 89 | else |
---|
[3611] | 90 | this->videoFlags |= SDL_SWSURFACE; |
---|
[3610] | 91 | /* |
---|
[3619] | 92 | if(VideoInfo -> blit_hw) |
---|
[3610] | 93 | VideoFlags |= SDL_HWACCEL; |
---|
| 94 | */ |
---|
[3611] | 95 | |
---|
[3617] | 96 | // setting up the Resolution |
---|
[3619] | 97 | this->setResolution(800, 600, 16); |
---|
[3610] | 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 |
---|
[3621] | 103 | SDL_WM_SetIcon(SDL_LoadBMP("../data/pictures/orxonox-icon32x32.bmp"), NULL); |
---|
[3610] | 104 | |
---|
[3621] | 105 | // Enable default GL stuff |
---|
| 106 | glEnable(GL_DEPTH_TEST); |
---|
[3365] | 107 | } |
---|
[1853] | 108 | |
---|
[3621] | 109 | /** |
---|
| 110 | \brief Sets the GL-attributes |
---|
| 111 | */ |
---|
[3617] | 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 | |
---|
[3621] | 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 | */ |
---|
[3619] | 137 | int GraphicsEngine::setResolution(int width, int height, int bpp) |
---|
[3610] | 138 | { |
---|
[3611] | 139 | this->resolutionX = width; |
---|
| 140 | this->resolutionY = height; |
---|
| 141 | this->bitsPerPixel = bpp; |
---|
| 142 | |
---|
[3619] | 143 | printf ("ok\n"); |
---|
[3621] | 144 | if((this->screen = SDL_SetVideoMode(this->resolutionX, this->resolutionY, this->bitsPerPixel, this->videoFlags)) == NULL) |
---|
[3611] | 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 | } |
---|
[3610] | 150 | |
---|
[3543] | 151 | } |
---|
[3617] | 152 | |
---|
[3621] | 153 | /** |
---|
| 154 | \brief Signalhandler, for when the resolution has changed |
---|
| 155 | \param resizeInfo SDL information about the size of the new screen size |
---|
| 156 | */ |
---|
[3619] | 157 | int GraphicsEngine::resolutionChanged(SDL_ResizeEvent* resizeInfo) |
---|
| 158 | { |
---|
| 159 | this->setResolution(resizeInfo->w, resizeInfo->h, this->bitsPerPixel); |
---|
| 160 | } |
---|
[3617] | 161 | |
---|
[3622] | 162 | /** |
---|
| 163 | \brief if Textures should be enabled |
---|
| 164 | */ |
---|
| 165 | bool GraphicsEngine::texturesEnabled = true; |
---|
[3617] | 166 | |
---|
| 167 | |
---|
| 168 | |
---|
[3790] | 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 | SDL_Surface *screen = SDL_GetVideoSurface(); |
---|
| 177 | |
---|
| 178 | /* Note, there may be other things you need to change, |
---|
| 179 | depending on how you have your OpenGL state set up. |
---|
| 180 | */ |
---|
| 181 | glPushAttrib(GL_ENABLE_BIT); |
---|
| 182 | glDisable(GL_DEPTH_TEST); |
---|
| 183 | glDisable(GL_CULL_FACE); |
---|
| 184 | glDisable(GL_LIGHTING); // will be set back when leaving 2D-mode |
---|
| 185 | glEnable(GL_TEXTURE_2D); |
---|
[3617] | 186 | |
---|
[3790] | 187 | /* This allows alpha blending of 2D textures with the scene */ |
---|
| 188 | glEnable(GL_BLEND); |
---|
| 189 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
---|
| 190 | |
---|
| 191 | glViewport(0, 0, screen->w, screen->h); |
---|
| 192 | |
---|
| 193 | glMatrixMode(GL_PROJECTION); |
---|
| 194 | glPushMatrix(); |
---|
| 195 | glLoadIdentity(); |
---|
| 196 | |
---|
| 197 | glOrtho(0.0, (GLdouble)screen->w, (GLdouble)screen->h, 0.0, 0.0, 1.0); |
---|
| 198 | |
---|
| 199 | glMatrixMode(GL_MODELVIEW); |
---|
| 200 | glPushMatrix(); |
---|
| 201 | glLoadIdentity(); |
---|
| 202 | |
---|
| 203 | glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); |
---|
| 204 | } |
---|
[3617] | 205 | |
---|
[3790] | 206 | /** |
---|
| 207 | \brief leaves the 2DMode again also \see Font::enter2DMode(void) |
---|
| 208 | */ |
---|
| 209 | void GraphicsEngine::leave2DMode(void) |
---|
| 210 | { |
---|
| 211 | glMatrixMode(GL_MODELVIEW); |
---|
| 212 | glPopMatrix(); |
---|
| 213 | |
---|
| 214 | glMatrixMode(GL_PROJECTION); |
---|
| 215 | glPopMatrix(); |
---|
| 216 | |
---|
| 217 | glPopAttrib(); |
---|
| 218 | } |
---|
[3622] | 219 | |
---|
| 220 | |
---|
[3790] | 221 | |
---|
| 222 | |
---|
[3617] | 223 | /** |
---|
| 224 | \brief outputs all the Fullscreen modes. |
---|
| 225 | */ |
---|
| 226 | void GraphicsEngine::listModes(void) |
---|
| 227 | { |
---|
| 228 | /* Get available fullscreen/hardware modes */ |
---|
| 229 | this->videoModes=SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_HWSURFACE); |
---|
| 230 | |
---|
| 231 | /* Check is there are any modes available */ |
---|
| 232 | if(this->videoModes == (SDL_Rect **)0){ |
---|
| 233 | PRINTF(1)("No modes available!\n"); |
---|
| 234 | exit(-1); |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | /* Check if our resolution is restricted */ |
---|
| 238 | if(this->videoModes == (SDL_Rect **)-1){ |
---|
| 239 | PRINTF(1)("All resolutions available.\n"); |
---|
| 240 | } |
---|
| 241 | else{ |
---|
| 242 | /* Print valid modes */ |
---|
| 243 | PRINT(0)("Available Resoulution Modes are\n"); |
---|
| 244 | for(int i = 0; this->videoModes[i]; ++i) |
---|
| 245 | PRINT(0)(" | %d x %d\n", this->videoModes[i]->w, this->videoModes[i]->h); |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | } |
---|