Load The Texture

The first part of loading sprites into Amethyst is to read the image into memory. Currently Amethyst supports PngFormat, BmpFormat, JpgFormat and TgaFormat.

The following snippet shows how to load a PNG image:

# extern crate amethyst;
use amethyst::assets::{AssetStorage, Loader};
use amethyst::prelude::*;
use amethyst::renderer::{PngFormat, Texture, TextureMetadata, TextureHandle};

pub fn load_texture<N>(name: N, world: &World) -> TextureHandle
where
    N: Into<String>,
{
    let loader = world.read_resource::<Loader>();
    loader.load(
        name,
        PngFormat,
        TextureMetadata::srgb(),
        (),
        &world.read_resource::<AssetStorage<Texture>>(),
    )
}

#[derive(Debug)]
struct ExampleState;

impl SimpleState for ExampleState {
    fn on_start(&mut self, data: StateData<'_, GameData<'_, '_>>) {
        let texture_handle = load_texture("texture/sprite_sheet.png", &data.world);
    }
}
#
# fn main() {}

There is one thing that may surprise you.

The loaded texture will use linear filter, e.g. screen pixels will be linearly interpolated between the closest image pixels. In layman's terms, if your images have small resolution, sprites will look blury. Use TextureMetadata::srgb_scale() instead to avoid such effect. Screen pixel will be taken from nearest pixel of texture in that case.