| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | #include "imgui.h" |
| 11 | #include "imgui_impl_glfw.h" |
| 12 | #include "imgui_impl_metal.h" |
| 13 | #include <stdio.h> |
| 14 | |
| 15 | #define GLFW_INCLUDE_NONE |
| 16 | #define GLFW_EXPOSE_NATIVE_COCOA |
| 17 | #include <GLFW/glfw3.h> |
| 18 | #include <GLFW/glfw3native.h> |
| 19 | |
| 20 | #import <Metal/Metal.h> |
| 21 | #import <QuartzCore/QuartzCore.h> |
| 22 | |
| 23 | static void glfw_error_callback(int error, const char* description) |
| 24 | { |
| 25 | fprintf(stderr, "Glfw Error %d: %s\n", error, description); |
| 26 | } |
| 27 | |
| 28 | int main(int, char**) |
| 29 | { |
| 30 | glfwSetErrorCallback(glfw_error_callback); |
| 31 | if (!glfwInit()) |
| 32 | return 1; |
| 33 | |
| 34 | |
| 35 | float main_scale = ImGui_ImplGlfw_GetContentScaleForMonitor(glfwGetPrimaryMonitor()); |
| 36 | glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); |
| 37 | GLFWwindow* window = glfwCreateWindow((int)(1280 * main_scale), (int)(800 * main_scale), "Dear ImGui GLFW+Metal example", nullptr, nullptr); |
| 38 | if (window == nullptr) |
| 39 | return 1; |
| 40 | |
| 41 | |
| 42 | IMGUI_CHECKVERSION(); |
| 43 | ImGui::CreateContext(); |
| 44 | ImGuiIO& io = ImGui::GetIO(); (void)io; |
| 45 | io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; |
| 46 | io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; |
| 47 | io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; |
| 48 | io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; |
| 49 | |
| 50 | |
| 51 | ImGui::StyleColorsDark(); |
| 52 | |
| 53 | |
| 54 | |
| 55 | ImGuiStyle& style = ImGui::GetStyle(); |
| 56 | style.ScaleAllSizes(main_scale); |
| 57 | style.FontScaleDpi = main_scale; |
| 58 | io.ConfigDpiScaleFonts = true; |
| 59 | io.ConfigDpiScaleViewports = true; |
| 60 | |
| 61 | |
| 62 | if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) |
| 63 | { |
| 64 | style.WindowRounding = 0.0f; |
| 65 | style.Colors[ImGuiCol_WindowBg].w = 1.0f; |
| 66 | } |
| 67 | |
| 68 | id <MTLDevice> device = MTLCreateSystemDefaultDevice(); |
| 69 | id <MTLCommandQueue> commandQueue = [device newCommandQueue]; |
| 70 | |
| 71 | |
| 72 | ImGui_ImplGlfw_InitForOpenGL(window, true); |
| 73 | ImGui_ImplMetal_Init(device); |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | NSWindow *nswin = glfwGetCocoaWindow(window); |
| 94 | CAMetalLayer *layer = [CAMetalLayer layer]; |
| 95 | layer.device = device; |
| 96 | layer.pixelFormat = MTLPixelFormatBGRA8Unorm; |
| 97 | nswin.contentView.layer = layer; |
| 98 | nswin.contentView.wantsLayer = YES; |
| 99 | |
| 100 | MTLRenderPassDescriptor *renderPassDescriptor = [MTLRenderPassDescriptor new]; |
| 101 | |
| 102 | |
| 103 | bool show_demo_window = true; |
| 104 | bool show_another_window = false; |
| 105 | float clear_color[4] = {0.45f, 0.55f, 0.60f, 1.00f}; |
| 106 | |
| 107 | |
| 108 | while (!glfwWindowShouldClose(window)) |
| 109 | { |
| 110 | @autoreleasepool |
| 111 | { |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | glfwPollEvents(); |
| 118 | |
| 119 | int width, height; |
| 120 | glfwGetFramebufferSize(window, &width, &height); |
| 121 | layer.drawableSize = CGSizeMake(width, height); |
| 122 | id<CAMetalDrawable> drawable = [layer nextDrawable]; |
| 123 | |
| 124 | id<MTLCommandBuffer> commandBuffer = [commandQueue commandBuffer]; |
| 125 | renderPassDescriptor.colorAttachments[0].clearColor = MTLClearColorMake(clear_color[0] * clear_color[3], clear_color[1] * clear_color[3], clear_color[2] * clear_color[3], clear_color[3]); |
| 126 | renderPassDescriptor.colorAttachments[0].texture = drawable.texture; |
| 127 | renderPassDescriptor.colorAttachments[0].loadAction = MTLLoadActionClear; |
| 128 | renderPassDescriptor.colorAttachments[0].storeAction = MTLStoreActionStore; |
| 129 | id <MTLRenderCommandEncoder> renderEncoder = [commandBuffer renderCommandEncoderWithDescriptor:renderPassDescriptor]; |
| 130 | [renderEncoder pushDebugGroup:@"ImGui demo"]; |
| 131 | |
| 132 | |
| 133 | ImGui_ImplMetal_NewFrame(renderPassDescriptor); |
| 134 | ImGui_ImplGlfw_NewFrame(); |
| 135 | ImGui::NewFrame(); |
| 136 | |
| 137 | |
| 138 | if (show_demo_window) |
| 139 | ImGui::ShowDemoWindow(&show_demo_window); |
| 140 | |
| 141 | |
| 142 | { |
| 143 | static float f = 0.0f; |
| 144 | static int counter = 0; |
| 145 | |
| 146 | ImGui::Begin("Hello, world!"); |
| 147 | |
| 148 | ImGui::Text("This is some useful text."); |
| 149 | ImGui::Checkbox("Demo Window", &show_demo_window); |
| 150 | ImGui::Checkbox("Another Window", &show_another_window); |
| 151 | |
| 152 | ImGui::SliderFloat("float", &f, 0.0f, 1.0f); |
| 153 | ImGui::ColorEdit3("clear color", (float*)&clear_color); |
| 154 | |
| 155 | if (ImGui::Button("Button")) |
| 156 | counter++; |
| 157 | ImGui::SameLine(); |
| 158 | ImGui::Text("counter = %d", counter); |
| 159 | |
| 160 | ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate); |
| 161 | ImGui::End(); |
| 162 | } |
| 163 | |
| 164 | |
| 165 | if (show_another_window) |
| 166 | { |
| 167 | ImGui::Begin("Another Window", &show_another_window); |
| 168 | ImGui::Text("Hello from another window!"); |
| 169 | if (ImGui::Button("Close Me")) |
| 170 | show_another_window = false; |
| 171 | ImGui::End(); |
| 172 | } |
| 173 | |
| 174 | |
| 175 | ImGui::Render(); |
| 176 | ImGui_ImplMetal_RenderDrawData(ImGui::GetDrawData(), commandBuffer, renderEncoder); |
| 177 | |
| 178 | |
| 179 | if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) |
| 180 | { |
| 181 | ImGui::UpdatePlatformWindows(); |
| 182 | ImGui::RenderPlatformWindowsDefault(); |
| 183 | } |
| 184 | |
| 185 | [renderEncoder popDebugGroup]; |
| 186 | [renderEncoder endEncoding]; |
| 187 | |
| 188 | [commandBuffer presentDrawable:drawable]; |
| 189 | [commandBuffer commit]; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | |
| 194 | ImGui_ImplMetal_Shutdown(); |
| 195 | ImGui_ImplGlfw_Shutdown(); |
| 196 | ImGui::DestroyContext(); |
| 197 | |
| 198 | glfwDestroyWindow(window); |
| 199 | glfwTerminate(); |
| 200 | |
| 201 | return 0; |
| 202 | } |