From 0efe514a89f379671879bf069e1e74492654967d Mon Sep 17 00:00:00 2001 From: shivshank Date: Fri, 27 Jul 2018 22:54:52 -0400 Subject: [PATCH] Expose resizable, enhance persist implementation --- examples/resizable.rs | 25 +++++++++++++++++++++++++ src/core.rs | 35 +++++++++++++++++++++++++++++++++-- src/lib.rs | 18 ++++++++++++++++-- 3 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 examples/resizable.rs diff --git a/examples/resizable.rs b/examples/resizable.rs new file mode 100644 index 0000000..d85cbb4 --- /dev/null +++ b/examples/resizable.rs @@ -0,0 +1,25 @@ +extern crate mini_gl_fb; + +use mini_gl_fb::{Config, BufferFormat}; + +fn main() { + let mut fb = mini_gl_fb::get_fancy(Config { + window_title: "Hello world!", + window_size: (800.0, 600.0), + buffer_size: (2, 2), + resizable: true, + .. Default::default() + }); + + + fb.change_buffer_format::(BufferFormat::R); + fb.use_grayscale_shader(); + + let buffer = [128u8, 255, 50, 25]; + fb.update_buffer(&buffer); + + // This can also be configured at creation + // fb.set_resizable(true); + + fb.persist(); +} diff --git a/src/core.rs b/src/core.rs index 23d7728..c366def 100644 --- a/src/core.rs +++ b/src/core.rs @@ -10,13 +10,14 @@ use glutin::{ GlContext, Event, WindowEvent, + VirtualKeyCode, + ElementState, }; use glutin::dpi::LogicalSize; use gl; use gl::types::*; -use std::ptr::null; use std::mem::size_of_val; /// Create a context using glutin given a configuration. @@ -154,6 +155,15 @@ impl Internal { self.gl_window.swap_buffers().unwrap(); } + pub fn set_resizable(&mut self, resizable: bool) { + self.gl_window.set_resizable(resizable); + } + + pub fn resize_viewport(&mut self, width: u32, height: u32) { + self.gl_window.resize((width, height).into()); + self.fb.resize_viewport(width, height); + } + pub fn persist(&mut self) { self.persist_and_redraw(false); } @@ -161,18 +171,39 @@ impl Internal { pub fn persist_and_redraw(&mut self, redraw: bool) { let mut running = true; while running { + let mut new_size = 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) => { + new_size = Some(logical_size); + } _ => {}, }, _ => {}, } }); - if redraw { + if let Some(size) = new_size { + 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(); + } else { + if redraw { + self.fb.redraw(); + self.gl_window.swap_buffers().unwrap(); + } } } } diff --git a/src/lib.rs b/src/lib.rs index ef1c6cf..7ac5f5b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -213,6 +213,8 @@ impl MiniGlFb { /// Set the size of the OpenGL viewport (does not trigger a redraw). /// + /// For high DPI screens this is the physical size of the viewport. + /// /// This does not resize the window or image buffer, only the area to which OpenGL draws. You /// only need to call this function when you are handling events manually and have a resizable /// window. @@ -223,7 +225,19 @@ impl MiniGlFb { self.internal.fb.resize_viewport(width, height); } + /// Set whether or not the window is resizable. + /// + /// Please note that if you are handling events yourself that you need to call + /// `resize_viewport` when the window is resized, otherwise the buffer will only be drawn to + /// a small portion of the window. + pub fn set_resizable(&mut self, resizable: bool) { + self.internal.set_resizable(resizable); + } + /// Keeps the window open until the user closes it. + /// + /// Supports pressing escape to quit. Automatically scales the rendered buffer to the size of + /// the window if the window is resiable (but this does not resize the buffer). pub fn persist(&mut self) { self.internal.persist(); } @@ -231,6 +245,8 @@ impl MiniGlFb { /// `persist` implementation. /// /// When redraw is true, redraws as fast as possible. This function is primarily for debugging. + /// + /// See `persist` method documentation for more info. pub fn persist_and_redraw(&mut self, redraw: bool) { self.internal.persist_and_redraw(redraw); } @@ -246,6 +262,4 @@ impl MiniGlFb { pub fn glutin_breakout(self) -> GlutinBreakout { self.internal.glutin_breakout() } - - // TODO: set_resizable }