Preparing things
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
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!(
|
||||
);
|
||||
|
||||
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, 500, 500))
|
||||
.expect("Failed to create texture")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user