Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get SpriteConsole's sprite sheet when rendering sprite #287

Merged
merged 1 commit into from
Aug 10, 2022

Conversation

bilowik
Copy link
Contributor

@bilowik bilowik commented Aug 10, 2022

Related to #152 and also got me very very close to the fix included in this PR, so credits to aknautiyal.

I think the expected behavior is that a SpriteConsole has a sprite_sheet idx and uses this to render from the correct SpriteSheet from BTermInternal.sprite_sheets. Currently though the code in bracket-terminal/src/hal/gl_common/shared_main_loop.rs will only ever pull from the first sprite sheet. The change I provide here gets the correct sprite sheet idx from the SpriteConsole so that it will render the correct sprite.

Minimal case for reproduction:

use bracket_terminal::prelude::*;

struct State;

impl GameState for State {
    fn tick(&mut self, ctx: &mut BTerm) {
        ctx.set_active_console(0); // The console that registered the sprite_dood
        ctx.cls(); // Clear console 0
        ctx.add_sprite(Rect::with_size(1, 1, 64, 64), 0, RGBA::from_f32(1.0, 1.0, 1.0, 1.0), 0);

        ctx.set_active_console(1); // The console that registered the example tiles
        ctx.cls(); // Clear console 0
        ctx.add_sprite(Rect::with_size(1, 65, 64, 64), 0, RGBA::from_f32(1.0, 1.0, 1.0, 1.0), 0);
    }
}

fn main() -> BError {
    let ctx = BTermBuilder::new()
        .with_font("terminal8x8.png", 8, 8)
        .with_sprite_sheet(SpriteSheet::new("../resources/other_dood.png") // indexed by 0
            .add_sprite(Rect::with_size(0, 0, 16, 16)))
        .with_sprite_sheet(SpriteSheet::new("../resources/sprite_dood.png") // indexed by 1
            .add_sprite(Rect::with_size(0, 0, 85, 132)))
        .with_sprite_console(640, 480, 0) // Uses the sprite_dood SpriteSheet, indexed by 0
        .with_sprite_console(640, 480, 1) // Uses the example_tiles SpriteSheet, indexed by 1
        .build()?;

    main_loop(ctx, State)
}

The above code produces this output on current master, it is pulling from the first spritesheet for both SpriteConsoles
image

This PR produces this output, which has them each pulling their respective sprite sheets:
image

I don't have a super deep understanding of this project's code yet so apologies if there's some glaring and obvious reason why this code is the way it is. This could also be a breaking change if some projects relied on it always pulling from the first spritesheet even when specifying others.

I assumed that it is pretty guaranteed that the ConsoleBacking will never mismatch and that the console will always be a SpriteConsole in that branch of the match statement so that the downcast should never panic. If there were a mismatch for some reason and the underlying Console isn't a SpriteConsole, I would assume there'd be a panic or some undefined behavior from trying to draw a sprite to it anyway.

@thebracket
Copy link
Collaborator

That looks great, and was on my to-do list - thank you! I'll merge it via Github (my work PC just lost its main drive, so I'm on an ancient laptop and can't test much) and check it as soon as I can.

@thebracket thebracket merged commit 34ba812 into amethyst:master Aug 10, 2022
@thebracket thebracket self-assigned this Aug 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants