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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
use crate::core::colors::Color;
use crate::core::engine::ENGINE_STATE;
use serde::*;

#[derive(Copy, Clone, Serialize, Deserialize)]
#[repr(C)]
pub struct NativeGlyph {
    pub symbol: u32,
    pub foreground: Color,
    pub background: Color,
}

impl Default for NativeGlyph {
    fn default() -> Self {
        NativeGlyph {
            symbol: ' ' as u32,
            foreground: Color::GRAY,
            background: Color::BLACK,
        }
    }
}

#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub enum GlyphTint {
    None,
    Fore(Color),
    Back(Color),
    BackFore(Color, Color),
}

impl Default for GlyphTint {
    fn default() -> Self {
        GlyphTint::None
    }
}

#[derive(Clone, Copy, Default, Debug, Serialize, Deserialize)]
pub struct Glyph {
    pub symbol: Option<char>,
    pub tint: GlyphTint,
}

impl Glyph {
    pub const FLOOR: Glyph = Glyph {
        symbol: Some('.'),
        tint: GlyphTint::Fore(Color::WHITE),
    };

    pub const HERO: Glyph = Glyph {
        symbol: Some('@'),
        tint: GlyphTint::Fore(Color::WHITE),
    };

    pub const WALL: Glyph = Glyph {
        symbol: Some('#'),
        tint: GlyphTint::Fore(Color::GRAY),
    };
}

pub fn clear_screen() {
    let screen = &mut ENGINE_STATE.lock().unwrap().render_state.screen;
    if screen.is_ready() {
        for glyph in screen.glyphs_mut() {
            glyph.symbol = ' ' as u32;
            glyph.foreground = Color::GRAY;
            glyph.background = Color::BLACK;
        }
    }
}

pub fn print_char<P: Into<(u32, u32)>>(position: P, chr: char, depth: u32) {
    print_string_colors(position, chr.to_string(), Color::WHITE, Color::BLACK, depth);
}

pub fn print_glyph<P: Into<(u32, u32)>>(position: P, glyph: Glyph, depth: u32) {
    let pt = position.into();
    if glyph.symbol.is_some() {
        print_char(pt, glyph.symbol.unwrap(), depth);
    }

    match glyph.tint {
        GlyphTint::None => {}
        GlyphTint::Fore(f) => paint_tile(pt, Some(f), None, depth),
        GlyphTint::Back(b) => paint_tile(pt, None, Some(b), depth),
        GlyphTint::BackFore(b, f) => paint_tile(pt, Some(f), Some(b), depth),
    }
}

pub fn print_char_colors<P: Into<(u32, u32)>>(
    position: P,
    chr: char,
    fore: Color,
    back: Color,
    depth: u32,
) {
    print_string_colors(position, chr.to_string(), fore, back, depth);
}

pub fn print_string<S: AsRef<str>, P: Into<(u32, u32)>>(position: P, text: S, depth: u32) {
    print_string_colors(position, text, Color::WHITE, Color::BLACK, depth);
}

pub fn print_string_colors<S: AsRef<str>, P: Into<(u32, u32)>>(
    position: P,
    text: S,
    fore: Color,
    back: Color,
    depth: u32,
) {
    let window_size = ENGINE_STATE.lock().unwrap().render_state.screen_size;
    let screen = &mut ENGINE_STATE.lock().unwrap().render_state.screen;
    if screen.is_ready() {
        let mut p = position.into();
        if p.0 >= window_size.0 {
            return;
        }

        let mut index = screen.get_index_for_position(p).clone() as usize;

        for letter in text.as_ref().chars() {
            if p.0 >= window_size.0 {
                return;
            }

            p.0 += 1;

            if index >= screen.limit {
                return;
            }

            if screen.symbol_depth[index] <= depth {
                screen.symbol_depth[index] = depth;
                let glyphs = screen.glyphs_mut();
                glyphs[index].symbol = letter as u32;
            }

            if screen.fg_depth[index] <= depth {
                screen.fg_depth[index] = depth;
                let glyphs = screen.glyphs_mut();
                glyphs[index].foreground = fore;
            }

            if screen.bg_depth[index] <= depth {
                screen.bg_depth[index] = depth;
                let glyphs = screen.glyphs_mut();
                glyphs[index].background = back;
            }

            index += 1;
        }
    }
}

pub fn paint_tile<P: Into<(u32, u32)>>(
    position: P,
    fore: Option<Color>,
    back: Option<Color>,
    depth: u32,
) {
    let window_size = ENGINE_STATE
        .lock()
        .unwrap()
        .runtime_state
        .options
        .window_size
        .clone();

    let screen = &mut ENGINE_STATE.lock().unwrap().render_state.screen;
    if screen.is_ready() {
        let p = position.into();
        let index = screen.get_index_for_position(p).clone() as usize;

        if p.0 >= window_size.0 {
            return;
        }

        if index >= screen.limit {
            return;
        }

        if fore.is_some() && screen.fg_depth[index] <= depth {
            screen.fg_depth[index] = depth;
            let glyphs = screen.glyphs_mut();
            glyphs[index].foreground = fore.unwrap();
        }

        if back.is_some() && screen.bg_depth[index] <= depth {
            screen.bg_depth[index] = depth;
            let glyphs = screen.glyphs_mut();
            glyphs[index].background = back.unwrap();
        }
    }
}

pub fn paint_all_tiles(fore: Option<Color>, back: Option<Color>) {
    let screen = &mut ENGINE_STATE.lock().unwrap().render_state.screen;
    if screen.is_ready() {
        if fore.is_some() {
            let fg = fore.unwrap();
            for glyph in screen.glyphs_mut() {
                glyph.foreground = fg;
            }
        }

        if back.is_some() {
            let bg = back.unwrap();
            for glyph in screen.glyphs_mut() {
                glyph.foreground = bg;
            }
        }
    }
}