main.cpp 6.0 KB

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