Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 93 additions & 49 deletions bracket-terminal/src/hal/native/init.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::BACKEND;
use crate::BResult;
use crate::hal::native::{WrappedContext, shader_strings};
use crate::hal::native::{NativeInitSettings, WrappedContext, shader_strings};
use crate::hal::scaler::ScreenScaler;
use crate::hal::{Framebuffer, Shader, setup_quad};
use crate::prelude::{BACKEND_INTERNAL, BTerm, InitHints};
Expand All @@ -16,28 +16,99 @@ use std::ffi::CString;
use std::num::NonZeroU32;
use winit::{
dpi::LogicalSize,
event_loop::EventLoop,
event_loop::{ActiveEventLoop, EventLoop},
raw_window_handle::{HasDisplayHandle, HasWindowHandle},
window::{Fullscreen, WindowAttributes},
window::{Fullscreen, Window, WindowAttributes},
};

#[allow(deprecated)]
pub struct NativeRuntime {
pub window: Window,
pub gl_context: glutin::context::PossiblyCurrentContext,
pub gl_surface: glutin::surface::Surface<WindowSurface>,
}

pub fn init_raw<S: ToString>(
width_pixels: u32,
height_pixels: u32,
window_title: S,
platform_hints: InitHints,
) -> BResult<BTerm> {
let mut scaler = ScreenScaler::new(platform_hints.desired_gutter, width_pixels, height_pixels);
let el = EventLoop::new()?;
let frame_sleep_time = crate::hal::convert_fps_to_wait(platform_hints.frame_sleep_time);
let resize_scaling = platform_hints.resize_scaling;
let scaler = ScreenScaler::new(platform_hints.desired_gutter, width_pixels, height_pixels);
{
let mut be = BACKEND.lock();
be.context_wrapper = Some(WrappedContext {
el,
init: NativeInitSettings {
width_pixels,
height_pixels,
window_title: window_title.to_string(),
platform_hints,
},
});
be.frame_sleep_time = frame_sleep_time;
be.resize_scaling = resize_scaling;
be.screen_scaler = scaler;
}

let bterm = BTerm {
width_pixels,
height_pixels,
original_width_pixels: width_pixels,
original_height_pixels: height_pixels,
fps: 0.0,
frame_time_ms: 0.0,
active_console: 0,
key: None,
mouse_pos: (0, 0),
left_click: false,
shift: false,
control: false,
alt: false,
web_button: None,
quitting: false,
post_scanlines: false,
post_screenburn: false,
screen_burn_color: bracket_color::prelude::RGB::from_f32(0.0, 1.0, 1.0),
mouse_visible: true,
};
Ok(bterm)
}

