imgui_impl_dx10.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. // ImGui Win32 + DirectX10 binding
  2. // In this binding, ImTextureID is used to store a 'ID3D10ShaderResourceView*' texture identifier. Read the FAQ about ImTextureID in imgui.cpp.
  3. // You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
  4. // If you use this binding you'll need to call 4 functions: ImGui_ImplXXXX_Init(), ImGui_ImplXXXX_NewFrame(), ImGui::Render() and ImGui_ImplXXXX_Shutdown().
  5. // If you are new to ImGui, see examples/README.txt and documentation at the top of imgui.cpp.
  6. // https://github.com/ocornut/imgui
  7. #include "imgui.h"
  8. #include "imgui_impl_dx10.h"
  9. // DirectX
  10. #include <d3d10_1.h>
  11. #include <d3d10.h>
  12. #include <d3dcompiler.h>
  13. #define DIRECTINPUT_VERSION 0x0800
  14. #include <dinput.h>
  15. // Data
  16. static INT64 g_Time = 0;
  17. static INT64 g_TicksPerSecond = 0;
  18. static HWND g_hWnd = 0;
  19. static ID3D10Device* g_pd3dDevice = NULL;
  20. static ID3D10Buffer* g_pVB = NULL;
  21. static ID3D10Buffer* g_pIB = NULL;
  22. static ID3D10Blob * g_pVertexShaderBlob = NULL;
  23. static ID3D10VertexShader* g_pVertexShader = NULL;
  24. static ID3D10InputLayout* g_pInputLayout = NULL;
  25. static ID3D10Buffer* g_pVertexConstantBuffer = NULL;
  26. static ID3D10Blob * g_pPixelShaderBlob = NULL;
  27. static ID3D10PixelShader* g_pPixelShader = NULL;
  28. static ID3D10SamplerState* g_pFontSampler = NULL;
  29. static ID3D10ShaderResourceView*g_pFontTextureView = NULL;
  30. static ID3D10RasterizerState* g_pRasterizerState = NULL;
  31. static ID3D10BlendState* g_pBlendState = NULL;
  32. static int g_VertexBufferSize = 5000, g_IndexBufferSize = 10000;
  33. struct VERTEX_CONSTANT_BUFFER
  34. {
  35. float mvp[4][4];
  36. };
  37. // This is the main rendering function that you have to implement and provide to ImGui (via setting up 'RenderDrawListsFn' in the ImGuiIO structure)
  38. // If text or lines are blurry when integrating ImGui in your engine:
  39. // - in your Render function, try translating your projection matrix by (0.5f,0.5f) or (0.375f,0.375f)
  40. void ImGui_ImplDX10_RenderDrawLists(ImDrawData* draw_data)
  41. {
  42. // Create and grow vertex/index buffers if needed
  43. if (!g_pVB || g_VertexBufferSize < draw_data->TotalVtxCount)
  44. {
  45. if (g_pVB) { g_pVB->Release(); g_pVB = NULL; }
  46. g_VertexBufferSize = draw_data->TotalVtxCount + 5000;
  47. D3D10_BUFFER_DESC desc;
  48. memset(&desc, 0, sizeof(D3D10_BUFFER_DESC));
  49. desc.Usage = D3D10_USAGE_DYNAMIC;
  50. desc.ByteWidth = g_VertexBufferSize * sizeof(ImDrawVert);
  51. desc.BindFlags = D3D10_BIND_VERTEX_BUFFER;
  52. desc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
  53. desc.MiscFlags = 0;
  54. if (g_pd3dDevice->CreateBuffer(&desc, NULL, &g_pVB) < 0)
  55. return;
  56. }
  57. if (!g_pIB || g_IndexBufferSize < draw_data->TotalIdxCount)
  58. {
  59. if (g_pIB) { g_pIB->Release(); g_pIB = NULL; }
  60. g_IndexBufferSize = draw_data->TotalIdxCount + 10000;
  61. D3D10_BUFFER_DESC bufferDesc;
  62. memset(&bufferDesc, 0, sizeof(D3D10_BUFFER_DESC));
  63. bufferDesc.Usage = D3D10_USAGE_DYNAMIC;
  64. bufferDesc.ByteWidth = g_IndexBufferSize * sizeof(ImDrawIdx);
  65. bufferDesc.BindFlags = D3D10_BIND_INDEX_BUFFER;
  66. bufferDesc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
  67. if (g_pd3dDevice->CreateBuffer(&bufferDesc, NULL, &g_pIB) < 0)
  68. return;
  69. }
  70. // Copy and convert all vertices into a single contiguous buffer
  71. ImDrawVert* vtx_dst = NULL;
  72. ImDrawIdx* idx_dst = NULL;
  73. g_pVB->Map(D3D10_MAP_WRITE_DISCARD, 0, (void**)&vtx_dst);
  74. g_pIB->Map(D3D10_MAP_WRITE_DISCARD, 0, (void**)&idx_dst);
  75. for (int n = 0; n < draw_data->CmdListsCount; n++)
  76. {
  77. const ImDrawList* cmd_list = draw_data->CmdLists[n];
  78. memcpy(vtx_dst, &cmd_list->VtxBuffer[0], cmd_list->VtxBuffer.size() * sizeof(ImDrawVert));
  79. memcpy(idx_dst, &cmd_list->IdxBuffer[0], cmd_list->IdxBuffer.size() * sizeof(ImDrawIdx));
  80. vtx_dst += cmd_list->VtxBuffer.size();
  81. idx_dst += cmd_list->IdxBuffer.size();
  82. }
  83. g_pVB->Unmap();
  84. g_pIB->Unmap();
  85. // Setup orthographic projection matrix into our constant buffer
  86. {
  87. void* mappedResource;
  88. if (g_pVertexConstantBuffer->Map(D3D10_MAP_WRITE_DISCARD, 0, &mappedResource) != S_OK)
  89. return;
  90. VERTEX_CONSTANT_BUFFER* pConstantBuffer = (VERTEX_CONSTANT_BUFFER*)mappedResource;
  91. const float L = 0.0f;
  92. const float R = ImGui::GetIO().DisplaySize.x;
  93. const float B = ImGui::GetIO().DisplaySize.y;
  94. const float T = 0.0f;
  95. const float mvp[4][4] =
  96. {
  97. { 2.0f/(R-L), 0.0f, 0.0f, 0.0f },
  98. { 0.0f, 2.0f/(T-B), 0.0f, 0.0f },
  99. { 0.0f, 0.0f, 0.5f, 0.0f },
  100. { (R+L)/(L-R), (T+B)/(B-T), 0.5f, 1.0f },
  101. };
  102. memcpy(&pConstantBuffer->mvp, mvp, sizeof(mvp));
  103. g_pVertexConstantBuffer->Unmap();
  104. }
  105. // Setup viewport
  106. {
  107. D3D10_VIEWPORT vp;
  108. memset(&vp, 0, sizeof(D3D10_VIEWPORT));
  109. vp.Width = (UINT)ImGui::GetIO().DisplaySize.x;
  110. vp.Height = (UINT)ImGui::GetIO().DisplaySize.y;
  111. vp.MinDepth = 0.0f;
  112. vp.MaxDepth = 1.0f;
  113. vp.TopLeftX = 0;
  114. vp.TopLeftY = 0;
  115. g_pd3dDevice->RSSetViewports(1, &vp);
  116. }
  117. // Bind shader and vertex buffers
  118. unsigned int stride = sizeof(ImDrawVert);
  119. unsigned int offset = 0;
  120. g_pd3dDevice->IASetInputLayout(g_pInputLayout);
  121. g_pd3dDevice->IASetVertexBuffers(0, 1, &g_pVB, &stride, &offset);
  122. g_pd3dDevice->IASetIndexBuffer(g_pIB, sizeof(ImDrawIdx) == 2 ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_R32_UINT, 0);
  123. g_pd3dDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
  124. g_pd3dDevice->VSSetShader(g_pVertexShader);
  125. g_pd3dDevice->VSSetConstantBuffers(0, 1, &g_pVertexConstantBuffer);
  126. g_pd3dDevice->PSSetShader(g_pPixelShader);
  127. g_pd3dDevice->PSSetSamplers(0, 1, &g_pFontSampler);
  128. // Setup render state
  129. const float blendFactor[4] = { 0.f, 0.f, 0.f, 0.f };
  130. g_pd3dDevice->OMSetBlendState(g_pBlendState, blendFactor, 0xffffffff);
  131. g_pd3dDevice->RSSetState(g_pRasterizerState);
  132. // Render command lists
  133. int vtx_offset = 0;
  134. int idx_offset = 0;
  135. for (int n = 0; n < draw_data->CmdListsCount; n++)
  136. {
  137. const ImDrawList* cmd_list = draw_data->CmdLists[n];
  138. for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.size(); cmd_i++)
  139. {
  140. const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
  141. if (pcmd->UserCallback)
  142. {
  143. pcmd->UserCallback(cmd_list, pcmd);
  144. }
  145. else
  146. {
  147. const D3D10_RECT r = { (LONG)pcmd->ClipRect.x, (LONG)pcmd->ClipRect.y, (LONG)pcmd->ClipRect.z, (LONG)pcmd->ClipRect.w };
  148. g_pd3dDevice->PSSetShaderResources(0, 1, (ID3D10ShaderResourceView**)&pcmd->TextureId);
  149. g_pd3dDevice->RSSetScissorRects(1, &r);
  150. g_pd3dDevice->DrawIndexed(pcmd->ElemCount, idx_offset, vtx_offset);
  151. }
  152. idx_offset += pcmd->ElemCount;
  153. }
  154. vtx_offset += cmd_list->VtxBuffer.size();
  155. }
  156. // Restore modified state
  157. g_pd3dDevice->IASetInputLayout(NULL);
  158. g_pd3dDevice->PSSetShader(NULL);
  159. g_pd3dDevice->VSSetShader(NULL);
  160. }
  161. IMGUI_API LRESULT ImGui_ImplDX10_WndProcHandler(HWND, UINT msg, WPARAM wParam, LPARAM lParam)
  162. {
  163. ImGuiIO& io = ImGui::GetIO();
  164. switch (msg)
  165. {
  166. case WM_LBUTTONDOWN:
  167. io.MouseDown[0] = true;
  168. return true;
  169. case WM_LBUTTONUP:
  170. io.MouseDown[0] = false;
  171. return true;
  172. case WM_RBUTTONDOWN:
  173. io.MouseDown[1] = true;
  174. return true;
  175. case WM_RBUTTONUP:
  176. io.MouseDown[1] = false;
  177. return true;
  178. case WM_MBUTTONDOWN:
  179. io.MouseDown[2] = true;
  180. return true;
  181. case WM_MBUTTONUP:
  182. io.MouseDown[2] = false;
  183. return true;
  184. case WM_MOUSEWHEEL:
  185. io.MouseWheel += GET_WHEEL_DELTA_WPARAM(wParam) > 0 ? +1.0f : -1.0f;
  186. return true;
  187. case WM_MOUSEMOVE:
  188. io.MousePos.x = (signed short)(lParam);
  189. io.MousePos.y = (signed short)(lParam >> 16);
  190. return true;
  191. case WM_KEYDOWN:
  192. if (wParam < 256)
  193. io.KeysDown[wParam] = 1;
  194. return true;
  195. case WM_KEYUP:
  196. if (wParam < 256)
  197. io.KeysDown[wParam] = 0;
  198. return true;
  199. case WM_CHAR:
  200. // You can also use ToAscii()+GetKeyboardState() to retrieve characters.
  201. if (wParam > 0 && wParam < 0x10000)
  202. io.AddInputCharacter((unsigned short)wParam);
  203. return true;
  204. }
  205. return 0;
  206. }
  207. static void ImGui_ImplDX10_CreateFontsTexture()
  208. {
  209. ImGuiIO& io = ImGui::GetIO();
  210. // Build
  211. unsigned char* pixels;
  212. int width, height;
  213. io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
  214. // Create DX10 texture
  215. {
  216. D3D10_TEXTURE2D_DESC texDesc;
  217. ZeroMemory(&texDesc, sizeof(texDesc));
  218. texDesc.Width = width;
  219. texDesc.Height = height;
  220. texDesc.MipLevels = 1;
  221. texDesc.ArraySize = 1;
  222. texDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  223. texDesc.SampleDesc.Count = 1;
  224. texDesc.Usage = D3D10_USAGE_DEFAULT;
  225. texDesc.BindFlags = D3D10_BIND_SHADER_RESOURCE;
  226. texDesc.CPUAccessFlags = 0;
  227. ID3D10Texture2D *pTexture = NULL;
  228. D3D10_SUBRESOURCE_DATA subResource;
  229. subResource.pSysMem = pixels;
  230. subResource.SysMemPitch = texDesc.Width * 4;
  231. subResource.SysMemSlicePitch = 0;
  232. g_pd3dDevice->CreateTexture2D(&texDesc, &subResource, &pTexture);
  233. // Create texture view
  234. D3D10_SHADER_RESOURCE_VIEW_DESC srvDesc;
  235. ZeroMemory(&srvDesc, sizeof(srvDesc));
  236. srvDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  237. srvDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D;
  238. srvDesc.Texture2D.MipLevels = texDesc.MipLevels;
  239. srvDesc.Texture2D.MostDetailedMip = 0;
  240. g_pd3dDevice->CreateShaderResourceView(pTexture, &srvDesc, &g_pFontTextureView);
  241. pTexture->Release();
  242. }
  243. // Store our identifier
  244. io.Fonts->TexID = (void *)g_pFontTextureView;
  245. // Create texture sampler
  246. {
  247. D3D10_SAMPLER_DESC samplerDesc;
  248. ZeroMemory(&samplerDesc, sizeof(samplerDesc));
  249. samplerDesc.Filter = D3D10_FILTER_MIN_MAG_MIP_LINEAR;
  250. samplerDesc.AddressU = D3D10_TEXTURE_ADDRESS_WRAP;
  251. samplerDesc.AddressV = D3D10_TEXTURE_ADDRESS_WRAP;
  252. samplerDesc.AddressW = D3D10_TEXTURE_ADDRESS_WRAP;
  253. samplerDesc.MipLODBias = 0.f;
  254. samplerDesc.ComparisonFunc = D3D10_COMPARISON_ALWAYS;
  255. samplerDesc.MinLOD = 0.f;
  256. samplerDesc.MaxLOD = 0.f;
  257. g_pd3dDevice->CreateSamplerState(&samplerDesc, &g_pFontSampler);
  258. }
  259. // Cleanup (don't clear the input data if you want to append new fonts later)
  260. io.Fonts->ClearInputData();
  261. io.Fonts->ClearTexData();
  262. }
  263. bool ImGui_ImplDX10_CreateDeviceObjects()
  264. {
  265. if (!g_pd3dDevice)
  266. return false;
  267. if (g_pFontSampler)
  268. ImGui_ImplDX10_InvalidateDeviceObjects();
  269. // Create the vertex shader
  270. {
  271. static const char* vertexShader =
  272. "cbuffer vertexBuffer : register(b0) \
  273. {\
  274. float4x4 ProjectionMatrix; \
  275. };\
  276. struct VS_INPUT\
  277. {\
  278. float2 pos : POSITION;\
  279. float4 col : COLOR0;\
  280. float2 uv : TEXCOORD0;\
  281. };\
  282. \
  283. struct PS_INPUT\
  284. {\
  285. float4 pos : SV_POSITION;\
  286. float4 col : COLOR0;\
  287. float2 uv : TEXCOORD0;\
  288. };\
  289. \
  290. PS_INPUT main(VS_INPUT input)\
  291. {\
  292. PS_INPUT output;\
  293. output.pos = mul( ProjectionMatrix, float4(input.pos.xy, 0.f, 1.f));\
  294. output.col = input.col;\
  295. output.uv = input.uv;\
  296. return output;\
  297. }";
  298. D3DCompile(vertexShader, strlen(vertexShader), NULL, NULL, NULL, "main", "vs_4_0", 0, 0, &g_pVertexShaderBlob, NULL);
  299. 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!
  300. return false;
  301. if (g_pd3dDevice->CreateVertexShader((DWORD*)g_pVertexShaderBlob->GetBufferPointer(), g_pVertexShaderBlob->GetBufferSize(), &g_pVertexShader) != S_OK)
  302. return false;
  303. // Create the input layout
  304. D3D10_INPUT_ELEMENT_DESC localLayout[] = {
  305. { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (size_t)(&((ImDrawVert*)0)->pos), D3D10_INPUT_PER_VERTEX_DATA, 0 },
  306. { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (size_t)(&((ImDrawVert*)0)->uv), D3D10_INPUT_PER_VERTEX_DATA, 0 },
  307. { "COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, (size_t)(&((ImDrawVert*)0)->col), D3D10_INPUT_PER_VERTEX_DATA, 0 },
  308. };
  309. if (g_pd3dDevice->CreateInputLayout(localLayout, 3, g_pVertexShaderBlob->GetBufferPointer(), g_pVertexShaderBlob->GetBufferSize(), &g_pInputLayout) != S_OK)
  310. return false;
  311. // Create the constant buffer
  312. {
  313. D3D10_BUFFER_DESC cbDesc;
  314. cbDesc.ByteWidth = sizeof(VERTEX_CONSTANT_BUFFER);
  315. cbDesc.Usage = D3D10_USAGE_DYNAMIC;
  316. cbDesc.BindFlags = D3D10_BIND_CONSTANT_BUFFER;
  317. cbDesc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
  318. cbDesc.MiscFlags = 0;
  319. g_pd3dDevice->CreateBuffer(&cbDesc, NULL, &g_pVertexConstantBuffer);
  320. }
  321. }
  322. // Create the pixel shader
  323. {
  324. static const char* pixelShader =
  325. "struct PS_INPUT\
  326. {\
  327. float4 pos : SV_POSITION;\
  328. float4 col : COLOR0;\
  329. float2 uv : TEXCOORD0;\
  330. };\
  331. sampler sampler0;\
  332. Texture2D texture0;\
  333. \
  334. float4 main(PS_INPUT input) : SV_Target\
  335. {\
  336. float4 out_col = input.col * texture0.Sample(sampler0, input.uv); \
  337. return out_col; \
  338. }";
  339. D3DCompile(pixelShader, strlen(pixelShader), NULL, NULL, NULL, "main", "ps_4_0", 0, 0, &g_pPixelShaderBlob, NULL);
  340. 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!
  341. return false;
  342. if (g_pd3dDevice->CreatePixelShader((DWORD*)g_pPixelShaderBlob->GetBufferPointer(), g_pPixelShaderBlob->GetBufferSize(), &g_pPixelShader) != S_OK)
  343. return false;
  344. }
  345. // Create the blending setup
  346. {
  347. D3D10_BLEND_DESC desc;
  348. ZeroMemory(&desc, sizeof(desc));
  349. desc.AlphaToCoverageEnable = false;
  350. desc.BlendEnable[0] = true;
  351. desc.SrcBlend = D3D10_BLEND_SRC_ALPHA;
  352. desc.DestBlend = D3D10_BLEND_INV_SRC_ALPHA;
  353. desc.BlendOp = D3D10_BLEND_OP_ADD;
  354. desc.SrcBlendAlpha = D3D10_BLEND_INV_SRC_ALPHA;
  355. desc.DestBlendAlpha = D3D10_BLEND_ZERO;
  356. desc.BlendOpAlpha = D3D10_BLEND_OP_ADD;
  357. desc.RenderTargetWriteMask[0] = D3D10_COLOR_WRITE_ENABLE_ALL;
  358. g_pd3dDevice->CreateBlendState(&desc, &g_pBlendState);
  359. }
  360. // Create the rasterizer state
  361. {
  362. D3D10_RASTERIZER_DESC desc;
  363. ZeroMemory(&desc, sizeof(desc));
  364. desc.FillMode = D3D10_FILL_SOLID;
  365. desc.CullMode = D3D10_CULL_NONE;
  366. desc.ScissorEnable = true;
  367. desc.DepthClipEnable = true;
  368. g_pd3dDevice->CreateRasterizerState(&desc, &g_pRasterizerState);
  369. }
  370. ImGui_ImplDX10_CreateFontsTexture();
  371. return true;
  372. }
  373. void ImGui_ImplDX10_InvalidateDeviceObjects()
  374. {
  375. if (!g_pd3dDevice)
  376. return;
  377. if (g_pFontSampler) { g_pFontSampler->Release(); g_pFontSampler = NULL; }
  378. if (g_pFontTextureView) { g_pFontTextureView->Release(); g_pFontTextureView = NULL; ImGui::GetIO().Fonts->TexID = 0; }
  379. if (g_pIB) { g_pIB->Release(); g_pIB = NULL; }
  380. if (g_pVB) { g_pVB->Release(); g_pVB = NULL; }
  381. if (g_pBlendState) { g_pBlendState->Release(); g_pBlendState = NULL; }
  382. if (g_pRasterizerState) { g_pRasterizerState->Release(); g_pRasterizerState = NULL; }
  383. if (g_pPixelShader) { g_pPixelShader->Release(); g_pPixelShader = NULL; }
  384. if (g_pPixelShaderBlob) { g_pPixelShaderBlob->Release(); g_pPixelShaderBlob = NULL; }
  385. if (g_pVertexConstantBuffer) { g_pVertexConstantBuffer->Release(); g_pVertexConstantBuffer = NULL; }
  386. if (g_pInputLayout) { g_pInputLayout->Release(); g_pInputLayout = NULL; }
  387. if (g_pVertexShader) { g_pVertexShader->Release(); g_pVertexShader = NULL; }
  388. if (g_pVertexShaderBlob) { g_pVertexShaderBlob->Release(); g_pVertexShaderBlob = NULL; }
  389. }
  390. bool ImGui_ImplDX10_Init(void* hwnd, ID3D10Device* device)
  391. {
  392. g_hWnd = (HWND)hwnd;
  393. g_pd3dDevice = device;
  394. if (!QueryPerformanceFrequency((LARGE_INTEGER *)&g_TicksPerSecond))
  395. return false;
  396. if (!QueryPerformanceCounter((LARGE_INTEGER *)&g_Time))
  397. return false;
  398. ImGuiIO& io = ImGui::GetIO();
  399. 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.
  400. io.KeyMap[ImGuiKey_LeftArrow] = VK_LEFT;
  401. io.KeyMap[ImGuiKey_RightArrow] = VK_RIGHT;
  402. io.KeyMap[ImGuiKey_UpArrow] = VK_UP;
  403. io.KeyMap[ImGuiKey_DownArrow] = VK_DOWN;
  404. io.KeyMap[ImGuiKey_PageUp] = VK_PRIOR;
  405. io.KeyMap[ImGuiKey_PageDown] = VK_NEXT;
  406. io.KeyMap[ImGuiKey_Home] = VK_HOME;
  407. io.KeyMap[ImGuiKey_End] = VK_END;
  408. io.KeyMap[ImGuiKey_Delete] = VK_DELETE;
  409. io.KeyMap[ImGuiKey_Backspace] = VK_BACK;
  410. io.KeyMap[ImGuiKey_Enter] = VK_RETURN;
  411. io.KeyMap[ImGuiKey_Escape] = VK_ESCAPE;
  412. io.KeyMap[ImGuiKey_A] = 'A';
  413. io.KeyMap[ImGuiKey_C] = 'C';
  414. io.KeyMap[ImGuiKey_V] = 'V';
  415. io.KeyMap[ImGuiKey_X] = 'X';
  416. io.KeyMap[ImGuiKey_Y] = 'Y';
  417. io.KeyMap[ImGuiKey_Z] = 'Z';
  418. io.RenderDrawListsFn = ImGui_ImplDX10_RenderDrawLists; // Alternatively you can set this to NULL and call ImGui::GetDrawData() after ImGui::Render() to get the same ImDrawData pointer.
  419. io.ImeWindowHandle = g_hWnd;
  420. return true;
  421. }
  422. void ImGui_ImplDX10_Shutdown()
  423. {
  424. ImGui_ImplDX10_InvalidateDeviceObjects();
  425. ImGui::Shutdown();
  426. g_pd3dDevice = NULL;
  427. g_hWnd = (HWND)0;
  428. }
  429. void ImGui_ImplDX10_NewFrame()
  430. {
  431. if (!g_pFontSampler)
  432. ImGui_ImplDX10_CreateDeviceObjects();
  433. ImGuiIO& io = ImGui::GetIO();
  434. // Setup display size (every frame to accommodate for window resizing)
  435. RECT rect;
  436. GetClientRect(g_hWnd, &rect);
  437. io.DisplaySize = ImVec2((float)(rect.right - rect.left), (float)(rect.bottom - rect.top));
  438. // Setup time step
  439. INT64 current_time;
  440. QueryPerformanceCounter((LARGE_INTEGER *)&current_time);
  441. io.DeltaTime = (float)(current_time - g_Time) / g_TicksPerSecond;
  442. g_Time = current_time;
  443. // Read keyboard modifiers inputs
  444. io.KeyCtrl = (GetKeyState(VK_CONTROL) & 0x8000) != 0;
  445. io.KeyShift = (GetKeyState(VK_SHIFT) & 0x8000) != 0;
  446. io.KeyAlt = (GetKeyState(VK_MENU) & 0x8000) != 0;
  447. io.KeySuper = false;
  448. // io.KeysDown : filled by WM_KEYDOWN/WM_KEYUP events
  449. // io.MousePos : filled by WM_MOUSEMOVE events
  450. // io.MouseDown : filled by WM_*BUTTON* events
  451. // io.MouseWheel : filled by WM_MOUSEWHEEL events
  452. // Hide OS mouse cursor if ImGui is drawing it
  453. SetCursor(io.MouseDrawCursor ? NULL : LoadCursor(NULL, IDC_ARROW));
  454. // Start the frame
  455. ImGui::NewFrame();
  456. }