Skip to content

Commit

Permalink
Add SDL2 example (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
silbinarywolf authored Sep 22, 2024
1 parent 7f74ebb commit 7133284
Show file tree
Hide file tree
Showing 21 changed files with 1,502 additions and 59 deletions.
46 changes: 32 additions & 14 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,12 @@ jobs:
matrix:
include:
- os: "ubuntu-22.04"
zig: "0.13.0" # aka "latest" at time of writing (2024-09-20)

# Use the nightly build
- os: "ubuntu-22.04"
zig: "master"

runs-on: ${{matrix.os}}

steps:
- uses: actions/checkout@v4

- name: Setup Zig
# note(jae): 2024-09-15
# Uses download mirror first as preferred by Zig Foundation
# see: https://ziglang.org/news/migrate-to-self-hosting/
uses: mlugg/setup-zig@v1
with:
version: ${{matrix.zig}}

- name: Set up JDK 17
uses: actions/setup-java@v3
with:
Expand All @@ -48,6 +35,37 @@ jobs:
with:
packages: 'tools platform-tools platforms;android-35 build-tools;35.0.0 ndk;27.0.12077973'

- name: Build
- name: Setup Zig 0.13.0
# note(jae): 2024-09-15
# Uses download mirror first as preferred by Zig Foundation
# see: https://ziglang.org/news/migrate-to-self-hosting/
uses: mlugg/setup-zig@v1
with:
version: "0.13.0"

- name: Build Minimal Example (Zig 0.13.0)
run: zig build -Dandroid=true --verbose
working-directory: examples/minimal

- name: Build SDL2 Example (Zig 0.13.0)
run: zig build -Dandroid=true --verbose
working-directory: examples/sdl2

- name: Setup Zig Nightly
uses: mlugg/setup-zig@v1
with:
version: "master"

- name: Build Minimal Example (Zig Nightly)
run: zig build -Dandroid=true --verbose
working-directory: examples/minimal

- name: Build SDL2 Example (Zig Nightly)
# "zig build -Dandroid=true" fails for 0.14.0-dev.1632
#
# android/sdk/ndk/27.0.12077973/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/android/hardware_buffer.h:322:42:
# error: expression is not an integral constant expression
#
# See: https://github.com/silbinarywolf/zig-android-sdk/actions/runs/10979711793/job/30484520174?pr=5#step:10:30
run: zig build -Dtarget=x86_64-linux-android --verbose
working-directory: examples/sdl2
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ Add the following to your build.zig.zon file and run `zig build`.

## Examples

* [minimal](examples/minimal): This is based off [ZigAndroidTemplate's minimal](https://github.com/ikskuh/ZigAndroidTemplate/tree/master/examples/minimal) example.
* [minimal](examples/minimal): This is based off ZigAndroidTemplate's minimal example.
* [SDL2](examples/sdl2): This is based off Andrew Kelly's SDL Zig Demo but modified to run on Android, Windows, Mac and Linux.

## Credits

Expand Down
2 changes: 1 addition & 1 deletion examples/minimal/android/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@
</activity>
</application>

</manifest>
</manifest>
2 changes: 1 addition & 1 deletion examples/minimal/android/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
your signing key with this identifier and will prevent updates if the key changes.
-->
<string name="package_name">com.zig.minimal</string>
</resources>
</resources>
44 changes: 16 additions & 28 deletions examples/minimal/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,22 @@ pub fn build(b: *std.Build) void {
const android_apk: ?*android.APK = blk: {
if (android_targets.len == 0) {
break :blk null;
} else {
const android_tools = android.Tools.create(b, .{
.api_level = .android15,
.build_tools_version = "35.0.0",
.ndk_version = "27.0.12077973",
});
const apk = android.APK.create(b, android_tools);

const key_store_file = android_tools.createKeyStore(android.CreateKey.example());
apk.setKeyStore(key_store_file);
apk.setAndroidManifest(b.path("android/AndroidManifest.xml"));
apk.addResourceDirectory(b.path("android/res"));

// Add Java files
apk.addJavaSourceFile(.{ .file = b.path("android/src/NativeInvocationHandler.java") });
}
const android_tools = android.Tools.create(b, .{
.api_level = .android15,
.build_tools_version = "35.0.0",
.ndk_version = "27.0.12077973",
});
const apk = android.APK.create(b, android_tools);

// Example of adding Java files from an external package
// const sdl_dep = b.dependency("sdl", .{
// .optimize = .ReleaseFast,
// .target = android_targets[0],
// });
// const sdl_java_files = sdl_dep.namedWriteFiles("sdljava");
// for (sdl_java_files.files.items) |file| {
// apk.addJavaSourceFile(.{ .file = file.getPath() });
// }
const key_store_file = android_tools.createKeyStore(android.CreateKey.example());
apk.setKeyStore(key_store_file);
apk.setAndroidManifest(b.path("android/AndroidManifest.xml"));
apk.addResourceDirectory(b.path("android/res"));

break :blk apk;
}
// Add Java files
apk.addJavaSourceFile(.{ .file = b.path("android/src/NativeInvocationHandler.java") });
break :blk apk;
};

for (targets) |target| {
Expand Down Expand Up @@ -84,7 +72,7 @@ pub fn build(b: *std.Build) void {
}
}
}
if (android_apk) |ab| {
ab.installApk();
if (android_apk) |apk| {
apk.installApk();
}
}
46 changes: 46 additions & 0 deletions examples/sdl2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# SDL2 Example

This is a copy-paste of [Andrew Kelly's SDL Zig Demo](https://github.com/andrewrk/sdl-zig-demo) but running on Android. The build is setup so you can also target your native operating system as well.

### Build and install to test one target against a local emulator

```sh
zig build -Dtarget=x86_64-linux-android
adb install ./zig-out/bin/sdl-zig-demo.apk
```

### Build and install for all supported Android targets

```sh
zig build -Dandroid=true
adb install ./zig-out/bin/sdl-zig-demo.apk
```

### Build and run natively on your operating system

```sh
zig build run
```

### Uninstall your application

If installing your application fails with something like:
```
adb: failed to install ./zig-out/bin/sdl2.apk: Failure [INSTALL_FAILED_UPDATE_INCOMPATIBLE: Existing package com.zig.sdl2 signatures do not match newer version; ignoring!]
```

```sh
adb uninstall "com.zig.sdl2"
```

### View logs of application

Powershell
```sh
adb logcat | Select-String com.zig.sdl2:
```

Bash
```sh
adb logcat --pid=`adb shell pidof -s com.zig.sdl2`
```
104 changes: 104 additions & 0 deletions examples/sdl2/android/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1"
android:versionName="1.0"
android:installLocation="auto"
package="com.zig.sdl2">
<!--
Based on:
https://github.com/libsdl-org/SDL/blob/release-2.30.7/android-project/app/src/main/AndroidManifest.xml
-->

<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="35" />

<!-- OpenGL ES 2.0 -->
<uses-feature android:glEsVersion="0x00020000" />

<!-- Touchscreen support -->
<uses-feature
android:name="android.hardware.touchscreen"
android:required="false" />

<!-- Game controller support -->
<uses-feature
android:name="android.hardware.bluetooth"
android:required="false" />
<uses-feature
android:name="android.hardware.gamepad"
android:required="false" />
<uses-feature
android:name="android.hardware.usb.host"
android:required="false" />

<!-- External mouse input events -->
<uses-feature
android:name="android.hardware.type.pc"
android:required="false" />

<!-- Audio recording support -->
<!-- if you want to capture audio, uncomment this. -->
<!-- <uses-feature
android:name="android.hardware.microphone"
android:required="false" /> -->

<!-- Allow downloading to the external storage on Android 5.1 and older -->
<!-- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="22" /> -->

<!-- Allow access to Bluetooth devices -->
<!-- Currently this is just for Steam Controller support and requires setting SDL_HINT_JOYSTICK_HIDAPI_STEAM -->
<!-- <uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" /> -->
<!-- <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" /> -->

<!-- Allow access to the vibrator -->
<uses-permission android:name="android.permission.VIBRATE" />

<!-- if you want to capture audio, uncomment this. -->
<!-- <uses-permission android:name="android.permission.RECORD_AUDIO" /> -->

<!-- Create a Java class extending SDLActivity and place it in a
directory under app/src/main/java matching the package, e.g. app/src/main/java/com/gamemaker/game/MyGame.java
then replace "SDLActivity" with the name of your class (e.g. "MyGame")
in the XML below.
An example Java class can be found in README-android.md
-->
<application android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:allowBackup="true"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:hardwareAccelerated="true"
android:debuggable="true">

<!-- Example of setting SDL hints from AndroidManifest.xml:
<meta-data android:name="SDL_ENV.SDL_ACCELEROMETER_AS_JOYSTICK" android:value="0"/>
-->

<activity android:name="ZigSDLActivity"
android:label="@string/app_name"
android:alwaysRetainTaskState="true"
android:launchMode="singleInstance"
android:configChanges="layoutDirection|locale|orientation|uiMode|screenLayout|screenSize|smallestScreenSize|keyboard|keyboardHidden|navigation"
android:preferMinimalPostProcessing="true"
android:exported="true"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- Let Android know that we can handle some USB devices and should receive this event -->
<!-- <intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter> -->
<!-- Drop file event -->
<!--
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="*/*" />
</intent-filter>
-->
</activity>
</application>

</manifest>
Binary file added examples/sdl2/android/res/mipmap/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions examples/sdl2/android/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Pretty name of your app -->
<string name="app_name">Zig SDL2</string>
<!--
This is required for the APK name. This identifies your app, Android will associate
your signing key with this identifier and will prevent updates if the key changes.
-->
<string name="package_name">com.zig.sdl2</string>
</resources>
21 changes: 21 additions & 0 deletions examples/sdl2/android/src/ZigSDLActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.zig.sdl2; // <- Your game package name

import org.libsdl.app.SDLActivity;

/**
* A sample wrapper class that just calls SDLActivity
*/
public class ZigSDLActivity extends SDLActivity {
// @Override
// protected String[] getLibraries() {
// return new String[] {
// "hidapi",
// "SDL2",
// // "SDL2_image",
// // "SDL2_mixer",
// // "SDL2_net",
// // "SDL2_ttf",
// "main"
// };
// }
}
Loading

0 comments on commit 7133284

Please sign in to comment.