pub(super) fn init_runtime(
event_loop: &ActiveEventLoop,
init: NativeInitSettings,
) -> BResult<NativeRuntime> {
let NativeInitSettings {
width_pixels,
height_pixels,
window_title,
platform_hints,
} = init;
let InitHints {
vsync,
fullscreen,
gl_version,
gl_profile,
hardware_acceleration,
srgb,
frame_sleep_time,
resize_scaling,
desired_gutter,
fitscreen,
} = platform_hints;

let mut scaler = ScreenScaler::new(desired_gutter, width_pixels, height_pixels);
let window_size = scaler.new_window_size();
let window_size = LogicalSize::new(window_size.width, window_size.height);
let window_attributes = WindowAttributes::default()
.with_title(window_title.to_string())
.with_resizable(platform_hints.fitscreen)
.with_title(window_title)
.with_resizable(fitscreen)
.with_min_inner_size(window_size)
.with_inner_size(window_size);
let window = el.create_window(window_attributes)?;
let window = event_loop.create_window(window_attributes)?;

let raw_display = window.display_handle()?.as_raw();
#[cfg(target_os = "macos")]
Expand All @@ -53,7 +124,7 @@ pub fn init_raw<S: ToString>(
.with_alpha_size(8)
.with_transparency(false)
.with_surface_type(ConfigSurfaceTypes::WINDOW);
if platform_hints.hardware_acceleration {
if hardware_acceleration {
template_builder = template_builder.prefer_hardware_accelerated(Some(true));
}

Expand All @@ -72,8 +143,8 @@ pub fn init_raw<S: ToString>(
};

let context_attributes = ContextAttributesBuilder::new()
.with_profile(platform_hints.gl_profile)
.with_context_api(platform_hints.gl_version)
.with_profile(gl_profile)
.with_context_api(gl_version)
.build(Some(raw_window_handle));
let not_current_gl_context =
unsafe { gl_display.create_context(&config, &context_attributes)? };
Expand All @@ -82,20 +153,20 @@ pub fn init_raw<S: ToString>(
let width = NonZeroU32::new(physical_size.width.max(1)).unwrap();
let height = NonZeroU32::new(physical_size.height.max(1)).unwrap();
let attrs = SurfaceAttributesBuilder::<WindowSurface>::new()
.with_srgb(Some(platform_hints.srgb))
.with_srgb(Some(srgb))
.build(raw_window_handle, width, height);
let gl_surface = unsafe { gl_display.create_window_surface(&config, &attrs)? };
let gl_context = not_current_gl_context.make_current(&gl_surface)?;

if platform_hints.vsync {
if vsync {
let _ = gl_surface
.set_swap_interval(&gl_context, SwapInterval::Wait(NonZeroU32::new(1).unwrap()));
} else {
let _ = gl_surface.set_swap_interval(&gl_context, SwapInterval::DontWait);
}

if platform_hints.fullscreen {
if let Some(mh) = window.available_monitors().next() {
if fullscreen {
if let Some(mh) = event_loop.available_monitors().next() {
window.set_fullscreen(Some(Fullscreen::Borderless(Some(mh))));
} else {
return Err("No available monitor found".into());
Expand All @@ -119,7 +190,6 @@ pub fn init_raw<S: ToString>(
);
}

// Load our basic shaders
let shaders: Vec<Shader> = vec![
Shader::new(
&gl,
Expand Down Expand Up @@ -149,54 +219,28 @@ pub fn init_raw<S: ToString>(
),
];

// Build the backing frame-buffer
let initial_dpi_factor = window.scale_factor();
scaler.change_logical_size(width_pixels, height_pixels, initial_dpi_factor as f32);
let backing_fbo = Framebuffer::build_fbo(
&gl,
scaler.logical_size.0 as i32,
scaler.logical_size.1 as i32,
)?;

// Build a simple quad rendering VAO
let quad_vao = setup_quad(&gl);

let mut be = BACKEND.lock();
be.gl = Some(gl);
be.quad_vao = Some(quad_vao);
be.context_wrapper = Some(WrappedContext {
el,
window,
gl_context,
gl_surface,
});
be.backing_buffer = Some(backing_fbo);
be.frame_sleep_time = crate::hal::convert_fps_to_wait(platform_hints.frame_sleep_time);
be.resize_scaling = platform_hints.resize_scaling;
be.frame_sleep_time = crate::hal::convert_fps_to_wait(frame_sleep_time);
be.resize_scaling = resize_scaling;
be.screen_scaler = scaler;

BACKEND_INTERNAL.lock().shaders = shaders;

let bterm = BTerm {
width_pixels,
height_pixels,
original_width_pixels: width_pixels,
original_height_pixels: height_pixels,
fps: 0.0,
frame_time_ms: 0.0,
active_console: 0,
key: None,
mouse_pos: (0, 0),
left_click: false,
shift: false,
control: false,
alt: false,
web_button: None,
quitting: false,
post_scanlines: false,
post_screenburn: false,
screen_burn_color: bracket_color::prelude::RGB::from_f32(0.0, 1.0, 1.0),
mouse_visible: true,
};
Ok(bterm)
Ok(NativeRuntime {
window,
gl_context,
gl_surface,
})
}
Loading
Loading