<commit_before>/*
 * Copyright 2014-2015 <NAME>. All rights reserved.
 * License: http://www.opensource.org/licenses/BSD-2-Clause
 */

#include <bgfx/bgfx.h>
#include <bx/allocator.h>
#include <bx/fpumath.h>
#include <bx/timer.h>
#include <ocornut-imgui/imgui.h>
#include <ocornut-imgui/imgui_wm.h>
#include "imgui.h"
#include "ocornut_imgui.h"
#include <stb/stb_image.c>

#if defined(SCI_NAMESPACE)
#	include "../entry/input.h"
#	include "scintilla.h"
#endif // defined(SCI_NAMESPACE)

#include "vs_ocornut_imgui.bin.h"
#include "fs_ocornut_imgui.bin.h"

class PlatformWindow : public ImGuiWM::PlatformWindow
{
	typedef ImGuiWM::PlatformWindow Super;

public:
	PlatformWindow(bool _mainWindow, bool _isDragWindow)
		: ImGuiWM::PlatformWindow(_mainWindow, _isDragWindow)
		, m_pos(0.0f, 0.0f)
		, m_size(0.0f, 0.0f)
		, m_drag(false)
	{
	}

	virtual ~PlatformWindow() BX_OVERRIDE
	{
	}

	virtual bool Init(ImGuiWM::PlatformWindow* /*_parent*/) BX_OVERRIDE
	{
		return true;
	}

	virtual const ImVec2& GetPosition() const BX_OVERRIDE
	{
		return m_pos;
	}

	virtual const ImVec2& GetSize() const BX_OVERRIDE
	{
		return m_size;
	}

	virtual void Show() BX_OVERRIDE
	{
	}

	virtual void Hide() BX_OVERRIDE
	{
	}

	virtual void SetSize(const ImVec2& _size) BX_OVERRIDE
	{
		m_size = _size;
	}

	virtual void SetPosition(const ImVec2& _pos) BX_OVERRIDE
	{
		m_pos = _pos;
	}

	virtual void SetTitle(const char* /*_title*/) BX_OVERRIDE
	{
	}

	virtual void PreUpdate() BX_OVERRIDE
	{
	}

	virtual void Paint() BX_OVERRIDE
	{
		if (!m_bIsDragWindow)
		{
			Super::Paint();
		}
	}

	virtual void Destroy() BX_OVERRIDE
	{
	}

	virtual void StartDrag() BX_OVERRIDE
	{
		m_drag = true;
	}

	virtual void StopDrag() BX_OVERRIDE
	{
		m_drag = false;
	}

	virtual bool IsDraging() BX_OVERRIDE
	{
		return m_drag;
	}

private:
	ImVec2 m_pos;
	ImVec2 m_size;
	bool m_drag;
};

class WindowManager : public ImGuiWM::WindowManager
{
	typedef ImGuiWM::WindowManager Super;

public:
	WindowManager()
	{
	}

	virtual ~WindowManager() BX_OVERRIDE
	{
	}

protected:
	virtual ImGuiWM::PlatformWindow* CreatePlatformWindow(bool _main, ImGuiWM::PlatformWindow* _parent, bool _isDragWindow) BX_OVERRIDE
	{
		PlatformWindow* window = new PlatformWindow(_main, _isDragWindow);
		window->Init(_parent);
		return static_cast<ImGuiWM::PlatformWindow*>(window);
	}

	virtual void LogFormatted(const char* _str) BX_OVERRIDE
	{
		BX_TRACE("%s", _str); BX_UNUSED(_str);
	}

	virtual void InternalRun() BX_OVERRIDE
	{
		PreUpdate();
		Update();
	}

	virtual ImVec2 GetCursorPos() BX_OVERRIDE
	{
		ImGuiIO& io = ImGui::GetIO();
		return io.MousePos;
	}

	virtual bool IsLeftClickDown() BX_OVERRIDE
	{
		ImGuiIO& io = ImGui::GetIO();
		return io.MouseDown[0];
	}
};

struct OcornutImguiContext
{
	static void* memAlloc(size_t _size);
	static void memFree(void* _ptr);
	static void renderDrawLists(ImDrawData* draw_data);

