home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 May / PCP163A.iso / Runimage / Cbuilder4 / Examples / DDraw / DDraw2 / MAIN.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-26  |  5.2 KB  |  215 lines

  1. /*
  2.   This program introduces the concept of loading a bitmap into a DirectDraw
  3.   scene. The program flips back and forth between two surfaces, one of
  4.   which contains a bitmap, while the second is black. Therefore the
  5.   picture should appear to blink on and off.
  6. */
  7.  
  8. // Includes 
  9. #include <vcl.h>
  10. #include <ddraw.h>
  11. #pragma hdrstop
  12. #include "Main.h"
  13. #include "ddutil.h"
  14. #pragma resource "*.dfm"
  15.  
  16. //Const
  17. #define TIMER_ID        1
  18. #define TIMER_RATE      500
  19.  
  20. // Global Variables
  21. TForm1 *Form1;
  22. char szBackground[] = "BACK";
  23.  
  24. ///////////////////////////////////////
  25. // Constructor
  26. ///////////////////////////////////////
  27. __fastcall TForm1::TForm1(TComponent* Owner)
  28.         : TForm(Owner)
  29. {
  30.   lpDD = NULL;
  31.   phase = 0;
  32.   bActive = False;
  33.   FrontMsg = "Front buffer (F12 or Esc to quit)";
  34.   BackMsg = "Back buffer (F12 or Esc to quit)";
  35. }
  36.  
  37. ///////////////////////////////////////
  38. // WM_DESTROY messages
  39. ///////////////////////////////////////
  40. void __fastcall TForm1::FormDestroy(TObject *Sender)
  41. {
  42.   if(lpDD != NULL)
  43.   {
  44.     if(lpDDSPrimary != NULL)
  45.     {
  46.       lpDDSPrimary->Release();
  47.       lpDDSPrimary = NULL;
  48.     }
  49.     if(lpDDPal != NULL )
  50.     {
  51.       lpDDPal->Release();
  52.       lpDDPal = NULL;
  53.     }
  54.     lpDD->Release();
  55.     lpDD = NULL;
  56.   }
  57. }
  58.  
  59. ///////////////////////////////////////
  60. // Initialize DirectDraw
  61. ///////////////////////////////////////
  62. void __fastcall TForm1::Start()
  63. {
  64.   HRESULT ddrval;
  65.   DDSURFACEDESC ddsd;
  66.   DDSCAPS ddscaps;
  67.   char buf[256];
  68.  
  69.   ddrval = DirectDrawCreate(NULL, &lpDD, NULL);
  70.   if(ddrval == DD_OK)
  71.   {
  72.     // Enter exclusive mode
  73.     ddrval = lpDD->SetCooperativeLevel(Handle,
  74.       DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
  75.     if(ddrval == DD_OK)
  76.     {
  77.       ddrval = lpDD->SetDisplayMode(640, 480, 8);
  78.       if(ddrval == DD_OK)
  79.       {
  80.         // Create the primary surface with 1 back buffer
  81.         ddsd.dwSize = sizeof(ddsd);
  82.         ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
  83.         ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE |
  84.                               DDSCAPS_FLIP |
  85.                               DDSCAPS_COMPLEX;
  86.         ddsd.dwBackBufferCount = 1;
  87.         ddrval = lpDD->CreateSurface(&ddsd, &lpDDSPrimary, NULL);
  88.         if(ddrval == DD_OK)
  89.         {
  90.           // Retrieve pointer to back buffer
  91.           ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
  92.           ddrval = lpDDSPrimary->GetAttachedSurface(&ddscaps,
  93.                                                 &lpDDSBack);
  94.  
  95.           if(ddrval == DD_OK)
  96.           {
  97.             lpDDPal = DDLoadPalette(lpDD, szBackground);
  98.             if (lpDDPal == NULL)
  99.               goto error;
  100.  
  101.             ddrval = lpDDSPrimary->SetPalette(lpDDPal);
  102.  
  103.             if( ddrval != DD_OK )
  104.               goto error;
  105.  
  106.             // load a bitmap into the back buffer.
  107.             ddrval = DDReLoadBitmap(lpDDSBack, szBackground);
  108.  
  109.             if( ddrval != DD_OK )
  110.               goto error;
  111.  
  112.             // Create a timer to flip the pages
  113.             Timer1->Enabled = True;
  114.             bActive = True;
  115.             return;
  116.           }
  117.         }
  118.       }
  119.     }
  120.   }
  121. error:
  122.   wsprintf(buf, "Direct Draw Init Failed (%08lx)\n", ddrval);
  123.   MessageBox(Handle, buf, "ERROR", MB_OK);
  124.   Close();
  125. }
  126.  
  127. ///////////////////////////////////////
  128. // WM_KEYDOWN messages
  129. ///////////////////////////////////////
  130. void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key,
  131.         TShiftState Shift)
  132. {
  133.  
  134.    switch (Key)
  135.    {
  136.      case VK_F3:
  137.        Start();
  138.        break;
  139.  
  140.      case VK_ESCAPE:
  141.      case VK_F12:
  142.        Close();
  143.        break;
  144.    }
  145. }
  146.  
  147. ///////////////////////////////////////
  148. // WM_PAINT messages
  149. ///////////////////////////////////////
  150. void __fastcall TForm1::FormPaint(TObject *Sender)
  151. {
  152.   RECT rc;
  153.   SIZE size;
  154.   char szMsg[] = "Page Flipping Test: Press F3 to start, F12 or Esc to exit";
  155.   
  156.   if (!bActive)
  157.   {
  158.     HDC DC = GetDC(Handle);
  159.     rc = GetClientRect();
  160.     GetTextExtentPoint(DC, szMsg, lstrlen(szMsg), &size);
  161.     SetBkColor(DC, RGB(0, 0, 0));
  162.     SetTextColor(DC, RGB(255, 255, 0));
  163.     TextOut(DC, (rc.right - size.cx)/2, (rc.bottom - size.cy)/2,
  164.       szMsg, sizeof(szMsg)-1);
  165.     ReleaseDC(Handle, DC);
  166.   }
  167. }
  168.  
  169. ///////////////////////////////////////
  170. // WM_TIMER messages
  171. ///////////////////////////////////////
  172. void __fastcall TForm1::Timer1Timer(TObject *Sender)
  173. {
  174.   HDC DC;
  175.  
  176.   if (lpDDSBack->GetDC(&DC) == DD_OK)
  177.   {
  178.     if(phase)
  179.     {
  180.       SetBkColor(DC, RGB(0, 0, 255));
  181.       SetTextColor(DC, RGB(255, 255, 0));
  182.       TextOut(DC, 0, 0, FrontMsg.c_str(), FrontMsg.Length());
  183.       phase = 0;
  184.     }
  185.     else
  186.     {
  187.       SetBkColor(DC, RGB(0, 0, 255));
  188.       SetTextColor(DC, RGB(0, 255, 255));
  189.       TextOut(DC, 0, 0, BackMsg.c_str(), BackMsg.Length());
  190.       phase = 1;
  191.     }
  192.     lpDDSBack->ReleaseDC(DC);
  193.   }
  194.  
  195.   while(1)
  196.   {
  197.     HRESULT ddrval;
  198.     ddrval = lpDDSPrimary->Flip( NULL, 0 );
  199.     if( ddrval == DD_OK )
  200.       break;
  201.     if( ddrval == DDERR_SURFACELOST )
  202.     {
  203.       ddrval = lpDDSPrimary->Restore();
  204.       if( ddrval != DD_OK )
  205.         break;
  206.       ddrval = DDReLoadBitmap(lpDDSBack, szBackground);
  207.       if( ddrval != DD_OK )
  208.         break;
  209.     }
  210.     if( ddrval != DDERR_WASSTILLDRAWING )
  211.       break;
  212.   }
  213. }
  214.  
  215.