Skip to content

Commit 84e7433

Browse files
committed
Made the setup work for iOS and desktop
1 parent 828c978 commit 84e7433

File tree

16 files changed

+1894
-71
lines changed

16 files changed

+1894
-71
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
.tags
1111

1212
build/
13+
build_ios/
1314
ignored/
1415
makebuild/
1516

Diff for: CMakeLists.txt

+48-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
cmake_minimum_required(VERSION 3.14.7 FATAL_ERROR)
22
cmake_policy(VERSION 3.14.7...3.28.1)
33

4-
project(hello-sdl2-android-ios)
4+
set(PROJECT_NAME hello_sdl2)
5+
project(${PROJECT_NAME})
6+
7+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
8+
9+
if (IOS)
10+
# Disable ImageIO for SDL2_image as it's not available for iOS, just OS X, and will fail at link time
11+
set(SDL2IMAGE_BACKEND_IMAGEIO 0)
12+
endif()
513

614
include(FetchContent)
715

816
FetchContent_Declare(
917
SDL
10-
URL https://github.com/libsdl-org/SDL/archive/refs/tags/release-2.30.8.zip
18+
URL https://github.com/libsdl-org/SDL/archive/refs/tags/release-2.30.10.zip
1119
SOURCE_DIR ${CMAKE_BINARY_DIR}/external/SDL2
1220
)
1321

@@ -17,17 +25,52 @@ FetchContent_Declare(
1725
SOURCE_DIR ${CMAKE_BINARY_DIR}/external/SDL2_image
1826
)
1927

28+
# Only build the shared library for SDL2
29+
set(BUILD_SHARED_LIBS ON)
30+
set(SDL2_DISABLE_SDL2MAIN ON CACHE BOOL "" FORCE)
31+
set(SDL2_DISABLE_UNINSTALL ON CACHE BOOL "" FORCE)
32+
set(SDL_TEST_ENABLED_BY_DEFAULT OFF)
33+
34+
# Make sure debug libs have the same name as release ones
35+
set(SDL_CMAKE_DEBUG_POSTFIX "")
36+
set(SDL2IMAGE_DEBUG_POSTFIX "")
37+
2038
FetchContent_MakeAvailable(SDL SDL_IMAGE)
2139

40+
# Making sure libraries don't generate symlinks
41+
set_property(TARGET SDL2 PROPERTY VERSION)
42+
set_property(TARGET SDL2 PROPERTY SOVERSION)
43+
set_property(TARGET SDL2_image PROPERTY VERSION)
44+
set_property(TARGET SDL2_image PROPERTY SOVERSION)
45+
2246
# Copying needed files for the android project
2347
set(SDL_ACTIVITY_FOLDER app/src/main/java/org/libsdl/app/)
2448
file(REMOVE_RECURSE ${CMAKE_SOURCE_DIR}/android/${SDL_ACTIVITY_FOLDER})
2549
file(COPY ${CMAKE_BINARY_DIR}/external/SDL2/android-project/${SDL_ACTIVITY_FOLDER} DESTINATION ${CMAKE_SOURCE_DIR}/android/${SDL_ACTIVITY_FOLDER})
2650
file(REMOVE_RECURSE ${CMAKE_SOURCE_DIR}/android/app/src/main/assets/res)
2751
file(COPY ${CMAKE_SOURCE_DIR}/res DESTINATION ${CMAKE_SOURCE_DIR}/android/app/src/main/assets)
2852

29-
set(SOURCE_FILES ${CMAKE_SOURCE_DIR}/src/main.cpp)
53+
# Find sources recursively
54+
file(GLOB_RECURSE SOURCE_FILES "src/*.cpp")
55+
file(GLOB_RECURSE HEADER_FILES "src/*.h")
56+
57+
# Add the folders with headers to the include path
58+
foreach (_headerFile ${HEADER_FILES})
59+
get_filename_component(_dir ${_headerFile} PATH)
60+
list (APPEND INCLUDE_DIRS ${_dir})
61+
endforeach()
62+
63+
include_directories(${INCLUDE_DIRS})
64+
65+
if (IOS)
66+
# Remove main.cpp as it's manually added to the XCode project directly as the entry point
67+
list(REMOVE_ITEM SOURCE_FILES ${CMAKE_SOURCE_DIR}/src/main.cpp)
3068

31-
add_library( main SHARED ${SOURCE_FILES} )
69+
add_library( ${PROJECT_NAME} SHARED ${SOURCE_FILES} )
70+
elseif(ANDROID)
71+
add_library( ${PROJECT_NAME} SHARED ${SOURCE_FILES})
72+
else()
73+
add_executable( ${PROJECT_NAME} ${SOURCE_FILES} )
74+
endif()
3275

33-
target_link_libraries( main SDL2 SDL2_image )
76+
target_link_libraries( ${PROJECT_NAME} SDL2 SDL2_image )

Diff for: README.md

+21-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,30 @@
22

33
This is a simple bootstrap project to start using SDL in Android and iOS with CMake
44

5+
The project's source code is split into different files to show how CMake can help support more complex code base structures.
6+
57
## Android
68
This project is an update of the hello world project here https://github.com/stephen47/android-sdl2-gradle-template, as I couldn't find a good way to integrate a CMake built C++ project to an android application.
79
It features a sample hello world using SDL and SDL_image.
810

9-
The project has now been restructured to use a simple flow. To use it, simply open `android` as a project in Android Studio. Compiling and running should work out of the box.
11+
The project has now been restructured to use a simple flow. To use it, simply open `android` as a project in Android Studio, then compile and run from there.
1012

1113
## iOS
12-
The setup was inspired from https://lazyfoo.net/tutorials/SDL/52_hello_mobile/ios_mac/index.php
14+
Similarly to the Android setup, simply open the Xcode project under `ios` and you can compile and run from there.
15+
When you add new files to `src`, you don't need to add them in the Xcode project, CMake will take care of it
16+
17+
The setup was inspired from:
18+
- https://lazyfoo.net/tutorials/SDL/52_hello_mobile/ios_mac/index.php
19+
- https://github.com/leetal/ios-cmake
20+
21+
## Desktop
22+
Desktop also works for Windows, iOS and Linux. Just run
23+
24+
```
25+
mkdir build
26+
cd build
27+
cmake ..
28+
cmake --build .
29+
cd ..
30+
./build/hello_sdl2
31+
```

Diff for: ios/Main.storyboard

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="17150" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
3+
<dependencies>
4+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="17122"/>
5+
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
6+
<capability name="System colors in document resources" minToolsVersion="11.0"/>
7+
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
8+
</dependencies>
9+
<scenes>
10+
<!--View Controller-->
11+
<scene sceneID="s0d-6b-0kx">
12+
<objects>
13+
<viewController id="Y6W-OH-hqX" sceneMemberID="viewController">
14+
<view key="view" contentMode="scaleToFill" id="5EZ-qb-Rvc">
15+
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
16+
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
17+
<viewLayoutGuide key="safeArea" id="vDu-zF-Fre"/>
18+
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
19+
</view>
20+
</viewController>
21+
<placeholder placeholderIdentifier="IBFirstResponder" id="Ief-a0-LHa" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
22+
</objects>
23+
</scene>
24+
</scenes>
25+
<resources>
26+
<systemColor name="systemBackgroundColor">
27+
<color white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
28+
</systemColor>
29+
</resources>
30+
</document>

0 commit comments

Comments
 (0)