Skip to content

Instantly share code, notes, and snippets.

@erikyuzwa
Last active October 19, 2024 15:19
Show Gist options
  • Save erikyuzwa/94f1dae4b822a8c526bb05cf6b46444b to your computer and use it in GitHub Desktop.
Save erikyuzwa/94f1dae4b822a8c526bb05cf6b46444b to your computer and use it in GitHub Desktop.
CMakeLists.txt for SDL2 projects (including SDL2_image, SDL2_ttf)
# CMakeLists.txt project file for SDL2 projects
#
cmake_minimum_required(VERSION 3.30)
project(hello_sdl2_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 SDL2 support
find_package(SDL2 QUIET)
if (NOT SDL2_FOUND)
message(STATUS "Getting SDL2 from Github")
include_dependency(SDL2 https://github.com/libsdl-org/SDL.git release-2.30.8)
else()
message(STATUS "Using local SDL2")
endif()
# add SDL2_image support
find_package(SDL2_image QUIET)
if (NOT SDL2_image_FOUND)
message(STATUS "Getting SDL2_image from Github")
include_dependency(SDL2_image https://github.com/libsdl-org/SDL_image.git release-2.8.2)
else()
message(STATUS "Using local SDL2_image")
endif()
# add SDL2_ttf support
find_package(SDL2_ttf QUIET)
if (NOT SDL2_ttf_FOUND)
message(STATUS "Getting SDL2_ttf from Github")
include_dependency(SDL2_ttf https://github.com/libsdl-org/SDL_ttf.git release-2.22.0)
else()
message(STATUS "Using local SDL2_ttf")
endif()
add_executable(hello_sdl2_with_cmake WIN32 main.c)
# set the include directory
target_include_directories(
hello_sdl2_with_cmake
PUBLIC
${SDL2_INCLUDE_DIRS}
${SDL2_IMAGE_INCLUDE_DIRS}
${SDL2_TTF_INCLUDE_DIRS}
)
# link all libraries to the project
target_link_libraries(
hello_sdl2_with_cmake
PRIVATE
SDL2::SDL2
SDL2::SDL2main
SDL2_image::SDL2_image
SDL2_ttf::SDL2_ttf
)
if (WIN32)
add_custom_command(
TARGET hello_sdl2_with_cmake POST_BUILD
COMMAND "${CMAKE_COMMAND}" -E copy_if_different "$<TARGET_FILE:SDL2::SDL2>" "$<TARGET_FILE_DIR:hello_sdl2_with_cmake>"
COMMAND "${CMAKE_COMMAND}" -E copy_if_different "$<TARGET_FILE:SDL2_image::SDL2_image>" "$<TARGET_FILE_DIR:hello_sdl2_with_cmake>"
COMMAND "${CMAKE_COMMAND}" -E copy_if_different "$<TARGET_FILE:SDL2_ttf::SDL2_ttf>" "$<TARGET_FILE_DIR:hello_sdl2_with_cmake>"
VERBATIM
)
endif()
#include <stdio.h>
#include <stdbool.h>
#include "SDL.h"
#include "SDL_image.h"
#include "SDL_ttf.h"
static SDL_Window *window = NULL;
static SDL_Renderer *renderer = NULL;
int main(int argc, char* argv[]) {
printf("Hello, World!\n");
SDL_Init(SDL_INIT_VIDEO);
// 800x450 is 16:9
SDL_CreateWindowAndRenderer(800, 450, 0, &window, &renderer);
IMG_Init(IMG_INIT_PNG);
TTF_Init();
SDL_Event event;
bool quit = false;
while(!quit) {
while (SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_QUIT:
quit = true;
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_ESCAPE) {
quit = true;
}
break;
}
}
SDL_SetRenderDrawColor(renderer, 135, 206, 235, 255);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
SDL_Delay(1);
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
IMG_Quit();
TTF_Quit();
SDL_Quit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment