main.mm 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. // If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp.
  4. // Read online: https://github.com/ocornut/imgui/tree/master/docs
  5. #include "imgui.h"
  6. #include "imgui_impl_sdl.h"
  7. #include "imgui_impl_metal.h"
  8. #include <stdio.h>
  9. #include <SDL.h>
  10. #import <Metal/Metal.h>
  11. #import <QuartzCore/QuartzCore.h>
  12. int main(int, char**)
  13. {
  14. // Setup Dear ImGui context
  15. IMGUI_CHECKVERSION();
  16. ImGui::CreateContext();
  17. ImGuiIO& io = ImGui::GetIO(); (void)io;
  18. //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
  19. //io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
  20. io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
  21. io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows
  22. // Setup style
  23. ImGui::StyleColorsDark();
  24. //ImGui::StyleColorsLight();
  25. // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
  26. ImGuiStyle& style = ImGui::GetStyle();
  27. if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
  28. {
  29. style.WindowRounding = 0.0f;
  30. style.Colors[ImGuiCol_WindowBg].w = 1.0f;
  31. }
  32. // Load Fonts
  33. // - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
  34. // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
  35. // - If the file cannot be loaded, the function will return NULL. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
  36. // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
  37. // - Read 'docs/FONTS.txt' for more instructions and details.
  38. // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
  39. //io.Fonts->AddFontDefault();
  40. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
  41. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
  42. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
  43. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f);
  44. //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
  45. //IM_ASSERT(font != NULL);
  46. // Setup SDL
  47. // (Some versions of SDL before <2.0.10 appears to have performance/stalling issues on a minority of Windows systems,
  48. // depending on whether SDL_INIT_GAMECONTROLLER is enabled or disabled.. updating to latest version of SDL is recommended!)
  49. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0)
  50. {
  51. printf("Error: %s\n", SDL_GetError());
  52. return -1;
  53. }
  54. // Inform SDL that we will be using metal for rendering. Without this hint initialization of metal renderer may fail.
  55. SDL_SetHint(SDL_HINT_RENDER_DRIVER, "metal");
  56. SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL+Metal example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
  57. if (window == NULL)
  58. {
  59. printf("Error creating window: %s\n", SDL_GetError());
  60. return -2;
  61. }
  62. SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
  63. if (renderer == NULL)
  64. {
  65. printf("Error creating renderer: %s\n", SDL_GetError());
  66. return -3;
  67. }
  68. // Setup Platform/Renderer backends
  69. CAMetalLayer* layer = (__bridge CAMetalLayer*)SDL_RenderGetMetalLayer(renderer);
  70. layer.pixelFormat = MTLPixelFormatBGRA8Unorm;
  71. ImGui_ImplMetal_Init(layer.device);
  72. ImGui_ImplSDL2_InitForMetal(window);
  73. id<MTLCommandQueue> commandQueue = [layer.device newCommandQueue];
  74. MTLRenderPassDescriptor* renderPassDescriptor = [MTLRenderPassDescriptor new];
  75. // Our state
  76. bool show_demo_window = true;
  77. bool show_another_window = false;
  78. float clear_color[4] = {0.45f, 0.55f, 0.60f, 1.00f};
  79. // Main loop
  80. bool done = false;
  81. while (!done)
  82. {
  83. @autoreleasepool
  84. {
  85. // Poll and handle events (inputs, window resize, etc.)
  86. // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
  87. // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
  88. // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
  89. // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
  90. SDL_Event event;
  91. while (SDL_PollEvent(&event))
  92. {
  93. ImGui_ImplSDL2_ProcessEvent(&event);
  94. if (event.type == SDL_QUIT)
  95. done = true;
  96. if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window))
  97. done = true;
  98. }
  99. int width, height;
  100. SDL_GetRendererOutputSize(renderer, &width, &height);
  101. layer.drawableSize = CGSizeMake(width, height);
  102. id<CAMetalDrawable> drawable = [layer nextDrawable];
  103. id<MTLCommandBuffer> commandBuffer = [commandQueue commandBuffer];
  104. 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]);
  105. renderPassDescriptor.colorAttachments[0].texture = drawable.texture;
  106. renderPassDescriptor.colorAttachments[0].loadAction = MTLLoadActionClear;
  107. renderPassDescriptor.colorAttachments[0].storeAction = MTLStoreActionStore;
  108. id <MTLRenderCommandEncoder> renderEncoder = [commandBuffer renderCommandEncoderWithDescriptor:renderPassDescriptor];
  109. [renderEncoder pushDebugGroup:@"ImGui demo"];
  110. // Start the Dear ImGui frame
  111. ImGui_ImplMetal_NewFrame(renderPassDescriptor);
  112. ImGui_ImplSDL2_NewFrame();
  113. ImGui::NewFrame();
  114. // 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!).
  115. if (show_demo_window)
  116. ImGui::ShowDemoWindow(&show_demo_window);
  117. // 2. Show a simple window that we create ourselves. We use a Begin/End pair to created a named window.
  118. {
  119. static float f = 0.0f;
  120. static int counter = 0;
  121. ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
  122. ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
  123. ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
  124. ImGui::Checkbox("Another Window", &show_another_window);
  125. ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
  126. ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
  127. if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
  128. counter++;
  129. ImGui::SameLine();
  130. ImGui::Text("counter = %d", counter);
  131. ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
  132. ImGui::End();
  133. }
  134. // 3. Show another simple window.
  135. if (show_another_window)
  136. {
  137. 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)
  138. ImGui::Text("Hello from another window!");
  139. if (ImGui::Button("Close Me"))
  140. show_another_window = false;
  141. ImGui::End();
  142. }
  143. // Rendering
  144. ImGui::Render();
  145. ImGui_ImplMetal_RenderDrawData(ImGui::GetDrawData(), commandBuffer, renderEncoder);
  146. // Update and Render additional Platform Windows
  147. if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
  148. {
  149. ImGui::UpdatePlatformWindows();
  150. ImGui::RenderPlatformWindowsDefault();
  151. }
  152. [renderEncoder popDebugGroup];
  153. [renderEncoder endEncoding];
  154. [commandBuffer presentDrawable:drawable];
  155. [commandBuffer commit];
  156. }
  157. }
  158. // Cleanup
  159. ImGui_ImplMetal_Shutdown();
  160. ImGui_ImplSDL2_Shutdown();
  161. ImGui::DestroyContext();
  162. SDL_DestroyRenderer(renderer);
  163. SDL_DestroyWindow(window);
  164. SDL_Quit();
  165. return 0;
  166. }