From ad7b52c0bf3653bdc8a37a48a4b441e00e73e49d Mon Sep 17 00:00:00 2001 From: _ <> Date: Sun, 16 Feb 2020 23:21:32 +0000 Subject: [PATCH] :recycle: Extract file.rs --- src/file.rs | 21 +++++++++++++++++++++ src/main.rs | 29 +++++------------------------ 2 files changed, 26 insertions(+), 24 deletions(-) create mode 100644 src/file.rs diff --git a/src/file.rs b/src/file.rs new file mode 100644 index 0000000..157fc85 --- /dev/null +++ b/src/file.rs @@ -0,0 +1,21 @@ +use std::convert::TryInto; +use std::fs::File; +use std::io::Read; +use std::path::Path; + +pub fn load_small_file

(name: P, max_size: u64) -> Vec +where P: AsRef +{ + let mut f = File::open (name).unwrap (); + let len = f.metadata ().unwrap ().len (); + + if len > max_size { + panic! ("File is too big"); + } + + let mut data = vec! [0u8; len.try_into ().unwrap ()]; + + f.read_exact (&mut data [..]).unwrap (); + + data +} diff --git a/src/main.rs b/src/main.rs index 949fc1d..25f1dd3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,40 +3,21 @@ use glam::{Mat4, Vec3, Vec4}; use sdl2::event::Event; use sdl2::keyboard::{Keycode, Scancode}; use std::collections::HashMap; -use std::convert::TryInto; -use std::ffi::{c_void, CString}; -use std::fs::File; -use std::io::Read; -use std::path::Path; +use std::ffi::{c_void}; use std::time::{Duration}; +mod file; mod iqm; mod shader; mod texture; mod timestep; +use file::load_small_file; use iqm::Model; use shader::{ShaderProgram, ShaderObject}; use texture::Texture; use timestep::TimeStep; -pub fn load_small_file

(name: P) -> Vec -where P: AsRef -{ - let mut f = File::open (name).unwrap (); - let len = f.metadata ().unwrap ().len (); - - if len > 1024 * 1024 { - panic! ("File is too big"); - } - - let mut data = vec! [0u8; len.try_into ().unwrap ()]; - - f.read_exact (&mut data [..]).unwrap (); - - data -} - pub fn color_from_255 (rgb: V) -> Vec3 where V: Into { @@ -256,10 +237,10 @@ fn main () { let texture = Texture::from_file ("sky.png"); texture.bind (); - let model_data = load_small_file ("pumpking.iqm"); + let model_data = load_small_file ("pumpking.iqm", 1024 * 1024); let model = Model::from_slice (&model_data [..]); - let sky_data = load_small_file ("sky-sphere.iqm"); + let sky_data = load_small_file ("sky-sphere.iqm", 1024 * 1024); let sky_model = Model::from_slice (&sky_data [..]); const FALSE_U8: u8 = 0;