9.7 KB、202行 生 Blame 履歴
1// Dear ImGui: standalone example application for GLFW + Metal, using programmable pipeline
2// (GLFW is a cross-platform general purpose library for handling windows, inputs, OpenGL/Vulkan/Metal graphics context creation, etc.)
3
4// Learn about Dear ImGui:
5// - FAQ https://dearimgui.com/faq
6// - Getting Started https://dearimgui.com/getting-started
7// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
8// - Introduction, links and more at the top of imgui.cpp
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
23static void glfw_error_callback(int error, const char* description)
24{
25 fprintf(stderr, "Glfw Error %d: %s\n", error, description);
26}
27
28int main(int, char**)
29{
30 glfwSetErrorCallback(glfw_error_callback);
31 if (!glfwInit())
32 return 1;
33
34 // Create window with graphics context
35 float main_scale = ImGui_ImplGlfw_GetContentScaleForMonitor(glfwGetPrimaryMonitor()); // Valid on GLFW 3.3+ only
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 // Setup Dear ImGui context
42 IMGUI_CHECKVERSION();
43 ImGui::CreateContext();
44 ImGuiIO& io = ImGui::GetIO(); (void)io;
45 io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
46 io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
47 io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
48 io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows
49
50 // Setup Dear ImGui style
51 ImGui::StyleColorsDark();
52 //ImGui::StyleColorsLight();
53
54 // Setup scaling
55 ImGuiStyle& style = ImGui::GetStyle();
56 style.ScaleAllSizes(main_scale); // Bake a fixed style scale. (until we have a solution for dynamic style scaling, changing this requires resetting Style + calling this again)
57 style.FontScaleDpi = main_scale; // Set initial font scale. (in docking branch: using io.ConfigDpiScaleFonts=true automatically overrides this for every window depending on the current monitor)
58 io.ConfigDpiScaleFonts = true; // [Experimental] Automatically overwrite style.FontScaleDpi in Begin() when Monitor DPI changes. This will scale fonts but _NOT_ scale sizes/padding for now.
59 io.ConfigDpiScaleViewports = true; // [Experimental] Scale Dear ImGui and Platform Windows when Monitor DPI changes.
60
61 // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
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 // Setup Platform/Renderer backends
72 ImGui_ImplGlfw_InitForOpenGL(window, true);
73 ImGui_ImplMetal_Init(device);
74
75 // Load Fonts
76 // - If fonts are not explicitly loaded, Dear ImGui will select an embedded font: either AddFontDefaultVector() or AddFontDefaultBitmap().
77 // This selection is based on (style.FontSizeBase * style.FontScaleMain * style.FontScaleDpi) reaching a small threshold.
78 // - You can load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
79 // - If a file cannot be loaded, AddFont functions will return a nullptr. Please handle those errors in your code (e.g. use an assertion, display an error and quit).
80 // - Read 'docs/FONTS.md' for more instructions and details.
81 // - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use FreeType for higher quality font rendering.
82 // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
83 //style.FontSizeBase = 20.0f;
84 //io.Fonts->AddFontDefaultVector();
85 //io.Fonts->AddFontDefaultBitmap();
86 //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf");
87 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf");
88 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf");
89 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf");
90 //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf");
91 //IM_ASSERT(font != nullptr);
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 // Our state
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 // Main loop
108 while (!glfwWindowShouldClose(window))
109 {
110 @autoreleasepool
111 {
112 // Poll and handle events (inputs, window resize, etc.)
113 // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
114 // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
115 // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
116 // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
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 // Start the Dear ImGui frame
133 ImGui_ImplMetal_NewFrame(renderPassDescriptor);
134 ImGui_ImplGlfw_NewFrame();
135 ImGui::NewFrame();
136
137 // 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!).
138 if (show_demo_window)
139 ImGui::ShowDemoWindow(&show_demo_window);
140
141 // 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window.
142 {
143 static float f = 0.0f;
144 static int counter = 0;
145
146 ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
147
148 ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
149 ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
150 ImGui::Checkbox("Another Window", &show_another_window);
151
152 ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
153 ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
154
155 if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
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 // 3. Show another simple window.
165 if (show_another_window)
166 {
167 ImGui::Begin("Another Window", &show_another_window); // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked)
168 ImGui::Text("Hello from another window!");
169 if (ImGui::Button("Close Me"))
170 show_another_window = false;
171 ImGui::End();
172 }
173
174 // Rendering
175 ImGui::Render();
176 ImGui_ImplMetal_RenderDrawData(ImGui::GetDrawData(), commandBuffer, renderEncoder);
177
178 // Update and Render additional Platform Windows
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 // Cleanup
194 ImGui_ImplMetal_Shutdown();
195 ImGui_ImplGlfw_Shutdown();
196 ImGui::DestroyContext();
197
198 glfwDestroyWindow(window);
199 glfwTerminate();
200
201 return 0;
202}