Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cross-platform main function #847

Merged
merged 1 commit into from
Nov 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,6 @@ tracing = "0.1.21"
tracing-chrome = "0.2.0"
tracing-subscriber = "0.2.15"

# bevy (Android)
[target.'cfg(target_os = "android")'.dependencies]
android_logger = "0.9"
ndk-glue = {version = "0.2", features = ["logger"]}

[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
console_error_panic_hook = "0.1.6"
console_log = {version = "0.2", features = ["color"]}
Expand Down
36 changes: 36 additions & 0 deletions crates/bevy_derive/src/bevy_main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, ItemFn};

pub fn bevy_main(_attr: TokenStream, item: TokenStream) -> TokenStream {
let input = parse_macro_input!(item as ItemFn);
if input.sig.ident != "main" {
panic!("bevy_main can only be used on a function called 'main'")
}

TokenStream::from(quote! {
#[no_mangle]
#[cfg(target_os = "android")]
unsafe extern "C" fn ANativeActivity_onCreate(
activity: *mut std::os::raw::c_void,
saved_state: *mut std::os::raw::c_void,
saved_state_size: usize,
) {
bevy::ndk_glue::init(
activity as _,
saved_state as _,
saved_state_size as _,
main,
);
}

#[no_mangle]
#[cfg(target_os = "ios")]
extern "C" fn main_rs() {
main();
}

#[allow(unused)]
#input
})
}
6 changes: 6 additions & 0 deletions crates/bevy_derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
extern crate proc_macro;

mod app_plugin;
mod bevy_main;
mod bytes;
mod modules;
mod render_resource;
Expand Down Expand Up @@ -60,3 +61,8 @@ pub fn type_uuid_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStre
pub fn external_type_uuid(tokens: proc_macro::TokenStream) -> proc_macro::TokenStream {
type_uuid::external_type_uuid(tokens)
}

#[proc_macro_attribute]
pub fn bevy_main(attr: TokenStream, item: TokenStream) -> TokenStream {
bevy_main::bevy_main(attr, item)
}
3 changes: 3 additions & 0 deletions crates/bevy_internal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ bevy_app = { path = "../bevy_app", version = "0.3.0" }
bevy_asset = { path = "../bevy_asset", version = "0.3.0" }
bevy_type_registry = { path = "../bevy_type_registry", version = "0.3.0" }
bevy_core = { path = "../bevy_core", version = "0.3.0" }
bevy_derive = { path = "../bevy_derive", version = "0.3.0" }
bevy_diagnostic = { path = "../bevy_diagnostic", version = "0.3.0" }
bevy_ecs = { path = "../bevy_ecs", version = "0.3.0" }
bevy_input = { path = "../bevy_input", version = "0.3.0" }
Expand All @@ -63,3 +64,5 @@ bevy_wgpu = { path = "../bevy_wgpu", optional = true, version = "0.3.0" }
bevy_winit = { path = "../bevy_winit", optional = true, version = "0.3.0" }
bevy_gilrs = { path = "../bevy_gilrs", optional = true, version = "0.3.0" }

[target.'cfg(target_os = "android")'.dependencies]
ndk-glue = {version = "0.2", features = ["logger"]}
3 changes: 3 additions & 0 deletions crates/bevy_internal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,6 @@ pub mod wgpu {
pub mod dynamic_plugin {
pub use bevy_dynamic_plugin::*;
}

#[cfg(target_os = "android")]
pub use ndk_glue;
2 changes: 2 additions & 0 deletions crates/bevy_internal/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ pub use crate::{
type_registry::RegisterType, window::prelude::*, DefaultPlugins, MinimalPlugins,
};

pub use bevy_derive::bevy_main;

#[cfg(feature = "bevy_audio")]
pub use crate::audio::prelude::*;

Expand Down
51 changes: 43 additions & 8 deletions examples/android/android.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,45 @@
// Edit to run any single file example from Bevy.
include!("../3d/3d_scene.rs");
use bevy::prelude::*;

#[cfg_attr(
target_os = "android",
ndk_glue::main(logger(level = "debug", tag = "android"), backtrace = "full")
)]
pub fn android_main() {
main();
// the `bevy_main` proc_macro generates the required android boilerplate
#[bevy_main]
fn main() {
App::build()
.add_resource(Msaa { samples: 2 })
.add_plugins(DefaultPlugins)
.add_startup_system(setup.system())
.run();
}

/// set up a simple 3D scene
fn setup(
commands: &mut Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// add entities to the world
commands
// plane
.spawn(PbrComponents {
mesh: meshes.add(Mesh::from(shape::Plane { size: 10.0 })),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
..Default::default()
})
// cube
.spawn(PbrComponents {
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
transform: Transform::from_translation(Vec3::new(0.0, 1.0, 0.0)),
..Default::default()
})
// light
.spawn(LightComponents {
transform: Transform::from_translation(Vec3::new(4.0, 8.0, 4.0)),
..Default::default()
})
// camera
.spawn(Camera3dComponents {
transform: Transform::from_translation(Vec3::new(-3.0, 5.0, 8.0))
.looking_at(Vec3::default(), Vec3::unit_y()),
..Default::default()
});
}
14 changes: 4 additions & 10 deletions examples/ios/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
use bevy::{
prelude::{
shape, App, Assets, Camera3dComponents, Color, Commands, DefaultPlugins, IntoSystem,
LightComponents, Mesh, Msaa, PbrComponents, ResMut, StandardMaterial, Transform, Vec3,
WindowDescriptor,
},
window::WindowMode,
};
use bevy::{prelude::*, window::WindowMode};

#[no_mangle]
extern "C" fn main_rs() {
// the `bevy_main` proc_macro generates the required ios boilerplate
#[bevy_main]
fn main() {
App::build()
.add_resource(WindowDescriptor {
vsync: true,
Expand Down