|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "imgui.h" |
|
|
#include "imgui_impl_opengl3.h" |
|
|
#include "imgui_impl_win32.h" |
|
|
#ifndef WIN32_LEAN_AND_MEAN |
|
|
#define WIN32_LEAN_AND_MEAN |
|
|
#endif |
|
|
#include <windows.h> |
|
|
#include <GL/GL.h> |
|
|
#include <tchar.h> |
|
|
|
|
|
|
|
|
struct WGL_WindowData { HDC hDC; }; |
|
|
|
|
|
|
|
|
static HGLRC g_hRC; |
|
|
static WGL_WindowData g_MainWindow; |
|
|
static int g_Width; |
|
|
static int g_Height; |
|
|
|
|
|
|
|
|
bool CreateDeviceWGL(HWND hWnd, WGL_WindowData* data); |
|
|
void CleanupDeviceWGL(HWND hWnd, WGL_WindowData* data); |
|
|
void ResetDeviceWGL(); |
|
|
LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); |
|
|
|
|
|
|
|
|
int main(int, char**) |
|
|
{ |
|
|
|
|
|
|
|
|
WNDCLASSEXW wc = { sizeof(wc), CS_OWNDC, WndProc, 0L, 0L, GetModuleHandle(nullptr), nullptr, nullptr, nullptr, nullptr, L"ImGui Example", nullptr }; |
|
|
::RegisterClassExW(&wc); |
|
|
HWND hwnd = ::CreateWindowW(wc.lpszClassName, L"Dear ImGui Win32+OpenGL3 Example", WS_OVERLAPPEDWINDOW, 100, 100, 1280, 800, nullptr, nullptr, wc.hInstance, nullptr); |
|
|
|
|
|
|
|
|
if (!CreateDeviceWGL(hwnd, &g_MainWindow)) |
|
|
{ |
|
|
CleanupDeviceWGL(hwnd, &g_MainWindow); |
|
|
::DestroyWindow(hwnd); |
|
|
::UnregisterClassW(wc.lpszClassName, wc.hInstance); |
|
|
return 1; |
|
|
} |
|
|
wglMakeCurrent(g_MainWindow.hDC, g_hRC); |
|
|
|
|
|
|
|
|
::ShowWindow(hwnd, SW_SHOWDEFAULT); |
|
|
::UpdateWindow(hwnd); |
|
|
|
|
|
|
|
|
IMGUI_CHECKVERSION(); |
|
|
ImGui::CreateContext(); |
|
|
ImGuiIO& io = ImGui::GetIO(); (void)io; |
|
|
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; |
|
|
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; |
|
|
|
|
|
|
|
|
ImGui::StyleColorsDark(); |
|
|
|
|
|
|
|
|
|
|
|
ImGui_ImplWin32_InitForOpenGL(hwnd); |
|
|
ImGui_ImplOpenGL3_Init(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool show_demo_window = true; |
|
|
bool show_another_window = false; |
|
|
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); |
|
|
|
|
|
|
|
|
bool done = false; |
|
|
while (!done) |
|
|
{ |
|
|
|
|
|
|
|
|
MSG msg; |
|
|
while (::PeekMessage(&msg, nullptr, 0U, 0U, PM_REMOVE)) |
|
|
{ |
|
|
::TranslateMessage(&msg); |
|
|
::DispatchMessage(&msg); |
|
|
if (msg.message == WM_QUIT) |
|
|
done = true; |
|
|
} |
|
|
if (done) |
|
|
break; |
|
|
if (::IsIconic(hwnd)) |
|
|
{ |
|
|
::Sleep(10); |
|
|
continue; |
|
|
} |
|
|
|
|
|
|
|
|
ImGui_ImplOpenGL3_NewFrame(); |
|
|
ImGui_ImplWin32_NewFrame(); |
|
|
ImGui::NewFrame(); |
|
|
|
|
|
|
|
|
if (show_demo_window) |
|
|
ImGui::ShowDemoWindow(&show_demo_window); |
|
|
|
|
|
|
|
|
{ |
|
|
static float f = 0.0f; |
|
|
static int counter = 0; |
|
|
|
|
|
ImGui::Begin("Hello, world!"); |
|
|
|
|
|
ImGui::Text("This is some useful text."); |
|
|
ImGui::Checkbox("Demo Window", &show_demo_window); |
|
|
ImGui::Checkbox("Another Window", &show_another_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::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate); |
|
|
ImGui::End(); |
|
|
} |
|
|
|
|
|
|
|
|
if (show_another_window) |
|
|
{ |
|
|
ImGui::Begin("Another Window", &show_another_window); |
|
|
ImGui::Text("Hello from another window!"); |
|
|
if (ImGui::Button("Close Me")) |
|
|
show_another_window = false; |
|
|
ImGui::End(); |
|
|
} |
|
|
|
|
|
|
|
|
ImGui::Render(); |
|
|
glViewport(0, 0, g_Width, g_Height); |
|
|
glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w); |
|
|
glClear(GL_COLOR_BUFFER_BIT); |
|
|
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); |
|
|
|
|
|
|
|
|
::SwapBuffers(g_MainWindow.hDC); |
|
|
} |
|
|
|
|
|
ImGui_ImplOpenGL3_Shutdown(); |
|
|
ImGui_ImplWin32_Shutdown(); |
|
|
ImGui::DestroyContext(); |
|
|
|
|
|
CleanupDeviceWGL(hwnd, &g_MainWindow); |
|
|
wglDeleteContext(g_hRC); |
|
|
::DestroyWindow(hwnd); |
|
|
::UnregisterClassW(wc.lpszClassName, wc.hInstance); |
|
|
|
|
|
return 0; |
|
|
} |
|
|
|
|
|
|
|
|
bool CreateDeviceWGL(HWND hWnd, WGL_WindowData* data) |
|
|
{ |
|
|
HDC hDc = ::GetDC(hWnd); |
|
|
PIXELFORMATDESCRIPTOR pfd = { 0 }; |
|
|
pfd.nSize = sizeof(pfd); |
|
|
pfd.nVersion = 1; |
|
|
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; |
|
|
pfd.iPixelType = PFD_TYPE_RGBA; |
|
|
pfd.cColorBits = 32; |
|
|
|
|
|
const int pf = ::ChoosePixelFormat(hDc, &pfd); |
|
|
if (pf == 0) |
|
|
return false; |
|
|
if (::SetPixelFormat(hDc, pf, &pfd) == FALSE) |
|
|
return false; |
|
|
::ReleaseDC(hWnd, hDc); |
|
|
|
|
|
data->hDC = ::GetDC(hWnd); |
|
|
if (!g_hRC) |
|
|
g_hRC = wglCreateContext(data->hDC); |
|
|
return true; |
|
|
} |
|
|
|
|
|
void CleanupDeviceWGL(HWND hWnd, WGL_WindowData* data) |
|
|
{ |
|
|
wglMakeCurrent(nullptr, nullptr); |
|
|
::ReleaseDC(hWnd, data->hDC); |
|
|
} |
|
|
|
|
|
|
|
|
extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) |
|
|
{ |
|
|
if (ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam)) |
|
|
return true; |
|
|
|
|
|
switch (msg) |
|
|
{ |
|
|
case WM_SIZE: |
|
|
if (wParam != SIZE_MINIMIZED) |
|
|
{ |
|
|
g_Width = LOWORD(lParam); |
|
|
g_Height = HIWORD(lParam); |
|
|
} |
|
|
return 0; |
|
|
case WM_SYSCOMMAND: |
|
|
if ((wParam & 0xfff0) == SC_KEYMENU) |
|
|
return 0; |
|
|
break; |
|
|
case WM_DESTROY: |
|
|
::PostQuitMessage(0); |
|
|
return 0; |
|
|
} |
|
|
return ::DefWindowProcW(hWnd, msg, wParam, lParam); |
|
|
} |
|
|
|