Skip to content

Instantly share code, notes, and snippets.

@Lightnet
Created February 23, 2025 23:48
Show Gist options
  • Save Lightnet/187573a58b66c75e419bbf2e7ceaaf98 to your computer and use it in GitHub Desktop.
Save Lightnet/187573a58b66c75e419bbf2e7ceaaf98 to your computer and use it in GitHub Desktop.
Grok SDL3 build test.

Work in progress test.

# CMake configuration for SDL 3.2.4 project with additional libraries (shared libraries)
# This file sets up a project to build an SDL 3 application in C, fetching SDL and its
# extension libraries (SDL_ttf, SDL_image, SDL_mixer) directly from GitHub repositories.
cmake_minimum_required(VERSION 3.11)
# Notes:
# - CMake 3.11+ is required for FetchContent, which downloads and builds external dependencies.
# - We're using C (not C++) for main.c, which uses SDL 3's callback-based main functions.
project(MySDLProject VERSION 1.0 DESCRIPTION "SDL 3.2.4 C test with callbacks" LANGUAGES C)
# Notes:
# - PROJECT_NAME is set to "MySDLProject", used later as the executable name.
# - VERSION and DESCRIPTION are metadata for the project.
# - LANGUAGES C specifies this is a C project, disabling C++ defaults.
# Set C standard to C11 for modern C features (e.g., safer type handling)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
# Notes:
# - C11 ensures compatibility with SDL 3's C API and modern compilers (VS2022 supports it).
# - REQUIRED ON enforces this standard, failing if the compiler doesn't support it.
# Suppress developer warnings from subprojects (SDL, SDL_ttf, etc.)
set(CMAKE_WARN_DEPRECATED OFF CACHE BOOL "Suppress deprecated warnings" FORCE)
# Notes:
# - SDL's CMake files might emit warnings for maintainers (e.g., deprecated features).
# - This hides them to keep output clean for end-users like us; use -Wno-dev on command line alternatively.
# Enable shared library builds by default (DLLs on Windows)
set(BUILD_SHARED_LIBS ON CACHE BOOL "Default to shared libs" FORCE)
# Notes:
# - BUILD_SHARED_LIBS ON influences all subprojects to build shared libraries (e.g., SDL3.dll).
# - FORCE overrides any cached or default settings, ensuring consistency.
# - Shared libs reduce executable size but require DLLs at runtime.
# Set output directories for binaries and libraries (DLLs go to Debug/Release folder)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/$<CONFIG>")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/$<CONFIG>")
# Notes:
# - CMAKE_BINARY_DIR is the build root (e.g., my_sdl_project/build/).
# - $<CONFIG> is a generator expression (Debug or Release), so DLLs end up in build/Debug/ or build/Release/.
# - This eliminates manual DLL copying after building.
# Include FetchContent module to download and build external dependencies
include(FetchContent)
# Notes:
# - FetchContent allows us to fetch SDL and its extensions from GitHub without manual downloads.
# - It integrates them as subprojects in our build.
# Fetch SDL 3.2.4 from GitHub
FetchContent_Declare(
SDL3
GIT_REPOSITORY https://github.com/libsdl-org/SDL.git
GIT_TAG release-3.2.4 # Stable release from Feb 7, 2025
)
message(STATUS "Fetching and building SDL 3.2.4...")
set(SDL_SHARED ON CACHE BOOL "Build SDL shared library" FORCE) # Builds SDL3.dll
set(SDL_STATIC OFF CACHE BOOL "Build SDL static library" FORCE) # Disables static SDL3.lib
set(SDL_TEST_LIBRARY OFF CACHE BOOL "Build SDL test library" FORCE) # Disables SDL3_test.lib
FetchContent_MakeAvailable(SDL3)
# Notes:
# - SDL3 is the core library, providing windowing, rendering, and event handling.
# - GIT_TAG release-3.2.4 locks to a stable version compatible with SDL 3 extensions.
# - SDL_SHARED ON ensures we get SDL3.dll; SDL_STATIC OFF skips the static version.
# - SDL_TEST_LIBRARY OFF prevents building SDL3_test.lib, a utility library we don’t need.
# - FetchContent_MakeAvailable downloads, configures, and adds SDL3 to the build.
# Fetch SDL_ttf prerelease-3.1.2 for text rendering
FetchContent_Declare(
SDL3_ttf
GIT_REPOSITORY https://github.com/libsdl-org/SDL_ttf.git
GIT_TAG prerelease-3.1.2 # Preview release from Dec 31, 2024
)
message(STATUS "Fetching and building SDL_ttf prerelease-3.1.2...")
set(SDL3TTF_BUILD_SHARED_LIBS ON CACHE BOOL "Build SDL_ttf shared library" FORCE) # Builds SDL3_ttf.dll
set(CMAKE_DISABLE_FIND_PACKAGE_PkgConfig ON CACHE BOOL "Disable PkgConfig" FORCE) # Avoids PkgConfig dependency
FetchContent_MakeAvailable(SDL3_ttf)
# Notes:
# - SDL_ttf enables TrueType font rendering with SDL 3.
# - prerelease-3.1.2 is SDL 3-compatible but not fully stable—use release-3.2.0 for stability if needed.
# - SDL3TTF_BUILD_SHARED_LIBS ON ensures a shared library (SDL3_ttf.dll).
# - Disabling PkgConfig avoids errors on Windows where it’s not standard.
# Fetch SDL_image release-3.2.0 with minimal image support
FetchContent_Declare(
SDL3_image
GIT_REPOSITORY https://github.com/libsdl-org/SDL_image.git
GIT_TAG release-3.2.0 # Stable release from Jan 23, 2025
)
message(STATUS "Fetching and building SDL_image release-3.2.0...")
set(SDL3IMAGE_BUILD_SHARED_LIBS ON CACHE BOOL "Build SDL_image shared library" FORCE) # Builds SDL3_image.dll
# Minimize dependencies by disabling external libraries and vendored builds
set(SDL3IMAGE_VENDORED OFF CACHE BOOL "Use vendored libraries" FORCE) # Don’t build internal libpng, etc.
set(SDL3IMAGE_DEPS_SHARED OFF CACHE BOOL "Link dependencies as shared libs" FORCE) # Avoid shared external deps
# Enable only BMP support (no external libs needed)
set(SDL3IMAGE_BMP ON CACHE BOOL "Support loading BMP images" FORCE)
set(SDL3IMAGE_GIF OFF CACHE BOOL "Support loading GIF images" FORCE)
set(SDL3IMAGE_JPG OFF CACHE BOOL "Support loading JPEG images" FORCE)
set(SDL3IMAGE_PNG OFF CACHE BOOL "Support loading PNG images" FORCE)
set(SDL3IMAGE_SVG OFF CACHE BOOL "Support loading SVG images" FORCE)
set(SDL3IMAGE_TIF OFF CACHE BOOL "Support loading TIFF images" FORCE)
set(SDL3IMAGE_WEBP OFF CACHE BOOL "Support loading WEBP images" FORCE)
set(SDL3IMAGE_AVIF OFF CACHE BOOL "Support loading AVIF images" FORCE)
set(SDL3IMAGE_LBM OFF CACHE BOOL "Support loading LBM images" FORCE)
set(SDL3IMAGE_PCX OFF CACHE BOOL "Support loading PCX images" FORCE)
set(SDL3IMAGE_PNM OFF CACHE BOOL "Support loading PNM images" FORCE)
set(SDL3IMAGE_QOI OFF CACHE BOOL "Support loading QOI images" FORCE)
set(SDL3IMAGE_XCF OFF CACHE BOOL "Support loading XCF images" FORCE)
set(SDL3IMAGE_XPM OFF CACHE BOOL "Support loading XPM images" FORCE)
set(SDL3IMAGE_XV OFF CACHE BOOL "Support loading XV images" FORCE)
# Disable save support (not needed for this test)
set(SDL3IMAGE_JPG_SAVE OFF CACHE BOOL "Support saving JPEG images" FORCE)
set(SDL3IMAGE_PNG_SAVE OFF CACHE BOOL "Support saving PNG images" FORCE)
set(SDL3IMAGE_AVIF_SAVE OFF CACHE BOOL "Support saving AVIF images" FORCE)
set(CMAKE_DISABLE_FIND_PACKAGE_Perl ON CACHE BOOL "Disable Perl" FORCE) # Avoids Perl for some formats
FetchContent_MakeAvailable(SDL3_image)
# Notes:
# - SDL_image adds image loading to SDL 3; BMP-only config avoids libpng, libjpeg, etc.
# - Disabling vendored libs and external deps ensures a lightweight build.
# - Perl is disabled as some formats might require it for build scripts (not BMP).
# Fetch SDL_mixer from main branch with minimal audio support
FetchContent_Declare(
SDL3_mixer
GIT_REPOSITORY https://github.com/libsdl-org/SDL_mixer.git
GIT_TAG main # Latest development branch as of Feb 22, 2025, with SDL 3 support
)
message(STATUS "Fetching and building SDL_mixer from main branch...")
set(SDL3MIXER_BUILD_SHARED_LIBS ON CACHE BOOL "Build SDL_mixer shared library" FORCE) # Builds SDL3_mixer.dll
# Disable external audio formats to minimize dependencies
set(SDL3MIXER_FLAC OFF CACHE BOOL "Support FLAC audio" FORCE) # Requires libFLAC
set(SDL3MIXER_MP3 OFF CACHE BOOL "Support MP3 audio via mpg123" FORCE) # Requires libmpg123
set(SDL3MIXER_OGG OFF CACHE BOOL "Support OGG Vorbis audio" FORCE) # Requires libvorbis/libogg
set(SDL3MIXER_OPUS OFF CACHE BOOL "Support Opus audio" FORCE) # Requires libopus
set(SDL3MIXER_MOD OFF CACHE BOOL "Support MOD audio" FORCE) # Requires external trackers
set(SDL3MIXER_MIDI OFF CACHE BOOL "Support MIDI audio" FORCE) # Requires external MIDI libs
set(SDL3MIXER_WAV ON CACHE BOOL "Support WAV audio" FORCE) # Built-in, no external deps
# Minimize external library usage
set(SDL3MIXER_VENDORED OFF CACHE BOOL "Use vendored libraries" FORCE) # Don’t build internal audio libs
set(SDL3MIXER_DEPS_SHARED OFF CACHE BOOL "Link dependencies as shared libs" FORCE) # Avoid shared external deps
FetchContent_MakeAvailable(SDL3_mixer)
# Notes:
# - SDL_mixer adds audio playback to SDL 3; WAV-only config avoids external audio libraries.
# - Main branch ensures SDL 3 compatibility (release-2.8.1 is SDL 2.x only).
# - Disabling vendored libs keeps the build independent of bundled dependencies.
# Define executable from C source file
add_executable(MySDLProject src/main.c)
# Notes:
# - MySDLProject is the executable name, matching PROJECT_NAME.
# - src/main.c uses SDL 3’s callback framework (SDL_AppInit, etc.).
# Enable SDL_main callbacks for SDL 3’s alternative main entry point
target_compile_definitions(MySDLProject PRIVATE SDL_MAIN_USE_CALLBACKS)
# Notes:
# - SDL_MAIN_USE_CALLBACKS tells SDL to use SDL_App* functions instead of traditional main().
# - PRIVATE scope limits this define to MySDLProject, not affecting subprojects.
# Print include paths for debugging during cmake configuration
message(STATUS "SDL3 include dir: ${sdl3_SOURCE_DIR}/include")
message(STATUS "SDL_ttf include dir: ${sdl3_ttf_SOURCE_DIR}")
message(STATUS "SDL_image include dir: ${sdl3_image_SOURCE_DIR}")
message(STATUS "SDL_mixer include dir: ${sdl3_mixer_SOURCE_DIR}")
# Notes:
# - These messages appear during cmake .., showing where headers are sourced.
# - Helps verify paths like build/_deps/sdl3-src/include/ are correct.
# Set include directories for the compiler to find SDL headers
target_include_directories(MySDLProject PRIVATE
"${sdl3_SOURCE_DIR}/include" # SDL3 headers (SDL3/SDL.h)
"${sdl3_ttf_SOURCE_DIR}" # SDL_ttf headers (SDL3/SDL_ttf.h)
"${sdl3_image_SOURCE_DIR}" # SDL_image headers (SDL3/SDL_image.h)
"${sdl3_mixer_SOURCE_DIR}" # SDL_mixer headers (SDL3/SDL_mixer.h)
)
# Notes:
# - PRIVATE scope ensures these paths are only for MySDLProject.
# - Paths point to fetched source directories (e.g., build/_deps/sdl3-src/include/).
# - Headers like SDL3/SDL.h are in these dirs after FetchContent downloads them.
# Link SDL libraries to the executable using modern SDL 3 target names
target_link_libraries(MySDLProject PRIVATE
SDL3::SDL3 # Core SDL 3 library (SDL3.dll)
SDL3_ttf::SDL3_ttf # SDL_ttf library (SDL3_ttf.dll)
SDL3_image::SDL3_image # SDL_image library (SDL3_image.dll)
SDL3_mixer::SDL3_mixer # SDL_mixer library (SDL3_mixer.dll)
)
# Notes:
# - SDL3::SDL3 is the CMake target for the shared SDL 3 library, set by FetchContent.
# - Similarly, SDL3_ttf::SDL3_ttf, etc., are targets for their respective shared libs.
# - PRIVATE scope means these libs are only linked to MySDLProject, not exposed publicly.
# - Ensures runtime linking to DLLs in build/Debug/.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment