imgui_impl_dx11.cpp 18 KB

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