main.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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_DESTROY:
  30. PostQuitMessage(0);
  31. return 0;
  32. }
  33. return DefWindowProc(hWnd, msg, wParam, lParam);
  34. }
  35. int main(int argc, char** argv)
  36. {
  37. // Create application window
  38. WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, WndProc, 0L, 0L, GetModuleHandle(NULL), NULL, LoadCursor(NULL, IDC_ARROW), NULL, NULL, L"ImGui Example", NULL };
  39. RegisterClassEx(&wc);
  40. HWND hwnd = CreateWindow(L"ImGui Example", L"ImGui DirectX9 Example", WS_OVERLAPPEDWINDOW, 100, 100, 1280, 800, NULL, NULL, wc.hInstance, NULL);
  41. // Initialize Direct3D
  42. LPDIRECT3D9 pD3D;
  43. if ((pD3D = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)
  44. {
  45. UnregisterClass(L"ImGui Example", wc.hInstance);
  46. return 0;
  47. }
  48. ZeroMemory(&g_d3dpp, sizeof(g_d3dpp));
  49. g_d3dpp.Windowed = TRUE;
  50. g_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  51. g_d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
  52. g_d3dpp.EnableAutoDepthStencil = TRUE;
  53. g_d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
  54. g_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
  55. // Create the D3DDevice
  56. if (pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &g_d3dpp, &g_pd3dDevice) < 0)
  57. {
  58. pD3D->Release();
  59. UnregisterClass(L"ImGui Example", wc.hInstance);
  60. return 0;
  61. }
  62. // Setup ImGui binding
  63. ImGui_ImplDX9_Init(hwnd, g_pd3dDevice);
  64. //ImGuiIO& io = ImGui::GetIO();
  65. //ImFont* my_font1 = io.Fonts->AddFontDefault();
  66. //ImFont* my_font2 = io.Fonts->AddFontFromFileTTF("extra_fonts/Karla-Regular.ttf", 15.0f);
  67. //ImFont* my_font3 = io.Fonts->AddFontFromFileTTF("extra_fonts/ProggyClean.ttf", 13.0f); my_font3->DisplayOffset.y += 1;
  68. //ImFont* my_font4 = io.Fonts->AddFontFromFileTTF("extra_fonts/ProggyTiny.ttf", 10.0f); my_font4->DisplayOffset.y += 1;
  69. //ImFont* my_font5 = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, io.Fonts->GetGlyphRangesJapanese());
  70. ImGui_ImplDX9_InitFontsTexture();
  71. ShowWindow(hwnd, SW_SHOWDEFAULT);
  72. UpdateWindow(hwnd);
  73. bool show_test_window = true;
  74. bool show_another_window = false;
  75. ImVec4 clear_col = ImColor(114, 144, 154);
  76. // Main loop
  77. MSG msg;
  78. ZeroMemory(&msg, sizeof(msg));
  79. while (msg.message != WM_QUIT)
  80. {
  81. if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
  82. {
  83. TranslateMessage(&msg);
  84. DispatchMessage(&msg);
  85. continue;
  86. }
  87. ImGui_ImplDX9_NewFrame();
  88. // 1. Show a simple window
  89. // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets appears in a window automatically called "Debug"
  90. {
  91. static float f;
  92. ImGui::Text("Hello, world!");
  93. ImGui::SliderFloat("float", &f, 0.0f, 1.0f);
  94. ImGui::ColorEdit3("clear color", (float*)&clear_col);
  95. if (ImGui::Button("Test Window")) show_test_window ^= 1;
  96. if (ImGui::Button("Another Window")) show_another_window ^= 1;
  97. ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
  98. }
  99. // 2. Show another simple window, this time using an explicit Begin/End pair
  100. if (show_another_window)
  101. {
  102. ImGui::Begin("Another Window", &show_another_window, ImVec2(200,100));
  103. ImGui::Text("Hello");
  104. ImGui::End();
  105. }
  106. // 3. Show the ImGui test window. Most of the sample code is in ImGui::ShowTestWindow()
  107. if (show_test_window)
  108. {
  109. ImGui::SetNextWindowPos(ImVec2(650, 20), ImGuiSetCond_FirstUseEver);
  110. ImGui::ShowTestWindow(&show_test_window);
  111. }
  112. // Rendering
  113. g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, false);
  114. g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
  115. g_pd3dDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, false);
  116. 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));
  117. g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, clear_col_dx, 1.0f, 0);
  118. if (g_pd3dDevice->BeginScene() >= 0)
  119. {
  120. ImGui::Render();
  121. g_pd3dDevice->EndScene();
  122. }
  123. g_pd3dDevice->Present(NULL, NULL, NULL, NULL);
  124. }
  125. ImGui_ImplDX9_Shutdown();
  126. if (g_pd3dDevice) g_pd3dDevice->Release();
  127. if (pD3D) pD3D->Release();
  128. UnregisterClass(L"ImGui Example", wc.hInstance);
  129. return 0;
  130. }