changeset 705:ed65f9412bc0

anmrenderer: split common loading functions in another module.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 23 Aug 2019 19:29:00 +0200
parents 84af5bedbde4
children bca515da9047
files examples/anmrenderer.rs examples/common.rs
diffstat 2 files changed, 108 insertions(+), 99 deletions(-) [+]
line wrap: on
line diff
--- a/examples/anmrenderer.rs
+++ b/examples/anmrenderer.rs
@@ -1,13 +1,12 @@
-use image::{GenericImageView, DynamicImage};
 use luminance::blending::{Equation, Factor};
 use luminance::context::GraphicsContext;
 use luminance::framebuffer::Framebuffer;
 use luminance::pipeline::BoundTexture;
-use luminance::pixel::{NormRGB8UI, NormRGBA8UI, Floating};
+use luminance::pixel::Floating;
 use luminance::render_state::RenderState;
 use luminance::shader::program::{Program, Uniform};
 use luminance::tess::{Mode, TessBuilder};
-use luminance::texture::{Dim2, Flat, Sampler, Texture, GenMipmaps};
+use luminance::texture::{Dim2, Flat};
 use luminance_derive::{Semantics, Vertex, UniformInterface};
 use luminance_glfw::event::{Action, Key, WindowEvent};
 use luminance_glfw::surface::{GlfwSurface, Surface, WindowDim, WindowOpt};
