diff options
author | BigBrotherNii <nicochr1004@gmail.com> | 2024-07-16 18:48:37 +0200 |
---|---|---|
committer | BigBrotherNii <nicochr1004@gmail.com> | 2024-07-16 18:48:37 +0200 |
commit | 4be5accf7d1d664c19eb3946a85741b630979266 (patch) | |
tree | aab5a4fede64ab410c22cba212afcb2a394c5bf0 /light-client/src/render/sprite.rs | |
parent | f4de3a810769d902e8fd880f727b17b0bff60caa (diff) | |
parent | 4064c56783bd78b96b0e79d5a7b15b1fb0d8edad (diff) | |
download | hurrycurry-4be5accf7d1d664c19eb3946a85741b630979266.tar hurrycurry-4be5accf7d1d664c19eb3946a85741b630979266.tar.bz2 hurrycurry-4be5accf7d1d664c19eb3946a85741b630979266.tar.zst |
updated textures for light-client
Diffstat (limited to 'light-client/src/render/sprite.rs')
-rw-r--r-- | light-client/src/render/sprite.rs | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/light-client/src/render/sprite.rs b/light-client/src/render/sprite.rs new file mode 100644 index 00000000..c19f8392 --- /dev/null +++ b/light-client/src/render/sprite.rs @@ -0,0 +1,63 @@ +use hurrycurry_protocol::glam::Vec2; +use sdl2::rect::{FRect, Rect}; + +pub struct Sprite { + z_offset: f32, + src: Rect, + relative_dst: FRect, +} + +impl Sprite { + pub fn new(src: Rect, anchor: Vec2, elevation: f32) -> Self { + let relative_dst = FRect::new( + anchor.x - (src.w as f32) / 32. / 2., + anchor.y - (src.h as f32) / 24., + (src.w as f32) / 32., + (src.h as f32) / 24., + ); + Self { + z_offset: elevation, + src, + relative_dst, + } + } + pub fn new_tile(src: Rect) -> Self { + Self::new(src, Vec2::new(0.5, 1.0), 0.) + } + pub fn at(&self, pos: Vec2) -> SpriteDraw { + SpriteDraw { + z_order: ((self.z_offset + pos.y) * 24.) as i32, + src: self.src, + dst: FRect::new( + self.relative_dst.x + pos.x, + self.relative_dst.y + pos.y, + self.relative_dst.w, + self.relative_dst.h, + ), + } + } +} + +#[derive(Debug, Clone, Copy)] +pub struct SpriteDraw { + pub z_order: i32, + pub src: Rect, + pub dst: FRect, +} + +impl Ord for SpriteDraw { + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + self.z_order.cmp(&other.z_order) + } +} +impl PartialOrd for SpriteDraw { + fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { + Some(self.cmp(&other)) + } +} +impl Eq for SpriteDraw {} +impl PartialEq for SpriteDraw { + fn eq(&self, other: &Self) -> bool { + self.z_order == other.z_order && self.src == other.src && self.dst == other.dst + } +} |