imgui_impl_dx9.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // ImGui Win32 + DirectX9 binding
  2. // https://github.com/ocornut/imgui
  3. #include <imgui.h>
  4. #include "imgui_impl_dx9.h"
  5. // DirectX
  6. #include <d3dx9.h>
  7. #define DIRECTINPUT_VERSION 0x0800
  8. #include <dinput.h>
  9. // Data
  10. static INT64 g_Time = 0;
  11. static INT64 g_TicksPerSecond = 0;
  12. static bool g_FontTextureLoaded = false;
  13. static LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;
  14. static LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL;
  15. struct CUSTOMVERTEX
  16. {
  17. D3DXVECTOR3 pos;
  18. D3DCOLOR col;
  19. D3DXVECTOR2 uv;
  20. };
  21. #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1)
  22. // This is the main rendering function that you have to implement and provide to ImGui (via setting up 'RenderDrawListsFn' in the ImGuiIO structure)
  23. // If text or lines are blurry when integrating ImGui in your engine:
  24. // - in your Render function, try translating your projection matrix by (0.5f,0.5f) or (0.375f,0.375f)
  25. static void ImGui_ImplDX9_RenderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_count)
  26. {
  27. size_t total_vtx_count = 0;
  28. for (int n = 0; n < cmd_lists_count; n++)
  29. total_vtx_count += cmd_lists[n]->vtx_buffer.size();
  30. if (total_vtx_count == 0)
  31. return;
  32. // Copy and convert all vertices into a single contiguous buffer
  33. CUSTOMVERTEX* vtx_dst;
  34. if (g_pVB->Lock(0, (UINT)total_vtx_count, (void**)&vtx_dst, D3DLOCK_DISCARD) < 0)
  35. return;
  36. for (int n = 0; n < cmd_lists_count; n++)
  37. {
  38. const ImDrawList* cmd_list = cmd_lists[n];
  39. const ImDrawVert* vtx_src = &cmd_list->vtx_buffer[0];
  40. for (size_t i = 0; i < cmd_list->vtx_buffer.size(); i++)
  41. {
  42. vtx_dst->pos.x = vtx_src->pos.x;
  43. vtx_dst->pos.y = vtx_src->pos.y;
  44. vtx_dst->pos.z = 0.0f;
  45. vtx_dst->col = (vtx_src->col & 0xFF00FF00) | ((vtx_src->col & 0xFF0000)>>16) | ((vtx_src->col & 0xFF) << 16); // RGBA --> ARGB for DirectX9
  46. vtx_dst->uv.x = vtx_src->uv.x;
  47. vtx_dst->uv.y = vtx_src->uv.y;
  48. vtx_dst++;
  49. vtx_src++;
  50. }
  51. }
  52. g_pVB->Unlock();
  53. g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof( CUSTOMVERTEX ) );
  54. g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
  55. // Setup render state: alpha-blending, no face culling, no depth testing
  56. g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
  57. g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, false );
  58. g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, false );
  59. g_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, true );
  60. g_pd3dDevice->SetRenderState( D3DRS_BLENDOP, D3DBLENDOP_ADD );
  61. g_pd3dDevice->SetRenderState( D3DRS_ALPHATESTENABLE, false );
  62. g_pd3dDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );
  63. g_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );
  64. g_pd3dDevice->SetRenderState( D3DRS_SCISSORTESTENABLE, true );
  65. g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );
  66. g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_DIFFUSE );
  67. g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE );
  68. g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
  69. g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
  70. g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
  71. g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
  72. // Setup orthographic projection matrix
  73. D3DXMATRIXA16 mat;
  74. D3DXMatrixIdentity(&mat);
  75. g_pd3dDevice->SetTransform(D3DTS_WORLD, &mat);
  76. g_pd3dDevice->SetTransform(D3DTS_VIEW, &mat);
  77. D3DXMatrixOrthoOffCenterLH(&mat, 0.5f, ImGui::GetIO().DisplaySize.x+0.5f, ImGui::GetIO().DisplaySize.y+0.5f, 0.5f, -1.0f, +1.0f);
  78. g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &mat);
  79. // Render command lists
  80. int vtx_offset = 0;
  81. for (int n = 0; n < cmd_lists_count; n++)
  82. {
  83. // Render command list
  84. const ImDrawList* cmd_list = cmd_lists[n];
  85. for (size_t cmd_i = 0; cmd_i < cmd_list->commands.size(); cmd_i++)
  86. {
  87. const ImDrawCmd* pcmd = &cmd_list->commands[cmd_i];
  88. const RECT r = { (LONG)pcmd->clip_rect.x, (LONG)pcmd->clip_rect.y, (LONG)pcmd->clip_rect.z, (LONG)pcmd->clip_rect.w };
  89. g_pd3dDevice->SetTexture( 0, (LPDIRECT3DTEXTURE9)pcmd->texture_id );
  90. g_pd3dDevice->SetScissorRect(&r);
  91. g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, vtx_offset, pcmd->vtx_count/3);
  92. vtx_offset += pcmd->vtx_count;
  93. }
  94. }
  95. }
  96. LRESULT ImGui_ImplDX9_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  97. {
  98. ImGuiIO& io = ImGui::GetIO();
  99. switch (msg)
  100. {
  101. case WM_LBUTTONDOWN:
  102. io.MouseDown[0] = true;
  103. return true;
  104. case WM_LBUTTONUP:
  105. io.MouseDown[0] = false;
  106. return true;
  107. case WM_RBUTTONDOWN:
  108. io.MouseDown[1] = true;
  109. return true;
  110. case WM_RBUTTONUP:
  111. io.MouseDown[1] = false;
  112. return true;
  113. case WM_MOUSEWHEEL:
  114. io.MouseWheel += GET_WHEEL_DELTA_WPARAM(wParam) > 0 ? +1.0f : -1.0f;
  115. return true;
  116. case WM_MOUSEMOVE:
  117. io.MousePos.x = (signed short)(lParam);
  118. io.MousePos.y = (signed short)(lParam >> 16);
  119. return true;
  120. case WM_KEYDOWN:
  121. if (wParam >= 0 && wParam < 256)
  122. io.KeysDown[wParam] = 1;
  123. return true;
  124. case WM_KEYUP:
  125. if (wParam >= 0 && wParam < 256)
  126. io.KeysDown[wParam] = 0;
  127. return true;
  128. case WM_CHAR:
  129. // You can also use ToAscii()+GetKeyboardState() to retrieve characters.
  130. if (wParam > 0 && wParam < 0x10000)
  131. io.AddInputCharacter((unsigned short)wParam);
  132. return true;
  133. }
  134. return 0;
  135. }
  136. void ImGui_ImplDX9_InitFontsTexture()
  137. {
  138. ImGuiIO& io = ImGui::GetIO();
  139. // Build
  140. unsigned char* pixels;
  141. int width, height, bytes_per_pixel;
  142. io.Fonts->GetTexDataAsAlpha8(&pixels, &width, &height, &bytes_per_pixel);
  143. // Create DX9 texture
  144. LPDIRECT3DTEXTURE9 pTexture = NULL;
  145. if (D3DXCreateTexture(g_pd3dDevice, width, height, 1, D3DUSAGE_DYNAMIC, D3DFMT_A8, D3DPOOL_DEFAULT, &pTexture) < 0)
  146. {
  147. IM_ASSERT(0);
  148. return;
  149. }
  150. D3DLOCKED_RECT tex_locked_rect;
  151. if (pTexture->LockRect(0, &tex_locked_rect, NULL, 0) != D3D_OK)
  152. {
  153. IM_ASSERT(0);
  154. return;
  155. }
  156. for (int y = 0; y < height; y++)
  157. memcpy((unsigned char *)tex_locked_rect.pBits + tex_locked_rect.Pitch * y, pixels + (width * bytes_per_pixel) * y, (width * bytes_per_pixel));
  158. pTexture->UnlockRect(0);
  159. // Store our identifier
  160. io.Fonts->TexID = (void *)pTexture;
  161. g_FontTextureLoaded = true;
  162. }
  163. bool ImGui_ImplDX9_Init(void* hwnd, IDirect3DDevice9* device, bool install_callbacks)
  164. {
  165. g_pd3dDevice = device;
  166. if (!QueryPerformanceFrequency((LARGE_INTEGER *)&g_TicksPerSecond))
  167. return false;
  168. if (!QueryPerformanceCounter((LARGE_INTEGER *)&g_Time))
  169. return false;
  170. // FIXME
  171. RECT rect;
  172. GetClientRect((HWND)hwnd, &rect);
  173. int display_w = (int)(rect.right - rect.left);
  174. int display_h = (int)(rect.bottom - rect.top);
  175. ImGuiIO& io = ImGui::GetIO();
  176. io.DisplaySize = ImVec2((float)display_w, (float)display_h); // Display size, in pixels. For clamping windows positions.
  177. 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.
  178. io.KeyMap[ImGuiKey_LeftArrow] = VK_LEFT;
  179. io.KeyMap[ImGuiKey_RightArrow] = VK_RIGHT;
  180. io.KeyMap[ImGuiKey_UpArrow] = VK_UP;
  181. io.KeyMap[ImGuiKey_DownArrow] = VK_UP;
  182. io.KeyMap[ImGuiKey_Home] = VK_HOME;
  183. io.KeyMap[ImGuiKey_End] = VK_END;
  184. io.KeyMap[ImGuiKey_Delete] = VK_DELETE;
  185. io.KeyMap[ImGuiKey_Backspace] = VK_BACK;
  186. io.KeyMap[ImGuiKey_Enter] = VK_RETURN;
  187. io.KeyMap[ImGuiKey_Escape] = VK_ESCAPE;
  188. io.KeyMap[ImGuiKey_A] = 'A';
  189. io.KeyMap[ImGuiKey_C] = 'C';
  190. io.KeyMap[ImGuiKey_V] = 'V';
  191. io.KeyMap[ImGuiKey_X] = 'X';
  192. io.KeyMap[ImGuiKey_Y] = 'Y';
  193. io.KeyMap[ImGuiKey_Z] = 'Z';
  194. io.RenderDrawListsFn = ImGui_ImplDX9_RenderDrawLists;
  195. io.ImeWindowHandle = hwnd;
  196. // Create the vertex buffer
  197. if (g_pd3dDevice->CreateVertexBuffer(10000 * sizeof(CUSTOMVERTEX), D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVB, NULL) < 0)
  198. return false;
  199. return true;
  200. }
  201. void ImGui_ImplDX9_Shutdown()
  202. {
  203. if (g_pVB)
  204. {
  205. g_pVB->Release();
  206. g_pVB = NULL;
  207. }
  208. if (LPDIRECT3DTEXTURE9 tex = (LPDIRECT3DTEXTURE9)ImGui::GetIO().Fonts->TexID)
  209. {
  210. tex->Release();
  211. ImGui::GetIO().Fonts->TexID = 0;
  212. }
  213. ImGui::Shutdown();
  214. }
  215. void ImGui_ImplDX9_NewFrame()
  216. {
  217. if (!g_FontTextureLoaded)
  218. ImGui_ImplDX9_InitFontsTexture();
  219. ImGuiIO& io = ImGui::GetIO();
  220. // Setup time step
  221. INT64 current_time;
  222. QueryPerformanceCounter((LARGE_INTEGER *)&current_time);
  223. io.DeltaTime = (float)(current_time - g_Time) / g_TicksPerSecond;
  224. g_Time = current_time;
  225. // Read keyboard modifiers inputs
  226. io.KeyCtrl = (GetKeyState(VK_CONTROL) & 0x8000) != 0;
  227. io.KeyShift = (GetKeyState(VK_SHIFT) & 0x8000) != 0;
  228. // io.KeysDown : filled by WM_KEYDOWN/WM_KEYUP events
  229. // io.MousePos : filled by WM_MOUSEMOVE events
  230. // io.MouseDown : filled by WM_*BUTTON* events
  231. // io.MouseWheel : filled by WM_MOUSEWHEEL events
  232. // Start the frame
  233. ImGui::NewFrame();
  234. }