How can I know sprite really got loaded?

#1

Hi there!
I was looking for some fresh experience, so I went to meet Rust and Amethyst. So following code is kind of my “Hello World!” :slight_smile:
I took code from pong, where it works perfectly, and tried to implement to somewhat changed amethyst-starter-2d, but there it fails to render a sprite. Simply it does not show up. No error, nothing, just blank screen.
How can I know sprite got really loaded? Didnt find any method saying picture or RON file really loaded. When I did some errors in ron file, compiler gave me panick so I knew there was something wrong.
When I change name of file to a nonsense, amethyst does not give me any failure or panick. I dont see any check for existence of the given file, as well as I dont see a method that would tell me “ok, file got loaded”. There is just load method that seems to be synchronous, so I can tell only it should be finished when dbg! behind is in console…

Btw I have dbg! on sprite_sheet_handle, handle has id = 0 while texture_handle has id = 6. That seems to be a bit “unnatural” to me as usually IDs start from 1 and when there is 0, usually something is wrong or entity did not get saved or is before save or whatever. Does it have similar meaning here?

I tried to somehow debug, but failed.
This is implementation of Game class, it contains few other methods, but they are not part of sprite rendering.

impl Game {

    fn load_sprite(self: &Self, world: &mut World) -> Handle<SpriteSheet> {

        let texture_handle = {
            let loader = world.read_resource::<Loader>();
            let texture_storage = world.read_resource::<AssetStorage<Texture>>();
            loader.load(
                "sprites/knight.png",
                ImageFormat::default(),
                (),
                &texture_storage,
            )
        };

        dbg!(&texture_handle);
        dbg!(world.read_resource::<AssetStorage<Texture>>().get_by_id(6)); // this gives me 0 information

        let loader = world.read_resource::<Loader>();
        let sprite_sheet_store = world.read_resource::<AssetStorage<SpriteSheet>>();
        loader.load(
            "sprites/knight.ron",
            SpriteSheetFormat(texture_handle), // pass texture to ron file
            (),
            &sprite_sheet_store,
        )
    }

    fn initialize_sprite(
        &mut self,
        world: &mut World,
        sprite_sheet_handle: Handle<SpriteSheet>,
        dimensions: &ScreenDimensions
    ) {
        // Move the sprite to the middle of the window
        let mut sprite_transform = Transform::default();
        sprite_transform.set_translation_xyz(dimensions.width() / 2., dimensions.height() / 2., 0.);

        let sprite_render = SpriteRender {
            sprite_sheet: sprite_sheet_handle,
            sprite_number: 0, // First sprite
        };

        dbg!(&sprite_render);
        world
            .create_entity()
            .with(sprite_render.clone())
            .with(sprite_transform)
            .build();
    }
}

Call of given methods

fn on_start(&mut self, data: StateData<'_, GameData<'_, '_>>) {
        let world = data.world;
        let mut dimensions = (*world.read_resource::<ScreenDimensions>()).clone();

        self.sprite_sheet_handle.replace(self.load_sprite(world));

        dbg!(&self.sprite_sheet_handle);
        dbg!(&world.read_resource::<AssetStorage<SpriteSheet>>().get_by_id(0));

        self.initialize_sprite(world, self.sprite_sheet_handle.clone().unwrap(), &dimensions);
}

And finally output I get:

[INFO][amethyst::app] Initializing Amethyst...
[INFO][amethyst::app] Version: 0.15.0
[INFO][amethyst::app] Platform: x86_64-unknown-linux-gnu
[INFO][amethyst::app] Amethyst git commit: UNKNOWN
[INFO][amethyst::app] Rustc version: 1.44.1 Stable
[INFO][amethyst::app] Rustc git commit: c7087fe00d2ba919df1d813c040a5d47e43b0fe7
[INFO][winit::platform::platform::x11::window] Guessed window DPI factor: 1.5
[WARN][gfx_backend_vulkan] Unable to find extension: VK_KHR_wayland_surface
[WARN][gfx_backend_vulkan] Unable to find layer: VK_LAYER_LUNARG_standard_validation
[WARN][rendy_factory::factory] Slow safety checks are enabled! Disable them in production by enabling the 'no-slow-safety-checks' feature!
[INFO][rendy_util::wrap] Slow safety checks are enabled! You can disable them in production by enabling the 'no-slow-safety-checks' feature!
[src/state.rs:35] &texture_handle = Handle {
    id: 6,
}
[src/state.rs:36] world.read_resource::<AssetStorage<Texture>>().get_by_id(6) = None
[src/state.rs:128] &self.sprite_sheet_handle = Some(
    Handle {
        id: 0,
    },
)
[src/state.rs:129] &world.read_resource::<AssetStorage<SpriteSheet>>().get_by_id(0) = None
[src/state.rs:63] &sprite_render = SpriteRender {
    sprite_sheet: Handle {
        id: 0,
    },
    sprite_number: 0,
}
[INFO][amethyst::app] Engine is shutting down

Process finished with exit code 0
#2

Ok, so after some more debugging and digging in the code I was still in the dead end. I don’t know Rust and even less I know Amethyst. But that’s what it makes fun!
So I tried to simply run new project based on amethyst-starter-2d again. Placed dbg! macros and tried to mess around.
Originally I had spritesheet defined by Grid. So I changed to List. And it worked! Now I have screen populated with sprites, yaaaay!

original Grid

Grid((
    texture_width: 910,
    texture_height: 415,
    columns: 12,
    sprite_count: 48
))

new List

List((
    texture_width: 910,
    texture_height: 415,
    sprites: [
        (
            x: 0,
            y: 0,
            width: 65,
            height: 100,
        ),
        (
            x: 65,
            y: 0,
            width: 65,
            height: 100,
        ),
        (
            x: 130,
            y: 0,
            width: 65,
            height: 100,
        )
    ]
))

Frankly, I have no idea why Grid was problem. Syntax seems good to me. Before when I messed with syntax in Grid, I saw error in console. There was no error in console, no warning, nothing. To me it looks like it just failed quietly.

Anyone having an idea what happened?

1 Like
(Nathan) #3

According to the API docs, sprite_count is an Option<u32>, so your Grid would probably work if you did either [enable(implicit_some)] at the top of the file or sprite_count: Some(48)

The book really doesn’t make that clear, so I would have run into the same thing as you. If that fixes the problem, will you submit a PR to update the book to be more clear? The file to change would be: book/src/sprites/define_the_sprite_sheet.md

3 Likes
#4

Yup, it helped, so I created PR. Thanks for clarification!

2 Likes