main.mm 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. // Setup style
  21. ImGui::StyleColorsDark();
  22. //ImGui::StyleColorsClassic();
  23. // Load Fonts
  24. // - 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.
  25. // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
  26. // - 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).
  27. // - 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.
  28. // - Read 'docs/FONTS.txt' for more instructions and details.
  29. // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
  30. //io.Fonts->AddFontDefault();
  31. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
  32. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
  33. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
  34. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f);
  35. //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
  36. //IM_ASSERT(font != NULL);
  37. // Setup SDL
  38. // (Some versions of SDL before <2.0.10 appears to have performance/stalling issues on a minority of Windows systems,
  39. // depending on whether SDL_INIT_GAMECONTROLLER is enabled or disabled.. updating to latest version of SDL is recommended!)
  40. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0)
  41. {
  42. printf("Error: %s\n", SDL_GetError());
  43. return -1;
  44. }
  45. // Inform SDL that we will be using metal for rendering. Without this hint initialization of metal renderer may fail.
  46. SDL_SetHint(SDL_HINT_RENDER_DRIVER, "metal");
  47. 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);
  48. if (window == NULL)
  49. {
  50. printf("Error creating window: %s\n", SDL_GetError());
  51. return -2;
  52. }
  53. SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
  54. if (renderer == NULL)
  55. {
  56. printf("Error creating renderer: %s\n", SDL_GetError());
  57. return -3;
  58. }
  59. // Setup Platform/Renderer backends
  60. CAMetalLayer* layer = (__bridge CAMetalLayer*)SDL_RenderGetMetalLayer(renderer);
  61. layer.pixelFormat = MTLPixelFormatBGRA8Unorm;
  62. ImGui_ImplMetal_Init(layer.device);
  63. ImGui_ImplSDL2_InitForMetal(window);
  64. id<MTLCommandQueue> commandQueue = [layer.device newCommandQueue];
  65. MTLRenderPassDescriptor* renderPassDescriptor = [MTLRenderPassDescriptor new];
  66. // Our state
  67. bool show_demo_window = true;
  68. bool show_another_window = false;
  69. float clear_color[4] = {0.45f, 0.55f, 0.60f, 1.00f};
  70. // Main loop
  71. bool done = false;
  72. while (!done)
  73. {
  74. @autoreleasepool
  75. {
  76. // Poll and handle events (inputs, window resize, etc.)
  77. // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
  78. // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
  79. // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
  80. // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
  81. SDL_Event event;
  82. while (SDL_PollEvent(&event))
  83. {
  84. ImGui_ImplSDL2_ProcessEvent(&event);
  85. if (event.type == SDL_QUIT)
  86. done = true;
  87. if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window))
  88. done = true;
  89. }
  90. int width, height;
  91. SDL_GetRendererOutputSize(renderer, &width, &height);
  92. layer.drawableSize = CGSizeMake(width, height);
  93. id<CAMetalDrawable> drawable = [layer nextDrawable];
  94. id<MTLCommandBuffer> commandBuffer = [commandQueue commandBuffer];
  95. 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]);
  96. renderPassDescriptor.colorAttachments[0].texture = drawable.texture;
  97. renderPassDescriptor.colorAttachments[0].loadAction = MTLLoadActionClear;
  98. renderPassDescriptor.colorAttachments[0].storeAction = MTLStoreActionStore;
  99. id <MTLRenderCommandEncoder> renderEncoder = [commandBuffer renderCommandEncoderWithDescriptor:renderPassDescriptor];
  100. [renderEncoder pushDebugGroup:@"ImGui demo"];
  101. // Start the Dear ImGui frame
  102. ImGui_ImplMetal_NewFrame(renderPassDescriptor);
  103. ImGui_ImplSDL2_NewFrame();
  104. ImGui::NewFrame();
  105. // 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!).
  106. if (show_demo_window)
  107. ImGui::ShowDemoWindow(&show_demo_window);
  108. // 2. Show a simple window that we create ourselves. We use a Begin/End pair to created a named window.
  109. {
  110. static float f = 0.0f;
  111. static int counter = 0;
  112. ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
  113. ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
  114. ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
  115. ImGui::Checkbox("Another Window", &show_another_window);
  116. ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
  117. ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
  118. if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
  119. counter++;
  120. ImGui::SameLine();
  121. ImGui::Text("counter = %d", counter);
  122. ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
  123. ImGui::End();
  124. }
  125. // 3. Show another simple window.
  126. if (show_another_window)
  127. {
  128. 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)
  129. ImGui::Text("Hello from another window!");
  130. if (ImGui::Button("Close Me"))
  131. show_another_window = false;
  132. ImGui::End();
  133. }
  134. // Rendering
  135. ImGui::Render();
  136. ImGui_ImplMetal_RenderDrawData(ImGui::GetDrawData(), commandBuffer, renderEncoder);
  137. [renderEncoder popDebugGroup];
  138. [renderEncoder endEncoding];
  139. [commandBuffer presentDrawable:drawable];
  140. [commandBuffer commit];
  141. }
  142. }
  143. // Cleanup
  144. ImGui_ImplMetal_Shutdown();
  145. ImGui_ImplSDL2_Shutdown();
  146. ImGui::DestroyContext();
  147. SDL_DestroyRenderer(renderer);
  148. SDL_DestroyWindow(window);
  149. SDL_Quit();
  150. return 0;
  151. }