From 46f2bf4bbc02ef2aaa0cd38310b892bf96670f0d Mon Sep 17 00:00:00 2001 From: shivshank Date: Wed, 25 Jul 2018 23:31:35 -0400 Subject: [PATCH] Enhance the glutin breakout example --- examples/glutin_breakout.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/examples/glutin_breakout.rs b/examples/glutin_breakout.rs index d4aff19..64fe6c9 100644 --- a/examples/glutin_breakout.rs +++ b/examples/glutin_breakout.rs @@ -4,9 +4,11 @@ use mini_gl_fb::{glutin, GlutinBreakout}; use glutin::GlContext; +use std::cmp::{min, max}; + fn main() { 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); let GlutinBreakout { @@ -16,6 +18,9 @@ fn main() { } = fb.glutin_breakout(); let mut running = true; + let mut mouse_x = 0; + let mut mouse_y = 0; + let mut mouse_down = false; while running { events_loop.poll_events(|event| { use glutin::{Event, ElementState, VirtualKeyCode}; @@ -36,6 +41,23 @@ fn main() { let dpi_factor = gl_window.get_hidpi_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; + } + } _ => {} } });