Advertisement
xiahanlu

CDirectDraw.hxx

Jul 5th, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.28 KB | None | 0 0
  1. //  CDirectDraw.hxx
  2. //  Provide simple graphical rendering and texture reading
  3. # if _MSC_VER >= 1000
  4. #   pragma once
  5. # endif // _MSC_VER >= 1000
  6.  
  7. # if !defined (_CDIRECTDRAW_INCLUED_HEADER_CXX_)
  8. #  define _CDIRECTDRAW_INCLUED_HEADER_CXX_
  9.  
  10. # include <atldef.h>
  11. # include <directx/include/d3dx9.h>
  12.  
  13. // extern asm interface.
  14. extern "C" {
  15.   int CAlphaSingle32 (void *  _vptr, int pitch,
  16.           int dst_x, int dst_y,
  17.                                           int dst_w, int dst_h,
  18.           COLORREF col,
  19.               BYTE alpha_dst, BYTE alpha_src);
  20.   int CSurfaceCopy32 (void *  _dvptr, int d_pos_x,  int d_pos_y, int d_pitch,
  21.     void *  _svptr, int s_pos_x,  int s_pos_y, int s_pitch,
  22.                                           int w, int h);
  23. }
  24.  
  25. # if !defined (_WIN64)
  26. #  define CSurfaceCopy CSurfaceCopy32
  27. #  define CAlphaSingle CAlphaSingle32
  28. # else
  29. # endif
  30.  
  31. class CDirectDraw
  32. {
  33. private:
  34.   IDirect3DDevice9  *m_Direct3D9_device;  
  35.   IDirect3DSurface9  *m_Direct3D9_surface;
  36.  
  37.   D3DPRESENT_PARAMETERS m_d3dpp;
  38.  
  39.   HWND m_AttachWindow;
  40.  
  41.   void initSettings (void) {
  42.     m_Direct3D9_device = NULL;
  43.     m_Direct3D9_surface = NULL;
  44.   }
  45.   void CreateD3dppSurface (int width, int height)  {
  46.  
  47.     // reset d3d desc
  48.     ResetD3dpp (m_d3dpp, m_AttachWindow, width, height);
  49.    
  50.     // create d3d device
  51.     HRESULT si = sm_Direct3D9_root->CreateDevice (0,
  52.                         D3DDEVTYPE_HAL,
  53.                            m_AttachWindow,
  54.                               D3DCREATE_SOFTWARE_VERTEXPROCESSING, &m_d3dpp, & m_Direct3D9_device);
  55.     ATLASSERT (SUCCEEDED (si));
  56.   }
  57. public:
  58.   struct graph_desc {
  59.     DWORD *__vptr;
  60.     DWORD pitch;
  61.     DWORD max_width;
  62.     DWORD max_height;
  63.   };
  64.  
  65.   int lockBuffer (IDirect3DSurface9 *surface, RECT *rcLock, graph_desc &gd)  {
  66.     D3DLOCKED_RECT lock_rect;
  67.     D3DSURFACE_DESC lock_desc;
  68.     HRESULT rets = surface->LockRect (& lock_rect, rcLock, D3DLOCK_DISCARD | D3DLOCK_NOOVERWRITE);
  69.     if (FAILED (rets))
  70.       return -1;
  71.     else  ;
  72.     rets = surface->GetDesc (& lock_desc);
  73.     ATLASSERT (SUCCEEDED (rets));
  74.     gd.__vptr = (DWORD *) lock_rect.pBits;
  75.     gd.pitch = lock_rect.Pitch;
  76.     if (rcLock != NULL) {
  77.       gd.max_width = abs (rcLock->right -  rcLock->left);
  78.       gd.max_height = abs (rcLock->bottom -  rcLock->top);
  79.     } else  {
  80.       gd.max_width = lock_desc.Width;
  81.       gd.max_height = lock_desc.Height;
  82.     }
  83.     return 0;
  84.   }
  85.  
  86.   void resetBackSurface (int width, int height)  {
  87.     if (m_Direct3D9_surface != NULL)  {
  88.       m_Direct3D9_surface->Release ();
  89.       m_Direct3D9_surface = NULL;
  90.     }
  91.     HRESULT rets = m_Direct3D9_device->CreateOffscreenPlainSurface (width, height, D3DFMT_X8R8G8B8, D3DPOOL_SYSTEMMEM,
  92.           & m_Direct3D9_surface, NULL);
  93.     ATLASSERT (SUCCEEDED (rets));
  94.   }
  95.  
  96.   int lockBuffer (RECT *rcLock, graph_desc &gd)  {
  97.     return lockBuffer (m_Direct3D9_surface, rcLock, gd);
  98.   }
  99.   int unlockBuffer (void)  {
  100.     HRESULT rets =m_Direct3D9_surface->UnlockRect ();
  101.     if (FAILED (rets))
  102.       return -1;
  103.     else  return 0;
  104.   }
  105.  
  106.   void ResetD3dppSurface (int width, int height)  {
  107.  
  108.     ResetD3dpp (m_d3dpp, m_AttachWindow, width, height);
  109.  
  110.     HRESULT rets = m_Direct3D9_device->Reset (& m_d3dpp);
  111.     ATLASSERT (SUCCEEDED (rets));
  112.   }
  113.  
  114.   // global direct3d COM root.
  115.   static IDirect3D9 *sm_Direct3D9_root;
  116. public:
  117.   static void InitDirect3D (void) {
  118.     if (sm_Direct3D9_root == NULL) {
  119.       sm_Direct3D9_root = Direct3DCreate9 (D3D_SDK_VERSION);
  120.       ATLASSERT (sm_Direct3D9_root != NULL);
  121.     }
  122.   }
  123.   static void UninitDirect3D (void) {
  124.     release_com<IDirect3D9> (sm_Direct3D9_root);
  125.   }
  126.  
  127.   void ResetD3dpp (D3DPRESENT_PARAMETERS &d3dpp, HWND window, int width, int height)  {
  128.  
  129.     ZeroMemory (& d3dpp, sizeof (d3dpp));
  130.  
  131.     d3dpp.BackBufferFormat      = D3DFMT_X8R8G8B8;
  132.     d3dpp.SwapEffect            = D3DSWAPEFFECT_COPY;
  133.     d3dpp.Flags                 = 0;
  134.     d3dpp.hDeviceWindow         = window;
  135.     d3dpp.Windowed              = TRUE;
  136.     d3dpp.PresentationInterval  = D3DPRESENT_INTERVAL_IMMEDIATE;
  137.     d3dpp.BackBufferHeight = height;
  138.     d3dpp.BackBufferWidth = width;
  139.   }
  140.  
  141.   int BlitBtoV (POINT *ptDst, POINT *ptSrc, SIZE *szSize)  {
  142.     IDirect3DSurface9 *t;
  143.     D3DSURFACE_DESC lock_desc;
  144.     int rc = -1;
  145.     HRESULT rets = m_Direct3D9_device->GetBackBuffer (0, 0, D3DBACKBUFFER_TYPE_MONO, & t);
  146.     ATLASSERT (SUCCEEDED (rets));
  147.     POINT ptSrcT = { 0, 0};
  148.     POINT *ptPpc = ptSrc ? ptSrc : & ptSrcT;
  149.     // Automatic fit size.
  150.     rets = m_Direct3D9_surface->GetDesc (& lock_desc);
  151.     ATLASSERT (SUCCEEDED (rets));
  152.     SIZE *szPpc = szSize;
  153.     SIZE szSrcT;
  154.     if (szPpc == NULL)  {
  155.       szPpc = & szSrcT;
  156.       szSrcT.cx = lock_desc.Width - ptPpc->x;
  157.       szSrcT.cy = lock_desc.Height - ptPpc->y;
  158.       if (szSrcT.cx <= 0 || (szSrcT.cy <= 0))
  159.         goto  __cleanup;
  160.     } if (1) {
  161.       RECT ac = { ptPpc->x, ptPpc->y, ptPpc->x + szPpc->cx, ptPpc->y + szPpc->cy};
  162.       rets = m_Direct3D9_device->UpdateSurface (m_Direct3D9_surface, & ac, t, ptDst);
  163.       rc = (SUCCEEDED (rets))  ? 0 : -1;
  164.     }
  165. __cleanup:
  166.     rets =  t->Release ();
  167.     ATLASSERT (SUCCEEDED (rets));
  168.     return rc;
  169.   }
  170.  
  171.   int postRender (RECT *ptDst, RECT *ptSrc)  {
  172.     HRESULT rets = m_Direct3D9_device->Present (ptSrc, ptDst, NULL, NULL);
  173.     if (SUCCEEDED (rets))
  174.       return  0;
  175.     else  return -1;
  176.   }
  177.   int postRender (POINT &ptDst, POINT &ptSrc, SIZE &size_)  {
  178.     RECT rcDst = { ptDst.x, ptDst.y, ptDst.x + size_.cx, ptDst.y + size_.cy};
  179.     RECT rcSrc = { ptSrc.x, ptSrc.y, ptSrc.x + size_.cx, ptSrc.y + size_.cy};
  180.     return postRender (& rcDst, & rcSrc);
  181.   }
  182.   // DTOR
  183.  ~CDirectDraw () {
  184.    release_com<IDirect3DSurface9> (m_Direct3D9_surface);
  185.    release_com<IDirect3DDevice9> (m_Direct3D9_device);
  186.   }
  187.   // CTOR
  188.   CDirectDraw (HWND attach, SIZE &VSize, SIZE &BSize) {
  189.     initSettings ();
  190.     // Set the empty brush to avoid repainting the system and cause flickering.
  191.     // SetClassLongPtr (attach, GCLP_HBRBACKGROUND, (LONG_PTR) GetStockObject (NULL_BRUSH)) ;
  192.     m_AttachWindow = attach;
  193.     CreateD3dppSurface (VSize.cx, VSize.cy);
  194.     resetBackSurface (BSize.cx, BSize.cy);
  195.   }
  196.   static int __POW2 (int nc)  {
  197.     int n;
  198.     for  (n= 1; n < nc; n<<=1) {}
  199.     return n;
  200.   }
  201.   // load resourece
  202.   int LoadImage (TCHAR *fname, int width, int height, graph_desc &gd)   {
  203.    {
  204.       IDirect3DTexture9 *tex;
  205.       D3DXIMAGE_INFO infos;
  206.      
  207.       if (FAILED ( D3DXCreateTextureFromFileEx (m_Direct3D9_device, fname,
  208.         D3DX_DEFAULT_NONPOW2,
  209.         D3DX_DEFAULT_NONPOW2,
  210.         D3DX_FROM_FILE,
  211.         0,
  212.         D3DFMT_UNKNOWN,
  213.         D3DPOOL_MANAGED,
  214.         D3DX_DEFAULT, // D3DX_DEFAULT
  215.         D3DX_DEFAULT, // D3DX_DEFAULT
  216.         0, //D3DCOLOR_XRGB (0, 0, 0),  
  217.         & infos, NULL, & tex)) )
  218.         return -1;
  219.       {
  220.         D3DLOCKED_RECT vd;
  221.         HRESULT sig;
  222.         DWORD id2;
  223.         // lock it
  224.         sig = tex->LockRect (0, & vd, NULL, D3DLOCK_READONLY);
  225.         ATLASSERT (SUCCEEDED (sig));
  226.  
  227.         gd.max_width = infos.Width;
  228.         gd.max_height = infos.Height;
  229.         gd.pitch = __POW2 (vd.Pitch); // step 4 * RGB-depth
  230.         gd.__vptr = (DWORD *) _aligned_malloc (gd.pitch *gd.max_height, 64);
  231.  
  232.         for (id2 = 0; id2 != infos.Height; id2++)
  233.           memcpy ( & ( ((char *)gd.__vptr)[id2 * gd.pitch]),
  234.              & ( ((char *)vd.pBits)[id2 * vd.Pitch]) ,
  235.              infos.Width *4);
  236.  
  237.         // unlock., release.
  238.         tex->UnlockRect (0);
  239.         tex->Release ();
  240.         return 0;
  241.       }
  242.       return -1;
  243.     }
  244.   }
  245.   int LoadImage (int resid, int width, int height, graph_desc &gd)   {
  246.    {
  247.       IDirect3DTexture9 *tex;
  248.       D3DXIMAGE_INFO infos;
  249.      
  250.       if (FAILED ( D3DXCreateTextureFromResourceEx (m_Direct3D9_device, NULL,
  251.         MAKEINTRESOURCE (resid),
  252.         D3DX_DEFAULT_NONPOW2,
  253.         D3DX_DEFAULT_NONPOW2,
  254.         D3DX_FROM_FILE,
  255.         0,
  256.         D3DFMT_UNKNOWN,
  257.         D3DPOOL_MANAGED,
  258.         D3DX_DEFAULT, // D3DX_DEFAULT
  259.         D3DX_DEFAULT, // D3DX_DEFAULT
  260.         0, //D3DCOLOR_XRGB (0, 0, 0),  
  261.         & infos, NULL, & tex)) )
  262.         return -1;
  263.       {
  264.         D3DLOCKED_RECT vd;
  265.         HRESULT sig;
  266.         DWORD id2;
  267.         // lock it
  268.         sig = tex->LockRect (0, & vd, NULL, D3DLOCK_READONLY);
  269.         ATLASSERT (SUCCEEDED (sig));
  270.  
  271.         gd.max_width = infos.Width;
  272.         gd.max_height = infos.Height;
  273.         gd.pitch = __POW2 (vd.Pitch); // step 4 * RGB-depth
  274.         gd.__vptr = (DWORD *) _aligned_malloc (gd.pitch *gd.max_height, 64);
  275.  
  276.         for (id2 = 0; id2 != infos.Height; id2++)
  277.           memcpy ( & ( ((char *)gd.__vptr)[id2 * gd.pitch]),
  278.              & ( ((char *)vd.pBits)[id2 * vd.Pitch]) ,
  279.              infos.Width *4);
  280.  
  281.         // unlock., release.
  282.         tex->UnlockRect (0);
  283.         tex->Release ();
  284.         return 0;
  285.       }
  286.       return -1;
  287.     }
  288.   }
  289.   int FreeImage (graph_desc &gd)  {
  290.     if (gd.__vptr != NULL)  {
  291.       _aligned_free (gd.__vptr);
  292.       gd.__vptr = NULL;
  293.     }
  294.     return 0;
  295.   }
  296.   static void CSurfaceBlit (graph_desc &dst, int d_pos_x,  int d_pos_y,
  297.                            graph_desc &src, int s_pos_x,  int s_pos_y,
  298.                                           int w, int h)
  299.   {
  300.     CSurfaceCopy (dst.__vptr, d_pos_x, d_pos_y, dst.pitch,
  301.             src.__vptr, s_pos_x, s_pos_y, src.pitch,
  302.             w, h);
  303.   }
  304.   static void CSurfaceBlit (graph_desc &dst, POINT &ptDst,
  305.                            graph_desc &src, POINT &ptSrc, SIZE &size)
  306.   {
  307.     CSurfaceBlit (dst, ptDst.x, ptDst.y,
  308.          src, ptSrc.x, ptSrc.y, size.cx, size.cy);
  309.   }
  310.   static void CSurfaceSingleAlpha (graph_desc &dst, int x,  int y,
  311.                            int w,  int h,
  312.                            DWORD col, BYTE dst_alpha, BYTE src_alpha)
  313.   {
  314.     CAlphaSingle (dst.__vptr, dst.pitch, x, y, w, h,
  315.             col, dst_alpha, src_alpha);
  316.   }
  317.   static void CSurfaceSingleAlpha (graph_desc &dst, POINT &pt,
  318.                            SIZE &size,
  319.                            DWORD col, BYTE dst_alpha, BYTE src_alpha)
  320.   {
  321.     CSurfaceSingleAlpha (dst, pt.x, pt.y,
  322.          size.cx, size.cy, col, dst_alpha, src_alpha);
  323.   }
  324. };
  325.  
  326. IDirect3D9 *CDirectDraw::sm_Direct3D9_root = nullptr;
  327.  
  328. # endif // !defined(_CDIRECTDRAW_INCLUED_HEADER_CXX_)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement