home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / 3DTOSHI2.ZIP / mpgfx / source / gfxddraw.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-16  |  10.2 KB  |  373 lines

  1.  
  2. // gfxddraw.cpp
  3. //
  4. // Copyright (c) 1995 by Toshiaki Tsuji, all rights reserved.
  5.  
  6. #include "stdgfx.h"
  7. #include "gfxddraw.h"
  8. #include "lgfxdib.h"
  9.  
  10. DDRAWDRIVER::DDRAWDRIVER ()
  11.   {
  12.     #if defined (__FORGDK__)
  13.       lpDirectDraw = NULL;
  14.       lpPrimarySurface = NULL;
  15.       lpSecondarySurface = NULL;
  16.             lpDirectDrawPalette = NULL;
  17.       hMainWindow = NULL;
  18.     #endif  
  19.   } // End of Constructor for DDRAWDRIVER
  20.  
  21. DDRAWDRIVER::~DDRAWDRIVER ()
  22.   {
  23.     Reset ();  
  24.   } // End of Destructor for DDRAWDRIVER
  25.  
  26. BOOLEAN DDRAWDRIVER::SetUp ( DISPLAYDATA *Data )
  27.   {
  28.      if (Data)
  29.        {} 
  30.      #if defined (__FORGDK__)
  31.        HRESULT Result;
  32.        
  33.        Result = DirectDrawCreate ( NULL, &lpDirectDraw, NULL );
  34.        if (Result!=DD_OK)
  35.          return FAILURE;
  36.        Result = lpDirectDraw->SetCooperativeLevel ( Data->hMainWindow,
  37.                                                     DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN );
  38.        if (Result!=DD_OK)
  39.          {
  40.            Reset ();  
  41.            return FAILURE;                                              
  42.          } // End if
  43.  
  44.        Result = lpDirectDraw->SetDisplayMode ( ModeTable[Data->Mode].Width,
  45.                                                ModeTable[Data->Mode].Height,   
  46.                                                ModeTable[Data->Mode].BitSize );
  47.        if (Result!=DD_OK)
  48.          {
  49.            Reset ();  
  50.            return FAILURE;                                              
  51.          } // End if
  52.  
  53.        Width = ModeTable[Data->Mode].Width;
  54.        Height = ModeTable[Data->Mode].Height;
  55.        BitSize = ModeTable[Data->Mode].BitSize;
  56.        BytesPerRow = Width*8 / BitSize;
  57.        
  58.        DDSURFACEDESC ddsdDesc;
  59.        ddsdDesc.dwSize = sizeof(DDSURFACEDESC);
  60.        ddsdDesc.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
  61.        ddsdDesc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX;
  62.        ddsdDesc.dwBackBufferCount = 1;
  63.        Result = lpDirectDraw->CreateSurface ( &ddsdDesc, &lpPrimarySurface, NULL );
  64.        if (Result!=DD_OK)
  65.          {
  66.            Reset ();  
  67.            return FAILURE;                                              
  68.          } // End if
  69.  
  70.        DDSCAPS ddscCaps;
  71.        ddscCaps.dwCaps = DDSCAPS_BACKBUFFER;
  72.        Result = lpPrimarySurface->GetAttachedSurface ( &ddscCaps, &lpSecondarySurface );                
  73.        if (Result!=DD_OK)
  74.          {
  75.            Reset ();  
  76.            return FAILURE;                                              
  77.          } // End if
  78.  
  79.               PALETTEENTRY pePalette[256]; 
  80.              memset ( pePalette, 0, sizeof(PALETTEENTRY)*256 );
  81.              Result = lpDirectDraw->CreatePalette ( DDPCAPS_8BIT, pePalette, &lpDirectDrawPalette, NULL );
  82.        if (Result!=DD_OK)
  83.          {
  84.            Reset ();  
  85.            return FAILURE;                                              
  86.          } // End if
  87.  
  88.        if (lpDirectDrawPalette==NULL)
  89.          {
  90.            Reset ();  
  91.            return FAILURE;                                              
  92.          } // End if
  93.  
  94.              Result = lpPrimarySurface->SetPalette ( lpDirectDrawPalette );
  95.        if (Result!=DD_OK)
  96.          {
  97.            Reset ();  
  98.            return FAILURE;                                              
  99.          } // End if
  100.  
  101.      #endif  
  102.      return SUCCESS;
  103.   } // End of SetUp for DDRAWDRIVER
  104.  
  105. VOID DDRAWDRIVER::Reset ()
  106.   {
  107.      #if defined (__FORGDK__)
  108.        if (lpDirectDraw!=NULL)
  109.          {
  110.            lpDirectDraw->RestoreDisplayMode ();
  111.            lpDirectDraw->Release ();
  112.            lpDirectDraw = NULL;  
  113.          } // End if
  114.      #endif 
  115.   } // End of Reset for DDRAWDRIVER
  116.  
  117. VOID DDRAWDRIVER::Clear ( HDISPLAY hDisplay, LONG Color )
  118.   {
  119.     if (hDisplay)
  120.       {}
  121.     if (Color)
  122.       {}    
  123.     #if defined (__FORGDK__)
  124.       INT i;
  125.       for (i=0;i<2;i++)
  126.         {
  127.           DDBLTFX ddbltfx;
  128.           ddbltfx.dwSize = sizeof (DDBLTFX);
  129.           ddbltfx.dwFillColor = Color;
  130.           if (lpSecondarySurface->Blt ( NULL, NULL, NULL, DDBLT_WAIT | DDBLT_COLORFILL,
  131.                                         &ddbltfx )!=DD_OK)
  132.             return;                           
  133.           if (lpPrimarySurface->Flip ( NULL, DDFLIP_WAIT )!=DD_OK)
  134.             return;
  135.         } // End for
  136.     #else
  137.       if (hDisplay)
  138.         {}
  139.       if (Color)
  140.         {}
  141.     #endif
  142.   } // End of Clear for DDRAWDRIVER
  143.  
  144. LONG DDRAWDRIVER::GetWidth ( HDISPLAY hDisplay )
  145.   {
  146.     if (hDisplay)
  147.       {}
  148.     #if defined (__FORGDK__)
  149.       return Width;
  150.     #else
  151.       return NULL;
  152.     #endif
  153.   } // End of GetWidth for DDRAWDRIVER
  154.  
  155. LONG DDRAWDRIVER::GetHeight ( HDISPLAY hDisplay )
  156.   {
  157.     if (hDisplay)
  158.       {}
  159.     #if defined (__FORGDK__)
  160.       return Height;
  161.     #else
  162.       return NULL;
  163.     #endif
  164.   } // End of GetHeight for DDRAWDRIVER
  165.  
  166. VOID DDRAWDRIVER::DisplayImage ( IMAGE *Image, LONG Sx, LONG Sy,
  167.                                  LONG Wd, LONG Ht, HDISPLAY Dest,
  168.                                  LONG Cx, LONG Cy, LONG DestWd, LONG DestHt )
  169.   {
  170.      if (Image)
  171.        {}
  172.      if (Sx&Sy&Wd&Ht&Cx&Cy)
  173.        {}
  174.      if (DestWd&DestHt)
  175.        {}  
  176.      if (Dest)
  177.        {}
  178.      #if defined (__FORGDK__)
  179.        HRESULT Result;
  180.        DDSURFACEDESC ddsdDesc;
  181.        INT i;
  182.        
  183.        ddsdDesc.dwSize = sizeof(DDSURFACEDESC);       
  184.        Result = lpSecondarySurface->Lock ( NULL, &ddsdDesc, 0, NULL );
  185.  
  186.        if (Result==DDERR_SURFACELOST)
  187.          {
  188.            if (lpPrimarySurface->Restore()!=DD_OK)
  189.              return;               
  190.            Clear ( hMainWindow, 0 );
  191.            Result = lpSecondarySurface->Lock ( NULL, &ddsdDesc, 0, NULL );           
  192.          } // End if
  193.        if (Result!=DD_OK)
  194.          return;
  195.  
  196.        BYTE *DestBuffer = (BYTE*)ddsdDesc.lpSurface;
  197.        DestBuffer += Cy*Width+Cx;
  198.        LONG lDestPitch = ddsdDesc.lPitch;
  199.          
  200.        BYTE *SrcBuffer = Image->SetOffset ( Sx, Sy );    
  201.        
  202.        if ((MagH==DivH)&&(MagV==DivV))
  203.          {
  204.            for (i=0;i<Ht;i++)
  205.             {
  206.               memcpy ( DestBuffer, SrcBuffer, Wd );
  207.               SrcBuffer = Image->GetNextRow ( ROW_DOWN );
  208.               DestBuffer += BytesPerRow;
  209.             } // End for
  210.          } // End if
  211.        else
  212.          {    
  213.            WORD Error = 0;
  214.            DWORD AddError = (((LONG)DivH<<16)/MagH);
  215.  
  216.            INT i; 
  217.            for (i=0;i<DestHt;i++)
  218.              {
  219.                ScaleBlt256To256 ( DestBuffer, SrcBuffer, DestWd, Error, AddError );
  220.                SrcBuffer = Image->SetOffset ( Sx, Sy+(i*DivV)/MagV );
  221.                DestBuffer += BytesPerRow;
  222.              } // End for
  223.           } // End else
  224.  
  225.               Result = lpSecondarySurface->Unlock (NULL);
  226.         if (Result!=DD_OK)
  227.           {
  228.                       return;
  229.           } // End if  
  230.  
  231.         Result = lpPrimarySurface->Flip ( NULL, DDFLIP_WAIT );
  232.         if (Result!=DD_OK)
  233.           {
  234.             if (Result==DDERR_SURFACELOST)
  235.               {
  236.                 lpPrimarySurface->Restore ();                  
  237.               } // End if  
  238.           } // End if  
  239.      #endif
  240.   } // End of DisplayImage for DDRAWDRIVER
  241.  
  242. VOID DDRAWDRIVER::SetPalette ( HDISPLAY hDisplay, RGBPALETTE *Pal )
  243.   {
  244.      if (hDisplay)
  245.        {}
  246.      if (Pal)
  247.        {}
  248.      #if defined (__FORGDK__)
  249.            PALETTEENTRY pePalette[256];
  250.              RGBCOLOR *Entry;
  251.  
  252.              Entry = Pal->GetEntry ();
  253.  
  254.              INT i;
  255.              for (i=0;i<256;i++)
  256.                  {
  257.                    pePalette[i].peRed = Entry[i].Red;
  258.                    pePalette[i].peGreen = Entry[i].Green;
  259.                    pePalette[i].peBlue = Entry[i].Blue;
  260.                  } // End for
  261.  
  262.            lpDirectDrawPalette->SetEntries ( DDSETPAL_IMMEDIATE, 0, 256, pePalette );           
  263.      #endif
  264.   } // End of SetPalette for DDRAWDRIVER
  265.  
  266. VOID DDRAWDRIVER::GetPalette ( HDISPLAY hDisplay, RGBPALETTE *Pal )
  267.   {
  268.      if (hDisplay)
  269.        {}
  270.      if (Pal)
  271.        {}
  272.      #if defined (__FORGDK__)
  273.            PALETTEENTRY pePalette[256];
  274.              RGBCOLOR *Entry;
  275.  
  276.              Entry = Pal->GetEntry ();
  277.  
  278.            lpDirectDrawPalette->SetEntries ( 0, 0, 256, pePalette );           
  279.  
  280.              INT i;
  281.              for (i=0;i<256;i++)
  282.                  {
  283.                    Entry[i].Red = pePalette[i].peRed;
  284.                    Entry[i].Green = pePalette[i].peGreen;
  285.                    Entry[i].Blue = pePalette[i].peBlue;
  286.                  } // End for
  287.      #endif
  288.   } // End of GetPalette for DDRAWDRIVER
  289.  
  290. VOID DDRAWDRIVER::DrawLine ( HDISPLAY hDisplay, LONG x1, LONG y1, LONG x2, LONG y2,
  291.                                 COLOR  Color )
  292.   {
  293.      if (hDisplay)
  294.        {}
  295.      if (x1&y1&x2&y2)
  296.        {}
  297.      if (Color)
  298.        {}
  299.      #if defined (__FORGDK__)
  300.      #endif
  301.   } // End of DrawLine for DDRAWDRIVER
  302.  
  303. VOID DDRAWDRIVER::DrawRect ( HDISPLAY hDisplay, LONG x1, LONG y1, LONG x2, LONG y2,
  304.                                  COLOR Color )
  305.   {
  306.      if (hDisplay)
  307.        {}
  308.      if (x1&y1&x2&y2)
  309.        {}
  310.      if (Color)
  311.        {}
  312.      #if defined (__FORGDK__)
  313.      #endif
  314.   } // End of DrawRect for DDRAWDRIVER
  315.  
  316. VOID DDRAWDRIVER::FillRect ( HDISPLAY hDisplay, LONG x1, LONG y1, LONG x2, LONG y2,
  317.                                COLOR Color )
  318.   {
  319.      if (hDisplay)
  320.        {}
  321.      if (x1&y1&x2&y2)
  322.        {}
  323.      if (Color)
  324.        {}
  325.   } // End of FillRect for DDRAWDRIVER
  326.  
  327. VOID DDRAWDRIVER::DrawPixel ( HDISPLAY hDisplay, LONG x, LONG y,
  328.                                   COLOR Color )
  329.   {
  330.      if (hDisplay)
  331.        {}
  332.      if (x&y)
  333.        {}
  334.      if (Color)
  335.        {}
  336.   } // End of DrawPixel for DDRAWDRIVER
  337.  
  338. VOID DDRAWDRIVER::DrawEllipse ( HDISPLAY hDisplay, LONG Cx, LONG Cy, LONG Rx, LONG Ry,
  339.                                     COLOR Color )
  340.   {
  341.      if (hDisplay)
  342.        {}
  343.      if (Cx&Cy&Rx&Ry)
  344.        {}
  345.      if (Color)
  346.        {}
  347.   } // End of DrawEllipse for DDRAWDRIVER
  348.  
  349. VOID DDRAWDRIVER::FillEllipse ( HDISPLAY hDisplay, LONG Cx, LONG Cy, LONG Rx, LONG Ry,
  350.                                   COLOR Color )
  351.   {
  352.      if (hDisplay)
  353.        {}
  354.      if (Cx&Cy&Rx&Ry)
  355.        {}
  356.      if (Color)
  357.        {}
  358.   } // End of FillEllipse for DDRAWDRIVER
  359.  
  360. VOID DDRAWDRIVER::DrawText ( HDISPLAY hDisplay, STRING Text, LONG x, LONG y,
  361.                                  COLOR Color )
  362.   {
  363.      if (hDisplay)
  364.        {}
  365.      if (x&y)
  366.        {}
  367.      if (Color)
  368.        {}
  369.      if (Text)
  370.        {}
  371.   } // End of DrawText for DDRAWDRIVER
  372.  
  373.