amethyst / bracket-lib

The Roguelike Toolkit (RLTK), implemented for Rust.
MIT License
1.52k stars 108 forks source link

Resizing a BTerm #213

Closed Cthutu closed 2 years ago

Cthutu commented 3 years ago

When I resize a BTerm after creation, the characters in the window grow and shrink with it. Is it possible to increase and decrease the number of characters in a BTerm, so as to keep the size of the characters constant?

Cthutu commented 3 years ago

I've discovered the with_automatic_console_resize method in BTermBuilder, but there are 2 problems with it.

1) The fonts still shrink and grow slightly when resizing the window. 2) When the window is enlarged to a certain point, it causes a crash.

This was building on Windows 10 with the latest version of Rltk. Below is the code I used to test it:

use rltk::{GameState, Rltk};

struct State {}

impl GameState for State {
    fn tick(&mut self, ctx: &mut Rltk) {
        ctx.cls();
        ctx.print(1, 1, "Hello Rust World");
    }
}

fn main() -> rltk::BError {
    use rltk::RltkBuilder;
    let context = RltkBuilder::simple80x50()
        .with_automatic_console_resize(true)
        .with_title("Roguelike Tutorial")
        .build()?;
    let gs = State {};
    rltk::main_loop(context, gs)
}
Cthutu commented 3 years ago

Interestingly enough, it doesn't crash when maximising the window, only when dragging it.

thebracket commented 3 years ago

Does the bracket-lib example bench-scalable work for you? It uses the resizable console. I couldn't get it to crash on my Win10 machine. When I have some time (probably after the book release; it's literally eating every spare minute right now) I'll see if I can reproduce the crash with your code.

Cthutu commented 3 years ago

The font's do scale a little (this is probably because you're stretching the OpenGL texture which is a multiple of the font's dimensions to a window, which is not. The solution to stop the warping of characters is to either put a slight margin within the window, or restrict the inner size of the window to a multiple of the font's dimensions.

As for the crash, I could not reproduce it in that demo.

waldoplus commented 3 years ago

This issue might be related to #178. In that issue, a few pixels of the tiles are getting shaved off the bottom of tiles. Could also be due to the window size not being a multiple of the font size. Maybe you're on to something with using the letterboxing. Kyzrati describes the letterboxing and calculations used in making Cogmind pixel-perfect. https://www.gridsagegames.com/blog/2014/09/fonts-in-roguelikes/

Cthutu commented 3 years ago

I just colour the right and lower margins black in my C++ ASCII engine: https://github.com/cthutu/agf. I get it for free due to the clamping settings on the colour and ASCII character code textures I use.

thebracket commented 2 years ago

This does appear to be #178 , so I'll pursue it in there.

thebracket commented 2 years ago

The scaling system is a lot smarter now. Check the "bench_scalable" example - it supports adding/removing characters on resize rather than just scaling the window. #239 has made this quite a bit more stable.

thebracket commented 2 years ago

The latest scaling code should help with this. It only resizes the backing buffer to "natural" sizes, and adds gutters to avoid squishing - so the console grows/shrinks by a consistent amount.

thebracket commented 2 years ago

I'm no longer able to reproduce this bug, so I'm closing the issue. Please reopen if you run into the problem again.