Memory override #3

This commit is contained in:
Sepehr Taghdisian 2014-08-31 14:58:21 +04:30
commit 25080d53e5
21 changed files with 5357 additions and 5139 deletions

View File

@ -30,13 +30,34 @@ The Immediate Mode GUI paradigm may at first appear unusual to some users. This
- [Jari Komppa's tutorial on building an ImGui library](http://iki.fi/sol/imgui/). - [Jari Komppa's tutorial on building an ImGui library](http://iki.fi/sol/imgui/).
- [Casey Muratori's original video that popularized the concept](https://mollyrocket.com/861). - [Casey Muratori's original video that popularized the concept](https://mollyrocket.com/861).
Frequently Asked Question
-------------------------
<b>How do you use ImGui on a platform that may not have a mouse and keyboard?</b>
I recommend using [Synergy](http://synergy-project.org) and the uSynergy.c micro client to share your mouse and keyboard. This way you can seemingly use your PC input devices on a video game console or a tablet. ImGui was also designed to function with touch inputs if you increase the padding of widgets to compensate for the lack of precision of touch devices, but it is recommended you use a mouse to allow optimising for screen real-estate.
<b>I integrated ImGui in my engine and the text or lines are blurry..</b>
- Try adjusting ImGui::GetIO().PixelCenterOffset to 0.0f or 0.5f.
- In your Render function, try translating your projection matrix by (0.5f,0.5f) or (0.375f,0.375f).
<b>Can you create elaborate/serious tools with ImGui?</b>
Yes. I have written data browsers, debuggers, profilers and all sort of non-trivial tools with the library. There's no reason you cannot, and in my experience the simplicity of the API is very empowering. However note that ImGui is programmer centric and the immediate-mode GUI paradigm might requires a bit of adaptation before you can realize its full potential.
<b>Can you reskin the look of ImGui?</b>
Yes, you can alter the look of the interface to some degree: changing colors, sizes and padding, font. However, as ImGui is designed and optimised to create debug tools, the amount of skinning you can apply is limited. There is only so much you can stray away from the default look and feel of the interface. The example below uses modified settings to create a more compact UI with different colors:
![skinning screenshot 1](/web/skinning_sample_01.png?raw=true)
Credits Credits
------- -------
Developed by [Omar Cornut](http://www.miracleworld.net). The library was developed with the support of [Media Molecule](http://www.mediamolecule.com) and first used internally on the game [Tearaway](http://tearaway.mediamolecule.com). Developed by [Omar Cornut](http://www.miracleworld.net). The library was developed with the support of [Media Molecule](http://www.mediamolecule.com) and first used internally on the game [Tearaway](http://tearaway.mediamolecule.com).
Embeds [proggy_clean](http://www.proggyfonts.net/) font by Tristan Grimmer (also MIT license). Embeds [proggy_clean](http://upperbounds.net) font by Tristan Grimmer (also MIT license).
Inspiration, feedback, and testing: Casey Muratori, Atman Binstock, Mikko Mononen, Emmanuel Briney, Stefan Kamoda, Anton Mikhailov, Matt Willis. Thanks! Inspiration, feedback, and testing: Casey Muratori, Atman Binstock, Mikko Mononen, Emmanuel Briney, Stefan Kamoda, Anton Mikhailov, Matt Willis. Thanks!

View File

@ -5,98 +5,101 @@
#include <dinput.h> #include <dinput.h>
#include "../../imgui.h" #include "../../imgui.h"
#pragma warning (disable: 4996) // 'This function or variable may be unsafe': strdup #pragma warning (disable: 4996) // 'This function or variable may be unsafe': strdup
static HWND hWnd; static HWND hWnd;
static LPDIRECT3D9 g_pD3D = NULL; // Used to create the D3DDevice static LPDIRECT3D9 g_pD3D = NULL; // Used to create the D3DDevice
static LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // Our rendering device static LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // Our rendering device
static LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL; // Buffer to hold vertices static LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL; // Buffer to hold vertices
static LPDIRECT3DTEXTURE9 g_pTexture = NULL; // Our texture static LPDIRECT3DTEXTURE9 g_pTexture = NULL; // Our texture
struct CUSTOMVERTEX struct CUSTOMVERTEX
{ {
D3DXVECTOR3 position; D3DXVECTOR3 position;
D3DCOLOR color; D3DCOLOR color;
float tu, tv; float tu, tv;
}; };
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1) #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1)
// This is the main rendering function that you have to implement and provide to ImGui (via setting up 'RenderDrawListsFn' in the ImGuiIO structuer) // 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)
// - try adjusting ImGui::GetIO().PixelCenterOffset to 0.5f or 0.375f
static void ImImpl_RenderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_count) static void ImImpl_RenderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_count)
{ {
size_t total_vtx_count = 0; size_t total_vtx_count = 0;
for (int n = 0; n < cmd_lists_count; n++) for (int n = 0; n < cmd_lists_count; n++)
total_vtx_count += cmd_lists[n]->vtx_buffer.size(); total_vtx_count += cmd_lists[n]->vtx_buffer.size();
if (total_vtx_count == 0) if (total_vtx_count == 0)
return; return;
// Copy and convert all vertices into a single contiguous buffer // Copy and convert all vertices into a single contiguous buffer
CUSTOMVERTEX* vtx_dst; CUSTOMVERTEX* vtx_dst;
if (g_pVB->Lock(0, total_vtx_count, (void**)&vtx_dst, D3DLOCK_DISCARD) < 0) if (g_pVB->Lock(0, total_vtx_count, (void**)&vtx_dst, D3DLOCK_DISCARD) < 0)
return; return;
for (int n = 0; n < cmd_lists_count; n++) for (int n = 0; n < cmd_lists_count; n++)
{ {
const ImDrawList* cmd_list = cmd_lists[n]; const ImDrawList* cmd_list = cmd_lists[n];
const ImDrawVert* vtx_src = &cmd_list->vtx_buffer[0]; const ImDrawVert* vtx_src = &cmd_list->vtx_buffer[0];
for (size_t i = 0; i < cmd_list->vtx_buffer.size(); i++) for (size_t i = 0; i < cmd_list->vtx_buffer.size(); i++)
{ {
vtx_dst->position.x = vtx_src->pos.x; vtx_dst->position.x = vtx_src->pos.x;
vtx_dst->position.y = vtx_src->pos.y; vtx_dst->position.y = vtx_src->pos.y;
vtx_dst->position.z = 0.0f; vtx_dst->position.z = 0.0f;
vtx_dst->color = (vtx_src->col & 0xFF00FF00) | ((vtx_src->col & 0xFF0000)>>16) | ((vtx_src->col & 0xFF) << 16); // RGBA --> ARGB for DirectX9 vtx_dst->color = (vtx_src->col & 0xFF00FF00) | ((vtx_src->col & 0xFF0000)>>16) | ((vtx_src->col & 0xFF) << 16); // RGBA --> ARGB for DirectX9
vtx_dst->tu = vtx_src->uv.x; vtx_dst->tu = vtx_src->uv.x;
vtx_dst->tv = vtx_src->uv.y; vtx_dst->tv = vtx_src->uv.y;
vtx_dst++; vtx_dst++;
vtx_src++; vtx_src++;
} }
} }
g_pVB->Unlock(); g_pVB->Unlock();
g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof( CUSTOMVERTEX ) ); g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof( CUSTOMVERTEX ) );
g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX ); g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
// Setup render state: alpha-blending, no face culling, no depth testing // Setup render state: alpha-blending, no face culling, no depth testing
g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE ); g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, false ); g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, false );
g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, false ); g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, false );
g_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, true ); g_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, true );
g_pd3dDevice->SetRenderState( D3DRS_BLENDOP, D3DBLENDOP_ADD ); g_pd3dDevice->SetRenderState( D3DRS_BLENDOP, D3DBLENDOP_ADD );
g_pd3dDevice->SetRenderState( D3DRS_ALPHATESTENABLE, false ); g_pd3dDevice->SetRenderState( D3DRS_ALPHATESTENABLE, false );
g_pd3dDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA ); g_pd3dDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );
g_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA ); g_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );
g_pd3dDevice->SetRenderState( D3DRS_SCISSORTESTENABLE, true ); g_pd3dDevice->SetRenderState( D3DRS_SCISSORTESTENABLE, true );
// Setup texture // Setup texture
g_pd3dDevice->SetTexture( 0, g_pTexture ); g_pd3dDevice->SetTexture( 0, g_pTexture );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE ); g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE ); g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE ); g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE ); g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE ); g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE ); g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
// Setup orthographic projection matrix // Setup orthographic projection matrix
D3DXMATRIXA16 mat; D3DXMATRIXA16 mat;
D3DXMatrixIdentity(&mat); D3DXMatrixIdentity(&mat);
g_pd3dDevice->SetTransform(D3DTS_WORLD, &mat); g_pd3dDevice->SetTransform(D3DTS_WORLD, &mat);
g_pd3dDevice->SetTransform(D3DTS_VIEW, &mat); g_pd3dDevice->SetTransform(D3DTS_VIEW, &mat);
D3DXMatrixOrthoOffCenterLH(&mat, 0.0f, ImGui::GetIO().DisplaySize.x, ImGui::GetIO().DisplaySize.y, 0.0f, -1.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);
g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &mat); g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &mat);
// Render command lists // Render command lists
int vtx_offset = 0; int vtx_offset = 0;
for (int n = 0; n < cmd_lists_count; n++) for (int n = 0; n < cmd_lists_count; n++)
{ {
// Render command list // Render command list
const ImDrawList* cmd_list = cmd_lists[n]; const ImDrawList* cmd_list = cmd_lists[n];
const ImDrawCmd* pcmd_end = cmd_list->commands.end(); const ImDrawCmd* pcmd_end = cmd_list->commands.end();
for (const ImDrawCmd* pcmd = cmd_list->commands.begin(); pcmd != pcmd_end; pcmd++) for (const ImDrawCmd* pcmd = cmd_list->commands.begin(); pcmd != pcmd_end; pcmd++)
{ {
const RECT r = { (LONG)pcmd->clip_rect.x, (LONG)pcmd->clip_rect.y, (LONG)pcmd->clip_rect.z, (LONG)pcmd->clip_rect.w }; const RECT r = { (LONG)pcmd->clip_rect.x, (LONG)pcmd->clip_rect.y, (LONG)pcmd->clip_rect.z, (LONG)pcmd->clip_rect.w };
g_pd3dDevice->SetScissorRect(&r); g_pd3dDevice->SetScissorRect(&r);
g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, vtx_offset, pcmd->vtx_count/3); g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, vtx_offset, pcmd->vtx_count/3);
vtx_offset += pcmd->vtx_count; vtx_offset += pcmd->vtx_count;
} }
} }
} }
HRESULT InitD3D(HWND hWnd) HRESULT InitD3D(HWND hWnd)
@ -111,7 +114,7 @@ HRESULT InitD3D(HWND hWnd)
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
d3dpp.EnableAutoDepthStencil = TRUE; d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16; d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
// Create the D3DDevice // Create the D3DDevice
if (g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice) < 0) if (g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice) < 0)
@ -134,90 +137,90 @@ void Cleanup()
LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{ {
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
switch (msg) switch (msg)
{ {
case WM_LBUTTONDOWN: case WM_LBUTTONDOWN:
io.MouseDown[0] = true; io.MouseDown[0] = true;
return true; return true;
case WM_LBUTTONUP: case WM_LBUTTONUP:
io.MouseDown[0] = false; io.MouseDown[0] = false;
return true; return true;
case WM_RBUTTONDOWN: case WM_RBUTTONDOWN:
io.MouseDown[1] = true; io.MouseDown[1] = true;
return true; return true;
case WM_RBUTTONUP: case WM_RBUTTONUP:
io.MouseDown[1] = false; io.MouseDown[1] = false;
return true; return true;
case WM_MOUSEWHEEL: case WM_MOUSEWHEEL:
// Mouse wheel: -1,0,+1 // Mouse wheel: -1,0,+1
io.MouseWheel = GET_WHEEL_DELTA_WPARAM(wParam) > 0 ? +1 : -1; io.MouseWheel = GET_WHEEL_DELTA_WPARAM(wParam) > 0 ? +1 : -1;
return true; return true;
case WM_MOUSEMOVE: case WM_MOUSEMOVE:
// Mouse position, in pixels (set to -1,-1 if no mouse / on another screen, etc.) // Mouse position, in pixels (set to -1,-1 if no mouse / on another screen, etc.)
io.MousePos.x = (signed short)(lParam); io.MousePos.x = (signed short)(lParam);
io.MousePos.y = (signed short)(lParam >> 16); io.MousePos.y = (signed short)(lParam >> 16);
return true; return true;
case WM_CHAR: case WM_CHAR:
// You can also use ToAscii()+GetKeyboardState() to retrieve characters. // You can also use ToAscii()+GetKeyboardState() to retrieve characters.
if (wParam > 1 && wParam < 256) if (wParam > 1 && wParam < 256)
io.AddInputCharacter((char)wParam); io.AddInputCharacter((char)wParam);
return true; return true;
case WM_DESTROY: case WM_DESTROY:
{ {
Cleanup(); Cleanup();
PostQuitMessage(0); PostQuitMessage(0);
return 0; return 0;
} }
} }
return DefWindowProc(hWnd, msg, wParam, lParam); return DefWindowProc(hWnd, msg, wParam, lParam);
} }
void InitImGui() void InitImGui()
{ {
RECT rect; RECT rect;
GetClientRect(hWnd, &rect); GetClientRect(hWnd, &rect);
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
io.DisplaySize = ImVec2((float)(rect.right - rect.left), (float)(rect.bottom - rect.top)); // Display size, in pixels. For clamping windows positions. io.DisplaySize = ImVec2((float)(rect.right - rect.left), (float)(rect.bottom - rect.top)); // Display size, in pixels. For clamping windows positions.
io.DeltaTime = 1.0f/60.0f; // Time elapsed since last frame, in seconds (in this sample app we'll override this every frame because our timestep is variable) io.DeltaTime = 1.0f/60.0f; // Time elapsed since last frame, in seconds (in this sample app we'll override this every frame because our timestep is variable)
io.PixelCenterOffset = 0.0f; // Align Direct3D Texels io.PixelCenterOffset = 0.0f; // Align Direct3D Texels
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_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_LeftArrow] = VK_LEFT;
io.KeyMap[ImGuiKey_RightArrow] = VK_RIGHT; io.KeyMap[ImGuiKey_RightArrow] = VK_RIGHT;
io.KeyMap[ImGuiKey_UpArrow] = VK_UP; io.KeyMap[ImGuiKey_UpArrow] = VK_UP;
io.KeyMap[ImGuiKey_DownArrow] = VK_UP; io.KeyMap[ImGuiKey_DownArrow] = VK_UP;
io.KeyMap[ImGuiKey_Home] = VK_HOME; io.KeyMap[ImGuiKey_Home] = VK_HOME;
io.KeyMap[ImGuiKey_End] = VK_END; io.KeyMap[ImGuiKey_End] = VK_END;
io.KeyMap[ImGuiKey_Delete] = VK_DELETE; io.KeyMap[ImGuiKey_Delete] = VK_DELETE;
io.KeyMap[ImGuiKey_Backspace] = VK_BACK; io.KeyMap[ImGuiKey_Backspace] = VK_BACK;
io.KeyMap[ImGuiKey_Enter] = VK_RETURN; io.KeyMap[ImGuiKey_Enter] = VK_RETURN;
io.KeyMap[ImGuiKey_Escape] = VK_ESCAPE; io.KeyMap[ImGuiKey_Escape] = VK_ESCAPE;
io.KeyMap[ImGuiKey_A] = 'A'; io.KeyMap[ImGuiKey_A] = 'A';
io.KeyMap[ImGuiKey_C] = 'C'; io.KeyMap[ImGuiKey_C] = 'C';
io.KeyMap[ImGuiKey_V] = 'V'; io.KeyMap[ImGuiKey_V] = 'V';
io.KeyMap[ImGuiKey_X] = 'X'; io.KeyMap[ImGuiKey_X] = 'X';
io.KeyMap[ImGuiKey_Y] = 'Y'; io.KeyMap[ImGuiKey_Y] = 'Y';
io.KeyMap[ImGuiKey_Z] = 'Z'; io.KeyMap[ImGuiKey_Z] = 'Z';
io.RenderDrawListsFn = ImImpl_RenderDrawLists; io.RenderDrawListsFn = ImImpl_RenderDrawLists;
// Create the vertex buffer // Create the vertex buffer
if (g_pd3dDevice->CreateVertexBuffer(10000 * sizeof(CUSTOMVERTEX), D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVB, NULL) < 0) if (g_pd3dDevice->CreateVertexBuffer(10000 * sizeof(CUSTOMVERTEX), D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVB, NULL) < 0)
{ {
IM_ASSERT(0); IM_ASSERT(0);
return; return;
} }
// Load font texture // Load font texture
const void* png_data; const void* png_data;
unsigned int png_size; unsigned int png_size;
ImGui::GetDefaultFontData(NULL, NULL, &png_data, &png_size); ImGui::GetDefaultFontData(NULL, NULL, &png_data, &png_size);
if (D3DXCreateTextureFromFileInMemory(g_pd3dDevice, png_data, png_size, &g_pTexture) < 0) if (D3DXCreateTextureFromFileInMemory(g_pd3dDevice, png_data, png_size, &g_pTexture) < 0)
{ {
IM_ASSERT(0); IM_ASSERT(0);
return; return;
} }
} }
INT64 ticks_per_second = 0; INT64 ticks_per_second = 0;
@ -225,28 +228,28 @@ INT64 time = 0;
void UpdateImGui() void UpdateImGui()
{ {
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
// Setup timestep // Setup timestep
INT64 current_time; INT64 current_time;
QueryPerformanceCounter((LARGE_INTEGER *)&current_time); QueryPerformanceCounter((LARGE_INTEGER *)&current_time);
io.DeltaTime = (float)(current_time - time) / ticks_per_second; io.DeltaTime = (float)(current_time - time) / ticks_per_second;
time = current_time; time = current_time;
// Setup inputs // Setup inputs
// (we already got mouse position, buttons, wheel from the window message callback) // (we already got mouse position, buttons, wheel from the window message callback)
BYTE keystate[256]; BYTE keystate[256];
GetKeyboardState(keystate); GetKeyboardState(keystate);
for (int i = 0; i < 256; i++) for (int i = 0; i < 256; i++)
io.KeysDown[i] = (keystate[i] & 0x80) != 0; io.KeysDown[i] = (keystate[i] & 0x80) != 0;
io.KeyCtrl = (keystate[VK_CONTROL] & 0x80) != 0; io.KeyCtrl = (keystate[VK_CONTROL] & 0x80) != 0;
io.KeyShift = (keystate[VK_SHIFT] & 0x80) != 0; io.KeyShift = (keystate[VK_SHIFT] & 0x80) != 0;
// io.MousePos : filled by WM_MOUSEMOVE event // io.MousePos : filled by WM_MOUSEMOVE event
// io.MouseDown : filled by WM_*BUTTON* events // io.MouseDown : filled by WM_*BUTTON* events
// io.MouseWheel : filled by WM_MOUSEWHEEL events // io.MouseWheel : filled by WM_MOUSEWHEEL events
// Start the frame // Start the frame
ImGui::NewFrame(); ImGui::NewFrame();
} }
int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE, LPWSTR, int) int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE, LPWSTR, int)
@ -258,25 +261,25 @@ int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE, LPWSTR, int)
// Create the application's window // Create the application's window
hWnd = CreateWindow(L"ImGui Example", L"ImGui DirectX9 Example", WS_OVERLAPPEDWINDOW, 100, 100, 1280, 800, NULL, NULL, wc.hInstance, NULL); hWnd = CreateWindow(L"ImGui Example", L"ImGui DirectX9 Example", WS_OVERLAPPEDWINDOW, 100, 100, 1280, 800, NULL, NULL, wc.hInstance, NULL);
if (!QueryPerformanceFrequency((LARGE_INTEGER *)&ticks_per_second)) if (!QueryPerformanceFrequency((LARGE_INTEGER *)&ticks_per_second))
return 1; return 1;
if (!QueryPerformanceCounter((LARGE_INTEGER *)&time)) if (!QueryPerformanceCounter((LARGE_INTEGER *)&time))
return 1; return 1;
// Initialize Direct3D // Initialize Direct3D
if (InitD3D(hWnd) < 0) if (InitD3D(hWnd) < 0)
{ {
if (g_pVB) if (g_pVB)
g_pVB->Release(); g_pVB->Release();
UnregisterClass(L"ImGui Example", wc.hInstance); UnregisterClass(L"ImGui Example", wc.hInstance);
return 1; return 1;
} }
// Show the window // Show the window
ShowWindow(hWnd, SW_SHOWDEFAULT); ShowWindow(hWnd, SW_SHOWDEFAULT);
UpdateWindow(hWnd); UpdateWindow(hWnd);
InitImGui(); InitImGui();
// Enter the message loop // Enter the message loop
MSG msg; MSG msg;
@ -287,65 +290,65 @@ int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE, LPWSTR, int)
{ {
TranslateMessage(&msg); TranslateMessage(&msg);
DispatchMessage(&msg); DispatchMessage(&msg);
continue; continue;
} }
UpdateImGui(); UpdateImGui();
// Create a simple window // Create a simple window
// Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets appears in a window automatically called "Debug" // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets appears in a window automatically called "Debug"
static bool show_test_window = true; static bool show_test_window = true;
static bool show_another_window = false; static bool show_another_window = false;
static float f; static float f;
ImGui::Text("Hello, world!"); ImGui::Text("Hello, world!");
ImGui::SliderFloat("float", &f, 0.0f, 1.0f); ImGui::SliderFloat("float", &f, 0.0f, 1.0f);
show_test_window ^= ImGui::Button("Test Window"); show_test_window ^= ImGui::Button("Test Window");
show_another_window ^= ImGui::Button("Another Window"); show_another_window ^= ImGui::Button("Another Window");
// Calculate and show framerate // Calculate and show framerate
static float ms_per_frame[120] = { 0 }; static float ms_per_frame[120] = { 0 };
static int ms_per_frame_idx = 0; static int ms_per_frame_idx = 0;
static float ms_per_frame_accum = 0.0f; static float ms_per_frame_accum = 0.0f;
ms_per_frame_accum -= ms_per_frame[ms_per_frame_idx]; ms_per_frame_accum -= ms_per_frame[ms_per_frame_idx];
ms_per_frame[ms_per_frame_idx] = ImGui::GetIO().DeltaTime * 1000.0f; ms_per_frame[ms_per_frame_idx] = ImGui::GetIO().DeltaTime * 1000.0f;
ms_per_frame_accum += ms_per_frame[ms_per_frame_idx]; ms_per_frame_accum += ms_per_frame[ms_per_frame_idx];
ms_per_frame_idx = (ms_per_frame_idx + 1) % 120; ms_per_frame_idx = (ms_per_frame_idx + 1) % 120;
const float ms_per_frame_avg = ms_per_frame_accum / 120; const float ms_per_frame_avg = ms_per_frame_accum / 120;
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", ms_per_frame_avg, 1000.0f / ms_per_frame_avg); ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", ms_per_frame_avg, 1000.0f / ms_per_frame_avg);
// Show the ImGui test window // Show the ImGui test window
// Most of user example code is in ImGui::ShowTestWindow() // Most of user example code is in ImGui::ShowTestWindow()
if (show_test_window) if (show_test_window)
{ {
ImGui::SetNewWindowDefaultPos(ImVec2(650, 20)); // Normally user code doesn't need/want to call it because positions are saved in .ini file anyway. Here we just want to make the demo initial state a bit more friendly! ImGui::SetNewWindowDefaultPos(ImVec2(650, 20)); // Normally user code doesn't need/want to call it because positions are saved in .ini file anyway. Here we just want to make the demo initial state a bit more friendly!
ImGui::ShowTestWindow(&show_test_window); ImGui::ShowTestWindow(&show_test_window);
} }
// Show another simple window // Show another simple window
if (show_another_window) if (show_another_window)
{ {
ImGui::Begin("Another Window", &show_another_window, ImVec2(200,100)); ImGui::Begin("Another Window", &show_another_window, ImVec2(200,100));
ImGui::Text("Hello"); ImGui::Text("Hello");
ImGui::End(); ImGui::End();
} }
// Rendering // Rendering
g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, false); g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, false);
g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, false); g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
g_pd3dDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, false); g_pd3dDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, false);
g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(204, 153, 153), 1.0f, 0); g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(204, 153, 153), 1.0f, 0);
if (g_pd3dDevice->BeginScene() >= 0) if (g_pd3dDevice->BeginScene() >= 0)
{ {
ImGui::Render(); ImGui::Render();
g_pd3dDevice->EndScene(); g_pd3dDevice->EndScene();
} }
g_pd3dDevice->Present(NULL, NULL, NULL, NULL); g_pd3dDevice->Present(NULL, NULL, NULL, NULL);
} }
ImGui::Shutdown(); ImGui::Shutdown();
if (g_pVB) if (g_pVB)
g_pVB->Release(); g_pVB->Release();
UnregisterClass(L"ImGui Example", wc.hInstance); UnregisterClass(L"ImGui Example", wc.hInstance);
return 0; return 0;

