mini_gl_fb/examples/glutin_breakout.rs

64 lines
2.4 KiB
Rust
Raw Normal View History

2018-07-26 13:17:33 +10:00
extern crate mini_gl_fb;
extern crate glutin;
2018-07-26 13:17:33 +10:00
2018-07-26 13:31:35 +10:00
use std::cmp::{min, max};
use mini_gl_fb::GlutinBreakout;
use mini_gl_fb::glutin::event::WindowEvent::KeyboardInput;
use mini_gl_fb::glutin::event::{Event, VirtualKeyCode, ElementState, WindowEvent};
use mini_gl_fb::glutin::event_loop::ControlFlow;
2018-07-26 13:31:35 +10:00
2018-07-26 13:17:33 +10:00
fn main() {
let (event_loop, mut fb) = mini_gl_fb::gotta_go_fast("Hello world!", 800.0, 600.0);
2018-07-26 13:31:35 +10:00
let mut buffer = vec![[128u8, 0, 0, 255]; 800 * 600];
2018-07-26 13:17:33 +10:00
fb.update_buffer(&buffer);
let GlutinBreakout {
context,
2018-07-26 13:17:33 +10:00
mut fb,
} = fb.glutin_breakout();
2018-07-26 13:31:35 +10:00
let mut mouse_down = false;
2018-07-26 13:17:33 +10:00
event_loop.run(move |event, _, flow| {
match event {
Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => {
*flow = ControlFlow::Exit;
}
Event::WindowEvent { event: KeyboardInput { input, .. }, .. } => {
if let Some(k) = input.virtual_keycode {
if k == VirtualKeyCode::Escape && input.state == ElementState::Pressed {
*flow = ControlFlow::Exit;
2018-07-26 13:17:33 +10:00
}
}
}
Event::WindowEvent { event: WindowEvent::Resized(size), .. } => {
context.resize(size);
context.window().request_redraw();
}
Event::WindowEvent { event: WindowEvent::CursorMoved { position, .. }, .. } => {
let (x, y) = position.to_logical::<f64>(context.window().scale_factor()).into();
println!("({}, {})", x, y);
let mouse_x = min(max(x, 0), 800 - 1);
let mouse_y = min(max(fb.buffer_size.height - y, 0), 600 - 1);
if mouse_down {
buffer[(mouse_x + mouse_y * 800) as usize] = [64, 128, 255, 255];
fb.update_buffer(&buffer);
context.window().request_redraw();
2018-07-26 13:31:35 +10:00
}
}
Event::WindowEvent { event: WindowEvent::MouseInput { state, .. }, .. } => {
if state == ElementState::Pressed {
mouse_down = true;
} else {
mouse_down = false;
2018-07-26 13:31:35 +10:00
}
2018-07-26 13:17:33 +10:00
}
Event::RedrawRequested(_) => {
fb.redraw();
context.swap_buffers().unwrap();
}
_ => {}
}
});
2018-07-26 13:17:33 +10:00
}