144 lines
3.3 KiB
Rust
144 lines
3.3 KiB
Rust
use sfml::graphics::{IntRect, Texture as SfTexture};
|
|
|
|
/// The number of pixels of a sprite.
|
|
pub const SPRITE_SIZE_I32: i32 = 32;
|
|
|
|
/// The number of pixels of a sprite.
|
|
pub const SPRITE_SIZE_F32: f32 = 32.0;
|
|
|
|
/// Converts the byte with bits corresponding to the neighbours to the offset on the generated
|
|
/// tileset.
|
|
pub fn byte_to_index(byte: u8) -> i32 {
|
|
match byte {
|
|
0 => 0,
|
|
1 => 1,
|
|
4 => 2,
|
|
5 => 3,
|
|
7 => 4,
|
|
16 => 5,
|
|
17 => 6,
|
|
20 => 7,
|
|
21 => 8,
|
|
23 => 9,
|
|
28 => 10,
|
|
29 => 11,
|
|
31 => 12,
|
|
64 => 13,
|
|
65 => 14,
|
|
68 => 15,
|
|
69 => 16,
|
|
71 => 17,
|
|
80 => 18,
|
|
81 => 19,
|
|
84 => 20,
|
|
85 => 21,
|
|
87 => 22,
|
|
92 => 23,
|
|
93 => 24,
|
|
95 => 25,
|
|
112 => 26,
|
|
113 => 27,
|
|
116 => 28,
|
|
117 => 29,
|
|
119 => 30,
|
|
124 => 31,
|
|
125 => 32,
|
|
127 => 33,
|
|
193 => 34,
|
|
197 => 35,
|
|
199 => 36,
|
|
209 => 37,
|
|
213 => 38,
|
|
215 => 39,
|
|
221 => 40,
|
|
223 => 41,
|
|
241 => 42,
|
|
245 => 43,
|
|
247 => 44,
|
|
253 => 45,
|
|
255 => 46,
|
|
_ => panic!("Incorrect byte {}", byte),
|
|
}
|
|
}
|
|
|
|
macro_rules! make_textures {
|
|
( $(
|
|
$enum_name: ident,
|
|
$texture_name: ident,
|
|
$function_name: ident,
|
|
$texture_path: tt,
|
|
$width: expr,
|
|
$height: expr, )
|
|
*) => {
|
|
|
|
/// Describes all the textures that will be used in this game.
|
|
#[derive(Copy, Clone)]
|
|
pub enum Texture {
|
|
$(
|
|
/// A texture.
|
|
$enum_name,
|
|
)*
|
|
}
|
|
|
|
/// A struct that holds all the textures.
|
|
pub struct TextureManager {
|
|
$(
|
|
/// A texture.
|
|
$texture_name: SfTexture,
|
|
)*
|
|
}
|
|
|
|
impl TextureManager {
|
|
/// Creates a new texture manager and initializes all textures.
|
|
pub fn new() -> TextureManager {
|
|
TextureManager {
|
|
$(
|
|
$texture_name: TextureManager::$function_name(),
|
|
)*
|
|
}
|
|
}
|
|
|
|
$(
|
|
/// Creates the texture.
|
|
fn $function_name() -> SfTexture {
|
|
let bytes = include_bytes!($texture_path).to_vec();
|
|
TextureManager::make_texture_from_bytes(bytes, $width, $height)
|
|
}
|
|
)*
|
|
|
|
/// Returns the real texture from the texture id.
|
|
pub fn get(&self, id: Texture) -> &SfTexture {
|
|
match id {
|
|
$(
|
|
Texture::$enum_name => &self.$texture_name,
|
|
)*
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
make_textures!(
|
|
Rusty,
|
|
rusty,
|
|
make_rusty_texture,
|
|
"../../../assets/textures/rusty.png",
|
|
256,
|
|
256,
|
|
Overworld,
|
|
overworld,
|
|
make_overworld_texture,
|
|
"../../../assets/textures-generated/grass.png",
|
|
32 * 47,
|
|
32,
|
|
);
|
|
|
|
impl TextureManager {
|
|
/// Creates a textures from an array of bytes.
|
|
fn make_texture_from_bytes(bytes: Vec<u8>, width: i32, height: i32) -> SfTexture {
|
|
SfTexture::from_memory(&bytes, &IntRect::new(0, 0, width, height))
|
|
.expect("Failed to create texture")
|
|
}
|
|
}
|