@@ -16,12 +15,15 @@ use touhou::th06::anm0_vm::{AnmRunner, S
 use touhou::util::math::{perspective, setup_camera};
 use touhou::util::prng::Prng;
 use std::cell::RefCell;
-use std::fs::File;
-use std::io::{BufReader, Read};
 use std::rc::Rc;
 use std::env;
 use std::path::Path;
 
+#[path = "common.rs"]
+mod common;
+
+use common::{load_file_into_vec, load_rgb_png, load_rgb_a_pngs, LoadedTexture};
+
 const VS: &str = r#"
 in ivec3 in_position;
 in vec2 in_texcoord;
@@ -87,17 +89,14 @@ struct ShaderInterface {
     mvp: Uniform<[[f32; 4]; 4]>,
 }
 
-fn load_file_into_vec(filename: &Path) -> Vec<u8> {
-    let file = File::open(filename).unwrap();
-    let mut file = BufReader::new(file);
-    let mut buf = vec![];
-    file.read_to_end(&mut buf).unwrap();
-    buf
+fn fill_vertices_ptr(sprite: Rc<RefCell<Sprite>>, vertices: *mut Vertex) {
+    let mut fake_vertices = unsafe { std::mem::transmute::<*mut Vertex, &mut [FakeVertex; 4]>(vertices) };
+    sprite.borrow().fill_vertices(&mut fake_vertices, 0., 0., 0.);
 }
 
-enum LoadedTexture {
-    Rgba(Texture<Flat, Dim2, NormRGBA8UI>),
-    Rgb(Texture<Flat, Dim2, NormRGB8UI>),
+fn fill_vertices(sprite: Rc<RefCell<Sprite>>, vertices: &mut [Vertex; 4]) {
+    let mut fake_vertices = unsafe { std::mem::transmute::<&mut [Vertex; 4], &mut [FakeVertex; 4]>(vertices) };
+    sprite.borrow().fill_vertices(&mut fake_vertices, 0., 0., 0.);
 }
 
 fn main() {
@@ -140,10 +139,10 @@ fn main() {
     let tex = match anm0.alpha_filename {
         Some(ref filename) => {
             let alpha_filename = anm_filename.with_file_name(Path::new(filename).file_name().unwrap());
-            LoadedTexture::Rgba(png_alpha(&mut surface, &png_filename, &alpha_filename).expect("texture loading"))
+            load_rgb_a_pngs(&mut surface, &png_filename, &alpha_filename).expect("texture loading")
         },
         None => {
-            LoadedTexture::Rgb(load_from_disk(&mut surface, &png_filename).expect("texture loading"))
+            load_rgb_png(&mut surface, &png_filename).expect("texture loading")
         }
     };
 
@@ -218,86 +217,3 @@ fn main() {
         surface.swap_buffers();
     }
 }
-
-fn fill_vertices_ptr(sprite: Rc<RefCell<Sprite>>, vertices: *mut Vertex) {
-    let mut fake_vertices = unsafe { std::mem::transmute::<*mut Vertex, &mut [FakeVertex; 4]>(vertices) };
-    sprite.borrow().fill_vertices(&mut fake_vertices, 0., 0., 0.);
-}
-
-fn fill_vertices(sprite: Rc<RefCell<Sprite>>, vertices: &mut [Vertex; 4]) {
-    let mut fake_vertices = unsafe { std::mem::transmute::<&mut [Vertex; 4], &mut [FakeVertex; 4]>(vertices) };
-    sprite.borrow().fill_vertices(&mut fake_vertices, 0., 0., 0.);
-}
-
-fn load_from_disk(surface: &mut GlfwSurface, path: &Path) -> Option<Texture<Flat, Dim2, NormRGB8UI>> {
-    // load the texture into memory as a whole bloc (i.e. no streaming)
-    match image::open(&path) {
-        Ok(img) => {
-            let (width, height) = img.dimensions();
-            let texels = img
-                .pixels()
-                .map(|(_x, _y, rgb)| (rgb[0], rgb[1], rgb[2]))
-                .collect::<Vec<_>>();
-
-            // create the luminance texture; the third argument is the number of mipmaps we want (leave it
-            // to 0 for now) and the latest is a the sampler to use when sampling the texels in the
-            // shader (we’ll just use the default one)
-            let tex =
-                Texture::new(surface, [width, height], 0, &Sampler::default()).expect("luminance texture creation");
-
-            // the first argument disables mipmap generation (we don’t care so far)
-            tex.upload(GenMipmaps::No, &texels);
-
-            Some(tex)
-        }
-
-        Err(e) => {
-            eprintln!("cannot open image {}: {}", path.display(), e);
-            None
-        }
-    }
-}
-
-fn png_alpha(surface: &mut GlfwSurface, rgb: &Path, alpha: &Path) -> Option<Texture<Flat, Dim2, NormRGBA8UI>> {
-    // load the texture into memory as a whole bloc (i.e. no streaming)
-    match image::open(&alpha) {
-        Ok(img) => {
-            let (width, height) = img.dimensions();
-            let alpha = match img.grayscale() {
-                DynamicImage::ImageLuma8(img) => img,
-                _ => {
-                    eprintln!("cannot convert alpha image {} to grayscale", alpha.display());
-                    return None;
-                }
-            };
-            let img = match image::open(&rgb) {
-                Ok(img) => img,
-                Err(e) => {
-                    eprintln!("cannot open rgb image {}: {}", rgb.display(), e);
-                    return None;
-                },
-            };
-            let texels = img
-                .pixels()
-                .zip(alpha.pixels())
-                .map(|((_x, _y, rgb), luma)| (rgb[0], rgb[1], rgb[1], luma[0]))
-                .collect::<Vec<_>>();
-
-            // create the luminance texture; the third argument is the number of mipmaps we want (leave it
-            // to 0 for now) and the latest is a the sampler to use when sampling the texels in the
-            // shader (we’ll just use the default one)
-            let tex =
-                Texture::new(surface, [width, height], 0, &Sampler::default()).expect("luminance texture creation");
-
-            // the first argument disables mipmap generation (we don’t care so far)
-            tex.upload(GenMipmaps::No, &texels);
-
-            Some(tex)
-        }
-
-        Err(e) => {
-            eprintln!("cannot open alpha image {}: {}", alpha.display(), e);
-            None
-        }
-    }
-}
new file mode 100644
--- /dev/null
+++ b/examples/common.rs
@@ -0,0 +1,93 @@
+use image::{GenericImageView, DynamicImage};
+use luminance::pixel::{NormRGB8UI, NormRGBA8UI};
+use luminance::texture::{Dim2, Flat, Sampler, Texture, GenMipmaps};
+use luminance_glfw::surface::GlfwSurface;
+use std::fs::File;
+use std::io::{BufReader, Read};
+use std::path::Path;
+
+pub fn load_file_into_vec(filename: &Path) -> Vec<u8> {
+    let file = File::open(filename).unwrap();
+    let mut file = BufReader::new(file);
+    let mut buf = vec![];
+    file.read_to_end(&mut buf).unwrap();
+    buf
+}
+
+pub enum LoadedTexture {
+    Rgba(Texture<Flat, Dim2, NormRGBA8UI>),
+    Rgb(Texture<Flat, Dim2, NormRGB8UI>),
+}
+
+pub 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) => {
+            let (width, height) = img.dimensions();
+            let texels = img
+                .pixels()
+                .map(|(_x, _y, rgb)| (rgb[0], rgb[1], rgb[2]))
+                .collect::<Vec<_>>();
+
+            // create the luminance texture; the third argument is the number of mipmaps we want (leave it
+            // to 0 for now) and the latest is a the sampler to use when sampling the texels in the
+            // shader (we’ll just use the default one)
+            let tex =
+                Texture::new(surface, [width, height], 0, &Sampler::default()).expect("luminance texture creation");
+
+            // the first argument disables mipmap generation (we don’t care so far)
+            tex.upload(GenMipmaps::No, &texels);
+
+            Some(LoadedTexture::Rgb(tex))
+        }
+
+        Err(e) => {
+            eprintln!("cannot open image {}: {}", path.display(), e);
+            None
+        }
+    }
+}
+
+pub 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) => {
+            let (width, height) = img.dimensions();
+            let alpha = match img.grayscale() {
+                DynamicImage::ImageLuma8(img) => img,
+                _ => {
+                    eprintln!("cannot convert alpha image {} to grayscale", alpha.display());
+                    return None;
+                }
+            };
+            let img = match image::open(&rgb) {
+                Ok(img) => img,
+                Err(e) => {
+                    eprintln!("cannot open rgb image {}: {}", rgb.display(), e);
+                    return None;
+                },
+            };
+            let texels = img
+                .pixels()
+                .zip(alpha.pixels())
+                .map(|((_x, _y, rgb), luma)| (rgb[0], rgb[1], rgb[1], luma[0]))
+                .collect::<Vec<_>>();
+
+            // create the luminance texture; the third argument is the number of mipmaps we want (leave it
+            // to 0 for now) and the latest is a the sampler to use when sampling the texels in the
+            // shader (we’ll just use the default one)
+            let tex =
+                Texture::new(surface, [width, height], 0, &Sampler::default()).expect("luminance texture creation");
+
+            // the first argument disables mipmap generation (we don’t care so far)
+            tex.upload(GenMipmaps::No, &texels);
+
+            Some(LoadedTexture::Rgba(tex))
+        }
+
+        Err(e) => {
+            eprintln!("cannot open alpha image {}: {}", alpha.display(), e);
+            None
+        }
+    }
+}