From 50df86985d99e20d5a145abb284b83a4b1cb6e7a Mon Sep 17 00:00:00 2001 From: ocornut Date: Tue, 3 May 2016 10:47:42 +0200 Subject: [PATCH] Examples: DirectX9: Removed dependency on dx3x9.h so it can be used in a DirectXMath.h only environment (#611) --- examples/directx9_example/imgui_impl_dx9.cpp | 46 +++++++++++++------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/examples/directx9_example/imgui_impl_dx9.cpp b/examples/directx9_example/imgui_impl_dx9.cpp index 813d58ef..3db8c8c9 100644 --- a/examples/directx9_example/imgui_impl_dx9.cpp +++ b/examples/directx9_example/imgui_impl_dx9.cpp @@ -10,7 +10,7 @@ #include "imgui_impl_dx9.h" // DirectX -#include +#include #define DIRECTINPUT_VERSION 0x0800 #include @@ -26,9 +26,9 @@ static int g_VertexBufferSize = 5000, g_IndexBufferSize = 1 struct CUSTOMVERTEX { - D3DXVECTOR3 pos; - D3DCOLOR col; - D3DXVECTOR2 uv; + float pos[3]; + D3DCOLOR col; + float uv[2]; }; #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1) @@ -37,6 +37,11 @@ struct CUSTOMVERTEX // - in your Render function, try translating your projection matrix by (0.5f,0.5f) or (0.375f,0.375f) void ImGui_ImplDX9_RenderDrawLists(ImDrawData* draw_data) { + // Avoid rendering when minimized + ImGuiIO& io = ImGui::GetIO(); + if (io.DisplaySize.x <= 0.0f || io.DisplaySize.y <= 0.0f) + return; + // Create and grow buffers if needed if (!g_pVB || g_VertexBufferSize < draw_data->TotalVtxCount) { @@ -75,12 +80,12 @@ void ImGui_ImplDX9_RenderDrawLists(ImDrawData* draw_data) const ImDrawVert* vtx_src = &cmd_list->VtxBuffer[0]; for (int i = 0; i < cmd_list->VtxBuffer.size(); i++) { - vtx_dst->pos.x = vtx_src->pos.x; - vtx_dst->pos.y = vtx_src->pos.y; - vtx_dst->pos.z = 0.0f; + vtx_dst->pos[0] = vtx_src->pos.x; + vtx_dst->pos[1] = vtx_src->pos.y; + vtx_dst->pos[2] = 0.0f; vtx_dst->col = (vtx_src->col & 0xFF00FF00) | ((vtx_src->col & 0xFF0000)>>16) | ((vtx_src->col & 0xFF) << 16); // RGBA --> ARGB for DirectX9 - vtx_dst->uv.x = vtx_src->uv.x; - vtx_dst->uv.y = vtx_src->uv.y; + vtx_dst->uv[0] = vtx_src->uv.x; + vtx_dst->uv[1] = vtx_src->uv.y; vtx_dst++; vtx_src++; } @@ -115,12 +120,21 @@ void ImGui_ImplDX9_RenderDrawLists(ImDrawData* draw_data) g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR ); // Setup orthographic projection matrix - D3DXMATRIXA16 mat; - D3DXMatrixIdentity(&mat); - g_pd3dDevice->SetTransform( D3DTS_WORLD, &mat ); - g_pd3dDevice->SetTransform( D3DTS_VIEW, &mat ); - D3DXMatrixOrthoOffCenterLH( &mat, 0.5f, ImGui::GetIO().DisplaySize.x+0.5f, ImGui::GetIO().DisplaySize.y+0.5f, 0.5f, -1.0f, +1.0f ); - g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &mat ); + // Being agnostic of whether or can be used, we aren't relying on D3DXMatrixIdentity()/D3DXMatrixOrthoOffCenterLH() or DirectX::XMMatrixIdentity()/DirectX::XMMatrixOrthographicOffCenterLH() + { + const float L = 0.5f, R = io.DisplaySize.x+0.5f, T = 0.5f, B = io.DisplaySize.y+0.5f; + D3DMATRIX mat_identity = { { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f } }; + D3DMATRIX mat_projection = + { + 2.0f/(R-L), 0.0f, 0.0f, 0.0f, + 0.0f, 2.0f/(T-B), 0.0f, 0.0f, + 0.0f, 0.0f, 0.5f, 0.0f, + (L+R)/(L-R), (T+B)/(B-T), 0.5f, 1.0f, + }; + g_pd3dDevice->SetTransform(D3DTS_WORLD, &mat_identity); + g_pd3dDevice->SetTransform(D3DTS_VIEW, &mat_identity); + g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &mat_projection); + } // Render command lists int vtx_offset = 0; @@ -256,7 +270,7 @@ static bool ImGui_ImplDX9_CreateFontsTexture() // Upload texture to graphics system g_FontTexture = NULL; - if (D3DXCreateTexture(g_pd3dDevice, width, height, 1, D3DUSAGE_DYNAMIC, D3DFMT_A8B8G8R8, D3DPOOL_DEFAULT, &g_FontTexture) < 0) + if (g_pd3dDevice->CreateTexture(width, height, 1, D3DUSAGE_DYNAMIC, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &g_FontTexture, NULL) < 0) return false; D3DLOCKED_RECT tex_locked_rect; if (g_FontTexture->LockRect(0, &tex_locked_rect, NULL, 0) != D3D_OK)