	void render(ImDrawData* draw_data)
	{
		const float width  = ImGui::GetIO().DisplaySize.x;
		const float height = ImGui::GetIO().DisplaySize.y;

		float ortho[16];
		bx::mtxOrtho(ortho, 0.0f, width, height, 0.0f, -1.0f, 1.0f);

		bgfx::setViewTransform(m_viewId, NULL, ortho);

		// Render command lists
		for (int32_t ii = 0; ii < draw_data->CmdListsCount; ++ii)
		{
			bgfx::TransientVertexBuffer tvb;
			bgfx::TransientIndexBuffer tib;

			const ImDrawList* cmd_list   = draw_data->CmdLists[ii];
			uint32_t vtx_size = (uint32_t)cmd_list->VtxBuffer.size();
			uint32_t idx_size = (uint32_t)cmd_list->IdxBuffer.size();

			if (!bgfx::checkAvailTransientVertexBuffer(vtx_size, m_decl) || !bgfx::checkAvailTransientIndexBuffer(idx_size) )
			{
				// not enough space in transient buffer just quit drawing the rest...
				break;
			}

			bgfx::allocTransientVertexBuffer(&tvb, vtx_size, m_decl);
			bgfx::allocTransientIndexBuffer(&tib, idx_size);

			ImDrawVert* verts = (ImDrawVert*)tvb.data;
			memcpy(verts, cmd_list->VtxBuffer.begin(), vtx_size * sizeof(ImDrawVert) );

			ImDrawIdx* indices = (ImDrawIdx*)tib.data;
			memcpy(indices, cmd_list->IdxBuffer.begin(), idx_size * sizeof(ImDrawIdx) );

			uint32_t elem_offset = 0;
			const ImDrawCmd* pcmd_begin = cmd_list->CmdBuffer.begin();
			const ImDrawCmd* pcmd_end   = cmd_list->CmdBuffer.end();
			for (const ImDrawCmd* pcmd = pcmd_begin; pcmd != pcmd_end; pcmd++)
			{
				if (pcmd->UserCallback)
				{
					pcmd->UserCallback(cmd_list, pcmd);
					elem_offset += pcmd->ElemCount;
					continue;
				}
				if (0 == pcmd->ElemCount)
				{
					continue;
				}

				bgfx::setState(0
					| BGFX_STATE_RGB_WRITE
					| BGFX_STATE_ALPHA_WRITE
					| BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
					| BGFX_STATE_MSAA
					);
				bgfx::setScissor(uint16_t(bx::fmax(pcmd->ClipRect.x, 0.0f) )
					, uint16_t(bx::fmax(pcmd->ClipRect.y, 0.0f) )
					, uint16_t(bx::fmin(pcmd->ClipRect.z, 65535.0f)-bx::fmax(pcmd->ClipRect.x, 0.0f) )
					, uint16_t(bx::fmin(pcmd->ClipRect.w, 65535.0f)-bx::fmax(pcmd->ClipRect.y, 0.0f) )
					);
				union { void* ptr; bgfx::TextureHandle handle; } texture = { pcmd->TextureId };

				bgfx::setTexture(0, s_tex, 0 != texture.handle.idx
					? texture.handle
					: m_texture
					);

				bgfx::setVertexBuffer(&tvb, 0, vtx_size);
				bgfx::setIndexBuffer(&tib, elem_offset, pcmd->ElemCount);
				bgfx::submit(m_viewId, m_program);

				elem_offset += pcmd->ElemCount;
			}
		}
	}

