Last active
September 5, 2021 06:39
-
-
Save DanielGibson/ca02b1692499ce4911f7884dc0a67bbf to your computer and use it in GitHub Desktop.
SDL2 + CMake test
This file contains 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
cmake_minimum_required(VERSION 2.8.11) | |
# this can be used to test if both SDL2_LIBRARIES/SDL2_INCLUDE_DIRS | |
# and SDL2::SDL2/SDL2::SDL2main can be used to build an SDL2 application | |
# it also prints all properties of SDL2::SDL2 and SDL2::SDL2main and the | |
# values of SDL2_LIBRARIES/SDL2_LIBDIR/SDL2_INCLUDEDIRS | |
# you might have to disable printing the properties, see comment around line 66 | |
project(SDL2Test) | |
#set(CMAKE_PREFIX_PATH "d:/dev/sdl2inst/cmake") | |
find_package(SDL2 REQUIRED) | |
################################################# | |
# function to print all properties of a target # | |
# based on https://stackoverflow.com/a/56738858 # | |
# Get all propreties that cmake supports | |
execute_process(COMMAND cmake --help-property-list OUTPUT_VARIABLE CMAKE_PROPERTY_LIST) | |
# Convert command output into a CMake list | |
STRING(REGEX REPLACE ";" "\\\\;" CMAKE_PROPERTY_LIST "${CMAKE_PROPERTY_LIST}") | |
STRING(REGEX REPLACE "\n" ";" CMAKE_PROPERTY_LIST "${CMAKE_PROPERTY_LIST}") | |
function(print_properties) | |
message ("CMAKE_PROPERTY_LIST = ${CMAKE_PROPERTY_LIST}") | |
endfunction(print_properties) | |
function(print_target_properties tgt) | |
if(NOT TARGET ${tgt}) | |
message("There is no target named '${tgt}'") | |
return() | |
endif() | |
get_target_property(my_configs ${tgt} IMPORTED_CONFIGURATIONS) | |
#message(STATUS "my_configs = ${my_configs}") | |
foreach (prop ${CMAKE_PROPERTY_LIST}) | |
if(${prop} MATCHES ".*_<CONFIG>$") | |
#message(STATUS "${prop} matches") | |
foreach(cfg ${my_configs}) | |
string(REPLACE "<CONFIG>" ${cfg} propstr ${prop}) | |
#get_property(propval TARGET ${tgt} PROPERTY ${propstr} SET) | |
get_target_property(propval ${tgt} ${propstr}) | |
if (propval) | |
#get_target_property(propval ${tgt} ${propstr}) | |
message ("${tgt} ${propstr} = ${propval}") | |
endif() | |
endforeach(cfg) | |
else() | |
#get_property(propval TARGET ${tgt} PROPERTY ${prop} SET) | |
get_target_property(propval ${tgt} ${prop}) | |
if (propval) | |
#get_target_property(propval ${tgt} ${prop}) | |
message ("${tgt} ${prop} = ${propval}") | |
endif() | |
endif() | |
endforeach(prop) | |
endfunction(print_target_properties) | |
# # | |
################################################# | |
# Note: this does not work for SDL2::SDL2* from sdl2-config.cmake (with my patches applied) when | |
# building with MinGW, because get_target_property() gives errors when called on INTERFACE library targets | |
# (=> comment out these lines when testing that combination) | |
print_target_properties(SDL2::SDL2) | |
print_target_properties(SDL2::SDL2main) | |
message("SDL2_LIBRARIES: ${SDL2_LIBRARIES} SDL2_LIBDIR: ${SDL2_LIBDIR} SDL2_INCLUDE_DIRS: ${SDL2_INCLUDE_DIRS}") | |
add_executable(SDL2Test main.cpp) | |
target_link_libraries(SDL2Test ${SDL2_LIBRARIES}) | |
target_include_directories(SDL2Test PRIVATE ${SDL2_INCLUDE_DIRS}) | |
add_executable(SDL2Test2 main.cpp) | |
target_link_libraries(SDL2Test2 SDL2::SDL2main SDL2::SDL2) # SDL2main must be before SDL2 for linking to work | |
# Note: include directory should be set implicitly by SDL2::SDL2 |
This file contains 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
// just a very simple test that shows a grey window that closes when pressing [Esc] or [Q] | |
// to make sure the application was successfully linked against libSDL2 | |
#include <stdio.h> | |
#include <string.h> | |
#include <SDL.h> | |
int main(int argc, char** argv) | |
{ | |
SDL_Init(SDL_INIT_EVERYTHING); | |
SDL_version ver; | |
SDL_GetVersion(&ver); | |
char windowTitle[128]; | |
snprintf(windowTitle, sizeof(windowTitle), "SDL2 Test - SDL2 %d.%d.%d", (int)ver.major, (int)ver.minor, (int)ver.patch); | |
SDL_Window* win = SDL_CreateWindow(windowTitle, 100, 100, 640, 480, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE); | |
SDL_Surface* winSurf = SDL_GetWindowSurface(win); | |
Uint32 grey = SDL_MapRGB(winSurf->format, 127, 127, 127); | |
SDL_FillRect(winSurf, NULL, grey); | |
SDL_UpdateWindowSurface(win); | |
bool quit = false; | |
while(!quit) { | |
SDL_Event ev; | |
if(SDL_PollEvent(&ev) == 0) { | |
winSurf = SDL_GetWindowSurface(win); | |
SDL_FillRect(winSurf, NULL, grey); | |
SDL_UpdateWindowSurface(win); | |
SDL_Delay(100); | |
continue; | |
} | |
switch(ev.type) { | |
case SDL_QUIT: | |
quit = true; | |
break; | |
case SDL_KEYDOWN: | |
if(ev.key.keysym.sym == SDLK_q || ev.key.keysym.sym == SDLK_ESCAPE) | |
quit = true; | |
break; | |
default: | |
// just ignore all the mouse events and stuff | |
break; | |
} | |
} | |
SDL_DestroyWindow(win); | |
SDL_Quit(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment