Skip to content

Commit 2ec1e85

Browse files
committed
Working on android
1 parent 37a3f34 commit 2ec1e85

File tree

141 files changed

+2924
-2336
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

141 files changed

+2924
-2336
lines changed

.gitignore

+3-3
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,9 @@ bin_debug
212212
# Android Studio
213213
.idea
214214
*.iml
215+
*.hprof
216+
gradle
215217
gradlew
216218
gradlew.bat
217-
*.hprof
218219
local.properties
219-
android/gradle
220-
android/app/src/main/jniLibs
220+
android/src/main/jniLibs

CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ cmake_policy(VERSION 3.6)
33
include("cmake/defaults.cmake")
44
set(NAME VulkanCppExamples)
55

6+
67
project(${NAME})
78

89
if (NOT "${CMAKE_SIZEOF_VOID_P}" EQUAL "8")
@@ -13,7 +14,6 @@ include("cmake/compiler.cmake")
1314

1415
find_package(Threads REQUIRED)
1516

16-
1717
# This define is specific to Vulkan has a depth range of [0, 1], unlike OpenGL which has a [-1, 1]
1818
add_definitions(-DGLM_FORCE_DEPTH_ZERO_TO_ONE)
1919

@@ -29,8 +29,8 @@ if (NOT ANDROID)
2929
set_target_properties(SetupDebug PROPERTIES FOLDER "CMakeTargets")
3030
endif()
3131

32-
3332
if (ANDROID)
33+
add_definitions(-DVULKAN_HPP_NO_SMART_HANDLE)
3434
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -u ANativeActivity_onCreate")
3535
set(APP_GLUE_DIR ${ANDROID_NDK}/sources/android/native_app_glue)
3636
include_directories(${APP_GLUE_DIR})

android/app/build.gradle

-50
This file was deleted.

android/app/proguard-rules.pro

-21
This file was deleted.

android/build.gradle

+43-15
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,51 @@
1-
// Top-level build file where you can add configuration options common to all sub-projects/modules.
1+
apply plugin: 'com.android.application'
22

