Skip to content

Commit 1f64345

Browse files
committed
Add Android platform support, Vulkan integration, and improved input handling.
- Integrated Vulkan for Android with proper feature and resource management. - Updated input handling to process Android-specific events. - Added CMake and Gradle configurations for Android build setups. - Refactored cross-platform code to isolate Android-specific logic.
1 parent d4b75b3 commit 1f64345

31 files changed

+953
-1144
lines changed

attachments/simple_engine/CMakeLists.txt

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,21 @@ project(SimpleEngine VERSION 1.0.0 LANGUAGES CXX C)
88
# Add CMake module path for custom find modules
99
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../CMake")
1010

11+
if(ANDROID)
12+
include(FetchContent)
13+
FetchContent_Declare(
14+
VulkanHeaders
15+
GIT_REPOSITORY https://github.com/KhronosGroup/Vulkan-Headers.git
16+
GIT_TAG v1.3.275 # Or a specific tag/commit This needs to correspond to the NDK version of Vulkan.
17+
)
18+
FetchContent_MakeAvailable(VulkanHeaders)
19+
endif ()
20+
1121
# Find required packages
12-
find_package (glfw3 REQUIRED)
1322
find_package (glm REQUIRED)
1423
find_package (Vulkan REQUIRED)
1524
find_package (tinygltf REQUIRED)
1625
find_package (KTX REQUIRED)
17-
find_package (OpenAL REQUIRED)
18-
19-
# set up Vulkan C++ module
20-
add_library(VulkanCppModule)
21-
add_library(Vulkan::cppm ALIAS VulkanCppModule)
22-
23-
target_compile_definitions(VulkanCppModule
24-
PUBLIC VULKAN_HPP_DISPATCH_LOADER_DYNAMIC=1 VULKAN_HPP_NO_STRUCT_CONSTRUCTORS=1
25-
)
26-
target_include_directories(VulkanCppModule
27-
PRIVATE
28-
"${Vulkan_INCLUDE_DIR}"
29-
)
30-
target_link_libraries(VulkanCppModule
31-
PUBLIC
32-
Vulkan::Vulkan
33-
)
34-
35-
set_target_properties(VulkanCppModule PROPERTIES CXX_STANDARD 20)
36-
37-
target_sources(VulkanCppModule
38-
PUBLIC
39-
FILE_SET cxx_modules TYPE CXX_MODULES
40-
BASE_DIRS
41-
"${Vulkan_INCLUDE_DIR}"
42-
FILES
43-
"${Vulkan_INCLUDE_DIR}/vulkan/vulkan.cppm"
44-
)
45-
4626

4727

4828
# Platform-specific settings
@@ -52,6 +32,8 @@ if(ANDROID)
5232
else()
5333
# Desktop-specific settings
5434
add_definitions(-DPLATFORM_DESKTOP)
35+
find_package (glfw3 REQUIRED)
36+
find_package (OpenAL REQUIRED)
5537
endif()
5638

5739
# Shader compilation
@@ -117,22 +99,42 @@ set(SOURCES
11799
mikktspace.c
118100
)
119101

120-
# Create executable
121-
add_executable(SimpleEngine ${SOURCES})
102+
# Create executable or library based on the platform
103+
if(ANDROID)
104+
add_library(SimpleEngine STATIC ${SOURCES})
105+
else()
106+
add_executable(SimpleEngine ${SOURCES})
107+
endif()
108+
122109
add_dependencies(SimpleEngine shaders)
123110
set_target_properties (SimpleEngine PROPERTIES CXX_STANDARD 20)
124111

