imgui_impl_dx11.cpp 18 KB

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