Added custom allocator to imgui.

This commit is contained in:
Branimir Karadžić 2015-04-15 20:00:15 -07:00
parent 4210fb0d64
commit 5e5a0a8a9e
6 changed files with 73 additions and 35 deletions

View file

@ -21,6 +21,8 @@
//---- Don't implement default handlers for Windows (so as not to link with OpenClipboard() and others Win32 functions)
#define IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCS
#define IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCS
#define IMGUI_DISABLE_STB_RECT_PACK_IMPLEMENTATION
#define IMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION
//---- Include imgui_user.inl at the end of imgui.cpp so you can include code that extends ImGui using its private data/functions.
//#define IMGUI_INCLUDE_IMGUI_USER_INL

View file

@ -393,6 +393,8 @@ namespace IMGUI_STB_NAMESPACE
#ifndef IMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION
#define STBTT_STATIC
#define STB_TRUETYPE_IMPLEMENTATION
#else
#define STBTT_DEF extern
#endif
#include "stb_truetype.h"

View file

@ -69,34 +69,22 @@ static const int32_t SCROLL_AREA_PADDING = 6;
static const int32_t AREA_HEADER = 20;
static const float s_tabStops[4] = {150, 210, 270, 330};
// For a custom allocator, define this and implement imguiMalloc and imguiFree somewhere in the project.
#ifndef IMGUI_CONFIG_CUSTOM_ALLOCATOR
# define IMGUI_CONFIG_CUSTOM_ALLOCATOR 0
#endif // ENTRY_CONFIG_USE_TINYSTL
#if IMGUI_CONFIG_CUSTOM_ALLOCATOR
void* imguiMalloc(size_t size, void* /*_userptr*/);
void imguiFree(void* _ptr, void* /*_userptr*/);
#else
static void* imguiMalloc(size_t _size, void* /*_userptr*/)
{
return malloc(_size);
}
static void imguiFree(void* _ptr, void* /*_userptr*/)
{
free(_ptr);
}
#endif //IMGUI_CONFIG_CUSTOM_ALLOCATOR
void* imguiMalloc(size_t _size, void*);
void imguiFree(void* _ptr, void*);
#define IMGUI_MIN(_a, _b) (_a)<(_b)?(_a):(_b)
#define IMGUI_MAX(_a, _b) (_a)>(_b)?(_a):(_b)
#define IMGUI_CLAMP(_a, _min, _max) IMGUI_MIN(IMGUI_MAX(_a, _min), _max)
#define STBTT_malloc(_x, _y) imguiMalloc(_x, _y)
#define STBTT_free(_x, _y) imguiFree(_x, _y)
BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4505); // error C4505: '' : unreferenced local function has been removed
BX_PRAGMA_DIAGNOSTIC_PUSH();
#define STBTT_malloc(_size, _userData) imguiMalloc(_size, _userData)
#define STBTT_free(_ptr, _userData) imguiFree(_ptr, _userData)
#define STB_RECT_PACK_IMPLEMENTATION
#include <stb/stb_rect_pack.h>
#define STB_TRUETYPE_IMPLEMENTATION
#include <stb/stb_truetype.h>
BX_PRAGMA_DIAGNOSTIC_POP();
namespace
{
@ -461,15 +449,23 @@ struct Imgui
return bgfx::createTexture2D(uint16_t(_width), uint16_t(_height), 0, bgfx::TextureFormat::BGRA8, 0, mem);
}
ImguiFontHandle create(const void* _data, uint32_t _size, float _fontSize)
ImguiFontHandle create(const void* _data, uint32_t _size, float _fontSize, bx::AllocatorI* _allocator)
{
m_allocator = _allocator;
if (NULL == m_allocator)
{
static bx::CrtAllocator allocator;
m_allocator = &allocator;
}
if (NULL == _data)
{
_data = s_droidSansTtf;
_size = sizeof(s_droidSansTtf);
}
IMGUI_create(_data, _size, _fontSize);
IMGUI_create(_data, _size, _fontSize, _allocator);
m_nvg = nvgCreate(1, m_view);
nvgCreateFontMem(m_nvg, "default", (unsigned char*)_data, INT32_MAX, 0);
@ -3150,6 +3146,7 @@ struct Imgui
Ty m_ids[Max];
};
bx::AllocatorI* m_allocator;
int32_t m_mx;
int32_t m_my;
int32_t m_scroll;
@ -3222,9 +3219,19 @@ struct Imgui
static Imgui s_imgui;
ImguiFontHandle imguiCreate(const void* _data, uint32_t _size, float _fontSize)
void* imguiMalloc(size_t _size, void*)
{
return s_imgui.create(_data, _size, _fontSize);
return BX_ALLOC(s_imgui.m_allocator, _size);
}
void imguiFree(void* _ptr, void*)
{
BX_FREE(s_imgui.m_allocator, _ptr);
}
ImguiFontHandle imguiCreate(const void* _data, uint32_t _size, float _fontSize, bx::AllocatorI* _allocator)
{
return s_imgui.create(_data, _size, _fontSize, _allocator);
}
void imguiDestroy()

