Lib
QOLを高める
graphics.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "util.h"
4 #include <windows.h>
5 #pragma warning(disable: 4005)
6 #include <d3d11.h>
7 #include <memory>
8 #include <unordered_map>
9 
10 namespace yappy {
12 namespace graphics {
13 
14 struct Texture : private util::noncopyable {
17  uint32_t w, h;
18 
19  Texture(RvPtr::pointer pRV_, uint32_t w_, uint32_t h_) :
20  pRV(pRV_), w(w_), h(h_)
21  {}
22  ~Texture() = default;
23 };
24 
25 struct FontTexture : private util::noncopyable {
28  std::vector<TexPtr> pTexList;
29  std::vector<RvPtr> pRVList;
30  uint32_t w, h;
31  uint32_t startChar, endChar;
32 
33  FontTexture(uint32_t w_, uint32_t h_, uint32_t startChar_, uint32_t endChar_) :
34  w(w_), h(h_), startChar(startChar_), endChar(endChar_)
35  {}
36  ~FontTexture() = default;
37 };
38 
39 struct DrawTask {
41  const RvPtr &pRV;
42  uint32_t texW, texH;
43  int dx, dy;
44  bool lrInv, udInv;
45  int sx, sy, sw, sh;
46  int cx, cy;
47  float scaleX, scaleY;
48  float angle;
49  uint32_t fontColor; // ARGB
50  float alpha;
51 
52  DrawTask(const RvPtr &pRV_,
53  uint32_t texW_, uint32_t texH_,
54  int dx_, int dy_, bool lrInv_, bool udInv_,
55  int sx_, int sy_, int sw_, int sh_,
56  int cx_, int cy_, float scaleX_, float scaleY_, float angle_,
57  uint32_t fontColor_, float alpha_) :
58  pRV(pRV_), texW(texW_), texH(texH_),
59  dx(dx_), dy(dy_), lrInv(lrInv_), udInv(udInv_),
60  sx(sx_), sy(sy_), sw(sw_), sh(sh_),
61  cx(cx_), cy(cy_), scaleX(scaleX_), scaleY(scaleY_), angle(angle_),
62  fontColor(fontColor_), alpha(alpha_)
63  {}
64  DrawTask(const DrawTask &) = default;
65  DrawTask &operator=(const DrawTask &) = default;
66  ~DrawTask() = default;
67 };
68 
69 struct GraphicsParam {
70  HWND hWnd = nullptr;
71  int w = 1024;
72  int h = 768;
73  uint32_t refreshRate = 60;
74  bool fullScreen = false;
75  bool vsync = true;
76 };
77 
80 class DGraphics : private util::noncopyable {
81 public:
82  using TextureResource = const Texture;
83  using TextureResourcePtr = std::shared_ptr<TextureResource>;
84  using FontResource = const FontTexture;
85  using FontResourcePtr = std::shared_ptr<FontResource>;
86 
90  static const int SrcSizeDefault = -1;
91 
92  explicit DGraphics(const GraphicsParam &param);
93  ~DGraphics();
94 
95  void render();
96  LRESULT onSize(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
97 
101  TextureResourcePtr loadTexture(const wchar_t *path);
102 
120  void drawTexture(const TextureResourcePtr &texture,
121  int dx, int dy, bool lrInv = false, bool udInv = false,
122  int sx = 0, int sy = 0, int sw = SrcSizeDefault, int sh = SrcSizeDefault,
123  int cx = 0, int cy = 0, float angle = 0.0f,
124  float scaleX = 1.0f, float scaleY = 1.0f, float alpha = 1.0f);
125 
126  FontResourcePtr loadFont(const wchar_t *fontName,
127  uint32_t startChar, uint32_t endChar, uint32_t w, uint32_t h);
128 
129  void drawChar(const FontResourcePtr &font, wchar_t c, int dx, int dy,
130  uint32_t color = 0x000000,
131  float scaleX = 1.0f, float scaleY = 1.0f, float alpha = 1.0f,
132  int *nextx = nullptr, int *nexty = nullptr);
133 
147  void drawString(const FontResourcePtr &font, const wchar_t *str, int dx, int dy,
148  uint32_t color = 0x000000, int ajustX = 0,
149  float scaleX = 1.0f, float scaleY = 1.0f, float alpha = 1.0f,
150  int *nextx = nullptr, int *nexty = nullptr);
151 
152 private:
153  const DXGI_FORMAT BufferFormat = DXGI_FORMAT_R8G8B8A8_UNORM;
154  const DXGI_SWAP_CHAIN_FLAG SwapChainFlag = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
155  const float ClearColor[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
156  const size_t DrawListMax = 1024; // not strict limit
157  const wchar_t * const VS_FileName = L"@VertexShader.cso";
158  const wchar_t * const PS_FileName = L"@PixelShader.cso";
159 
160  GraphicsParam m_param;
161  util::ComPtr<ID3D11Device> m_pDevice;
163  util::ComPtr<IDXGISwapChain> m_pSwapChain;
164  util::ComPtr<ID3D11RenderTargetView> m_pRenderTargetView;
165  util::ComPtr<ID3D11VertexShader> m_pVertexShader;
166  util::ComPtr<ID3D11PixelShader> m_pPixelShader;
167  util::ComPtr<ID3D11InputLayout> m_pInputLayout;
168  util::ComPtr<ID3D11Buffer> m_pVertexBuffer;
169  util::ComPtr<ID3D11Buffer> m_pCBNeverChanges, m_pCBChanges;
170  util::ComPtr<ID3D11RasterizerState> m_pRasterizerState;
171  util::ComPtr<ID3D11SamplerState> m_pSamplerState;
172  util::ComPtr<ID3D11BlendState> m_pBlendState;
173 
174  std::vector<DrawTask> m_drawTaskList;
175 
176  void initializeD3D();
177  void initBackBuffer();
178 };
179 
180 } // namespace graphics
181 } // namespace yappy
Noncopyable class.
Definition: util.h:24
XMMATRIX udInv
Definition: graphics.cpp:36
const char * str
Definition: input.cpp:197
sy
Definition: Memo.txt:67
sw
Definition: Memo.txt:67
util::ComPtr< ID3D11ShaderResourceView > RvPtr
Definition: graphics.h:15
Texture::RvPtr RvPtr
Definition: graphics.h:40
DirectGraphics manager.
Definition: graphics.h:80
util::ComPtr< ID3D11Texture2D > TexPtr
Definition: graphics.h:26
Definition: config.cpp:6
noncopyable & operator=(const noncopyable &)=delete
util::ComPtr< ID3D11ShaderResourceView > RvPtr
Definition: graphics.h:27
Texture(RvPtr::pointer pRV_, uint32_t w_, uint32_t h_)
Definition: graphics.h:19
std::shared_ptr< FontResource > FontResourcePtr
Definition: graphics.h:85
XMMATRIX lrInv
Definition: graphics.cpp:35
Utilities.
FontTexture(uint32_t w_, uint32_t h_, uint32_t startChar_, uint32_t endChar_)
Definition: graphics.h:33
char msg[LINE_DATA_SIZE-sizeof(LARGE_INTEGER)-sizeof(uint32_t)]
Definition: debug.cpp:159
std::shared_ptr< TextureResource > TextureResourcePtr
Definition: graphics.h:83
std::vector< TexPtr > pTexList
Definition: graphics.h:28
std::vector< RvPtr > pRVList
Definition: graphics.h:29
DrawTask(const RvPtr &pRV_, uint32_t texW_, uint32_t texH_, int dx_, int dy_, bool lrInv_, bool udInv_, int sx_, int sy_, int sw_, int sh_, int cx_, int cy_, float scaleX_, float scaleY_, float angle_, uint32_t fontColor_, float alpha_)
Definition: graphics.h:52
const RvPtr & pRV
Definition: graphics.h:41
std::unique_ptr< T, ComDeleter > ComPtr
unique_ptr of IUnknown with ComDeleter.
Definition: util.h:88