Advertisement
Guest User

direct2d fail

a guest
Feb 6th, 2013
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.06 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <WindowsX.h>
  3. #include <wincodec.h>
  4. #include <d2d1.h>
  5.  
  6. #pragma comment (lib, "d2d1.lib")
  7.  
  8. LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
  9. HRESULT LoadBitmapFromFile(ID2D1RenderTarget *pRenderTarget, IWICImagingFactory *pIWICFactory, PCWSTR uri, UINT destinationWidth, UINT destinationHeight, ID2D1Bitmap **ppBitmap);
  10. template<class Interface> inline void SafeRelease(Interface **ppInterfaceToRelease);
  11.  
  12. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  13. {
  14.     HWND hWnd;
  15.     WNDCLASSEX wc;
  16.  
  17.     ZeroMemory(&wc, sizeof(WNDCLASSEX));
  18.  
  19.     wc.cbSize = sizeof(WNDCLASSEX);
  20.     wc.style = CS_HREDRAW | CS_VREDRAW;
  21.     wc.lpfnWndProc = WindowProc;
  22.     wc.hInstance = hInstance;
  23.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  24.     wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
  25.     wc.lpszClassName = "BaseDirect2D";
  26.  
  27.     RegisterClassEx(&wc);
  28.  
  29.     hWnd = CreateWindowEx(NULL, "BaseDirect2D","BaseDirect2D", WS_OVERLAPPEDWINDOW, 300, 300, 500, 400, NULL, NULL, hInstance, NULL);
  30.  
  31.  
  32.     //Create a ID2D1Factory
  33.     ID2D1Factory *pD2DFactory = NULL;
  34.     HRESULT hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &pD2DFactory);
  35.  
  36.  
  37.     //Create a WICFactory
  38.     IWICImagingFactory *pWICFactory = NULL;
  39.     if(SUCCEEDED(hr))
  40.     {
  41.         hr = CoCreateInstance(CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pWICFactory));
  42.     }
  43.  
  44.     //Create a ID2D1HwndRenderTarget
  45.     RECT rc;
  46.     GetClientRect(hWnd, &rc);
  47.  
  48.     ID2D1HwndRenderTarget* pRT = NULL;
  49.     hr = pD2DFactory->CreateHwndRenderTarget(D2D1::RenderTargetProperties(), D2D1::HwndRenderTargetProperties(hWnd, D2D1::SizeU(rc.right - rc.left, rc.bottom-rc.top)), &pRT);
  50.  
  51.     //Create a ID2D1Bitmap
  52.     ID2D1Bitmap *myBitmap = NULL;
  53.     hr = LoadBitmapFromFile(pRT, pWICFactory, L".\\biglokeseal_paint.png", 0, 0, &myBitmap);
  54.  
  55.  
  56.     ShowWindow(hWnd, nCmdShow);
  57.  
  58.     MSG msg;
  59.  
  60.     while(TRUE)
  61.     {
  62.         while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  63.         {
  64.             TranslateMessage(&msg);
  65.             DispatchMessage(&msg);
  66.         }
  67.  
  68.         if(msg.message == WM_QUIT) break;
  69.  
  70.         pRT->BeginDraw();
  71.  
  72.         pRT->Clear(D2D1::ColorF(D2D1::ColorF::Black));
  73.  
  74.         pRT->DrawBitmap(myBitmap, D2D1::RectF(0.f, 0.f, 50.f, 50.f), 1.0f, D2D1_BITMAP_INTERPOLATION_MODE_LINEAR, D2D1::RectF(0.f, 0.f, 50.f, 50.f));
  75.  
  76.         hr = pRT->EndDraw();
  77.     }
  78.  
  79.     SafeRelease(&pRT);
  80.     SafeRelease(&pD2DFactory);
  81.  
  82.     return msg.wParam;
  83. }
  84.  
  85.  
  86. HRESULT LoadBitmapFromFile(ID2D1RenderTarget *pRenderTarget, IWICImagingFactory *pIWICFactory, PCWSTR uri, UINT destinationWidth, UINT destinationHeight, ID2D1Bitmap **ppBitmap)
  87. {
  88.     IWICBitmapDecoder *pDecoder = NULL;
  89.     IWICBitmapFrameDecode *pSource = NULL;
  90.     IWICStream *pStream = NULL;
  91.     IWICFormatConverter *pConverter = NULL;
  92.     IWICBitmapScaler *pScaler = NULL;
  93.  
  94.     HRSRC imageResHandle = NULL;
  95.     HGLOBAL imageResDataHandle = NULL;
  96.     void *pImageFile = NULL;
  97.     DWORD imageFileSize = 0;
  98.  
  99.  
  100.     HRESULT hr = pIWICFactory->CreateDecoderFromFilename(uri, NULL, GENERIC_READ, WICDecodeMetadataCacheOnLoad, &pDecoder);
  101.  
  102.  
  103.     if(SUCCEEDED(hr))
  104.     {
  105.         hr = pDecoder->GetFrame(0, &pSource);
  106.     }
  107.  
  108.     if(SUCCEEDED(hr))
  109.     {
  110.         hr = pIWICFactory->CreateFormatConverter(&pConverter);
  111.     }
  112.  
  113.     if(SUCCEEDED(hr))
  114.     {
  115.         hr = pConverter->Initialize(pSource, GUID_WICPixelFormat32bppPBGRA, WICBitmapDitherTypeNone, NULL, 0.f, WICBitmapPaletteTypeMedianCut);
  116.     }
  117.  
  118.     if(SUCCEEDED(hr))
  119.     {
  120.         hr = pRenderTarget->CreateBitmapFromWicBitmap(pConverter, NULL, ppBitmap);
  121.     }
  122.  
  123.     SafeRelease(&pDecoder);
  124.     SafeRelease(&pSource);
  125.     SafeRelease(&pStream);
  126.     SafeRelease(&pConverter);
  127.     SafeRelease(&pScaler);
  128.  
  129.     return hr;
  130. }
  131.  
  132. LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  133. {
  134.     LRESULT result = 0;
  135.  
  136.     switch(message)
  137.     {
  138.         case WM_DESTROY:
  139.             PostQuitMessage(0);
  140.             result = 0;
  141.             break;
  142.         default:
  143.             result = DefWindowProc(hWnd, message, wParam, lParam);
  144.     }
  145.  
  146.     return result;
  147. }
  148.  
  149. template<class Interface> inline void SafeRelease(Interface **ppInterfaceToRelease)
  150. {
  151.     if (*ppInterfaceToRelease != NULL)
  152.     {
  153.         (*ppInterfaceToRelease)->Release();
  154.         (*ppInterfaceToRelease) = NULL;
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement