Skip to content

Commit

Permalink
Remove compilation warnings caused by examples (#253)
Browse files Browse the repository at this point in the history
Nicolas-Ferre authored Nov 4, 2023
1 parent e7197f2 commit d65bd24
Showing 45 changed files with 1,194 additions and 1,127 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@ doc-comment = "0.3"
fs_extra = "1.2"
futures = "0.3"
fxhash = "0.2"
getrandom = { features = ["js"] } # To fix this issue: https://github.com/rust-random/getrandom/issues/208
getrandom = { version = "*", features = ["js"] } # To fix this issue: https://github.com/rust-random/getrandom/issues/208
gilrs = "0.10"
image = "0.24"
instant = "0.1"
92 changes: 21 additions & 71 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
@@ -34,110 +34,60 @@ target_sdk_version = 31

[[example]]
name = "animation_2d_android"
path = "examples/animation_2d.rs"
path = "examples/android/animation_2d.rs"
crate-type = ["cdylib"]

[[example]]
name = "animation_2d"
path = "examples/animation_2d.rs"
crate-type = ["bin"]

[[example]]
name = "anti_aliasing_android"
path = "examples/anti_aliasing.rs"
path = "examples/android/anti_aliasing.rs"
crate-type = ["cdylib"]

[[example]]
name = "anti_aliasing"
path = "examples/anti_aliasing.rs"
crate-type = ["bin"]

[[example]]
name = "rendering_2d_android"
path = "examples/rendering_2d.rs"
name = "collisions_2d_android"
path = "examples/android/collisions_2d.rs"
crate-type = ["cdylib"]

[[example]]
name = "rendering_2d"
path = "examples/rendering_2d.rs"
crate-type = ["bin"]

[[example]]
name = "text_2d_android"
path = "examples/text_2d.rs"
name = "game_of_life_android"
path = "examples/android/game_of_life.rs"
crate-type = ["cdylib"]

[[example]]
name = "text_2d"
path = "examples/text_2d.rs"
crate-type = ["bin"]

[[example]]
name = "texture_2d_android"
path = "examples/texture_2d.rs"
name = "gamepad_android"
path = "examples/android/gamepad.rs"
crate-type = ["cdylib"]

[[example]]
name = "texture_2d"
path = "examples/texture_2d.rs"
crate-type = ["bin"]

[[example]]
name = "keyboard_android"
path = "examples/keyboard.rs"
path = "examples/android/keyboard.rs"
crate-type = ["cdylib"]

[[example]]
name = "keyboard"
path = "examples/keyboard.rs"
crate-type = ["bin"]

[[example]]
name = "mouse_android"
path = "examples/mouse.rs"
path = "examples/android/mouse.rs"
crate-type = ["cdylib"]

[[example]]
name = "mouse"
path = "examples/mouse.rs"
crate-type = ["bin"]

[[example]]
name = "touch_android"
path = "examples/touch.rs"
name = "pong_android"
path = "examples/android/pong.rs"
crate-type = ["cdylib"]

[[example]]
name = "touch"
path = "examples/touch.rs"
crate-type = ["bin"]

[[example]]
name = "gamepad_android"
path = "examples/gamepad.rs"
name = "rendering_2d_android"
path = "examples/android/rendering_2d.rs"
crate-type = ["cdylib"]

[[example]]
name = "gamepad"
path = "examples/gamepad.rs"
crate-type = ["bin"]

[[example]]
name = "game_of_life_android"
path = "examples/game_of_life.rs"
name = "text_2d_android"
path = "examples/android/text_2d.rs"
crate-type = ["cdylib"]

[[example]]
name = "game_of_life"
path = "examples/game_of_life.rs"
crate-type = ["bin"]

[[example]]
name = "pong_android"
path = "examples/pong/main.rs"
name = "texture_2d_android"
path = "examples/android/texture_2d.rs"
crate-type = ["cdylib"]

[[example]]
name = "pong"
path = "examples/pong/main.rs"
crate-type = ["bin"]
name = "touch_android"
path = "examples/android/touch.rs"
crate-type = ["cdylib"]
6 changes: 6 additions & 0 deletions examples/examples/android/animation_2d.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![allow(missing_docs)]

#[cfg_attr(target_os = "android", ndk_glue::main(backtrace = "on"))]
pub fn main() {
modor_examples::animation_2d::main();
}
6 changes: 6 additions & 0 deletions examples/examples/android/anti_aliasing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![allow(missing_docs)]

#[cfg_attr(target_os = "android", ndk_glue::main(backtrace = "on"))]
pub fn main() {
modor_examples::anti_aliasing::main();
}
6 changes: 6 additions & 0 deletions examples/examples/android/collisions_2d.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![allow(missing_docs)]

#[cfg_attr(target_os = "android", ndk_glue::main(backtrace = "on"))]
pub fn main() {
modor_examples::collisions_2d::main();
}
6 changes: 6 additions & 0 deletions examples/examples/android/game_of_life.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![allow(missing_docs)]

#[cfg_attr(target_os = "android", ndk_glue::main(backtrace = "on"))]
pub fn main() {
modor_examples::game_of_life::main();
}
6 changes: 6 additions & 0 deletions examples/examples/android/gamepad.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![allow(missing_docs)]

#[cfg_attr(target_os = "android", ndk_glue::main(backtrace = "on"))]
pub fn main() {
modor_examples::gamepad::main();
}
6 changes: 6 additions & 0 deletions examples/examples/android/keyboard.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![allow(missing_docs)]

#[cfg_attr(target_os = "android", ndk_glue::main(backtrace = "on"))]
pub fn main() {
modor_examples::keyboard::main();
}
6 changes: 6 additions & 0 deletions examples/examples/android/mouse.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![allow(missing_docs)]

#[cfg_attr(target_os = "android", ndk_glue::main(backtrace = "on"))]
pub fn main() {
modor_examples::mouse::main();
}
6 changes: 6 additions & 0 deletions examples/examples/android/pong.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![allow(missing_docs)]

#[cfg_attr(target_os = "android", ndk_glue::main(backtrace = "on"))]
pub fn main() {
modor_examples::pong::main();
}
6 changes: 6 additions & 0 deletions examples/examples/android/rendering_2d.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![allow(missing_docs)]

#[cfg_attr(target_os = "android", ndk_glue::main(backtrace = "on"))]
pub fn main() {
modor_examples::rendering_2d::main();
}
6 changes: 6 additions & 0 deletions examples/examples/android/text_2d.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![allow(missing_docs)]

#[cfg_attr(target_os = "android", ndk_glue::main(backtrace = "on"))]
pub fn main() {
modor_examples::text_2d::main();
}
6 changes: 6 additions & 0 deletions examples/examples/android/texture_2d.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![allow(missing_docs)]

#[cfg_attr(target_os = "android", ndk_glue::main(backtrace = "on"))]
pub fn main() {
modor_examples::texture_2d::main();
}
6 changes: 6 additions & 0 deletions examples/examples/android/touch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![allow(missing_docs)]

#[cfg_attr(target_os = "android", ndk_glue::main(backtrace = "on"))]
pub fn main() {
modor_examples::touch::main();
}
125 changes: 1 addition & 124 deletions examples/examples/animation_2d.rs
Original file line number Diff line number Diff line change
@@ -1,128 +1,5 @@
#![allow(missing_docs)]

use modor::{systems, App, BuiltEntity, Component, SingleRef};
use modor_graphics::{
model_2d, window_target, Color, Material, Model2DMaterial, RenderTarget, Sprite, Texture,
TextureAnimation, WINDOW_CAMERA_2D,
};
use modor_input::{Key, Keyboard};
use modor_math::Vec2;
use modor_physics::{Dynamics2D, Transform2D};
use modor_resources::ResKey;

#[cfg_attr(target_os = "android", ndk_glue::main(backtrace = "on"))]
pub fn main() {
App::new()
.with_entity(modor_text::module())
.with_entity(window())
.with_entity(character())
.run(modor_graphics::runner);
}

fn window() -> impl BuiltEntity + Sized {
window_target().updated(|r: &mut RenderTarget| r.background_color = Color::DARK_GRAY)
}

fn character() -> impl BuiltEntity {
let texture_key = ResKey::unique("character");
let sprites = Direction::Down.stopped_sprites();
model_2d(WINDOW_CAMERA_2D, Model2DMaterial::Rectangle)
.updated(|t: &mut Transform2D| t.size = Vec2::ONE * 0.15)
.updated(|m: &mut Material| m.texture_key = Some(texture_key))
.component(Dynamics2D::new())
.component(Texture::from_path(texture_key, "slime.png"))
.with(|t| t.is_smooth = false)
.component(TextureAnimation::new(5, 9, sprites))
.component(Character::default())
}

#[derive(Component, Default)]
struct Character {
direction: Direction,
}

#[systems]
impl Character {
#[run]
fn update(
&mut self,
transform: &mut Transform2D,
dynamics: &mut Dynamics2D,
animation: &mut TextureAnimation,
keyboard: SingleRef<'_, '_, Keyboard>,
) {
dynamics.velocity = 0.2
* keyboard
.get()
.direction(Key::Left, Key::Right, Key::Up, Key::Down);
self.direction = self.direction(dynamics.velocity);
animation.sprites = self.direction.sprites(dynamics.velocity == Vec2::ZERO);
transform.size.x = self.direction.size_x_sign() * transform.size.x.abs();
}

fn direction(&self, direction: Vec2) -> Direction {
if direction.x < 0. {
Direction::Left
} else if direction.x > 0. {
Direction::Right
} else if direction.y > 0. {
Direction::Up
} else if direction.y < 0. {
Direction::Down
} else {
self.direction
}
}
}

#[derive(Clone, Copy, PartialEq, Eq, Default)]
enum Direction {
Left,
Right,
Up,
#[default]
Down,
}

impl Direction {
fn sprites(self, is_stopped: bool) -> Vec<Sprite> {
if is_stopped {
self.stopped_sprites()
} else {
self.moving_sprites()
}
}

fn moving_sprites(self) -> Vec<Sprite> {
let texture_y = self.texture_y();
vec![
Sprite::new(0, texture_y + 1),
Sprite::new(1, texture_y + 1),
Sprite::new(2, texture_y + 1),
Sprite::new(3, texture_y + 1),
Sprite::new(4, texture_y + 1),
Sprite::new(0, texture_y + 2),
Sprite::new(1, texture_y + 2),
]
}

fn stopped_sprites(self) -> Vec<Sprite> {
let texture_y = self.texture_y();
vec![Sprite::new(0, texture_y), Sprite::new(1, texture_y)]
}

fn texture_y(self) -> u16 {
match self {
Self::Left | Self::Right => 0,
Self::Up => 6,
Self::Down => 3,
}
}

fn size_x_sign(self) -> f32 {
match self {
Self::Left | Self::Up | Self::Down => 1.,
Self::Right => -1.,
}
}
modor_examples::animation_2d::main();
}
Loading

0 comments on commit d65bd24

Please sign in to comment.