# HG changeset patch # User alfadur # Date 1541550956 -10800 # Node ID 3c8a33ba06ba9e6fea7e9c90be79a1b624a6226a # Parent 6205a5230d23982c38baef364c15d604111c1da9 start loading theme textures diff -r 6205a5230d23 -r 3c8a33ba06ba rust/mapgen/Cargo.toml --- a/rust/mapgen/Cargo.toml Wed Nov 07 00:02:48 2018 +0300 +++ b/rust/mapgen/Cargo.toml Wed Nov 07 03:35:56 2018 +0300 @@ -13,4 +13,5 @@ rand = "0.5" serde = "1.0" serde_yaml = "0.8" -serde_derive = "1.0" \ No newline at end of file +serde_derive = "1.0" +png = "0.13" \ No newline at end of file diff -r 6205a5230d23 -r 3c8a33ba06ba rust/mapgen/src/lib.rs --- a/rust/mapgen/src/lib.rs Wed Nov 07 00:02:48 2018 +0300 +++ b/rust/mapgen/src/lib.rs Wed Nov 07 03:35:56 2018 +0300 @@ -1,3 +1,5 @@ +pub mod theme; + use std::{ collections::hash_map::HashMap, borrow::Borrow, @@ -11,6 +13,8 @@ outline_template::OutlineTemplate }; use rand::{thread_rng, Rng}; +use land2d::Land2D; +use theme::Theme; #[derive(Deserialize)] struct PointDesc { @@ -101,6 +105,10 @@ pub fn get_template(&self, template_type: &str) -> Option<&OutlineTemplate> { self.templates.get(template_type).and_then(|t| thread_rng().choose(t)) } + + pub fn make_texture(&self, land: &Land2D, theme: &Theme) { + + } } #[cfg(test)] diff -r 6205a5230d23 -r 3c8a33ba06ba rust/mapgen/src/theme.rs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rust/mapgen/src/theme.rs Wed Nov 07 03:35:56 2018 +0300 @@ -0,0 +1,91 @@ +use std::{ + slice, + io, + io::BufReader, + fs::{File, read_dir}, + path::Path +}; +use png::{ + BitDepth, + ColorType, + Decoder, + DecodingError +}; + +use integral_geometry::{ + Rect, Size +}; + +pub struct ThemeSprite { + bounds: Size, + pixels: Vec +} + +pub struct Theme { + land_texture: Option +} + +pub enum ThemeLoadError { + File(io::Error), + Decoding(DecodingError), + Format(String) +} + +impl From for ThemeLoadError { + fn from(e: io::Error) -> Self { + ThemeLoadError::File(e) + } +} + +impl From for ThemeLoadError { + fn from(e: DecodingError) -> Self { + ThemeLoadError::Decoding(e) + } +} + +impl Theme { + pub fn new() -> Self { + Theme { + land_texture: None + } + } + + pub fn load(path: &Path) -> Result { + let mut theme = Self::new(); + + for entry in read_dir(path)? { + let file = entry?; + if file.file_name() == "LandTex.png" { + let buffer = BufReader::new(File::create(file.path())?); + let decoder = Decoder::new(buffer); + let (info, mut reader) = decoder.read_info()?; + + if info.color_type != ColorType::RGBA { + return Err(ThemeLoadError::Format( + format!("Unexpected format: {:?}", info.color_type))); + } + let size = Size::new(info.width as usize, info.height as usize); + + let mut buffer: Vec = Vec::with_capacity(size.area()); + let mut slice_u32 = buffer.as_mut_slice(); + let mut slice_u8 = unsafe { + slice::from_raw_parts_mut::( + slice.as_mut_ptr() as *mut u8, + slice.len() / 4 + ) + }; + reader.next_frame(slice_u8); + + let land_tex = ThemeSprite { + bounds: size, + pixels: buffer + }; + theme.land_texture = Some(land_tex) + } + } + + Ok(theme) + } +} + +