Examples: DirectX9: Removed dependency on dx3x9.h so it can be used in a DirectXMath.h only environment (#611)

This commit is contained in:
ocornut 2016-05-03 10:47:42 +02:00
parent 89d5026187
commit 50df86985d

View File

@ -10,7 +10,7 @@
#include "imgui_impl_dx9.h" #include "imgui_impl_dx9.h"
// DirectX // DirectX
#include <d3dx9.h> #include <d3d9.h>
#define DIRECTINPUT_VERSION 0x0800 #define DIRECTINPUT_VERSION 0x0800
#include <dinput.h> #include <dinput.h>
@ -26,9 +26,9 @@ static int g_VertexBufferSize = 5000, g_IndexBufferSize = 1
struct CUSTOMVERTEX struct CUSTOMVERTEX
{ {
D3DXVECTOR3 pos; float pos[3];
D3DCOLOR col; D3DCOLOR col;
D3DXVECTOR2 uv; float uv[2];
}; };
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1) #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) // - 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) 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 // Create and grow buffers if needed
if (!g_pVB || g_VertexBufferSize < draw_data->TotalVtxCount) 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]; const ImDrawVert* vtx_src = &cmd_list->VtxBuffer[0];
for (int i = 0; i < cmd_list->VtxBuffer.size(); i++) for (int i = 0; i < cmd_list->VtxBuffer.size(); i++)
{ {
vtx_dst->pos.x = vtx_src->pos.x; vtx_dst->pos[0] = vtx_src->pos.x;
vtx_dst->pos.y = vtx_src->pos.y; vtx_dst->pos[1] = vtx_src->pos.y;
vtx_dst->pos.z = 0.0f; 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->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[0] = vtx_src->uv.x;
vtx_dst->uv.y = vtx_src->uv.y; vtx_dst->uv[1] = vtx_src->uv.y;
vtx_dst++; vtx_dst++;
vtx_src++; vtx_src++;
} }
@ -115,12 +120,21 @@ void ImGui_ImplDX9_RenderDrawLists(ImDrawData* draw_data)
g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
// Setup orthographic projection matrix // Setup orthographic projection matrix
D3DXMATRIXA16 mat; // Being agnostic of whether <d3dx9.h> or <DirectXMath.h> can be used, we aren't relying on D3DXMatrixIdentity()/D3DXMatrixOrthoOffCenterLH() or DirectX::XMMatrixIdentity()/DirectX::XMMatrixOrthographicOffCenterLH()
D3DXMatrixIdentity(&mat); {
g_pd3dDevice->SetTransform( D3DTS_WORLD, &mat ); const float L = 0.5f, R = io.DisplaySize.x+0.5f, T = 0.5f, B = io.DisplaySize.y+0.5f;
g_pd3dDevice->SetTransform( D3DTS_VIEW, &mat ); 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 } };
D3DXMatrixOrthoOffCenterLH( &mat, 0.5f, ImGui::GetIO().DisplaySize.x+0.5f, ImGui::GetIO().DisplaySize.y+0.5f, 0.5f, -1.0f, +1.0f ); D3DMATRIX mat_projection =
g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &mat ); {
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 // Render command lists
int vtx_offset = 0; int vtx_offset = 0;
@ -256,7 +270,7 @@ static bool ImGui_ImplDX9_CreateFontsTexture()
// Upload texture to graphics system // Upload texture to graphics system
g_FontTexture = NULL; 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; return false;
D3DLOCKED_RECT tex_locked_rect; D3DLOCKED_RECT tex_locked_rect;
if (g_FontTexture->LockRect(0, &tex_locked_rect, NULL, 0) != D3D_OK) if (g_FontTexture->LockRect(0, &tex_locked_rect, NULL, 0) != D3D_OK)