comparison examples/common.rs @ 746:0ebf6467e4ff

examples: Add a menu example.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 18 Jan 2020 19:19:51 +0100
parents 90e907859bae
children ee45bfde91bb
comparison
equal deleted inserted replaced
745:90e907859bae 746:0ebf6467e4ff
43 .zip(alpha.pixels()) 43 .zip(alpha.pixels())
44 .map(|((_x, _y, rgb), luma)| (rgb[0], rgb[1], rgb[2], luma[0])) 44 .map(|((_x, _y, rgb), luma)| (rgb[0], rgb[1], rgb[2], luma[0]))
45 .collect::<Vec<_>>() 45 .collect::<Vec<_>>()
46 } 46 }
47 47
48 fn load_rgb_png(surface: &mut GlfwSurface, path: &Path) -> Result<LoadedTexture, TextureLoadError> { 48 pub fn load_from_data(data: &[u8]) -> Result<DynamicImage, ImageError> {
49 let img = open_rgb_png(&path)?; 49 image::load_from_memory(data)
50 }
51
52 pub fn upload_texture_from_rgb_image(surface: &mut GlfwSurface, img: DynamicImage) -> Result<LoadedTexture, TextureLoadError> {
50 let (width, height) = img.dimensions(); 53 let (width, height) = img.dimensions();
51 let texels = img 54 let texels = img
52 .pixels() 55 .pixels()
53 .map(|(_x, _y, rgb)| (rgb[0], rgb[1], rgb[2])) 56 .map(|(_x, _y, rgb)| (rgb[0], rgb[1], rgb[2]))
54 .collect::<Vec<_>>(); 57 .collect::<Vec<_>>();
61 64
62 // the first argument disables mipmap generation (we don’t care so far) 65 // the first argument disables mipmap generation (we don’t care so far)
63 tex.upload(GenMipmaps::No, &texels).unwrap(); 66 tex.upload(GenMipmaps::No, &texels).unwrap();
64 67
65 Ok(LoadedTexture::Rgb(tex)) 68 Ok(LoadedTexture::Rgb(tex))
69 }
70
71 pub fn load_rgb_texture(surface: &mut GlfwSurface, path: &Path) -> Result<LoadedTexture, TextureLoadError> {
72 let img = open_rgb_png(&path)?;
73 upload_texture_from_rgb_image(surface, img)
66 } 74 }
67 75
68 fn load_rgb_a_pngs(surface: &mut GlfwSurface, rgb: &Path, alpha: &Path) -> Result<LoadedTexture, TextureLoadError> { 76 fn load_rgb_a_pngs(surface: &mut GlfwSurface, rgb: &Path, alpha: &Path) -> Result<LoadedTexture, TextureLoadError> {
69 let img = open_alpha_png(&alpha)?; 77 let img = open_alpha_png(&alpha)?;
70 let alpha = match img.grayscale() { 78 let alpha = match img.grayscale() {
97 Some(ref filename) => { 105 Some(ref filename) => {
98 let alpha_filename = anm_filename.with_file_name(Path::new(filename).file_name().unwrap()); 106 let alpha_filename = anm_filename.with_file_name(Path::new(filename).file_name().unwrap());
99 load_rgb_a_pngs(&mut surface, &png_filename, &alpha_filename) 107 load_rgb_a_pngs(&mut surface, &png_filename, &alpha_filename)
100 }, 108 },
101 None => { 109 None => {
102 load_rgb_png(&mut surface, &png_filename) 110 load_rgb_texture(&mut surface, &png_filename)
103 } 111 }
104 } 112 }
105 } 113 }