From f9c833b4a5fce38c489dd018df3d3f1b51096d7d Mon Sep 17 00:00:00 2001 From: ocornut Date: Mon, 9 Mar 2015 14:55:46 +0000 Subject: [PATCH] Examples: DirectX9 example cleanup to match DirectX11 structure. --- .../directx11_example/imgui_impl_dx11.cpp | 4 +- examples/directx9_example/imgui_impl_dx9.cpp | 104 +++++++++--------- examples/directx9_example/imgui_impl_dx9.h | 3 +- examples/directx9_example/main.cpp | 1 - 4 files changed, 55 insertions(+), 57 deletions(-) diff --git a/examples/directx11_example/imgui_impl_dx11.cpp b/examples/directx11_example/imgui_impl_dx11.cpp index ffcb7cdb2..a2cbd0da4 100644 --- a/examples/directx11_example/imgui_impl_dx11.cpp +++ b/examples/directx11_example/imgui_impl_dx11.cpp @@ -180,7 +180,7 @@ LRESULT ImGui_ImplDX11_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM return 0; } -static void ImGui_ImplDX11_InitFontsTexture() +static void ImGui_ImplDX11_CreateFontsTexture() { ImGuiIO& io = ImGui::GetIO(); @@ -359,7 +359,7 @@ bool ImGui_ImplDX11_CreateDeviceObjects() return false; } - ImGui_ImplDX11_InitFontsTexture(); + ImGui_ImplDX11_CreateFontsTexture(); return true; } diff --git a/examples/directx9_example/imgui_impl_dx9.cpp b/examples/directx9_example/imgui_impl_dx9.cpp index c69a8d2e0..259102af5 100644 --- a/examples/directx9_example/imgui_impl_dx9.cpp +++ b/examples/directx9_example/imgui_impl_dx9.cpp @@ -13,7 +13,6 @@ static HWND g_hWnd = 0; static INT64 g_Time = 0; static INT64 g_TicksPerSecond = 0; -static bool g_FontTextureLoaded = false; static LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; static LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL; @@ -145,37 +144,6 @@ LRESULT ImGui_ImplDX9_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM return 0; } -void ImGui_ImplDX9_InitFontsTexture() -{ - ImGuiIO& io = ImGui::GetIO(); - - // Build - unsigned char* pixels; - int width, height, bytes_per_pixel; - io.Fonts->GetTexDataAsAlpha8(&pixels, &width, &height, &bytes_per_pixel); - - // Create DX9 texture - LPDIRECT3DTEXTURE9 pTexture = NULL; - if (D3DXCreateTexture(g_pd3dDevice, width, height, 1, D3DUSAGE_DYNAMIC, D3DFMT_A8, D3DPOOL_DEFAULT, &pTexture) < 0) - { - IM_ASSERT(0); - return; - } - D3DLOCKED_RECT tex_locked_rect; - if (pTexture->LockRect(0, &tex_locked_rect, NULL, 0) != D3D_OK) - { - IM_ASSERT(0); - return; - } - for (int y = 0; y < height; y++) - memcpy((unsigned char *)tex_locked_rect.pBits + tex_locked_rect.Pitch * y, pixels + (width * bytes_per_pixel) * y, (width * bytes_per_pixel)); - pTexture->UnlockRect(0); - - // Store our identifier - io.Fonts->TexID = (void *)pTexture; - g_FontTextureLoaded = true; -} - bool ImGui_ImplDX9_Init(void* hwnd, IDirect3DDevice9* device) { g_hWnd = (HWND)hwnd; @@ -214,6 +182,56 @@ bool ImGui_ImplDX9_Init(void* hwnd, IDirect3DDevice9* device) return true; } +void ImGui_ImplDX9_Shutdown() +{ + ImGui_ImplDX9_InvalidateDeviceObjects(); + ImGui::Shutdown(); + g_pd3dDevice = NULL; + g_hWnd = 0; +} + +static void ImGui_ImplDX9_CreateFontsTexture() +{ + ImGuiIO& io = ImGui::GetIO(); + + // Build + unsigned char* pixels; + int width, height, bytes_per_pixel; + io.Fonts->GetTexDataAsAlpha8(&pixels, &width, &height, &bytes_per_pixel); + + // Create DX9 texture + LPDIRECT3DTEXTURE9 pTexture = NULL; + if (D3DXCreateTexture(g_pd3dDevice, width, height, 1, D3DUSAGE_DYNAMIC, D3DFMT_A8, D3DPOOL_DEFAULT, &pTexture) < 0) + { + IM_ASSERT(0); + return; + } + D3DLOCKED_RECT tex_locked_rect; + if (pTexture->LockRect(0, &tex_locked_rect, NULL, 0) != D3D_OK) + { + IM_ASSERT(0); + return; + } + for (int y = 0; y < height; y++) + memcpy((unsigned char *)tex_locked_rect.pBits + tex_locked_rect.Pitch * y, pixels + (width * bytes_per_pixel) * y, (width * bytes_per_pixel)); + pTexture->UnlockRect(0); + + // Store our identifier + io.Fonts->TexID = (void *)pTexture; +} + +bool ImGui_ImplDX9_CreateDeviceObjects() +{ + if (!g_pd3dDevice) + return false; + + if (g_pd3dDevice->CreateVertexBuffer(10000 * sizeof(CUSTOMVERTEX), D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVB, NULL) < 0) + return false; + + ImGui_ImplDX9_CreateFontsTexture(); + return true; +} + void ImGui_ImplDX9_InvalidateDeviceObjects() { if (!g_pd3dDevice) @@ -230,28 +248,10 @@ void ImGui_ImplDX9_InvalidateDeviceObjects() } } -void ImGui_ImplDX9_CreateDeviceObjects() -{ - if (!g_pd3dDevice) - return; - if (g_pd3dDevice->CreateVertexBuffer(10000 * sizeof(CUSTOMVERTEX), D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVB, NULL) < 0) - return; - - ImGui_ImplDX9_InitFontsTexture(); -} - -void ImGui_ImplDX9_Shutdown() -{ - ImGui_ImplDX9_InvalidateDeviceObjects(); - ImGui::Shutdown(); - g_pd3dDevice = NULL; - g_hWnd = 0; -} - void ImGui_ImplDX9_NewFrame() { - if (!g_FontTextureLoaded) - ImGui_ImplDX9_InitFontsTexture(); + if (!g_pVB) + ImGui_ImplDX9_CreateDeviceObjects(); ImGuiIO& io = ImGui::GetIO(); diff --git a/examples/directx9_example/imgui_impl_dx9.h b/examples/directx9_example/imgui_impl_dx9.h index 1822bee88..0f53b3d5b 100644 --- a/examples/directx9_example/imgui_impl_dx9.h +++ b/examples/directx9_example/imgui_impl_dx9.h @@ -4,13 +4,12 @@ struct IDirect3DDevice9; bool ImGui_ImplDX9_Init(void* hwnd, IDirect3DDevice9* device); -void ImGui_ImplDX9_InitFontsTexture(); void ImGui_ImplDX9_Shutdown(); void ImGui_ImplDX9_NewFrame(); // Use if you want to reset your rendering device without losing ImGui state. void ImGui_ImplDX9_InvalidateDeviceObjects(); -void ImGui_ImplDX9_CreateDeviceObjects(); +bool ImGui_ImplDX9_CreateDeviceObjects(); // Handler for Win32 messages, update mouse/keyboard data. // You may or not need this for your implementation, but it can serve as reference for handling inputs. diff --git a/examples/directx9_example/main.cpp b/examples/directx9_example/main.cpp index 2f2a6188b..75892ffbf 100644 --- a/examples/directx9_example/main.cpp +++ b/examples/directx9_example/main.cpp @@ -75,7 +75,6 @@ int main(int argc, char** argv) //ImFont* my_font3 = io.Fonts->AddFontFromFileTTF("extra_fonts/ProggyClean.ttf", 13.0f); my_font3->DisplayOffset.y += 1; //ImFont* my_font4 = io.Fonts->AddFontFromFileTTF("extra_fonts/ProggyTiny.ttf", 10.0f); my_font4->DisplayOffset.y += 1; //ImFont* my_font5 = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, io.Fonts->GetGlyphRangesJapanese()); - ImGui_ImplDX9_InitFontsTexture(); ShowWindow(hwnd, SW_SHOWDEFAULT); UpdateWindow(hwnd);