From 4fb356822ef98241d2e44b0b0bd31d7f214f9f20 Mon Sep 17 00:00:00 2001 From: lamp Date: Sun, 5 Mar 2023 21:27:40 +0000 Subject: init --- src/texture.rs | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 src/texture.rs (limited to 'src/texture.rs') diff --git a/src/texture.rs b/src/texture.rs new file mode 100644 index 0000000..2751b7a --- /dev/null +++ b/src/texture.rs @@ -0,0 +1,81 @@ +pub struct Font { + pub charset_length: usize, + pub glyph_size: usize, + pub glyphs: Vec, +} + +impl Font { + pub fn load_from_bmp(bmp_data: &Vec, 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, +} + +impl Texture { + pub fn load_from_bmp(bmp_data: &Vec) -> 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, + } + } +} -- cgit v1.2.3