125112
# Link libraries
126-
target_link_libraries(SimpleEngine PRIVATE
127-
Vulkan::cppm
113+
target_link_libraries(SimpleEngine PUBLIC
114+
Vulkan::Vulkan
115+
Vulkan::Headers
128116
glm::glm
129117
tinygltf::tinygltf
130118
KTX::ktx
131-
OpenAL::OpenAL
132119
)
133120

134-
if(NOT ANDROID)
135-
target_link_libraries(SimpleEngine PRIVATE glfw)
121+
if(ANDROID)
122+
target_link_libraries(SimpleEngine PUBLIC
123+
android
124+
log
125+
EGL
126+
GLESv2
127+
game-activity::game-activity
128+
OpenSLES
129+
)
130+
target_include_directories(SimpleEngine PUBLIC
131+
${VulkanHeaders_SOURCE_DIR}/include
132+
${ANDROID_NDK}/sources/android/native_app_glue
133+
)
134+
target_compile_definitions(SimpleEngine PRIVATE VULKAN_HPP_NO_STRUCT_CONSTRUCTORS)
135+
else()
136+
target_link_libraries(SimpleEngine PRIVATE glfw OpenAL::OpenAL)
137+
target_compile_definitions(SimpleEngine PRIVATE VULKAN_HPP_NO_STRUCT_CONSTRUCTORS VULKAN_HPP_DISPATCH_LOADER_DYNAMIC)
136138
endif()
137139

138140
# Copy model and texture files if they exist
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
= Android Project for Vulkan Tutorial
2+
3+
This Android project allows you to run different chapters of the Vulkan Tutorial on Android devices.
4+
5+
== Selecting a Chapter
6+
7+
By default, the project builds and runs the `34_android` chapter. You can select a different chapter by setting the `chapter` property in your Gradle build.
8+
9+
=== Available Chapters
10+
11+
* `34_android`: The Android chapter that uses tinyobjloader to load OBJ models
12+
* `35_gltf_ktx`: The glTF and KTX chapter that uses tinygltf to load glTF models and KTX to load KTX2 textures
13+
14+
=== How to Select a Chapter
15+
16+
==== From the Command Line
17+
18+
[source,bash]
19+
----
20+
./gradlew assembleDebug -Pchapter=35_gltf_ktx
21+
----
22+
23+
==== From Android Studio
24+
25+
1. Edit the `gradle.properties` file in the project root directory
26+
2. Add the following line:
27+
+
28+
[source]
29+
----
30+
chapter=35_gltf_ktx
31+
----
32+
3. Sync the project and build
33+
34+
== Adding New Chapters
35+
36+
To add support for a new chapter:
37+
38+
1. Add the chapter name to the `SUPPORTED_CHAPTERS` list in `app/src/main/cpp/CMakeLists.txt`
39+
2. Add any chapter-specific libraries and compile definitions in the same file
40+
3. Make sure the chapter's source file exists in the `attachments` directory
41+
42+
For example, to add support for a hypothetical `36_new_feature` chapter:
43+
44+
[source,cmake]
45+
----
46+
# Define the list of supported chapters
47+
set(SUPPORTED_CHAPTERS
48+
"34_android"
49+
"35_gltf_ktx"
50+
"36_new_feature"
51+
)
52+
53+
# Add chapter-specific libraries and definitions
54+
if(CHAPTER STREQUAL "34_android")
55+
# ...
56+
elseif(CHAPTER STREQUAL "35_gltf_ktx")
57+
# ...
58+
elseif(CHAPTER STREQUAL "36_new_feature")
59+
target_link_libraries(vulkan_tutorial_android
60+
# Add any required libraries here
61+
)
62+
63+
target_compile_definitions(vulkan_tutorial_android PRIVATE
64+
# Add any required compile definitions here
65+
)
66+
endif()
67+
----
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
plugins {
2+
id 'com.android.application'
3+
}
4+
5+
android {
6+
namespace "com.simple_engine"
7+
compileSdk 36
8+
defaultConfig {
9+
applicationId "com.simple_engine"
10+
minSdk 24
11+
targetSdk 36
12+
versionCode 1
13+
versionName "1.0"
14+
15+
externalNativeBuild {
16+
cmake {
17+
abiFilters 'arm64-v8a', 'x86_64'
18+
}
19+
}
20+
}
21+
22+
buildTypes {
23+
release {
24+
minifyEnabled false
25+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
26+
}
27+
}
28+
29+
compileOptions {
30+
sourceCompatibility JavaVersion.VERSION_11
31+
targetCompatibility JavaVersion.VERSION_11
32+
}
33+
34+
externalNativeBuild {
35+
cmake {
36+
path "src/main/cpp/CMakeLists.txt"
37+
version "4.0.2+"
38+
}
39+
}
40+
41+
ndkVersion "28.1.13356709"
42+
43+
// Use assets from the dedicated assets directory and locally compiled shaders
44+
sourceSets {
45+
main {
46+
assets {
47+
srcDirs = [
48+
// Point to the dedicated assets directory
49+
'../../Assets/'
50+
]
51+
}
52+
}
53+
}
54+
buildFeatures {
55+
prefab true
56+
buildConfig true
57+
}
58+
}
59+
60+
dependencies {
61+
implementation 'androidx.appcompat:appcompat:1.7.1'
62+
implementation 'com.google.android.material:material:1.12.0'
63+
implementation 'androidx.games:games-activity:4.0.0'
64+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
3+
<!-- Declare that this app uses Vulkan -->
4+
<uses-feature android:name="android.hardware.vulkan.version" android:version="0x400003" android:required="true" />
5+
<uses-feature android:name="android.hardware.vulkan.level" android:version="0" android:required="true" />
6+
7+
<application
8+
android:allowBackup="true"
9+
android:fullBackupContent="@xml/backup_rules"
10+
android:dataExtractionRules="@xml/data_extraction_rules"
11+
android:label="@string/app_name"
12+
android:supportsRtl="true"
13+
android:theme="@style/AppTheme">
14+
<activity
15+
android:name=".VulkanActivity"
16+
android:configChanges="orientation|keyboardHidden|screenSize"
17+
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
18+
android:exported="true">
19+
<intent-filter>
20+
<action android:name="android.intent.action.MAIN" />
21+
<category android:name="android.intent.category.LAUNCHER" />
22+
</intent-filter>
23+
<meta-data
24+
android:name="android.app.lib_name"
25+
android:value="simple_engine_android" />
26+
</activity>
27+
</application>
28+
29+
</manifest>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
cmake_minimum_required(VERSION 3.22.1)
2+
3+
project(simple_engine_android)
4+
5+
# Add the parent project's cmake folder to the module path
6+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../attachments/CMake")
7+
8+
# Include the game-activity library
9+
find_package(game-activity REQUIRED CONFIG)
10+
11+
# Set C++ standard to match the main project
12+
set(CMAKE_CXX_STANDARD 20)
13+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
14+
15+
# Add the simple_engine project as a subdirectory
16+
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/../../../../.." simple_engine_build)
17+
18+
# Add the main native library
19+
add_library(simple_engine_android SHARED
20+
game_activity_bridge.cpp
21+
)
22+
23+
# Link against libraries
24+
target_link_libraries(simple_engine_android
25+
SimpleEngine
26+
game-activity::game-activity
27+
android
28+
log
29+
)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Intentionally empty bridge: rely entirely on GameActivity's native_app_glue
2+
// provided by the prefab (libgame-activity). That glue will invoke our
3+
// android_main(android_app*) defined in main.cpp. Defining another
4+
// GameActivity_onCreate here causes duplicate symbol linker errors.
5+
// Keeping a translation unit avoids removing the target from CMake.
6+
7+
#include <android/log.h>
8+
9+
#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "SimpleEngine", __VA_ARGS__))
10+
#define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "SimpleEngine", __VA_ARGS__))
11+
#define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, "SimpleEngine", __VA_ARGS__))
12+
13+
// Nothing to do here.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.simple_engine;
2+
3+
import android.os.Bundle;
4+
import android.view.WindowManager;
5+
import com.google.androidgamesdk.GameActivity;
6+
7+
public class VulkanActivity extends GameActivity {
8+
@Override
9+
protected void onCreate(Bundle savedInstanceState) {
10+
super.onCreate(savedInstanceState);
11+
12+
// Keep the screen on while the app is running
13+
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
14+
}
15+
16+
// Load the native library
17+
static {
18+
System.loadLibrary("simple_engine_android");
19+
}
20+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<resources>
2+
<string name="app_name">Simple Engine</string>
3+
</resources>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<resources>
2+
<!-- Base application theme -->
3+
<style name="AppTheme" parent="android:Theme.Material.Light.NoActionBar">
4+
<!-- Customize your theme here -->
5+
</style>
6+
</resources>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<full-backup-content>
3+
<!-- Backup everything by default -->
4+
<include domain="root" path="."/>
5+
<include domain="file" path="."/>
6+
<include domain="database" path="."/>
7+
<include domain="sharedpref" path="."/>
8+
<include domain="external" path="."/>
9+
</full-backup-content>

0 commit comments

Comments
 (0)