Skip to content

Commit

Permalink
IOS, Android... same thing (#7493)
Browse files Browse the repository at this point in the history
# Objective

- Merge the examples on iOS and Android
- Make sure they both work from the same code

## Solution

- don't create window when not in an active state (from #6830)
- exit on suspend on Android (from #6830)
- automatically enable dependency feature of bevy_audio on android so that it works out of the box
- don't inverse y position of touch events
- reuse the same example for both Android and iOS

Fixes #4616
Fixes #4103
Fixes #3648
Fixes #3458
Fixes #3249
Fixes #86
  • Loading branch information
mockersf committed Feb 6, 2023
1 parent 4fd092f commit d3188e2
Show file tree
Hide file tree
Showing 22 changed files with 73 additions and 130 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/post-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
--no-push \
--exclude ci \
--exclude errors \
--exclude bevy-ios-example \
--exclude bevy_mobile_example \
--exclude build-wasm-example
- name: Create PR
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
--dependent-version upgrade \
--exclude ci \
--exclude errors \
--exclude bevy-ios-example \
--exclude bevy_mobile_example \
--exclude build-wasm-example
- name: Create PR
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/validation-jobs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
run: rustup target add aarch64-apple-ios x86_64-apple-ios

- name: Build and install iOS app in iOS Simulator.
run: cd examples/ios && make install
run: cd examples/mobile && make install

build-android:
runs-on: ubuntu-latest
Expand All @@ -56,7 +56,7 @@ jobs:
run: cargo install --force cargo-apk

- name: Build APK
run: ANDROID_NDK_ROOT=$ANDROID_NDK_LATEST_HOME cargo apk build --package bevy-android-example
run: ANDROID_NDK_ROOT=$ANDROID_NDK_LATEST_HOME cargo apk build --package bevy_mobile_example

run-examples-on-windows-dx12:
runs-on: windows-latest
Expand Down
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ rust-version = "1.67.0"
exclude = ["benches", "crates/bevy_ecs_compile_fail_tests", "crates/bevy_reflect_compile_fail_tests"]
members = [
"crates/*",
"examples/android",
"examples/ios",
"examples/mobile",
"tools/ci",
"tools/build-example-pages",
"tools/build-wasm-example",
Expand All @@ -44,6 +43,7 @@ default = [
"vorbis",
"x11",
"filesystem_watcher",
"android_shared_stdcxx"
]

# Force dynamic linking, which improves iterative compile times
Expand Down Expand Up @@ -118,6 +118,9 @@ debug_asset_server = ["bevy_internal/debug_asset_server"]
# Enable animation support, and glTF animation loading
animation = ["bevy_internal/animation"]

# Enable using a shared stdlib for cxx on Android.
android_shared_stdcxx = ["bevy_internal/android_shared_stdcxx"]

[dependencies]
bevy_dylib = { path = "crates/bevy_dylib", version = "0.9.0", default-features = false, optional = true }
bevy_internal = { path = "crates/bevy_internal", version = "0.9.0", default-features = false }
Expand Down
5 changes: 5 additions & 0 deletions crates/bevy_audio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ anyhow = "1.0.4"
rodio = { version = "0.16", default-features = false }
parking_lot = "0.12.1"

[target.'cfg(target_os = "android")'.dependencies]
oboe = { version = "0.4", optional = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
rodio = { version = "0.16", default-features = false, features = ["wasm-bindgen"] }

Expand All @@ -37,3 +40,5 @@ symphonia-isomp4 = ["rodio/symphonia-isomp4"]
symphonia-mp3 = ["rodio/symphonia-mp3"]
symphonia-vorbis = ["rodio/symphonia-vorbis"]
symphonia-wav = ["rodio/symphonia-wav"]
# Enable using a shared stdlib for cxx on Android.
android_shared_stdcxx = ["oboe/shared-stdcxx"]
3 changes: 3 additions & 0 deletions crates/bevy_internal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ animation = ["bevy_animation", "bevy_gltf?/bevy_animation"]
# Used to disable code that is unsupported when Bevy is dynamically linked
dynamic_linking = ["bevy_diagnostic/dynamic_linking"]

# Enable using a shared stdlib for cxx on Android.
android_shared_stdcxx = ["bevy_audio/android_shared_stdcxx"]

[dependencies]
# bevy
bevy_app = { path = "../bevy_app", version = "0.9.0" }
Expand Down
20 changes: 10 additions & 10 deletions crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ struct WinitPersistentState {
impl Default for WinitPersistentState {
fn default() -> Self {
Self {
active: true,
active: false,
low_power_event: false,
redraw_request_sent: false,
timeout_reached: false,
Expand Down Expand Up @@ -290,7 +290,7 @@ pub fn winit_runner(mut app: App) {
}
}

{
if winit_state.active {
#[cfg(not(target_arch = "wasm32"))]
let (commands, mut new_windows, created_window_writer, winit_windows) =
create_window_system_state.get_mut(&mut app.world);
Expand Down Expand Up @@ -475,14 +475,7 @@ pub fn winit_runner(mut app: App) {
}
},
WindowEvent::Touch(touch) => {
let mut location =
touch.location.to_logical(window.resolution.scale_factor());

// On a mobile window, the start is from the top while on PC/Linux/OSX from
// bottom
if cfg!(target_os = "android") || cfg!(target_os = "ios") {
location.y = window.height() as f64 - location.y;
}
let location = touch.location.to_logical(window.resolution.scale_factor());

// Event
input_events
Expand Down Expand Up @@ -616,6 +609,13 @@ pub fn winit_runner(mut app: App) {
}
event::Event::Suspended => {
winit_state.active = false;
#[cfg(target_os = "android")]
{
// Bevy doesn't support suspend/resume so we just exit
// and Android will restart the application on resume
// TODO: Save save some state and load on resume
*control_flow = ControlFlow::Exit;
}
}
event::Event::Resumed => {
winit_state.active = true;
Expand Down
10 changes: 5 additions & 5 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ When using `NDK (Side by side)`, the environment variable `ANDROID_NDK_ROOT` mus
To run on a device setup for Android development, run:

```sh
cargo apk run --example android_example
cargo apk run -p bevy_mobile_example
```

When using Bevy as a library, the following fields must be added to `Cargo.toml`:
Expand Down Expand Up @@ -414,7 +414,7 @@ min_sdk_version = >>API or less<<

Example | File | Description
--- | --- | ---
`android` | [`android/android.rs`](./android/android.rs) | The `3d/3d_scene.rs` example for Android
`android` | [`mobile/src/lib.rs`](./mobile/src/lib.rs) | A 3d Scene with a button and playing sound

## iOS

Expand All @@ -435,7 +435,7 @@ rustup target add aarch64-apple-ios x86_64-apple-ios aarch64-apple-ios-sim
Using bash:

```sh
cd examples/ios
cd examples/mobile
make run
```

Expand All @@ -450,15 +450,15 @@ DEVICE_ID=${YOUR_DEVICE_ID} make run
If you'd like to see xcode do stuff, you can run

```sh
open bevy_ios_example.xcodeproj/
open bevy_mobile_example.xcodeproj/
```

which will open xcode. You then must push the zoom zoom play button and wait
for the magic.

Example | File | Description
--- | --- | ---
`ios` | [`ios/src/lib.rs`](./ios/src/lib.rs) | The `3d/3d_scene.rs` example for iOS
`ios` | [`mobile/src/lib.rs`](./mobile/src/lib.rs) | A 3d Scene with a button and playing sound

## WASM

Expand Down
10 changes: 5 additions & 5 deletions examples/README.md.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ When using `NDK (Side by side)`, the environment variable `ANDROID_NDK_ROOT` mus
To run on a device setup for Android development, run:

```sh
cargo apk run --example android_example
cargo apk run -p bevy_mobile_example
```

When using Bevy as a library, the following fields must be added to `Cargo.toml`:
Expand Down Expand Up @@ -145,7 +145,7 @@ min_sdk_version = >>API or less<<

Example | File | Description
--- | --- | ---
`android` | [`android/android.rs`](./android/android.rs) | The `3d/3d_scene.rs` example for Android
`android` | [`mobile/src/lib.rs`](./mobile/src/lib.rs) | A 3d Scene with a button and playing sound

## iOS

Expand All @@ -166,7 +166,7 @@ rustup target add aarch64-apple-ios x86_64-apple-ios aarch64-apple-ios-sim
Using bash:

```sh
cd examples/ios
cd examples/mobile
make run
```

Expand All @@ -181,15 +181,15 @@ DEVICE_ID=${YOUR_DEVICE_ID} make run
If you'd like to see xcode do stuff, you can run

```sh
open bevy_ios_example.xcodeproj/
open bevy_mobile_example.xcodeproj/
```

which will open xcode. You then must push the zoom zoom play button and wait
for the magic.

Example | File | Description
--- | --- | ---
`ios` | [`ios/src/lib.rs`](./ios/src/lib.rs) | The `3d/3d_scene.rs` example for iOS
`ios` | [`mobile/src/lib.rs`](./mobile/src/lib.rs) | A 3d Scene with a button and playing sound

## WASM

Expand Down
54 changes: 0 additions & 54 deletions examples/android/src/lib.rs

This file was deleted.

14 changes: 0 additions & 14 deletions examples/ios/Cargo.toml

This file was deleted.

File renamed without changes.
8 changes: 4 additions & 4 deletions examples/android/Cargo.toml → examples/mobile/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[package]
name = "bevy-android-example"
name = "bevy_mobile_example"
version = "0.1.0"
edition = "2021"
description = "Example for building an Android app with Bevy"
description = "Example for building an iOS or Android app with Bevy"
publish = false
license = "MIT OR Apache-2.0"

[lib]
name = "bevy_android_example"
crate-type = ["cdylib"]
name = "bevy_mobile_example"
crate-type = ["staticlib", "cdylib"]

[dependencies]
bevy = { path = "../../" }
Expand Down
8 changes: 4 additions & 4 deletions examples/ios/Makefile → examples/mobile/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ ifndef DEVICE_ID
endif

run: install
xcrun simctl launch --console $(DEVICE) com.rust.bevy-ios-example
xcrun simctl launch --console $(DEVICE) com.rust.bevy_mobile_example

boot-sim:
xcrun simctl boot $(DEVICE) || true

install: xcodebuild-simulator boot-sim
xcrun simctl install $(DEVICE) build/Build/Products/Debug-iphonesimulator/bevy_ios_example.app
xcrun simctl install $(DEVICE) build/Build/Products/Debug-iphonesimulator/bevy_mobile_example.app

xcodebuild-simulator:
IOS_TARGETS=x86_64-apple-ios xcodebuild -scheme bevy_ios_example -configuration Debug -derivedDataPath build -destination "id=$(DEVICE)"
IOS_TARGETS=x86_64-apple-ios xcodebuild -scheme bevy_mobile_example -configuration Debug -derivedDataPath build -destination "id=$(DEVICE)"

xcodebuild-iphone:
IOS_TARGETS=aarch64-apple-ios xcodebuild -scheme bevy_ios_example -configuration Debug -derivedDataPath build -arch arm64
IOS_TARGETS=aarch64-apple-ios xcodebuild -scheme bevy_mobile_example -configuration Debug -derivedDataPath build -arch arm64

clean:
rm -r build
Expand Down
Loading

0 comments on commit d3188e2

Please sign in to comment.