home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / Chapt_04 / TestDDraw / TestDDraw.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-11  |  5.3 KB  |  195 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   : testddraw.cpp                                                         //
  10. //  Description: Test program for DirectDraw method hooking                          //
  11. //  Version    : 1.00.000, May 31, 2000                                              //
  12. //-----------------------------------------------------------------------------------//
  13.  
  14. #define STRICT
  15. #define NOCRYPT
  16. #include <windows.h>
  17. #include <assert.h>
  18. #include <tchar.h>
  19. #include <ddraw.h>
  20.  
  21. #include "..\..\include\win.h"
  22. #include "..\Diver\Diver.h"
  23.  
  24. const TCHAR szMessage[] = _T("Hello, World !");
  25. const TCHAR szFace[]    = _T("Times New Roman");
  26. const TCHAR szHint[]    = _T("Press ESC to quit.");
  27. const TCHAR szProgram[] = _T("HelloWorld4");
  28.  
  29.  
  30. void CenterText(HDC hDC, int x, int y, LPCTSTR szFace, 
  31.                 LPCTSTR szMessage, int point)
  32. {
  33.     HFONT hFont = CreateFont(
  34.         - point * GetDeviceCaps(hDC, LOGPIXELSY) / 72,
  35.         0, 0, 0, FW_BOLD, TRUE, FALSE, FALSE, 
  36.         ANSI_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, 
  37.         PROOF_QUALITY, VARIABLE_PITCH, szFace);
  38.     assert(hFont);
  39.  
  40.     HGDIOBJ hOld = SelectObject(hDC, hFont);
  41.  
  42.     SetTextAlign(hDC, TA_CENTER | TA_BASELINE);
  43.  
  44.     SetBkMode(hDC, TRANSPARENT);
  45.     SetTextColor(hDC, RGB(0, 0, 0xFF));
  46.     TextOut(hDC, x, y, szMessage, _tcslen(szMessage));
  47.  
  48.     SelectObject(hDC, hOld);
  49.     DeleteObject(hFont);
  50. }
  51.  
  52.  
  53. class KDDrawWindow : public KWindow
  54. {
  55.     LPDIRECTDRAW        lpdd;
  56.     LPDIRECTDRAWSURFACE lpddsprimary;
  57.     
  58.     void OnKeyDown(WPARAM wParam, LPARAM lParam)
  59.     {
  60.         if ( wParam==VK_ESCAPE )
  61.             PostMessage(m_hWnd, WM_CLOSE, 0, 0);
  62.     }
  63.  
  64.     void Blend(int left, int right, int top, int bottom);
  65.  
  66.     void OnDraw(HDC hDC)
  67.     {
  68.         TextOut(hDC, 0, 0, szHint, lstrlen(szHint));
  69.         CenterText(hDC, GetSystemMetrics(SM_CXSCREEN)/2,
  70.         GetSystemMetrics(SM_CYSCREEN)/2,
  71.         szFace, szMessage, 48);
  72.  
  73.         Blend(80, 560, 160, 250);
  74.     }
  75.  
  76. public:
  77.     KDDrawWindow(void)
  78.     {
  79.         lpdd         = NULL;
  80.         lpddsprimary = NULL;
  81.     }
  82.  
  83.     ~KDDrawWindow(void)
  84.     {
  85.         if ( lpddsprimary )
  86.         {
  87.             lpddsprimary->Release();
  88.             lpddsprimary = NULL;
  89.         }
  90.  
  91.         if ( lpdd )
  92.         {
  93.             lpdd->Release();
  94.             lpdd = NULL;
  95.         }
  96.     }
  97.  
  98.     bool CreateSurface(void);
  99. };
  100.  
  101.  
  102. bool KDDrawWindow::CreateSurface(void)
  103. {
  104.     HRESULT hr;
  105.  
  106.     hr = DirectDrawCreate(NULL, &lpdd, NULL);
  107.     if (hr!=DD_OK) 
  108.         return false;
  109.  
  110.     hr = lpdd->SetCooperativeLevel(m_hWnd, 
  111.         DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE);
  112.     if (hr!=DD_OK) 
  113.         return false;
  114.  
  115.     hr = lpdd->SetDisplayMode(640, 480, 24);
  116.     if (hr!=DD_OK) 
  117.         return false;
  118.  
  119.     DDSURFACEDESC ddsd;
  120.     memset(& ddsd, 0, sizeof(ddsd));
  121.     ddsd.dwSize = sizeof(ddsd);
  122.     ddsd.dwFlags = DDSD_CAPS;
  123.     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
  124.  
  125.     return lpdd->CreateSurface(&ddsd, &lpddsprimary, NULL)
  126.        ==DD_OK;
  127. }
  128.  
  129.  
  130. void inline Blend(unsigned char *dest, unsigned char *src)
  131. {
  132.     dest[0] = (dest[0] + src[0])/2;
  133.     dest[1] = (dest[1] + src[1])/2;
  134.     dest[2] = (dest[2] + src[2])/2; 
  135. }
  136.  
  137.  
  138. void KDDrawWindow::Blend(int left, int right, 
  139.                          int top, int bottom)
  140. {
  141.     if (lpddsprimary==NULL)
  142.         return;
  143.  
  144.     DDSURFACEDESC ddsd;
  145.  
  146.     memset(&ddsd, 0, sizeof(ddsd));
  147.     ddsd.dwSize = sizeof(ddsd);
  148.  
  149.     HRESULT hr = lpddsprimary->Lock(NULL, &ddsd, 
  150.         DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT, NULL);
  151.     assert(hr==DD_OK);
  152.  
  153.     unsigned char *screen = (unsigned char *) ddsd.lpSurface;
  154.  
  155.     for (int y=top; y<bottom; y++)
  156.     {
  157.         unsigned char * pixel = screen + y * ddsd.lPitch 
  158.            + left * 3;
  159.  
  160.         for (int x=left; x<right; x++, pixel+=3)
  161.             if ( pixel[0]!=255 || pixel[1]!=255 || 
  162.                pixel[2]!=255 ) // non white
  163.             {
  164.                 ::Blend(pixel-3, pixel);           // left
  165.                 ::Blend(pixel+3, pixel);           // right
  166.  
  167.                 ::Blend(pixel-ddsd.lPitch, pixel); // up
  168.                 ::Blend(pixel+ddsd.lPitch, pixel); // down
  169.             }
  170.     }
  171.  
  172.     lpddsprimary->Unlock(ddsd.lpSurface);
  173. }
  174.  
  175.  
  176. int WINAPI WinMain(HINSTANCE hInst, HINSTANCE, 
  177.                    LPSTR lpCmd, int nShow)
  178. {
  179.     KDDrawWindow win;
  180.  
  181.     StartDiver();
  182.  
  183.     win.CreateEx(0, szProgram, szProgram,
  184.                  WS_POPUP,
  185.                  0, 0,
  186.                  GetSystemMetrics( SM_CXSCREEN ),
  187.                  GetSystemMetrics( SM_CYSCREEN ),
  188.                  NULL, NULL, hInst);
  189.  
  190.     win.CreateSurface();
  191.     win.ShowWindow(nShow);
  192.     win.UpdateWindow();
  193.     
  194.     return win.MessageLoop();
  195. }