From d23a222da47b7a27671e815c60279bc04ee29a60 Mon Sep 17 00:00:00 2001 From: Jakob Meier Date: Sat, 25 Feb 2023 20:31:31 +0100 Subject: [PATCH] Bundled assets into source code --- src/blocks.rs | 208 ++++++++++++++++++++++++------------------------ src/main.rs | 1 + src/textures.rs | 110 +++++++++---------------- 3 files changed, 143 insertions(+), 176 deletions(-) diff --git a/src/blocks.rs b/src/blocks.rs index 77946b7..17bb871 100644 --- a/src/blocks.rs +++ b/src/blocks.rs @@ -1,8 +1,9 @@ -use std::collections::HashMap; +use macroquad::prelude::*; use crate::textures::DirectionalTexture; use nanoserde::{DeJson, SerJson}; +use std::collections::HashMap; -static mut CACHE: Option> = None; +static mut CACHE: Option> = None; #[derive(Eq, Hash, Clone, PartialEq, SerJson, DeJson)] pub enum Block { @@ -108,126 +109,123 @@ pub enum Block { impl Block { /// returns the texture for the block pub async fn get_texture(&self) -> DirectionalTexture { - let file_name = match self { - Block::Bridge => "bridge", - Block::BuildingCenter => "building_center", - Block::BuildingCenterBeige => "building_centerBeige", - Block::BuildingCorner => "building_corner", - Block::BuildingCornerBeige => "building_cornerBeige", - Block::BuildingDoor => "building_door", - Block::BuildingDoorBeige => "building_doorBeige", - Block::BuildingDoorWindows => "building_doorWindows", - Block::BuildingDoorWindowsBeige => "building_doorWindowsBeige", - Block::BuildingWindow => "building_window", - Block::BuildingWindowBeige => "building_windowBeige", - Block::BuildingWindows => "building_windows", - Block::BuildingWindowsBeige => "building_windowsBeige", - Block::CastleBend => "castle_bend", - Block::CastleCenter => "castle_center", - Block::CastleCorner => "castle_corner", - Block::CastleGate => "castle_gate", - Block::CastleGateOpen => "castle_gateOpen", - Block::CastleSlope => "castle_slope", - Block::CastleTower => "castle_tower", - Block::CastleTowerBeige => "castle_towerBeige", - Block::CastleTowerBrown => "castle_towerBrown", - Block::CastleTowerGreen => "castle_towerGreen", - Block::CastleTowerPurple => "castle_towerPurple", - Block::CastleWall => "castle_wall", - Block::CastelWindow => "castle_window", - - Block::DirtCenter => "dirt_center", - Block::DirtLow => "dirt_low", + unsafe { + if CACHE.is_none() { + CACHE = Some(HashMap::new()); + } + if let Some(map) = &CACHE { + if let Some(dt) = map.get(self) { + return dt.clone(); + } + } + } - Block::GrassCenter => "grass_center", - Block::GrassCorner => "grass_corner", - Block::GrassPath => "grass_path", - Block::GrassPathBend => "grass_pathBend", - Block::GrassPathCorner => "grass_pathCorner", - Block::GrassPathCrossing => "grass_pathCrossing", - Block::GrassPathEnd => "grass_pathEnd", - Block::GrassPathEndSquare => "grass_pathEndSquare", - Block::GrassPathSlope => "grass_pathSlope", - Block::GrassPathSplit => "grass_pathSplit", - Block::GrassRiver => "grass_river", - Block::GrassRiverBend => "grass_riverBend", - Block::GrassRiverBridge => "grass_riverBridge", - Block::GrassRiverCorner => "grass_riverCorner", - Block::GrassRiverCrossing => "grass_riverCrossing", - Block::GrassRiverEnd => "grass_riverEnd", - Block::GrassRiverEndSquare => "grass_riverEndSquare", - Block::GrassRiverSlope => "grass_riverSlope", - Block::GrassRiverSplit => "grass_riverSplit", - Block::GrassSlope => "grass_slope", - Block::GrassSlopeConcave => "grass_slopeConcave", - Block::GrassSlopeConvex => "grass_slopeConvex", - Block::GrassWater => "grass_water", - Block::GrassWaterConcave => "grass_waterConcave", - Block::GrassWaterConvex => "grass_waterConvex", - Block::GrassWaterRiver => "grass_waterRiver", + let dt = match self { + Block::Bridge => include_base_tile!("bridge"), + Block::BuildingCenter => include_base_tile!("building_center"), + Block::BuildingCenterBeige => include_base_tile!("building_centerBeige"), + Block::BuildingCorner => include_base_tile!("building_corner"), + Block::BuildingCornerBeige => include_base_tile!("building_cornerBeige"), + Block::BuildingDoor => include_base_tile!("building_door"), + Block::BuildingDoorBeige => include_base_tile!("building_doorBeige"), + Block::BuildingDoorWindows => include_base_tile!("building_doorWindows"), + Block::BuildingDoorWindowsBeige => include_base_tile!("building_doorWindowsBeige"), + Block::BuildingWindow => include_base_tile!("building_window"), + Block::BuildingWindowBeige => include_base_tile!("building_windowBeige"), + Block::BuildingWindows => include_base_tile!("building_windows"), + Block::BuildingWindowsBeige => include_base_tile!("building_windowsBeige"), - Block::RocksDirt => "rocks_dirt", - Block::RocksGrass => "rocks_grass", + Block::CastleBend => include_base_tile!("castle_bend"), + Block::CastleCenter => include_base_tile!("castle_center"), + Block::CastleCorner => include_base_tile!("castle_corner"), + Block::CastleGate => include_base_tile!("castle_gate"), + Block::CastleGateOpen => include_base_tile!("castle_gateOpen"), + Block::CastleSlope => include_base_tile!("castle_slope"), + Block::CastleTower => include_base_tile!("castle_tower"), + Block::CastleTowerBeige => include_base_tile!("castle_towerBeige"), + Block::CastleTowerBrown => include_base_tile!("castle_towerBrown"), + Block::CastleTowerGreen => include_base_tile!("castle_towerGreen"), + Block::CastleTowerPurple => include_base_tile!("castle_towerPurple"), + Block::CastleWall => include_base_tile!("castle_wall"), + Block::CastelWindow => include_base_tile!("castle_window"), - Block::RoofChurchBeige => "roof_churchBeige", - Block::RoofChuchBrown => "roof_churchBrown", - Block::RoofChurchGreen => "roof_churchGreen", - Block::RoofChurchPurple => "roof_churchPurple", - Block::RoofGableBeige => "roof_gableBeige", - Block::RoofGableBrown => "roof_gableBrown", - Block::RoofGableGreen => "roof_gableGreen", - Block::RoofGablePurple => "roof_gablePurple", - Block::RoofPointBeige => "roof_pointBeige", - Block::RoofPointBrown => "roof_pointBrown", - Block::RoofPointGreen => "roof_pointGreen", - Block::RoofPointPurple => "roof_pointPurple", - Block::RoofRoundBeige => "roof_roundBeige", - Block::RoofRoundBrown => "roof_roundBrown", - Block::RoofRoundGreen => "roof_roundGreen", - Block::RoofRoundPurple => "roof_roundPurple", - Block::RoofRoundedBeige => "roof_roundedBeige", - Block::RoofRoundedBrown => "roof_roundedBrown", - Block::RoofRoundedGreen => "roof_roundedGreen", - Block::RoofRoundedPurple => "roof_roundedPurple", - Block::RoofSlantBeige => "roof_slantBeige", - Block::RoofSlantBrown => "roof_slantBrown", - Block::RoofSlantGreen => "roof_slantGreen", - Block::RoofSlantPurple => "roof_slantPurple", + Block::DirtCenter => include_base_tile!("dirt_center"), + Block::DirtLow => include_base_tile!("dirt_low"), - Block::StructureArch => "structure_arch", - Block::StructureHigh => "structure_high", - Block::StructureLow => "structure_low", + Block::GrassCenter => include_base_tile!("grass_center"), + Block::GrassCorner => include_base_tile!("grass_corner"), + Block::GrassPath => include_base_tile!("grass_path"), + Block::GrassPathBend => include_base_tile!("grass_pathBend"), + Block::GrassPathCorner => include_base_tile!("grass_pathCorner"), + Block::GrassPathCrossing => include_base_tile!("grass_pathCrossing"), + Block::GrassPathEnd => include_base_tile!("grass_pathEnd"), + Block::GrassPathEndSquare => include_base_tile!("grass_pathEndSquare"), + Block::GrassPathSlope => include_base_tile!("grass_pathSlope"), + Block::GrassPathSplit => include_base_tile!("grass_pathSplit"), + Block::GrassRiver => include_base_tile!("grass_river"), + Block::GrassRiverBend => include_base_tile!("grass_riverBend"), + Block::GrassRiverBridge => include_base_tile!("grass_riverBridge"), + Block::GrassRiverCorner => include_base_tile!("grass_riverCorner"), + Block::GrassRiverCrossing => include_base_tile!("grass_riverCrossing"), + Block::GrassRiverEnd => include_base_tile!("grass_riverEnd"), + Block::GrassRiverEndSquare => include_base_tile!("grass_riverEndSquare"), + Block::GrassRiverSlope => include_base_tile!("grass_riverSlope"), + Block::GrassRiverSplit => include_base_tile!("grass_riverSplit"), + Block::GrassSlope => include_base_tile!("grass_slope"), + Block::GrassSlopeConcave => include_base_tile!("grass_slopeConcave"), + Block::GrassSlopeConvex => include_base_tile!("grass_slopeConvex"), + Block::GrassWater => include_base_tile!("grass_water"), + Block::GrassWaterConcave => include_base_tile!("grass_waterConcave"), + Block::GrassWaterConvex => include_base_tile!("grass_waterConvex"), + Block::GrassWaterRiver => include_base_tile!("grass_waterRiver"), - Block::Trees => "tree_multiple", - Block::Tree => "tree_single", + Block::RocksDirt => include_base_tile!("rocks_dirt"), + Block::RocksGrass => include_base_tile!("rocks_grass"), - Block::WaterCenter => "water_center", - Block::WaterFall => "water_fall" - }; + Block::RoofChurchBeige => include_base_tile!("roof_churchBeige"), + Block::RoofChuchBrown => include_base_tile!("roof_churchBrown"), + Block::RoofChurchGreen => include_base_tile!("roof_churchGreen"), + Block::RoofChurchPurple => include_base_tile!("roof_churchPurple"), + Block::RoofGableBeige => include_base_tile!("roof_gableBeige"), + Block::RoofGableBrown => include_base_tile!("roof_gableBrown"), + Block::RoofGableGreen => include_base_tile!("roof_gableGreen"), + Block::RoofGablePurple => include_base_tile!("roof_gablePurple"), + Block::RoofPointBeige => include_base_tile!("roof_pointBeige"), + Block::RoofPointBrown => include_base_tile!("roof_pointBrown"), + Block::RoofPointGreen => include_base_tile!("roof_pointGreen"), + Block::RoofPointPurple => include_base_tile!("roof_pointPurple"), + Block::RoofRoundBeige => include_base_tile!("roof_roundBeige"), + Block::RoofRoundBrown => include_base_tile!("roof_roundBrown"), + Block::RoofRoundGreen => include_base_tile!("roof_roundGreen"), + Block::RoofRoundPurple => include_base_tile!("roof_roundPurple"), + Block::RoofRoundedBeige => include_base_tile!("roof_roundedBeige"), + Block::RoofRoundedBrown => include_base_tile!("roof_roundedBrown"), + Block::RoofRoundedGreen => include_base_tile!("roof_roundedGreen"), + Block::RoofRoundedPurple => include_base_tile!("roof_roundedPurple"), + Block::RoofSlantBeige => include_base_tile!("roof_slantBeige"), + Block::RoofSlantBrown => include_base_tile!("roof_slantBrown"), + Block::RoofSlantGreen => include_base_tile!("roof_slantGreen"), + Block::RoofSlantPurple => include_base_tile!("roof_slantPurple"), + Block::StructureArch => include_base_tile!("structure_arch"), + Block::StructureHigh => include_base_tile!("structure_high"), + Block::StructureLow => include_base_tile!("structure_low"), - unsafe { - if CACHE.is_none() { - CACHE = Some(HashMap::new()); - } + Block::Trees => include_base_tile!("tree_multiple"), + Block::Tree => include_base_tile!("tree_single"), - if let Some(map) = &CACHE { - if let Some(tx) = map.get(file_name) { - return tx.clone(); - } - } + Block::WaterCenter => include_base_tile!("water_center"), + Block::WaterFall => include_base_tile!("water_fall") }; - let texture = DirectionalTexture::from_auto_png(file_name).await; - unsafe { if let Some(map) = &mut CACHE { - map.insert(file_name, texture.clone()); + map.insert(self.clone(), dt.clone()); } } - return texture.clone(); + return dt; } /// returns a list of all blocks pub fn all() -> Vec { diff --git a/src/main.rs b/src/main.rs index 62d2630..7bd8a6b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ use macroquad::prelude::*; mod screens; +#[macro_use] mod textures; mod types; mod blocks; diff --git a/src/textures.rs b/src/textures.rs index 8a6279b..16076b9 100644 --- a/src/textures.rs +++ b/src/textures.rs @@ -1,29 +1,6 @@ use macroquad::prelude::*; use crate::types::Direction; -const ASSET_FOLDER: &str = "assets/"; -const ASSET_BASE_FOLDER: &str = "base/"; -const ASSET_EXP_FOLDER: &str = "exp/"; -const ASSET_UI_FOLDER: &str = "ui/"; -const ASSET_FONTS_FOLDER: &str = "fonts/"; - -fn in_assets_folder(path: &str) -> String { - format!("{}{}", ASSET_FOLDER, path) -} -fn in_base_folder(path: &str) -> String { - in_assets_folder(&format!("{}{}", ASSET_BASE_FOLDER, path)) -} -fn in_exp_folder(path: &str) -> String { - in_assets_folder(&format!("{}{}", ASSET_EXP_FOLDER, path)) -} -fn in_ui_folder(path: &str) -> String { - in_assets_folder(&format!("{}{}", ASSET_UI_FOLDER, path)) -} -fn in_font_folder(path: &str) -> String { - in_assets_folder(&format!("{}{}", ASSET_FONTS_FOLDER, path)) -} - - /// Collection of textures required by game pub struct AssetStore { pub font: Font, @@ -45,22 +22,6 @@ pub struct DirectionalTexture { pub west: Texture2D, } impl DirectionalTexture { - pub async fn from_auto_png(name: &str) -> Self { - Self { - north: load_texture( - &in_base_folder(&format!("Tiles/{}_N.png", name))) - .await.expect("Unable to load texture N"), - south: load_texture( - &in_base_folder(&format!("Tiles/{}_S.png", name))) - .await.expect("Unable to load texture S"), - west: load_texture( - &in_base_folder(&format!("Tiles/{}_W.png", name))) - .await.expect("Unable to load texture W"), - east: load_texture( - &in_base_folder(&format!("Tiles/{}_E.png", name))) - .await.expect("Unable to load texture E"), - } - } pub fn get_dir(&self, dir: &Direction) -> Texture2D { match dir { Direction::North => self.north, @@ -71,43 +32,50 @@ impl DirectionalTexture { } } +macro_rules! include_base_tile { + ($name:literal, $dir:literal) => { + Texture2D::from_image( + &Image::from_file_with_format( + include_bytes!(concat!("../assets/base/Tiles/", $name, "_", $dir, ".png")), + Some(ImageFormat::Png) + ) + ) + }; + ($name:literal) => { + DirectionalTexture { + north: include_base_tile!($name, "N"), + south: include_base_tile!($name, "S"), + east: include_base_tile!($name, "E"), + west: include_base_tile!($name, "W"), + } + } +} +macro_rules! include_ui_img { + ($name:literal) => { + Texture2D::from_image( + &Image::from_file_with_format( + include_bytes!(concat!("../assets/ui/PNG/", $name, ".png")), + Some(ImageFormat::Png) + ) + ) + }; +} + impl AssetStore { /// loads all the textures pub async fn init() -> Self { Self { - font: load_ttf_font(&in_font_folder("Fonts/Kenney Pixel.ttf")) - .await.expect("Unable to load font"), + font: load_ttf_font_from_bytes(include_bytes!("../assets/fonts/Fonts/Kenney Pixel.ttf")).expect("Failed to load font"), ui: UIAssetCollection { - long_button: ( - load_texture( - &in_ui_folder("PNG/buttonLong_brown.png")) - .await.expect("Unable to load texture"), - load_texture( - &in_ui_folder("PNG/buttonLong_brown_pressed.png")) - .await.expect("Unable to load texture")), - panel_blue: ( - load_texture( - &in_ui_folder("PNG/panel_blue.png")) - .await.expect("Unable to load texture"), - load_texture( - &in_ui_folder("PNG/panelInset_blue.png")) - .await.expect("Unable to load texture")), - panel_brown: ( - load_texture( - &in_ui_folder("PNG/panel_brown.png")) - .await.expect("Unable to load texture"), - load_texture( - &in_ui_folder("PNG/panelInset_brown.png")) - .await.expect("Unable to load texture")), - arrow_left: load_texture( - &in_ui_folder("PNG/arrowBrown_left.png")) - .await.expect("Unable to load texture"), - arrow_right: load_texture( - &in_ui_folder("PNG/arrowBrown_right.png")) - .await.expect("Unable to load texture"), - close_button: load_texture( - &in_ui_folder("PNG/iconCross_brown.png")) - .await.expect("Unable to load texture") + long_button: (include_ui_img!("buttonLong_brown"), + include_ui_img!("buttonLong_brown_pressed")), + panel_blue: (include_ui_img!("panel_blue"), + include_ui_img!("panelInset_blue")), + panel_brown: (include_ui_img!("panel_brown"), + include_ui_img!("panelInset_brown")), + arrow_left: include_ui_img!("arrowBrown_left"), + arrow_right: include_ui_img!("arrowBrown_right"), + close_button: include_ui_img!("iconCross_brown") }, } } -- 2.38.5