main.cpp 5.0 KB

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