vn/code/vn_asset.cpp

98 lines
2.3 KiB
C++
Raw Permalink 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);
}
}
}
2024-01-20 11:18:57 +00:00
static render_handle A_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);
2024-01-20 11:18:57 +00:00
}
static r32 A_ScaleFromAssetID(asset_id ID)
{
Assert(ID >= AssetID_None && ID < AssetID_COUNT);
r32 Result = AssetScaleLUT[ID];
return(Result);
}
static asset_id A_AssetIDFromString(string String)
{
asset_id Result = AssetID_None;
for(u64 AssetIndex = 0; AssetIndex < AssetID_COUNT; AssetIndex += 1)
{
if(AreEqual(String, MakeString(AssetNameLUT[AssetIndex])))
{
Result = AssetIndex;
break;
}
}
return(Result);
}