From 03f17314adb4e966a5019b696930d4dbcb50ffa6 Mon Sep 17 00:00:00 2001 From: shivshank Date: Sat, 28 Jul 2018 14:18:12 -0400 Subject: [PATCH] Add is_running and redraw methods --- src/core.rs | 40 +++++++++++++++++++++++++++++++++++++--- src/lib.rs | 19 +++++++++++++++++++ 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/src/core.rs b/src/core.rs index 8b8c059..ad2f3c7 100644 --- a/src/core.rs +++ b/src/core.rs @@ -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(); diff --git a/src/lib.rs b/src/lib.rs index 7ac5f5b..cd4baae 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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