annotate examples/common.rs @ 753:a662dddd4a2b

examples: Use array textures for enemy PNGs This requires luminance 0.39.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 25 Feb 2020 21:03:44 +0100
parents 5e5e7136ac92
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
745
90e907859bae examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 740
diff changeset
1 use image::{GenericImageView, DynamicImage, GrayImage, ImageError};
705
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2 use luminance::pixel::{NormRGB8UI, NormRGBA8UI};
753
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
3 use luminance::texture::{Dim2, Dim2Array, Sampler, Texture, GenMipmaps};
707
987409d48991 Switch to versioned luminance now that it got released, not hardcoded paths.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 706
diff changeset
4 use luminance_glfw::GlfwSurface;
706
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 705
diff changeset
5 use touhou::th06::anm0::Anm0;
705
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6 use std::fs::File;
752
5e5e7136ac92 examples: Return a Result for load_file_into_vec()
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 749
diff changeset
7 use std::io::{self, BufReader, Read};
705
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
8 use std::path::Path;
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9
752
5e5e7136ac92 examples: Return a Result for load_file_into_vec()
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 749
diff changeset
10 pub fn load_file_into_vec<P: AsRef<Path>>(filename: P) -> io::Result<Vec<u8>> {
5e5e7136ac92 examples: Return a Result for load_file_into_vec()
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 749
diff changeset
11 let file = File::open(filename)?;
705
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12 let mut file = BufReader::new(file);
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13 let mut buf = vec![];
752
5e5e7136ac92 examples: Return a Result for load_file_into_vec()
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 749
diff changeset
14 file.read_to_end(&mut buf)?;
5e5e7136ac92 examples: Return a Result for load_file_into_vec()
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 749
diff changeset
15 Ok(buf)
705
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
16 }
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
18 pub enum LoadedTexture {
753
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
19 Rgba(Texture<Dim2, NormRGBA8UI>),
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
20 Rgb(Texture<Dim2, NormRGB8UI>),
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
21 RgbaArray(Texture<Dim2Array, NormRGBA8UI>),
705
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
22 }
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23
717
d5d5496e4e53 examples: Propagate image loading errors.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 715
diff changeset
24 #[derive(Debug)]
d5d5496e4e53 examples: Propagate image loading errors.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 715
diff changeset
25 pub enum TextureLoadError {
d5d5496e4e53 examples: Propagate image loading errors.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 715
diff changeset
26 CannotOpenRgb(String, ImageError),
d5d5496e4e53 examples: Propagate image loading errors.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 715
diff changeset
27 CannotOpenAlpha(String, ImageError),
d5d5496e4e53 examples: Propagate image loading errors.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 715
diff changeset
28 AlphaToGrayscale(String),
d5d5496e4e53 examples: Propagate image loading errors.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 715
diff changeset
29 }
d5d5496e4e53 examples: Propagate image loading errors.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 715
diff changeset
30
745
90e907859bae examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 740
diff changeset
31 fn open_rgb_png(path: &Path) -> Result<DynamicImage, TextureLoadError> {
90e907859bae examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 740
diff changeset
32 // load the texture into memory as a whole bloc (i.e. no streaming)
90e907859bae examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 740
diff changeset
33 image::open(&path).map_err(|e| TextureLoadError::CannotOpenRgb(path.to_str().unwrap().to_owned(), e))
90e907859bae examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 740
diff changeset
34 }
90e907859bae examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 740
diff changeset
35
90e907859bae examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 740
diff changeset
36 fn open_alpha_png(path: &Path) -> Result<DynamicImage, TextureLoadError> {
705
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37 // load the texture into memory as a whole bloc (i.e. no streaming)
745
90e907859bae examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 740
diff changeset
38 image::open(&path).map_err(|e| TextureLoadError::CannotOpenAlpha(path.to_str().unwrap().to_owned(), e))
90e907859bae examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 740
diff changeset
39 }
90e907859bae examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 740
diff changeset
40
90e907859bae examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 740
diff changeset
41 fn merge_rgb_alpha(rgb: &DynamicImage, alpha: &GrayImage) -> Vec<(u8, u8, u8, u8)> {
90e907859bae examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 740
diff changeset
42 rgb
90e907859bae examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 740
diff changeset
43 .pixels()
90e907859bae examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 740
diff changeset
44 .zip(alpha.pixels())
90e907859bae examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 740
diff changeset
45 .map(|((_x, _y, rgb), luma)| (rgb[0], rgb[1], rgb[2], luma[0]))
90e907859bae examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 740
diff changeset
46 .collect::<Vec<_>>()
90e907859bae examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 740
diff changeset
47 }
705
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48
746
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 745
diff changeset
49 pub fn load_from_data(data: &[u8]) -> Result<DynamicImage, ImageError> {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 745
diff changeset
50 image::load_from_memory(data)
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 745
diff changeset
51 }
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 745
diff changeset
52
753
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
53 pub fn reupload_texture_from_rgb_image(tex: &mut Texture<Dim2, NormRGB8UI>, img: DynamicImage) -> Result<(), TextureLoadError> {
745
90e907859bae examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 740
diff changeset
54 let texels = img
90e907859bae examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 740
diff changeset
55 .pixels()
90e907859bae examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 740
diff changeset
56 .map(|(_x, _y, rgb)| (rgb[0], rgb[1], rgb[2]))
90e907859bae examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 740
diff changeset
57 .collect::<Vec<_>>();
705
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
58
749
ee45bfde91bb examples: Add missing reupload_texture_from_rgb_image() to common
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 746
diff changeset
59 // the first argument disables mipmap generation (we don’t care so far)
ee45bfde91bb examples: Add missing reupload_texture_from_rgb_image() to common
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 746
diff changeset
60 tex.upload(GenMipmaps::No, &texels).unwrap();
ee45bfde91bb examples: Add missing reupload_texture_from_rgb_image() to common
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 746
diff changeset
61
ee45bfde91bb examples: Add missing reupload_texture_from_rgb_image() to common
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 746
diff changeset
62 Ok(())
ee45bfde91bb examples: Add missing reupload_texture_from_rgb_image() to common
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 746
diff changeset
63 }
ee45bfde91bb examples: Add missing reupload_texture_from_rgb_image() to common
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 746
diff changeset
64
ee45bfde91bb examples: Add missing reupload_texture_from_rgb_image() to common
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 746
diff changeset
65 pub fn upload_texture_from_rgb_image(surface: &mut GlfwSurface, img: DynamicImage) -> Result<LoadedTexture, TextureLoadError> {
ee45bfde91bb examples: Add missing reupload_texture_from_rgb_image() to common
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 746
diff changeset
66 let (width, height) = img.dimensions();
ee45bfde91bb examples: Add missing reupload_texture_from_rgb_image() to common
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 746
diff changeset
67
745
90e907859bae examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 740
diff changeset
68 // create the luminance texture; the third argument is the number of mipmaps we want (leave it
90e907859bae examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 740
diff changeset
69 // to 0 for now) and the latest is a the sampler to use when sampling the texels in the
90e907859bae examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 740
diff changeset
70 // shader (we’ll just use the default one)
749
ee45bfde91bb examples: Add missing reupload_texture_from_rgb_image() to common
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 746
diff changeset
71 let mut tex =
745
90e907859bae examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 740
diff changeset
72 Texture::new(surface, [width, height], 0, Sampler::default()).expect("luminance texture creation");
705
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
73
749
ee45bfde91bb examples: Add missing reupload_texture_from_rgb_image() to common
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 746
diff changeset
74 reupload_texture_from_rgb_image(&mut tex, img)?;
705
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
75
745
90e907859bae examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 740
diff changeset
76 Ok(LoadedTexture::Rgb(tex))
705
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
77 }
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
78
746
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 745
diff changeset
79 pub fn load_rgb_texture(surface: &mut GlfwSurface, path: &Path) -> Result<LoadedTexture, TextureLoadError> {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 745
diff changeset
80 let img = open_rgb_png(&path)?;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 745
diff changeset
81 upload_texture_from_rgb_image(surface, img)
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 745
diff changeset
82 }
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 745
diff changeset
83
717
d5d5496e4e53 examples: Propagate image loading errors.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 715
diff changeset
84 fn load_rgb_a_pngs(surface: &mut GlfwSurface, rgb: &Path, alpha: &Path) -> Result<LoadedTexture, TextureLoadError> {
745
90e907859bae examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 740
diff changeset
85 let img = open_alpha_png(&alpha)?;
90e907859bae examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 740
diff changeset
86 let alpha = match img.grayscale() {
90e907859bae examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 740
diff changeset
87 DynamicImage::ImageLuma8(img) => img,
90e907859bae examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 740
diff changeset
88 _ => {
90e907859bae examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 740
diff changeset
89 return Err(TextureLoadError::AlphaToGrayscale(alpha.to_str().unwrap().to_owned()))
90e907859bae examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 740
diff changeset
90 }
90e907859bae examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 740
diff changeset
91 };
90e907859bae examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 740
diff changeset
92 let (width, height) = img.dimensions();
90e907859bae examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 740
diff changeset
93 let img = open_rgb_png(&rgb)?;
90e907859bae examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 740
diff changeset
94 assert_eq!((width, height), img.dimensions());
90e907859bae examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 740
diff changeset
95 let texels = merge_rgb_alpha(&img, &alpha);
705
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
96
745
90e907859bae examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 740
diff changeset
97 // create the luminance texture; the third argument is the number of mipmaps we want (leave it
90e907859bae examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 740
diff changeset
98 // to 0 for now) and the latest is a the sampler to use when sampling the texels in the
90e907859bae examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 740
diff changeset
99 // shader (we’ll just use the default one)
90e907859bae examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 740
diff changeset
100 let tex =
90e907859bae examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 740
diff changeset
101 Texture::new(surface, [width, height], 0, Sampler::default()).expect("luminance texture creation");
705
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
102
745
90e907859bae examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 740
diff changeset
103 // the first argument disables mipmap generation (we don’t care so far)
90e907859bae examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 740
diff changeset
104 tex.upload(GenMipmaps::No, &texels).unwrap();
705
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
105
745
90e907859bae examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 740
diff changeset
106 Ok(LoadedTexture::Rgba(tex))
705
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
107 }
706
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 705
diff changeset
108
740
8d29dac12219 examples: Make the common functions take AsRef<Path>.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 717
diff changeset
109 pub fn load_anm_image<P: AsRef<Path>>(mut surface: &mut GlfwSurface, anm0: &Anm0, anm_filename: P) -> Result<LoadedTexture, TextureLoadError> {
8d29dac12219 examples: Make the common functions take AsRef<Path>.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 717
diff changeset
110 let anm_filename = anm_filename.as_ref();
706
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 705
diff changeset
111 let png_filename = anm_filename.with_file_name(Path::new(&anm0.png_filename).file_name().unwrap());
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 705
diff changeset
112 match anm0.alpha_filename {
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 705
diff changeset
113 Some(ref filename) => {
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 705
diff changeset
114 let alpha_filename = anm_filename.with_file_name(Path::new(filename).file_name().unwrap());
717
d5d5496e4e53 examples: Propagate image loading errors.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 715
diff changeset
115 load_rgb_a_pngs(&mut surface, &png_filename, &alpha_filename)
706
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 705
diff changeset
116 },
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 705
diff changeset
117 None => {
746
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 745
diff changeset
118 load_rgb_texture(&mut surface, &png_filename)
706
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 705
diff changeset
119 }
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 705
diff changeset
120 }
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 705
diff changeset
121 }
753
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
122
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
123 fn load_array_texture(surface: &mut GlfwSurface, images: &[(&Path, &Path)]) -> Result<LoadedTexture, TextureLoadError> {
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
124 let mut decoded = vec![];
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
125 let dimensions = (256, 256);
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
126 for (rgb, alpha) in images {
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
127 let img = open_alpha_png(&alpha)?;
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
128 assert_eq!(dimensions, img.dimensions());
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
129 let alpha = match img.grayscale() {
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
130 DynamicImage::ImageLuma8(img) => img,
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
131 _ => {
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
132 return Err(TextureLoadError::AlphaToGrayscale(alpha.to_str().unwrap().to_owned()))
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
133 }
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
134 };
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
135 let img = open_rgb_png(&rgb)?;
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
136 assert_eq!(dimensions, img.dimensions());
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
137 let texels = merge_rgb_alpha(&img, &alpha);
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
138 decoded.push(texels);
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
139 }
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
140
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
141 // create the luminance texture; the third argument is the number of mipmaps we want (leave it
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
142 // to 0 for now) and the latest is a the sampler to use when sampling the texels in the
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
143 // shader (we’ll just use the default one)
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
144 let tex =
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
145 Texture::new(surface, ([dimensions.0, dimensions.1], images.len() as u32), 0, Sampler::default()).expect("luminance texture creation");
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
146
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
147 // the first argument disables mipmap generation (we don’t care so far)
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
148 tex.upload(GenMipmaps::No, &decoded.into_iter().flatten().collect::<Vec<_>>()).unwrap();
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
149
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
150 Ok(LoadedTexture::RgbaArray(tex))
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
151 }
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
152
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
153 pub fn load_multiple_anm_images<P: AsRef<Path>>(mut surface: &mut GlfwSurface, anms: &[Anm0], anm_filename: P) -> Result<LoadedTexture, TextureLoadError> {
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
154 let anm_filename = anm_filename.as_ref();
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
155 let mut paths = vec![];
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
156 for anm0 in anms.iter() {
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
157 let rgb_filename = anm_filename.with_file_name(Path::new(&anm0.png_filename).file_name().unwrap());
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
158 let filename = anm0.alpha_filename.as_ref().expect("Can’t not have alpha here!");
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
159 let alpha_filename = anm_filename.with_file_name(Path::new(filename).file_name().unwrap());
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
160 paths.push((rgb_filename, alpha_filename));
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
161 }
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
162 let paths: Vec<_> = paths.iter().map(|(rgb, alpha)| (rgb.as_ref(), alpha.as_ref())).collect();
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
163 load_array_texture(&mut surface, paths.as_slice())
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
164 }