Skip to content

Commit

Permalink
Run rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Entscheider committed Jun 24, 2020
1 parent 2b5126e commit 4224a8d
Show file tree
Hide file tree
Showing 6 changed files with 336 additions and 269 deletions.
114 changes: 61 additions & 53 deletions examples/glwinhelp/imgwin.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use image;
use glium;
use glium::{Surface};
use glium::index::PrimitiveType;
use std::time::{Instant, Duration};
use glium::Surface;
use image;
use std::time::{Duration, Instant};

use glium::{implement_vertex, program, uniform};
use glium::glutin::ContextBuilder;
use glium::glutin::window::WindowBuilder;
use glium::texture::{RawImage2d, CompressedSrgbTexture2d};
use glium::glutin::event::{Event, WindowEvent, ElementState, StartCause, VirtualKeyCode};
use glium::glutin::event::{ElementState, Event, StartCause, VirtualKeyCode, WindowEvent};
use glium::glutin::event_loop::{ControlFlow, EventLoop};
use glium::glutin::window::WindowBuilder;
use glium::glutin::ContextBuilder;
use glium::texture::{CompressedSrgbTexture2d, RawImage2d};
use glium::{implement_vertex, program, uniform};

#[derive(Copy,Clone)]
#[derive(Copy, Clone)]
struct Vertex {
position: [f32; 2],
tex_coords: [f32; 2],
Expand All @@ -20,14 +20,14 @@ struct Vertex {
implement_vertex!(Vertex, position, tex_coords);

/// An Application creates windows and runs a main loop
pub struct Application{
pub struct Application {
main_loop: EventLoop<()>,
}

impl Application {
pub fn new() -> Application {
Application {
main_loop: EventLoop::new()
main_loop: EventLoop::new(),
}
}

Expand All @@ -39,31 +39,33 @@ impl Application {
/// and `handler.next_frame` is called `fps` times per seconds.
/// Whenever `handler.should_exit` turns true, the program exit.
pub fn run<T: MainloopHandler + 'static>(self, mut handler: T, fps: u32) -> ! {
self.main_loop.run(move |event, _, control_flow|{
self.main_loop.run(move |event, _, control_flow| {
let now = Instant::now();
match event {
Event::WindowEvent { event: win_event, .. } => {
match win_event {
WindowEvent::CloseRequested => {
handler.close_event();
},
WindowEvent::KeyboardInput { input, .. } if input.state == ElementState::Pressed => {
handler.key_event(input.virtual_keycode)
},
_ => ()
Event::WindowEvent {
event: win_event, ..
} => match win_event {
WindowEvent::CloseRequested => {
handler.close_event();
}
WindowEvent::KeyboardInput { input, .. }
if input.state == ElementState::Pressed =>
{
handler.key_event(input.virtual_keycode)
}
_ => (),
},
Event::NewEvents(StartCause::ResumeTimeReached {..}) | Event::NewEvents(StartCause::Init) => {
handler.next_frame()
}
_ => ()
Event::NewEvents(StartCause::ResumeTimeReached { .. })
| Event::NewEvents(StartCause::Init) => handler.next_frame(),
_ => (),
}

if handler.should_exit() {
*control_flow = ControlFlow::Exit;
handler.on_exit();
} else {
*control_flow = ControlFlow::WaitUntil(now + Duration::from_secs_f32(1f32 / fps as f32));
*control_flow =
ControlFlow::WaitUntil(now + Duration::from_secs_f32(1f32 / fps as f32));
}
});
}
Expand Down Expand Up @@ -96,30 +98,33 @@ pub trait MainloopHandler {

impl ImgWindow {
fn new<T: Into<String>>(title: T, main_loop: &EventLoop<()>) -> ImgWindow {
let wb =
WindowBuilder::new().with_title(title.into());
let cb= ContextBuilder::new().with_vsync(true);
let wb = WindowBuilder::new().with_title(title.into());
let cb = ContextBuilder::new().with_vsync(true);
let display = glium::Display::new(wb, cb, &main_loop).unwrap();

// vertex for a rect for drawing an image to the whole window
let vertex_buffer = glium::VertexBuffer::new(&display,
&[Vertex {
position: [-1.0, -1.0],
tex_coords: [0.0, 0.0],
},
Vertex {
position: [-1.0, 1.0],
tex_coords: [0.0, 1.0],
},
Vertex {
position: [1.0, 1.0],
tex_coords: [1.0, 1.0],
},
Vertex {
position: [1.0, -1.0],
tex_coords: [1.0, 0.0],
}])
.unwrap();
let vertex_buffer = glium::VertexBuffer::new(
&display,
&[
Vertex {
position: [-1.0, -1.0],
tex_coords: [0.0, 0.0],
},
Vertex {
position: [-1.0, 1.0],
tex_coords: [0.0, 1.0],
},
Vertex {
position: [1.0, 1.0],
tex_coords: [1.0, 1.0],
},
Vertex {
position: [1.0, -1.0],
tex_coords: [1.0, 0.0],
},
],
)
.unwrap();
let index_buffer =
glium::IndexBuffer::new(&display, PrimitiveType::TriangleStrip, &[1 as u16, 2, 0, 3])
.unwrap();
Expand Down Expand Up @@ -149,7 +154,7 @@ impl ImgWindow {
}
"
},)
.unwrap();
.unwrap();

ImgWindow {
texture: None,
Expand Down Expand Up @@ -182,11 +187,14 @@ impl ImgWindow {
],
tex: texture
};
target.draw(&self.vertex_buffer,
&self.index_buffer,
&self.program,
&uniforms,
&Default::default())
target
.draw(
&self.vertex_buffer,
&self.index_buffer,
&self.program,
&uniforms,
&Default::default(),
)
.unwrap();
}
target.finish().unwrap();
Expand Down
1 change: 0 additions & 1 deletion examples/glwinhelp/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

pub mod imgwin;

pub use glium::glutin::event::VirtualKeyCode;
40 changes: 24 additions & 16 deletions examples/kinect_live.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ mod glwinhelp;

use glwinhelp::{imgwin, VirtualKeyCode};

use crate::glwinhelp::imgwin::ImgWindow;
use freenectrs::freenect;
use freenectrs::freenect::{
FreenectContext, FreenectDepthStream, FreenectError, FreenectVideoStream,
};
use std::error::Error;
use freenectrs::freenect::{FreenectError, FreenectVideoStream, FreenectDepthStream, FreenectContext};
use crate::glwinhelp::imgwin::ImgWindow;

#[inline]
fn depth_to_img(data: &[u16]) -> image::RgbaImage {
Expand All @@ -14,8 +16,12 @@ fn depth_to_img(data: &[u16]) -> image::RgbaImage {
// the depth value for the current pixel
let depth_value = data[idx as usize];

// we start at value of 600 for depth
let depth_value = if depth_value > 600 { depth_value - 600 } else { 0 };
// we start at a value of 600 for depth
let depth_value = if depth_value > 600 {
depth_value - 600
} else {
0
};
// scale the value down
let depth_value = depth_value / 2;
// and use this value as a gray value by clipping everything above the maximal
Expand All @@ -26,8 +32,7 @@ fn depth_to_img(data: &[u16]) -> image::RgbaImage {
})
}

pub fn main() -> Result<(), Box<dyn Error>>{

pub fn main() -> Result<(), Box<dyn Error>> {
// we init the device with support for depth, video and motor
let ctx = Box::new(freenect::FreenectContext::init_with_video_motor()?);
// We create a 'static lifetime by leaking. Doing so we can fulfill the requirement of glium's
Expand All @@ -49,10 +54,14 @@ pub fn main() -> Result<(), Box<dyn Error>>{
// mainloop run (called in Application::run)
let device = Box::leak(device);
// init the depth and video mode
device.set_depth_mode(freenect::FreenectResolution::Medium,
freenect::FreenectDepthFormat::MM)?;
device.set_video_mode(freenect::FreenectResolution::Medium,
freenect::FreenectVideoFormat::Rgb)?;
device.set_depth_mode(
freenect::FreenectResolution::Medium,
freenect::FreenectDepthFormat::MM,
)?;
device.set_video_mode(
freenect::FreenectResolution::Medium,
freenect::FreenectVideoFormat::Rgb,
)?;

// creating the gui elements
let app = imgwin::Application::new();
Expand Down Expand Up @@ -80,7 +89,7 @@ pub fn main() -> Result<(), Box<dyn Error>>{
vimg,
dwin,
vwin,
ctx
ctx,
};
// run the gui main loop
app.run(input_handler, 25);
Expand All @@ -105,11 +114,10 @@ struct InputHandler<'a, 'b: 'a> {
/// the window on which we draw the rgb image
vwin: ImgWindow,
/// the freenect context so we can stop its main loop
ctx: &'a FreenectContext
ctx: &'a FreenectContext,
}

impl<'a, 'b> imgwin::MainloopHandler for InputHandler<'a, 'b> {

fn close_event(&mut self) {
self.is_closed = true;
}
Expand Down Expand Up @@ -143,12 +151,12 @@ impl<'a, 'b> imgwin::MainloopHandler for InputHandler<'a, 'b> {
}

fn next_frame(&mut self) {
// get and render the depth bytes to a image
// get and render the depth bytes to an image
if let Ok((data, _ /* timestamp */)) = self.dstream.receiver.try_recv() {
self.dimg = depth_to_img(&*data);
}

// get and create an image from the rgb byzes
// get and create an image from the rgb bytes
if let Ok((data, _ /* timestamp */)) = self.vstream.receiver.try_recv() {
self.vimg = image::RgbaImage::from_fn(640, 480, |x, y| {
let idx = 3 * (y * 640 + x) as usize;
Expand All @@ -157,7 +165,7 @@ impl<'a, 'b> imgwin::MainloopHandler for InputHandler<'a, 'b> {
});
}

// and draw it to window
// and draw it to the windows
let dimg = self.dimg.clone();
self.dwin.set_img(dimg);
let vimg = self.vimg.clone();
Expand Down
Loading

0 comments on commit 4224a8d

Please sign in to comment.