Skip to content

Commit 4385146

Browse files
Adjective-Objectnot-fl3
authored andcommitted
Support rotation/ in draw_tiles
This change adds support for `tiled`'s flipping flags when rendering a tilemap - Adds new api in Map `.spr_flip`, which renders a tile including the tile's [flipping parameters](https://doc.mapeditor.org/en/stable/reference/global-tile-ids/#mapping-a-gid-to-a-local-tile-id) - Uses that api in .draw_tiles
1 parent 44cad5d commit 4385146

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

tiled/src/lib.rs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::collections::HashMap;
77
mod error;
88
mod tiled;
99

10+
use core::f32::consts::PI;
1011
pub use error::Error;
1112
pub use tiled::{Property, PropertyVal};
1213

@@ -47,6 +48,7 @@ pub struct Tile {
4748
/// Whether the tile is vertically flipped
4849
pub flip_y: bool,
4950
/// Whether the tile is anti-diagonally flipped
51+
/// (equivalent to a 90 degree clockwise rotation followed by a horizontal flip)
5052
pub flip_d: bool,
5153
}
5254

@@ -95,8 +97,28 @@ pub struct Map {
9597
pub raw_tiled_map: tiled::Map,
9698
}
9799

100+
pub struct TileFlippedParams {
101+
flip_x: bool,
102+
flip_y: bool,
103+
flip_d: bool,
104+
}
105+
106+
impl Default for TileFlippedParams {
107+
fn default() -> Self {
108+
TileFlippedParams {
109+
flip_x: false,
110+
flip_y: false,
111+
flip_d: false,
112+
}
113+
}
114+
}
115+
98116
impl Map {
99117
pub fn spr(&self, tileset: &str, sprite: u32, dest: Rect) {
118+
self.spr_flip(tileset, sprite, dest, TileFlippedParams::default())
119+
}
120+
121+
pub fn spr_flip(&self, tileset: &str, sprite: u32, dest: Rect, flip: TileFlippedParams) {
100122
if self.tilesets.contains_key(tileset) == false {
101123
panic!(
102124
"No such tileset: {}, tilesets available: {:?}",
@@ -107,6 +129,13 @@ impl Map {
107129
let tileset = &self.tilesets[tileset];
108130
let spr_rect = tileset.sprite_rect(sprite);
109131

132+
let rotation = if flip.flip_d {
133+
// diagonal flip
134+
-PI / 2.0
135+
} else {
136+
0.0
137+
};
138+
110139
draw_texture_ex(
111140
&tileset.texture,
112141
dest.x,
@@ -120,6 +149,9 @@ impl Map {
120149
spr_rect.w + 2.0,
121150
spr_rect.h + 2.0,
122151
)),
152+
flip_x: flip.flip_x,
153+
flip_y: flip.flip_y ^ flip.flip_d,
154+
rotation: rotation,
123155
..Default::default()
124156
},
125157
);
@@ -186,7 +218,16 @@ impl Map {
186218

187219
for (tileset, tileset_layer) in &separated_by_ts {
188220
for (tile, rect) in tileset_layer {
189-
self.spr(tileset, tile.id, *rect);
221+
self.spr_flip(
222+
tileset,
223+
tile.id,
224+
*rect,
225+
TileFlippedParams {
226+
flip_x: tile.flip_x,
227+
flip_y: tile.flip_y,
228+
flip_d: tile.flip_d,
229+
},
230+
);
190231
}
191232
}
192233
}

0 commit comments

Comments
 (0)