diff --git a/backend_test/example_sdl_renderer3/CMakeLists.txt b/backend_test/example_sdl_renderer3/CMakeLists.txt new file mode 100644 index 0000000..f1e83e1 --- /dev/null +++ b/backend_test/example_sdl_renderer3/CMakeLists.txt @@ -0,0 +1,36 @@ +cmake_minimum_required(VERSION 3.30) +project(cimgui_sdlrenderer3 LANGUAGES C CXX) + +set(CMAKE_C_STANDARD 11) + +include(FetchContent) + +FetchContent_Declare( + sdl3 + GIT_REPOSITORY https://github.com/libsdl-org/SDL.git + GIT_TAG release-3.4.0 + #GIT_SHALLOW TRUE + GIT_PROGRESS TRUE +) + +set(SDL_TEST_LIBRARY OFF CACHE BOOL "" FORCE) +FetchContent_MakeAvailable(sdl3) + +include(../cmake/GenerateCimguiBindings.cmake) + +set(inclulist ${sdl3_SOURCE_DIR}/include) +GenerateCimguiBindings(cimgui_with_backend sdl3 sdlrenderer3 inclulist) +target_link_libraries(cimgui_with_backend PRIVATE SDL3::SDL3) + +add_executable(${PROJECT_NAME} + main.c +) + +target_link_libraries(${PROJECT_NAME} PRIVATE SDL3::SDL3 cimgui_with_backend) +target_compile_definitions( + ${PROJECT_NAME} + PRIVATE + CIMGUI_DEFINE_ENUMS_AND_STRUCTS=1 + CIMGUI_USE_SDL3=1 + CIMGUI_USE_SDLRENDERER3=1 +) diff --git a/backend_test/example_sdl_renderer3/README.md b/backend_test/example_sdl_renderer3/README.md new file mode 100644 index 0000000..17e1567 --- /dev/null +++ b/backend_test/example_sdl_renderer3/README.md @@ -0,0 +1,16 @@ +# SDLRenderer3 + +This example takes from `example_sdlgpu3`. We need to generate bindings for SDLRenderer3 backend because they are not native to `cimgui`. Then you can add the compiled library for linking in your application. + +For the generation phase from cmake you need LuaJIT to be present. + +## Building + +From the build directory of your choice: + +`cmake path_to_example_sdlgpu3` + +Then simply run: + +`make` + diff --git a/backend_test/example_sdl_renderer3/main.c b/backend_test/example_sdl_renderer3/main.c new file mode 100644 index 0000000..4d7fcf1 --- /dev/null +++ b/backend_test/example_sdl_renderer3/main.c @@ -0,0 +1,158 @@ +#include +#include +#include +#include + +#include + +#include +#include + +#define igGetIO igGetIO_Nil + +int main() { + // Setup SDL library + if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD)) { + fprintf(stderr, "Failed to init video! %s", SDL_GetError()); + return 1; + }; + + // Setup window and renderer + float main_scale = SDL_GetDisplayContentScale(SDL_GetPrimaryDisplay()); + SDL_WindowFlags window_flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN | SDL_WINDOW_HIGH_PIXEL_DENSITY; + SDL_Window* window = NULL; + SDL_Renderer* renderer = NULL; + if (!SDL_CreateWindowAndRenderer("Dear ImGui SDL3 Renderer example", (int)(1280 * main_scale), (int)(720 * main_scale), window_flags, &window, &renderer)) + { + printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError()); + return -1; + } + SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED); + SDL_ShowWindow(window); + + // Setup Dear ImGui context + igCreateContext(NULL); + ImGuiIO* io = igGetIO(); (void)io; + io->ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; + io->ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; + + // Setup Dear ImGui style + igStyleColorsDark(NULL); + //igStyleColorsLight(NULL); + + // Setup scaling + ImGuiStyle* style = igGetStyle(); + ImGuiStyle_ScaleAllSizes(style, main_scale); + style->FontScaleDpi = main_scale; // Set initial font scale. (using io.ConfigDpiScaleFonts=true makes this unnecessary. We leave both here for documentation purpose) + io->ConfigDpiScaleFonts = true; // [Experimental] Automatically overwrite style.FontScaleDpi in Begin() when Monitor DPI changes. This will scale fonts but _NOT_ scale sizes/padding for now. + io->ConfigDpiScaleViewports = true; // [Experimental] Scale Dear ImGui and Platform Windows when Monitor DPI changes. + + // Setup Platform/Renderer backends + ImGui_ImplSDL3_InitForSDLRenderer(window, renderer); + ImGui_ImplSDLRenderer3_Init(renderer); + + // Our state + bool show_demo_window = true; + bool show_another_window = false; + ImVec4 clear_color; + clear_color.x = 0.45f; + clear_color.y = 0.55f; + clear_color.z = 0.60f; + clear_color.w = 1.00f; + + // Main loop + bool done = false; + while (!done) + { + // 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. + // [If using SDL_MAIN_USE_CALLBACKS: call ImGui_ImplSDL3_ProcessEvent() from your SDL_AppEvent() function] + SDL_Event event; + while (SDL_PollEvent(&event)) + { + ImGui_ImplSDL3_ProcessEvent(&event); + if (event.type == SDL_EVENT_QUIT) + done = true; + if (event.type == SDL_EVENT_WINDOW_CLOSE_REQUESTED && event.window.windowID == SDL_GetWindowID(window)) + done = true; + } + + // [If using SDL_MAIN_USE_CALLBACKS: all code below would likely be your SDL_AppIterate() function] + if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) + { + SDL_Delay(10); + continue; + } + + // Setup Dear ImGui frame + SDL_SetRenderDrawColorFloat(renderer, clear_color.x, clear_color.y, clear_color.z, clear_color.w); + SDL_RenderClear(renderer); + ImGui_ImplSDLRenderer3_NewFrame(); + ImGui_ImplSDL3_NewFrame(); + igNewFrame(); + + // 1. Show the big demo window (Most of the sample code is in igShowDemoWindow()! You can browse its code to learn more about Dear ImGui!). + if (show_demo_window) + igShowDemoWindow(&show_demo_window); + + // 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window. + { + static float f = 0.0f; + static int counter = 0; + + igBegin("Hello, world!", NULL, 0); // Create a window called "Hello, world!" and append into it. + + igText("This is some useful text."); // Display some text (you can use a format strings too) + igCheckbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state + igCheckbox("Another Window", &show_another_window); + + igSliderFloat("float", &f, 0.0f, 1.0f, "%.3f", 0); // Edit 1 float using a slider from 0.0f to 1.0f + igColorEdit4("clear color", (float*)&clear_color, 0); // Edit 3 floats representing a color + + ImVec2 buttonSize; + buttonSize.x = 0; + buttonSize.y = 0; + if (igButton("Button", buttonSize)) // Buttons return true when clicked (most widgets return true when edited/activated) + counter++; + igSameLine(0.0f, -1.0f); + igText("counter = %d", counter); + + igText("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io->Framerate, io->Framerate); + igEnd(); + } + + // 3. Show another simple window. + if (show_another_window) + { + igBegin("Another Window", &show_another_window, 0); // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked) + igText("Hello from another window!"); + ImVec2 buttonSize; + buttonSize.x = 0; buttonSize.y = 0; + if (igButton("Close Me", buttonSize)) + show_another_window = false; + igEnd(); + } + + // Rendering + igRender(); + ImDrawData* draw_data = igGetDrawData(); + const bool is_minimized = (draw_data->DisplaySize.x <= 0.0f || draw_data->DisplaySize.y <= 0.0f); + ImGui_ImplSDLRenderer3_RenderDrawData(draw_data, renderer); + SDL_RenderPresent(renderer); + } + + // Cleanup + // [If using SDL_MAIN_USE_CALLBACKS: all code below would likely be your SDL_AppQuit() function] + ImGui_ImplSDL3_Shutdown(); + ImGui_ImplSDLRenderer3_Shutdown(); + igDestroyContext(NULL); + + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + SDL_Quit(); + + return 0; +}