2
0

imgui.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. /*
  2. * Copyright 2014-2015 Daniel Collin. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  4. */
  5. #include <bgfx/bgfx.h>
  6. #include <bgfx/embedded_shader.h>
  7. #include <bx/allocator.h>
  8. #include <bx/math.h>
  9. #include <bx/timer.h>
  10. #include <dear-imgui/imgui.h>
  11. #include <dear-imgui/imgui_internal.h>
  12. #include "imgui.h"
  13. #include "../bgfx_utils.h"
  14. #ifndef USE_ENTRY
  15. # define USE_ENTRY 0
  16. #endif // USE_ENTRY
  17. #ifndef USE_LOCAL_STB
  18. # define USE_LOCAL_STB 1
  19. #endif // USE_LOCAL_STB
  20. #if USE_ENTRY
  21. # include "../entry/entry.h"
  22. # include "../entry/input.h"
  23. #endif // USE_ENTRY
  24. #include "vs_ocornut_imgui.bin.h"
  25. #include "fs_ocornut_imgui.bin.h"
  26. #include "vs_imgui_image.bin.h"
  27. #include "fs_imgui_image.bin.h"
  28. #include "roboto_regular.ttf.h"
  29. #include "robotomono_regular.ttf.h"
  30. #include "icons_kenney.ttf.h"
  31. #include "icons_font_awesome.ttf.h"
  32. static const bgfx::EmbeddedShader s_embeddedShaders[] =
  33. {
  34. BGFX_EMBEDDED_SHADER(vs_ocornut_imgui),
  35. BGFX_EMBEDDED_SHADER(fs_ocornut_imgui),
  36. BGFX_EMBEDDED_SHADER(vs_imgui_image),
  37. BGFX_EMBEDDED_SHADER(fs_imgui_image),
  38. BGFX_EMBEDDED_SHADER_END()
  39. };
  40. struct FontRangeMerge
  41. {
  42. const void* data;
  43. size_t size;
  44. ImWchar ranges[3];
  45. };
  46. static FontRangeMerge s_fontRangeMerge[] =
  47. {
  48. { s_iconsKenneyTtf, sizeof(s_iconsKenneyTtf), { ICON_MIN_KI, ICON_MAX_KI, 0 } },
  49. { s_iconsFontAwesomeTtf, sizeof(s_iconsFontAwesomeTtf), { ICON_MIN_FA, ICON_MAX_FA, 0 } },
  50. };
  51. static void* memAlloc(size_t _size, void* _userData);
  52. static void memFree(void* _ptr, void* _userData);
  53. struct OcornutImguiContext
  54. {
  55. void render(ImDrawData* _drawData)
  56. {
  57. if (NULL != _drawData->Textures)
  58. {
  59. for (ImTextureData* texData : *_drawData->Textures)
  60. {
  61. switch (texData->Status)
  62. {
  63. case ImTextureStatus_WantCreate:
  64. {
  65. ImGui::TextureBgfx tex =
  66. {
  67. .handle = bgfx::createTexture2D(
  68. (uint16_t)texData->Width
  69. , (uint16_t)texData->Height
  70. , false
  71. , 1
  72. , bgfx::TextureFormat::BGRA8
  73. , 0
  74. ),
  75. .flags = IMGUI_FLAGS_ALPHA_BLEND,
  76. .mip = 0,
  77. .unused = 0,
  78. };
  79. bgfx::setName(tex.handle, "ImGui Font Atlas");
  80. bgfx::updateTexture2D(tex.handle, 0, 0, 0, 0
  81. , bx::narrowCast<uint16_t>(texData->Width)
  82. , bx::narrowCast<uint16_t>(texData->Height)
  83. , bgfx::copy(texData->GetPixels(), texData->GetSizeInBytes() )
  84. );
  85. texData->SetTexID(bx::bitCast<ImTextureID>(tex) );
  86. texData->SetStatus(ImTextureStatus_OK);
  87. }
  88. break;
  89. case ImTextureStatus_WantDestroy:
  90. {
  91. ImGui::TextureBgfx tex = bx::bitCast<ImGui::TextureBgfx>(texData->GetTexID() );
  92. bgfx::destroy(tex.handle);
  93. texData->SetTexID(ImTextureID_Invalid);
  94. texData->SetStatus(ImTextureStatus_Destroyed);
  95. }
  96. break;
  97. case ImTextureStatus_WantUpdates:
  98. {
  99. ImGui::TextureBgfx tex = bx::bitCast<ImGui::TextureBgfx>(texData->GetTexID() );
  100. for (ImTextureRect& rect : texData->Updates)
  101. {
  102. const uint32_t bpp = texData->BytesPerPixel;
  103. const bgfx::Memory* pix = bgfx::alloc(rect.h * rect.w * bpp);
  104. bx::gather(pix->data, texData->GetPixelsAt(rect.x, rect.y), texData->GetPitch(), rect.w * bpp, rect.h);
  105. bgfx::updateTexture2D(tex.handle, 0, 0, rect.x, rect.y, rect.w, rect.h, pix);
  106. }
  107. }
  108. break;
  109. default:
  110. break;
  111. }
  112. }
  113. }
  114. // Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates)
  115. int32_t dispWidth = int32_t(_drawData->DisplaySize.x * _drawData->FramebufferScale.x);
  116. int32_t dispHeight = int32_t(_drawData->DisplaySize.y * _drawData->FramebufferScale.y);
  117. if (dispWidth <= 0
  118. || dispHeight <= 0)
  119. {
  120. return;
  121. }
  122. bgfx::setViewName(m_viewId, "ImGui");
  123. bgfx::setViewMode(m_viewId, bgfx::ViewMode::Sequential);
  124. const bgfx::Caps* caps = bgfx::getCaps();
  125. {
  126. float ortho[16];
  127. float x = _drawData->DisplayPos.x;
  128. float y = _drawData->DisplayPos.y;
  129. float width = _drawData->DisplaySize.x;
  130. float height = _drawData->DisplaySize.y;
  131. bx::mtxOrtho(ortho, x, x + width, y + height, y, 0.0f, 1000.0f, 0.0f, caps->homogeneousDepth);
  132. bgfx::setViewTransform(m_viewId, NULL, ortho);
  133. bgfx::setViewRect(m_viewId, 0, 0, uint16_t(width), uint16_t(height) );
  134. }
  135. const ImVec2 clipPos = _drawData->DisplayPos; // (0,0) unless using multi-viewports
  136. const ImVec2 clipScale = _drawData->FramebufferScale; // (1,1) unless using retina display which are often (2,2)
  137. // Render command lists
  138. for (int32_t ii = 0, num = _drawData->CmdListsCount; ii < num; ++ii)
  139. {
  140. bgfx::TransientVertexBuffer tvb;
  141. bgfx::TransientIndexBuffer tib;
  142. const ImDrawList* drawList = _drawData->CmdLists[ii];
  143. uint32_t numVertices = (uint32_t)drawList->VtxBuffer.size();
  144. uint32_t numIndices = (uint32_t)drawList->IdxBuffer.size();
  145. if (!checkAvailTransientBuffers(numVertices, m_layout, numIndices) )
  146. {
  147. // not enough space in transient buffer just quit drawing the rest...
  148. break;
  149. }
  150. bgfx::allocTransientVertexBuffer(&tvb, numVertices, m_layout);
  151. bgfx::allocTransientIndexBuffer(&tib, numIndices, sizeof(ImDrawIdx) == 4);
  152. ImDrawVert* verts = (ImDrawVert*)tvb.data;
  153. bx::memCopy(verts, drawList->VtxBuffer.begin(), numVertices * sizeof(ImDrawVert) );
  154. ImDrawIdx* indices = (ImDrawIdx*)tib.data;
  155. bx::memCopy(indices, drawList->IdxBuffer.begin(), numIndices * sizeof(ImDrawIdx) );
  156. bgfx::Encoder* encoder = bgfx::begin();
  157. for (const ImDrawCmd* cmd = drawList->CmdBuffer.begin(), *cmdEnd = drawList->CmdBuffer.end(); cmd != cmdEnd; ++cmd)
  158. {
  159. if (cmd->UserCallback)
  160. {
  161. cmd->UserCallback(drawList, cmd);
  162. }
  163. else if (0 != cmd->ElemCount)
  164. {
  165. uint64_t state = 0
  166. | BGFX_STATE_WRITE_RGB
  167. | BGFX_STATE_WRITE_A
  168. | BGFX_STATE_MSAA
  169. ;
  170. bgfx::TextureHandle th = BGFX_INVALID_HANDLE;
  171. bgfx::ProgramHandle program = m_program;
  172. const ImTextureID texId = cmd->GetTexID();
  173. if (ImTextureID_Invalid != texId)
  174. {
  175. ImGui::TextureBgfx tex = bx::bitCast<ImGui::TextureBgfx>(texId);
  176. state |= 0 != (IMGUI_FLAGS_ALPHA_BLEND & tex.flags)
  177. ? BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
  178. : BGFX_STATE_NONE
  179. ;
  180. th = tex.handle;
  181. if (0 != tex.mip)
  182. {
  183. const float lodEnabled[4] = { float(tex.mip), 1.0f, 0.0f, 0.0f };
  184. bgfx::setUniform(u_imageLodEnabled, lodEnabled);
  185. program = m_imageProgram;
  186. }
  187. }
  188. else
  189. {
  190. state |= BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA);
  191. }
  192. // Project scissor/clipping rectangles into framebuffer space
  193. ImVec4 clipRect;
  194. clipRect.x = (cmd->ClipRect.x - clipPos.x) * clipScale.x;
  195. clipRect.y = (cmd->ClipRect.y - clipPos.y) * clipScale.y;
  196. clipRect.z = (cmd->ClipRect.z - clipPos.x) * clipScale.x;
  197. clipRect.w = (cmd->ClipRect.w - clipPos.y) * clipScale.y;
  198. if (clipRect.x < dispWidth
  199. && clipRect.y < dispHeight
  200. && clipRect.z >= 0.0f
  201. && clipRect.w >= 0.0f)
  202. {
  203. const uint16_t xx = uint16_t(bx::max(clipRect.x, 0.0f) );
  204. const uint16_t yy = uint16_t(bx::max(clipRect.y, 0.0f) );
  205. encoder->setScissor(xx, yy
  206. , uint16_t(bx::min(clipRect.z, 65535.0f)-xx)
  207. , uint16_t(bx::min(clipRect.w, 65535.0f)-yy)
  208. );
  209. encoder->setState(state);
  210. encoder->setTexture(0, s_tex, th);
  211. encoder->setVertexBuffer(0, &tvb, cmd->VtxOffset, numVertices);
  212. encoder->setIndexBuffer(&tib, cmd->IdxOffset, cmd->ElemCount);
  213. encoder->submit(m_viewId, program);
  214. }
  215. }
  216. }
  217. bgfx::end(encoder);
  218. }
  219. }
  220. void create(float _fontSize, bx::AllocatorI* _allocator)
  221. {
  222. IMGUI_CHECKVERSION();
  223. m_allocator = _allocator;
  224. if (NULL == _allocator)
  225. {
  226. static bx::DefaultAllocator allocator;
  227. m_allocator = &allocator;
  228. }
  229. m_viewId = 255;
  230. m_lastScroll = 0;
  231. m_last = bx::getHPCounter();
  232. ImGui::SetAllocatorFunctions(memAlloc, memFree, NULL);
  233. m_imgui = ImGui::CreateContext();
  234. ImGuiIO& io = ImGui::GetIO();
  235. io.DisplaySize = ImVec2(1280.0f, 720.0f);
  236. io.DeltaTime = 1.0f / 60.0f;
  237. io.IniFilename = NULL;
  238. setupStyle(true);
  239. io.BackendFlags |= 0
  240. | ImGuiBackendFlags_RendererHasVtxOffset
  241. | ImGuiBackendFlags_RendererHasTextures
  242. ;
  243. io.ConfigDebugHighlightIdConflicts = !!BX_CONFIG_DEBUG;
  244. #if USE_ENTRY
  245. for (int32_t ii = 0; ii < (int32_t)entry::Key::Count; ++ii)
  246. {
  247. m_keyMap[ii] = ImGuiKey_None;
  248. }
  249. m_keyMap[entry::Key::Esc] = ImGuiKey_Escape;
  250. m_keyMap[entry::Key::Return] = ImGuiKey_Enter;
  251. m_keyMap[entry::Key::Tab] = ImGuiKey_Tab;
  252. m_keyMap[entry::Key::Space] = ImGuiKey_Space;
  253. m_keyMap[entry::Key::Backspace] = ImGuiKey_Backspace;
  254. m_keyMap[entry::Key::Up] = ImGuiKey_UpArrow;
  255. m_keyMap[entry::Key::Down] = ImGuiKey_DownArrow;
  256. m_keyMap[entry::Key::Left] = ImGuiKey_LeftArrow;
  257. m_keyMap[entry::Key::Right] = ImGuiKey_RightArrow;
  258. m_keyMap[entry::Key::Insert] = ImGuiKey_Insert;
  259. m_keyMap[entry::Key::Delete] = ImGuiKey_Delete;
  260. m_keyMap[entry::Key::Home] = ImGuiKey_Home;
  261. m_keyMap[entry::Key::End] = ImGuiKey_End;
  262. m_keyMap[entry::Key::PageUp] = ImGuiKey_PageUp;
  263. m_keyMap[entry::Key::PageDown] = ImGuiKey_PageDown;
  264. m_keyMap[entry::Key::Print] = ImGuiKey_PrintScreen;
  265. m_keyMap[entry::Key::Plus] = ImGuiKey_Equal;
  266. m_keyMap[entry::Key::Minus] = ImGuiKey_Minus;
  267. m_keyMap[entry::Key::LeftBracket] = ImGuiKey_LeftBracket;
  268. m_keyMap[entry::Key::RightBracket] = ImGuiKey_RightBracket;
  269. m_keyMap[entry::Key::Semicolon] = ImGuiKey_Semicolon;
  270. m_keyMap[entry::Key::Quote] = ImGuiKey_Apostrophe;
  271. m_keyMap[entry::Key::Comma] = ImGuiKey_Comma;
  272. m_keyMap[entry::Key::Period] = ImGuiKey_Period;
  273. m_keyMap[entry::Key::Slash] = ImGuiKey_Slash;
  274. m_keyMap[entry::Key::Backslash] = ImGuiKey_Backslash;
  275. m_keyMap[entry::Key::Tilde] = ImGuiKey_GraveAccent;
  276. m_keyMap[entry::Key::F1] = ImGuiKey_F1;
  277. m_keyMap[entry::Key::F2] = ImGuiKey_F2;
  278. m_keyMap[entry::Key::F3] = ImGuiKey_F3;
  279. m_keyMap[entry::Key::F4] = ImGuiKey_F4;
  280. m_keyMap[entry::Key::F5] = ImGuiKey_F5;
  281. m_keyMap[entry::Key::F6] = ImGuiKey_F6;
  282. m_keyMap[entry::Key::F7] = ImGuiKey_F7;
  283. m_keyMap[entry::Key::F8] = ImGuiKey_F8;
  284. m_keyMap[entry::Key::F9] = ImGuiKey_F9;
  285. m_keyMap[entry::Key::F10] = ImGuiKey_F10;
  286. m_keyMap[entry::Key::F11] = ImGuiKey_F11;
  287. m_keyMap[entry::Key::F12] = ImGuiKey_F12;
  288. m_keyMap[entry::Key::NumPad0] = ImGuiKey_Keypad0;
  289. m_keyMap[entry::Key::NumPad1] = ImGuiKey_Keypad1;
  290. m_keyMap[entry::Key::NumPad2] = ImGuiKey_Keypad2;
  291. m_keyMap[entry::Key::NumPad3] = ImGuiKey_Keypad3;
  292. m_keyMap[entry::Key::NumPad4] = ImGuiKey_Keypad4;
  293. m_keyMap[entry::Key::NumPad5] = ImGuiKey_Keypad5;
  294. m_keyMap[entry::Key::NumPad6] = ImGuiKey_Keypad6;
  295. m_keyMap[entry::Key::NumPad7] = ImGuiKey_Keypad7;
  296. m_keyMap[entry::Key::NumPad8] = ImGuiKey_Keypad8;
  297. m_keyMap[entry::Key::NumPad9] = ImGuiKey_Keypad9;
  298. m_keyMap[entry::Key::Key0] = ImGuiKey_0;
  299. m_keyMap[entry::Key::Key1] = ImGuiKey_1;
  300. m_keyMap[entry::Key::Key2] = ImGuiKey_2;
  301. m_keyMap[entry::Key::Key3] = ImGuiKey_3;
  302. m_keyMap[entry::Key::Key4] = ImGuiKey_4;
  303. m_keyMap[entry::Key::Key5] = ImGuiKey_5;
  304. m_keyMap[entry::Key::Key6] = ImGuiKey_6;
  305. m_keyMap[entry::Key::Key7] = ImGuiKey_7;
  306. m_keyMap[entry::Key::Key8] = ImGuiKey_8;
  307. m_keyMap[entry::Key::Key9] = ImGuiKey_9;
  308. m_keyMap[entry::Key::KeyA] = ImGuiKey_A;
  309. m_keyMap[entry::Key::KeyB] = ImGuiKey_B;
  310. m_keyMap[entry::Key::KeyC] = ImGuiKey_C;
  311. m_keyMap[entry::Key::KeyD] = ImGuiKey_D;
  312. m_keyMap[entry::Key::KeyE] = ImGuiKey_E;
  313. m_keyMap[entry::Key::KeyF] = ImGuiKey_F;
  314. m_keyMap[entry::Key::KeyG] = ImGuiKey_G;
  315. m_keyMap[entry::Key::KeyH] = ImGuiKey_H;
  316. m_keyMap[entry::Key::KeyI] = ImGuiKey_I;
  317. m_keyMap[entry::Key::KeyJ] = ImGuiKey_J;
  318. m_keyMap[entry::Key::KeyK] = ImGuiKey_K;
  319. m_keyMap[entry::Key::KeyL] = ImGuiKey_L;
  320. m_keyMap[entry::Key::KeyM] = ImGuiKey_M;
  321. m_keyMap[entry::Key::KeyN] = ImGuiKey_N;
  322. m_keyMap[entry::Key::KeyO] = ImGuiKey_O;
  323. m_keyMap[entry::Key::KeyP] = ImGuiKey_P;
  324. m_keyMap[entry::Key::KeyQ] = ImGuiKey_Q;
  325. m_keyMap[entry::Key::KeyR] = ImGuiKey_R;
  326. m_keyMap[entry::Key::KeyS] = ImGuiKey_S;
  327. m_keyMap[entry::Key::KeyT] = ImGuiKey_T;
  328. m_keyMap[entry::Key::KeyU] = ImGuiKey_U;
  329. m_keyMap[entry::Key::KeyV] = ImGuiKey_V;
  330. m_keyMap[entry::Key::KeyW] = ImGuiKey_W;
  331. m_keyMap[entry::Key::KeyX] = ImGuiKey_X;
  332. m_keyMap[entry::Key::KeyY] = ImGuiKey_Y;
  333. m_keyMap[entry::Key::KeyZ] = ImGuiKey_Z;
  334. io.ConfigFlags |= 0
  335. | ImGuiConfigFlags_NavEnableGamepad
  336. | ImGuiConfigFlags_NavEnableKeyboard
  337. ;
  338. m_keyMap[entry::Key::GamepadStart] = ImGuiKey_GamepadStart;
  339. m_keyMap[entry::Key::GamepadBack] = ImGuiKey_GamepadBack;
  340. m_keyMap[entry::Key::GamepadY] = ImGuiKey_GamepadFaceUp;
  341. m_keyMap[entry::Key::GamepadA] = ImGuiKey_GamepadFaceDown;
  342. m_keyMap[entry::Key::GamepadX] = ImGuiKey_GamepadFaceLeft;
  343. m_keyMap[entry::Key::GamepadB] = ImGuiKey_GamepadFaceRight;
  344. m_keyMap[entry::Key::GamepadUp] = ImGuiKey_GamepadDpadUp;
  345. m_keyMap[entry::Key::GamepadDown] = ImGuiKey_GamepadDpadDown;
  346. m_keyMap[entry::Key::GamepadLeft] = ImGuiKey_GamepadDpadLeft;
  347. m_keyMap[entry::Key::GamepadRight] = ImGuiKey_GamepadDpadRight;
  348. m_keyMap[entry::Key::GamepadShoulderL] = ImGuiKey_GamepadL1;
  349. m_keyMap[entry::Key::GamepadShoulderR] = ImGuiKey_GamepadR1;
  350. m_keyMap[entry::Key::GamepadThumbL] = ImGuiKey_GamepadL3;
  351. m_keyMap[entry::Key::GamepadThumbR] = ImGuiKey_GamepadR3;
  352. #endif // USE_ENTRY
  353. bgfx::RendererType::Enum type = bgfx::getRendererType();
  354. m_program = bgfx::createProgram(
  355. bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_ocornut_imgui")
  356. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_ocornut_imgui")
  357. , true
  358. );
  359. u_imageLodEnabled = bgfx::createUniform("u_imageLodEnabled", bgfx::UniformType::Vec4);
  360. m_imageProgram = bgfx::createProgram(
  361. bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_imgui_image")
  362. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_imgui_image")
  363. , true
  364. );
  365. m_layout
  366. .begin()
  367. .add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
  368. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
  369. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  370. .end();
  371. s_tex = bgfx::createUniform("s_tex", bgfx::UniformType::Sampler);
  372. {
  373. ImFontConfig config;
  374. config.FontDataOwnedByAtlas = false;
  375. config.MergeMode = false;
  376. const ImWchar* ranges = io.Fonts->GetGlyphRangesDefault();
  377. m_font[ImGui::Font::Regular] = io.Fonts->AddFontFromMemoryTTF( (void*)s_robotoRegularTtf, sizeof(s_robotoRegularTtf), _fontSize, &config, ranges);
  378. m_font[ImGui::Font::Mono ] = io.Fonts->AddFontFromMemoryTTF( (void*)s_robotoMonoRegularTtf, sizeof(s_robotoMonoRegularTtf), _fontSize-3.0f, &config, ranges);
  379. config.MergeMode = true;
  380. config.DstFont = m_font[ImGui::Font::Regular];
  381. for (uint32_t ii = 0; ii < BX_COUNTOF(s_fontRangeMerge); ++ii)
  382. {
  383. const FontRangeMerge& frm = s_fontRangeMerge[ii];
  384. io.Fonts->AddFontFromMemoryTTF( (void*)frm.data
  385. , (int)frm.size
  386. , _fontSize-3.0f
  387. , &config
  388. , frm.ranges
  389. );
  390. }
  391. }
  392. ImGuizmo::Create();
  393. ImGui::InitDockContext();
  394. }
  395. void destroy()
  396. {
  397. ImGuizmo::Destroy();
  398. for (ImTextureData* texData : ImGui::GetPlatformIO().Textures)
  399. {
  400. if (1 == texData->RefCount)
  401. {
  402. ImGui::TextureBgfx tex = bx::bitCast<ImGui::TextureBgfx>(texData->GetTexID() );
  403. bgfx::destroy(tex.handle);
  404. texData->SetTexID(ImTextureID_Invalid);
  405. texData->SetStatus(ImTextureStatus_Destroyed);
  406. }
  407. }
  408. ImGui::ShutdownDockContext();
  409. ImGui::DestroyContext(m_imgui);
  410. bgfx::destroy(s_tex);
  411. bgfx::destroy(u_imageLodEnabled);
  412. bgfx::destroy(m_imageProgram);
  413. bgfx::destroy(m_program);
  414. m_allocator = NULL;
  415. }
  416. void setupStyle(bool _dark)
  417. {
  418. // Doug Binks' darl color scheme
  419. // https://gist.github.com/dougbinks/8089b4bbaccaaf6fa204236978d165a9
  420. ImGuiStyle& style = ImGui::GetStyle();
  421. if (_dark)
  422. {
  423. ImGui::StyleColorsDark(&style);
  424. }
  425. else
  426. {
  427. ImGui::StyleColorsLight(&style);
  428. }
  429. style.FrameRounding = 4.0f;
  430. style.WindowBorderSize = 0.0f;
  431. }
  432. void beginFrame(
  433. int32_t _mx
  434. , int32_t _my
  435. , uint8_t _button
  436. , int32_t _scroll
  437. , int _width
  438. , int _height
  439. , int _inputChar
  440. , bgfx::ViewId _viewId
  441. )
  442. {
  443. m_viewId = _viewId;
  444. ImGuiIO& io = ImGui::GetIO();
  445. if (_inputChar >= 0)
  446. {
  447. io.AddInputCharacter(_inputChar);
  448. }
  449. io.DisplaySize = ImVec2( (float)_width, (float)_height);
  450. const int64_t now = bx::getHPCounter();
  451. const int64_t frameTime = now - m_last;
  452. m_last = now;
  453. const double freq = double(bx::getHPFrequency() );
  454. io.DeltaTime = float(frameTime/freq);
  455. io.AddMousePosEvent( (float)_mx, (float)_my);
  456. io.AddMouseButtonEvent(ImGuiMouseButton_Left, 0 != (_button & IMGUI_MBUT_LEFT ) );
  457. io.AddMouseButtonEvent(ImGuiMouseButton_Right, 0 != (_button & IMGUI_MBUT_RIGHT ) );
  458. io.AddMouseButtonEvent(ImGuiMouseButton_Middle, 0 != (_button & IMGUI_MBUT_MIDDLE) );
  459. io.AddMouseWheelEvent(0.0f, (float)(_scroll - m_lastScroll) );
  460. m_lastScroll = _scroll;
  461. #if USE_ENTRY
  462. const uint8_t modifiers = inputGetModifiersState();
  463. io.AddKeyEvent(ImGuiMod_Shift, 0 != (modifiers & (entry::Modifier::LeftShift | entry::Modifier::RightShift) ) );
  464. io.AddKeyEvent(ImGuiMod_Ctrl, 0 != (modifiers & (entry::Modifier::LeftCtrl | entry::Modifier::RightCtrl ) ) );
  465. io.AddKeyEvent(ImGuiMod_Alt, 0 != (modifiers & (entry::Modifier::LeftAlt | entry::Modifier::RightAlt ) ) );
  466. io.AddKeyEvent(ImGuiMod_Super, 0 != (modifiers & (entry::Modifier::LeftMeta | entry::Modifier::RightMeta ) ) );
  467. for (int32_t ii = 0; ii < int32_t(entry::Key::Count); ++ii)
  468. {
  469. io.AddKeyEvent(m_keyMap[ii], inputGetKeyState(entry::Key::Enum(ii) ) );
  470. io.SetKeyEventNativeData(m_keyMap[ii], 0, 0, ii);
  471. }
  472. #endif // USE_ENTRY
  473. ImGui::NewFrame();
  474. ImGuizmo::BeginFrame();
  475. }
  476. void endFrame()
  477. {
  478. ImGui::Render();
  479. render(ImGui::GetDrawData() );
  480. }
  481. ImGuiContext* m_imgui;
  482. bx::AllocatorI* m_allocator;
  483. bgfx::VertexLayout m_layout;
  484. bgfx::ProgramHandle m_program;
  485. bgfx::ProgramHandle m_imageProgram;
  486. bgfx::UniformHandle s_tex;
  487. bgfx::UniformHandle u_imageLodEnabled;
  488. ImFont* m_font[ImGui::Font::Count];
  489. int64_t m_last;
  490. int32_t m_lastScroll;
  491. bgfx::ViewId m_viewId;
  492. #if USE_ENTRY
  493. ImGuiKey m_keyMap[(int)entry::Key::Count];
  494. #endif // USE_ENTRY
  495. };
  496. static OcornutImguiContext s_ctx;
  497. static void* memAlloc(size_t _size, void* _userData)
  498. {
  499. BX_UNUSED(_userData);
  500. return bx::alloc(s_ctx.m_allocator, _size);
  501. }
  502. static void memFree(void* _ptr, void* _userData)
  503. {
  504. BX_UNUSED(_userData);
  505. bx::free(s_ctx.m_allocator, _ptr);
  506. }
  507. void imguiCreate(float _fontSize, bx::AllocatorI* _allocator)
  508. {
  509. s_ctx.create(_fontSize, _allocator);
  510. }
  511. void imguiDestroy()
  512. {
  513. s_ctx.destroy();
  514. }
  515. void imguiBeginFrame(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll, uint16_t _width, uint16_t _height, int _inputChar, bgfx::ViewId _viewId)
  516. {
  517. s_ctx.beginFrame(_mx, _my, _button, _scroll, _width, _height, _inputChar, _viewId);
  518. }
  519. void imguiEndFrame()
  520. {
  521. s_ctx.endFrame();
  522. }
  523. namespace ImGui
  524. {
  525. void PushFont(Font::Enum _font, float _fontSizeBaseUnscaled)
  526. {
  527. PushFont(s_ctx.m_font[_font], _fontSizeBaseUnscaled);
  528. }
  529. } // namespace ImGui
  530. #if USE_LOCAL_STB
  531. BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4244); // error C4244: 'argument': conversion from 'double' to 'float', possible loss of data
  532. BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4505); // error C4505: '' : unreferenced local function has been removed
  533. BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wunused-function"); // warning: 'int rect_width_compare(const void*, const void*)' defined but not used
  534. BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG("-Wunknown-pragmas")
  535. BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wtype-limits"); // warning: comparison is always true due to limited range of data type
  536. # define STBTT_ifloor(_a) int32_t(bx::floor(_a) )
  537. # define STBTT_iceil(_a) int32_t(bx::ceil(_a) )
  538. # define STBTT_sqrt(_a) bx::sqrt(_a)
  539. # define STBTT_pow(_a, _b) bx::pow(_a, _b)
  540. # define STBTT_fmod(_a, _b) bx::mod(_a, _b)
  541. # define STBTT_cos(_a) bx::cos(_a)
  542. # define STBTT_acos(_a) bx::acos(_a)
  543. # define STBTT_fabs(_a) bx::abs(_a)
  544. # define STBTT_strlen(_str) bx::strLen(_str)
  545. # define STBTT_memcpy(_dst, _src, _numBytes) bx::memCopy(_dst, _src, _numBytes)
  546. # define STBTT_memset(_dst, _ch, _numBytes) bx::memSet(_dst, _ch, _numBytes)
  547. # define STBTT_malloc(_size, _userData) memAlloc(_size, _userData)
  548. # define STBTT_free(_ptr, _userData) memFree(_ptr, _userData)
  549. # define STB_RECT_PACK_IMPLEMENTATION
  550. # include <stb/stb_rect_pack.h>
  551. # define STB_TRUETYPE_IMPLEMENTATION
  552. # include <stb/stb_truetype.h>
  553. #endif // USE_LOCAL_STB