aboutsummaryrefslogtreecommitdiff
path: root/pixel-client/src/render/sprite.rs
blob: 292a99abf2c54a183eab18e323b7df3e921a4ecb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
    Hurry Curry! - a game about cooking
    Copyright 2024 metamuffin

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, version 3 of the License only.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.

*/
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.5)
    }
    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,
            ),
            tint: [0xff; 4],
        }
    }
}

#[derive(Debug, Clone, Copy)]
pub struct SpriteDraw {
    pub tint: [u8; 4],
    pub z_order: i32,
    pub src: Rect,
    pub dst: FRect,
}

impl SpriteDraw {
    pub fn screen(src: Rect, z_order: i32, pos: Vec2, size: Vec2, tint: Option<[u8; 4]>) -> Self {
        Self {
            dst: FRect::new(pos.x, pos.y, size.x, size.y),
            src,
            tint: tint.unwrap_or([0xff; 4]),
            z_order,
        }
    }
    pub fn overlay(src: Rect, pos: Vec2, size: Vec2, tint: Option<[u8; 4]>) -> Self {
        SpriteDraw::screen(src, i32::MAX, pos, size, tint)
    }
    pub fn underlay(src: Rect, pos: Vec2, size: Vec2, tint: Option<[u8; 4]>) -> Self {
        SpriteDraw::screen(src, i32::MIN, pos, size, tint)
    }
    pub fn alpha(mut self, alpha: f32) -> Self {
        self.tint[3] = (alpha.clamp(0., 1.) * 255.) as u8;
        self
    }
    pub fn tint(mut self, r: u8, g: u8, b: u8) -> Self {
        self.tint[0] = r;
        self.tint[1] = g;
        self.tint[2] = b;
        self
    }
}

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
    }
}