♻️ Extract file.rs

main
_ 2020-02-16 23:21:32 +00:00
parent 94f963bca6
commit ad7b52c0bf
2 changed files with 26 additions and 24 deletions

21
src/file.rs Normal file
View File

@ -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 <P> (name: P, max_size: u64) -> Vec <u8>
where P: AsRef <Path>
{
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
}

View File

@ -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 <P> (name: P) -> Vec <u8>
where P: AsRef <Path>
{
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 <V> (rgb: V) -> Vec3
where V: Into <Vec3>
{
@ -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;