View file

@ -133,7 +133,9 @@ ImguiFontHandle imguiCreateFont(const void* _data, float _fontSize=15.0f);
void imguiSetFont(ImguiFontHandle _handle);
ImguiFontHandle imguiGetCurrentFont();
ImguiFontHandle imguiCreate(const void* _data = NULL, uint32_t _size = 0, float _fontSize = 15.0f);
namespace bx { struct AllocatorI; }
ImguiFontHandle imguiCreate(const void* _data = NULL, uint32_t _size = 0, float _fontSize = 15.0f, bx::AllocatorI* _allocator = NULL);
void imguiDestroy();
void imguiBeginFrame(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll, uint16_t _width, uint16_t _height, char _inputChar = 0, uint8_t _view = 255);

View file

@ -4,6 +4,7 @@
*/
#include <bgfx.h>
#include <bx/allocator.h>
#include <bx/fpumath.h>
#include <ocornut-imgui/imgui.h>
#include "imgui.h"
@ -13,10 +14,12 @@
#include "vs_ocornut_imgui.bin.h"
#include "fs_ocornut_imgui.bin.h"
static void imguiRender(ImDrawList** const _lists, int cmd_lists_count);
struct OcornutImguiContext
{
static void* memAlloc(size_t _size);
static void memFree(void* _ptr);
static void renderDrawLists(ImDrawList** const _lists, int _count);
void render(ImDrawList** const _lists, int _count)
{
const float width = ImGui::GetIO().DisplaySize.x;
@ -79,11 +82,18 @@ struct OcornutImguiContext
}
}
void create(const void* _data, uint32_t _size, float _fontSize)
void create(const void* _data, uint32_t _size, float _fontSize, bx::AllocatorI* _allocator)
{
m_viewId = 255;
m_allocator = _allocator;
ImGuiIO& io = ImGui::GetIO();
io.RenderDrawListsFn = renderDrawLists;
if (NULL != m_allocator)
{
io.MemAllocFn = memAlloc;
io.MemFreeFn = memFree;
}
io.DisplaySize = ImVec2(1280.0f, 720.0f);
io.DeltaTime = 1.0f / 60.0f;
io.IniFilename = NULL;
@ -143,15 +153,17 @@ struct OcornutImguiContext
ImGuiStyle& style = ImGui::GetStyle();
style.FrameRounding = 4.0f;
io.RenderDrawListsFn = imguiRender;
}
void destroy()
{
ImGui::Shutdown();
bgfx::destroyUniform(s_tex);
bgfx::destroyTexture(m_texture);
bgfx::destroyProgram(m_program);
m_allocator = NULL;
}
void beginFrame(int32_t _mx, int32_t _my, uint8_t _button, int _width, int _height, char _inputChar, uint8_t _viewId)
@ -174,6 +186,7 @@ struct OcornutImguiContext
ImGui::Render();
}
bx::AllocatorI* m_allocator;
bgfx::VertexDecl m_decl;
bgfx::ProgramHandle m_program;
bgfx::TextureHandle m_texture;
@ -183,14 +196,24 @@ struct OcornutImguiContext
static OcornutImguiContext s_ctx;
static void imguiRender(ImDrawList** const _lists, int _count)
void* OcornutImguiContext::memAlloc(size_t _size)
{
return BX_ALLOC(s_ctx.m_allocator, _size);
}
void OcornutImguiContext::memFree(void* _ptr)
{
BX_FREE(s_ctx.m_allocator, _ptr);
}
void OcornutImguiContext::renderDrawLists(ImDrawList** const _lists, int _count)
{
s_ctx.render(_lists, _count);
}
void IMGUI_create(const void* _data, uint32_t _size, float _fontSize)
void IMGUI_create(const void* _data, uint32_t _size, float _fontSize, bx::AllocatorI* _allocator)
{
s_ctx.create(_data, _size, _fontSize);
s_ctx.create(_data, _size, _fontSize, _allocator);
}
void IMGUI_destroy()

View file

@ -8,7 +8,9 @@
#include <ocornut-imgui/imgui.h>
void IMGUI_create(const void* _data, uint32_t _size, float _fontSize);
namespace bx { struct AllocatorI; }
void IMGUI_create(const void* _data, uint32_t _size, float _fontSize, bx::AllocatorI* _allocator);
void IMGUI_destroy();
void IMGUI_beginFrame(int32_t _mx, int32_t _my, uint8_t _button, int _width, int _height, char _inputChar, uint8_t _viewId);
void IMGUI_endFrame();