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