mirror of
https://github.com/ocornut/imgui.git
synced 2024-11-28 01:20:55 +01:00
Examples: DirectX9 example cleanup to match DirectX11 structure.
This commit is contained in:
parent
66a5837ba8
commit
f9c833b4a5
@ -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;
|
||||
}
|
||||
|
@ -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();
|
||||
|
||||
|
@ -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.
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user