imgui_impl_dx10.cpp 19 KB

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