Implement automatic tile creation
This commit is contained in:
@@ -1,11 +1,74 @@
|
||||
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, )
|
||||
$texture_path: tt,
|
||||
$width: expr,
|
||||
$height: expr, )
|
||||
*) => {
|
||||
|
||||
/// Describes all the textures that will be used in this game.
|
||||
@@ -39,7 +102,7 @@ macro_rules! make_textures {
|
||||
/// Creates the texture.
|
||||
fn $function_name() -> SfTexture {
|
||||
let bytes = include_bytes!($texture_path).to_vec();
|
||||
TextureManager::make_texture_from_bytes(bytes)
|
||||
TextureManager::make_texture_from_bytes(bytes, $width, $height)
|
||||
}
|
||||
)*
|
||||
|
||||
@@ -61,16 +124,20 @@ make_textures!(
|
||||
mario,
|
||||
make_mario_texture,
|
||||
"../../../assets/textures/mario.png",
|
||||
256,
|
||||
256,
|
||||
Overworld,
|
||||
overworld,
|
||||
make_overworld_texture,
|
||||
"../../../assets/textures/overworld.png",
|
||||
"../../../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>) -> SfTexture {
|
||||
SfTexture::from_memory(&bytes, &IntRect::new(0, 0, 256, 256))
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user