vn/code/vn_asset.cpp

78 lines
2.2 KiB
C++

global assets *Global_Assets;
static assets *GetAssets()
{
return(Global_Assets);
}
static void SetAssets(assets *Assets)
{
Global_Assets = Assets;
}
static void LoadAsset(asset_id ID)
{
assets *Assets = GetAssets();
Assert(ID >= AssetID_None && ID < AssetID_COUNT);
asset *Asset = Assets->Assets + ID;
if(!Asset->IsLoaded)
{
temporary_memory Scratch = GetScratch();
string Filepath = PushFormat(Scratch.Arena, "data/%s", AssetPathLUT[ID]);
platform_file_handle File = Platform.OpenFile(Filepath, PlatformAccess_Read);
if(File.IsValid)
{
u64 DataCount = Platform.GetFileSize(File);
u8 *Data = PushArray(Scratch.Arena, u8, DataCount);
Platform.ReadFile(File, Data, 0, DataCount);
s32 Width, Height, BPP;
u8 *TextureData = stbi_load_from_memory(Data, DataCount, &Width, &Height, &BPP, 0);
render_texture_format TextureFormat = Render_TextureFormat_Invalid;
switch(BPP)
{
InvalidDefaultCase;
case 1: { TextureFormat = Render_TextureFormat_R8; } break;
case 3: { TextureFormat = Render_TextureFormat_RGB8; } break;
case 4: { TextureFormat = Render_TextureFormat_RGBA8; } break;
}
Asset->Handle = Assets->AllocateTexture(V2S32(Width, Height), TextureFormat, true, TextureData);
stbi_image_free(TextureData);
Platform.CloseFile(File);
}
ReleaseScratch(Scratch);
}
}
static void LoadPermanentAssets(assets *Assets)
{
SetAssets(Assets);
//- sixten: assign ID to all assets & load all permanent ones
for(int Index = 0; Index < AssetID_COUNT; ++Index)
{
asset_id ID = (asset_id)Index;
asset *Asset = Assets->Assets + Index;
Asset->ID = ID;
if(ID != AssetID_None)
{
LoadAsset(ID);
}
}
}
static render_handle TextureFromAssetID(asset_id ID)
{
Assert(ID >= AssetID_None && ID < AssetID_COUNT);
asset *Asset = GetAssets()->Assets + ID;
return(Asset->Handle);
}