Skip to content

Commit

Permalink
remove ndk-glue, get the new android app and native activity
Browse files Browse the repository at this point in the history
  • Loading branch information
mockersf committed Feb 3, 2023
1 parent 7775a6d commit f5bfd0f
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 18 deletions.
6 changes: 3 additions & 3 deletions crates/bevy_asset/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ bevy_reflect = { path = "../bevy_reflect", version = "0.9.0", features = ["bevy"
bevy_tasks = { path = "../bevy_tasks", version = "0.9.0" }
bevy_utils = { path = "../bevy_utils", version = "0.9.0" }

[target.'cfg(target_os = "android")'.dependencies]
bevy_winit = { path = "../bevy_winit", version = "0.9.0" }

# other
serde = { version = "1", features = ["derive"] }
crossbeam-channel = "0.5.0"
Expand All @@ -39,9 +42,6 @@ web-sys = { version = "0.3", features = ["Request", "Window", "Response"] }
wasm-bindgen-futures = "0.4"
js-sys = "0.3"

[target.'cfg(target_os = "android")'.dependencies]
ndk-glue = { version = "0.7" }

[dev-dependencies]
futures-lite = "1.4.0"
tempfile = "3.2.0"
Expand Down
5 changes: 4 additions & 1 deletion crates/bevy_asset/src/io/android_asset_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ impl AndroidAssetIo {
impl AssetIo for AndroidAssetIo {
fn load_path<'a>(&'a self, path: &'a Path) -> BoxedFuture<'a, Result<Vec<u8>, AssetIoError>> {
Box::pin(async move {
let asset_manager = ndk_glue::native_activity().asset_manager();
let asset_manager = bevy_winit::ANDROID_APP
.get()
.expect("Bevy must be setup with the #[bevy_main] macro on Android")
.asset_manager();
let mut opened_asset = asset_manager
.open(&CString::new(path.to_str().unwrap()).unwrap())
.ok_or(AssetIoError::NotFound(path.to_path_buf()))?;
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_derive/src/bevy_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ pub fn bevy_main(_attr: TokenStream, item: TokenStream) -> TokenStream {
);

TokenStream::from(quote! {
// use ndk-glue macro to create an activity: https://github.com/rust-mobile/ndk-glue/tree/main/ndk-macro
#[no_mangle]
#[cfg(target_os = "android")]
#[cfg_attr(target_os = "android", bevy::ndk_glue::main(backtrace = "on", ndk_glue = "bevy::ndk_glue"))]
fn android_main() {
main()
fn android_main(android_app: bevy::winit::AndroidApp) {
let _ = bevy::winit::ANDROID_APP.set(android_app);
main();
}

#[no_mangle]
Expand Down
5 changes: 0 additions & 5 deletions crates/bevy_internal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,3 @@ bevy_text = { path = "../bevy_text", optional = true, version = "0.9.0" }
bevy_ui = { path = "../bevy_ui", optional = true, version = "0.9.0" }
bevy_winit = { path = "../bevy_winit", optional = true, version = "0.9.0" }
bevy_gilrs = { path = "../bevy_gilrs", optional = true, version = "0.9.0" }

[target.'cfg(target_os = "android")'.dependencies]
# This version *must* be the same as the version used by winit,
# or Android will break: https://github.com/rust-windowing/winit#android
ndk-glue = {version = "0.7", features = ["logger"]}
3 changes: 0 additions & 3 deletions crates/bevy_internal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,3 @@ pub mod dynamic_plugin {
//! Dynamic linking of plugins
pub use bevy_dynamic_plugin::*;
}

#[cfg(target_os = "android")]
pub use ndk_glue;
4 changes: 4 additions & 0 deletions crates/bevy_winit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ winit = { version = "0.28", default-features = false }
approx = { version = "0.5", default-features = false }
raw-window-handle = "0.5"

[target.'cfg(target_os = "android")'.dependencies]
winit = { version = "0.28", default-features = false, features = ["android-native-activity"] }
once_cell = "1.11"

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen = { version = "0.2" }
web-sys = "0.3"
Expand Down
23 changes: 21 additions & 2 deletions crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,40 @@ use bevy_window::{
WindowScaleFactorChanged,
};

#[cfg(target_os = "android")]
pub use winit::platform::android::activity::AndroidApp;

use winit::{
event::{self, DeviceEvent, Event, StartCause, WindowEvent},
event_loop::{ControlFlow, EventLoop, EventLoopWindowTarget},
event_loop::{ControlFlow, EventLoop, EventLoopBuilder, EventLoopWindowTarget},
};

use crate::system::WinitWindowInfo;
#[cfg(target_arch = "wasm32")]
use crate::web_resize::{CanvasParentResizeEventChannel, CanvasParentResizePlugin};

#[cfg(target_os = "android")]
pub static ANDROID_APP: once_cell::sync::OnceCell<AndroidApp> = once_cell::sync::OnceCell::new();

#[derive(Default)]
pub struct WinitPlugin;

impl Plugin for WinitPlugin {
fn build(&self, app: &mut App) {
let event_loop = EventLoop::new();
let mut event_loop_builder = EventLoopBuilder::<()>::with_user_event();

#[cfg(target_os = "android")]
{
use winit::platform::android::EventLoopBuilderExtAndroid;
event_loop_builder.with_android_app(
ANDROID_APP
.get()
.expect("Bevy must be setup with the #[bevy_main] macro on Android")
.clone(),
);
}

let event_loop = event_loop_builder.build();
app.insert_non_send_resource(event_loop);

app.init_non_send_resource::<WinitWindows>()
Expand Down

0 comments on commit f5bfd0f

Please sign in to comment.