imgui_impl_dx11.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. // ImGui Win32 + DirectX11 binding
  2. // https://github.com/ocornut/imgui
  3. #include <imgui.h>
  4. #include "imgui_impl_dx11.h"
  5. // DirectX
  6. #include <d3d11.h>
  7. #include <d3dcompiler.h>
  8. #define DIRECTINPUT_VERSION 0x0800
  9. #include <dinput.h>
  10. // Data
  11. static INT64 g_Time = 0;
  12. static INT64 g_TicksPerSecond = 0;
  13. static HWND g_hWnd = 0;
  14. static ID3D11Device* g_pd3dDevice = NULL;
  15. static ID3D11DeviceContext* g_pd3dDeviceContext = NULL;
  16. static ID3D11Buffer* g_pVB = NULL;
  17. static ID3D11Buffer* g_pIB = NULL;
  18. static ID3D10Blob * g_pVertexShaderBlob = NULL;
  19. static ID3D11VertexShader* g_pVertexShader = NULL;
  20. static ID3D11InputLayout* g_pInputLayout = NULL;
  21. static ID3D11Buffer* g_pVertexConstantBuffer = NULL;
  22. static ID3D10Blob * g_pPixelShaderBlob = NULL;
  23. static ID3D11PixelShader* g_pPixelShader = NULL;
  24. static ID3D11SamplerState* g_pFontSampler = NULL;
  25. static ID3D11ShaderResourceView*g_pFontTextureView = NULL;
  26. static ID3D11RasterizerState* g_pRasterizerState = NULL;
  27. static ID3D11BlendState* g_pBlendState = NULL;
  28. static int VERTEX_BUFFER_SIZE = 30000; // TODO: Make buffers smaller and grow dynamically as needed.
  29. static int INDEX_BUFFER_SIZE = 30000; // TODO: Make buffers smaller and grow dynamically as needed.
  30. struct VERTEX_CONSTANT_BUFFER
  31. {
  32. float mvp[4][4];
  33. };
  34. // This is the main rendering function that you have to implement and provide to ImGui (via setting up 'RenderDrawListsFn' in the ImGuiIO structure)
  35. // If text or lines are blurry when integrating ImGui in your engine:
  36. // - in your Render function, try translating your projection matrix by (0.5f,0.5f) or (0.375f,0.375f)
  37. static void ImGui_ImplDX11_RenderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_count)
  38. {
  39. // Copy and convert all vertices into a single contiguous buffer
  40. D3D11_MAPPED_SUBRESOURCE vtx_resource, idx_resource;
  41. if (g_pd3dDeviceContext->Map(g_pVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &vtx_resource) != S_OK)
  42. return;
  43. if (g_pd3dDeviceContext->Map(g_pIB, 0, D3D11_MAP_WRITE_DISCARD, 0, &idx_resource) != S_OK)
  44. return;
  45. ImDrawVert* vtx_dst = (ImDrawVert*)vtx_resource.pData;
  46. ImDrawIdx* idx_dst = (ImDrawIdx*)idx_resource.pData;
  47. for (int n = 0; n < cmd_lists_count; n++)
  48. {
  49. const ImDrawList* cmd_list = cmd_lists[n];
  50. memcpy(vtx_dst, &cmd_list->vtx_buffer[0], cmd_list->vtx_buffer.size() * sizeof(ImDrawVert));
  51. memcpy(idx_dst, &cmd_list->idx_buffer[0], cmd_list->idx_buffer.size() * sizeof(ImDrawIdx));
  52. vtx_dst += cmd_list->vtx_buffer.size();
  53. idx_dst += cmd_list->idx_buffer.size();
  54. }
  55. g_pd3dDeviceContext->Unmap(g_pVB, 0);
  56. g_pd3dDeviceContext->Unmap(g_pIB, 0);
  57. // Setup orthographic projection matrix into our constant buffer
  58. {
  59. D3D11_MAPPED_SUBRESOURCE mappedResource;
  60. if (g_pd3dDeviceContext->Map(g_pVertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource) != S_OK)
  61. return;
  62. VERTEX_CONSTANT_BUFFER* pConstantBuffer = (VERTEX_CONSTANT_BUFFER*)mappedResource.pData;
  63. const float L = 0.0f;
  64. const float R = ImGui::GetIO().DisplaySize.x;
  65. const float B = ImGui::GetIO().DisplaySize.y;
  66. const float T = 0.0f;
  67. const float mvp[4][4] =
  68. {
  69. { 2.0f/(R-L), 0.0f, 0.0f, 0.0f},
  70. { 0.0f, 2.0f/(T-B), 0.0f, 0.0f,},
  71. { 0.0f, 0.0f, 0.5f, 0.0f },
  72. { (R+L)/(L-R), (T+B)/(B-T), 0.5f, 1.0f },
  73. };
  74. memcpy(&pConstantBuffer->mvp, mvp, sizeof(mvp));
  75. g_pd3dDeviceContext->Unmap(g_pVertexConstantBuffer, 0);
  76. }
  77. // Setup viewport
  78. {
  79. D3D11_VIEWPORT vp;
  80. memset(&vp, 0, sizeof(D3D11_VIEWPORT));
  81. vp.Width = ImGui::GetIO().DisplaySize.x;
  82. vp.Height = ImGui::GetIO().DisplaySize.y;
  83. vp.MinDepth = 0.0f;
  84. vp.MaxDepth = 1.0f;
  85. vp.TopLeftX = 0;
  86. vp.TopLeftY = 0;
  87. g_pd3dDeviceContext->RSSetViewports(1, &vp);
  88. }
  89. // Bind shader and vertex buffers
  90. unsigned int stride = sizeof(ImDrawVert);
  91. unsigned int offset = 0;
  92. g_pd3dDeviceContext->IASetInputLayout(g_pInputLayout);
  93. g_pd3dDeviceContext->IASetVertexBuffers(0, 1, &g_pVB, &stride, &offset);
  94. g_pd3dDeviceContext->IASetIndexBuffer(g_pIB, DXGI_FORMAT_R16_UINT, 0);
  95. g_pd3dDeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
  96. g_pd3dDeviceContext->VSSetShader(g_pVertexShader, NULL, 0);
  97. g_pd3dDeviceContext->VSSetConstantBuffers(0, 1, &g_pVertexConstantBuffer);
  98. g_pd3dDeviceContext->PSSetShader(g_pPixelShader, NULL, 0);
  99. g_pd3dDeviceContext->PSSetSamplers(0, 1, &g_pFontSampler);
  100. // Setup render state
  101. const float blendFactor[4] = { 0.f, 0.f, 0.f, 0.f };
  102. g_pd3dDeviceContext->OMSetBlendState(g_pBlendState, blendFactor, 0xffffffff);
  103. g_pd3dDeviceContext->RSSetState(g_pRasterizerState);
  104. // Render command lists
  105. int vtx_offset = 0;
  106. int idx_offset = 0;
  107. for (int n = 0; n < cmd_lists_count; n++)
  108. {
  109. const ImDrawList* cmd_list = cmd_lists[n];
  110. for (size_t cmd_i = 0; cmd_i < cmd_list->commands.size(); cmd_i++)
  111. {
  112. const ImDrawCmd* pcmd = &cmd_list->commands[cmd_i];
  113. if (pcmd->user_callback)
  114. {
  115. pcmd->user_callback(cmd_list, pcmd);
  116. }
  117. else
  118. {
  119. const D3D11_RECT r = { (LONG)pcmd->clip_rect.x, (LONG)pcmd->clip_rect.y, (LONG)pcmd->clip_rect.z, (LONG)pcmd->clip_rect.w };
  120. g_pd3dDeviceContext->PSSetShaderResources(0, 1, (ID3D11ShaderResourceView**)&pcmd->texture_id);
  121. g_pd3dDeviceContext->RSSetScissorRects(1, &r);
  122. g_pd3dDeviceContext->DrawIndexed(pcmd->idx_count, idx_offset, vtx_offset);
  123. }
  124. idx_offset += pcmd->idx_count;
  125. }
  126. vtx_offset += (int)cmd_list->vtx_buffer.size();
  127. }
  128. // Restore modified state
  129. g_pd3dDeviceContext->IASetInputLayout(NULL);
  130. g_pd3dDeviceContext->PSSetShader(NULL, NULL, 0);
  131. g_pd3dDeviceContext->VSSetShader(NULL, NULL, 0);
  132. }
  133. LRESULT ImGui_ImplDX11_WndProcHandler(HWND, UINT msg, WPARAM wParam, LPARAM lParam)
  134. {
  135. ImGuiIO& io = ImGui::GetIO();
  136. switch (msg)
  137. {
  138. case WM_LBUTTONDOWN:
  139. io.MouseDown[0] = true;
  140. return true;
  141. case WM_LBUTTONUP:
  142. io.MouseDown[0] = false;
  143. return true;
  144. case WM_RBUTTONDOWN:
  145. io.MouseDown[1] = true;
  146. return true;
  147. case WM_RBUTTONUP:
  148. io.MouseDown[1] = false;
  149. return true;
  150. case WM_MOUSEWHEEL:
  151. io.MouseWheel += GET_WHEEL_DELTA_WPARAM(wParam) > 0 ? +1.0f : -1.0f;
  152. return true;
  153. case WM_MOUSEMOVE:
  154. io.MousePos.x = (signed short)(lParam);
  155. io.MousePos.y = (signed short)(lParam >> 16);
  156. return true;
  157. case WM_KEYDOWN:
  158. if (wParam < 256)
  159. io.KeysDown[wParam] = 1;
  160. return true;
  161. case WM_KEYUP:
  162. if (wParam < 256)
  163. io.KeysDown[wParam] = 0;
  164. return true;
  165. case WM_CHAR:
  166. // You can also use ToAscii()+GetKeyboardState() to retrieve characters.
  167. if (wParam > 0 && wParam < 0x10000)
  168. io.AddInputCharacter((unsigned short)wParam);
  169. return true;
  170. }
  171. return 0;
  172. }
  173. static void ImGui_ImplDX11_CreateFontsTexture()
  174. {
  175. ImGuiIO& io = ImGui::GetIO();
  176. // Build
  177. unsigned char* pixels;
  178. int width, height;
  179. io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
  180. // Create DX11 texture
  181. {
  182. D3D11_TEXTURE2D_DESC texDesc;
  183. ZeroMemory(&texDesc, sizeof(texDesc));
  184. texDesc.Width = width;
  185. texDesc.Height = height;
  186. texDesc.MipLevels = 1;
  187. texDesc.ArraySize = 1;
  188. texDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  189. texDesc.SampleDesc.Count = 1;
  190. texDesc.Usage = D3D11_USAGE_DEFAULT;
  191. texDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
  192. texDesc.CPUAccessFlags = 0;
  193. ID3D11Texture2D *pTexture = NULL;
  194. D3D11_SUBRESOURCE_DATA subResource;
  195. subResource.pSysMem = pixels;
  196. subResource.SysMemPitch = texDesc.Width * 4;
  197. subResource.SysMemSlicePitch = 0;
  198. g_pd3dDevice->CreateTexture2D(&texDesc, &subResource, &pTexture);
  199. // Create texture view
  200. D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
  201. ZeroMemory(&srvDesc, sizeof(srvDesc));
  202. srvDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  203. srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
  204. srvDesc.Texture2D.MipLevels = texDesc.MipLevels;
  205. srvDesc.Texture2D.MostDetailedMip = 0;
  206. g_pd3dDevice->CreateShaderResourceView(pTexture, &srvDesc, &g_pFontTextureView);
  207. pTexture->Release();
  208. }
  209. // Store our identifier
  210. io.Fonts->TexID = (void *)g_pFontTextureView;
  211. // Create texture sampler
  212. {
  213. D3D11_SAMPLER_DESC samplerDesc;
  214. ZeroMemory(&samplerDesc, sizeof(samplerDesc));
  215. samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
  216. samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
  217. samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
  218. samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
  219. samplerDesc.MipLODBias = 0.f;
  220. samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
  221. samplerDesc.MinLOD = 0.f;
  222. samplerDesc.MaxLOD = 0.f;
  223. g_pd3dDevice->CreateSamplerState(&samplerDesc, &g_pFontSampler);
  224. }
  225. // Cleanup (don't clear the input data if you want to append new fonts later)
  226. io.Fonts->ClearInputData();
  227. io.Fonts->ClearTexData();
  228. }
  229. bool ImGui_ImplDX11_CreateDeviceObjects()
  230. {
  231. if (!g_pd3dDevice)
  232. return false;
  233. if (g_pVB)
  234. ImGui_ImplDX11_InvalidateDeviceObjects();
  235. // Create the vertex shader
  236. {
  237. static const char* vertexShader =
  238. "cbuffer vertexBuffer : register(c0) \
  239. {\
  240. float4x4 ProjectionMatrix; \
  241. };\
  242. struct VS_INPUT\
  243. {\
  244. float2 pos : POSITION;\
  245. float4 col : COLOR0;\
  246. float2 uv : TEXCOORD0;\
  247. };\
  248. \
  249. struct PS_INPUT\
  250. {\
  251. float4 pos : SV_POSITION;\
  252. float4 col : COLOR0;\
  253. float2 uv : TEXCOORD0;\
  254. };\
  255. \
  256. PS_INPUT main(VS_INPUT input)\
  257. {\
  258. PS_INPUT output;\
  259. output.pos = mul( ProjectionMatrix, float4(input.pos.xy, 0.f, 1.f));\
  260. output.col = input.col;\
  261. output.uv = input.uv;\
  262. return output;\
  263. }";
  264. D3DCompile(vertexShader, strlen(vertexShader), NULL, NULL, NULL, "main", "vs_5_0", 0, 0, &g_pVertexShaderBlob, NULL);
  265. if (g_pVertexShaderBlob == NULL) // NB: Pass ID3D10Blob* pErrorBlob to D3DCompile() to get error showing in (const char*)pErrorBlob->GetBufferPointer(). Make sure to Release() the blob!
  266. return false;
  267. if (g_pd3dDevice->CreateVertexShader((DWORD*)g_pVertexShaderBlob->GetBufferPointer(), g_pVertexShaderBlob->GetBufferSize(), NULL, &g_pVertexShader) != S_OK)
  268. return false;
  269. // Create the input layout
  270. D3D11_INPUT_ELEMENT_DESC localLayout[] = {
  271. { "POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, (size_t)(&((ImDrawVert*)0)->pos), D3D11_INPUT_PER_VERTEX_DATA, 0 },
  272. { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (size_t)(&((ImDrawVert*)0)->uv), D3D11_INPUT_PER_VERTEX_DATA, 0 },
  273. { "COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, (size_t)(&((ImDrawVert*)0)->col), D3D11_INPUT_PER_VERTEX_DATA, 0 },
  274. };
  275. if (g_pd3dDevice->CreateInputLayout(localLayout, 3, g_pVertexShaderBlob->GetBufferPointer(), g_pVertexShaderBlob->GetBufferSize(), &g_pInputLayout) != S_OK)
  276. return false;
  277. // Create the constant buffer
  278. {
  279. D3D11_BUFFER_DESC cbDesc;
  280. cbDesc.ByteWidth = sizeof(VERTEX_CONSTANT_BUFFER);
  281. cbDesc.Usage = D3D11_USAGE_DYNAMIC;
  282. cbDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
  283. cbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
  284. cbDesc.MiscFlags = 0;
  285. g_pd3dDevice->CreateBuffer(&cbDesc, NULL, &g_pVertexConstantBuffer);
  286. }
  287. }
  288. // Create the pixel shader
  289. {
  290. static const char* pixelShader =
  291. "struct PS_INPUT\
  292. {\
  293. float4 pos : SV_POSITION;\
  294. float4 col : COLOR0;\
  295. float2 uv : TEXCOORD0;\
  296. };\
  297. sampler sampler0;\
  298. Texture2D texture0;\
  299. \
  300. float4 main(PS_INPUT input) : SV_Target\
  301. {\
  302. float4 out_col = input.col * texture0.Sample(sampler0, input.uv); \
  303. return out_col; \
  304. }";
  305. D3DCompile(pixelShader, strlen(pixelShader), NULL, NULL, NULL, "main", "ps_5_0", 0, 0, &g_pPixelShaderBlob, NULL);
  306. if (g_pPixelShaderBlob == NULL) // NB: Pass ID3D10Blob* pErrorBlob to D3DCompile() to get error showing in (const char*)pErrorBlob->GetBufferPointer(). Make sure to Release() the blob!
  307. return false;
  308. if (g_pd3dDevice->CreatePixelShader((DWORD*)g_pPixelShaderBlob->GetBufferPointer(), g_pPixelShaderBlob->GetBufferSize(), NULL, &g_pPixelShader) != S_OK)
  309. return false;
  310. }
  311. // Create the blending setup
  312. {
  313. D3D11_BLEND_DESC desc;
  314. ZeroMemory(&desc, sizeof(desc));
  315. desc.AlphaToCoverageEnable = false;
  316. desc.RenderTarget[0].BlendEnable = true;
  317. desc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
  318. desc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
  319. desc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
  320. desc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA;
  321. desc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
  322. desc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
  323. desc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
  324. g_pd3dDevice->CreateBlendState(&desc, &g_pBlendState);
  325. }
  326. // Create the rasterizer state
  327. {
  328. D3D11_RASTERIZER_DESC desc;
  329. ZeroMemory(&desc, sizeof(desc));
  330. desc.FillMode = D3D11_FILL_SOLID;
  331. desc.CullMode = D3D11_CULL_NONE;
  332. desc.ScissorEnable = true;
  333. desc.DepthClipEnable = true;
  334. g_pd3dDevice->CreateRasterizerState(&desc, &g_pRasterizerState);
  335. }
  336. // Create the vertex buffer
  337. {
  338. D3D11_BUFFER_DESC desc;
  339. memset(&desc, 0, sizeof(D3D11_BUFFER_DESC));
  340. desc.Usage = D3D11_USAGE_DYNAMIC;
  341. desc.ByteWidth = VERTEX_BUFFER_SIZE * sizeof(ImDrawVert);
  342. desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
  343. desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
  344. desc.MiscFlags = 0;
  345. if (g_pd3dDevice->CreateBuffer(&desc, NULL, &g_pVB) < 0)
  346. return false;
  347. }
  348. // Create the index buffer
  349. {
  350. D3D11_BUFFER_DESC bufferDesc;
  351. memset(&bufferDesc, 0, sizeof(D3D11_BUFFER_DESC));
  352. bufferDesc.Usage = D3D11_USAGE_DYNAMIC;
  353. bufferDesc.ByteWidth = INDEX_BUFFER_SIZE * sizeof(ImDrawIdx);
  354. bufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
  355. bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
  356. if (g_pd3dDevice->CreateBuffer(&bufferDesc, NULL, &g_pIB) < 0)
  357. return false;
  358. }
  359. ImGui_ImplDX11_CreateFontsTexture();
  360. return true;
  361. }
  362. void ImGui_ImplDX11_InvalidateDeviceObjects()
  363. {
  364. if (!g_pd3dDevice)
  365. return;
  366. if (g_pFontSampler) { g_pFontSampler->Release(); g_pFontSampler = NULL; }
  367. if (g_pFontTextureView) { g_pFontTextureView->Release(); ImGui::GetIO().Fonts->TexID = 0; }
  368. if (g_pIB) { g_pIB->Release(); g_pIB = NULL; }
  369. if (g_pVB) { g_pVB->Release(); g_pVB = NULL; }
  370. if (g_pBlendState) { g_pBlendState->Release(); g_pBlendState = NULL; }
  371. if (g_pRasterizerState) { g_pRasterizerState->Release(); g_pRasterizerState = NULL; }
  372. if (g_pPixelShader) { g_pPixelShader->Release(); g_pPixelShader = NULL; }
  373. if (g_pPixelShaderBlob) { g_pPixelShaderBlob->Release(); g_pPixelShaderBlob = NULL; }
  374. if (g_pVertexConstantBuffer) { g_pVertexConstantBuffer->Release(); g_pVertexConstantBuffer = NULL; }
  375. if (g_pInputLayout) { g_pInputLayout->Release(); g_pInputLayout = NULL; }
  376. if (g_pVertexShader) { g_pVertexShader->Release(); g_pVertexShader = NULL; }
  377. if (g_pVertexShaderBlob) { g_pVertexShaderBlob->Release(); g_pVertexShaderBlob = NULL; }
  378. }
  379. bool ImGui_ImplDX11_Init(void* hwnd, ID3D11Device* device, ID3D11DeviceContext* device_context)
  380. {
  381. g_hWnd = (HWND)hwnd;
  382. g_pd3dDevice = device;
  383. g_pd3dDeviceContext = device_context;
  384. if (!QueryPerformanceFrequency((LARGE_INTEGER *)&g_TicksPerSecond))
  385. return false;
  386. if (!QueryPerformanceCounter((LARGE_INTEGER *)&g_Time))
  387. return false;
  388. ImGuiIO& io = ImGui::GetIO();
  389. io.KeyMap[ImGuiKey_Tab] = VK_TAB; // Keyboard mapping. ImGui will use those indices to peek into the io.KeyDown[] array that we will update during the application lifetime.
  390. io.KeyMap[ImGuiKey_LeftArrow] = VK_LEFT;
  391. io.KeyMap[ImGuiKey_RightArrow] = VK_RIGHT;
  392. io.KeyMap[ImGuiKey_UpArrow] = VK_UP;
  393. io.KeyMap[ImGuiKey_DownArrow] = VK_DOWN;
  394. io.KeyMap[ImGuiKey_PageUp] = VK_PRIOR;
  395. io.KeyMap[ImGuiKey_PageDown] = VK_NEXT;
  396. io.KeyMap[ImGuiKey_Home] = VK_HOME;
  397. io.KeyMap[ImGuiKey_End] = VK_END;
  398. io.KeyMap[ImGuiKey_Delete] = VK_DELETE;
  399. io.KeyMap[ImGuiKey_Backspace] = VK_BACK;
  400. io.KeyMap[ImGuiKey_Enter] = VK_RETURN;
  401. io.KeyMap[ImGuiKey_Escape] = VK_ESCAPE;
  402. io.KeyMap[ImGuiKey_A] = 'A';
  403. io.KeyMap[ImGuiKey_C] = 'C';
  404. io.KeyMap[ImGuiKey_V] = 'V';
  405. io.KeyMap[ImGuiKey_X] = 'X';
  406. io.KeyMap[ImGuiKey_Y] = 'Y';
  407. io.KeyMap[ImGuiKey_Z] = 'Z';
  408. io.RenderDrawListsFn = ImGui_ImplDX11_RenderDrawLists;
  409. io.ImeWindowHandle = g_hWnd;
  410. return true;
  411. }
  412. void ImGui_ImplDX11_Shutdown()
  413. {
  414. ImGui_ImplDX11_InvalidateDeviceObjects();
  415. ImGui::Shutdown();
  416. g_pd3dDevice = NULL;
  417. g_pd3dDeviceContext = NULL;
  418. g_hWnd = (HWND)0;
  419. }
  420. void ImGui_ImplDX11_NewFrame()
  421. {
  422. if (!g_pVB)
  423. ImGui_ImplDX11_CreateDeviceObjects();
  424. ImGuiIO& io = ImGui::GetIO();
  425. // Setup display size (every frame to accommodate for window resizing)
  426. RECT rect;
  427. GetClientRect(g_hWnd, &rect);
  428. io.DisplaySize = ImVec2((float)(rect.right - rect.left), (float)(rect.bottom - rect.top));
  429. // Setup time step
  430. INT64 current_time;
  431. QueryPerformanceCounter((LARGE_INTEGER *)&current_time);
  432. io.DeltaTime = (float)(current_time - g_Time) / g_TicksPerSecond;
  433. g_Time = current_time;
  434. // Read keyboard modifiers inputs
  435. io.KeyCtrl = (GetKeyState(VK_CONTROL) & 0x8000) != 0;
  436. io.KeyShift = (GetKeyState(VK_SHIFT) & 0x8000) != 0;
  437. io.KeyAlt = (GetKeyState(VK_MENU) & 0x8000) != 0;
  438. // io.KeysDown : filled by WM_KEYDOWN/WM_KEYUP events
  439. // io.MousePos : filled by WM_MOUSEMOVE events
  440. // io.MouseDown : filled by WM_*BUTTON* events
  441. // io.MouseWheel : filled by WM_MOUSEWHEEL events
  442. // Hide OS mouse cursor if ImGui is drawing it
  443. SetCursor(io.MouseDrawCursor ? NULL : LoadCursor(NULL, IDC_ARROW));
  444. // Start the frame
  445. ImGui::NewFrame();
  446. }