Add is_running and redraw methods

This commit is contained in:
shivshank 2018-07-28 14:18:12 -04:00
parent 0ec51b2969
commit 03f17314ad
2 changed files with 56 additions and 3 deletions

View file

@ -164,6 +164,42 @@ impl Internal {
self.fb.resize_viewport(width, height);
}
pub fn is_running(&mut self) -> bool {
let mut running = true;
let mut resized = None;
self.events_loop.poll_events(|event| {
match event {
Event::WindowEvent { event, .. } => match event {
WindowEvent::CloseRequested => running = false,
WindowEvent::KeyboardInput { input, .. } => {
if let Some(k) = input.virtual_keycode {
if k == VirtualKeyCode::Escape
&& input.state == ElementState::Released {
running = false;
}
}
}
WindowEvent::Resized(logical_size) => {
resized = Some(logical_size);
}
_ => {},
},
_ => {},
}
});
if let Some(size) = resized {
let dpi_factor = self.gl_window.get_hidpi_factor();
let (x, y) = size.to_physical(dpi_factor).into();
self.resize_viewport(x, y);
}
running
}
pub fn redraw(&mut self) {
self.fb.redraw();
self.gl_window.swap_buffers().unwrap();
}
pub fn persist(&mut self) {
self.persist_and_redraw(false);
}
@ -196,9 +232,7 @@ impl Internal {
let dpi_factor = self.gl_window.get_hidpi_factor();
let (x, y) = size.to_physical(dpi_factor).into();
self.resize_viewport(x, y);
self.fb.redraw();
self.gl_window.swap_buffers().unwrap();
self.redraw();
} else {
if redraw {
self.fb.redraw();

View file

@ -169,6 +169,25 @@ impl MiniGlFb {
self.internal.update_buffer(image_data);
}
/// Checks if escape has been pressed or the window has been asked to close.
///
/// This function is a good choice for a while loop condition when you are making a simulation
/// that needs to progress over time but does not need to handle user input.
///
/// Calling this function clears the event queue and also handles resizes for you (if your
/// window is resizable). This does not resize the image buffer; the rendered buffer will
/// instead scale to fit the window.
///
/// Please note that if your window does change size, for buffer to appear scaled it must
/// be redrawn, typically either by calling `redraw` or `update_buffer`.
pub fn is_running(&mut self) -> bool {
self.internal.is_running()
}
pub fn redraw(&mut self) {
self.internal.redraw();
}
/// Changes the format of the image buffer.
///
/// OpenGL will interpret any missing components as 0, except the alpha which it will assume is