examples 폴더에서 example_win32_directx11.sln을 실행합니다.
example_win32_directx11을 시작 프로젝트로 설정 후 실행하면 다양한 ImGui UI가 실행됩니다.
imgui-master 폴더의 imgui*.cpp/h, backends/imgui_impl_dx11.cpp/h, imgui_impl_win32.cpp/h를 사용합니다.Engine/99.Utils/ImGui 필터를 생성하여 ImGui 관련 소스 파일을 넣습니다.EnginePch.h에 다음 헤더를 추가합니다:
#include "imgui.h"
#include "imgui_impl_dx11.h"
#include "imgui_impl_win32.h"
ImGui는 외부 소스이므로, ImGui 관련 .cpp 파일들에 대해 Precompiled Header 사용 안 함으로 설정합니다.
StaticMeshDemo를 복사하여 ImGuiDemo 클래스를 생성합니다.
class ImGuiDemo : public IExecute
{
public:
void Init() override;
void Update() override;
void Render() override;
private:
void Test(); // ImGui 테스트용 UI
bool show_demo_window = true;
bool show_another_window = false;
Vec4 clear_color = Vec4(0.f);
};
Init(): ImGui 컨텍스트 생성 및 초기화Update(): 프레임 시작Render(): 그리기 명령 처리void ImGuiDemo::Init()
{
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard | ImGuiConfigFlags_NavEnableGamepad;
ImGui::StyleColorsDark();
ImGui_ImplWin32_Init(GAME->GetGameDesc().hWnd);
ImGui_ImplDX11_Init(DEVICE.Get(), DC.Get());
}
ImGui_ImplWin32_WndProcHandler를 메시지 루프에 추가합니다:
extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK Game::WndProc(HWND handle, UINT message, WPARAM wParam, LPARAM lParam)
{
if (ImGui_ImplWin32_WndProcHandler(handle, message, wParam, lParam))
return true;
...
}
void ImGuiDemo::Test()
{
if (show_demo_window)
ImGui::ShowDemoWindow(&show_demo_window);
ImGui::Begin("Hello, world!");
ImGui::Text("This is some useful text.");
ImGui::Checkbox("Demo Window", &show_demo_window);
ImGui::SliderFloat("float", &f, 0.0f, 1.0f);
ImGui::ColorEdit3("clear color", (float*)&clear_color);
if (ImGui::Button("Button")) counter++;
ImGui::SameLine();
ImGui::Text("counter = %d", counter);
ImGui::End();
}
ImGui::Begin("My Window", nullptr,
ImGuiWindowFlags_NoTitleBar |
ImGuiWindowFlags_NoMove);
ImGui::Text("Fixed and Title-less window");
ImGui::End();
ImGuiWindowFlags_*는 비트 플래그이므로 조합 가능.
class ImGuiManager
{
DECLARE_SINGLE(ImGuiManager);
public:
void Init();
void Update();
void Render();
};
void Game::Update()
{
GUI->Update();
_desc.app->Update();
_desc.app->Render();
GUI->Render();
}
Update()는 Begin 프레임 역할Render()는 End 프레임 역할ImGui는 외부 UI 툴 없이 코드로 직접 UI를 설계할 수 있어 매우 직관적이고 효율적입니다. MFC, Slate와 비교해도 간결하고 강력한 API를 제공하므로, 다양한 도구 제작에 적합합니다.
vcpkg로 설치한 ImGui가 Visual Studio 전역에 영향을 줄 수 있으므로, 사용하지 않을 경우 꼭 삭제해 주세요. 예전 설치본 때문에 디버깅 오류가 발생한 경험이 있었음.