use crate::MaterialRef;
use int_math::{URect, UVec2, Vec2, Vec3};
use monotonic_time_rs::Millis;
pub trait Assets {
fn material_png_raw(&mut self, png: &[u8], label: &str) -> MaterialRef;
fn material_png(&mut self, name: &str) -> MaterialRef;
fn set_prefix(&mut self, prefix: &str);
fn frame_fixed_grid_material_png(&mut self, name: &str, grid_size: UVec2) -> FixedAtlas;
fn now(&self) -> Millis;
}
pub trait Gfx {
fn set_origo(&mut self, position: Vec2);
fn sprite_atlas_frame(&mut self, position: Vec3, frame: u16, atlas: &impl FrameLookup);
fn now(&self) -> Millis;
}
pub trait FrameLookup {
fn lookup(&self, frame: u16) -> (MaterialRef, URect);
}
#[derive(Debug, PartialEq, Eq)]
pub struct FixedAtlas {
material: MaterialRef,
grid_size: UVec2,
cell_size: UVec2,
}
impl FixedAtlas {
pub fn new(grid_size: UVec2, material_ref: MaterialRef) -> Self {
let cell_size = UVec2::new(
material_ref.texture_size.x / grid_size.x,
material_ref.texture_size.y / grid_size.y,
);
Self {
material: material_ref,
grid_size,
cell_size,
}
}
}
impl FrameLookup for FixedAtlas {
fn lookup(&self, frame: u16) -> (MaterialRef, URect) {
let x = frame % self.cell_size.x;
let y = frame / self.cell_size.x;
(
self.material.clone(),
URect::new(
x * self.grid_size.x,
y * self.grid_size.y,
self.grid_size.x,
self.grid_size.y,
),
)
}
}