comparison examples/anmrenderer.rs @ 700:ccb739c5b66c

examples: factorise file reading into a buffer.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 23 Aug 2019 13:05:48 +0200
parents 6be3320a1cb3
children b6c351ca0a35
comparison
equal deleted inserted replaced
699:2a60f12bd5bd 700:ccb739c5b66c
85 85
86 #[uniform(name = "mvp")] 86 #[uniform(name = "mvp")]
87 mvp: Uniform<[[f32; 4]; 4]>, 87 mvp: Uniform<[[f32; 4]; 4]>,
88 } 88 }
89 89
90 fn load_file_into_vec(filename: &str) -> Vec<u8> {
91 let file = File::open(filename).unwrap();
92 let mut file = BufReader::new(file);
93 let mut buf = vec![];
94 file.read_to_end(&mut buf).unwrap();
95 buf
96 }
97
90 fn main() { 98 fn main() {
91 // Parse arguments. 99 // Parse arguments.
92 let args: Vec<_> = env::args().collect(); 100 let args: Vec<_> = env::args().collect();
93 if args.len() != 4 { 101 if args.len() != 4 {
94 eprintln!("Usage: {} <ANM file> <PNG file> <script number>", args[0]); 102 eprintln!("Usage: {} <ANM file> <PNG file> <script number>", args[0]);
97 let anm_filename = &args[1]; 105 let anm_filename = &args[1];
98 let png_filename = &args[2]; 106 let png_filename = &args[2];
99 let script: u8 = args[3].parse().expect("number"); 107 let script: u8 = args[3].parse().expect("number");
100 108
101 // Open the ANM file. 109 // Open the ANM file.
102 let file = File::open(anm_filename).unwrap(); 110 let buf = load_file_into_vec(anm_filename);
103 let mut file = BufReader::new(file);
104 let mut buf = vec![];
105 file.read_to_end(&mut buf).unwrap();
106 let anm0 = Anm0::from_slice(&buf).unwrap(); 111 let anm0 = Anm0::from_slice(&buf).unwrap();
107 112
108 if !anm0.scripts.contains_key(&script) { 113 if !anm0.scripts.contains_key(&script) {
109 eprintln!("This anm0 doesn’t contain a script named {}.", script); 114 eprintln!("This anm0 doesn’t contain a script named {}.", script);
110 return; 115 return;