main.mm 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. // 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_glfw.h"
  7. #include "imgui_impl_metal.h"
  8. #include <stdio.h>
  9. #define GLFW_INCLUDE_NONE
  10. #define GLFW_EXPOSE_NATIVE_COCOA
  11. #include <GLFW/glfw3.h>
  12. #include <GLFW/glfw3native.h>
  13. #import <Metal/Metal.h>
  14. #import <QuartzCore/QuartzCore.h>
  15. static void glfw_error_callback(int error, const char* description)
  16. {
  17. fprintf(stderr, "Glfw Error %d: %s\n", error, description);
  18. }
  19. int 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. // Setup style
  28. ImGui::StyleColorsDark();
  29. //ImGui::StyleColorsClassic();
  30. // Load Fonts
  31. // - 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.
  32. // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
  33. // - 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).
  34. // - 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.
  35. // - Read 'docs/FONTS.txt' for more instructions and details.
  36. // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
  37. //io.Fonts->AddFontDefault();
  38. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
  39. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
  40. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
  41. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f);
  42. //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
  43. //IM_ASSERT(font != NULL);
  44. // Setup window
  45. glfwSetErrorCallback(glfw_error_callback);
  46. if (!glfwInit())
  47. return 1;
  48. // Create window with graphics context
  49. glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
  50. GLFWwindow* window = glfwCreateWindow(1280, 720, "Dear ImGui GLFW+Metal example", NULL, NULL);
  51. if (window == NULL)
  52. return 1;
  53. id <MTLDevice> device = MTLCreateSystemDefaultDevice();
  54. id <MTLCommandQueue> commandQueue = [device newCommandQueue];
  55. // Setup Platform/Renderer backends
  56. ImGui_ImplGlfw_InitForOpenGL(window, true);
  57. ImGui_ImplMetal_Init(device);
  58. NSWindow *nswin = glfwGetCocoaWindow(window);
  59. CAMetalLayer *layer = [CAMetalLayer layer];
  60. layer.device = device;
  61. layer.pixelFormat = MTLPixelFormatBGRA8Unorm;
  62. nswin.contentView.layer = layer;
  63. nswin.contentView.wantsLayer = YES;
  64. MTLRenderPassDescriptor *renderPassDescriptor = [MTLRenderPassDescriptor new];
  65. // Our state
  66. bool show_demo_window = true;
  67. bool show_another_window = false;
  68. float clear_color[4] = {0.45f, 0.55f, 0.60f, 1.00f};
  69. // Main loop
  70. while (!glfwWindowShouldClose(window))
  71. {
  72. @autoreleasepool
  73. {
  74. // Poll and handle events (inputs, window resize, etc.)
  75. // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
  76. // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
  77. // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
  78. // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
  79. glfwPollEvents();
  80. int width, height;
  81. glfwGetFramebufferSize(window, &width, &height);
  82. layer.drawableSize = CGSizeMake(width, height);
  83. id<CAMetalDrawable> drawable = [layer nextDrawable];
  84. id<MTLCommandBuffer> commandBuffer = [commandQueue commandBuffer];
  85. 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]);
  86. renderPassDescriptor.colorAttachments[0].texture = drawable.texture;
  87. renderPassDescriptor.colorAttachments[0].loadAction = MTLLoadActionClear;
  88. renderPassDescriptor.colorAttachments[0].storeAction = MTLStoreActionStore;
  89. id <MTLRenderCommandEncoder> renderEncoder = [commandBuffer renderCommandEncoderWithDescriptor:renderPassDescriptor];
  90. [renderEncoder pushDebugGroup:@"ImGui demo"];
  91. // Start the Dear ImGui frame
  92. ImGui_ImplMetal_NewFrame(renderPassDescriptor);
  93. ImGui_ImplGlfw_NewFrame();
  94. ImGui::NewFrame();
  95. // 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!).
  96. if (show_demo_window)
  97. ImGui::ShowDemoWindow(&show_demo_window);
  98. // 2. Show a simple window that we create ourselves. We use a Begin/End pair to created a named window.
  99. {
  100. static float f = 0.0f;
  101. static int counter = 0;
  102. ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
  103. ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
  104. ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
  105. ImGui::Checkbox("Another Window", &show_another_window);
  106. ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
  107. ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
  108. if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
  109. counter++;
  110. ImGui::SameLine();
  111. ImGui::Text("counter = %d", counter);
  112. ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
  113. ImGui::End();
  114. }
  115. // 3. Show another simple window.
  116. if (show_another_window)
  117. {
  118. 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)
  119. ImGui::Text("Hello from another window!");
  120. if (ImGui::Button("Close Me"))
  121. show_another_window = false;
  122. ImGui::End();
  123. }
  124. // Rendering
  125. ImGui::Render();
  126. ImGui_ImplMetal_RenderDrawData(ImGui::GetDrawData(), commandBuffer, renderEncoder);
  127. [renderEncoder popDebugGroup];
  128. [renderEncoder endEncoding];
  129. [commandBuffer presentDrawable:drawable];
  130. [commandBuffer commit];
  131. }
  132. }
  133. // Cleanup
  134. ImGui_ImplMetal_Shutdown();
  135. ImGui_ImplGlfw_Shutdown();
  136. ImGui::DestroyContext();
  137. glfwDestroyWindow(window);
  138. glfwTerminate();
  139. return 0;
  140. }