imgui.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /*
  2. * Copyright 2014-2015 Daniel Collin. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  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 <ocornut-imgui/imgui.h>
  11. #include "imgui.h"
  12. #include "../bgfx_utils.h"
  13. #ifndef USE_ENTRY
  14. # if defined(SCI_NAMESPACE)
  15. # define USE_ENTRY 1
  16. # else
  17. # define USE_ENTRY 0
  18. # endif // defined(SCI_NAMESPACE)
  19. #endif // USE_ENTRY
  20. #if USE_ENTRY
  21. # include "../entry/entry.h"
  22. #endif // USE_ENTRY
  23. #if defined(SCI_NAMESPACE)
  24. # include "../entry/input.h"
  25. # include "scintilla.h"
  26. #endif // defined(SCI_NAMESPACE)
  27. #include "vs_ocornut_imgui.bin.h"
  28. #include "fs_ocornut_imgui.bin.h"
  29. #include "vs_imgui_image.bin.h"
  30. #include "fs_imgui_image.bin.h"
  31. #include "roboto_regular.ttf.h"
  32. #include "robotomono_regular.ttf.h"
  33. #include "icons_kenney.ttf.h"
  34. #include "icons_font_awesome.ttf.h"
  35. static const bgfx::EmbeddedShader s_embeddedShaders[] =
  36. {
  37. BGFX_EMBEDDED_SHADER(vs_ocornut_imgui),
  38. BGFX_EMBEDDED_SHADER(fs_ocornut_imgui),
  39. BGFX_EMBEDDED_SHADER(vs_imgui_image),
  40. BGFX_EMBEDDED_SHADER(fs_imgui_image),
  41. BGFX_EMBEDDED_SHADER_END()
  42. };
  43. struct FontRangeMerge
  44. {
  45. const void* data;
  46. size_t size;
  47. ImWchar ranges[3];
  48. };
  49. static FontRangeMerge s_fontRangeMerge[] =
  50. {
  51. { s_iconsKenneyTtf, sizeof(s_iconsKenneyTtf), { ICON_MIN_KI, ICON_MAX_KI, 0 } },
  52. { s_iconsFontAwesomeTtf, sizeof(s_iconsFontAwesomeTtf), { ICON_MIN_FA, ICON_MAX_FA, 0 } },
  53. };
  54. static void* memAlloc(size_t _size);
  55. static void memFree(void* _ptr);
  56. struct OcornutImguiContext
  57. {
  58. static void renderDrawLists(ImDrawData* _drawData);
  59. void render(ImDrawData* _drawData)
  60. {
  61. const ImGuiIO& io = ImGui::GetIO();
  62. const float width = io.DisplaySize.x;
  63. const float height = io.DisplaySize.y;
  64. bgfx::setViewName(m_viewId, "ImGui");
  65. bgfx::setViewMode(m_viewId, bgfx::ViewMode::Sequential);
  66. const bgfx::HMD* hmd = bgfx::getHMD();
  67. const bgfx::Caps* caps = bgfx::getCaps();
  68. if (NULL != hmd && 0 != (hmd->flags & BGFX_HMD_RENDERING) )
  69. {
  70. float proj[16];
  71. bx::mtxProj(proj, hmd->eye[0].fov, 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
  72. static float time = 0.0f;
  73. time += 0.05f;
  74. const float dist = 10.0f;
  75. const float offset0 = -proj[8] + (hmd->eye[0].viewOffset[0] / dist * proj[0]);
  76. const float offset1 = -proj[8] + (hmd->eye[1].viewOffset[0] / dist * proj[0]);
  77. float ortho[2][16];
  78. const float viewOffset = width/4.0f;
  79. const float viewWidth = width/2.0f;
  80. bx::mtxOrtho(ortho[0], viewOffset, viewOffset + viewWidth, height, 0.0f, 0.0f, 1000.0f, offset0, caps->homogeneousDepth);
  81. bx::mtxOrtho(ortho[1], viewOffset, viewOffset + viewWidth, height, 0.0f, 0.0f, 1000.0f, offset1, caps->homogeneousDepth);
  82. bgfx::setViewTransform(m_viewId, NULL, ortho[0], BGFX_VIEW_STEREO, ortho[1]);
  83. bgfx::setViewRect(m_viewId, 0, 0, hmd->width, hmd->height);
  84. }
  85. else
  86. {
  87. float ortho[16];
  88. bx::mtxOrtho(ortho, 0.0f, width, height, 0.0f, 0.0f, 1000.0f, 0.0f, caps->homogeneousDepth);
  89. bgfx::setViewTransform(m_viewId, NULL, ortho);
  90. bgfx::setViewRect(m_viewId, 0, 0, uint16_t(width), uint16_t(height) );
  91. }
  92. // Render command lists
  93. for (int32_t ii = 0, num = _drawData->CmdListsCount; ii < num; ++ii)
  94. {
  95. bgfx::TransientVertexBuffer tvb;
  96. bgfx::TransientIndexBuffer tib;
  97. const ImDrawList* drawList = _drawData->CmdLists[ii];
  98. uint32_t numVertices = (uint32_t)drawList->VtxBuffer.size();
  99. uint32_t numIndices = (uint32_t)drawList->IdxBuffer.size();
  100. if (!checkAvailTransientBuffers(numVertices, m_decl, numIndices) )
  101. {
  102. // not enough space in transient buffer just quit drawing the rest...
  103. break;
  104. }
  105. bgfx::allocTransientVertexBuffer(&tvb, numVertices, m_decl);
  106. bgfx::allocTransientIndexBuffer(&tib, numIndices);
  107. ImDrawVert* verts = (ImDrawVert*)tvb.data;
  108. bx::memCopy(verts, drawList->VtxBuffer.begin(), numVertices * sizeof(ImDrawVert) );
  109. ImDrawIdx* indices = (ImDrawIdx*)tib.data;
  110. bx::memCopy(indices, drawList->IdxBuffer.begin(), numIndices * sizeof(ImDrawIdx) );
  111. uint32_t offset = 0;
  112. for (const ImDrawCmd* cmd = drawList->CmdBuffer.begin(), *cmdEnd = drawList->CmdBuffer.end(); cmd != cmdEnd; ++cmd)
  113. {
  114. if (cmd->UserCallback)
  115. {
  116. cmd->UserCallback(drawList, cmd);
  117. }
  118. else if (0 != cmd->ElemCount)
  119. {
  120. uint64_t state = 0
  121. | BGFX_STATE_RGB_WRITE
  122. | BGFX_STATE_ALPHA_WRITE
  123. | BGFX_STATE_MSAA
  124. ;
  125. bgfx::TextureHandle th = m_texture;
  126. bgfx::ProgramHandle program = m_program;
  127. if (NULL != cmd->TextureId)
  128. {
  129. union { ImTextureID ptr; struct { bgfx::TextureHandle handle; uint8_t flags; uint8_t mip; } s; } texture = { cmd->TextureId };
  130. state |= 0 != (IMGUI_FLAGS_ALPHA_BLEND & texture.s.flags)
  131. ? BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
  132. : BGFX_STATE_NONE
  133. ;
  134. th = texture.s.handle;
  135. if (0 != texture.s.mip)
  136. {
  137. const float lodEnabled[4] = { float(texture.s.mip), 1.0f, 0.0f, 0.0f };
  138. bgfx::setUniform(u_imageLodEnabled, lodEnabled);
  139. program = m_imageProgram;
  140. }
  141. }
  142. else
  143. {
  144. state |= BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA);
  145. }
  146. const uint16_t xx = uint16_t(bx::fmax(cmd->ClipRect.x, 0.0f) );
  147. const uint16_t yy = uint16_t(bx::fmax(cmd->ClipRect.y, 0.0f) );
  148. bgfx::setScissor(xx, yy
  149. , uint16_t(bx::fmin(cmd->ClipRect.z, 65535.0f)-xx)
  150. , uint16_t(bx::fmin(cmd->ClipRect.w, 65535.0f)-yy)
  151. );
  152. bgfx::setState(state);
  153. bgfx::setTexture(0, s_tex, th);
  154. bgfx::setVertexBuffer(0, &tvb, 0, numVertices);
  155. bgfx::setIndexBuffer(&tib, offset, cmd->ElemCount);
  156. bgfx::submit(cmd->ViewId, program);
  157. }
  158. offset += cmd->ElemCount;
  159. }
  160. }
  161. }
  162. void create(float _fontSize, bx::AllocatorI* _allocator)
  163. {
  164. m_allocator = _allocator;
  165. if (NULL == _allocator)
  166. {
  167. static bx::DefaultAllocator allocator;
  168. m_allocator = &allocator;
  169. }
  170. m_viewId = 255;
  171. m_lastScroll = 0;
  172. m_last = bx::getHPCounter();
  173. ImGuiIO& io = ImGui::GetIO();
  174. io.RenderDrawListsFn = renderDrawLists;
  175. io.MemAllocFn = memAlloc;
  176. io.MemFreeFn = memFree;
  177. io.DisplaySize = ImVec2(1280.0f, 720.0f);
  178. io.DeltaTime = 1.0f / 60.0f;
  179. io.IniFilename = NULL;
  180. setupStyle(true);
  181. #if defined(SCI_NAMESPACE)
  182. io.KeyMap[ImGuiKey_Tab] = (int)entry::Key::Tab;
  183. io.KeyMap[ImGuiKey_LeftArrow] = (int)entry::Key::Left;
  184. io.KeyMap[ImGuiKey_RightArrow] = (int)entry::Key::Right;
  185. io.KeyMap[ImGuiKey_UpArrow] = (int)entry::Key::Up;
  186. io.KeyMap[ImGuiKey_DownArrow] = (int)entry::Key::Down;
  187. io.KeyMap[ImGuiKey_Home] = (int)entry::Key::Home;
  188. io.KeyMap[ImGuiKey_End] = (int)entry::Key::End;
  189. io.KeyMap[ImGuiKey_Delete] = (int)entry::Key::Delete;
  190. io.KeyMap[ImGuiKey_Backspace] = (int)entry::Key::Backspace;
  191. io.KeyMap[ImGuiKey_Enter] = (int)entry::Key::Return;
  192. io.KeyMap[ImGuiKey_Escape] = (int)entry::Key::Esc;
  193. io.KeyMap[ImGuiKey_A] = (int)entry::Key::KeyA;
  194. io.KeyMap[ImGuiKey_C] = (int)entry::Key::KeyC;
  195. io.KeyMap[ImGuiKey_V] = (int)entry::Key::KeyV;
  196. io.KeyMap[ImGuiKey_X] = (int)entry::Key::KeyX;
  197. io.KeyMap[ImGuiKey_Y] = (int)entry::Key::KeyY;
  198. io.KeyMap[ImGuiKey_Z] = (int)entry::Key::KeyZ;
  199. #endif // defined(SCI_NAMESPACE)
  200. bgfx::RendererType::Enum type = bgfx::getRendererType();
  201. m_program = bgfx::createProgram(
  202. bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_ocornut_imgui")
  203. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_ocornut_imgui")
  204. , true
  205. );
  206. u_imageLodEnabled = bgfx::createUniform("u_imageLodEnabled", bgfx::UniformType::Vec4);
  207. m_imageProgram = bgfx::createProgram(
  208. bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_imgui_image")
  209. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_imgui_image")
  210. , true
  211. );
  212. m_decl
  213. .begin()
  214. .add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
  215. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
  216. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  217. .end();
  218. s_tex = bgfx::createUniform("s_tex", bgfx::UniformType::Int1);
  219. uint8_t* data;
  220. int32_t width;
  221. int32_t height;
  222. {
  223. ImFontConfig config;
  224. config.FontDataOwnedByAtlas = false;
  225. config.MergeMode = false;
  226. // config.MergeGlyphCenterV = true;
  227. m_font[ImGui::Font::Regular] = io.Fonts->AddFontFromMemoryTTF( (void*)s_robotoRegularTtf, sizeof(s_robotoRegularTtf), _fontSize, &config);
  228. m_font[ImGui::Font::Mono ] = io.Fonts->AddFontFromMemoryTTF( (void*)s_robotoMonoRegularTtf, sizeof(s_robotoMonoRegularTtf), _fontSize-3.0f, &config);
  229. config.MergeMode = true;
  230. config.DstFont = m_font[ImGui::Font::Regular];
  231. for (uint32_t ii = 0; ii < BX_COUNTOF(s_fontRangeMerge); ++ii)
  232. {
  233. const FontRangeMerge& frm = s_fontRangeMerge[ii];
  234. io.Fonts->AddFontFromMemoryTTF( (void*)frm.data
  235. , (int)frm.size
  236. , _fontSize-3.0f
  237. , &config
  238. , frm.ranges
  239. );
  240. }
  241. }
  242. io.Fonts->GetTexDataAsRGBA32(&data, &width, &height);
  243. m_texture = bgfx::createTexture2D(
  244. (uint16_t)width
  245. , (uint16_t)height
  246. , false
  247. , 1
  248. , bgfx::TextureFormat::BGRA8
  249. , 0
  250. , bgfx::copy(data, width*height*4)
  251. );
  252. ImGui::InitDockContext();
  253. }
  254. void destroy()
  255. {
  256. ImGui::ShutdownDockContext();
  257. ImGui::Shutdown();
  258. bgfx::destroyUniform(s_tex);
  259. bgfx::destroyTexture(m_texture);
  260. bgfx::destroyUniform(u_imageLodEnabled);
  261. bgfx::destroyProgram(m_imageProgram);
  262. bgfx::destroyProgram(m_program);
  263. m_allocator = NULL;
  264. }
  265. void setupStyle(bool _dark)
  266. {
  267. // Doug Binks' darl color scheme
  268. // https://gist.github.com/dougbinks/8089b4bbaccaaf6fa204236978d165a9
  269. ImGuiStyle& style = ImGui::GetStyle();
  270. style.FrameRounding = 4.0f;
  271. // light style from Pacome Danhiez (user itamago)
  272. // https://github.com/ocornut/imgui/pull/511#issuecomment-175719267
  273. style.Colors[ImGuiCol_Text] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f);
  274. style.Colors[ImGuiCol_TextDisabled] = ImVec4(0.60f, 0.60f, 0.60f, 1.00f);
  275. style.Colors[ImGuiCol_WindowBg] = ImVec4(0.94f, 0.94f, 0.94f, 1.00f);
  276. style.Colors[ImGuiCol_ChildWindowBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);
  277. style.Colors[ImGuiCol_Border] = ImVec4(0.00f, 0.00f, 0.00f, 0.39f);
  278. style.Colors[ImGuiCol_BorderShadow] = ImVec4(1.00f, 1.00f, 1.00f, 0.10f);
  279. style.Colors[ImGuiCol_FrameBg] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);
  280. style.Colors[ImGuiCol_FrameBgHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.40f);
  281. style.Colors[ImGuiCol_FrameBgActive] = ImVec4(0.26f, 0.59f, 0.98f, 0.67f);
  282. style.Colors[ImGuiCol_TitleBg] = ImVec4(0.96f, 0.96f, 0.96f, 1.00f);
  283. style.Colors[ImGuiCol_TitleBgCollapsed] = ImVec4(1.00f, 1.00f, 1.00f, 0.51f);
  284. style.Colors[ImGuiCol_TitleBgActive] = ImVec4(0.82f, 0.82f, 0.82f, 1.00f);
  285. style.Colors[ImGuiCol_MenuBarBg] = ImVec4(0.86f, 0.86f, 0.86f, 1.00f);
  286. style.Colors[ImGuiCol_ScrollbarBg] = ImVec4(0.98f, 0.98f, 0.98f, 0.53f);
  287. style.Colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.69f, 0.69f, 0.69f, 0.80f);
  288. style.Colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.49f, 0.49f, 0.49f, 0.80f);
  289. style.Colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.49f, 0.49f, 0.49f, 1.00f);
  290. style.Colors[ImGuiCol_ComboBg] = ImVec4(0.86f, 0.86f, 0.86f, 0.99f);
  291. style.Colors[ImGuiCol_CheckMark] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);
  292. style.Colors[ImGuiCol_SliderGrab] = ImVec4(0.26f, 0.59f, 0.98f, 0.78f);
  293. style.Colors[ImGuiCol_SliderGrabActive] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);
  294. style.Colors[ImGuiCol_Button] = ImVec4(0.26f, 0.59f, 0.98f, 0.40f);
  295. style.Colors[ImGuiCol_ButtonHovered] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);
  296. style.Colors[ImGuiCol_ButtonActive] = ImVec4(0.06f, 0.53f, 0.98f, 1.00f);
  297. style.Colors[ImGuiCol_Header] = ImVec4(0.26f, 0.59f, 0.98f, 0.31f);
  298. style.Colors[ImGuiCol_HeaderHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.80f);
  299. style.Colors[ImGuiCol_HeaderActive] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);
  300. style.Colors[ImGuiCol_Column] = ImVec4(0.39f, 0.39f, 0.39f, 1.00f);
  301. style.Colors[ImGuiCol_ColumnHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.78f);
  302. style.Colors[ImGuiCol_ColumnActive] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);
  303. style.Colors[ImGuiCol_ResizeGrip] = ImVec4(1.00f, 1.00f, 1.00f, 0.50f);
  304. style.Colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.67f);
  305. style.Colors[ImGuiCol_ResizeGripActive] = ImVec4(0.26f, 0.59f, 0.98f, 0.95f);
  306. style.Colors[ImGuiCol_CloseButton] = ImVec4(0.59f, 0.59f, 0.59f, 0.50f);
  307. style.Colors[ImGuiCol_CloseButtonHovered] = ImVec4(0.98f, 0.39f, 0.36f, 1.00f);
  308. style.Colors[ImGuiCol_CloseButtonActive] = ImVec4(0.98f, 0.39f, 0.36f, 1.00f);
  309. style.Colors[ImGuiCol_PlotLines] = ImVec4(0.39f, 0.39f, 0.39f, 1.00f);
  310. style.Colors[ImGuiCol_PlotLinesHovered] = ImVec4(1.00f, 0.43f, 0.35f, 1.00f);
  311. style.Colors[ImGuiCol_PlotHistogram] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f);
  312. style.Colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.60f, 0.00f, 1.00f);
  313. style.Colors[ImGuiCol_TextSelectedBg] = ImVec4(0.26f, 0.59f, 0.98f, 0.35f);
  314. style.Colors[ImGuiCol_PopupBg] = ImVec4(1.00f, 1.00f, 1.00f, 0.94f);
  315. style.Colors[ImGuiCol_ModalWindowDarkening] = ImVec4(0.20f, 0.20f, 0.20f, 0.35f);
  316. if (_dark)
  317. {
  318. for (int i = 0; i <= ImGuiCol_COUNT; i++)
  319. {
  320. ImVec4& col = style.Colors[i];
  321. float H, S, V;
  322. ImGui::ColorConvertRGBtoHSV( col.x, col.y, col.z, H, S, V );
  323. if( S < 0.1f )
  324. {
  325. V = 1.0f - V;
  326. }
  327. ImGui::ColorConvertHSVtoRGB( H, S, V, col.x, col.y, col.z );
  328. }
  329. }
  330. }
  331. void beginFrame(
  332. int32_t _mx
  333. , int32_t _my
  334. , uint8_t _button
  335. , int32_t _scroll
  336. , int _width
  337. , int _height
  338. , char _inputChar
  339. , uint8_t _viewId
  340. )
  341. {
  342. m_viewId = _viewId;
  343. ImGuiIO& io = ImGui::GetIO();
  344. if (_inputChar < 0x7f)
  345. {
  346. io.AddInputCharacter(_inputChar); // ASCII or GTFO! :(
  347. }
  348. io.DisplaySize = ImVec2( (float)_width, (float)_height);
  349. const int64_t now = bx::getHPCounter();
  350. const int64_t frameTime = now - m_last;
  351. m_last = now;
  352. const double freq = double(bx::getHPFrequency() );
  353. io.DeltaTime = float(frameTime/freq);
  354. io.MousePos = ImVec2( (float)_mx, (float)_my);
  355. io.MouseDown[0] = 0 != (_button & IMGUI_MBUT_LEFT);
  356. io.MouseDown[1] = 0 != (_button & IMGUI_MBUT_RIGHT);
  357. io.MouseDown[2] = 0 != (_button & IMGUI_MBUT_MIDDLE);
  358. io.MouseWheel = (float)(_scroll - m_lastScroll);
  359. m_lastScroll = _scroll;
  360. #if defined(SCI_NAMESPACE)
  361. uint8_t modifiers = inputGetModifiersState();
  362. io.KeyShift = 0 != (modifiers & (entry::Modifier::LeftShift | entry::Modifier::RightShift) );
  363. io.KeyCtrl = 0 != (modifiers & (entry::Modifier::LeftCtrl | entry::Modifier::RightCtrl ) );
  364. io.KeyAlt = 0 != (modifiers & (entry::Modifier::LeftAlt | entry::Modifier::RightAlt ) );
  365. for (int32_t ii = 0; ii < (int32_t)entry::Key::Count; ++ii)
  366. {
  367. io.KeysDown[ii] = inputGetKeyState(entry::Key::Enum(ii) );
  368. }
  369. #endif // defined(SCI_NAMESPACE)
  370. ImGui::NewFrame();
  371. ImGui::PushStyleVar(ImGuiStyleVar_ViewId, (float)_viewId);
  372. ImGuizmo::BeginFrame();
  373. }
  374. void endFrame()
  375. {
  376. ImGui::PopStyleVar(1);
  377. ImGui::Render();
  378. }
  379. bx::AllocatorI* m_allocator;
  380. bgfx::VertexDecl m_decl;
  381. bgfx::ProgramHandle m_program;
  382. bgfx::ProgramHandle m_imageProgram;
  383. bgfx::TextureHandle m_texture;
  384. bgfx::UniformHandle s_tex;
  385. bgfx::UniformHandle u_imageLodEnabled;
  386. ImFont* m_font[ImGui::Font::Count];
  387. int64_t m_last;
  388. int32_t m_lastScroll;
  389. uint8_t m_viewId;
  390. };
  391. static OcornutImguiContext s_ctx;
  392. static void* memAlloc(size_t _size)
  393. {
  394. return BX_ALLOC(s_ctx.m_allocator, _size);
  395. }
  396. static void memFree(void* _ptr)
  397. {
  398. BX_FREE(s_ctx.m_allocator, _ptr);
  399. }
  400. void OcornutImguiContext::renderDrawLists(ImDrawData* _drawData)
  401. {
  402. s_ctx.render(_drawData);
  403. }
  404. void imguiCreate(float _fontSize, bx::AllocatorI* _allocator)
  405. {
  406. s_ctx.create(_fontSize, _allocator);
  407. }
  408. void imguiDestroy()
  409. {
  410. s_ctx.destroy();
  411. }
  412. void imguiBeginFrame(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll, uint16_t _width, uint16_t _height, char _inputChar, uint8_t _viewId)
  413. {
  414. s_ctx.beginFrame(_mx, _my, _button, _scroll, _width, _height, _inputChar, _viewId);
  415. }
  416. void imguiEndFrame()
  417. {
  418. s_ctx.endFrame();
  419. }
  420. namespace ImGui
  421. {
  422. void PushFont(Font::Enum _font)
  423. {
  424. PushFont(s_ctx.m_font[_font]);
  425. }
  426. } // namespace ImGui
  427. BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4505); // error C4505: '' : unreferenced local function has been removed
  428. BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wunused-function"); // warning: ‘int rect_width_compare(const void*, const void*)’ defined but not used
  429. BX_PRAGMA_DIAGNOSTIC_PUSH();
  430. BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG("-Wunknown-pragmas")
  431. BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wunused-but-set-variable"); // warning: variable ‘L1’ set but not used
  432. BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wtype-limits"); // warning: comparison is always true due to limited range of data type
  433. #define STBTT_malloc(_size, _userData) memAlloc(_size)
  434. #define STBTT_free(_ptr, _userData) memFree(_ptr)
  435. #define STB_RECT_PACK_IMPLEMENTATION
  436. #include <stb/stb_rect_pack.h>
  437. #define STB_TRUETYPE_IMPLEMENTATION
  438. #include <stb/stb_truetype.h>
  439. BX_PRAGMA_DIAGNOSTIC_POP();