2
0

imgui.cpp 17 KB

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