	void create(const void* _data, uint32_t _size, float _fontSize, bx::AllocatorI* _allocator)
	{
		m_viewId = 255;
		m_allocator = _allocator;
		m_lastScroll = 0;
		m_last = bx::getHPCounter();

		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;

#if defined(SCI_NAMESPACE)
		io.KeyMap[ImGuiKey_Tab]        = (int)entry::Key::Tab;
		io.KeyMap[ImGuiKey_LeftArrow]  = (int)entry::Key::Left;
		io.KeyMap[ImGuiKey_RightArrow] = (int)entry::Key::Right;
		io.KeyMap[ImGuiKey_UpArrow]    = (int)entry::Key::Up;
		io.KeyMap[ImGuiKey_DownArrow]  = (int)entry::Key::Down;
		io.KeyMap[ImGuiKey_Home]       = (int)entry::Key::Home;
		io.KeyMap[ImGuiKey_End]        = (int)entry::Key::End;
		io.KeyMap[ImGuiKey_Delete]     = (int)entry::Key::Delete;
		io.KeyMap[ImGuiKey_Backspace]  = (int)entry::Key::Backspace;
		io.KeyMap[ImGuiKey_Enter]      = (int)entry::Key::Return;
		io.KeyMap[ImGuiKey_Escape]     = (int)entry::Key::Esc;
		io.KeyMap[ImGuiKey_A]          = (int)entry::Key::KeyA;
		io.KeyMap[ImGuiKey_C]          = (int)entry::Key::KeyC;
		io.KeyMap[ImGuiKey_V]          = (int)entry::Key::KeyV;
		io.KeyMap[ImGuiKey_X]          = (int)entry::Key::KeyX;
		io.KeyMap[ImGuiKey_Y]          = (int)entry::Key::KeyY;
		io.KeyMap[ImGuiKey_Z]          = (int)entry::Key::KeyZ;
#endif // defined(SCI_NAMESPACE)

		const bgfx::Memory* vsmem;
		const bgfx::Memory* fsmem;

		switch (bgfx::getRendererType() )
		{
		case bgfx::RendererType::Direct3D9:
			vsmem = bgfx::makeRef(vs_ocornut_imgui_dx9, sizeof(vs_ocornut_imgui_dx9) );
			fsmem = bgfx::makeRef(fs_ocornut_imgui_dx9, sizeof(fs_ocornut_imgui_dx9) );
			break;

		case bgfx::RendererType::Direct3D11:
		case bgfx::RendererType::Direct3D12:
			vsmem = bgfx::makeRef(vs_ocornut_imgui_dx11, sizeof(vs_ocornut_imgui_dx11) );
			fsmem = bgfx::makeRef(fs_ocornut_imgui_dx11, sizeof(fs_ocornut_imgui_dx11) );
			break;

		case bgfx::RendererType::Metal:
			vsmem = bgfx::makeRef(vs_ocornut_imgui_mtl, sizeof(vs_ocornut_imgui_mtl) );
			fsmem = bgfx::makeRef(fs_ocornut_imgui_mtl, sizeof(fs_ocornut_imgui_mtl) );
			break;

		default:
			vsmem = bgfx::makeRef(vs_ocornut_imgui_glsl, sizeof(vs_ocornut_imgui_glsl) );
			fsmem = bgfx::makeRef(fs_ocornut_imgui_glsl, sizeof(fs_ocornut_imgui_glsl) );
			break;
		}

		bgfx::ShaderHandle vsh = bgfx::createShader(vsmem);
		bgfx::ShaderHandle fsh = bgfx::createShader(fsmem);
		m_program = bgfx::createProgram(vsh, fsh, true);

		m_decl
			.begin()
			.add(bgfx::Attrib::Position,  2, bgfx::AttribType::Float)
			.add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
			.add(bgfx::Attrib::Color0,    4, bgfx::AttribType::Uint8, true)
			.end();

		s_tex = bgfx::createUniform("s_tex", bgfx::UniformType::Int1);

		uint8_t* data;
		int32_t width;
		int32_t height;
		void* font = ImGui::MemAlloc(_size);
		memcpy(font, _data, _size);
		io.Fonts->AddFontFromMemoryTTF(font, _size, _fontSize);

		io.Fonts->GetTexDataAsRGBA32(&data, &width, &height);

		m_texture = bgfx::createTexture2D( (uint16_t)width
			, (uint16_t)height
			, 1
			, bgfx::TextureFormat::BGRA8
			, 0
			, bgfx::copy(data, width*height*4)
			);

		ImGuiStyle& style = ImGui::GetStyle();
		style.FrameRounding = 4.0f;

		m_wm = BX_NEW(m_allocator, WindowManager);
		m_wm->Init();
	}

