main.cpp 5.0 KB

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