bracket-lib icon indicating copy to clipboard operation
bracket-lib copied to clipboard

`BTerm.draw_box` draws a box but does not fill it.

Open micutio opened this issue 5 years ago • 5 comments

I'm confused about the use of BTerm::draw_box. According to the documentation and underlying code it should draw a filled box. Both, when calling it directly or indirectly via DrawBatch it does not fill the box. This happens both in my own project and the tutorial code.

The attached screenshot shows that.

tmp

Also using draw_box on an area filled with DrawBatch.fill_area removes the color inside the box again. It seems to me I'm missing something about how rendering works with bracket-lib.

micutio avatar Nov 27 '20 13:11 micutio

That is an odd one, and I'm having trouble reproducing it. When you cargo run --example benchmark (in bracket-terminal) is the box with the FPS count filled in? The various console call down to a draw_box helper, which includes a loop to fill the box content with spaces.

thebracket avatar Feb 11 '21 17:02 thebracket

That is an odd one, and I'm having trouble reproducing it. When you cargo run --example benchmark (in bracket-terminal) is the box with the FPS count filled in? The various console call down to a draw_box helper, which includes a loop to fill the box content with spaces.

After running cargo run --example benchmark the box with the FPS count is black, which means unfilled I reckon. I did dive into the called code and saw draw_box there as well.

The only hunch that I have is that the underlying engine might handle spaces differently than other glyphs, as the same calls for the box outline differ only in the glyph that is being drawn and seem to work fine.

micutio avatar Feb 17 '21 09:02 micutio

I also ran into this issue while following the roguelike tutorial. It seems like it's not just drawing an un-filled box, but actually filling the box with black instead of the specified background color. I am clearing the screen with the same color I'm passing to draw_box's bg value, but the box is still filled with black.

Bug

draw_box_bug

Commenting out draw_box call shows that it is overwriting existing background color

draw_box_bug_control

I wrote this as a workaround:

fn draw_box_bugfix(context: &mut BTerm, x: i32, y: i32, width: i32, height: i32, fg: RGB, bg: RGB) {
    context.draw_box(x, y, width, height, fg, bg);

    let blank = to_cp437(' ');
    for x in (x + 1)..(x + width) {
        for y in (y + 1)..(y + height) {
            context.set(x, y, fg, bg, blank);
        }
    }
}
Workaround Results

draw_box_bug_workaround

For reference, here's the source: https://github.com/krscott/bracketlib-roguelike-hecs/blob/d029ea874121f4f2df1d5faea6fd7cfd8ccfb551/src/gui.rs

krscott avatar Mar 04 '21 21:03 krscott

I'm also experiencing this issue. The box is always filled with black. The issue seems to be with the draw_box function in gui_helpers.rs.

pub fn draw_box(
    console: &mut dyn Console,
    sx: i32,
    sy: i32,
    width: i32,
    height: i32,
    fg: RGBA,
    bg: RGBA,
) {
    for y in sy..sy + height {
        for x in sx..sx + width {
            console.set(
                x,
                y,
                RGBA::from_f32(1.0, 1.0, 1.0, fg.a),
                RGBA::from_f32(0.0, 0.0, 0.0, fg.a),
                32,
            );
        }
    }
...

The RGBA::from_f32(0.0, 0.0, 0.0, fg.a) sets every background color to black regardless of bg color was provided. The rest of this function draws the border of the box, with the correct colors but it never fills the center coordinates.

I was able to fix this for my use-case by updating this logic to simply use the provided fg and bg instead:

...
for y in sy..sy + height {
        for x in sx..sx + width {
            console.set(
                x,
                y,
                fg,
                bg
                32,
            );
        }
    }
...

Not sure if there are ramifications that I'm missing by making this change, but I'm happy to open a PR!

camerondubas avatar Aug 22 '21 17:08 camerondubas

I fixed this issue with my project by realizing I was using a simple_console_no_bg and wondering why the background wasn't working... Shifted it to a sparse console and it worked as-is. Not sure if everyone else is having an identical issue, but leaving this here for any other lost souls.

BiosElement avatar Sep 11 '21 02:09 BiosElement