imgui

FORK: Dear ImGui: Bloat-free Graphical User interface for C++ with minimal dependencies
git clone https://git.neptards.moe/neptards/imgui.git
Log | Files | Refs

main.mm (8885B)


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