Started SDL3 Renderer example

This commit is contained in:
jammy3855
2026-02-17 22:13:46 -06:00
parent 99f8065703
commit 77f726c6ca
3 changed files with 251 additions and 0 deletions

View File

@@ -0,0 +1,98 @@
cmake_minimum_required(VERSION 3.5)
Project(cimgui_sdl3_renderer)
if(WIN32) # to make mingw work as all the others
set(CMAKE_SHARED_LIBRARY_PREFIX "")
endif(WIN32)
# general settings
if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/../../imgui/backends)
set(BACKENDS_FOLDER "../../imgui/backends/")
else()
set(BACKENDS_FOLDER "../../imgui/examples/")
endif()
if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/../../imgui/imgui_tables.cpp)
set(TABLES_SOURCE "../../imgui/imgui_tables.cpp")
else()
set(TABLES_SOURCE "")
endif()
include_directories(../../imgui ../../imgui/backends)
include_directories(../../)
set(IMGUI_SOURCES ../../cimgui.cpp
../../cimgui_impl.cpp
../../imgui/imgui.cpp
../../imgui/imgui_draw.cpp
../../imgui/imgui_demo.cpp
../../imgui/imgui_widgets.cpp
${TABLES_SOURCE}
)
set(IMGUI_SOURCES_sdl)
set(IMGUI_LIBRARIES )
#optional adding freetype
option(IMGUI_FREETYPE "add Freetype2" OFF)
if(IMGUI_FREETYPE)
FIND_PACKAGE(freetype REQUIRED PATHS ${FREETYPE_PATH})
list(APPEND IMGUI_LIBRARIES freetype)
list(APPEND IMGUI_SOURCES ../../imgui/misc/freetype/imgui_freetype.cpp)
add_definitions("-DCIMGUI_FREETYPE=1")
endif(IMGUI_FREETYPE)
#sdl3
list(APPEND IMGUI_SOURCES ${BACKENDS_FOLDER}imgui_impl_sdl3.cpp)
include(FetchContent)
Set(FETCHCONTENT_QUIET FALSE)
FetchContent_Declare(
SDL3
GIT_REPOSITORY https://github.com/libsdl-org/SDL.git
GIT_TAG release-3.2.8
#GIT_SHALLOW TRUE
GIT_PROGRESS TRUE
)
FetchContent_GetProperties(SDL3)
if (NOT sdl3_POPULATED)
set(FETCHCONTENT_QUIET NO)
FetchContent_Populate(SDL3)
set(SDL_TEST OFF CACHE BOOL "" FORCE)
set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)
add_subdirectory(${sdl3_SOURCE_DIR} ${sdl3_BINARY_DIR})
endif()
include_directories(${SDL3_SOURCE_DIR}/include)
#if dynamic SDL3 then install
# install(TARGETS SDL3 RUNTIME DESTINATION ${CMAKE_CURRENT_BINARY_DIR}
# LIBRARY DESTINATION ${CMAKE_CURRENT_BINARY_DIR}
# )
add_library(cimgui_sdl STATIC ${IMGUI_SOURCES})
target_compile_definitions(cimgui_sdl PUBLIC "-DIMGUI_USER_CONFIG=\"../cimconfig.h\"")
target_compile_definitions(cimgui_sdl PUBLIC "-DIMGUI_DISABLE_OBSOLETE_FUNCTIONS=1")
target_compile_definitions(cimgui_sdl PUBLIC -DCIMGUI_USE_SDL3)
if (WIN32)
target_compile_definitions(cimgui_sdl PUBLIC "-DIMGUI_IMPL_API=extern \"C\" __declspec\(dllexport\)")
else(WIN32)
target_compile_definitions(cimgui_sdl PUBLIC "-DIMGUI_IMPL_API=extern \"C\" ")
endif(WIN32)
#target_link_libraries(cimgui_sdl ${IMGUI_LIBRARIES} SDL3-static)
#using library
add_executable(test_sdl main.c)
target_compile_definitions(test_sdl
PUBLIC
CIMGUI_DEFINE_ENUMS_AND_STRUCTS=1
CIMGUI_USE_SDL3=1
)
if (MINGW)
target_link_options(test_sdl PRIVATE "-mconsole")
endif()
target_link_libraries(test_sdl SDL3-static cimgui_sdl ${IMGUI_LIBRARIES})

View File

@@ -0,0 +1,2 @@
To build use `cmake path_to_example_sdl3_vulkan` and then `make install`

View File

@@ -0,0 +1,151 @@
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <SDL3/SDL.h>
#include <cimgui.h>
#include <cimgui_impl.h>
#define igGetIO igGetIO_Nil
int main() {
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD)) {
fprintf(stderr, "Failed to init video! %s", SDL_GetError());
return 1;
};
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
//IMGUI_CHECKVERSION();
igCreateContext(NULL);
ImGuiIO* io = igGetIO(); (void)io;
io->ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io->ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
// Setup Dear ImGui style
igStyleColorsDark(NULL);
//igStyleColorsLight(NULL);
// Setup scaling
ImGuiStyle* style = igGetStyle();
ImGuiStyle_ScaleAllSizes(style, main_scale); // Bake a fixed style scale. (until we have a solution for dynamic style scaling, changing this requires resetting Style + calling this again)
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);
// finish loading data
// 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;
}
// Start the Dear ImGui frame
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);
}
// Cleanup
// [If using SDL_MAIN_USE_CALLBACKS: all code below would likely be your SDL_AppQuit() function]
ImGui_ImplSDL3_Shutdown();
igDestroyContext(NULL);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}