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

Multiple SpriteConsoles

Open mettke opened this issue 4 years ago • 3 comments

I tried your example at bracket-lib/bracket-terminal/examples/sprites.rs and changed it to use the same sprite a second time but with grayscale. Unfortunately trying to use the second SpriteConsole does not work as it will use the SpriteSheet of the first console and not the second. Now I'm wondering whether I'm doing something wrong:

Code:

bracket_terminal::add_wasm_support!();

use bracket_random::prelude::*;
use bracket_terminal::prelude::*;

struct Dood {
    x: i32,
    y: i32,
}

struct State {
    frame: usize,
    timer: f32,
    doods: Vec<Dood>,
    rng: RandomNumberGenerator,
}

impl GameState for State {
    fn tick(&mut self, ctx: &mut BTerm) {
        ctx.set_active_console(1);
        ctx.cls();
        ctx.print(1, 1, "Watch them go!");
        ctx.printer(
            1,
            2,
            &format!("#[pink]FPS: #[]{}", ctx.fps),
            TextAlign::Left,
            None,
        );

        ctx.set_active_console(0);
        ctx.cls();

        for dood in self.doods.iter() {
            ctx.add_sprite(
                Rect::with_size(dood.x, dood.y, 32, 32),
                400 - dood.y,
                RGBA::from_f32(1.0, 1.0, 1.0, 1.0),
                self.frame % 4,
            )
        }

        self.timer += ctx.frame_time_ms;
        if self.timer > 66.0 {
            self.timer = 0.0;
            self.frame += 1;

            for dood in self.doods.iter_mut() {
                dood.x += self.rng.range(0, 3) - 1;
                dood.y += self.rng.range(0, 3) - 1;
            }
        }

        // Changed
        ctx.set_active_console(2);
        ctx.cls();
        ctx.add_sprite(
            Rect::with_size(200, 200, 50, 50),
            400,
            RGBA::from_f32(1.0, 1.0, 1.0, 1.0),
            0,
        )
    }
}

bracket_terminal::embedded_resource!(NYAN_CAT, "../res/sprite_dood.png");
// Changed
bracket_terminal::embedded_resource!(NYAN_CAT2, "../res/sprite_dood2.png");

fn main() -> BError {
    bracket_terminal::link_resource!(NYAN_CAT, "res/sprite_dood.png");
    // Changed
    bracket_terminal::link_resource!(NYAN_CAT2, "res/sprite_dood2.png");

    let context = BTermBuilder::new()
        .with_sprite_console(640, 400, 0)
        .with_font("terminal8x8.png", 8, 8)
        .with_simple_console_no_bg(80, 50, "terminal8x8.png")
        // Changed
        .with_sprite_console(640, 400, 1)
        .with_title("Bracket Terminal - Sprite Console")
        .with_sprite_sheet(
            SpriteSheet::new("res/sprite_dood.png")
                .add_sprite(Rect::with_size(0, 0, 85, 132))
                .add_sprite(Rect::with_size(85, 0, 85, 132))
                .add_sprite(Rect::with_size(170, 0, 85, 132))
                .add_sprite(Rect::with_size(255, 0, 85, 132)),
        )
        // Changed
        .with_sprite_sheet(
            SpriteSheet::new("res/sprite_dood2.png")
            .add_sprite(Rect::with_size(0, 0, 85, 132))
            .add_sprite(Rect::with_size(85, 0, 85, 132))
            .add_sprite(Rect::with_size(170, 0, 85, 132))
            .add_sprite(Rect::with_size(255, 0, 85, 132)),
        )
        .with_vsync(false)
        .build()?;

    let mut rng = RandomNumberGenerator::new();
    let mut doods = Vec::new();
    for _ in 0..100 {
        doods.push(Dood {
            x: rng.range(0, 640),
            y: rng.range(0, 400),
        });
    }

    let gs = State {
        frame: 0,
        timer: 0.0,
        doods,
        rng,
    };

    main_loop(context, gs)
}

This is my second sprite file:

sprite_dood2

and this is the result:

Screenshot from 2020-07-15 12-46-49

mettke avatar Jul 15 '20 10:07 mettke

@mettke I am facing the same issue. any luck yet? theoretically .with_add_spriteSheet() should push the new spritesheet to the vector BuilderTerm::sprite_sheet.

aknautiyal avatar Apr 12 '21 09:04 aknautiyal

@thebracket Is there any way to add multiple sprites. Currently it seem to get only the first sprite_sheet. Perhaps in file: bracket-terminal/src/hal/gl_common/backing/shared_main_loop.rs, function: render_consoles() it should be bi.sprite_sheets[cons.sprite_sheet]?

            ConsoleBacking::Sprite { backing } => {
                backing.gl_draw(bi.sprite_sheets[0].backing.as_ref().unwrap(), shader)?;
            }

aknautiyal avatar Apr 12 '21 10:04 aknautiyal

Spent a good 2 hours trying to figure out how to load multiple sprite sheets, was confident it was supported since I saw that it can store more than one sprite sheet, but it seems like it will only ever use the first one. Was there plans to support multiple sprite sheets that was scrapped or is still in progress?

Edit 2: Seems the above comment is right, making (roughly) that change fixes the issue. It will always just print from the first sprite sheet otherwise

bilowik avatar Aug 10 '22 03:08 bilowik