home *** CD-ROM | disk | FTP | other *** search
/ Enter 2005 March / ENTER.ISO / files / fwp-0.0.6-win32-installer.exe / Display.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-12-06  |  4.4 KB  |  174 lines

  1.  
  2. #include "Display.h"
  3.  
  4. #include "log.h"
  5.  
  6. unsigned int Display::videoFlags = 0;
  7. SDL_Surface* Display::mainWindow = NULL;
  8.  
  9. bool Display::initialized = false;
  10.  
  11. bool Display::init(){
  12.     if(initialized){
  13.         error("(in Display::init()): Display is already initialized.\n\n");
  14.         return false;
  15.     }
  16.  
  17.     log("\n");
  18.     log("****************************\n");
  19.     log("*** Initializing Display ***\n");
  20.     log("****************************\n");
  21.     log("\n");
  22.  
  23.     // update cvars
  24.     info.cvar.display_width->updateVar();
  25.     info.cvar.display_height->updateVar();
  26.     info.cvar.display_bpp->updateVar();
  27.     info.cvar.display_depthBufferSize->updateVar();
  28.     info.cvar.display_gamma->updateVar();
  29.     info.cvar.display_fullscreen->updateVar();
  30.  
  31.  
  32.     if(SDL_WasInit(SDL_INIT_VIDEO)==0){    // not initialized
  33.         error("(in Display::init()): SDL video system not initialized.\n\n");
  34.         return false;
  35.     }
  36.  
  37.     const SDL_VideoInfo* VideoInfo = SDL_GetVideoInfo();
  38.  
  39.     if(VideoInfo == NULL){
  40.         error("(in Display::init()): Failed getting Video Info: %s.\n\n",SDL_GetError());
  41.         return false;
  42.     }
  43.  
  44.     Display::videoFlags = 0;
  45.     Display::videoFlags |= SDL_OPENGL;
  46.     if(info.var.fullscreen)
  47.         Display::videoFlags |= SDL_FULLSCREEN;
  48.  
  49.  
  50.     SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
  51.     SDL_GL_SetAttribute( SDL_GL_BUFFER_SIZE, info.var.bpp);
  52. //    SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8);
  53. //    SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8);
  54. //    SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8);
  55. //    SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8);
  56.     SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, info.var.depthBufferSize);
  57.     SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, 0);
  58.     SDL_GL_SetAttribute( SDL_GL_ACCUM_RED_SIZE, 0);
  59.     SDL_GL_SetAttribute( SDL_GL_ACCUM_GREEN_SIZE, 0);
  60.     SDL_GL_SetAttribute( SDL_GL_ACCUM_BLUE_SIZE, 0);
  61.     SDL_GL_SetAttribute( SDL_GL_ACCUM_ALPHA_SIZE, 0);
  62.  
  63.     log("Probing for video modes...\n");
  64.     Display::listAvailableModes(Display::videoFlags);
  65.     log("\n");
  66.  
  67.     log("Setting video mode...\n");
  68.     Display::mainWindow = SDL_SetVideoMode(info.var.width, info.var.height, info.var.bpp, Display::videoFlags);
  69.  
  70.     if( Display::mainWindow == NULL ){
  71.         error("(in Display::init()): Failed to set video mode: %s.\n\n", SDL_GetError());
  72.         return false;
  73.     }
  74.  
  75.     SDL_WM_SetCaption("FWP", "FWP");
  76.     log("VideoMode: width: %i, height: %i, bpp: %i.\n"
  77.         , Display::mainWindow->w, Display::mainWindow->h, Display::mainWindow->format->BitsPerPixel);
  78.     log("Flags: 0x%X.\n", mainWindow->flags);
  79.  
  80.     info.var.width = Display::mainWindow->w;
  81.     info.var.height = Display::mainWindow->h;
  82.     info.var.bpp = Display::mainWindow->format->BitsPerPixel;
  83.     info.var.scaleX = info.var.width/(float)DISPLAY_VSCREEN_WIDTH;
  84.     info.var.scaleY = info.var.height/(float)DISPLAY_VSCREEN_HEIGHT;
  85.  
  86.  
  87.     Display::setGamma(info.var.gamma);
  88.     
  89.     initialized=true;
  90.  
  91.     return true;
  92. }
  93.  
  94. bool Display::shutdown(){
  95.     if(!initialized){
  96.         error("(in Display::shutdown()): Display is not initialized.\n\n");
  97.         return false;
  98.     }
  99.  
  100.     log("\n");
  101.     log("=============================\n");
  102.     log("=== Shutting down Display ===\n");
  103.     log("=============================\n");
  104.     log("\n");
  105.  
  106.     Display::setGamma(1.0f);
  107.  
  108.     initialized=false;
  109.  
  110.     return true;
  111. }
  112.  
  113. bool Display::wasInit(){
  114.     return initialized;
  115. }
  116.  
  117. bool Display::registerCVarsAndCCmds(){
  118.     return info.registerCVarsAndCCmds();
  119. }
  120.  
  121. bool Display::unregisterCVarsAndCCmds(){
  122.     return info.unregisterCVarsAndCCmds();
  123. }
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132. // FIXME: funzt nett!
  133. void Display::takeScreenshot(){
  134.     static int numScreenshots=0;
  135.  
  136.     char filename[128];
  137.     sprintf(filename , "screenshot%i.bmp", numScreenshots);
  138.     numScreenshots++;
  139.  
  140.     if(SDL_SaveBMP(mainWindow, filename) == -1){
  141.         error("(in display::takeScreenshot()): Failed to save screenshot: %s.\n\n", SDL_GetError());
  142.     }else{
  143.         log("Wrote screenshot '%s'.\n", filename);
  144.     }
  145. }
  146.  
  147.  
  148. void Display::setGamma(float gamma){
  149.     if(SDL_SetGamma(gamma, gamma, gamma)==-1){
  150.         error("(in display::setGamma()): Failed to set gamma values: %s.\n\n", SDL_GetError());
  151.     }
  152. }
  153.  
  154. void Display::listAvailableModes(unsigned int flags){
  155.     SDL_Rect **modes;
  156.     int i;
  157.  
  158.     modes=SDL_ListModes(NULL, flags);
  159.  
  160.     if(modes == (SDL_Rect **)0){
  161.         log("No modes available!\n");
  162.         return;
  163.     }
  164.  
  165.     if(modes == (SDL_Rect **)-1){
  166.         log("All resolutions available.\n");
  167.     }else{
  168.         log("Available modes:\n");
  169.         for(i=0;modes[i];++i){
  170.             log("  %d x %d\n", modes[i]->w, modes[i]->h);
  171.         }
  172.     }
  173. }
  174.