185 lines
3.7 KiB
C
185 lines
3.7 KiB
C
////////////////////////////////////////////////////////////
|
|
//~ Key types
|
|
|
|
Struct(SPR_SpanKey)
|
|
{
|
|
u64 hash;
|
|
};
|
|
|
|
Struct(SPR_SliceKey)
|
|
{
|
|
u64 hash;
|
|
};
|
|
|
|
////////////////////////////////////////////////////////////
|
|
//~ Texture types
|
|
|
|
Struct(SPR_Texture)
|
|
{
|
|
b32 valid;
|
|
b32 loaded;
|
|
GPU_Resource *gpu_texture;
|
|
u32 width;
|
|
u32 height;
|
|
};
|
|
|
|
extern Readonly SPR_Texture SPR_NilTexture;
|
|
|
|
////////////////////////////////////////////////////////////
|
|
//~ Sheet types
|
|
|
|
Struct(SPR_Frame)
|
|
{
|
|
u32 index;
|
|
f64 duration;
|
|
ClipRect clip;
|
|
};
|
|
|
|
Struct(SPR_Span)
|
|
{
|
|
SPR_Span *next_in_bin;
|
|
u64 hash;
|
|
String name;
|
|
u32 start;
|
|
u32 end;
|
|
};
|
|
|
|
Struct(SPR_SpanBin)
|
|
{
|
|
SPR_Span *first;
|
|
SPR_Span *last;
|
|
};
|
|
|
|
Struct(SPR_Slice)
|
|
{
|
|
/* If 1, this slice was not copied over from another frame in the sprite sheet */
|
|
b32 is_original;
|
|
|
|
/* If 1, the slice has a corresponding '.ray' slice affecting the 'dir' fields */
|
|
b32 has_dir;
|
|
|
|
/* Values are in the range -0.5 (top / left edge) -> +0.5 (bottom / right edge) */
|
|
Rect rect;
|
|
Vec2 center;
|
|
Vec2 dir;
|
|
|
|
/* '_px' values retain the original sprite pixel dimensions */
|
|
Rect rect_px;
|
|
Vec2 center_px;
|
|
Vec2 dir_px;
|
|
};
|
|
|
|
Struct(SPR_SliceGroup)
|
|
{
|
|
SPR_SliceGroup *next_in_bin;
|
|
u64 hash;
|
|
String name;
|
|
SPR_Slice *slices;
|
|
};
|
|
|
|
Struct(SPR_SliceGroupBin)
|
|
{
|
|
SPR_SliceGroup *first;
|
|
SPR_SliceGroup *last;
|
|
};
|
|
|
|
Struct(SPR_Sheet)
|
|
{
|
|
b32 valid;
|
|
b32 loaded;
|
|
Vec2 image_size;
|
|
Vec2 frame_size;
|
|
|
|
u32 frames_count;
|
|
SPR_Frame *frames;
|
|
|
|
u32 spans_count;
|
|
SPR_Span *spans;
|
|
|
|
u32 slice_groups_count;
|
|
SPR_SliceGroup *slice_groups;
|
|
|
|
u32 span_bins_count;
|
|
SPR_SpanBin *span_bins;
|
|
|
|
u32 slice_group_bins_count;
|
|
SPR_SliceGroupBin *slice_group_bins;
|
|
};
|
|
|
|
extern Readonly SPR_Sheet SPR_NilSheet;
|
|
|
|
////////////////////////////////////////////////////////////
|
|
//~ Cache types
|
|
|
|
Struct(SPR_Entry)
|
|
{
|
|
SPR_Entry *next_in_bin;
|
|
|
|
SPR_Texture texture;
|
|
SPR_Sheet sheet;
|
|
|
|
Atomic32 texture_touched;
|
|
Atomic32 sheet_touched;
|
|
|
|
ResourceKey resource;
|
|
Fence texture_ready_fence;
|
|
Fence sheet_ready_fence;
|
|
};
|
|
|
|
Struct(SPR_EntryBin)
|
|
{
|
|
SPR_Entry *first;
|
|
SPR_Entry *last;
|
|
Mutex mutex;
|
|
};
|
|
|
|
////////////////////////////////////////////////////////////
|
|
//~ State types
|
|
|
|
#define SPR_EntryBinsCount 1024
|
|
|
|
Struct(SPR_SharedState)
|
|
{
|
|
SPR_EntryBin entry_bins[SPR_EntryBinsCount];
|
|
} extern SPR_shared_state;
|
|
|
|
////////////////////////////////////////////////////////////
|
|
//~ Load jobs
|
|
|
|
JobDecl(SPR_LoadTexture, { SPR_Entry *entry; });
|
|
JobDecl(SPR_LoadSheet, { SPR_Entry *entry; });
|
|
|
|
////////////////////////////////////////////////////////////
|
|
//~ Cache operations
|
|
|
|
Enum(SPR_FetchFlag)
|
|
{
|
|
SPR_FetchFlag_None = 0,
|
|
SPR_FetchFlag_Texture = (1 << 0),
|
|
SPR_FetchFlag_Sheet = (1 << 1),
|
|
};
|
|
|
|
SPR_Entry *SPR_FetchEntry(ResourceKey resource, JobPoolId pool, SPR_FetchFlag flags);
|
|
|
|
////////////////////////////////////////////////////////////
|
|
//~ Sprite data retrieval operations
|
|
|
|
SPR_Texture *SPR_TextureFromResource(ResourceKey resource);
|
|
SPR_Texture *SPR_TextureFromResourceAsync(ResourceKey resource);
|
|
|
|
SPR_Sheet *SPR_SheetFromResource(ResourceKey resource);
|
|
SPR_Sheet *SPR_SheetFromResourceAsync(ResourceKey resource);
|
|
|
|
////////////////////////////////////////////////////////////
|
|
//~ Key helpers
|
|
|
|
SPR_SpanKey SPR_SpanKeyFromName(String name);
|
|
SPR_SliceKey SPR_SliceKeyFromName(String name);
|
|
|
|
////////////////////////////////////////////////////////////
|
|
//~ Sheet access operations
|
|
|
|
SPR_Span SPR_SpanFromKey(SPR_Sheet *sheet, SPR_SpanKey key);
|
|
SPR_Frame SPR_FrameFromIndex(SPR_Sheet *sheet, u64 index);
|
|
SPR_Slice SPR_SliceFromKey(SPR_Sheet *sheet, SPR_SliceKey key, u64 frame_index);
|