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) + } +} + +