Enhance the glutin breakout example

This commit is contained in:
shivshank 2018-07-25 23:31:35 -04:00
parent a027cf0e70
commit 46f2bf4bbc

View file

@ -4,9 +4,11 @@ use mini_gl_fb::{glutin, GlutinBreakout};
use glutin::GlContext; use glutin::GlContext;
use std::cmp::{min, max};
fn main() { fn main() {
let mut fb = mini_gl_fb::gotta_go_fast("Hello world!", 800.0, 600.0); let mut fb = mini_gl_fb::gotta_go_fast("Hello world!", 800.0, 600.0);
let buffer = vec![[128u8, 0, 0, 255]; 800 * 600]; let mut buffer = vec![[128u8, 0, 0, 255]; 800 * 600];
fb.update_buffer(&buffer); fb.update_buffer(&buffer);
let GlutinBreakout { let GlutinBreakout {
@ -16,6 +18,9 @@ fn main() {
} = fb.glutin_breakout(); } = fb.glutin_breakout();
let mut running = true; let mut running = true;
let mut mouse_x = 0;
let mut mouse_y = 0;
let mut mouse_down = false;
while running { while running {
events_loop.poll_events(|event| { events_loop.poll_events(|event| {
use glutin::{Event, ElementState, VirtualKeyCode}; use glutin::{Event, ElementState, VirtualKeyCode};
@ -36,6 +41,23 @@ fn main() {
let dpi_factor = gl_window.get_hidpi_factor(); let dpi_factor = gl_window.get_hidpi_factor();
gl_window.resize(logical_size.to_physical(dpi_factor)); gl_window.resize(logical_size.to_physical(dpi_factor));
} }
Event::WindowEvent { event: CursorMoved { position, .. }, .. } => {
let dpi_factor = gl_window.get_hidpi_factor();
let (x, y) = position.to_physical(dpi_factor).into();
mouse_x = min(max(x, 0), 800 - 1);
mouse_y = min(max(y, 0), 600 - 1);
if mouse_down {
buffer[(mouse_x + mouse_y * 800) as usize] = [64, 128, 255, 255];
fb.update_buffer(&buffer);
}
}
Event::WindowEvent { event: MouseInput { state, .. }, .. } => {
if state == ElementState::Pressed {
mouse_down = true;
} else {
mouse_down = false;
}
}
_ => {} _ => {}
} }
}); });