A minimal example project for Dear ImGui with SDL and SDL_image using CMake and Git submodules to keep everything self-contained.
Assumes you've already setup your build toolchain if using CLion
- Clone the repository thusly to also fetch the submodule repositories
# if you're using a relatively recent version of git:
$ git clone --recurse-submodules https://github.com/bploeckelman/sdl_image.git
# otherwise:
$ git clone https://github.com/bploeckelman/sdl_imgui.git
$ cd sdl_imgui
$ git submodule update --init
-
Open the cloned folder in an IDE like Clion and cross your fingers while CMake does it's business...
-
Run the default
sdl_imgui
run configuration and hopefully everything just works!
Finding useful examples of getting SDL up and running in a self-contained way, (ie. without downloading a set of pre-built development binaries, setting up search paths, etc...) has been a real annoyance over the years.
Setting up a repository to include the source for the required libraries as git submodules seems to be a reasonable alternative, but getting SDL and ImGui to play nice with each other using this has been harder than it seems like it should be, so I setup this minimal example project that works out of the box (normal caveats apply, works on my machine, etc...)
This may or may not be a good way to set this stuff up, but on the off chance that it can help others who were running into similar problems to me, then great!
I opened and closed a lot of browser tabs during the development of this repo, here are some that I still have open and found at least somewhat valuable...
- https://github.com/tcbrindle/sdl2-cmake-scripts
- (if you want to try it the old-fashioned way)
- https://github.com/retifrav/sdl-imgui-example
- https://marcelfischer.eu/blog/2019/sdl-opengl-cmake/
- https://marcelfischer.eu/blog/2019/imgui-in-sdl-opengl/
- https://blog.wasin.io/2018/10/31/integrate-cimgui-with-sdl2-in-c-code.html
- https://decovar.dev/blog/2019/05/26/sdl-imgui/
- https://github.com/ocornut/imgui/wiki/Image-Loading-and-Displaying-Examples#example-for-opengl-users
- https://github.com/Tyyppi77/imgui_sdl
- https://github.com/shxy0/SDLImGui
- https://github.com/aaronmjacobs/InitGL
- https://pabloariasal.github.io/2018/02/19/its-time-to-do-cmake-right/
- https://www.youtube.com/watch?v=rLopVhns4Zs
- If using a 32-bit Visual Studio toolchain, there's a symbol conflict on
__ftol2_sse
between SDL and the Microsoft C runtime (MSVCRT)- This can be fixed by adding the following to libs/sdl/CMakeLists.txt:
add_definitions(-DHAVE_LIBC)
- A better solution that doesn't require any changes to the library sources is to just get a 64 bit Visual Studio for your toolchain
- This can be fixed by adding the following to libs/sdl/CMakeLists.txt:
- SDL_image isn't really used in this example but I wanted to have a working example for my own projects. Setting up stb_image would probably be better, though it's a bit more advanced to use so that's left as an exercise for the reader.
- CMake deletes
libs/sdl_image/external/zlib-1.2.11/zconf.h
during the build process, leaving thelibs/sdl_image
dependency in a modified state.- This doesn't cause any problems with the build, but it is unsatisfying to have an unstaged change just hanging out in git.
- There may also be some modifications to
libs/sdl_image
in theXcode/Frameworks/webp.framework
subdirectories but I was able to revert these in the submodule folder and they didn't come back, so ¯\_ (ツ)_/¯ - Added a flag to .gitmodules to ignore a dirty state for
libs/sdl_image
which 'solves' this problem. (open to other suggestions for how to handle these)
- SDL and SDL_image are statically linked
- I know there are issues with this but I find it more convenient than setting up CMake custom commands to copy shared libs into build folders as a post-build step in order to run while developing.
If you'd rather link things dynamically, the following changes may work (untested):
## Remove the cmake variables that trigger static compilation
## - set(SDL_STATIC ON CACHE BOOL "" FORCE)
## - set(SDL_SHARED OFF CACHE BOOL "" FORCE)
## - set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)
## Change the target_link_libraries call to use the non-static SDL2 lib
target_link_libraries(sdl_imgui opengl32 SDL2 SDL2_image imgui)
## NOTE: not sure off the top of my head what cmake vars to use to pick out the correct dll name, so this is windows / debug only
## copy SDL2 dll to the build directory
set(SDL2_DLL "${PROJECT_BINARY_DIR}/libs/sdl/SDL2d.dll")
if (EXISTS ${SDL2_DLL})
add_custom_command(
TARGET sdl_imgui POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${SDL2_DLL}
$<TARGET_FILE_DIR:sdl_imgui>
)
endif()
## copy SDL_image dll and associated dlls to the build directory
set(SDL_IMAGE_DLL "${PROJECT_BINARY_DIR}/libs/sdl_image/SDL2_image.dll")
set(SDL_IMAGE_PNG_DLL "${PROJECT_BINARY_DIR}/libs/sdl_image/external/libpng-1.6.37/libpng16d.dll")
set(SDL_IMAGE_ZLIB_DLL "${PROJECT_BINARY_DIR}/libs/sdl_image/external/zlib-1.2.11/zlibd1.dll")
if (EXISTS ${SDL_IMAGE_DLL}
AND EXISTS ${SDL_IMAGE_PNG_DLL}
AND EXISTS ${SDL_IMAGE_ZLIB_DLL})
add_custom_command(
TARGET sdl_imgui POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${SDL_IMAGE_DLL}
${SDL_IMAGE_PNG_DLL}
${SDL_IMAGE_ZLIB_DLL}
$<TARGET_FILE_DIR:sdl_imgui>
)
endif()