3-
buildscript {
4-
5-
repositories {
6-
google()
7-
jcenter()
3+
android {
4+
compileSdkVersion 26
5+
defaultConfig {
6+
applicationId "org.saintandreas.vulkanexamples"
7+
minSdkVersion 26
8+
targetSdkVersion 26
9+
versionCode 1
10+
versionName "1.0"
11+
ndk { abiFilter "arm64-v8a" }
12+
externalNativeBuild {
13+
cmake {
14+
arguments '-DANDROID_PLATFORM=android-26',
15+
'-DANDROID_TOOLCHAIN=clang',
16+
'-DANDROID_STL=c++_shared',
17+
'-DVULKAN_SDK=' + vulkan_sdk
18+
}
19+
}
20+
compileOptions {
21+
sourceCompatibility JavaVersion.VERSION_1_8
22+
targetCompatibility JavaVersion.VERSION_1_8
23+
}
824
}
9-
dependencies {
10-
classpath 'com.android.tools.build:gradle:3.0.1'
25+
26+
sourceSets {
27+
main {
28+
jniLibs.srcDirs += "${android.ndkDirectory}/sources/third_party/vulkan/src/build-android/jniLibs"
29+
assets.srcDirs += "$projectDir.absolutePath/../data"
30+
}
31+
}
32+
33+
externalNativeBuild {
34+
cmake {
35+
path '../CMakeLists.txt'
36+
}
1137
}
12-
}
1338

14-
allprojects {
15-
repositories {
16-
google()
17-
jcenter()
39+
buildTypes {
40+
release {
41+
minifyEnabled false
42+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
43+
}
1844
}
1945
}
2046

21-
task clean(type: Delete) {
22-
delete rootProject.buildDir
47+
dependencies {
48+
implementation fileTree(dir: 'libs', include: ['*.jar'])
49+
implementation 'com.android.support:appcompat-v7:26.1.0'
50+
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
2351
}

android/settings.gradle

-1
This file was deleted.

android/app/src/main/AndroidManifest.xml android/src/main/AndroidManifest.xml

-6
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,6 @@
4747
android:name=".examples.ComputeShader">
4848
<meta-data android:name="android.app.lib_name" android:value="computeshader" />
4949
</activity>
50-
<!--
51-
<activity android:configChanges="orientation|screenSize|keyboardHidden" android:launchMode="singleTask" android:screenOrientation="landscape" android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
52-
android:name=".examples.Context">
53-
<meta-data android:name="android.app.lib_name" android:value="context" />
54-
</activity>
55-
-->
5650
<activity android:configChanges="orientation|screenSize|keyboardHidden" android:launchMode="singleTask" android:screenOrientation="landscape" android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
5751
android:name=".examples.Deferred">
5852
<meta-data android:name="android.app.lib_name" android:value="deferred" />

base/AndroidNativeApp.hpp

+153
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
//
2+
// Created by Brad on 12/21/2017.
3+
//
4+
5+
#pragma once
6+
#ifndef VULKAN_ANDROIDNATIVEAPP_H
7+
#define VULKAN_ANDROIDNATIVEAPP_H
8+
9+
#include "android_native_app_glue.h"
10+
#include <type_traits>
11+
12+
namespace android {
13+
class NativeApp {
14+
public:
15+
virtual ~NativeApp() {}
16+
17+
protected:
18+
static int32_t inputCallback(struct android_app* app, AInputEvent* event) {
19+
NativeApp* appClass = (NativeApp*)app->userData;
20+
return appClass->onInput(event);
21+
}
22+
23+
static void appCmdCallback(struct android_app* app, int32_t cmd) {
24+
NativeApp* appClass = (NativeApp*)app->userData;
25+
appClass->onCmd(cmd);
26+
}
27+
28+
virtual int32_t onInput(AInputEvent* event) { return 0; }
29+
30+
virtual void onCmd(int32_t cmd) {
31+
switch (cmd) {
32+
case APP_CMD_CONFIG_CHANGED:
33+
onConfigChanged();
34+
break;
35+
case APP_CMD_CONTENT_RECT_CHANGED:
36+
onContentRectChanged();
37+
break;
38+
case APP_CMD_DESTROY:
39+
onDestroy();
40+
break;
41+
case APP_CMD_GAINED_FOCUS:
42+
onGainedFocus();
43+
break;
44+
case APP_CMD_INIT_WINDOW:
45+
onInitWindow();
46+
break;
47+
case APP_CMD_INPUT_CHANGED:
48+
onInputCHanged();
49+
break;
50+
case APP_CMD_LOST_FOCUS:
51+
onLostFocus();
52+
break;
53+
case APP_CMD_LOW_MEMORY:
54+
onLowMemory();
55+
break;
56+
case APP_CMD_PAUSE:
57+
onPause();
58+
break;
59+
case APP_CMD_RESUME:
60+
onResume();
61+
break;
62+
case APP_CMD_SAVE_STATE:
63+
onSaveState();
64+
break;
65+
case APP_CMD_START:
66+
onStart();
67+
break;
68+
case APP_CMD_STOP:
69+
onStop();
70+
break;
71+
case APP_CMD_TERM_WINDOW:
72+
onTermWindow();
73+
break;
74+
case APP_CMD_WINDOW_REDRAW_NEEDED:
75+
onWindowRedrawNeeded();
76+
break;
77+
case APP_CMD_WINDOW_RESIZED:
78+
onWindowResized();
79+
break;
80+
}
81+
}
82+
83+
virtual void onConfigChanged() {}
84+
85+
virtual void onContentRectChanged() {}
86+
87+
virtual void onDestroy() {}
88+
89+
virtual void onGainedFocus() {}
90+
91+
virtual void onInitWindow() {}
92+
93+
virtual void onInputCHanged() {}
94+
95+
virtual void onLostFocus() {}
96+
97+
virtual void onLowMemory() {}
98+
99+
virtual void onPause() {}
100+
101+
virtual void onResume() {}
102+
103+
virtual void onSaveState() {}
104+
105+
virtual void onStart() {}
106+
107+
virtual void onStop() {}
108+
109+
virtual void onTermWindow() {}
110+
111+
virtual void onWindowRedrawNeeded() {}
112+
113+
virtual void onWindowResized() {}
114+
115+
protected:
116+
NativeApp(android_app* app)
117+
: app(app) {
118+
app->userData = this;
119+
app->onAppCmd = appCmdCallback;
120+
app->onInputEvent = inputCallback;
121+
}
122+
123+
android_app* const app;
124+
};
125+
126+
template <typename T>
127+
class NativeStatefulApp : public NativeApp {
128+
using Parent = NativeApp;
129+
// FIXME
130+
// static_assert(std::is_trivally_copyable<T>::value);
131+
132+
public:
133+
NativeStatefulApp(android_app* app)
134+
: Parent(app) {
135+
if (app->savedState != nullptr) {
136+
// We are starting with a previous saved state; restore from it.
137+
state = *reinterpret_cast<const T*>(app->savedState);
138+
}
139+
}
140+
141+
protected:
142+
void onSaveState() override {
143+
size_t size = sizeof(T);
144+
app->savedStateSize = size;
145+
app->savedState = malloc(size);
146+
*((T*)app->savedState) = state;
147+
}
148+
149+
T state;
150+
};
151+
} // namespace android
152+
153+
#endif //VULKAN_ANDROIDNATIVEAPP_H

base/CMakeLists.txt

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
set(TARGET_NAME base)
22

3-
43
set(SHADER_DIR "${PROJECT_SOURCE_DIR}/data/shaders")
54
file(GLOB_RECURSE SHADERS
65
${SHADER_DIR}/*.vert

base/common.hpp

+3-5
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,9 @@ class Vectors {
120120
}
121121
#endif
122122

123-
#define RUN_EXAMPLE(ExampleType) \
124-
ENTRY_POINT_START \
125-
ExampleType* example = new ExampleType(); \
126-
example->run(); \
127-
delete (example); \
123+
#define RUN_EXAMPLE(ExampleType) \
124+
ENTRY_POINT_START \
125+
ExampleType().run(); \
128126
ENTRY_POINT_END
129127

130128
#define VULKAN_EXAMPLE_MAIN() RUN_EXAMPLE(VulkanExample)

base/gl.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#include "gl.hpp"
2+
3+
#if !defined(__ANDROID__)
24
#include <mutex>
35

46
typedef PROC(APIENTRYP PFNWGLGETPROCADDRESS)(LPCSTR);
@@ -95,3 +97,4 @@ void gl::setupDebugLogging() {
9597
glDebugMessageCallback(debugMessageCallback, NULL);
9698
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
9799
}
100+
#endif

base/gl.hpp

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#include "common.hpp"
2+
3+
#if !defined(__ANDROID__)
24
#include <glad/glad.h>
35

46
namespace gl {
@@ -8,3 +10,5 @@ GLuint buildProgram(const std::string& vertexShaderSource, const std::string& fr
810
void report();
911
void setupDebugLogging();
1012
} // namespace gl
13+
14+
#endif

0 commit comments

Comments
 (0)