From 00cded225271e3354902d76e5b663f11e458f428 Mon Sep 17 00:00:00 2001 From: thatguydoru Date: Tue, 2 May 2023 03:03:45 +0800 Subject: [PATCH 1/4] chore: clean up stuff i messed up --- src/color.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/color.rs b/src/color.rs index 86ba52c4..a0131878 100644 --- a/src/color.rs +++ b/src/color.rs @@ -100,7 +100,7 @@ impl Color { Color { r, g, b, a } } - /// Build a color from 4 components between 0 and 255. + /// Build a color from 4 0..255 components. /// Unfortunately it can't be const fn due to [this issue](https://github.com/rust-lang/rust/issues/57241). /// When const version is needed "color_u8" macro may be a workaround. pub fn from_rgba(r: u8, g: u8, b: u8, a: u8) -> Color { From 903333bea9747d490c360d9a1a91aa21f37ba379 Mon Sep 17 00:00:00 2001 From: thatguydoru Date: Wed, 3 May 2023 08:34:48 +0800 Subject: [PATCH 2/4] feat (examples): Added isometric 2D example --- examples/grass_v1.png | Bin 0 -> 625 bytes examples/isometric2d.rs | 89 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 examples/grass_v1.png create mode 100644 examples/isometric2d.rs diff --git a/examples/grass_v1.png b/examples/grass_v1.png new file mode 100644 index 0000000000000000000000000000000000000000..5e059744683d7749f2fd20e5d19eac00043d8fb4 GIT binary patch literal 625 zcmeAS@N?(olHy`uVBq!ia0vp^4j|0I1|(Ny7TyC=jKx9jP7LeL$-HD>VAA$|qgVRI=c3e3;#xVYlf{no<4t z^xbC)Z_mBE_xXvmWyJ54}lNEbJ@g z_}agut9XsmioJV6<}8+V`m;3Xk<40wB>l@m&pp~#)OAj^aMyDAcsTCSoLd4n`FmaG z1WPGZevCM3xl?gbx1Z~r$x=!cpF@sXZWM602XckEK2{rs7V`05{3~9_=T!3gYSI1~ z->>AK@67n6sPM1mw_|suzwwFJx3#kda~q1t8$IS;E~ccy*T>Wl%r7X+evsk8 z6{jU#%^3^^-vo9(Qm|x@i0%j~oFL0k(Y`{I{U~EYDTiiZfGk5s`vfueB*umk4&6cp zIfg537sS}J7#;FBvS`OD}WZJOBBEZ^gx|EWbgSX5I`8hqU z9IQZJrMag&D^@UQ7betI%xZUF$S!rnK^E*PpKE|=h{4m<&t;uc GLK6Ur^Zkeb literal 0 HcmV?d00001 diff --git a/examples/isometric2d.rs b/examples/isometric2d.rs new file mode 100644 index 00000000..3d07b781 --- /dev/null +++ b/examples/isometric2d.rs @@ -0,0 +1,89 @@ +// Example: Isometric 2D +// Shows basic specialized usage of the Camera2D API. + +use macroquad::prelude::*; + +const TILE_SIZE: IVec2 = ivec2(64, 64); +const MAP_SIZE: IVec2 = ivec2(10, 10); + +#[macroquad::main("Isometric 2D")] +async fn main() { + set_pc_assets_folder("examples"); + + let texture = load_texture("grass_v1.png").await.unwrap(); + // Set camera area to some multiple of tile size (in this case 64x64). + let cam_area = vec2(768., 576.); + // Assumption here is the world origin is 0, 0. + let cam_pos = vec2(-cam_area.x / 2., -cam_area.y / 4.); + let mut camera = + Camera2D::from_display_rect(Rect::new(cam_pos.x, cam_pos.y, cam_area.x, cam_area.y)); + + loop { + clear_background(BLUE); + + // Exit when `ESC` is pressed. + if is_key_pressed(KeyCode::Escape) { + break; + } + + // Control the camera. + // W, A, S, D for moving the camera and scroll to zoom. + let cam_speed = 10.; + if is_key_down(KeyCode::W) || is_key_down(KeyCode::Up) { + camera.target.y -= cam_speed + } + if is_key_down(KeyCode::A) || is_key_down(KeyCode::Left) { + camera.target.x -= cam_speed; + } + if is_key_down(KeyCode::S) || is_key_down(KeyCode::Down) { + camera.target.y += cam_speed; + } + if is_key_down(KeyCode::D) || is_key_down(KeyCode::Right) { + camera.target.x += cam_speed; + } + let (_, scroll_y) = mouse_wheel(); + camera.zoom *= 1.1_f32.powf(scroll_y); + + // Draw tiles in camera perspective. + set_camera(&camera); + for y in 0..MAP_SIZE.y { + for x in 0..MAP_SIZE.x { + let world_pos = map_to_world(ivec2(x, y)); + draw_texture(texture, world_pos.x, world_pos.y, WHITE); + } + } + set_default_camera(); + + let mouse_in_world = camera.screen_to_world(mouse_position().into()); + draw_text( + &format!("Pointing at: {}", world_to_map(mouse_in_world)), + 0., + 20., + 20., + BLACK, + ); + + next_frame().await; + } +} + +// Transform world position to map position. +// Reference: https://youtu.be/04oQ2jOUjkU +fn world_to_map(world_pos: Vec2) -> IVec2 { + let ihat = vec2(0.5, 0.25) * TILE_SIZE.as_vec2(); + let jhat = vec2(-0.5, 0.25) * TILE_SIZE.as_vec2(); + let inverse = mat2(ihat, jhat).inverse(); + + inverse.mul_vec2(world_pos).as_ivec2() +} + +// Transform map position to world position. +// Reference: https://youtu.be/04oQ2jOUjkU +fn map_to_world(map_pos: IVec2) -> Vec2 { + let ihat = vec2(0.5, 0.25) * TILE_SIZE.as_vec2(); + let jhat = vec2(-0.5, 0.25) * TILE_SIZE.as_vec2(); + let transform = mat2(ihat, jhat); + let offset = ivec2(-TILE_SIZE.x / 2, 0); + + transform.mul_vec2(map_pos.as_vec2()) + offset.as_vec2() +} From 19db3936f8746100168ab82c5cd863eb3b3eb0e4 Mon Sep 17 00:00:00 2001 From: thatguydoru Date: Sun, 3 Mar 2024 02:34:42 +0800 Subject: [PATCH 3/4] chore: revert doc comment for from_rgba --- src/color.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/color.rs b/src/color.rs index a0131878..86ba52c4 100644 --- a/src/color.rs +++ b/src/color.rs @@ -100,7 +100,7 @@ impl Color { Color { r, g, b, a } } - /// Build a color from 4 0..255 components. + /// Build a color from 4 components between 0 and 255. /// Unfortunately it can't be const fn due to [this issue](https://github.com/rust-lang/rust/issues/57241). /// When const version is needed "color_u8" macro may be a workaround. pub fn from_rgba(r: u8, g: u8, b: u8, a: u8) -> Color { From 0a91c3849a828e5fbd6d2f9832d58a26af9d6753 Mon Sep 17 00:00:00 2001 From: thatguydoru Date: Sun, 3 Mar 2024 02:36:31 +0800 Subject: [PATCH 4/4] fix: turn texture2d into a ref for draw_texture --- examples/isometric2d.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/isometric2d.rs b/examples/isometric2d.rs index 3d07b781..64af2827 100644 --- a/examples/isometric2d.rs +++ b/examples/isometric2d.rs @@ -49,7 +49,7 @@ async fn main() { for y in 0..MAP_SIZE.y { for x in 0..MAP_SIZE.x { let world_pos = map_to_world(ivec2(x, y)); - draw_texture(texture, world_pos.x, world_pos.y, WHITE); + draw_texture(&texture, world_pos.x, world_pos.y, WHITE); } } set_default_camera();