zelda3
zelda3 copied to clipboard
build: use cmake as a build tool + include SDL2 as a submodule
This allows compiling with ease on any supported platform, now requiring zero external dependencies.
This came about after having trouble compiling on Windows due to the MSVC project using a hard-coded path to an SDL2 installation that was not part of the repo.
would this require compiling SDL2 as part of building the game, as opposed to installing the library somewhere as is generally done in other projects?
i think a more idiomatic way (rather than using a git submodule and recursing) to go about using SDL2 with a CMake project, at least that i've seen, is to instruct MSVC users to download the library from https://www.libsdl.org/download-2.0.php and extract it to, say, the project directory, and then
if(MSVC)
set(SDL2_INCLUDE_DIRS ${CMAKE_CURRENT_LIST_DIR}/SDL2/include)
if(CMAKE_SIZEOF_VOID_P EQUAL 8) # 64 bits
link_directories(${CMAKE_CURRENT_LIST_DIR}/SDL2/lib/x64)
else() # 32 bits
link_directories(${CMAKE_CURRENT_LIST_DIR}/SDL2/lib/x86)
endif()
set(SDL2_LIBRARIES SDL2main SDL2)
else()
find_package(SDL2 REQUIRED)
endif()
include_directories(${SDL2_INCLUDE_DIRS})
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARIES})
would this require compiling SDL2 as part of building the game, as opposed to installing the library somewhere as is generally done in other projects?
i think a more idiomatic way (rather than using a git submodule and recursing) to go about using SDL2 with a CMake project, at least that i've seen, is to instruct MSVC users to download the library from https://www.libsdl.org/download-2.0.php and extract it to, say, the project directory, and then
if(MSVC) set(SDL2_INCLUDE_DIRS ${CMAKE_CURRENT_LIST_DIR}/SDL2/include) if(CMAKE_SIZEOF_VOID_P EQUAL 8) # 64 bits link_directories(${CMAKE_CURRENT_LIST_DIR}/SDL2/lib/x64) else() # 32 bits link_directories(${CMAKE_CURRENT_LIST_DIR}/SDL2/lib/x86) endif() set(SDL2_LIBRARIES SDL2main SDL2) else() find_package(SDL2 REQUIRED) endif() include_directories(${SDL2_INCLUDE_DIRS}) add_executable(${PROJECT_NAME} ${SOURCE_FILES}) target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARIES})
In most situations, I find windows users are very unlikely to want to setup external dependencies, however, it is possible to do both approaches. one approach I have done previously is to use findSDL2.cmake, but then fallback to the embedded version if a system SDL library is not found.
# Attempt to find System SDL2
find_package(SDL2)
# If it was not found, use our embedded SDL2 instead
if (NOT SDL2_FOUND)
message(STATUS "SDL2 not installed, using embedded SDL2")
set(SDL_STATIC ON CACHE BOOL "" FORCE)
set(SDL_SHARED OFF CACHE BOOL "" FORCE)
add_subdirectory(3rdParty/SDL2)
set(SDL2_LIBRARY SDL2-static SDL2main)
set(SDL2_INCLUDE_DIR "${SDL2_SOURCE_DIR}/include" "${SDL2_BINARY_DIR}/include")
endif()
I'm not sure how this would work with Nuget on Windows...
I think this is not needed now that NuGet works smoothly on Windows. Please reopen if still an issue.
@snesrev I think this should be merged in. nuget has an old https://www.nuget.org/packages/sdl2 version from 2016 and I couldn't get the master branch compiling for Windows 11 ARM64.
I was able to compile this branch on Windows 11 ARM64 using cmake -G "Visual Studio 17 2022" -A ARM64
cmake also paves the way for other systems like the Switch.
I hope you can reconsider
Thanks