View File

@ -2,267 +2,273 @@
#include <GL/glew.h> #include <GL/glew.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h" // for .png loading #include "stb_image.h" // for .png loading
#include "../../imgui.h" #include "../../imgui.h"
#ifdef _MSC_VER #ifdef _MSC_VER
#pragma warning (disable: 4996) // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vsnprintf, sscanf, fopen #pragma warning (disable: 4996) // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vsnprintf, sscanf, fopen
#endif #endif
static GLFWwindow* window; static GLFWwindow* window;
static GLuint fontTex; static GLuint fontTex;
// This is the main rendering function that you have to implement and provide to ImGui (via setting up 'RenderDrawListsFn' in the ImGuiIO structuer) // This is the main rendering function that you have to implement and provide to ImGui (via setting up 'RenderDrawListsFn' in the ImGuiIO structure)
// We are using the fixed pipeline. // If text or lines are blurry when integrating ImGui in your engine:
// A faster way would be to collate all vertices from all cmd_lists into a single vertex buffer // - in your Render function, try translating your projection matrix by (0.5f,0.5f) or (0.375f,0.375f)
// - try adjusting ImGui::GetIO().PixelCenterOffset to 0.5f or 0.375f
static void ImImpl_RenderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_count) static void ImImpl_RenderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_count)
{ {
if (cmd_lists_count == 0) if (cmd_lists_count == 0)
return; return;
// Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled, vertex/texcoord/color pointers. // We are using the OpenGL fixed pipeline to make the example code simpler to read!
glEnable(GL_BLEND); // A probable faster way to render would be to collate all vertices from all cmd_lists into a single vertex buffer.
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled, vertex/texcoord/color pointers.
glDisable(GL_CULL_FACE); glEnable(GL_BLEND);
glDisable(GL_DEPTH_TEST); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_SCISSOR_TEST); glDisable(GL_CULL_FACE);
glEnableClientState(GL_VERTEX_ARRAY); glDisable(GL_DEPTH_TEST);
glEnableClientState(GL_TEXTURE_COORD_ARRAY); glEnable(GL_SCISSOR_TEST);
glEnableClientState(GL_COLOR_ARRAY); glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
// Setup texture // Setup texture
glBindTexture(GL_TEXTURE_2D, fontTex); glBindTexture(GL_TEXTURE_2D, fontTex);
glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);
// Setup orthographic projection matrix // Setup orthographic projection matrix
const float width = ImGui::GetIO().DisplaySize.x; const float width = ImGui::GetIO().DisplaySize.x;
const float height = ImGui::GetIO().DisplaySize.y; const float height = ImGui::GetIO().DisplaySize.y;
glMatrixMode(GL_PROJECTION); glMatrixMode(GL_PROJECTION);
glLoadIdentity(); glLoadIdentity();
glOrtho(0.0f, width, height, 0.0f, -1.0f, +1.0f); glOrtho(0.0f, width, height, 0.0f, -1.0f, +1.0f);
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); glLoadIdentity();
// Render command lists // Render command lists
for (int n = 0; n < cmd_lists_count; n++) for (int n = 0; n < cmd_lists_count; n++)
{ {
const ImDrawList* cmd_list = cmd_lists[n]; const ImDrawList* cmd_list = cmd_lists[n];
const unsigned char* vtx_buffer = (const unsigned char*)cmd_list->vtx_buffer.begin(); const unsigned char* vtx_buffer = (const unsigned char*)cmd_list->vtx_buffer.begin();
glVertexPointer(2, GL_FLOAT, sizeof(ImDrawVert), (void*)(vtx_buffer)); glVertexPointer(2, GL_FLOAT, sizeof(ImDrawVert), (void*)(vtx_buffer));
glTexCoordPointer(2, GL_FLOAT, sizeof(ImDrawVert), (void*)(vtx_buffer+8)); glTexCoordPointer(2, GL_FLOAT, sizeof(ImDrawVert), (void*)(vtx_buffer+8));
glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(ImDrawVert), (void*)(vtx_buffer+16)); glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(ImDrawVert), (void*)(vtx_buffer+16));
int vtx_offset = 0; int vtx_offset = 0;
const ImDrawCmd* pcmd_end = cmd_list->commands.end(); const ImDrawCmd* pcmd_end = cmd_list->commands.end();
for (const ImDrawCmd* pcmd = cmd_list->commands.begin(); pcmd != pcmd_end; pcmd++) for (const ImDrawCmd* pcmd = cmd_list->commands.begin(); pcmd != pcmd_end; pcmd++)
{ {
glScissor((int)pcmd->clip_rect.x, (int)(height - pcmd->clip_rect.w), (int)(pcmd->clip_rect.z - pcmd->clip_rect.x), (int)(pcmd->clip_rect.w - pcmd->clip_rect.y)); glScissor((int)pcmd->clip_rect.x, (int)(height - pcmd->clip_rect.w), (int)(pcmd->clip_rect.z - pcmd->clip_rect.x), (int)(pcmd->clip_rect.w - pcmd->clip_rect.y));
glDrawArrays(GL_TRIANGLES, vtx_offset, pcmd->vtx_count); glDrawArrays(GL_TRIANGLES, vtx_offset, pcmd->vtx_count);
vtx_offset += pcmd->vtx_count; vtx_offset += pcmd->vtx_count;
} }
} }
glDisable(GL_SCISSOR_TEST); glDisable(GL_SCISSOR_TEST);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
} }
static const char* ImImpl_GetClipboardTextFn() static const char* ImImpl_GetClipboardTextFn()
{ {
return glfwGetClipboardString(window); return glfwGetClipboardString(window);
} }
static void ImImpl_SetClipboardTextFn(const char* text, const char* text_end) static void ImImpl_SetClipboardTextFn(const char* text, const char* text_end)
{ {
if (!text_end) if (!text_end)
text_end = text + strlen(text); text_end = text + strlen(text);
if (*text_end == 0) if (*text_end == 0)
{ {
// Already got a zero-terminator at 'text_end', we don't need to add one // Already got a zero-terminator at 'text_end', we don't need to add one
glfwSetClipboardString(window, text); glfwSetClipboardString(window, text);
} }
else else
{ {
// Add a zero-terminator because glfw function doesn't take a size // Add a zero-terminator because glfw function doesn't take a size
char* buf = (char*)malloc(text_end - text + 1); char* buf = (char*)malloc(text_end - text + 1);
memcpy(buf, text, text_end-text); memcpy(buf, text, text_end-text);
buf[text_end-text] = '\0'; buf[text_end-text] = '\0';
glfwSetClipboardString(window, buf); glfwSetClipboardString(window, buf);
free(buf); free(buf);
} }
} }
// GLFW callbacks to get events // GLFW callbacks to get events
static void glfw_error_callback(int error, const char* description) static void glfw_error_callback(int error, const char* description)
{ {
fputs(description, stderr); fputs(description, stderr);
} }
static void glfw_scroll_callback(GLFWwindow* window, double xoffset, double yoffset) static void glfw_scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{ {
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
io.MouseWheel = (yoffset != 0.0f) ? yoffset > 0.0f ? 1 : - 1 : 0; // Mouse wheel: -1,0,+1 io.MouseWheel = (yoffset != 0.0f) ? yoffset > 0.0f ? 1 : - 1 : 0; // Mouse wheel: -1,0,+1
} }
static void glfw_key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) static void glfw_key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{ {
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
if (action == GLFW_PRESS) if (action == GLFW_PRESS)
io.KeysDown[key] = true; io.KeysDown[key] = true;
if (action == GLFW_RELEASE) if (action == GLFW_RELEASE)
io.KeysDown[key] = false; io.KeysDown[key] = false;
io.KeyCtrl = (mods & GLFW_MOD_CONTROL) != 0; io.KeyCtrl = (mods & GLFW_MOD_CONTROL) != 0;
io.KeyShift = (mods & GLFW_MOD_SHIFT) != 0; io.KeyShift = (mods & GLFW_MOD_SHIFT) != 0;
} }
static void glfw_char_callback(GLFWwindow* window, unsigned int c) static void glfw_char_callback(GLFWwindow* window, unsigned int c)
{ {
if (c > 0 && c <= 255) if (c > 0 && c <= 255)
ImGui::GetIO().AddInputCharacter((char)c); ImGui::GetIO().AddInputCharacter((char)c);
} }
// OpenGL code based on http://open.gl tutorials // OpenGL code based on http://open.gl tutorials
void InitGL() void InitGL()
{ {
glfwSetErrorCallback(glfw_error_callback); glfwSetErrorCallback(glfw_error_callback);
if (!glfwInit()) if (!glfwInit())
exit(1); exit(1);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
window = glfwCreateWindow(1280, 720, "ImGui OpenGL example", NULL, NULL); window = glfwCreateWindow(1280, 720, "ImGui OpenGL example", NULL, NULL);
glfwMakeContextCurrent(window); glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, glfw_key_callback); glfwSetKeyCallback(window, glfw_key_callback);
glfwSetScrollCallback(window, glfw_scroll_callback); glfwSetScrollCallback(window, glfw_scroll_callback);
glfwSetCharCallback(window, glfw_char_callback); glfwSetCharCallback(window, glfw_char_callback);
glewInit(); glewInit();
} }
void InitImGui() void InitImGui()
{ {
int w, h; int w, h;
glfwGetWindowSize(window, &w, &h); glfwGetWindowSize(window, &w, &h);
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
io.DisplaySize = ImVec2((float)w, (float)h); // Display size, in pixels. For clamping windows positions. io.DisplaySize = ImVec2((float)w, (float)h); // Display size, in pixels. For clamping windows positions.
io.DeltaTime = 1.0f/60.0f; // Time elapsed since last frame, in seconds (in this sample app we'll override this every frame because our timestep is variable) io.DeltaTime = 1.0f/60.0f; // Time elapsed since last frame, in seconds (in this sample app we'll override this every frame because our timestep is variable)
io.PixelCenterOffset = 0.5f; // Align OpenGL texels io.PixelCenterOffset = 0.0f; // Align OpenGL texels
io.KeyMap[ImGuiKey_Tab] = GLFW_KEY_TAB; // Keyboard mapping. ImGui will use those indices to peek into the io.KeyDown[] array. io.KeyMap[ImGuiKey_Tab] = GLFW_KEY_TAB; // Keyboard mapping. ImGui will use those indices to peek into the io.KeyDown[] array.
io.KeyMap[ImGuiKey_LeftArrow] = GLFW_KEY_LEFT; io.KeyMap[ImGuiKey_LeftArrow] = GLFW_KEY_LEFT;
io.KeyMap[ImGuiKey_RightArrow] = GLFW_KEY_RIGHT; io.KeyMap[ImGuiKey_RightArrow] = GLFW_KEY_RIGHT;
io.KeyMap[ImGuiKey_UpArrow] = GLFW_KEY_UP; io.KeyMap[ImGuiKey_UpArrow] = GLFW_KEY_UP;
io.KeyMap[ImGuiKey_DownArrow] = GLFW_KEY_DOWN; io.KeyMap[ImGuiKey_DownArrow] = GLFW_KEY_DOWN;
io.KeyMap[ImGuiKey_Home] = GLFW_KEY_HOME; io.KeyMap[ImGuiKey_Home] = GLFW_KEY_HOME;
io.KeyMap[ImGuiKey_End] = GLFW_KEY_END; io.KeyMap[ImGuiKey_End] = GLFW_KEY_END;
io.KeyMap[ImGuiKey_Delete] = GLFW_KEY_DELETE; io.KeyMap[ImGuiKey_Delete] = GLFW_KEY_DELETE;
io.KeyMap[ImGuiKey_Backspace] = GLFW_KEY_BACKSPACE; io.KeyMap[ImGuiKey_Backspace] = GLFW_KEY_BACKSPACE;
io.KeyMap[ImGuiKey_Enter] = GLFW_KEY_ENTER; io.KeyMap[ImGuiKey_Enter] = GLFW_KEY_ENTER;
io.KeyMap[ImGuiKey_Escape] = GLFW_KEY_ESCAPE; io.KeyMap[ImGuiKey_Escape] = GLFW_KEY_ESCAPE;
io.KeyMap[ImGuiKey_A] = GLFW_KEY_A; io.KeyMap[ImGuiKey_A] = GLFW_KEY_A;
io.KeyMap[ImGuiKey_C] = GLFW_KEY_C; io.KeyMap[ImGuiKey_C] = GLFW_KEY_C;
io.KeyMap[ImGuiKey_V] = GLFW_KEY_V; io.KeyMap[ImGuiKey_V] = GLFW_KEY_V;
io.KeyMap[ImGuiKey_X] = GLFW_KEY_X; io.KeyMap[ImGuiKey_X] = GLFW_KEY_X;
io.KeyMap[ImGuiKey_Y] = GLFW_KEY_Y; io.KeyMap[ImGuiKey_Y] = GLFW_KEY_Y;
io.KeyMap[ImGuiKey_Z] = GLFW_KEY_Z; io.KeyMap[ImGuiKey_Z] = GLFW_KEY_Z;
io.RenderDrawListsFn = ImImpl_RenderDrawLists; io.RenderDrawListsFn = ImImpl_RenderDrawLists;
io.SetClipboardTextFn = ImImpl_SetClipboardTextFn; io.SetClipboardTextFn = ImImpl_SetClipboardTextFn;
io.GetClipboardTextFn = ImImpl_GetClipboardTextFn; io.GetClipboardTextFn = ImImpl_GetClipboardTextFn;
// Load font texture // Load font texture
glGenTextures(1, &fontTex); glGenTextures(1, &fontTex);
glBindTexture(GL_TEXTURE_2D, fontTex); glBindTexture(GL_TEXTURE_2D, fontTex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
const void* png_data; const void* png_data;
unsigned int png_size; unsigned int png_size;
ImGui::GetDefaultFontData(NULL, NULL, &png_data, &png_size); ImGui::GetDefaultFontData(NULL, NULL, &png_data, &png_size);
int tex_x, tex_y, tex_comp; int tex_x, tex_y, tex_comp;
void* tex_data = stbi_load_from_memory((const unsigned char*)png_data, (int)png_size, &tex_x, &tex_y, &tex_comp, 0); void* tex_data = stbi_load_from_memory((const unsigned char*)png_data, (int)png_size, &tex_x, &tex_y, &tex_comp, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_x, tex_y, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex_data); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_x, tex_y, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex_data);
stbi_image_free(tex_data); stbi_image_free(tex_data);
} }
void UpdateImGui() void UpdateImGui()
{ {
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
// Setup timestep // Setup timestep
static double time = 0.0f; static double time = 0.0f;
const double current_time = glfwGetTime(); const double current_time = glfwGetTime();
io.DeltaTime = (float)(current_time - time); io.DeltaTime = (float)(current_time - time);
time = current_time; time = current_time;
// Setup inputs // Setup inputs
// (we already got mouse wheel, keyboard keys & characters from glfw callbacks polled in glfwPollEvents()) // (we already got mouse wheel, keyboard keys & characters from glfw callbacks polled in glfwPollEvents())
double mouse_x, mouse_y; double mouse_x, mouse_y;
glfwGetCursorPos(window, &mouse_x, &mouse_y); glfwGetCursorPos(window, &mouse_x, &mouse_y);
io.MousePos = ImVec2((float)mouse_x, (float)mouse_y); // Mouse position, in pixels (set to -1,-1 if no mouse / on another screen, etc.) io.MousePos = ImVec2((float)mouse_x, (float)mouse_y); // Mouse position, in pixels (set to -1,-1 if no mouse / on another screen, etc.)
io.MouseDown[0] = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) != 0; io.MouseDown[0] = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) != 0;
io.MouseDown[1] = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT) != 0; io.MouseDown[1] = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT) != 0;
// Start the frame // Start the frame
ImGui::NewFrame(); ImGui::NewFrame();
} }
// Application code // Application code
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
InitGL(); InitGL();
InitImGui(); InitImGui();
while (!glfwWindowShouldClose(window)) while (!glfwWindowShouldClose(window))
{ {
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
io.MouseWheel = 0; io.MouseWheel = 0;
glfwPollEvents(); glfwPollEvents();
UpdateImGui(); UpdateImGui();
// Create a simple window // Create a simple window
// Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets appears in a window automatically called "Debug" // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets appears in a window automatically called "Debug"
static bool show_test_window = true; static bool show_test_window = true;
static bool show_another_window = false; static bool show_another_window = false;
static float f; static float f;
ImGui::Text("Hello, world!"); ImGui::Text("Hello, world!");
ImGui::SliderFloat("float", &f, 0.0f, 1.0f); ImGui::SliderFloat("float", &f, 0.0f, 1.0f);
show_test_window ^= ImGui::Button("Test Window"); show_test_window ^= ImGui::Button("Test Window");
show_another_window ^= ImGui::Button("Another Window"); show_another_window ^= ImGui::Button("Another Window");
// Calculate and show framerate // Calculate and show framerate
static float ms_per_frame[120] = { 0 }; static float ms_per_frame[120] = { 0 };
static int ms_per_frame_idx = 0; static int ms_per_frame_idx = 0;
static float ms_per_frame_accum = 0.0f; static float ms_per_frame_accum = 0.0f;
ms_per_frame_accum -= ms_per_frame[ms_per_frame_idx]; ms_per_frame_accum -= ms_per_frame[ms_per_frame_idx];
ms_per_frame[ms_per_frame_idx] = ImGui::GetIO().DeltaTime * 1000.0f; ms_per_frame[ms_per_frame_idx] = ImGui::GetIO().DeltaTime * 1000.0f;
ms_per_frame_accum += ms_per_frame[ms_per_frame_idx]; ms_per_frame_accum += ms_per_frame[ms_per_frame_idx];
ms_per_frame_idx = (ms_per_frame_idx + 1) % 120; ms_per_frame_idx = (ms_per_frame_idx + 1) % 120;
const float ms_per_frame_avg = ms_per_frame_accum / 120; const float ms_per_frame_avg = ms_per_frame_accum / 120;
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", ms_per_frame_avg, 1000.0f / ms_per_frame_avg); ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", ms_per_frame_avg, 1000.0f / ms_per_frame_avg);
// Show the ImGui test window // Show the ImGui test window
// Most of user example code is in ImGui::ShowTestWindow() // Most of user example code is in ImGui::ShowTestWindow()
if (show_test_window) if (show_test_window)
{ {
ImGui::SetNewWindowDefaultPos(ImVec2(650, 20)); // Normally user code doesn't need/want to call it because positions are saved in .ini file anyway. Here we just want to make the demo initial state a bit more friendly! ImGui::SetNewWindowDefaultPos(ImVec2(650, 20)); // Normally user code doesn't need/want to call it because positions are saved in .ini file anyway. Here we just want to make the demo initial state a bit more friendly!
ImGui::ShowTestWindow(&show_test_window); ImGui::ShowTestWindow(&show_test_window);
} }
// Show another simple window // Show another simple window
if (show_another_window) if (show_another_window)
{ {
ImGui::Begin("Another Window", &show_another_window, ImVec2(200,100)); ImGui::Begin("Another Window", &show_another_window, ImVec2(200,100));
ImGui::Text("Hello"); ImGui::Text("Hello");
ImGui::End(); ImGui::End();
} }
// Rendering // Rendering
glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y); glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y);
glClearColor(0.8f, 0.6f, 0.6f, 1.0f); glClearColor(0.8f, 0.6f, 0.6f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
ImGui::Render(); ImGui::Render();
glfwSwapBuffers(window); glfwSwapBuffers(window);
} }
ImGui::Shutdown(); ImGui::Shutdown();
glfwTerminate(); glfwTerminate();
return 0; return 0;
} }

BIN
extra_fonts/ProggyClean.zip Normal file

Binary file not shown.

BIN
extra_fonts/ProggySmall.zip Normal file

Binary file not shown.

71
extra_fonts/README.txt Normal file
View File

@ -0,0 +1,71 @@
Extra fonts for ImGui.
THOSE FONTS ARE OPTIONAL.
ImGui embeds a copy of 'proggy_clean' that you can use without any external files.
Export your own font with bmfont (www.angelcode.com/products/bmfont).
bmfont reads fonts (.ttf, .fon, etc.) and output a .fnt file and a texture file, e.g:
proggy_clean.fon --> [bmfont] ---> proggy_clean_13.fnt
proggy_clean_13.png
Configure bmfont:
- Export .fnt as Binary
- Tip: uncheck "Render from TrueType outline" and "Font Smoothing" for best result with non-anti-aliased type fonts.
But you can experiment with other settings if you want anti-aliased fonts.
(A) Use font data embedded in ImGui
// Access embedded font data
const void* fnt_data; // pointer to FNT data
unsigned fnt_size; // size of FNT data
const void* png_data; // pointer to PNG data
unsigned int png_size; // size of PNG data
ImGui::GetDefaultFontData(&fnt_data, &fnt_size, &png_data, &png_size);
1. Load the .FNT data from 'fnt_data' (NB: this is done for you by default if you don't do anything)
ImGuiIO& io = ImGui::GetIO();
io.Font = new ImBitmapFont();
io.Font->LoadFromMemory(fnt_data, fnt_size);
2. Load the .PNG data from 'png_data' into a texture
(B) Use fonts from external files
ImGuiIO& io = ImGui::GetIO();
1. Load the .FNT data, e.g.
// proggy_clean_13 [default]
io.Font->LoadFromFile("proggy_clean_13.fnt");
io.FontTexUvForWhite = ImVec2(0.0f/256.0f,0.0f/128);
io.FontYOffset = +1;
// proggy_small_12
io.Font = new ImBitmapFont();
io.Font->LoadFromFile("proggy_small_12.fnt");
io.FontTexUvForWhite = ImVec2(84.0f/256.0f,20.0f/64);
io.FontYOffset = +2;
// proggy_small_14
io.Font = new ImBitmapFont();
io.Font->LoadFromFile("proggy_small_14.fnt");
io.FontTexUvForWhite = ImVec2(84.0f/256.0f,20.0f/64);
io.FontYOffset = +3;
// courier_new_16
io.Font->LoadFromFile("courier_new_16.fnt");
io.FontTexUvForWhite = ImVec2(1.0f/256.0f,4.0f/128);
// courier_new_18
io.Font->LoadFromFile("courier_new_18.fnt");
io.FontTexUvForWhite = ImVec2(4.0f/256.0f,5.0f/256);
2. Load the matching .PNG data into a texture

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 949 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 949 B

View File

@ -4,30 +4,38 @@
#pragma once #pragma once
//---- Define your own malloc/free/realloc functions if you want to override internal memory allocations for ImGui
/*
#define IM_MALLOC(_SIZE) MyMalloc(_SIZE)
#define IM_FREE(_PTR) MyFree(_PTR)
#define IM_REALLOC(_PTR, _SIZE) MyRealloc(_PTR, _SIZE)
#include <stdlib.h> // size_t
void* MyMalloc(size_t size);
void MyFree(void *ptr);
void* MyRealloc(void *ptr, size_t size);
*/
//---- Define your own ImVector<> type if you don't want to use the provided implementation defined in imgui.h //---- Define your own ImVector<> type if you don't want to use the provided implementation defined in imgui.h
//#include <vector> //#include <vector>
//#define ImVector std::vector //#define ImVector std::vector
//#define ImVector MyVector //#define ImVector MyVector
//---- Define assertion handler. Defaults to calling assert(). //---- Define assertion handler. Defaults to calling assert().
//#define IM_ASSERT(_EXPR) MyAssert(_EXPR) //#define IM_ASSERT(_EXPR) MyAssert(_EXPR)
//---- Don't implement default clipboard handlers for Windows (so as not to link with OpenClipboard(), etc.) //---- Don't implement default clipboard handlers for Windows (so as not to link with OpenClipboard(), etc.)
//#define IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCS //#define IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCS
//---- If you are loading a custom font, ImGui expect to find a pure white pixel at (0,0)
// Change it's UV coordinate here if you can't have a white pixel at (0,0)
//#define IMGUI_FONT_TEX_UV_FOR_WHITE ImVec2(0.f/256.f,0.f/256.f)
//---- Define implicit cast operators to convert back<>forth from your math types and ImVec2/ImVec4. //---- Define implicit cast operators to convert back<>forth from your math types and ImVec2/ImVec4.
/* /*
#define IM_VEC2_CLASS_EXTRA \ #define IM_VEC2_CLASS_EXTRA \
ImVec2(const MyVec2& f) { x = f.x; y = f.y; } \ ImVec2(const MyVec2& f) { x = f.x; y = f.y; } \
operator MyVec2() const { return MyVec2(x,y); } operator MyVec2() const { return MyVec2(x,y); }
#define IM_VEC4_CLASS_EXTRA \ #define IM_VEC4_CLASS_EXTRA \
ImVec4(const MyVec4& f) { x = f.x; y = f.y; z = f.z; w = f.w; } \ ImVec4(const MyVec4& f) { x = f.x; y = f.y; z = f.z; w = f.w; } \
operator MyVec4() const { return MyVec4(x,y,z,w); } operator MyVec4() const { return MyVec4(x,y,z,w); }
*/ */
//---- Freely implement extra functions within the ImGui:: namespace. //---- Freely implement extra functions within the ImGui:: namespace.
@ -35,29 +43,8 @@
/* /*
namespace ImGui namespace ImGui
{ {
void Value(const char* prefix, const MyVec2& v, const char* float_format = NULL); void Value(const char* prefix, const MyVec2& v, const char* float_format = NULL);
void Value(const char* prefix, const MyVec4& v, const char* float_format = NULL); void Value(const char* prefix, const MyVec4& v, const char* float_format = NULL);
}; };
*/ */
//---- Define malloc/free/realloc functions to override internal memory allocations for ImGui
/*
* #define IM_MALLOC imgui_malloc
* #define IM_FREE imgui_free
* #define IM_REALLOC imgui_realloc
*
* void* imgui_malloc(size_t size);
* void imgui_free(void *ptr);
* void* imgui_realloc(void *ptr, size_t size);
*/
#ifndef IM_MALLOC
#define IM_MALLOC malloc
#endif
#ifndef IM_FREE
#define IM_FREE free
#endif
#ifndef IM_REALLOC
#define IM_REALLOC realloc
#endif

8552
imgui.cpp

File diff suppressed because it is too large Load Diff

919
imgui.h

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,4 @@
// stb_textedit.h - v1.3 - public domain - Sean Barrett // stb_textedit.h - v1.4 - public domain - Sean Barrett
// Development of this library was sponsored by RAD Game Tools // Development of this library was sponsored by RAD Game Tools
// //
// This C header file implements the guts of a multi-line text-editing // This C header file implements the guts of a multi-line text-editing
@ -30,8 +30,9 @@
// //
// VERSION HISTORY // VERSION HISTORY
// //
// 1.3 (2013-06-19) fix mouse clicking to round to nearest char boundary // 1.4 (2014-08-17) fix signed/unsigned warnings
// 1.2 (2013-05-27) fix some RAD types that had crept into the new code // 1.3 (2014-06-19) fix mouse clicking to round to nearest char boundary
// 1.2 (2014-05-27) fix some RAD types that had crept into the new code
// 1.1 (2013-12-15) move-by-word (requires STB_TEXTEDIT_IS_SPACE ) // 1.1 (2013-12-15) move-by-word (requires STB_TEXTEDIT_IS_SPACE )
// 1.0 (2012-07-26) improve documentation, initial public release // 1.0 (2012-07-26) improve documentation, initial public release
// 0.3 (2012-02-24) bugfixes, single-line mode; insert mode // 0.3 (2012-02-24) bugfixes, single-line mode; insert mode
@ -41,7 +42,7 @@
// ADDITIONAL CONTRIBUTORS // ADDITIONAL CONTRIBUTORS
// //
// Ulf Winklemann: move-by-word in 1.1 // Ulf Winklemann: move-by-word in 1.1
// Scott Graham: mouse selectiom bugfix in 1.3 // Scott Graham: mouse selection bugfix in 1.3
// //
// USAGE // USAGE
// //
@ -445,7 +446,7 @@ static void stb_textedit_drag(STB_TEXTEDIT_STRING *str, STB_TexteditState *state
static void stb_text_undo(STB_TEXTEDIT_STRING *str, STB_TexteditState *state); static void stb_text_undo(STB_TEXTEDIT_STRING *str, STB_TexteditState *state);
static void stb_text_redo(STB_TEXTEDIT_STRING *str, STB_TexteditState *state); static void stb_text_redo(STB_TEXTEDIT_STRING *str, STB_TexteditState *state);
static void stb_text_makeundo_delete(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int length); static void stb_text_makeundo_delete(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int length);
static void stb_text_makeundo_insert(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int length); static void stb_text_makeundo_insert(STB_TexteditState *state, int where, int length);
static void stb_text_makeundo_replace(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int old_length, int new_length); static void stb_text_makeundo_replace(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int old_length, int new_length);
typedef struct typedef struct
@ -649,7 +650,7 @@ static int stb_textedit_paste(STB_TEXTEDIT_STRING *str, STB_TexteditState *state
stb_textedit_delete_selection(str,state); stb_textedit_delete_selection(str,state);
// try to insert the characters // try to insert the characters
if (STB_TEXTEDIT_INSERTCHARS(str, state->cursor, text, len)) { if (STB_TEXTEDIT_INSERTCHARS(str, state->cursor, text, len)) {
stb_text_makeundo_insert(str, state, state->cursor, len); stb_text_makeundo_insert(state, state->cursor, len);
state->cursor += len; state->cursor += len;
state->has_preferred_x = 0; state->has_preferred_x = 0;
return 1; return 1;
@ -684,7 +685,7 @@ retry:
} else { } else {
stb_textedit_delete_selection(str,state); // implicity clamps stb_textedit_delete_selection(str,state); // implicity clamps
if (STB_TEXTEDIT_INSERTCHARS(str, state->cursor, &ch, 1)) { if (STB_TEXTEDIT_INSERTCHARS(str, state->cursor, &ch, 1)) {
stb_text_makeundo_insert(str, state, state->cursor, 1); stb_text_makeundo_insert(state, state->cursor, 1);
++state->cursor; ++state->cursor;
state->has_preferred_x = 0; state->has_preferred_x = 0;
} }
@ -1007,13 +1008,13 @@ static void stb_textedit_discard_undo(StbUndoState *state)
int n = state->undo_rec[0].insert_length, i; int n = state->undo_rec[0].insert_length, i;
// delete n characters from all other records // delete n characters from all other records
state->undo_char_point = state->undo_char_point - (short) n; // vsnet05 state->undo_char_point = state->undo_char_point - (short) n; // vsnet05
memmove(state->undo_char, state->undo_char + n, state->undo_char_point*sizeof(STB_TEXTEDIT_CHARTYPE)); memmove(state->undo_char, state->undo_char + n, (size_t) (state->undo_char_point*sizeof(STB_TEXTEDIT_CHARTYPE)));
for (i=0; i < state->undo_point; ++i) for (i=0; i < state->undo_point; ++i)
if (state->undo_rec[i].char_storage >= 0) if (state->undo_rec[i].char_storage >= 0)
state->undo_rec[i].char_storage = state->undo_rec[i].char_storage - (short) n; // vsnet05 // @OPTIMIZE: get rid of char_storage and infer it state->undo_rec[i].char_storage = state->undo_rec[i].char_storage - (short) n; // vsnet05 // @OPTIMIZE: get rid of char_storage and infer it
} }
--state->undo_point; --state->undo_point;
memmove(state->undo_rec, state->undo_rec+1, state->undo_point*sizeof(state->undo_rec[0])); memmove(state->undo_rec, state->undo_rec+1, (size_t) (state->undo_point*sizeof(state->undo_rec[0])));
} }
} }
@ -1031,13 +1032,13 @@ static void stb_textedit_discard_redo(StbUndoState *state)
int n = state->undo_rec[k].insert_length, i; int n = state->undo_rec[k].insert_length, i;
// delete n characters from all other records // delete n characters from all other records
state->redo_char_point = state->redo_char_point + (short) n; // vsnet05 state->redo_char_point = state->redo_char_point + (short) n; // vsnet05
memmove(state->undo_char + state->redo_char_point, state->undo_char + state->redo_char_point-n, (STB_TEXTEDIT_UNDOSTATECOUNT - state->redo_char_point)*sizeof(STB_TEXTEDIT_CHARTYPE)); memmove(state->undo_char + state->redo_char_point, state->undo_char + state->redo_char_point-n, (size_t) ((STB_TEXTEDIT_UNDOSTATECOUNT - state->redo_char_point)*sizeof(STB_TEXTEDIT_CHARTYPE)));
for (i=state->redo_point; i < k; ++i) for (i=state->redo_point; i < k; ++i)
if (state->undo_rec[i].char_storage >= 0) if (state->undo_rec[i].char_storage >= 0)
state->undo_rec[i].char_storage = state->undo_rec[i].char_storage + (short) n; // vsnet05 state->undo_rec[i].char_storage = state->undo_rec[i].char_storage + (short) n; // vsnet05
} }
++state->redo_point; ++state->redo_point;
memmove(state->undo_rec + state->redo_point-1, state->undo_rec + state->redo_point, (STB_TEXTEDIT_UNDOSTATECOUNT - state->redo_point)*sizeof(state->undo_rec[0])); memmove(state->undo_rec + state->redo_point-1, state->undo_rec + state->redo_point, (size_t) ((STB_TEXTEDIT_UNDOSTATECOUNT - state->redo_point)*sizeof(state->undo_rec[0])));
} }
} }
@ -1203,7 +1204,7 @@ static void stb_text_redo(STB_TEXTEDIT_STRING *str, STB_TexteditState *state)
s->redo_point++; s->redo_point++;
} }
static void stb_text_makeundo_insert(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int length) static void stb_text_makeundo_insert(STB_TexteditState *state, int where, int length)
{ {
stb_text_createundo(&state->undostate, where, 0, length); stb_text_createundo(&state->undostate, where, 0, length);
} }

BIN
web/skinning_sample_01.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB