imgui_impl_dx9.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. // ImGui Win32 + DirectX9 binding
  2. // Implemented features:
  3. // [X] User texture binding. Use 'LPDIRECT3DTEXTURE9' as ImTextureID. Read the FAQ about ImTextureID in imgui.cpp.
  4. // You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
  5. // If you use this binding you'll need to call 4 functions: ImGui_ImplXXXX_Init(), ImGui_ImplXXXX_NewFrame(), ImGui::Render() and ImGui_ImplXXXX_Shutdown().
  6. // If you are new to ImGui, see examples/README.txt and documentation at the top of imgui.cpp.
  7. // https://github.com/ocornut/imgui
  8. // CHANGELOG
  9. // (minor and older changes stripped away, please see git history for details)
  10. // 2018-03-20: Misc: Setup io.BackendFlags ImGuiBackendFlags_HasMouseCursors and ImGuiBackendFlags_HasSetMousePos flags + honor ImGuiConfigFlags_NoMouseCursorChange flag.
  11. // 2018-02-20: Inputs: Added support for mouse cursors (ImGui::GetMouseCursor() value and WM_SETCURSOR message handling).
  12. // 2018-02-16: Misc: Obsoleted the io.RenderDrawListsFn callback and exposed ImGui_ImplDX9_RenderDrawData() in the .h file so you can call it yourself.
  13. // 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
  14. // 2018-02-06: Inputs: Honoring the io.WantSetMousePos by repositioning the mouse (when using navigation and ImGuiConfigFlags_NavEnableSetMousePos is set).
  15. // 2018-02-06: Inputs: Added mapping for ImGuiKey_Space.
  16. #include "imgui.h"
  17. #include "imgui_impl_dx9.h"
  18. // DirectX
  19. #include <d3d9.h>
  20. #define DIRECTINPUT_VERSION 0x0800
  21. #include <dinput.h>
  22. // Win32 data
  23. static HWND g_hWnd = 0;
  24. static INT64 g_Time = 0;
  25. static INT64 g_TicksPerSecond = 0;
  26. static ImGuiMouseCursor g_LastMouseCursor = ImGuiMouseCursor_COUNT;
  27. // DirectX data
  28. static LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;
  29. static LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL;
  30. static LPDIRECT3DINDEXBUFFER9 g_pIB = NULL;
  31. static LPDIRECT3DTEXTURE9 g_FontTexture = NULL;
  32. static int g_VertexBufferSize = 5000, g_IndexBufferSize = 10000;
  33. struct CUSTOMVERTEX
  34. {
  35. float pos[3];
  36. D3DCOLOR col;
  37. float uv[2];
  38. };
  39. #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1)
  40. // Render function.
  41. // (this used to be set in io.RenderDrawListsFn and called by ImGui::Render(), but you can now call this directly from your main loop)
  42. void ImGui_ImplDX9_RenderDrawData(ImDrawData* draw_data)
  43. {
  44. // Avoid rendering when minimized
  45. ImGuiIO& io = ImGui::GetIO();
  46. if (io.DisplaySize.x <= 0.0f || io.DisplaySize.y <= 0.0f)
  47. return;
  48. // Create and grow buffers if needed
  49. if (!g_pVB || g_VertexBufferSize < draw_data->TotalVtxCount)
  50. {
  51. if (g_pVB) { g_pVB->Release(); g_pVB = NULL; }
  52. g_VertexBufferSize = draw_data->TotalVtxCount + 5000;
  53. if (g_pd3dDevice->CreateVertexBuffer(g_VertexBufferSize * sizeof(CUSTOMVERTEX), D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVB, NULL) < 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. if (g_pd3dDevice->CreateIndexBuffer(g_IndexBufferSize * sizeof(ImDrawIdx), D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, sizeof(ImDrawIdx) == 2 ? D3DFMT_INDEX16 : D3DFMT_INDEX32, D3DPOOL_DEFAULT, &g_pIB, NULL) < 0)
  61. return;
  62. }
  63. // Backup the DX9 state
  64. IDirect3DStateBlock9* d3d9_state_block = NULL;
  65. if (g_pd3dDevice->CreateStateBlock(D3DSBT_ALL, &d3d9_state_block) < 0)
  66. return;
  67. // Copy and convert all vertices into a single contiguous buffer
  68. CUSTOMVERTEX* vtx_dst;
  69. ImDrawIdx* idx_dst;
  70. if (g_pVB->Lock(0, (UINT)(draw_data->TotalVtxCount * sizeof(CUSTOMVERTEX)), (void**)&vtx_dst, D3DLOCK_DISCARD) < 0)
  71. return;
  72. if (g_pIB->Lock(0, (UINT)(draw_data->TotalIdxCount * sizeof(ImDrawIdx)), (void**)&idx_dst, D3DLOCK_DISCARD) < 0)
  73. return;
  74. for (int n = 0; n < draw_data->CmdListsCount; n++)
  75. {
  76. const ImDrawList* cmd_list = draw_data->CmdLists[n];
  77. const ImDrawVert* vtx_src = cmd_list->VtxBuffer.Data;
  78. for (int i = 0; i < cmd_list->VtxBuffer.Size; i++)
  79. {
  80. vtx_dst->pos[0] = vtx_src->pos.x;
  81. vtx_dst->pos[1] = vtx_src->pos.y;
  82. vtx_dst->pos[2] = 0.0f;
  83. vtx_dst->col = (vtx_src->col & 0xFF00FF00) | ((vtx_src->col & 0xFF0000)>>16) | ((vtx_src->col & 0xFF) << 16); // RGBA --> ARGB for DirectX9
  84. vtx_dst->uv[0] = vtx_src->uv.x;
  85. vtx_dst->uv[1] = vtx_src->uv.y;
  86. vtx_dst++;
  87. vtx_src++;
  88. }
  89. memcpy(idx_dst, cmd_list->IdxBuffer.Data, cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx));
  90. idx_dst += cmd_list->IdxBuffer.Size;
  91. }
  92. g_pVB->Unlock();
  93. g_pIB->Unlock();
  94. g_pd3dDevice->SetStreamSource(0, g_pVB, 0, sizeof(CUSTOMVERTEX));
  95. g_pd3dDevice->SetIndices(g_pIB);
  96. g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
  97. // Setup viewport
  98. D3DVIEWPORT9 vp;
  99. vp.X = vp.Y = 0;
  100. vp.Width = (DWORD)io.DisplaySize.x;
  101. vp.Height = (DWORD)io.DisplaySize.y;
  102. vp.MinZ = 0.0f;
  103. vp.MaxZ = 1.0f;
  104. g_pd3dDevice->SetViewport(&vp);
  105. // Setup render state: fixed-pipeline, alpha-blending, no face culling, no depth testing
  106. g_pd3dDevice->SetPixelShader(NULL);
  107. g_pd3dDevice->SetVertexShader(NULL);
  108. g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
  109. g_pd3dDevice->SetRenderState(D3DRS_LIGHTING, false);
  110. g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, false);
  111. g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
  112. g_pd3dDevice->SetRenderState(D3DRS_ALPHATESTENABLE, false);
  113. g_pd3dDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
  114. g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
  115. g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
  116. g_pd3dDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, true);
  117. g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
  118. g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  119. g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
  120. g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
  121. g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
  122. g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
  123. g_pd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
  124. g_pd3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
  125. // Setup orthographic projection matrix
  126. // Being agnostic of whether <d3dx9.h> or <DirectXMath.h> can be used, we aren't relying on D3DXMatrixIdentity()/D3DXMatrixOrthoOffCenterLH() or DirectX::XMMatrixIdentity()/DirectX::XMMatrixOrthographicOffCenterLH()
  127. {
  128. const float L = 0.5f, R = io.DisplaySize.x+0.5f, T = 0.5f, B = io.DisplaySize.y+0.5f;
  129. D3DMATRIX mat_identity = { { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f } };
  130. D3DMATRIX mat_projection =
  131. {
  132. 2.0f/(R-L), 0.0f, 0.0f, 0.0f,
  133. 0.0f, 2.0f/(T-B), 0.0f, 0.0f,
  134. 0.0f, 0.0f, 0.5f, 0.0f,
  135. (L+R)/(L-R), (T+B)/(B-T), 0.5f, 1.0f,
  136. };
  137. g_pd3dDevice->SetTransform(D3DTS_WORLD, &mat_identity);
  138. g_pd3dDevice->SetTransform(D3DTS_VIEW, &mat_identity);
  139. g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &mat_projection);
  140. }
  141. // Render command lists
  142. int vtx_offset = 0;
  143. int idx_offset = 0;
  144. for (int n = 0; n < draw_data->CmdListsCount; n++)
  145. {
  146. const ImDrawList* cmd_list = draw_data->CmdLists[n];
  147. for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
  148. {
  149. const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
  150. if (pcmd->UserCallback)
  151. {
  152. pcmd->UserCallback(cmd_list, pcmd);
  153. }
  154. else
  155. {
  156. const RECT r = { (LONG)pcmd->ClipRect.x, (LONG)pcmd->ClipRect.y, (LONG)pcmd->ClipRect.z, (LONG)pcmd->ClipRect.w };
  157. g_pd3dDevice->SetTexture(0, (LPDIRECT3DTEXTURE9)pcmd->TextureId);
  158. g_pd3dDevice->SetScissorRect(&r);
  159. g_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, vtx_offset, 0, (UINT)cmd_list->VtxBuffer.Size, idx_offset, pcmd->ElemCount/3);
  160. }
  161. idx_offset += pcmd->ElemCount;
  162. }
  163. vtx_offset += cmd_list->VtxBuffer.Size;
  164. }
  165. // Restore the DX9 state
  166. d3d9_state_block->Apply();
  167. d3d9_state_block->Release();
  168. }
  169. static bool ImGui_ImplWin32_UpdateMouseCursor()
  170. {
  171. ImGuiIO& io = ImGui::GetIO();
  172. if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange)
  173. return false;
  174. ImGuiMouseCursor imgui_cursor = io.MouseDrawCursor ? ImGuiMouseCursor_None : ImGui::GetMouseCursor();
  175. if (imgui_cursor == ImGuiMouseCursor_None)
  176. {
  177. // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor
  178. ::SetCursor(NULL);
  179. }
  180. else
  181. {
  182. // Hardware cursor type
  183. LPTSTR win32_cursor = IDC_ARROW;
  184. switch (imgui_cursor)
  185. {
  186. case ImGuiMouseCursor_Arrow: win32_cursor = IDC_ARROW; break;
  187. case ImGuiMouseCursor_TextInput: win32_cursor = IDC_IBEAM; break;
  188. case ImGuiMouseCursor_ResizeAll: win32_cursor = IDC_SIZEALL; break;
  189. case ImGuiMouseCursor_ResizeEW: win32_cursor = IDC_SIZEWE; break;
  190. case ImGuiMouseCursor_ResizeNS: win32_cursor = IDC_SIZENS; break;
  191. case ImGuiMouseCursor_ResizeNESW: win32_cursor = IDC_SIZENESW; break;
  192. case ImGuiMouseCursor_ResizeNWSE: win32_cursor = IDC_SIZENWSE; break;
  193. }
  194. ::SetCursor(::LoadCursor(NULL, win32_cursor));
  195. }
  196. return true;
  197. }
  198. // Allow compilation with old Windows SDK. MinGW doesn't have default _WIN32_WINNT/WINVER versions.
  199. #ifndef WM_MOUSEHWHEEL
  200. #define WM_MOUSEHWHEEL 0x020E
  201. #endif
  202. // Process Win32 mouse/keyboard inputs.
  203. // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
  204. // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
  205. // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
  206. // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
  207. // PS: In this Win32 handler, we use the capture API (GetCapture/SetCapture/ReleaseCapture) to be able to read mouse coordinations when dragging mouse outside of our window bounds.
  208. // PS: We treat DBLCLK messages as regular mouse down messages, so this code will work on windows classes that have the CS_DBLCLKS flag set. Our own example app code doesn't set this flag.
  209. IMGUI_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  210. {
  211. if (ImGui::GetCurrentContext() == NULL)
  212. return 0;
  213. ImGuiIO& io = ImGui::GetIO();
  214. switch (msg)
  215. {
  216. case WM_LBUTTONDOWN: case WM_LBUTTONDBLCLK:
  217. case WM_RBUTTONDOWN: case WM_RBUTTONDBLCLK:
  218. case WM_MBUTTONDOWN: case WM_MBUTTONDBLCLK:
  219. {
  220. int button = 0;
  221. if (msg == WM_LBUTTONDOWN || msg == WM_LBUTTONDBLCLK) button = 0;
  222. if (msg == WM_RBUTTONDOWN || msg == WM_RBUTTONDBLCLK) button = 1;
  223. if (msg == WM_MBUTTONDOWN || msg == WM_MBUTTONDBLCLK) button = 2;
  224. if (!ImGui::IsAnyMouseDown() && ::GetCapture() == NULL)
  225. ::SetCapture(hwnd);
  226. io.MouseDown[button] = true;
  227. return 0;
  228. }
  229. case WM_LBUTTONUP:
  230. case WM_RBUTTONUP:
  231. case WM_MBUTTONUP:
  232. {
  233. int button = 0;
  234. if (msg == WM_LBUTTONUP) button = 0;
  235. if (msg == WM_RBUTTONUP) button = 1;
  236. if (msg == WM_MBUTTONUP) button = 2;
  237. io.MouseDown[button] = false;
  238. if (!ImGui::IsAnyMouseDown() && ::GetCapture() == hwnd)
  239. ::ReleaseCapture();
  240. return 0;
  241. }
  242. case WM_MOUSEWHEEL:
  243. io.MouseWheel += GET_WHEEL_DELTA_WPARAM(wParam) > 0 ? +1.0f : -1.0f;
  244. return 0;
  245. case WM_MOUSEHWHEEL:
  246. io.MouseWheelH += GET_WHEEL_DELTA_WPARAM(wParam) > 0 ? +1.0f : -1.0f;
  247. return 0;
  248. case WM_MOUSEMOVE:
  249. io.MousePos.x = (signed short)(lParam);
  250. io.MousePos.y = (signed short)(lParam >> 16);
  251. return 0;
  252. case WM_KEYDOWN:
  253. case WM_SYSKEYDOWN:
  254. if (wParam < 256)
  255. io.KeysDown[wParam] = 1;
  256. return 0;
  257. case WM_KEYUP:
  258. case WM_SYSKEYUP:
  259. if (wParam < 256)
  260. io.KeysDown[wParam] = 0;
  261. return 0;
  262. case WM_CHAR:
  263. // You can also use ToAscii()+GetKeyboardState() to retrieve characters.
  264. if (wParam > 0 && wParam < 0x10000)
  265. io.AddInputCharacter((unsigned short)wParam);
  266. return 0;
  267. case WM_SETCURSOR:
  268. if (LOWORD(lParam) == HTCLIENT && ImGui_ImplWin32_UpdateMouseCursor())
  269. return 1;
  270. return 0;
  271. }
  272. return 0;
  273. }
  274. bool ImGui_ImplDX9_Init(void* hwnd, IDirect3DDevice9* device)
  275. {
  276. g_hWnd = (HWND)hwnd;
  277. g_pd3dDevice = device;
  278. if (!QueryPerformanceFrequency((LARGE_INTEGER *)&g_TicksPerSecond))
  279. return false;
  280. if (!QueryPerformanceCounter((LARGE_INTEGER *)&g_Time))
  281. return false;
  282. // Setup back-end capabilities flags
  283. ImGuiIO& io = ImGui::GetIO();
  284. io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
  285. io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used)
  286. // Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array that we will update during the application lifetime.
  287. io.KeyMap[ImGuiKey_Tab] = VK_TAB;
  288. io.KeyMap[ImGuiKey_LeftArrow] = VK_LEFT;
  289. io.KeyMap[ImGuiKey_RightArrow] = VK_RIGHT;
  290. io.KeyMap[ImGuiKey_UpArrow] = VK_UP;
  291. io.KeyMap[ImGuiKey_DownArrow] = VK_DOWN;
  292. io.KeyMap[ImGuiKey_PageUp] = VK_PRIOR;
  293. io.KeyMap[ImGuiKey_PageDown] = VK_NEXT;
  294. io.KeyMap[ImGuiKey_Home] = VK_HOME;
  295. io.KeyMap[ImGuiKey_End] = VK_END;
  296. io.KeyMap[ImGuiKey_Insert] = VK_INSERT;
  297. io.KeyMap[ImGuiKey_Delete] = VK_DELETE;
  298. io.KeyMap[ImGuiKey_Backspace] = VK_BACK;
  299. io.KeyMap[ImGuiKey_Space] = VK_SPACE;
  300. io.KeyMap[ImGuiKey_Enter] = VK_RETURN;
  301. io.KeyMap[ImGuiKey_Escape] = VK_ESCAPE;
  302. io.KeyMap[ImGuiKey_A] = 'A';
  303. io.KeyMap[ImGuiKey_C] = 'C';
  304. io.KeyMap[ImGuiKey_V] = 'V';
  305. io.KeyMap[ImGuiKey_X] = 'X';
  306. io.KeyMap[ImGuiKey_Y] = 'Y';
  307. io.KeyMap[ImGuiKey_Z] = 'Z';
  308. io.ImeWindowHandle = g_hWnd;
  309. return true;
  310. }
  311. void ImGui_ImplDX9_Shutdown()
  312. {
  313. ImGui_ImplDX9_InvalidateDeviceObjects();
  314. g_pd3dDevice = NULL;
  315. g_hWnd = 0;
  316. }
  317. static bool ImGui_ImplDX9_CreateFontsTexture()
  318. {
  319. // Build texture atlas
  320. ImGuiIO& io = ImGui::GetIO();
  321. unsigned char* pixels;
  322. int width, height, bytes_per_pixel;
  323. io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height, &bytes_per_pixel);
  324. // Upload texture to graphics system
  325. g_FontTexture = NULL;
  326. if (g_pd3dDevice->CreateTexture(width, height, 1, D3DUSAGE_DYNAMIC, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &g_FontTexture, NULL) < 0)
  327. return false;
  328. D3DLOCKED_RECT tex_locked_rect;
  329. if (g_FontTexture->LockRect(0, &tex_locked_rect, NULL, 0) != D3D_OK)
  330. return false;
  331. for (int y = 0; y < height; y++)
  332. memcpy((unsigned char *)tex_locked_rect.pBits + tex_locked_rect.Pitch * y, pixels + (width * bytes_per_pixel) * y, (width * bytes_per_pixel));
  333. g_FontTexture->UnlockRect(0);
  334. // Store our identifier
  335. io.Fonts->TexID = (void *)g_FontTexture;
  336. return true;
  337. }
  338. bool ImGui_ImplDX9_CreateDeviceObjects()
  339. {
  340. if (!g_pd3dDevice)
  341. return false;
  342. if (!ImGui_ImplDX9_CreateFontsTexture())
  343. return false;
  344. return true;
  345. }
  346. void ImGui_ImplDX9_InvalidateDeviceObjects()
  347. {
  348. if (!g_pd3dDevice)
  349. return;
  350. if (g_pVB)
  351. {
  352. g_pVB->Release();
  353. g_pVB = NULL;
  354. }
  355. if (g_pIB)
  356. {
  357. g_pIB->Release();
  358. g_pIB = NULL;
  359. }
  360. // At this point note that we set ImGui::GetIO().Fonts->TexID to be == g_FontTexture, so clear both.
  361. ImGuiIO& io = ImGui::GetIO();
  362. IM_ASSERT(g_FontTexture == io.Fonts->TexID);
  363. if (g_FontTexture)
  364. g_FontTexture->Release();
  365. g_FontTexture = NULL;
  366. io.Fonts->TexID = NULL;
  367. }
  368. void ImGui_ImplDX9_NewFrame()
  369. {
  370. if (!g_FontTexture)
  371. ImGui_ImplDX9_CreateDeviceObjects();
  372. ImGuiIO& io = ImGui::GetIO();
  373. // Setup display size (every frame to accommodate for window resizing)
  374. RECT rect;
  375. GetClientRect(g_hWnd, &rect);
  376. io.DisplaySize = ImVec2((float)(rect.right - rect.left), (float)(rect.bottom - rect.top));
  377. // Setup time step
  378. INT64 current_time;
  379. QueryPerformanceCounter((LARGE_INTEGER *)&current_time);
  380. io.DeltaTime = (float)(current_time - g_Time) / g_TicksPerSecond;
  381. g_Time = current_time;
  382. // Read keyboard modifiers inputs
  383. io.KeyCtrl = (GetKeyState(VK_CONTROL) & 0x8000) != 0;
  384. io.KeyShift = (GetKeyState(VK_SHIFT) & 0x8000) != 0;
  385. io.KeyAlt = (GetKeyState(VK_MENU) & 0x8000) != 0;
  386. io.KeySuper = false;
  387. // io.KeysDown : filled by WM_KEYDOWN/WM_KEYUP events
  388. // io.MousePos : filled by WM_MOUSEMOVE events
  389. // io.MouseDown : filled by WM_*BUTTON* events
  390. // io.MouseWheel : filled by WM_MOUSEWHEEL events
  391. // Set OS mouse position if requested (only used when ImGuiConfigFlags_NavEnableSetMousePos is enabled by user)
  392. if (io.WantSetMousePos)
  393. {
  394. POINT pos = { (int)io.MousePos.x, (int)io.MousePos.y };
  395. ClientToScreen(g_hWnd, &pos);
  396. SetCursorPos(pos.x, pos.y);
  397. }
  398. // Update OS mouse cursor with the cursor requested by imgui
  399. ImGuiMouseCursor mouse_cursor = io.MouseDrawCursor ? ImGuiMouseCursor_None : ImGui::GetMouseCursor();
  400. if (g_LastMouseCursor != mouse_cursor)
  401. {
  402. g_LastMouseCursor = mouse_cursor;
  403. ImGui_ImplWin32_UpdateMouseCursor();
  404. }
  405. // Start the frame. This call will update the io.WantCaptureMouse, io.WantCaptureKeyboard flag that you can use to dispatch inputs (or not) to your application.
  406. ImGui::NewFrame();
  407. }