2015-03-09 14:45:23 +01:00
// ImGui Win32 + DirectX11 binding
// https://github.com/ocornut/imgui
# include <imgui.h>
# include "imgui_impl_dx11.h"
// DirectX
# include <d3d11.h>
# include <d3dcompiler.h>
# define DIRECTINPUT_VERSION 0x0800
# include <dinput.h>
// Data
static INT64 g_Time = 0 ;
static INT64 g_TicksPerSecond = 0 ;
2015-03-09 15:13:29 +01:00
static HWND g_hWnd = 0 ;
2015-03-09 14:45:23 +01:00
static ID3D11Device * g_pd3dDevice = NULL ;
static ID3D11DeviceContext * g_pd3dDeviceContext = NULL ;
static ID3D11Buffer * g_pVB = NULL ;
2015-04-09 22:05:35 +02:00
static ID3D11Buffer * g_pIB = NULL ;
2015-03-09 14:45:23 +01:00
static ID3D10Blob * g_pVertexShaderBlob = NULL ;
static ID3D11VertexShader * g_pVertexShader = NULL ;
static ID3D11InputLayout * g_pInputLayout = NULL ;
static ID3D11Buffer * g_pVertexConstantBuffer = NULL ;
static ID3D10Blob * g_pPixelShaderBlob = NULL ;
static ID3D11PixelShader * g_pPixelShader = NULL ;
static ID3D11SamplerState * g_pFontSampler = NULL ;
2015-03-09 15:51:10 +01:00
static ID3D11ShaderResourceView * g_pFontTextureView = NULL ;
2015-05-18 22:38:17 +02:00
static ID3D11RasterizerState * g_pRasterizerState = NULL ;
static ID3D11BlendState * g_pBlendState = NULL ;
2015-07-06 03:24:26 +02:00
static int VERTEX_BUFFER_SIZE = 20000 ; // TODO: Make buffers smaller and grow dynamically as needed.
static int INDEX_BUFFER_SIZE = 40000 ; // TODO: Make buffers smaller and grow dynamically as needed.
2015-03-09 14:45:23 +01:00
struct VERTEX_CONSTANT_BUFFER
{
float mvp [ 4 ] [ 4 ] ;
} ;
// This is the main rendering function that you have to implement and provide to ImGui (via setting up 'RenderDrawListsFn' in the ImGuiIO structure)
// If text or lines are blurry when integrating ImGui in your engine:
// - in your Render function, try translating your projection matrix by (0.5f,0.5f) or (0.375f,0.375f)
2015-07-06 06:03:46 +02:00
static void ImGui_ImplDX11_RenderDrawLists ( ImDrawData * draw_data )
2015-03-09 14:45:23 +01:00
{
// Copy and convert all vertices into a single contiguous buffer
2015-04-09 22:05:35 +02:00
D3D11_MAPPED_SUBRESOURCE vtx_resource , idx_resource ;
if ( g_pd3dDeviceContext - > Map ( g_pVB , 0 , D3D11_MAP_WRITE_DISCARD , 0 , & vtx_resource ) ! = S_OK )
2015-03-09 14:45:23 +01:00
return ;
2015-04-09 22:05:35 +02:00
if ( g_pd3dDeviceContext - > Map ( g_pIB , 0 , D3D11_MAP_WRITE_DISCARD , 0 , & idx_resource ) ! = S_OK )
return ;
2015-04-09 22:22:06 +02:00
ImDrawVert * vtx_dst = ( ImDrawVert * ) vtx_resource . pData ;
2015-04-09 22:05:35 +02:00
ImDrawIdx * idx_dst = ( ImDrawIdx * ) idx_resource . pData ;
2015-07-08 04:17:07 +02:00
for ( int n = 0 ; n < draw_data - > CmdListsCount ; n + + )
2015-03-09 14:45:23 +01:00
{
2015-07-08 04:17:07 +02:00
const ImDrawList * cmd_list = draw_data - > CmdLists [ n ] ;
memcpy ( vtx_dst , & cmd_list - > VtxBuffer [ 0 ] , cmd_list - > VtxBuffer . size ( ) * sizeof ( ImDrawVert ) ) ;
memcpy ( idx_dst , & cmd_list - > IdxBuffer [ 0 ] , cmd_list - > IdxBuffer . size ( ) * sizeof ( ImDrawIdx ) ) ;
vtx_dst + = cmd_list - > VtxBuffer . size ( ) ;
idx_dst + = cmd_list - > IdxBuffer . size ( ) ;
2015-03-09 14:45:23 +01:00
}
g_pd3dDeviceContext - > Unmap ( g_pVB , 0 ) ;
2015-04-09 22:05:35 +02:00
g_pd3dDeviceContext - > Unmap ( g_pIB , 0 ) ;
2015-03-09 14:45:23 +01:00
// Setup orthographic projection matrix into our constant buffer
{
D3D11_MAPPED_SUBRESOURCE mappedResource ;
if ( g_pd3dDeviceContext - > Map ( g_pVertexConstantBuffer , 0 , D3D11_MAP_WRITE_DISCARD , 0 , & mappedResource ) ! = S_OK )
return ;
VERTEX_CONSTANT_BUFFER * pConstantBuffer = ( VERTEX_CONSTANT_BUFFER * ) mappedResource . pData ;
const float L = 0.0f ;
const float R = ImGui : : GetIO ( ) . DisplaySize . x ;
const float B = ImGui : : GetIO ( ) . DisplaySize . y ;
const float T = 0.0f ;
const float mvp [ 4 ] [ 4 ] =
{
{ 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 } ,
{ ( R + L ) / ( L - R ) , ( T + B ) / ( B - T ) , 0.5f , 1.0f } ,
} ;
memcpy ( & pConstantBuffer - > mvp , mvp , sizeof ( mvp ) ) ;
g_pd3dDeviceContext - > Unmap ( g_pVertexConstantBuffer , 0 ) ;
}
// Setup viewport
{
D3D11_VIEWPORT vp ;
memset ( & vp , 0 , sizeof ( D3D11_VIEWPORT ) ) ;
vp . Width = ImGui : : GetIO ( ) . DisplaySize . x ;
vp . Height = ImGui : : GetIO ( ) . DisplaySize . y ;
vp . MinDepth = 0.0f ;
vp . MaxDepth = 1.0f ;
vp . TopLeftX = 0 ;
vp . TopLeftY = 0 ;
g_pd3dDeviceContext - > RSSetViewports ( 1 , & vp ) ;
}
// Bind shader and vertex buffers
2015-04-09 22:14:52 +02:00
unsigned int stride = sizeof ( ImDrawVert ) ;
2015-03-09 14:45:23 +01:00
unsigned int offset = 0 ;
g_pd3dDeviceContext - > IASetInputLayout ( g_pInputLayout ) ;
g_pd3dDeviceContext - > IASetVertexBuffers ( 0 , 1 , & g_pVB , & stride , & offset ) ;
2015-04-09 22:05:35 +02:00
g_pd3dDeviceContext - > IASetIndexBuffer ( g_pIB , DXGI_FORMAT_R16_UINT , 0 ) ;
2015-03-09 14:45:23 +01:00
g_pd3dDeviceContext - > IASetPrimitiveTopology ( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ) ;
g_pd3dDeviceContext - > VSSetShader ( g_pVertexShader , NULL , 0 ) ;
g_pd3dDeviceContext - > VSSetConstantBuffers ( 0 , 1 , & g_pVertexConstantBuffer ) ;
g_pd3dDeviceContext - > PSSetShader ( g_pPixelShader , NULL , 0 ) ;
g_pd3dDeviceContext - > PSSetSamplers ( 0 , 1 , & g_pFontSampler ) ;
// Setup render state
const float blendFactor [ 4 ] = { 0.f , 0.f , 0.f , 0.f } ;
2015-05-18 22:38:17 +02:00
g_pd3dDeviceContext - > OMSetBlendState ( g_pBlendState , blendFactor , 0xffffffff ) ;
g_pd3dDeviceContext - > RSSetState ( g_pRasterizerState ) ;
2015-03-09 14:45:23 +01:00
// Render command lists
int vtx_offset = 0 ;
2015-04-09 22:05:35 +02:00
int idx_offset = 0 ;
2015-07-08 04:17:07 +02:00
for ( int n = 0 ; n < draw_data - > CmdListsCount ; n + + )
2015-03-09 14:45:23 +01:00
{
2015-07-08 04:17:07 +02:00
const ImDrawList * cmd_list = draw_data - > CmdLists [ n ] ;
for ( int cmd_i = 0 ; cmd_i < cmd_list - > CmdBuffer . size ( ) ; cmd_i + + )
2015-03-09 14:45:23 +01:00
{
2015-07-08 04:17:07 +02:00
const ImDrawCmd * pcmd = & cmd_list - > CmdBuffer [ cmd_i ] ;
if ( pcmd - > UserCallback )
2015-03-09 16:26:58 +01:00
{
2015-07-08 04:17:07 +02:00
pcmd - > UserCallback ( cmd_list , pcmd ) ;
2015-03-09 16:26:58 +01:00
}
else
{
2015-07-08 04:17:07 +02:00
const D3D11_RECT r = { ( LONG ) pcmd - > ClipRect . x , ( LONG ) pcmd - > ClipRect . y , ( LONG ) pcmd - > ClipRect . z , ( LONG ) pcmd - > ClipRect . w } ;
g_pd3dDeviceContext - > PSSetShaderResources ( 0 , 1 , ( ID3D11ShaderResourceView * * ) & pcmd - > TextureId ) ;
2015-03-09 16:26:58 +01:00
g_pd3dDeviceContext - > RSSetScissorRects ( 1 , & r ) ;
2015-07-08 04:17:07 +02:00
g_pd3dDeviceContext - > DrawIndexed ( pcmd - > ElemCount , idx_offset , vtx_offset ) ;
2015-03-09 16:26:58 +01:00
}
2015-07-08 04:17:07 +02:00
idx_offset + = pcmd - > ElemCount ;
2015-03-09 14:45:23 +01:00
}
2015-07-08 04:17:07 +02:00
vtx_offset + = cmd_list - > VtxBuffer . size ( ) ;
2015-03-09 14:45:23 +01:00
}
// Restore modified state
g_pd3dDeviceContext - > IASetInputLayout ( NULL ) ;
g_pd3dDeviceContext - > PSSetShader ( NULL , NULL , 0 ) ;
g_pd3dDeviceContext - > VSSetShader ( NULL , NULL , 0 ) ;
}
2015-03-14 11:32:29 +01:00
LRESULT ImGui_ImplDX11_WndProcHandler ( HWND , UINT msg , WPARAM wParam , LPARAM lParam )
2015-03-09 14:45:23 +01:00
{
ImGuiIO & io = ImGui : : GetIO ( ) ;
switch ( msg )
{
case WM_LBUTTONDOWN :
io . MouseDown [ 0 ] = true ;
return true ;
case WM_LBUTTONUP :
io . MouseDown [ 0 ] = false ;
return true ;
case WM_RBUTTONDOWN :
io . MouseDown [ 1 ] = true ;
return true ;
case WM_RBUTTONUP :
io . MouseDown [ 1 ] = false ;
return true ;
case WM_MOUSEWHEEL :
io . MouseWheel + = GET_WHEEL_DELTA_WPARAM ( wParam ) > 0 ? + 1.0f : - 1.0f ;
return true ;
case WM_MOUSEMOVE :
io . MousePos . x = ( signed short ) ( lParam ) ;
io . MousePos . y = ( signed short ) ( lParam > > 16 ) ;
return true ;
case WM_KEYDOWN :
2015-03-14 11:49:26 +01:00
if ( wParam < 256 )
2015-03-09 14:45:23 +01:00
io . KeysDown [ wParam ] = 1 ;
return true ;
case WM_KEYUP :
2015-03-14 11:49:26 +01:00
if ( wParam < 256 )
2015-03-09 14:45:23 +01:00
io . KeysDown [ wParam ] = 0 ;
return true ;
case WM_CHAR :
// You can also use ToAscii()+GetKeyboardState() to retrieve characters.
if ( wParam > 0 & & wParam < 0x10000 )
io . AddInputCharacter ( ( unsigned short ) wParam ) ;
return true ;
}
return 0 ;
}
2015-03-09 15:55:46 +01:00
static void ImGui_ImplDX11_CreateFontsTexture ( )
2015-03-09 14:45:23 +01:00
{
ImGuiIO & io = ImGui : : GetIO ( ) ;
// Build
unsigned char * pixels ;
int width , height ;
io . Fonts - > GetTexDataAsRGBA32 ( & pixels , & width , & height ) ;
// Create DX11 texture
2015-03-09 15:51:10 +01:00
{
D3D11_TEXTURE2D_DESC texDesc ;
ZeroMemory ( & texDesc , sizeof ( texDesc ) ) ;
texDesc . Width = width ;
texDesc . Height = height ;
texDesc . MipLevels = 1 ;
texDesc . ArraySize = 1 ;
texDesc . Format = DXGI_FORMAT_R8G8B8A8_UNORM ;
texDesc . SampleDesc . Count = 1 ;
texDesc . Usage = D3D11_USAGE_DEFAULT ;
texDesc . BindFlags = D3D11_BIND_SHADER_RESOURCE ;
texDesc . CPUAccessFlags = 0 ;
ID3D11Texture2D * pTexture = NULL ;
D3D11_SUBRESOURCE_DATA subResource ;
subResource . pSysMem = pixels ;
subResource . SysMemPitch = texDesc . Width * 4 ;
subResource . SysMemSlicePitch = 0 ;
g_pd3dDevice - > CreateTexture2D ( & texDesc , & subResource , & pTexture ) ;
// Create texture view
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc ;
ZeroMemory ( & srvDesc , sizeof ( srvDesc ) ) ;
srvDesc . Format = DXGI_FORMAT_R8G8B8A8_UNORM ;
srvDesc . ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D ;
srvDesc . Texture2D . MipLevels = texDesc . MipLevels ;
srvDesc . Texture2D . MostDetailedMip = 0 ;
g_pd3dDevice - > CreateShaderResourceView ( pTexture , & srvDesc , & g_pFontTextureView ) ;
pTexture - > Release ( ) ;
}
2015-03-09 14:45:23 +01:00
// Store our identifier
2015-03-09 15:51:10 +01:00
io . Fonts - > TexID = ( void * ) g_pFontTextureView ;
2015-03-09 14:45:23 +01:00
// Create texture sampler
2015-03-09 15:51:10 +01:00
{
D3D11_SAMPLER_DESC samplerDesc ;
ZeroMemory ( & samplerDesc , sizeof ( samplerDesc ) ) ;
samplerDesc . Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR ;
samplerDesc . AddressU = D3D11_TEXTURE_ADDRESS_WRAP ;
samplerDesc . AddressV = D3D11_TEXTURE_ADDRESS_WRAP ;
samplerDesc . AddressW = D3D11_TEXTURE_ADDRESS_WRAP ;
samplerDesc . MipLODBias = 0.f ;
samplerDesc . ComparisonFunc = D3D11_COMPARISON_ALWAYS ;
samplerDesc . MinLOD = 0.f ;
samplerDesc . MaxLOD = 0.f ;
g_pd3dDevice - > CreateSamplerState ( & samplerDesc , & g_pFontSampler ) ;
}
2015-05-12 16:16:12 +02:00
// Cleanup (don't clear the input data if you want to append new fonts later)
io . Fonts - > ClearInputData ( ) ;
io . Fonts - > ClearTexData ( ) ;
2015-03-09 14:45:23 +01:00
}
2015-03-09 15:51:10 +01:00
bool ImGui_ImplDX11_CreateDeviceObjects ( )
2015-03-09 14:45:23 +01:00
{
2015-03-09 15:51:10 +01:00
if ( ! g_pd3dDevice )
return false ;
if ( g_pVB )
ImGui_ImplDX11_InvalidateDeviceObjects ( ) ;
2015-03-09 14:45:23 +01:00
// Create the vertex shader
{
static const char * vertexShader =
" cbuffer vertexBuffer : register(c0) \
{ \
float4x4 ProjectionMatrix ; \
} ; \
struct VS_INPUT \
{ \
float2 pos : POSITION ; \
float4 col : COLOR0 ; \
float2 uv : TEXCOORD0 ; \
} ; \
\
struct PS_INPUT \
{ \
float4 pos : SV_POSITION ; \
float4 col : COLOR0 ; \
float2 uv : TEXCOORD0 ; \
} ; \
\
PS_INPUT main ( VS_INPUT input ) \
{ \
PS_INPUT output ; \
output . pos = mul ( ProjectionMatrix , float4 ( input . pos . xy , 0.f , 1.f ) ) ; \
output . col = input . col ; \
output . uv = input . uv ; \
return output ; \
} " ;
D3DCompile ( vertexShader , strlen ( vertexShader ) , NULL , NULL , NULL , " main " , " vs_5_0 " , 0 , 0 , & g_pVertexShaderBlob , NULL ) ;
if ( g_pVertexShaderBlob = = NULL ) // NB: Pass ID3D10Blob* pErrorBlob to D3DCompile() to get error showing in (const char*)pErrorBlob->GetBufferPointer(). Make sure to Release() the blob!
return false ;
if ( g_pd3dDevice - > CreateVertexShader ( ( DWORD * ) g_pVertexShaderBlob - > GetBufferPointer ( ) , g_pVertexShaderBlob - > GetBufferSize ( ) , NULL , & g_pVertexShader ) ! = S_OK )
return false ;
// Create the input layout
D3D11_INPUT_ELEMENT_DESC localLayout [ ] = {
2015-04-09 22:14:52 +02:00
{ " POSITION " , 0 , DXGI_FORMAT_R32G32B32A32_FLOAT , 0 , ( size_t ) ( & ( ( ImDrawVert * ) 0 ) - > pos ) , D3D11_INPUT_PER_VERTEX_DATA , 0 } ,
{ " TEXCOORD " , 0 , DXGI_FORMAT_R32G32_FLOAT , 0 , ( size_t ) ( & ( ( ImDrawVert * ) 0 ) - > uv ) , D3D11_INPUT_PER_VERTEX_DATA , 0 } ,
{ " COLOR " , 0 , DXGI_FORMAT_R8G8B8A8_UNORM , 0 , ( size_t ) ( & ( ( ImDrawVert * ) 0 ) - > col ) , D3D11_INPUT_PER_VERTEX_DATA , 0 } ,
2015-03-09 14:45:23 +01:00
} ;
if ( g_pd3dDevice - > CreateInputLayout ( localLayout , 3 , g_pVertexShaderBlob - > GetBufferPointer ( ) , g_pVertexShaderBlob - > GetBufferSize ( ) , & g_pInputLayout ) ! = S_OK )
return false ;
// Create the constant buffer
{
D3D11_BUFFER_DESC cbDesc ;
cbDesc . ByteWidth = sizeof ( VERTEX_CONSTANT_BUFFER ) ;
cbDesc . Usage = D3D11_USAGE_DYNAMIC ;
cbDesc . BindFlags = D3D11_BIND_CONSTANT_BUFFER ;
cbDesc . CPUAccessFlags = D3D11_CPU_ACCESS_WRITE ;
cbDesc . MiscFlags = 0 ;
g_pd3dDevice - > CreateBuffer ( & cbDesc , NULL , & g_pVertexConstantBuffer ) ;
}
}
// Create the pixel shader
{
static const char * pixelShader =
" struct PS_INPUT \
{ \
float4 pos : SV_POSITION ; \
float4 col : COLOR0 ; \
float2 uv : TEXCOORD0 ; \
} ; \
sampler sampler0 ; \
Texture2D texture0 ; \
\
float4 main ( PS_INPUT input ) : SV_Target \
{ \
float4 out_col = input . col * texture0 . Sample ( sampler0 , input . uv ) ; \
return out_col ; \
} " ;
D3DCompile ( pixelShader , strlen ( pixelShader ) , NULL , NULL , NULL , " main " , " ps_5_0 " , 0 , 0 , & g_pPixelShaderBlob , NULL ) ;
if ( g_pPixelShaderBlob = = NULL ) // NB: Pass ID3D10Blob* pErrorBlob to D3DCompile() to get error showing in (const char*)pErrorBlob->GetBufferPointer(). Make sure to Release() the blob!
return false ;
if ( g_pd3dDevice - > CreatePixelShader ( ( DWORD * ) g_pPixelShaderBlob - > GetBufferPointer ( ) , g_pPixelShaderBlob - > GetBufferSize ( ) , NULL , & g_pPixelShader ) ! = S_OK )
return false ;
}
// Create the blending setup
{
D3D11_BLEND_DESC desc ;
ZeroMemory ( & desc , sizeof ( desc ) ) ;
desc . AlphaToCoverageEnable = false ;
desc . RenderTarget [ 0 ] . BlendEnable = true ;
desc . RenderTarget [ 0 ] . SrcBlend = D3D11_BLEND_SRC_ALPHA ;
desc . RenderTarget [ 0 ] . DestBlend = D3D11_BLEND_INV_SRC_ALPHA ;
desc . RenderTarget [ 0 ] . BlendOp = D3D11_BLEND_OP_ADD ;
desc . RenderTarget [ 0 ] . SrcBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA ;
desc . RenderTarget [ 0 ] . DestBlendAlpha = D3D11_BLEND_ZERO ;
desc . RenderTarget [ 0 ] . BlendOpAlpha = D3D11_BLEND_OP_ADD ;
desc . RenderTarget [ 0 ] . RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL ;
2015-05-18 22:38:17 +02:00
g_pd3dDevice - > CreateBlendState ( & desc , & g_pBlendState ) ;
}
// Create the rasterizer state
{
D3D11_RASTERIZER_DESC desc ;
ZeroMemory ( & desc , sizeof ( desc ) ) ;
desc . FillMode = D3D11_FILL_SOLID ;
desc . CullMode = D3D11_CULL_NONE ;
desc . ScissorEnable = true ;
desc . DepthClipEnable = true ;
g_pd3dDevice - > CreateRasterizerState ( & desc , & g_pRasterizerState ) ;
2015-03-09 14:45:23 +01:00
}
// Create the vertex buffer
{
2015-05-18 22:38:17 +02:00
D3D11_BUFFER_DESC desc ;
memset ( & desc , 0 , sizeof ( D3D11_BUFFER_DESC ) ) ;
desc . Usage = D3D11_USAGE_DYNAMIC ;
desc . ByteWidth = VERTEX_BUFFER_SIZE * sizeof ( ImDrawVert ) ;
desc . BindFlags = D3D11_BIND_VERTEX_BUFFER ;
desc . CPUAccessFlags = D3D11_CPU_ACCESS_WRITE ;
desc . MiscFlags = 0 ;
if ( g_pd3dDevice - > CreateBuffer ( & desc , NULL , & g_pVB ) < 0 )
2015-03-09 14:45:23 +01:00
return false ;
}
2015-04-09 22:05:35 +02:00
// Create the index buffer
{
D3D11_BUFFER_DESC bufferDesc ;
memset ( & bufferDesc , 0 , sizeof ( D3D11_BUFFER_DESC ) ) ;
bufferDesc . Usage = D3D11_USAGE_DYNAMIC ;
bufferDesc . ByteWidth = INDEX_BUFFER_SIZE * sizeof ( ImDrawIdx ) ;
bufferDesc . BindFlags = D3D11_BIND_INDEX_BUFFER ;
bufferDesc . CPUAccessFlags = D3D11_CPU_ACCESS_WRITE ;
if ( g_pd3dDevice - > CreateBuffer ( & bufferDesc , NULL , & g_pIB ) < 0 )
return false ;
}
2015-03-09 15:55:46 +01:00
ImGui_ImplDX11_CreateFontsTexture ( ) ;
2015-03-09 15:51:10 +01:00
2015-03-09 14:45:23 +01:00
return true ;
}
2015-03-09 15:51:10 +01:00
void ImGui_ImplDX11_InvalidateDeviceObjects ( )
{
if ( ! g_pd3dDevice )
return ;
if ( g_pFontSampler ) { g_pFontSampler - > Release ( ) ; g_pFontSampler = NULL ; }
if ( g_pFontTextureView ) { g_pFontTextureView - > Release ( ) ; ImGui : : GetIO ( ) . Fonts - > TexID = 0 ; }
2015-04-09 22:05:35 +02:00
if ( g_pIB ) { g_pIB - > Release ( ) ; g_pIB = NULL ; }
2015-03-09 15:51:10 +01:00
if ( g_pVB ) { g_pVB - > Release ( ) ; g_pVB = NULL ; }
2015-05-18 22:38:17 +02:00
if ( g_pBlendState ) { g_pBlendState - > Release ( ) ; g_pBlendState = NULL ; }
if ( g_pRasterizerState ) { g_pRasterizerState - > Release ( ) ; g_pRasterizerState = NULL ; }
2015-03-09 15:51:10 +01:00
if ( g_pPixelShader ) { g_pPixelShader - > Release ( ) ; g_pPixelShader = NULL ; }
if ( g_pPixelShaderBlob ) { g_pPixelShaderBlob - > Release ( ) ; g_pPixelShaderBlob = NULL ; }
if ( g_pVertexConstantBuffer ) { g_pVertexConstantBuffer - > Release ( ) ; g_pVertexConstantBuffer = NULL ; }
if ( g_pInputLayout ) { g_pInputLayout - > Release ( ) ; g_pInputLayout = NULL ; }
if ( g_pVertexShader ) { g_pVertexShader - > Release ( ) ; g_pVertexShader = NULL ; }
if ( g_pVertexShaderBlob ) { g_pVertexShaderBlob - > Release ( ) ; g_pVertexShaderBlob = NULL ; }
}
2015-03-09 14:45:23 +01:00
bool ImGui_ImplDX11_Init ( void * hwnd , ID3D11Device * device , ID3D11DeviceContext * device_context )
{
2015-03-09 15:13:29 +01:00
g_hWnd = ( HWND ) hwnd ;
2015-03-09 14:45:23 +01:00
g_pd3dDevice = device ;
g_pd3dDeviceContext = device_context ;
if ( ! QueryPerformanceFrequency ( ( LARGE_INTEGER * ) & g_TicksPerSecond ) )
return false ;
if ( ! QueryPerformanceCounter ( ( LARGE_INTEGER * ) & g_Time ) )
return false ;
ImGuiIO & io = ImGui : : GetIO ( ) ;
io . KeyMap [ ImGuiKey_Tab ] = VK_TAB ; // Keyboard mapping. ImGui will use those indices to peek into the io.KeyDown[] array that we will update during the application lifetime.
io . KeyMap [ ImGuiKey_LeftArrow ] = VK_LEFT ;
io . KeyMap [ ImGuiKey_RightArrow ] = VK_RIGHT ;
io . KeyMap [ ImGuiKey_UpArrow ] = VK_UP ;
2015-04-24 09:18:56 +02:00
io . KeyMap [ ImGuiKey_DownArrow ] = VK_DOWN ;
2015-07-03 05:43:07 +02:00
io . KeyMap [ ImGuiKey_PageUp ] = VK_PRIOR ;
io . KeyMap [ ImGuiKey_PageDown ] = VK_NEXT ;
2015-03-09 14:45:23 +01:00
io . KeyMap [ ImGuiKey_Home ] = VK_HOME ;
io . KeyMap [ ImGuiKey_End ] = VK_END ;
io . KeyMap [ ImGuiKey_Delete ] = VK_DELETE ;
io . KeyMap [ ImGuiKey_Backspace ] = VK_BACK ;
io . KeyMap [ ImGuiKey_Enter ] = VK_RETURN ;
io . KeyMap [ ImGuiKey_Escape ] = VK_ESCAPE ;
io . KeyMap [ ImGuiKey_A ] = ' A ' ;
io . KeyMap [ ImGuiKey_C ] = ' C ' ;
io . KeyMap [ ImGuiKey_V ] = ' V ' ;
io . KeyMap [ ImGuiKey_X ] = ' X ' ;
io . KeyMap [ ImGuiKey_Y ] = ' Y ' ;
io . KeyMap [ ImGuiKey_Z ] = ' Z ' ;
io . RenderDrawListsFn = ImGui_ImplDX11_RenderDrawLists ;
2015-03-09 15:13:29 +01:00
io . ImeWindowHandle = g_hWnd ;
2015-03-09 14:45:23 +01:00
return true ;
}
void ImGui_ImplDX11_Shutdown ( )
{
2015-03-09 15:51:10 +01:00
ImGui_ImplDX11_InvalidateDeviceObjects ( ) ;
2015-03-09 14:45:23 +01:00
ImGui : : Shutdown ( ) ;
2015-03-09 15:51:10 +01:00
g_pd3dDevice = NULL ;
g_pd3dDeviceContext = NULL ;
g_hWnd = ( HWND ) 0 ;
2015-03-09 14:45:23 +01:00
}
void ImGui_ImplDX11_NewFrame ( )
{
2015-03-09 15:51:10 +01:00
if ( ! g_pVB )
ImGui_ImplDX11_CreateDeviceObjects ( ) ;
2015-03-09 14:45:23 +01:00
ImGuiIO & io = ImGui : : GetIO ( ) ;
2015-03-09 15:13:29 +01:00
// Setup display size (every frame to accommodate for window resizing)
RECT rect ;
GetClientRect ( g_hWnd , & rect ) ;
io . DisplaySize = ImVec2 ( ( float ) ( rect . right - rect . left ) , ( float ) ( rect . bottom - rect . top ) ) ;
2015-03-09 14:45:23 +01:00
// Setup time step
INT64 current_time ;
QueryPerformanceCounter ( ( LARGE_INTEGER * ) & current_time ) ;
io . DeltaTime = ( float ) ( current_time - g_Time ) / g_TicksPerSecond ;
g_Time = current_time ;
// Read keyboard modifiers inputs
io . KeyCtrl = ( GetKeyState ( VK_CONTROL ) & 0x8000 ) ! = 0 ;
io . KeyShift = ( GetKeyState ( VK_SHIFT ) & 0x8000 ) ! = 0 ;
2015-03-13 10:49:38 +01:00
io . KeyAlt = ( GetKeyState ( VK_MENU ) & 0x8000 ) ! = 0 ;
2015-03-09 14:45:23 +01:00
// io.KeysDown : filled by WM_KEYDOWN/WM_KEYUP events
// io.MousePos : filled by WM_MOUSEMOVE events
// io.MouseDown : filled by WM_*BUTTON* events
// io.MouseWheel : filled by WM_MOUSEWHEEL events
2015-05-01 11:25:15 +02:00
// Hide OS mouse cursor if ImGui is drawing it
SetCursor ( io . MouseDrawCursor ? NULL : LoadCursor ( NULL , IDC_ARROW ) ) ;
2015-03-09 14:45:23 +01:00
// Start the frame
ImGui : : NewFrame ( ) ;
}