7 use std::time::{Duration, Instant};
8 use glium::{self, Display};
9 use glium::vertex::VertexBufferAny;
18 pub fn start_loop<F>(mut callback: F) where F: FnMut() -> Action {
19 let mut accumulator = Duration::new(0, 0);
20 let mut previous_clock = Instant::now();
24 Action::Stop => break,
25 Action::Continue => ()
28 let now = Instant::now();
29 accumulator += now - previous_clock;
32 let fixed_time_stamp = Duration::new(0, 16666667);
33 while accumulator >= fixed_time_stamp {
34 accumulator -= fixed_time_stamp;
36 // if you have a game, update the state here
39 thread::sleep(fixed_time_stamp - accumulator);
43 /// Returns a vertex buffer that should be rendered as `TrianglesList`.
44 pub fn load_wavefront(display: &Display, data: &[u8]) -> VertexBufferAny {
45 #[derive(Copy, Clone)]
52 implement_vertex!(Vertex, position, normal, texture);
54 let mut data = ::std::io::BufReader::new(data);
55 let data = obj::Obj::load(&mut data);
57 let mut vertex_data = Vec::new();
59 for object in data.object_iter() {
60 for shape in object.group_iter().flat_map(|g| g.indices().iter()) {
62 &genmesh::Polygon::PolyTri(genmesh::Triangle { x: v1, y: v2, z: v3 }) => {
63 for v in [v1, v2, v3].iter() {
64 let position = data.position()[v.0];
65 let texture = v.1.map(|index| data.texture()[index]);
66 let normal = v.2.map(|index| data.normal()[index]);
68 let texture = texture.unwrap_or([0.0, 0.0]);
69 let normal = normal.unwrap_or([0.0, 0.0, 0.0]);
71 vertex_data.push(Vertex {
83 glium::vertex::VertexBuffer::new(display, &vertex_data).unwrap().into_vertex_buffer_any()