d2d_window.h
1 #pragma once 2 #include <winrt/base.h> 3 #include <Windows.h> 4 #include <dxgi1_3.h> 5 #include <d3d11_2.h> 6 #include <d2d1_3.h> 7 #include <d2d1_3helper.h> 8 #include <d2d1helper.h> 9 #include <dcomp.h> 10 #include <dwmapi.h> 11 #include <string> 12 #include "d2d_svg.h" 13 14 #include <functional> 15 #include <optional> 16 17 class D2DWindow 18 { 19 public: 20 D2DWindow(); 21 void show(UINT x, UINT y, UINT width, UINT height); 22 void hide(); 23 void initialize(); 24 virtual ~D2DWindow(); 25 26 protected: 27 // Implement this: 28 29 // Initialization - called when D2D device needs to be created. 30 // When called all D2DWindow members will be initialized, including d2d_dc 31 virtual void init() = 0; 32 // resize - when called, window_width and window_height will have current window size 33 virtual void resize() = 0; 34 // render - called on WM_PAINT, BeginPaint/EndPaint is handled by D2DWindow 35 virtual void render(ID2D1DeviceContext5* d2d_dc) = 0; 36 // on_show, on_hide - called when the window is about to be shown or about to be hidden 37 virtual void on_show() = 0; 38 virtual void on_hide() = 0; 39 40 static LRESULT __stdcall d2d_window_proc(HWND window, UINT message, WPARAM wparam, LPARAM lparam); 41 static D2DWindow* this_from_hwnd(HWND window); 42 43 void base_init(); 44 void base_resize(UINT width, UINT height); 45 void base_render(); 46 void render_empty(); 47 48 std::recursive_mutex mutex; 49 bool hidden = true; 50 bool initialized = false; 51 HWND hwnd; 52 UINT window_width{}; 53 UINT window_height{}; 54 winrt::com_ptr<ID3D11Device> d3d_device; 55 winrt::com_ptr<IDXGIDevice> dxgi_device; 56 winrt::com_ptr<IDXGIFactory2> dxgi_factory; 57 winrt::com_ptr<IDXGISwapChain1> dxgi_swap_chain; 58 winrt::com_ptr<IDCompositionDevice> composition_device; 59 winrt::com_ptr<IDCompositionTarget> composition_target; 60 winrt::com_ptr<IDCompositionVisual> composition_visual; 61 winrt::com_ptr<IDXGISurface2> dxgi_surface; 62 winrt::com_ptr<ID2D1Bitmap1> d2d_bitmap; 63 winrt::com_ptr<ID2D1Factory6> d2d_factory; 64 winrt::com_ptr<ID2D1Device5> d2d_device; 65 winrt::com_ptr<ID2D1DeviceContext5> d2d_dc; 66 };