9.7 KB、208行 生 Blame 履歴
1// Dear ImGui: standalone example application for SDL2 + Metal
2// (SDL 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_sdl2.h"
12#include "imgui_impl_metal.h"
13#include <stdio.h>
14#include <SDL.h>
15
16#import <Metal/Metal.h>
17#import <QuartzCore/QuartzCore.h>
18
19int main(int, char**)
20{
21 // Setup Dear ImGui context
22 IMGUI_CHECKVERSION();
23 ImGui::CreateContext();
24 ImGuiIO& io = ImGui::GetIO(); (void)io;
25 io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
26 io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
27 io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
28 io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows
29
30 // Setup style
31 ImGui::StyleColorsDark();
32 //ImGui::StyleColorsLight();
33
34 // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
35 ImGuiStyle& style = ImGui::GetStyle();
36 if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
37 {
38 style.WindowRounding = 0.0f;
39 style.Colors[ImGuiCol_WindowBg].w = 1.0f;
40 }
41
42 // Load Fonts
43 // - If fonts are not explicitly loaded, Dear ImGui will select an embedded font: either AddFontDefaultVector() or AddFontDefaultBitmap().
44 // This selection is based on (style.FontSizeBase * style.FontScaleMain * style.FontScaleDpi) reaching a small threshold.
45 // - You can load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
46 // - 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).
47 // - Read 'docs/FONTS.md' for more instructions and details.
48 // - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use FreeType for higher quality font rendering.
49 // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
50 //style.FontSizeBase = 20.0f;
51 //io.Fonts->AddFontDefaultVector();
52 //io.Fonts->AddFontDefaultBitmap();
53 //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf");
54 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf");
55 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf");
56 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf");
57 //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf");
58 //IM_ASSERT(font != nullptr);
59
60 // Setup SDL
61 // (Some versions of SDL before <2.0.10 appears to have performance/stalling issues on a minority of Windows systems,
62 // depending on whether SDL_INIT_GAMECONTROLLER is enabled or disabled.. updating to latest version of SDL is recommended!)
63 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0)
64 {
65 printf("Error: %s\n", SDL_GetError());
66 return 1;
67 }
68
69 // Inform SDL that we will be using metal for rendering. Without this hint initialization of metal renderer may fail.
70 SDL_SetHint(SDL_HINT_RENDER_DRIVER, "metal");
71
72 // Enable native IME.
73 SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");
74
75 SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL+Metal example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 800, SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
76 if (window == nullptr)
77 {
78 printf("Error creating window: %s\n", SDL_GetError());
79 return -2;
80 }
81
82 SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
83 if (renderer == nullptr)
84 {
85 printf("Error creating renderer: %s\n", SDL_GetError());
86 return -3;
87 }
88
89 // Setup Platform/Renderer backends
90 CAMetalLayer* layer = (__bridge CAMetalLayer*)SDL_RenderGetMetalLayer(renderer);
91 layer.pixelFormat = MTLPixelFormatBGRA8Unorm;
92 ImGui_ImplMetal_Init(layer.device);
93 ImGui_ImplSDL2_InitForMetal(window);
94
95 id<MTLCommandQueue> commandQueue = [layer.device newCommandQueue];
96 MTLRenderPassDescriptor* renderPassDescriptor = [MTLRenderPassDescriptor new];
97
98 // Our state
99 bool show_demo_window = true;
100 bool show_another_window = false;
101 float clear_color[4] = {0.45f, 0.55f, 0.60f, 1.00f};
102
103 // Main loop
104 bool done = false;
105 while (!done)
106 {
107 @autoreleasepool
108 {
109 // Poll and handle events (inputs, window resize, etc.)
110 // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
111 // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
112 // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
113 // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
114 SDL_Event event;
115 while (SDL_PollEvent(&event))
116 {
117 ImGui_ImplSDL2_ProcessEvent(&event);
118 if (event.type == SDL_QUIT)
119 done = true;
120 if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window))
121 done = true;
122 }
123
124 int width, height;
125 SDL_GetRendererOutputSize(renderer, &width, &height);
126 layer.drawableSize = CGSizeMake(width, height);
127 id<CAMetalDrawable> drawable = [layer nextDrawable];
128
129 id<MTLCommandBuffer> commandBuffer = [commandQueue commandBuffer];
130 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]);
131 renderPassDescriptor.colorAttachments[0].texture = drawable.texture;
132 renderPassDescriptor.colorAttachments[0].loadAction = MTLLoadActionClear;
133 renderPassDescriptor.colorAttachments[0].storeAction = MTLStoreActionStore;
134 id <MTLRenderCommandEncoder> renderEncoder = [commandBuffer renderCommandEncoderWithDescriptor:renderPassDescriptor];
135 [renderEncoder pushDebugGroup:@"ImGui demo"];
136
137 // Start the Dear ImGui frame
138 ImGui_ImplMetal_NewFrame(renderPassDescriptor);
139 ImGui_ImplSDL2_NewFrame();
140 ImGui::NewFrame();
141
142 // 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!).
143 if (show_demo_window)
144 ImGui::ShowDemoWindow(&show_demo_window);
145
146 // 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window.
147 {
148 static float f = 0.0f;
149 static int counter = 0;
150
151 ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
152
153 ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
154 ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
155 ImGui::Checkbox("Another Window", &show_another_window);
156
157 ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
158 ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
159
160 if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
161 counter++;
162 ImGui::SameLine();
163 ImGui::Text("counter = %d", counter);
164
165 ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
166 ImGui::End();
167 }
168
169 // 3. Show another simple window.
170 if (show_another_window)
171 {
172 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)
173 ImGui::Text("Hello from another window!");
174 if (ImGui::Button("Close Me"))
175 show_another_window = false;
176 ImGui::End();
177 }
178
179 // Rendering
180 ImGui::Render();
181 ImGui_ImplMetal_RenderDrawData(ImGui::GetDrawData(), commandBuffer, renderEncoder);
182
183 // Update and Render additional Platform Windows
184 if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
185 {
186 ImGui::UpdatePlatformWindows();
187 ImGui::RenderPlatformWindowsDefault();
188 }
189
190 [renderEncoder popDebugGroup];
191 [renderEncoder endEncoding];
192
193 [commandBuffer presentDrawable:drawable];
194 [commandBuffer commit];
195 }
196 }
197
198 // Cleanup
199 ImGui_ImplMetal_Shutdown();
200 ImGui_ImplSDL2_Shutdown();
201 ImGui::DestroyContext();
202
203 SDL_DestroyRenderer(renderer);
204 SDL_DestroyWindow(window);
205 SDL_Quit();
206
207 return 0;
208}