mirror of
https://github.com/italicsjenga/mini_gl_fb.git
synced 2024-11-22 07:21:30 +11:00
Expose resizable, enhance persist implementation
This commit is contained in:
parent
aa46df7f74
commit
0efe514a89
25
examples/resizable.rs
Normal file
25
examples/resizable.rs
Normal file
|
@ -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::<u8>(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();
|
||||
}
|
35
src/core.rs
35
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
18
src/lib.rs
18
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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue