teloader/tiles/raw.rs
1use base64::{DecodeError, Engine};
2use bytemuck::{Pod, Zeroable};
3
4use crate::interim::TileInterim;
5
6#[derive(Debug, Clone, Copy)]
7#[repr(C)]
8pub struct RawTile {
9 pub model: i32,
10 pub angle: i32,
11 pub texture: i32,
12 pub pitch: i32,
13}
14
15//SAFETY:
16//You can fill it.
17//All zeroes is valid configuration (empty tile).
18unsafe impl Zeroable for RawTile {}
19//SAFETY:
20//You can fill it.
21//All possible combinations are valid. Sort of.
22//No padding bytes. (only i32)
23//All fields are Pod.
24//It is repr C.
25//No pointers what so ever.
26unsafe impl Pod for RawTile {}
27
28/// Collection of raw representation of tiles inside the format.
29///
30/// The tiles are retrieved as is. Since the editor uses Run Length Encoding for empty
31/// tiles, this struct will have very few tiles.
32#[derive(Debug, Clone)]
33pub struct RawTiles {
34 /// Width of the grid.
35 ///
36 /// This is the size along X axis in editor.
37 pub width: usize,
38 /// Height of the grid.
39 ///
40 /// This is the size along Y axis in editor.
41 pub height: usize,
42 /// Length of the grid.
43 ///
44 /// This is the size along Z axis in editor.
45 pub length: usize,
46 /// All tiles of the grid.
47 pub tiles: Vec<RawTile>,
48}
49
50// Constructors
51impl RawTiles {
52 /// Creates [RawTiles] from intermediate tile representation.
53 pub(crate) fn from_interim(interim: &TileInterim) -> Result<Self, DecodeError> {
54 //unload base64
55 let bytes = base64::prelude::BASE64_STANDARD.decode(&interim.data)?;
56 Ok(Self {
57 width: interim.width as usize,
58 height: interim.height as usize,
59 length: interim.length as usize,
60 tiles: bytemuck::cast_slice(&bytes[..]).to_vec(),
61 })
62 }
63}