Last active
March 22, 2025 18:26
-
-
Save erikyuzwa/b2793947896b9ce43cab9562b1a58cec to your computer and use it in GitHub Desktop.
CMakeLists.txt for SDL3 projects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# CMakeLists.txt project file for SDL3 projects | |
# | |
# NB: SDL_image, SDL_ttf and SDL_mixer are not yet tagged properly | |
# in github to be included via this fetch mechanism. | |
# As they are updated, I will endevour to keep this gist updated | |
cmake_minimum_required(VERSION 3.30) | |
project(hello_sdl3_with_cmake C) | |
set(CMAKE_C_STANDARD 23) | |
# Include the command that downloads libraries | |
include(FetchContent) | |
# define a function for adding git dependencies | |
function(include_dependency libName gitURL gitTag) | |
# setup the declare | |
FetchContent_Declare(${libName} | |
GIT_REPOSITORY ${gitURL} | |
GIT_TAG ${gitTag} | |
GIT_SHALLOW TRUE | |
GIT_PROGRESS TRUE | |
) | |
FetchContent_MakeAvailable(${libName}) | |
endfunction() | |
# add SDL3 support | |
find_package(SDL3 QUIET) | |
if (NOT SDL3_FOUND) | |
message(STATUS "Getting SDL3 from Github") | |
include_dependency(SDL3 https://github.com/libsdl-org/SDL.git release-3.2.0) | |
else() | |
message(STATUS "Using local SDL3") | |
endif() | |
add_executable(hello_sdl3_with_cmake WIN32 main.c) | |
# set the include directory | |
target_include_directories(hello_sdl3_with_cmake PUBLIC ${SDL3_INCLUDE_DIRS}) | |
target_compile_definitions(hello_sdl3_with_cmake PRIVATE SDL_MAIN_USE_CALLBACKS) | |
# link all libraries to the project | |
target_link_libraries(hello_sdl3_with_cmake PRIVATE SDL3::SDL3) | |
if (WIN32) | |
add_custom_command( | |
TARGET hello_sdl3_with_cmake POST_BUILD | |
COMMAND "${CMAKE_COMMAND}" -E copy_if_different "$<TARGET_FILE:SDL3::SDL3>" "$<TARGET_FILE_DIR:hello_sdl3_with_cmake>" | |
VERBATIM | |
) | |
endif() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <SDL3/SDL.h> | |
#include <SDL3/SDL_main.h> | |
static SDL_Window *window = NULL; | |
static SDL_Renderer *renderer = NULL; | |
SDL_AppResult SDL_AppInit(void **appstate, int argc, char* argv[]) | |
{ | |
if (SDL_Init(SDL_INIT_VIDEO) == false) | |
{ | |
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't initialize SDL!", SDL_GetError(), NULL); | |
return SDL_APP_FAILURE; | |
} | |
// 800x450 is 16:9 | |
if (SDL_CreateWindowAndRenderer("hello SDL3 with cmake", 800, 450, 0, &window, &renderer) == false) | |
{ | |
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't create window/renderer!", SDL_GetError(), NULL); | |
return SDL_APP_FAILURE; | |
} | |
// return success! | |
return SDL_APP_CONTINUE; | |
} | |
// This function runs when a new event occurs | |
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) | |
{ | |
switch (event->type) | |
{ | |
case SDL_EVENT_QUIT: | |
// end the program, reporting success to the OS | |
return SDL_APP_SUCCESS; | |
case SDL_EVENT_KEY_DOWN: | |
if (event->key.key == SDLK_ESCAPE) | |
{ | |
// end the program on ESC key, | |
// returning success to the OS | |
return SDL_APP_SUCCESS; | |
} | |
default: | |
break; | |
} | |
// return continue to continue | |
return SDL_APP_CONTINUE; | |
} | |
// This function runs once per frame, and is the heart of the program | |
SDL_AppResult SDL_AppIterate(void *appstate) | |
{ | |
SDL_SetRenderDrawColor(renderer, 135, 206, 235, 255); | |
SDL_RenderClear(renderer); | |
SDL_RenderPresent(renderer); | |
// return continue to continue | |
return SDL_APP_CONTINUE; | |
} | |
// This function runs once at shutdown | |
void SDL_AppQuit(void *appstate, SDL_AppResult result) | |
{ | |
// SDL will clean up the window/renderer for us. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment