SDLRenderer3 example working

This commit is contained in:
jammy3855
2026-02-19 21:44:03 -06:00
parent 77f726c6ca
commit ee8fbaaff4
3 changed files with 43 additions and 87 deletions

View File

@@ -1,98 +1,33 @@
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
cmake_minimum_required(VERSION 3.30)
project(cimgui_sdlrenderer3 LANGUAGES C CXX)
set(CMAKE_C_STANDARD 11)
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
sdl3
URL https://github.com/libsdl-org/SDL/releases/download/release-3.2.8/SDL3-3.2.8.tar.gz
)
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)
set(SDL_TEST_LIBRARY OFF CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(sdl3)
#if dynamic SDL3 then install
# install(TARGETS SDL3 RUNTIME DESTINATION ${CMAKE_CURRENT_BINARY_DIR}
# LIBRARY DESTINATION ${CMAKE_CURRENT_BINARY_DIR}
# )
include(../cmake/GenerateCimguiBindings.cmake)
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)
set(inclulist ${sdl3_SOURCE_DIR}/include)
GenerateCimguiBindings(cimgui_with_backend sdl3 sdlrenderer3 inclulist)
target_link_libraries(cimgui_with_backend PRIVATE SDL3::SDL3)
#using library
add_executable(test_sdl main.c)
target_compile_definitions(test_sdl
PUBLIC
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_SDL3=1
CIMGUI_USE_SDLRENDERER3=1
)
if (MINGW)
target_link_options(test_sdl PRIVATE "-mconsole")
endif()
target_link_libraries(test_sdl SDL3-static cimgui_sdl ${IMGUI_LIBRARIES})

View File

@@ -1,2 +1,16 @@
# SDLGPU3
This example is a little different from the others, because `cimgui` doesn't come with bindings for the SDLGPU3 backend out of the box. Instead, this example shows how to generate the necessary bindings during cmake's configure time, then add the compiled library as a target for your application to link to.
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`
and after
`make`
To build use `cmake path_to_example_sdl3_vulkan` and then `make install`

View File

@@ -48,6 +48,7 @@ int main() {
// Setup Platform/Renderer backends
ImGui_ImplSDL3_InitForSDLRenderer(window, renderer);
ImGui_ImplSDLRenderer3_Init(renderer);
// finish loading data
// Our state
@@ -87,6 +88,9 @@ int main() {
}
// Start the 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();
@@ -136,11 +140,14 @@ int main() {
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);