Asset management for MoonBit: image repositories, atlas allocation, source-image bindings, codec wrappers, and a fetcher abstraction (extracted from kagura)
Dependencies
pub trait AtlasAllocator {
fn allocate(Self, width : Int, height : Int) -> AtlasRegion?
fn release(Self, region : AtlasRegion) -> Unit
}pub(open) trait BytesFetcher {
fn fetch(Self, url : String, on_progress : (FetchProgress) -> Unit, on_complete : (Bytes?) -> Unit) -> FetchHandle
fn cancel(Self, handle : FetchHandle) -> Unit
fn poll(Self) -> Unit
}pub trait ImageRepository {
fn create_image(Self, key : AssetKey, spec : ImageSpec) -> ImageHandle raise
fn get_image(Self, key : AssetKey) -> ImageHandle?
fn remove_image(Self, key : AssetKey) -> Unit raise
}pub trait MaterialRepository {
fn register_material(Self, key : AssetKey, spec : MaterialSpec) -> Unit raise
fn get_material(Self, key : AssetKey) -> MaterialSpec?
}pub trait ShaderRepository {
fn create_shader(Self, key : AssetKey, spec : ShaderSpec) -> ShaderHandle raise
fn get_shader(Self, key : AssetKey) -> ShaderHandle?
fn remove_shader(Self, key : AssetKey) -> Unit raise
}pub struct AtlasDrawSource {
page_image_id : Int
region : AtlasRegion
u0 : Double
v0 : Double
u1 : Double
v1 : Double
} derive(Debug)impl Show for AtlasDrawSourcepub struct AtlasPageStats {
atlas_id : Int
image_count : Int
allocated_region_count : Int
atlas_width : Int
atlas_height : Int
is_empty : Bool
} derive(Debug)impl Show for AtlasPageStatsimpl Show for ImagePalette2x2pub struct ImageSpec {
width : Int
height : Int
format : String
palette : ImagePalette2x2
pixels_rgba8 : Array[Int]
} derive(Debug)pub struct MaterialSpec {
shader : AssetKey
blend : BlendMode
filter : FilterMode
uniform_keys : Array[String]
} derive(Debug)impl Show for MaterialSpecpub struct MultiPageAtlasRepository {
page_width : Int
page_height : Int
next_page_id : Int
pages : Array[SimpleAtlasImageRepository]
key_page_index : Array[(String, Int)]
}pub struct NativeFetcher {
read_file_fn : (String) -> Bytes?
next_id : Int
deferred : Array[DeferredResult]
}impl BytesFetcher for NativeFetcherfn fetch(self : NativeFetcher, url : String, on_progress : (FetchProgress) -> Unit, on_complete : (Bytes?) -> Unit) -> FetchHandleimpl Show for RasterImageFormatpub struct SimpleAtlasAllocator {
atlas_width : Int
atlas_height : Int
atlas_id : Int
cursor_x : Int
cursor_y : Int
row_height : Int
allocated : Array[AtlasRegion]
}impl AtlasAllocator for SimpleAtlasAllocatorpub struct SimpleAtlasImageRepository {
atlas_width : Int
atlas_height : Int
atlas_id : Int
next_image_id : Int
allocator : SimpleAtlasAllocator
images : Array[SimpleAtlasImageEntry]
page_generation : Int
page_dirty_rect : SourceImageDirtyRect?
}impl ImageRepository for SimpleImageRepositoryfn create_image(self : SimpleImageRepository, key : AssetKey, spec : ImageSpec) -> ImageHandle raisefn register_material(self : SimpleMaterialRepository, key : AssetKey, spec : MaterialSpec) -> Unit raiseimpl ShaderRepository for SimpleShaderRepositoryfn create_shader(self : SimpleShaderRepository, key : AssetKey, spec : ShaderSpec) -> ShaderHandle raisepub struct SourceImageBinding {
image_id : Int
width : Int
height : Int
generation : Int
dirty_rect : SourceImageDirtyRect?
palette : ImagePalette2x2
pixels_rgba8 : Array[Int]
} derive(Debug)impl Show for SourceImageBindingfn create_atlas_image(repo : SimpleAtlasImageRepository, key : AssetKey, spec : ImageSpec) -> ImageHandle?fn create_atlas_image_from_raster_bytes(repository : SimpleAtlasImageRepository, key : AssetKey, bytes : Bytes) -> ImageHandle? raisefn create_image_from_raster_bytes(repository : SimpleImageRepository, key : AssetKey, bytes : Bytes) -> ImageHandle? raisefn multi_page_create_image(repo : MultiPageAtlasRepository, key : AssetKey, spec : ImageSpec) -> ImageHandle?fn multi_page_update_image_spec(repo : MultiPageAtlasRepository, key : AssetKey, spec : ImageSpec) -> ImageHandle?fn new_simple_atlas_allocator(atlas_width : Int, atlas_height : Int, atlas_id : Int) -> SimpleAtlasAllocatorfn new_simple_atlas_image_repository(atlas_width : Int, atlas_height : Int, atlas_id : Int) -> SimpleAtlasImageRepositoryfn new_source_image_binding(image_id : Int, width : Int, height : Int, generation : Int, dirty_rect : SourceImageDirtyRect?, palette : ImagePalette2x2, pixels_rgba8 : Array[Int]) -> SourceImageBindingfn register_precomputed_image(repo : SimpleAtlasImageRepository, key : AssetKey, region : AtlasRegion, spec : ImageSpec) -> ImageHandlefn resize_image_spec(spec : ImageSpec, width : Int, height : Int, resize_method : RasterResizeMethod) -> ImageSpec raise EncodeErrorfn update_atlas_image_from_raster_bytes(repository : SimpleAtlasImageRepository, key : AssetKey, bytes : Bytes) -> ImageHandle? raisefn update_atlas_image_spec(repo : SimpleAtlasImageRepository, key : AssetKey, spec : ImageSpec) -> ImageHandle?fn update_image_from_raster_bytes(repository : SimpleImageRepository, key : AssetKey, bytes : Bytes) -> ImageHandle? raisefn update_image_spec(repo : SimpleImageRepository, key : AssetKey, spec : ImageSpec) -> ImageHandle?Asset management for MoonBit: image repositories, atlas allocation, source-image bindings, codec wrappers, and a fetcher abstraction (extracted from kagura)
Dependencies