macos版本imgui

This commit is contained in:
Nana 2024-06-09 17:13:50 +08:00
parent 65222098d5
commit baaf4c23a3
6 changed files with 150 additions and 20 deletions

View File

@ -1,7 +1,6 @@
project(arona LANGUAGES C CXX)
project(arona LANGUAGES C CXX OBJCXX OBJC)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Windows
#if (WIN32)

View File

@ -37,7 +37,7 @@ void configure_imgui(ImGuiIO& io) {
// - Read 'docs/FONTS.md' for more instructions and details.
// - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
//io.Fonts->AddFontDefault();
io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 24.0f);
//io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 24.0f);
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);

View File

@ -1,7 +1,6 @@
project(imgui LANGUAGES C CXX)
project(imgui LANGUAGES C CXX OBJCXX OBJC)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(ALL_FILES
${CMAKE_CURRENT_SOURCE_DIR}/imgui/imgui.h
@ -33,6 +32,9 @@ elseif (APPLE)
list(APPEND ALL_FILES
${CMAKE_CURRENT_SOURCE_DIR}/imgui/backends/imgui_impl_metal.mm
${CMAKE_CURRENT_SOURCE_DIR}/imgui/backends/imgui_impl_metal.h
${CMAKE_CURRENT_SOURCE_DIR}/imgui/backends/imgui_impl_glfw.cpp
${CMAKE_CURRENT_SOURCE_DIR}/imgui/backends/imgui_impl_glfw.h
${CMAKE_CURRENT_SOURCE_DIR}/imgui_macos_main.mm
)
# linux
else ()
@ -62,7 +64,7 @@ elseif (APPLE)
target_link_libraries(${PROJECT_NAME} PRIVATE "-framework Metal")
target_link_libraries(${PROJECT_NAME} PRIVATE "-framework Cocoa")
target_link_libraries(${PROJECT_NAME} PRIVATE "-framework IOKit")
target_link_libraries(${PROJECT_NAME} PRIVATE "-framework CoreVideo")
target_link_libraries(${PROJECT_NAME} PRIVATE "-framework QuartzCore")
target_link_libraries(${PROJECT_NAME} PRIVATE glfw)
else ()
target_link_libraries(${PROJECT_NAME} PRIVATE vulkan)

View File

@ -0,0 +1,141 @@
// Dear ImGui: standalone example application for GLFW + Metal, using programmable pipeline
// (GLFW is a cross-platform general purpose library for handling windows, inputs, OpenGL/Vulkan/Metal graphics context creation, etc.)
// Learn about Dear ImGui:
// - FAQ https://dearimgui.com/faq
// - Getting Started https://dearimgui.com/getting-started
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
// - Introduction, links and more at the top of imgui.cpp
#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_metal.h"
#include <stdio.h>
#include <thread>
#include "imgui_main.h"
#define GLFW_INCLUDE_NONE
#define GLFW_EXPOSE_NATIVE_COCOA
#include <GLFW/glfw3.h>
#include <GLFW/glfw3native.h>
#import <Metal/Metal.h>
#import <QuartzCore/QuartzCore.h>
static GLFWwindow* g_main_window = nullptr;
static void glfw_error_callback(int error, const char* description)
{
fprintf(stderr, "Glfw Error %d: %s\n", error, description);
}
int init_imgui() {
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
configure_imgui(io);
// Setup window
glfwSetErrorCallback(glfw_error_callback);
if (!glfwInit())
return 1;
// Create window with graphics context
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
g_main_window = glfwCreateWindow(1280, 720, "Dear ImGui GLFW+Metal example", nullptr, nullptr);
if (g_main_window == nullptr)
return 1;
return 0;
}
bool imgui_new_frame() {
return true;
}
void render_imgui() {
}
void shutdown_imgui() {
// Cleanup
ImGui_ImplMetal_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
glfwDestroyWindow(g_main_window);
glfwTerminate();
}
void run_imgui() {
init_imgui();
id <MTLDevice> device = MTLCreateSystemDefaultDevice();
id <MTLCommandQueue> commandQueue = [device newCommandQueue];
// Setup Platform/Renderer backends
ImGui_ImplGlfw_InitForOther(g_main_window, true);
ImGui_ImplMetal_Init(device);
NSWindow* nswin = glfwGetCocoaWindow(g_main_window);
CAMetalLayer* layer = [CAMetalLayer layer];
layer.device = device;
layer.pixelFormat = MTLPixelFormatBGRA8Unorm;
nswin.contentView.layer = layer;
nswin.contentView.wantsLayer = YES;
float clear_color[4] = {0.45f, 0.55f, 0.60f, 1.00f};
MTLRenderPassDescriptor* renderPassDescriptor = [MTLRenderPassDescriptor new];
while (!glfwWindowShouldClose(g_main_window)) {
@autoreleasepool {
// Poll and handle events (inputs, window resize, etc.)
// You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
// - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
// - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
// Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
glfwPollEvents();
std::this_thread::sleep_for(std::chrono::milliseconds(10));
int width, height;
glfwGetFramebufferSize(g_main_window, &width, &height);
layer.drawableSize = CGSizeMake(width, height);
id<CAMetalDrawable> drawable = [layer nextDrawable];
id<MTLCommandBuffer> commandBuffer = [commandQueue commandBuffer];
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]);
renderPassDescriptor.colorAttachments[0].texture = drawable.texture;
renderPassDescriptor.colorAttachments[0].loadAction = MTLLoadActionClear;
renderPassDescriptor.colorAttachments[0].storeAction = MTLStoreActionStore;
id <MTLRenderCommandEncoder> renderEncoder = [commandBuffer renderCommandEncoderWithDescriptor:renderPassDescriptor];
[renderEncoder pushDebugGroup:@"ImGui demo"];
// Start the Dear ImGui frame
ImGui_ImplMetal_NewFrame(renderPassDescriptor);
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ImGuiIO& io = ImGui::GetIO();
tick_imgui(io.DeltaTime);
draw_imgui(io.DeltaTime);
// Rendering
ImGui::Render();
ImGui_ImplMetal_RenderDrawData(ImGui::GetDrawData(), commandBuffer, renderEncoder);
// Update and Render additional Platform Windows
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
ImGui::UpdatePlatformWindows();
ImGui::RenderPlatformWindowsDefault();
}
[renderEncoder popDebugGroup];
[renderEncoder endEncoding];
[commandBuffer presentDrawable:drawable];
[commandBuffer commit];
}
}
shutdown_imgui();
}

View File

@ -12,18 +12,6 @@ extern void shutdown_imgui();
extern void tick_imgui(float delta_time);
inline void run_imgui() {
init_imgui();
while (true) {
if (!imgui_new_frame()) {
break;
}
ImGuiIO& io = ImGui::GetIO();
tick_imgui(io.DeltaTime);
draw_imgui(io.DeltaTime);
render_imgui();
}
shutdown_imgui();
}
extern void run_imgui();
extern std::string get_window_title();

@ -1 +1 @@
Subproject commit ad39426dae033a0e7e855dafb096b002f95469e3
Subproject commit 47ae2613c842650e91e61d8e203737071dad1146