aboutsummaryrefslogtreecommitdiff
path: root/src/texture.rs
blob: 2751b7a02cb4a6fb8dcf2048bcfc991bea67cc6c (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
pub struct Font {
    pub charset_length: usize,
    pub glyph_size: usize,
    pub glyphs: Vec<bool>,
}

impl Font {
    pub fn load_from_bmp(bmp_data: &Vec<u8>, glyph_size: usize) -> Font {
        let data_position = u32::from_le_bytes([
            bmp_data[0x0A],
            bmp_data[0x0B],
            bmp_data[0x0C],
            bmp_data[0x0D],
        ]);
        let mut glyphs = Vec::new();
        // step_by 3 assuming 24-bit depth
        for byte_index in ((data_position as usize)..bmp_data.len()).step_by(3) {
            glyphs.push(bmp_data[byte_index] == 0xFF);
        }
        Font {
            charset_length: 256,
            glyph_size,
            glyphs,
        }
    }
}

pub struct Texture {
    pub width: usize,
    pub height: usize,
    pub has_transparency: bool,
    pub data: Vec<u32>,
}

impl Texture {
    pub fn load_from_bmp(bmp_data: &Vec<u8>) -> Texture {
        let data_position = u32::from_le_bytes([
            bmp_data[0x0A],
            bmp_data[0x0B],
            bmp_data[0x0C],
            bmp_data[0x0D],
        ]);
        // assuming windows BITMAPINFOHEADER, these are i32
        let width = i32::from_le_bytes([
            bmp_data[0x12],
            bmp_data[0x13],
            bmp_data[0x14],
            bmp_data[0x15],
        ]) as usize;
        let height = i32::from_le_bytes([
            bmp_data[0x16],
            bmp_data[0x17],
            bmp_data[0x18],
            bmp_data[0x19],
        ]) as usize;
        let mut has_transparency = false;
        let mut data = Vec::with_capacity(width * height);
        // step_by 3 assuming 24-bit depth
        for byte_index in ((data_position as usize)..bmp_data.len()).step_by(3) {
            if bmp_data[byte_index] == 0x00
                && bmp_data[byte_index + 1] == 0x00
                && bmp_data[byte_index + 2] == 0x00
            {
                has_transparency = true;
            }
            data.push(u32::from_le_bytes([
                bmp_data[byte_index],
                bmp_data[byte_index + 1],
                bmp_data[byte_index + 2],
                0x00,
            ]));
        }
        data.reverse();
        Texture {
            width,
            height,
            has_transparency,
            data,
        }
    }
}