71 lines
2.0 KiB
Rust
71 lines
2.0 KiB
Rust
use sfml::graphics::{IntRect, Texture as SfTexture};
|
|
|
|
macro_rules! make_textures {
|
|
( $(
|
|
$enum_name: ident,
|
|
$texture_name: ident,
|
|
$function_name: ident,
|
|
$texture_path: tt, )
|
|
*) => {
|
|
|
|
/// 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)
|
|
}
|
|
)*
|
|
|
|
/// 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!(
|
|
Mario, mario, make_mario_texture, "../../../assets/test-textures/mario.png",
|
|
Overworld, overworld, make_overworld_texture, "../../../assets/textures/overworld.png",
|
|
);
|
|
|
|
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))
|
|
.expect("Failed to create texture")
|
|
}
|
|
}
|