home *** CD-ROM | disk | FTP | other *** search
/ Cutting-Edge 3D Game Programming with C++ / CE3DC++.ISO / BOOK / CHAP14 / FASTWIN.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-14  |  3.5 KB  |  113 lines

  1. //
  2. // File name: FastWin.CPP
  3. //
  4. // Description: The CreateDIBSection object.
  5. //
  6. // Author: John De Goes
  7. //
  8. // Project:
  9. //
  10. // Target:
  11. //
  12.  
  13. #include "FastWin.HPP"
  14.  
  15. void FastWin::Init ( DWORD Width, DWORD Height, RGBQUAD *Pal )
  16.    {
  17.    long N; Pal;
  18.    HWND ActiveWindow = GetActiveWindow ();
  19.  
  20.    HDC ScreenDC = GetDC ( ActiveWindow );
  21.  
  22.    BMInfo  = new MyBITMAPINFO;
  23.    PalInfo = new MyLOGPALETTE;
  24.    
  25.    HPALETTE PalHan;
  26.    
  27.    BMInfo->bmiHeader.biSize          = sizeof ( BITMAPINFOHEADER );
  28.    BMInfo->bmiHeader.biWidth         = Width;
  29.    BMInfo->bmiHeader.biHeight        = -abs ( Height ); // Top-down bitmap
  30.    BMInfo->bmiHeader.biPlanes        = 1;
  31.    BMInfo->bmiHeader.biBitCount      = 8;
  32.    BMInfo->bmiHeader.biCompression   = BI_RGB;
  33.    BMInfo->bmiHeader.biSizeImage     = NULL;
  34.    BMInfo->bmiHeader.biXPelsPerMeter = NULL;
  35.    BMInfo->bmiHeader.biYPelsPerMeter = NULL;
  36.    BMInfo->bmiHeader.biClrUsed       = 256;
  37.    BMInfo->bmiHeader.biClrImportant  = 256;
  38.  
  39.    if ( Pal != NULL )
  40.       {
  41.       for ( N = 0; N < 256; N++ )
  42.           {
  43.           BMInfo->bmiColors [ N ] = Pal [ N ];
  44.           }
  45.       PalInfo->palVersion = 0x300;
  46.       PalInfo->palNumEntries = 256;
  47.       for ( N = 0; N < 256; N++ )
  48.           {
  49.           PalInfo->palPalEntry [ N ].peRed   = Pal [ N ].rgbRed;
  50.           PalInfo->palPalEntry [ N ].peGreen = Pal [ N ].rgbGreen;
  51.           PalInfo->palPalEntry [ N ].peBlue  = Pal [ N ].rgbBlue;
  52.           PalInfo->palPalEntry [ N ].peFlags = PC_NOCOLLAPSE;// | PC_RESERVED;
  53.           }
  54.       PalHan = CreatePalette ( ( tagLOGPALETTE * ) PalInfo );
  55.       SelectPalette ( ScreenDC, PalHan, FALSE );
  56.       RealizePalette ( ScreenDC );
  57.       DeleteObject ( PalHan );
  58.       }
  59.  
  60.    BMHan = CreateDIBSection ( ScreenDC, ( BITMAPINFO * ) BMInfo, DIB_RGB_COLORS, 
  61.                               ( VOID * * ) &Bits, NULL, NULL );
  62.  
  63.    ReleaseDC ( ActiveWindow, ScreenDC );
  64.    }
  65.  
  66. void FastWin::Show ()
  67.    {
  68.    HWND ActiveWindow; HDC ScreenDC;
  69.    RECT Dest;
  70.    
  71.    if ( ( ActiveWindow = GetActiveWindow () ) == NULL )
  72.       return;
  73.    ScreenDC = GetDC ( ActiveWindow );
  74.    GetClientRect ( ActiveWindow, &Dest );
  75.  
  76.    HDC Context = CreateCompatibleDC ( 0 );
  77.    HBITMAP DefaultBitmap = SelectBitmap ( Context, BMHan );
  78.    
  79.    BitBlt ( ScreenDC,
  80.                    0, // x-coordinate of upper-left corner of destination rectangle 
  81.                    0, // y-coordinate of upper-left corner of destination rectangle 
  82.           Dest.right - Dest.left, // width of destination rectangle 
  83.          Dest.bottom - Dest.top, // height of destination rectangle 
  84.                Context, // handle of source device context 
  85.                    0, // x-coordinate of upper-left corner of source rectangle 
  86.                    0, // y-coordinate of upper-left corner of source rectangle
  87.               SRCCOPY // raster operation code 
  88.           );
  89.       
  90.    SelectBitmap ( Context, DefaultBitmap );
  91.    DeleteDC ( Context );
  92.    DeleteObject ( DefaultBitmap );
  93.    ReleaseDC ( ActiveWindow, ScreenDC );
  94.    }
  95.  
  96. void FastWin::Colors ()
  97.    {
  98.    // Get the active window:
  99.    HWND ActiveWindow = GetActiveWindow ();
  100.    HDC ScreenDC = GetDC ( ActiveWindow );
  101.    HPALETTE PalHan;
  102.  
  103.    // Create, select, and realize a palette:
  104.    PalHan = CreatePalette ( ( tagLOGPALETTE * ) PalInfo );
  105.    SelectPalette ( ScreenDC, PalHan, FALSE );
  106.    RealizePalette ( ScreenDC );
  107.  
  108.    // Release the screen device context:
  109.    ReleaseDC ( ActiveWindow, ScreenDC );
  110.  
  111.    // Delete the palette handle:
  112.    DeleteObject ( PalHan );
  113.    }