diff examples/common.rs @ 706:bca515da9047

examples: use common module.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 23 Aug 2019 19:46:47 +0200
parents ed65f9412bc0
children 987409d48991
line wrap: on
line diff
--- a/examples/common.rs
+++ b/examples/common.rs
@@ -2,6 +2,7 @@ use image::{GenericImageView, DynamicIma
 use luminance::pixel::{NormRGB8UI, NormRGBA8UI};
 use luminance::texture::{Dim2, Flat, Sampler, Texture, GenMipmaps};
 use luminance_glfw::surface::GlfwSurface;
+use touhou::th06::anm0::Anm0;
 use std::fs::File;
 use std::io::{BufReader, Read};
 use std::path::Path;
@@ -19,7 +20,7 @@ pub enum LoadedTexture {
     Rgb(Texture<Flat, Dim2, NormRGB8UI>),
 }
 
-pub fn load_rgb_png(surface: &mut GlfwSurface, path: &Path) -> Option<LoadedTexture> {
+fn load_rgb_png(surface: &mut GlfwSurface, path: &Path) -> Option<LoadedTexture> {
     // load the texture into memory as a whole bloc (i.e. no streaming)
     match image::open(&path) {
         Ok(img) => {
@@ -48,7 +49,7 @@ pub fn load_rgb_png(surface: &mut GlfwSu
     }
 }
 
-pub fn load_rgb_a_pngs(surface: &mut GlfwSurface, rgb: &Path, alpha: &Path) -> Option<LoadedTexture> {
+fn load_rgb_a_pngs(surface: &mut GlfwSurface, rgb: &Path, alpha: &Path) -> Option<LoadedTexture> {
     // load the texture into memory as a whole bloc (i.e. no streaming)
     match image::open(&alpha) {
         Ok(img) => {
@@ -70,7 +71,7 @@ pub fn load_rgb_a_pngs(surface: &mut Glf
             let texels = img
                 .pixels()
                 .zip(alpha.pixels())
-                .map(|((_x, _y, rgb), luma)| (rgb[0], rgb[1], rgb[1], luma[0]))
+                .map(|((_x, _y, rgb), luma)| (rgb[0], rgb[1], rgb[2], luma[0]))
                 .collect::<Vec<_>>();
 
             // create the luminance texture; the third argument is the number of mipmaps we want (leave it
@@ -91,3 +92,17 @@ pub fn load_rgb_a_pngs(surface: &mut Glf
         }
     }
 }
+
+pub fn load_anm_image(mut surface: &mut GlfwSurface, anm0: &Anm0, anm_filename: &Path) -> LoadedTexture {
+    let png_filename = anm_filename.with_file_name(Path::new(&anm0.png_filename).file_name().unwrap());
+    match anm0.alpha_filename {
+        Some(ref filename) => {
+            let alpha_filename = anm_filename.with_file_name(Path::new(filename).file_name().unwrap());
+            println!("alpha {:?}", alpha_filename);
+            load_rgb_a_pngs(&mut surface, &png_filename, &alpha_filename).expect("texture loading")
+        },
+        None => {
+            load_rgb_png(&mut surface, &png_filename).expect("texture loading")
+        }
+    }
+}