Skip to content

Commit 7d9d804

Browse files
committed
Allow fonts to work correctly on macOS
1 parent c4d89f1 commit 7d9d804

1 file changed

Lines changed: 41 additions & 31 deletions

File tree

DebugGUI/src/DebugGUIMacos.mm

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,14 @@ static void default_error_callback(int error, const char *description) {
2727
GLFWwindow *window;
2828
id<MTLDevice> device;
2929
id<MTLCommandQueue> commandQueue;
30-
MTLRenderPassDescriptor* renderPassDescriptor;
30+
MTLRenderPassDescriptor *renderPassDescriptor;
3131
};
3232

3333
CAMetalLayer *layer;
3434
GLFWwindow *window;
3535

3636
// @return an object of kind GLFWwindow* as void* to avoid having a direct dependency
37-
void *initGUI(const char *name,
38-
void (*error_callback)(int, char const *description)) {
37+
void *initGUI(const char *name, void (*error_callback)(int, char const *description)) {
3938
DebugGUIContext *context = nullptr;
4039

4140
if (name) {
@@ -53,14 +52,12 @@ static void default_error_callback(int error, const char *description) {
5352
if (error_callback == nullptr) {
5453
glfwSetErrorCallback(default_error_callback);
5554
}
56-
if (!glfwInit())
57-
return nullptr;
55+
if (!glfwInit()) return nullptr;
5856
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
5957
context = (DebugGUIContext *)(malloc(sizeof(DebugGUIContext)));
6058
window = glfwCreateWindow(1280, 720, name, nullptr, nullptr);
6159

62-
if (window == NULL)
63-
return 0;
60+
if (window == NULL) return 0;
6461

6562
context->window = window;
6663
context->device = MTLCreateSystemDefaultDevice();
@@ -69,6 +66,21 @@ static void default_error_callback(int error, const char *description) {
6966

7067
// Setup Platform/Renderer backends
7168
ImGui_ImplGlfw_InitForOpenGL(context->window, true);
69+
// Load Fonts
70+
// (there is a default font, this is only if you want to change it. see
71+
// extra_fonts/README.txt for more details)
72+
io.Fonts->AddFontDefault();
73+
static const ImWchar icons_ranges[] = {ICON_MIN_FA, ICON_MAX_FA, 0};
74+
ImFontConfig icons_config;
75+
icons_config.MergeMode = true;
76+
icons_config.PixelSnapH = true;
77+
icons_config.FontDataOwnedByAtlas = false;
78+
io.Fonts->AddFontFromMemoryTTF((void *)s_iconsFontAwesomeTtf, sizeof(s_iconsFontAwesomeTtf),
79+
12.0f, &icons_config, icons_ranges);
80+
81+
if (io.Fonts->ConfigData.empty()) io.Fonts->AddFontDefault();
82+
// io.Fonts->Build();
83+
io.DisplaySize = ImVec2(1280, 720);
7284
ImGui_ImplMetal_Init(context->device);
7385

7486
NSWindow *nswin = glfwGetCocoaWindow(context->window);
@@ -79,7 +91,6 @@ static void default_error_callback(int error, const char *description) {
7991
nswin.contentView.wantsLayer = YES;
8092

8193
context->renderPassDescriptor = [MTLRenderPassDescriptor new];
82-
ImGui_ImplMetal_CreateDeviceObjects(layer.device);
8394
} else {
8495
ImGui::CreateContext();
8596
ImGuiIO &io = ImGui::GetIO();
@@ -103,22 +114,23 @@ static void default_error_callback(int error, const char *description) {
103114
io.KeyMap[ImGuiKey_X] = GLFW_KEY_X;
104115
io.KeyMap[ImGuiKey_Y] = GLFW_KEY_Y;
105116
io.KeyMap[ImGuiKey_Z] = GLFW_KEY_Z;
117+
// Load Fonts
118+
// (there is a default font, this is only if you want to change it. see
119+
// extra_fonts/README.txt for more details)
120+
io.Fonts->AddFontDefault();
121+
static const ImWchar icons_ranges[] = {ICON_MIN_FA, ICON_MAX_FA, 0};
122+
ImFontConfig icons_config;
123+
icons_config.MergeMode = true;
124+
icons_config.PixelSnapH = true;
125+
icons_config.FontDataOwnedByAtlas = false;
126+
io.Fonts->AddFontFromMemoryTTF((void *)s_iconsFontAwesomeTtf, sizeof(s_iconsFontAwesomeTtf),
127+
12.0f, &icons_config, icons_ranges);
128+
129+
if (io.Fonts->ConfigData.empty()) io.Fonts->AddFontDefault();
130+
// io.Fonts->Build();
131+
io.DisplaySize = ImVec2(1280, 720);
106132
}
107133

108-
// Load Fonts
109-
// (there is a default font, this is only if you want to change it. see
110-
// extra_fonts/README.txt for more details)
111-
ImGuiIO &io = ImGui::GetIO();
112-
io.Fonts->AddFontDefault();
113-
static const ImWchar icons_ranges[] = {ICON_MIN_FA, ICON_MAX_FA, 0};
114-
ImFontConfig icons_config;
115-
icons_config.MergeMode = true;
116-
icons_config.PixelSnapH = true;
117-
icons_config.FontDataOwnedByAtlas = false;
118-
io.Fonts->AddFontFromMemoryTTF((void *)s_iconsFontAwesomeTtf,
119-
sizeof(s_iconsFontAwesomeTtf), 12.0f,
120-
&icons_config, icons_ranges);
121-
122134
ImPlot::CreateContext();
123135
return context;
124136
}
@@ -134,8 +146,8 @@ bool pollGUIPreRender(void *context, float delta) {
134146
return true;
135147
}
136148

137-
DebugGUIContext* ctx = reinterpret_cast<DebugGUIContext*>(context);
138-
NSWindow* nswin = glfwGetCocoaWindow(ctx->window);
149+
DebugGUIContext *ctx = reinterpret_cast<DebugGUIContext *>(context);
150+
NSWindow *nswin = glfwGetCocoaWindow(ctx->window);
139151
if (glfwWindowShouldClose(ctx->window)) {
140152
return false;
141153
}
@@ -148,8 +160,7 @@ bool pollGUIPreRender(void *context, float delta) {
148160
auto drawable = [layer nextDrawable];
149161

150162
ctx->renderPassDescriptor.colorAttachments[0].clearColor =
151-
MTLClearColorMake(clear_color[0], clear_color[1], clear_color[2],
152-
clear_color[3]);
163+
MTLClearColorMake(clear_color[0], clear_color[1], clear_color[2], clear_color[3]);
153164
ctx->renderPassDescriptor.colorAttachments[0].texture = drawable.texture;
154165
ctx->renderPassDescriptor.colorAttachments[0].loadAction = MTLLoadActionClear;
155166
ctx->renderPassDescriptor.colorAttachments[0].storeAction = MTLStoreActionStore;
@@ -172,12 +183,11 @@ void pollGUIPostRender(void *context, void *drawData) {
172183
auto drawable = [layer nextDrawable];
173184

174185
id<MTLCommandBuffer> commandBuffer = [ctx->commandQueue commandBuffer];
175-
id<MTLRenderCommandEncoder> renderEncoder = [commandBuffer
176-
renderCommandEncoderWithDescriptor:ctx->renderPassDescriptor];
186+
id<MTLRenderCommandEncoder> renderEncoder =
187+
[commandBuffer renderCommandEncoderWithDescriptor:ctx->renderPassDescriptor];
177188
[renderEncoder pushDebugGroup:@"ImGui demo"];
178189

179-
ImGui_ImplMetal_RenderDrawData((ImDrawData *)drawData, commandBuffer,
180-
renderEncoder);
190+
ImGui_ImplMetal_RenderDrawData((ImDrawData *)drawData, commandBuffer, renderEncoder);
181191

182192
[renderEncoder popDebugGroup];
183193
[renderEncoder endEncoding];
@@ -218,4 +228,4 @@ void disposeGUI() {
218228
glfwTerminate();
219229
}
220230

221-
} // namespace o2::framework
231+
} // namespace o2::framework

0 commit comments

Comments
 (0)