	void destroy()
	{
		m_wm->Exit();
		BX_DELETE(m_allocator, m_wm);
		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, int32_t _scroll, int _width, int _height, char _inputChar, uint8_t _viewId)
	{
		m_viewId = _viewId;
		ImGuiIO& io = ImGui::GetIO();
		if (_inputChar < 0x7f)
		{
			io.AddInputCharacter(_inputChar); // ASCII or GTFO! :(
		}

		io.DisplaySize = ImVec2( (float)_width, (float)_height);

		const int64_t now = bx::getHPCounter();<commit_msg>Added imgui dock debug code.<commit_after>/*
 * Copyright 2014-2015 <NAME>. All rights reserved.
 * License: http://www.opensource.org/licenses/BSD-2-Clause
 */

#include <bgfx/bgfx.h>
#include <bx/allocator.h>
#include <bx/fpumath.h>
#include <bx/timer.h>
#include <ocornut-imgui/imgui.h>
#include <ocornut-imgui/imgui_wm.h>
#include "imgui.h"
#include "ocornut_imgui.h"
#include <stb/stb_image.c>

#if defined(SCI_NAMESPACE)
#	include "../entry/input.h"
#	include "scintilla.h"
#endif // defined(SCI_NAMESPACE)

#include "vs_ocornut_imgui.bin.h"
#include "fs_ocornut_imgui.bin.h"

class PlatformWindow : public ImGuiWM::PlatformWindow
{
	typedef ImGuiWM::PlatformWindow Super;

public:
	PlatformWindow(bool _mainWindow, bool _isDragWindow)
		: ImGuiWM::PlatformWindow(_mainWindow, _isDragWindow)
		, m_pos(0.0f, 0.0f)
		, m_size(0.0f, 0.0f)
		, m_drag(false)
	{
	}

	virtual ~PlatformWindow() BX_OVERRIDE
	{
	}

	virtual bool Init(ImGuiWM::PlatformWindow* /*_parent*/) BX_OVERRIDE
	{
		return true;
	}

	virtual const ImVec2& GetPosition() const BX_OVERRIDE
	{
		return m_pos;
	}

	virtual const ImVec2& GetSize() const BX_OVERRIDE
	{
		return m_size;
	}

	virtual void Show() BX_OVERRIDE
	{
	}

	virtual void Hide() BX_OVERRIDE
	{
	}

	virtual void SetSize(const ImVec2& _size) BX_OVERRIDE
	{
		m_size = _size;
	}

	virtual void SetPosition(const ImVec2& _pos) BX_OVERRIDE
	{
		m_pos = _pos;
	}

	virtual void SetTitle(const char* /*_title*/) BX_OVERRIDE
	{
	}

	virtual void PreUpdate() BX_OVERRIDE
	{
	}

	virtual void Paint() BX_OVERRIDE
	{
		if (!m_bIsDragWindow)
		{
			Super::Paint();
		}
	}

	virtual void Destroy() BX_OVERRIDE
	{
	}

	virtual void StartDrag() BX_OVERRIDE
	{
		m_drag = true;
	}

	virtual void StopDrag() BX_OVERRIDE
	{
		m_drag = false;
	}

	virtual bool IsDraging() BX_OVERRIDE
	{
		return m_drag;
	}

private:
	ImVec2 m_pos;
	ImVec2 m_size;
	bool m_drag;
};

class WindowManager : public ImGuiWM::WindowManager
{
	typedef ImGuiWM::WindowManager Super;

public:
	WindowManager()
	{
	}

	virtual ~WindowManager() BX_OVERRIDE
	{
	}

protected:
	virtual ImGuiWM::PlatformWindow* CreatePlatformWindow(bool _main, ImGuiWM::PlatformWindow* _parent, bool _isDragWindow) BX_OVERRIDE
	{
		PlatformWindow* window = new PlatformWindow(_main, _isDragWindow);
		window->Init(_parent);
		return static_cast<ImGuiWM::PlatformWindow*>(window);
	}

	virtual void LogFormatted(const char* _str) BX_OVERRIDE
	{
		BX_TRACE("%s", _str); BX_UNUSED(_str);
	}

	virtual void InternalRun() BX_OVERRIDE
	{
		PreUpdate();
		Update();
	}

	virtual ImVec2 GetCursorPos() BX_OVERRIDE
	{
		ImGuiIO& io = ImGui::GetIO();
		return io.MousePos;
	}

	virtual bool IsLeftClickDown() BX_OVERRIDE
	{
		ImGuiIO& io = ImGui::GetIO();
		return io.MouseDown[0];
	}
};

struct OcornutImguiContext
{
	static void* memAlloc(size_t _size);
	static void memFree(void* _ptr);
	static void renderDrawLists(ImDrawData* draw_data);

	void render(ImDrawData* draw_data)
	{
		const float width  = ImGui::GetIO().DisplaySize.x;
		const float height = ImGui::GetIO().DisplaySize.y;

		float ortho[16];
		bx::mtxOrtho(ortho, 0.0f, width, height, 0.0f, -1.0f, 1.0f);

		bgfx::setViewTransform(m_viewId, NULL, ortho);

		// Render command lists
		for (int32_t ii = 0; ii < draw_data->CmdListsCount; ++ii)
		{
			bgfx::TransientVertexBuffer tvb;
			bgfx::TransientIndexBuffer tib;

			const ImDrawList* cmd_list   = draw_data->CmdLists[ii];
			uint32_t vtx_size = (uint32_t)cmd_list->VtxBuffer.size();
			uint32_t idx_size = (uint32_t)cmd_list->IdxBuffer.size();

			if (!bgfx::checkAvailTransientVertexBuffer(vtx_size, m_decl) || !bgfx::checkAvailTransientIndexBuffer(idx_size) )
			{
				// not enough space in transient buffer just quit drawing the rest...
				break;
			}

			bgfx::allocTransientVertexBuffer(&tvb, vtx_size, m_decl);
			bgfx::allocTransientIndexBuffer(&tib, idx_size);

			ImDrawVert* verts = (ImDrawVert*)tvb.data;
			memcpy(verts, cmd_list->VtxBuffer.begin(), vtx_size * sizeof(ImDrawVert) );

			ImDrawIdx* indices = (ImDrawIdx*)tib.data;
			memcpy(indices, cmd_list->IdxBuffer.begin(), idx_size * sizeof(ImDrawIdx) );

			uint32_t elem_offset = 0;
			const ImDrawCmd* pcmd_begin = cmd_list->CmdBuffer.begin();
			const ImDrawCmd* pcmd_end   = cmd_list->CmdBuffer.end();
			for (const ImDrawCmd* pcmd = pcmd_begin; pcmd != pcmd_end; pcmd++)
			{
				if (pcmd->UserCallback)
				{
					pcmd->UserCallback(cmd_list, pcmd);
					elem_offset += pcmd->ElemCount;
					continue;
				}
				if (0 == pcmd->ElemCount)
				{
					continue;
				}

				bgfx::setState(0
					| BGFX_STATE_RGB_WRITE
					| BGFX_STATE_ALPHA_WRITE
					| BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
					| BGFX_STATE_MSAA
					);
				bgfx::setScissor(uint16_t(bx::fmax(pcmd->ClipRect.x, 0.0f) )
					, uint16_t(bx::fmax(pcmd->ClipRect.y, 0.0f) )
					, uint16_t(bx::fmin(pcmd->ClipRect.z, 65535.0f)-bx::fmax(pcmd->ClipRect.x, 0.0f) )
					, uint16_t(bx::fmin(pcmd->ClipRect.w, 65535.0f)-bx::fmax(pcmd->ClipRect.y, 0.0f) )
					);
				union { void* ptr; bgfx::TextureHandle handle; } texture = { pcmd->TextureId };

				bgfx::setTexture(0, s_tex, 0 != texture.handle.idx
					? texture.handle
					: m_texture
					);

				bgfx::setVertexBuffer(&tvb, 0, vtx_size);
				bgfx::setIndexBuffer(&tib, elem_offset, pcmd->ElemCount);
				bgfx::submit(m_viewId, m_program);

				elem_offset += pcmd->ElemCount;
			}
		}
	}

	void create(const void* _data, uint32_t _size, float _fontSize, bx::AllocatorI* _allocator)
	{
		m_viewId = 255;
		m_allocator = _allocator;
		m_lastScroll = 0;
		m_last = bx::getHPCounter();

		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;

#if defined(SCI_NAMESPACE)
		io.KeyMap[ImGuiKey_Tab]        = (int)entry::Key::Tab;
		io.KeyMap[ImGuiKey_LeftArrow]  = (int)entry::Key::Left;
		io.KeyMap[ImGuiKey_RightArrow] = (int)entry::Key::Right;
		io.KeyMap[ImGuiKey_UpArrow]    = (int)entry::Key::Up;
		io.KeyMap[ImGuiKey_DownArrow]  = (int)entry::Key::Down;
		io.KeyMap[ImGuiKey_Home]       = (int)entry::Key::Home;
		io.KeyMap[ImGuiKey_End]        = (int)entry::Key::End;
		io.KeyMap[ImGuiKey_Delete]     = (int)entry::Key::Delete;
		io.KeyMap[ImGuiKey_Backspace]  = (int)entry::Key::Backspace;
		io.KeyMap[ImGuiKey_Enter]      = (int)entry::Key::Return;
		io.KeyMap[ImGuiKey_Escape]     = (int)entry::Key::Esc;
		io.KeyMap[ImGuiKey_A]          = (int)entry::Key::KeyA;
		io.KeyMap[ImGuiKey_C]          = (int)entry::Key::KeyC;
		io.KeyMap[ImGuiKey_V]          = (int)entry::Key::KeyV;
		io.KeyMap[ImGuiKey_X]          = (int)entry::Key::KeyX;
		io.KeyMap[ImGuiKey_Y]          = (int)entry::Key::KeyY;
		io.KeyMap[ImGuiKey_Z]          = (int)entry::Key::KeyZ;
#endif // defined(SCI_NAMESPACE)

		const bgfx::Memory* vsmem;
		const bgfx::Memory* fsmem;

		switch (bgfx::getRendererType() )
		{
		case bgfx::RendererType::Direct3D9:
			vsmem = bgfx::makeRef(vs_ocornut_imgui_dx9, sizeof(vs_ocornut_imgui_dx9) );
			fsmem = bgfx::makeRef(fs_ocornut_imgui_dx9, sizeof(fs_ocornut_imgui_dx9) );
			break;

		case bgfx::RendererType::Direct3D11:
		case bgfx::RendererType::Direct3D12:
			vsmem = bgfx::makeRef(vs_ocornut_imgui_dx11, sizeof(vs_ocornut_imgui_dx11) );
			fsmem = bgfx::makeRef(fs_ocornut_imgui_dx11, sizeof(fs_ocornut_imgui_dx11) );
			break;

		case bgfx::RendererType::Metal:
			vsmem = bgfx::makeRef(vs_ocornut_imgui_mtl, sizeof(vs_ocornut_imgui_mtl) );
			fsmem = bgfx::makeRef(fs_ocornut_imgui_mtl, sizeof(fs_ocornut_imgui_mtl) );
			break;

		default:
			vsmem = bgfx::makeRef(vs_ocornut_imgui_glsl, sizeof(vs_ocornut_imgui_glsl) );
			fsmem = bgfx::makeRef(fs_ocornut_imgui_glsl, sizeof(fs_ocornut_imgui_glsl) );
			break;
		}

		bgfx::ShaderHandle vsh = bgfx::createShader(vsmem);
		bgfx::ShaderHandle fsh = bgfx::createShader(fsmem);
		m_program = bgfx::createProgram(vsh, fsh, true);

		m_decl
			.begin()
			.add(bgfx::Attrib::Position,  2, bgfx::AttribType::Float)
			.add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
			.add(bgfx::Attrib::Color0,    4, bgfx::AttribType::Uint8, true)
			.end();

		s_tex = bgfx::createUniform("s_tex", bgfx::UniformType::Int1);

		uint8_t* data;
		int32_t width;
		int32_t height;
		void* font = ImGui::MemAlloc(_size);
		memcpy(font, _data, _size);
		io.Fonts->AddFontFromMemoryTTF(font, _size, _fontSize);

		io.Fonts->GetTexDataAsRGBA32(&data, &width, &height);

		m_texture = bgfx::createTexture2D( (uint16_t)width
			, (uint16_t)height
			, 1
			, bgfx::TextureFormat::BGRA8
			, 0
			, bgfx::copy(data, width*height*4)
			);

		ImGuiStyle& style = ImGui::GetStyle();
		style.FrameRounding = 4.0f;

		m_wm = BX_NEW(m_allocator, WindowManager);
		m_wm->Init();

#if 0
		{
			class Window : public ImGuiWM::Window
			{
			public:
				Window(const char* _title)
					: ImGuiWM::Window()
				{
					SetTitle(_title);
				}

				virtual void OnGui() BX_OVERRIDE
				{
				}
			};

			Window* w0 = new Window("test");
			Window* w1 = new Window("abcd");
			Window* w2 = new Window("xyzw");

			m_wm->Dock(w0);
			m_wm->DockWith(w1, w0, ImGuiWM::E_DOCK_ORIENTATION_RIGHT);
			m_wm->DockWith(w2, w1, ImGuiWM::E_DOCK_ORIENTATION_BOTTOM);
		}
#endif // 0
	}

	void destroy()
	{
		m_wm->Exit();
		BX_DELETE(m_allocator, m_wm);
		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, int32_t _scroll, int _width, int _height, char _inputChar, uint8_t _viewId)
	{
		m_viewId = _viewId;
		ImGuiIO& io = ImGui::GetIO();
		if (_inputChar < 0x7f)
		{
			io.AddInputCharacter(_inputChar); // ASCII or GTFO! :(
		}

		io.DisplaySize = ImVec2( (float)_width, (float)_height);

		const int64_t now = bx::getHPCounter();
{'fraction_non_alphanumeric': 0.14214322514167954, 'fraction_numerical': 0.01710458526532715, 'mean_word_length': 4.3591938155715075, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 76, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '353804', 'n_tokens_mistral': 8926, 'n_tokens_neox': 7924, 'n_words': 1266}