rust/mapgen/src/theme.rs
changeset 14175 a4c1a2d0ac24
parent 14169 1749961647b9
child 14180 76a52e8149e3
--- a/rust/mapgen/src/theme.rs	Thu Nov 08 00:00:58 2018 +0100
+++ b/rust/mapgen/src/theme.rs	Thu Nov 08 07:15:22 2018 +0300
@@ -38,7 +38,7 @@
     }
 
     #[inline]
-    pub fn rows(&self) -> impl Iterator<Item = &[u32]> {
+    pub fn rows(&self) -> impl DoubleEndedIterator<Item = &[u32]> {
         self.pixels.rows()
     }
 
@@ -46,16 +46,26 @@
     pub fn get_row(&self, index: usize) -> &[u32] {
         &self.pixels[index]
     }
+
+    #[inline]
+    pub fn get_pixel(&self, x: usize, y: usize) -> u32 {
+        self.pixels[y][x]
+    }
 }
 
 pub struct Theme {
-    land_texture: Option<ThemeSprite>
+    land_texture: Option<ThemeSprite>,
+    border_texture: Option<ThemeSprite>
 }
 
 impl Theme {
     pub fn land_texture(&self) -> Option<&ThemeSprite> {
         self.land_texture.as_ref()
     }
+
+    pub fn border_texture(&self) -> Option<&ThemeSprite> {
+        self.border_texture.as_ref()
+    }
 }
 
 #[derive(Debug)]
@@ -80,7 +90,8 @@
 impl Theme {
     pub fn new() -> Self {
         Theme {
-            land_texture: None
+            land_texture: None,
+            border_texture: None,
         }
     }
 
@@ -90,23 +101,9 @@
         for entry in read_dir(path)? {
             let file = entry?;
             if file.file_name() == "LandTex.png" {
-                let decoder = Decoder::new(
-                    BufReader::new(File::open(file.path())?));
-                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: Vec2D<u32> = Vec2D::new(size, 0);
-                reader.next_frame(slice_u32_to_u8_mut(buffer.as_mut_slice()))?;
-
-                let land_tex = ThemeSprite {
-                    pixels: buffer
-                };
-                theme.land_texture = Some(land_tex)
+                theme.land_texture = Some(load_sprite(&file.path())?)
+            } else if file.file_name() == "Border.png" {
+                theme.border_texture = Some(load_sprite(&file.path())?)
             }
         }
 
@@ -114,6 +111,23 @@
     }
 }
 
+fn load_sprite(path: &Path) -> Result<ThemeSprite, ThemeLoadError> {
+    let decoder = Decoder::new(
+        BufReader::new(File::open(path)?));
+    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 pixels: Vec2D<u32> = Vec2D::new(size, 0);
+    reader.next_frame(slice_u32_to_u8_mut(pixels.as_mut_slice()))?;
+
+    Ok(ThemeSprite { pixels })
+}
+
 pub fn slice_u32_to_u8(slice_u32: &[u32]) -> &[u8] {
     unsafe {
         from_raw_parts::<u8>(