main.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // ImGui - standalone example application for DirectX 9
  2. #include <imgui.h>
  3. #include "imgui_impl_dx9.h"
  4. #include <d3dx9.h>
  5. #define DIRECTINPUT_VERSION 0x0800
  6. #include <dinput.h>
  7. #include <tchar.h>
  8. // Data
  9. static LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;
  10. static D3DPRESENT_PARAMETERS g_d3dpp;
  11. extern LRESULT ImGui_ImplDX9_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
  12. LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  13. {
  14. if (ImGui_ImplDX9_WndProcHandler(hWnd, msg, wParam, lParam))
  15. return true;
  16. switch (msg)
  17. {
  18. case WM_SIZE:
  19. if (g_pd3dDevice != NULL && wParam != SIZE_MINIMIZED)
  20. {
  21. ImGui_ImplDX9_InvalidateDeviceObjects();
  22. g_d3dpp.BackBufferWidth = LOWORD(lParam);
  23. g_d3dpp.BackBufferHeight = HIWORD(lParam);
  24. HRESULT hr = g_pd3dDevice->Reset(&g_d3dpp);
  25. if (hr == D3DERR_INVALIDCALL)
  26. IM_ASSERT(0);
  27. ImGui_ImplDX9_CreateDeviceObjects();
  28. }
  29. return 0;
  30. case WM_SYSCOMMAND:
  31. if ((wParam & 0xfff0) == SC_KEYMENU) // Disable ALT application menu
  32. return 0;
  33. break;
  34. case WM_DESTROY:
  35. PostQuitMessage(0);
  36. return 0;
  37. }
  38. return DefWindowProc(hWnd, msg, wParam, lParam);
  39. }
  40. int main(int, char**)
  41. {
  42. // Create application window
  43. WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, WndProc, 0L, 0L, GetModuleHandle(NULL), NULL, LoadCursor(NULL, IDC_ARROW), NULL, NULL, _T("ImGui Example"), NULL };
  44. RegisterClassEx(&wc);
  45. HWND hwnd = CreateWindow(_T("ImGui Example"), _T("ImGui DirectX9 Example"), WS_OVERLAPPEDWINDOW, 100, 100, 1280, 800, NULL, NULL, wc.hInstance, NULL);
  46. // Initialize Direct3D
  47. LPDIRECT3D9 pD3D;
  48. if ((pD3D = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)
  49. {
  50. UnregisterClass(_T("ImGui Example"), wc.hInstance);
  51. return 0;
  52. }
  53. ZeroMemory(&g_d3dpp, sizeof(g_d3dpp));
  54. g_d3dpp.Windowed = TRUE;
  55. g_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  56. g_d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
  57. g_d3dpp.EnableAutoDepthStencil = TRUE;
  58. g_d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
  59. g_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
  60. // Create the D3DDevice
  61. if (pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &g_d3dpp, &g_pd3dDevice) < 0)
  62. {
  63. pD3D->Release();
  64. UnregisterClass(_T("ImGui Example"), wc.hInstance);
  65. return 0;
  66. }
  67. // Setup ImGui binding
  68. ImGui_ImplDX9_Init(hwnd, g_pd3dDevice);
  69. // Load Fonts
  70. // (see extra_fonts/README.txt for more details)
  71. //ImGuiIO& io = ImGui::GetIO();
  72. //io.Fonts->AddFontDefault();
  73. //io.Fonts->AddFontFromFileTTF("../../extra_fonts/Cousine-Regular.ttf", 15.0f);
  74. //io.Fonts->AddFontFromFileTTF("../../extra_fonts/DroidSans.ttf", 16.0f);
  75. //io.Fonts->AddFontFromFileTTF("../../extra_fonts/ProggyClean.ttf", 13.0f);
  76. //io.Fonts->AddFontFromFileTTF("../../extra_fonts/ProggyTiny.ttf", 10.0f);
  77. //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
  78. // Merge glyphs from multiple fonts into one (e.g. combine default font with another with Chinese glyphs, or add icons)
  79. //static const ImWchar icons_ranges[] = { 0xf000, 0xf3ff, 0 }; // will not be copied by AddFont* so keep in scope.
  80. //ImFontConfig icons_config; icons_config.MergeMode = true; icons_config.PixelSnapH = true;
  81. //io.Fonts->AddFontFromFileTTF("../../extra_fonts/DroidSans.ttf", 18.0f);
  82. //io.Fonts->AddFontFromFileTTF("../../extra_fonts/fontawesome-webfont.ttf", 18.0f, &icons_config, icons_ranges);
  83. bool show_test_window = true;
  84. bool show_another_window = false;
  85. ImVec4 clear_col = ImColor(114, 144, 154);
  86. // Main loop
  87. MSG msg;
  88. ZeroMemory(&msg, sizeof(msg));
  89. ShowWindow(hwnd, SW_SHOWDEFAULT);
  90. UpdateWindow(hwnd);
  91. while (msg.message != WM_QUIT)
  92. {
  93. if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
  94. {
  95. TranslateMessage(&msg);
  96. DispatchMessage(&msg);
  97. continue;
  98. }
  99. ImGui_ImplDX9_NewFrame();
  100. // 1. Show a simple window
  101. // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets appears in a window automatically called "Debug"
  102. {
  103. static float f = 0.0f;
  104. ImGui::Text("Hello, world!");
  105. ImGui::SliderFloat("float", &f, 0.0f, 1.0f);
  106. ImGui::ColorEdit3("clear color", (float*)&clear_col);
  107. if (ImGui::Button("Test Window")) show_test_window ^= 1;
  108. if (ImGui::Button("Another Window")) show_another_window ^= 1;
  109. ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
  110. }
  111. // 2. Show another simple window, this time using an explicit Begin/End pair
  112. if (show_another_window)
  113. {
  114. ImGui::SetNextWindowSize(ImVec2(200,100), ImGuiSetCond_FirstUseEver);
  115. ImGui::Begin("Another Window", &show_another_window);
  116. ImGui::Text("Hello");
  117. ImGui::End();
  118. }
  119. // 3. Show the ImGui test window. Most of the sample code is in ImGui::ShowTestWindow()
  120. if (show_test_window)
  121. {
  122. ImGui::SetNextWindowPos(ImVec2(650, 20), ImGuiSetCond_FirstUseEver);
  123. ImGui::ShowTestWindow(&show_test_window);
  124. }
  125. // Rendering
  126. g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, false);
  127. g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
  128. g_pd3dDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, false);
  129. D3DCOLOR clear_col_dx = D3DCOLOR_RGBA((int)(clear_col.x*255.0f), (int)(clear_col.y*255.0f), (int)(clear_col.z*255.0f), (int)(clear_col.w*255.0f));
  130. g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, clear_col_dx, 1.0f, 0);
  131. if (g_pd3dDevice->BeginScene() >= 0)
  132. {
  133. ImGui::Render();
  134. g_pd3dDevice->EndScene();
  135. }
  136. g_pd3dDevice->Present(NULL, NULL, NULL, NULL);
  137. }
  138. ImGui_ImplDX9_Shutdown();
  139. if (g_pd3dDevice) g_pd3dDevice->Release();
  140. if (pD3D) pD3D->Release();
  141. UnregisterClass(_T("ImGui Example"), wc.hInstance);
  142. return 0;
  143. }