mirror of
https://github.com/italicsjenga/mini_gl_fb.git
synced 2024-11-22 15:31:31 +11:00
Refactor init_framebuffer to take arguments instead of a config
This commit is contained in:
parent
16e1c25e31
commit
f23f653413
|
@ -11,8 +11,6 @@
|
|||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// To streamline this pattern and save you imports, see the `get_fancy!` macro.
|
||||
///
|
||||
/// If there's a config option you want to see or think is missing, please open an issue!
|
||||
pub struct Config<S: ToString> {
|
||||
/// Sets the scale of the buffer. The buffer will automatically scale to the size of the
|
||||
|
|
23
src/core.rs
23
src/core.rs
|
@ -43,13 +43,18 @@ pub fn init_glutin_context<S: ToString>(config: &Config<S>) -> (EventsLoop, GlWi
|
|||
type VertexFormat = buffer_layout!([f32; 2], [f32; 2]);
|
||||
|
||||
/// Create the OpenGL resources needed for drawing to a buffer.
|
||||
pub fn init_framebuffer<S: ToString>(config: &Config<S>) -> Framebuffer {
|
||||
pub fn init_framebuffer(
|
||||
buffer_width: u32,
|
||||
buffer_height: u32,
|
||||
viewport_width: u32,
|
||||
viewport_height: u32
|
||||
) -> Framebuffer {
|
||||
// The config takes the size in u32 because that's all that actually makes sense but since
|
||||
// OpenGL is from the Land of C where a Working Type System doesn't exist, we work with i32s
|
||||
let buffer_width = if config.buffer_size.0 == 0 { config.window_size.0.round() as _ }
|
||||
else { config.buffer_size.0 as _ };
|
||||
let buffer_height = if config.buffer_size.1 == 0 { config.window_size.1.round() as _ }
|
||||
else { config.buffer_size.1 as _ };
|
||||
let buffer_width = buffer_width as i32;
|
||||
let buffer_height = buffer_height as i32;
|
||||
let vp_width = viewport_width as i32;
|
||||
let vp_height = viewport_height as i32;
|
||||
|
||||
let vertex_shader = rustic_gl::raw::create_shader(
|
||||
gl::VERTEX_SHADER,
|
||||
|
@ -105,10 +110,10 @@ pub fn init_framebuffer<S: ToString>(config: &Config<S>) -> Framebuffer {
|
|||
}
|
||||
|
||||
Framebuffer {
|
||||
buffer_width: buffer_width,
|
||||
buffer_height: buffer_height,
|
||||
vp_width: config.window_size.0 as _,
|
||||
vp_height: config.window_size.1 as _,
|
||||
buffer_width,
|
||||
buffer_height,
|
||||
vp_width,
|
||||
vp_height,
|
||||
program,
|
||||
sampler_location,
|
||||
vertex_shader: Some(vertex_shader),
|
||||
|
|
59
src/lib.rs
59
src/lib.rs
|
@ -76,46 +76,6 @@ pub use breakout::GlutinBreakout;
|
|||
pub use config::Config;
|
||||
pub use core::{Internal, BufferFormat};
|
||||
|
||||
/*
|
||||
// TODO: Support mixed { prop, prop: value, .. } for creating configs through the macro
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! get_fancy {
|
||||
(
|
||||
$($setting:ident: $setting_value:expr),*
|
||||
) => {
|
||||
// Support both no trailing comma and trailing comma
|
||||
// (The core macro impl assumes trailing comma)
|
||||
get_fancy!($($setting: $setting_value),*,)
|
||||
};
|
||||
|
||||
(
|
||||
$($setting:ident),*
|
||||
) => {
|
||||
// Support both no trailing comma and trailing comma
|
||||
// (The core macro impl assumes trailing comma)
|
||||
get_fancy!($($setting),*,)
|
||||
};
|
||||
|
||||
(
|
||||
$($setting:ident),*,
|
||||
) => {
|
||||
get_fancy!($($setting: $setting),*,)
|
||||
};
|
||||
|
||||
(
|
||||
$($setting:ident: $setting_value:expr),*,
|
||||
) => {{
|
||||
let config = $crate::Config {
|
||||
$(
|
||||
$setting: $setting_value
|
||||
),*,
|
||||
.. Default::default()
|
||||
};
|
||||
$crate::get_fancy(config)
|
||||
}};
|
||||
}*/
|
||||
|
||||
/// Creates a non resizable window and framebuffer with a given size in pixels.
|
||||
///
|
||||
/// Please note that the window size is in logical device pixels, so on a high DPI monitor the
|
||||
|
@ -145,8 +105,25 @@ pub fn gotta_go_fast<S: ToString>(
|
|||
/// glutin or in this library, this function exists as a possible work around (or in case for some
|
||||
/// reason everything must be absolutely correct at window creation)
|
||||
pub fn get_fancy<S: ToString>(config: Config<S>) -> MiniGlFb {
|
||||
let buffer_width = if config.buffer_size.0 == 0 { config.window_size.0.round() as _ }
|
||||
else { config.buffer_size.0 };
|
||||
let buffer_height = if config.buffer_size.1 == 0 { config.window_size.1.round() as _ }
|
||||
else { config.buffer_size.1 };
|
||||
|
||||
let (events_loop, gl_window) = core::init_glutin_context(&config);
|
||||
let fb = core::init_framebuffer(&config);
|
||||
|
||||
let dpi_factor = gl_window.get_hidpi_factor();
|
||||
let (vp_width, vp_height) = gl_window.get_inner_size()
|
||||
.unwrap()
|
||||
.to_physical(dpi_factor)
|
||||
.into();
|
||||
|
||||
let fb = core::init_framebuffer(
|
||||
buffer_width,
|
||||
buffer_height,
|
||||
vp_width,
|
||||
vp_height,
|
||||
);
|
||||
|
||||
MiniGlFb {
|
||||
internal: Internal {
|
||||
|
|
Loading…
Reference in a new issue