Skip to content

Commit

Permalink
Merge pull request #60 from NiklasEi/android_support
Browse files Browse the repository at this point in the history
Android support
  • Loading branch information
NiklasEi authored Jul 17, 2023
2 parents 5830454 + 29de801 commit e30732d
Show file tree
Hide file tree
Showing 9 changed files with 400 additions and 265 deletions.
563 changes: 311 additions & 252 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ authors = ["Niklas Eicker <[email protected]>"] # ToDo: you are the author ;)
edition = "2021"
exclude = ["dist", "build", "assets", "credits"]

[workspace]
members = ["mobile"]

[profile.dev.package."*"]
opt-level = 3

Expand Down
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# A Bevy game template

Template for a Game using the awesome [Bevy engine][bevy] featuring out of the box builds for Windows, Linux, macOS, and Web (Wasm).
Template for a Game using the awesome [Bevy engine][bevy] featuring out of the box builds for Windows, Linux, macOS, and Web (Wasm). It also includes the setup for android support.

_Since Bevy is in heavy development, there regularly are unpublished new features or bug fixes. If you like living on the edge, you can use the branch `bevy_main` of this template to be close to the current state of Bevy's main branch_

Expand All @@ -22,6 +22,8 @@ _Since Bevy is in heavy development, there regularly are unpublished new feature
* requires [trunk]: `cargo install --locked trunk`
* requires `wasm32-unknown-unknown` target: `rustup target add wasm32-unknown-unknown`
* this will serve your app on `8080` and automatically rebuild + reload it after code changes
* Start the android app: `cargo apk run -p mobile` (update the library name if you changed it)
* check the [bevy example readme for android setup instructions][android-instructions]

You should keep the `credits` directory up to date. The release workflow automatically includes the directory in every build.

Expand All @@ -33,6 +35,7 @@ You should keep the `credits` directory up to date. The release workflow automat
2. Select the `File > Export As` menu item.
3. Change the file extension to `.ico` (or click `Select File Type (By Extension)` and select `Microsoft Windows Icon`)
4. Save as `build/windows/icon.ico`
3. Replace `build/android/res/mipmap-mdpi/icon.png` with `macos/AppIcon.iconset/icon_256x256.png`, but rename it to `icon.png`

### Deploy web build to GitHub pages
1. Trigger the `deploy-github-page` workflow
Expand All @@ -44,6 +47,11 @@ To deploy newer versions, just run the `deploy-github-page` workflow again.

Note that this does a `cargo build` and thus does not work with local dependencies. Consider pushing your "custom Bevy fork" to GitHub and using it as a git dependency.

# Removing mobile platforms

If you don't want to target Android or iOS, you can just delete the `/mobile`, `/build/android`, and `/build/ios` directories.
Then delete the `[workspace]` section from `Cargo.toml`.

# Getting started with Bevy

You should check out the Bevy website for [links to resources][bevy-learn] and the [Bevy Cheat Book] for a bunch of helpful documentation and examples. I can also recommend the [official Bevy Discord server][bevy-discord] for keeping up to date with the development and getting help from other Bevy users.
Expand All @@ -65,3 +73,4 @@ This project is licensed under [CC0 1.0 Universal](LICENSE) except some content
[Bevy Cheat Book]: https://bevy-cheatbook.github.io/introduction.html
[`wasm-server-runner`]: https://github.com/jakobhellermann/wasm-server-runner
[trunk]: https://trunkrs.dev/
[android-instructions]: https://github.com/bevyengine/bevy/blob/latest/examples/README.md#setup
Binary file added build/android/res/mipmap-mdpi/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
20 changes: 10 additions & 10 deletions build/macos/create_icns.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

rm -rf AppIcon.iconset/*
mkdir -p AppIcon.iconset
sips -z 16 16 icon_1024x1024.png --out AppIcon.iconset/icon_16x16.png
sips -z 32 32 icon_1024x1024.png --out AppIcon.iconset/[email protected]
sips -z 32 32 icon_1024x1024.png --out AppIcon.iconset/icon_32x32.png
sips -z 64 64 icon_1024x1024.png --out AppIcon.iconset/[email protected]
sips -z 128 128 icon_1024x1024.png --out AppIcon.iconset/icon_128x128.png
sips -z 256 256 icon_1024x1024.png --out AppIcon.iconset/[email protected]
sips -z 256 256 icon_1024x1024.png --out AppIcon.iconset/icon_256x256.png
sips -z 512 512 icon_1024x1024.png --out AppIcon.iconset/[email protected]
sips -z 512 512 icon_1024x1024.png --out AppIcon.iconset/icon_512x512.png
cp icon_1024x1024.png AppIcon.iconset/[email protected]
sips -z 16 16 ../icon_1024x1024.png --out AppIcon.iconset/icon_16x16.png
sips -z 32 32 ../icon_1024x1024.png --out AppIcon.iconset/[email protected]
sips -z 32 32 ../icon_1024x1024.png --out AppIcon.iconset/icon_32x32.png
sips -z 64 64 ../icon_1024x1024.png --out AppIcon.iconset/[email protected]
sips -z 128 128 ../icon_1024x1024.png --out AppIcon.iconset/icon_128x128.png
sips -z 256 256 ../icon_1024x1024.png --out AppIcon.iconset/[email protected]
sips -z 256 256 ../icon_1024x1024.png --out AppIcon.iconset/icon_256x256.png
sips -z 512 512 ../icon_1024x1024.png --out AppIcon.iconset/[email protected]
sips -z 512 512 ../icon_1024x1024.png --out AppIcon.iconset/icon_512x512.png
cp ../icon_1024x1024.png AppIcon.iconset/[email protected]
iconutil -c icns AppIcon.iconset
mkdir -p src/Game.app/Contents/Resources
mv AppIcon.icns src/Game.app/Contents/Resources/
34 changes: 34 additions & 0 deletions mobile/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[package]
name = "mobile"
version = "0.1.0"
edition = "2021"
publish = false

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

[dependencies]
bevy_game = { path = ".." } # ToDo
bevy = { version = "0.11", default-features = false }

# As long as Kira doesn't expose a feature for this, we need to enable it our self
# See https://github.com/tesselode/kira/pull/51
[target."cfg(target_os = \"android\")".dependencies.cpal]
version = "0.15"
features = ["oboe-shared-stdcxx"]

[package.metadata.android]
package = "me.nikl.bevygame" # ToDo
apk_name = "bevygame" # ToDo
assets = "../assets"
strip = "strip"
resources = "../assets/res"
build_targets = ["aarch64-linux-android"]

[package.metadata.android.sdk]
target_sdk_version = 31

[package.metadata.android.application]
icon = "@mipmap/icon"
label = "Bevy Game" # ToDo
7 changes: 7 additions & 0 deletions mobile/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use bevy::prelude::*;
use bevy_game::GamePlugin;

#[bevy_main]
fn main() {
App::new().add_plugins((DefaultPlugins, GamePlugin)).run()
}
27 changes: 25 additions & 2 deletions src/actions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use bevy::math::Vec3Swizzles;
use bevy::prelude::*;
use bevy::window::PrimaryWindow;

use crate::actions::game_control::{get_movement, GameControl};
use crate::player::Player;
use crate::GameState;

mod game_control;
Expand All @@ -23,14 +26,34 @@ pub struct Actions {
pub player_movement: Option<Vec2>,
}

pub fn set_movement_actions(mut actions: ResMut<Actions>, keyboard_input: Res<Input<KeyCode>>) {
let player_movement = Vec2::new(
pub fn set_movement_actions(
mut actions: ResMut<Actions>,
keyboard_input: Res<Input<KeyCode>>,
touch_input: Res<Touches>,
player: Query<&Transform, With<Player>>,
window: Query<&Window, With<PrimaryWindow>>,
) {
let mut player_movement = Vec2::new(
get_movement(GameControl::Right, &keyboard_input)
- get_movement(GameControl::Left, &keyboard_input),
get_movement(GameControl::Up, &keyboard_input)
- get_movement(GameControl::Down, &keyboard_input),
);

// Todo: simplify! This also produces wrong touch positions (may be related to https://github.com/bevyengine/bevy/issues/7528)
if let Some(touch_position) = touch_input.first_pressed_position() {
let resolution = &window.single().resolution;
let dimensions = Vec2::new(-resolution.width(), resolution.height());
let diff = Vec2::new(touch_position.x, -touch_position.y)
- player.single().translation.xy()
+ dimensions
/ (2.
* resolution
.scale_factor_override()
.unwrap_or(resolution.scale_factor()) as f32);
player_movement = diff.normalize();
}

if player_movement != Vec2::ZERO {
actions.player_movement = Some(player_movement.normalize());
} else {
Expand Down

0 comments on commit e30732d

Please sign in to comment.