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

  1. /*
  2.   This program is based heavily on an example from the
  3.   DirectX SDK. It demonstrates one way to animate a bitmap. 
  4. */
  5.  
  6. #include <vcl.h>
  7. #include <ddraw.h>
  8. #pragma hdrstop
  9. #include "ddutil.h"
  10. #include "Main.h"
  11. #pragma resource "*.dfm"
  12. #define NAME "DDExample4"
  13. #define TITLE "Direct Draw Example 4"
  14.  
  15. char szBitmap[] = "ALL";
  16. TForm1 *Form1;
  17.  
  18. __fastcall TForm1::TForm1(TComponent* Owner)
  19.         : TForm(Owner)
  20. {
  21.   FRunApp = True;
  22.   FActive = False;  
  23. }
  24.  
  25. MESSAGE void TForm1::MyMove(TMessage &Message)
  26. {
  27.   do
  28.   {
  29.     UpdateFrame();
  30.     Application->ProcessMessages();
  31.   }
  32.   while(FRunApp == True);
  33. }
  34.  
  35. void TForm1::InitFail()
  36. {
  37.     MessageBox(Handle, "DirectDraw Init FAILED", TITLE, MB_OK );
  38.     Close();
  39. } /* initFail */
  40.  
  41. void TForm1::Start()
  42. {
  43.   HRESULT ddrval;
  44.   DDSURFACEDESC    ddsd;
  45.   DDSCAPS ddscaps;
  46.  
  47.   ddrval = DirectDrawCreate( NULL, &lpDD, NULL );
  48.   if( ddrval != DD_OK )
  49.   {
  50.      InitFail();
  51.      return;
  52.   }
  53.  
  54.   // Get exclusive mode
  55.   ddrval = lpDD->SetCooperativeLevel(Handle, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN );
  56.   if( ddrval != DD_OK )
  57.   {
  58.     InitFail();
  59.     return;
  60.   }
  61.  
  62.   // Set the video mode to 640x480x8
  63.   ddrval = lpDD->SetDisplayMode( 640, 480, 8);
  64.   if( ddrval != DD_OK )
  65.   {
  66.     InitFail();
  67.     return;
  68.   }
  69.  
  70.   // Create the primary surface with 1 back buffer
  71.   ddsd.dwSize = sizeof( ddsd );
  72.   ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
  73.   ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE |
  74.                         DDSCAPS_FLIP |
  75.                         DDSCAPS_COMPLEX;
  76.   ddsd.dwBackBufferCount = 1;
  77.   ddrval = lpDD->CreateSurface( &ddsd, &lpDDSPrimary, NULL );
  78.   if( ddrval != DD_OK )
  79.   {
  80.     InitFail();
  81.     return;
  82.   }
  83.  
  84.   ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
  85.   ddrval = lpDDSPrimary->GetAttachedSurface(&ddscaps, &lpDDSBack);
  86.   if( ddrval != DD_OK )
  87.   {
  88.     InitFail();
  89.     return;
  90.   }
  91.  
  92.   // create and set the palette
  93.   lpDDPal = DDLoadPalette(lpDD, szBitmap);
  94.  
  95.   if (lpDDPal)
  96.       lpDDSPrimary->SetPalette(lpDDPal);
  97.  
  98.   // Create the offscreen surface, by loading our bitmap.
  99.   lpDDSOne = DDLoadBitmap(lpDD, szBitmap, 0, 0);
  100.  
  101.   if( lpDDSOne == NULL )
  102.   {
  103.     InitFail();
  104.     return;
  105.   }
  106.   // Set the color key for this bitmap (black)
  107.   DDSetColorKey(lpDDSOne, RGB(0,0,0));
  108. }
  109.  
  110. void __fastcall TForm1::FormDestroy(TObject *Sender)
  111. {
  112.   if( lpDD != NULL )
  113.   {
  114.     if( lpDDSPrimary != NULL )
  115.     {
  116.       lpDDSPrimary->Release();
  117.       lpDDSPrimary = NULL;
  118.     }
  119.     if( lpDDSOne != NULL )
  120.     {
  121.       lpDDSOne->Release();
  122.       lpDDSOne = NULL;
  123.     }
  124.     if( lpDDPal != NULL )
  125.     {
  126.       lpDDPal->Release();
  127.       lpDDPal = NULL;
  128.     }
  129.     lpDD->Release();
  130.     lpDD = NULL;
  131.   }
  132. }
  133. //---------------------------------------------------------------------------
  134. void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key,
  135.         TShiftState Shift)
  136. {
  137.   switch (Key)
  138.   {
  139.     case VK_F3:
  140.       FActive = True;
  141.       Start();
  142.       PostMessage(Handle, WM_INFOSTART, 0, 0);
  143.       break;
  144.       
  145.     case VK_ESCAPE:
  146.     case VK_F12:
  147.       FRunApp = False;
  148.       FActive = False;
  149.       Close();
  150.       break;
  151.   }
  152. }
  153.  
  154. HRESULT TForm1::RestoreAll( void )
  155. {
  156.   HRESULT    ddrval;
  157.  
  158.   ddrval = lpDDSPrimary->Restore();
  159.   if( ddrval == DD_OK )
  160.   {
  161.       ddrval = lpDDSOne->Restore();
  162.       if( ddrval == DD_OK )
  163.       {
  164.       DDReLoadBitmap(lpDDSOne, szBitmap);
  165.     }
  166.   }
  167.   return ddrval;
  168. } /* restoreAll */
  169.  
  170. void TForm1::UpdateFrame( void )
  171. {
  172.   static DWORD    lastTickCount[3] = {0,0,0};
  173.   static int        currentFrame[3] = {0,0,0};
  174.   DWORD        thisTickCount;
  175.   RECT        rcRect;
  176.   DWORD        delay[3] = {50, 78, 13};
  177.   int            i;
  178.   int            xpos[3] = {288, 190, 416};
  179.   int            ypos[3] = {128, 300, 256};
  180.   HRESULT        ddrval;
  181.  
  182.   // Decide which frame will be blitted next
  183.   thisTickCount = GetTickCount();
  184.   for(i=0; i<3; i++)
  185.   {
  186.     if((thisTickCount - lastTickCount[i]) > delay[i])
  187.     {
  188.       // Move to next frame;
  189.       lastTickCount[i] = thisTickCount;
  190.       currentFrame[i]++;
  191.       if(currentFrame[i] > 59)
  192.         currentFrame[i] = 0;
  193.     }
  194.   }
  195.  
  196.   // Blit the stuff for the next frame
  197.   rcRect.left = 0;
  198.   rcRect.top = 0;
  199.   rcRect.right = 640;
  200.   rcRect.bottom = 480;
  201.   while(1)
  202.   {
  203.     ddrval = lpDDSBack->BltFast( 0, 0, lpDDSOne,
  204.     &rcRect, DDBLTFAST_NOCOLORKEY );
  205.     if( ddrval == DD_OK )
  206.       break;
  207.  
  208.     if( ddrval == DDERR_SURFACELOST )
  209.     {
  210.       ddrval = RestoreAll();
  211.       if( ddrval != DD_OK )
  212.       {
  213.         return;
  214.       }
  215.     }
  216.     if( ddrval != DDERR_WASSTILLDRAWING )
  217.     {
  218.       return;
  219.     }
  220.   }
  221.  
  222.   if(ddrval != DD_OK)
  223.     return;
  224.  
  225.   for(i=0; i<3; i++)
  226.   {
  227.     rcRect.left   = currentFrame[i]%10*64;
  228.     rcRect.top    = currentFrame[i]/10*64 + 480;
  229.     rcRect.right  = currentFrame[i]%10*64 + 64;
  230.     rcRect.bottom = currentFrame[i]/10*64 + 64 + 480;
  231.  
  232.     while( 1 )
  233.     {
  234.       ddrval = lpDDSBack->BltFast( xpos[i], ypos[i], lpDDSOne,
  235.         &rcRect, DDBLTFAST_SRCCOLORKEY );
  236.       if( ddrval == DD_OK )
  237.        break;
  238.  
  239.       if( ddrval == DDERR_SURFACELOST )
  240.       {
  241.         ddrval = RestoreAll();
  242.         if( ddrval != DD_OK )
  243.           return;
  244.       }
  245.       if( ddrval != DDERR_WASSTILLDRAWING )
  246.         return;
  247.     }
  248.   }
  249.   // Flip the surfaces
  250.   while( 1 )
  251.   {
  252.     ddrval = lpDDSPrimary->Flip( NULL, 0 );
  253.     if( ddrval == DD_OK )
  254.       break;
  255.  
  256.     if( ddrval == DDERR_SURFACELOST )
  257.     {
  258.       ddrval = RestoreAll();
  259.       if( ddrval != DD_OK )
  260.         break;
  261.     }
  262.     if( ddrval != DDERR_WASSTILLDRAWING )
  263.       break;
  264.   }
  265. }
  266.  
  267. void __fastcall TForm1::FormPaint(TObject *Sender)
  268. {
  269.   AnsiString S = "F3 to Start, Esc to Exit";
  270.   HDC DC = GetDC(Handle);
  271.   if (!FActive)
  272.   {
  273.     SetBkMode(DC, TRANSPARENT);
  274.     TextOut(DC, 25, 100, S.c_str(), S.Length());
  275.   }
  276.   ReleaseDC(Handle, DC);
  277. }
  278.  
  279.