home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / Chapt_01 / Hello4 / Hello4.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-16  |  5.2 KB  |  190 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   : hello4.h                                                             //
  10. //  Description: Hello World Demo 4, DirectDraw, Chapter 1                           //
  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. #include <ddraw.h>
  21.  
  22. #include "..\..\include\win.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, 32);
  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.     DDSURFACEDESC ddsd;
  142.  
  143.     memset(&ddsd, 0, sizeof(ddsd));
  144.     ddsd.dwSize = sizeof(ddsd);
  145.  
  146.     HRESULT hr = lpddsprimary->Lock(NULL, &ddsd, 
  147.         DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT, NULL);
  148.     assert(hr==DD_OK);
  149.  
  150.     unsigned char *screen = (unsigned char *) ddsd.lpSurface;
  151.  
  152.     for (int y=top; y<bottom; y++)
  153.     {
  154.         unsigned char * pixel = screen + y * ddsd.lPitch 
  155.            + left * 4;
  156.  
  157.         for (int x=left; x<right; x++, pixel+=4)
  158.             if ( pixel[0]!=255 || pixel[1]!=255 || 
  159.                pixel[2]!=255 ) // non white
  160.             {
  161.                 ::Blend(pixel-4, pixel);           // left
  162.                 ::Blend(pixel+4, pixel);           // right
  163.  
  164.                 ::Blend(pixel-ddsd.lPitch, pixel); // up
  165.                 ::Blend(pixel+ddsd.lPitch, pixel); // down
  166.             }
  167.     }
  168.  
  169.     lpddsprimary->Unlock(ddsd.lpSurface);
  170. }
  171.  
  172.  
  173. int WINAPI WinMain(HINSTANCE hInst, HINSTANCE, 
  174.                    LPSTR lpCmd, int nShow)
  175. {
  176.     KDDrawWindow win;
  177.  
  178.     win.CreateEx(0, szProgram, szProgram,
  179.                  WS_POPUP,
  180.                  0, 0,
  181.                  GetSystemMetrics( SM_CXSCREEN ),
  182.                  GetSystemMetrics( SM_CYSCREEN ),
  183.                  NULL, NULL, hInst);
  184.  
  185.     win.CreateSurface();
  186.     win.ShowWindow(nShow);
  187.     win.UpdateWindow();
  188.  
  189.     return win.MessageLoop();
  190. }