vn/code/vn_asset.cpp

78 lines
1.8 KiB
C++
Raw Normal View History

global assets *Global_Assets;
static assets *GetAssets()
{
2023-12-23 07:27:22 +00:00
return(Global_Assets);
}
static void SetAssets(assets *Assets)
{
2023-12-23 07:27:22 +00:00
Global_Assets = Assets;
}
static void LoadAsset(asset_id ID)
{
2023-12-23 07:27:22 +00:00
assets *Assets = GetAssets();
Assert(ID >= AssetID_None && ID < AssetID_COUNT);
asset *Asset = Assets->Assets + ID;
if(!Asset->IsLoaded)
{
temp 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)
{
2023-12-23 07:27:22 +00:00
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)
{
2023-12-23 07:27:22 +00:00
Assert(ID >= AssetID_None && ID < AssetID_COUNT);
asset *Asset = GetAssets()->Assets + ID;
return(Asset->Handle);
}