main.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. //ImGuiIO& io = ImGui::GetIO();
  69. //ImFont* my_font1 = io.Fonts->AddFontDefault();
  70. //ImFont* my_font2 = io.Fonts->AddFontFromFileTTF("extra_fonts/Karla-Regular.ttf", 15.0f);
  71. //ImFont* my_font3 = io.Fonts->AddFontFromFileTTF("extra_fonts/ProggyClean.ttf", 13.0f); my_font3->DisplayOffset.y += 1;
  72. //ImFont* my_font4 = io.Fonts->AddFontFromFileTTF("extra_fonts/ProggyTiny.ttf", 10.0f); my_font4->DisplayOffset.y += 1;
  73. //ImFont* my_font5 = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, io.Fonts->GetGlyphRangesJapanese());
  74. ShowWindow(hwnd, SW_SHOWDEFAULT);
  75. UpdateWindow(hwnd);
  76. bool show_test_window = true;
  77. bool show_another_window = false;
  78. ImVec4 clear_col = ImColor(114, 144, 154);
  79. // Main loop
  80. MSG msg;
  81. ZeroMemory(&msg, sizeof(msg));
  82. while (msg.message != WM_QUIT)
  83. {
  84. if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
  85. {
  86. TranslateMessage(&msg);
  87. DispatchMessage(&msg);
  88. continue;
  89. }
  90. ImGui_ImplDX9_NewFrame();
  91. // 1. Show a simple window
  92. // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets appears in a window automatically called "Debug"
  93. {
  94. static float f;
  95. ImGui::Text("Hello, world!");
  96. ImGui::SliderFloat("float", &f, 0.0f, 1.0f);
  97. ImGui::ColorEdit3("clear color", (float*)&clear_col);
  98. if (ImGui::Button("Test Window")) show_test_window ^= 1;
  99. if (ImGui::Button("Another Window")) show_another_window ^= 1;
  100. ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
  101. }
  102. // 2. Show another simple window, this time using an explicit Begin/End pair
  103. if (show_another_window)
  104. {
  105. ImGui::Begin("Another Window", &show_another_window, ImVec2(200,100));
  106. ImGui::Text("Hello");
  107. ImGui::End();
  108. }
  109. // 3. Show the ImGui test window. Most of the sample code is in ImGui::ShowTestWindow()
  110. if (show_test_window)
  111. {
  112. ImGui::SetNextWindowPos(ImVec2(650, 20), ImGuiSetCond_FirstUseEver);
  113. ImGui::ShowTestWindow(&show_test_window);
  114. }
  115. // Rendering
  116. g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, false);
  117. g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
  118. g_pd3dDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, false);
  119. 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));
  120. g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, clear_col_dx, 1.0f, 0);
  121. if (g_pd3dDevice->BeginScene() >= 0)
  122. {
  123. ImGui::Render();
  124. g_pd3dDevice->EndScene();
  125. }
  126. g_pd3dDevice->Present(NULL, NULL, NULL, NULL);
  127. }
  128. ImGui_ImplDX9_Shutdown();
  129. if (g_pd3dDevice) g_pd3dDevice->Release();
  130. if (pD3D) pD3D->Release();
  131. UnregisterClass(L"ImGui Example", wc.hInstance);
  132. return 0;
  133. }