home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / Chapt_18 / ddbasic / ddbasic.cpp next >
Encoding:
C/C++ Source or Header  |  2000-05-23  |  5.2 KB  |  207 lines

  1. //-----------------------------------------------------------------------------------//
  2. //              Windows Graphics Programming: Win32 GDI and DirectDraw               //
  3. //                             ISBN  0-13-086985-6                                   //
  4. //                                                                                   //
  5. //  Written            by  Yuan, Feng                             www.fengyuan.com   //
  6. //  Copyright (c) 2000 by  Hewlett-Packard Company                www.hp.com         //
  7. //  Published          by  Prentice Hall PTR, Prentice-Hall, Inc. www.phptr.com      //
  8. //                                                                                   //
  9. //  FileName   : ddbasic.cpp                                                         //
  10. //  Description: Basic DirectDraw demo, Chapter 18                                   //
  11. //  Version    : 1.00.000, May 31, 2000                                              //
  12. //-----------------------------------------------------------------------------------//
  13.  
  14. #define STRICT
  15. #define WIN32_LEAN_AND_MEAN
  16.  
  17. #include <windows.h>
  18. #include <assert.h>
  19. #include <tchar.h>
  20.  
  21. #define INITGUID
  22. #include <ddraw.h>
  23. #include <d3d.h>
  24.  
  25. #include "..\..\include\win.h"
  26. #include "..\..\include\ddsurf.h"
  27. #include "..\..\include\ddwrap.h"
  28.  
  29. #include "resource.h"
  30.  
  31. BOOL PixelFillRect(KDDSurface & surface, int x, int y, int width, int height, DWORD dwColor[], int nColor)
  32. {
  33.     BYTE * pSurface = surface.LockSurface(NULL);
  34.         
  35.     const DDSURFACEDESC2 * pDesc = surface.GetSurfaceDesc();
  36.  
  37.     if ( pSurface )
  38.     {
  39.         int pitch   = surface.GetPitch();
  40.         int byt     = pDesc->ddpfPixelFormat.dwRGBBitCount / 8; 
  41.         
  42.         for (int j=0; j<height; j++)
  43.         {
  44.             BYTE * pS   = pSurface + (y+j) * pitch + x * byt;
  45.             DWORD color = dwColor[j % nColor];
  46.  
  47.             int i;
  48.  
  49.             switch ( byt )
  50.             {
  51.                 case 1:
  52.                     memset(pS, color, width);
  53.                     break;
  54.  
  55.                 case 2:
  56.                     for (i=0; i<width; i++)
  57.                     {
  58.                         * (unsigned short *) pS = (unsigned short) color;
  59.                         pS += sizeof(unsigned short);
  60.                     }
  61.                     break;
  62.  
  63.                 case 3:
  64.                     for (i=0; i<width; i++)
  65.                     {
  66.                         * (RGBTRIPLE *) pS = * (RGBTRIPLE *) & color;
  67.                         pS += sizeof(RGBTRIPLE);
  68.                     }
  69.                     break;
  70.  
  71.                 case 4:
  72.                     for (i=0; i<width; i++)
  73.                     {
  74.                         * (unsigned *) pS = color;
  75.                         pS += sizeof(unsigned);
  76.                     }
  77.                     break;
  78.                 
  79.                 default:
  80.                     return FALSE;
  81.             }
  82.         }
  83.  
  84.         surface.Unlock();
  85.         return TRUE;
  86.     }
  87.     else
  88.         return FALSE;
  89. }
  90.  
  91.  
  92. class KDDWin : public KWindow, public KDirectDraw
  93. {
  94.     HINSTANCE m_hInst;
  95.  
  96.     void OnNCPaint(void)
  97.     {
  98.         RECT rect;
  99.         GetWindowRect(m_hWnd, & rect);
  100.  
  101.         DWORD dwColor[18];
  102.         
  103.         for (int i=0; i<18; i++)
  104.             dwColor[i] = m_primary.ColorMatch(0, 0, 0x80 + abs(i-9)*12);
  105.  
  106.         PixelFillRect(m_primary, rect.left+24, rect.top+4, rect.right - 88 - rect.left, 18, dwColor, 18);
  107.  
  108.         BYTE * pSurface = m_primary.LockSurface(NULL); // just for address
  109.         m_primary.Unlock(NULL);
  110.     
  111.         if ( SUCCEEDED(m_primary.GetDC()) )
  112.         {
  113.             TCHAR temp[MAX_PATH];
  114.             const DDSURFACEDESC2 * pDesc = m_primary.GetSurfaceDesc();
  115.  
  116.             if ( pDesc )
  117.                 wsprintf(temp, "%dx%d %d-bpp, pitch=%d, lpSurface=0x%x", 
  118.                     pDesc->dwWidth, pDesc->dwHeight, pDesc->ddpfPixelFormat.dwRGBBitCount,
  119.                     pDesc->lPitch, pSurface);
  120.             else
  121.                 strcpy(temp, "LockSurface failed");
  122.  
  123.             SetBkMode(m_primary, TRANSPARENT);
  124.             SetTextColor(m_primary, RGB(0xFF, 0xFF, 0));
  125.             TextOut(m_primary, rect.left+24, rect.top+4, temp, _tcslen(temp));
  126.             
  127.             m_primary.ReleaseDC();
  128.         }
  129.     }
  130.  
  131.     void OnDraw(void)
  132.     {
  133.         SetClientRect(m_hWnd);
  134.         int n = min(m_rcDest.right-m_rcDest.left, m_rcDest.bottom-m_rcDest.top)/2;
  135.         
  136.         for (int i=0; i<n; i++)
  137.         {
  138.             DWORD color = m_primary.ColorMatch( 0xFF*(n-1-i)/(n-1), 0xFF*(n-1-i)/(n-1), 0xFF*i/(n-1) );
  139.  
  140.             m_primary.FillColor(m_rcDest.left+i, m_rcDest.top+i, m_rcDest.right-i, m_rcDest.bottom-i, color);
  141.         }
  142.     }
  143.  
  144.     LRESULT WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  145.     {
  146.         switch( uMsg )
  147.         {
  148.             case WM_CREATE:
  149.                 m_hWnd = hWnd;
  150.                 if ( FAILED(SetupDirectDraw(GetParent(hWnd), hWnd, false)) ) // initialize DirectDraw
  151.                 {
  152.                     MessageBox(NULL, _T("Unable to Initialize DirectDraw"), _T("KDDWin"), MB_OK);
  153.                     CloseWindow(hWnd);
  154.                 }
  155.                 return 0;
  156.  
  157.             case WM_PAINT:
  158.                 OnDraw();
  159.                 ValidateRect(hWnd, NULL);
  160.                 return 0;
  161.  
  162.             case WM_NCPAINT:
  163.                 DefWindowProc(hWnd, uMsg, wParam, lParam);
  164.                 OnNCPaint();
  165.                 return 0;
  166.  
  167.             case WM_DESTROY:
  168.                 PostQuitMessage(0);
  169.                 return 0;
  170.  
  171.             default:
  172.                 return DefWindowProc(hWnd, uMsg, wParam, lParam);
  173.         }
  174.     }
  175.  
  176.     void GetWndClassEx(WNDCLASSEX & wc)
  177.     {
  178.         KWindow::GetWndClassEx(wc);
  179.     
  180.         wc.style |= (CS_HREDRAW | CS_VREDRAW);
  181.         wc.hIcon = LoadIcon(m_hInst, MAKEINTRESOURCE(IDI_GRAPH));
  182.     }
  183.  
  184. public:
  185.     KDDWin(HINSTANCE hInst)
  186.     {
  187.         m_hInst = hInst;
  188.     }
  189. };
  190.  
  191.  
  192. int WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, int nShow)
  193. {
  194.     KDDWin main(hInst);
  195.     
  196.     main.CreateEx(0, _T("ClassName"), _T("Basic DirectDraw"),
  197.         WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
  198.         CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 
  199.         NULL, NULL, hInst);
  200.  
  201.     main.ShowWindow(nShow);
  202.     main.UpdateWindow();
  203.     main.MessageLoop();
  204.  
  205.     return 0;
  206. }
  207.