start loading theme textures
authoralfadur
Wed, 07 Nov 2018 03:35:56 +0300
changeset 14172 3c8a33ba06ba
parent 14171 6205a5230d23
child 14173 5acfdf49742d
start loading theme textures
rust/mapgen/Cargo.toml
rust/mapgen/src/lib.rs
rust/mapgen/src/theme.rs
--- 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
--- 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<u32>, theme: &Theme) {
+
+    }
 }
 
 #[cfg(test)]
--- /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<u32>
+}
+
+pub struct Theme {
+    land_texture: Option<ThemeSprite>
+}
+
+pub enum ThemeLoadError {
+    File(io::Error),
+    Decoding(DecodingError),
+    Format(String)
+}
+
+impl From<io::Error> for ThemeLoadError {
+    fn from(e: io::Error) -> Self {
+        ThemeLoadError::File(e)
+    }
+}
+
+impl From<DecodingError> 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<Theme, ThemeLoadError> {
+        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<u32> = Vec::with_capacity(size.area());
+                let mut slice_u32 = buffer.as_mut_slice();
+                let mut slice_u8 = unsafe {
+                    slice::from_raw_parts_mut::<u8>(
+                        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)
+    }
+}
+
+