Skip to content

Commit

Permalink
Native only: support resizing terminals with window resize by user (u…
Browse files Browse the repository at this point in the history
…ser pulls window). Part of #100
  • Loading branch information
thebracket committed Mar 9, 2020
1 parent 1f852b1 commit d5f507d
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 1 deletion.
5 changes: 5 additions & 0 deletions bracket-terminal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,9 @@ harness = false
[[example]]
name = "native_gl"
path = "examples/native_gl.rs"
features = [ "opengl" ]

[[example]]
name = "bench_scalable"
path = "examples/bench_scalable.rs"
features = [ "opengl" ]
65 changes: 65 additions & 0 deletions bracket-terminal/examples/bench_scalable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// This example helps checking the performances of differents
// backends and also performance regression from one version
// to another.
// The goal is to test a pathological case (every console cell
// updated every frame) using the fastest game state as possible
// to be as close as possible as measuring only rendering time
//////////////////////////////////////////////////////////////

use bracket_random::prelude::*;
use bracket_terminal::prelude::*;
bracket_terminal::add_wasm_support!();

struct State {
rng: RandomNumberGenerator,
}

impl State {
pub fn new() -> Self {
Self {
rng: RandomNumberGenerator::new(),
}
}
}

impl GameState for State {
fn tick(&mut self, ctx: &mut BTerm) {
let console_size = ctx.get_char_size();
for y in 0..console_size.1 as i32 {
for x in 0..console_size.0 as i32 {
let val = self.rng.rand::<u64>();
let back = RGB::from_u8(
(val & 0xFF) as u8,
((val >> 8) & 0x5F) as u8,
((val >> 16) & 0x3F) as u8,
);
let fore = RGB::from_u8(
((val >> 16) & 0xFF) as u8,
((val >> 24) & 0xFF) as u8,
((val >> 32) & 0xFF) as u8,
);
let ascii = ((val >> 40) & 0xFF) as u8;
ctx.set(x, y, fore, back, ascii);
}
}
ctx.draw_box(
30,
20,
20,
5,
RGB::from_u8(255, 255, 255),
RGB::from_u8(0, 0, 0),
);
ctx.print(35, 22, &format!("{} fps", ctx.fps as u32));
}
}

fn main() -> BError {
let context = BTermBuilder::simple(80, 45)
.unwrap()
.with_title("bracket-lib benchmark")
.with_vsync(false)
.with_resize_scaling(true)
.build()?;
main_loop(context, State::new())
}
1 change: 1 addition & 0 deletions bracket-terminal/src/hal/native/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ pub fn init_raw<S: ToString>(
});
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;

BACKEND_INTERNAL.lock().unwrap().shaders = shaders;

Expand Down
12 changes: 12 additions & 0 deletions bracket-terminal/src/hal/native/mainloop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ fn on_resize(bterm: &mut BTerm, physical_size: glutin::dpi::PhysicalSize<u32>) -
bterm.on_event(BEvent::Resized {
new_size: Point::new(physical_size.width, physical_size.height),
});

let mut bit = BACKEND_INTERNAL.lock().unwrap();
if be.resize_scaling {
let num_consoles = bit.consoles.len();
for i in 0..num_consoles {
let font_size = bit.fonts[bit.consoles[i].font_index].tile_size;
let chr_w = physical_size.width / font_size.0;
let chr_h = physical_size.height / font_size.1;
bit.consoles[i].console.set_char_size(chr_w, chr_h);
}
}

Ok(())
}

Expand Down
7 changes: 6 additions & 1 deletion bracket-terminal/src/hal/native/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ lazy_static! {
context_wrapper: None,
backing_buffer: None,
frame_sleep_time: None,
gl_callback: None
gl_callback: None,
resize_scaling: false
});
}

Expand All @@ -45,6 +46,7 @@ pub struct PlatformGL {
pub backing_buffer: Option<super::Framebuffer>,
pub frame_sleep_time: Option<u64>,
pub gl_callback: Option<GlCallback>,
pub resize_scaling: bool,
}

unsafe impl Send for PlatformGL {}
Expand All @@ -63,6 +65,7 @@ pub struct InitHints {
pub hardware_acceleration: bool,
pub srgb: bool,
pub frame_sleep_time: Option<f32>,
pub resize_scaling: bool
}

impl InitHints {
Expand All @@ -75,6 +78,7 @@ impl InitHints {
hardware_acceleration: true,
srgb: true,
frame_sleep_time: None,
resize_scaling: false
}
}
}
Expand All @@ -89,6 +93,7 @@ impl Default for InitHints {
hardware_acceleration: true,
srgb: true,
frame_sleep_time: None,
resize_scaling: false
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions bracket-terminal/src/initializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,13 @@ impl BTermBuilder {
self
}

/// Enable resize changing console size, rather than scaling. Native OpenGL only.
#[cfg(all(feature = "opengl", not(target_arch = "wasm32")))]
pub fn with_resize_scaling(mut self, resize_scaling: bool) -> Self {
self.platform_hints.resize_scaling = resize_scaling;
self
}

/// Combine all of the builder parameters, and return an BTerm context ready to go.
pub fn build(self) -> Result<BTerm> {
let mut context = init_raw(
Expand Down

0 comments on commit d5f507d

Please sign in to comment.