98 lines
2.3 KiB
C++
98 lines
2.3 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)
|
|
{
|
|
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)
|
|
{
|
|
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 A_TextureFromAssetID(asset_id ID)
|
|
{
|
|
Assert(ID >= AssetID_None && ID < AssetID_COUNT);
|
|
asset *Asset = GetAssets()->Assets + ID;
|
|
return(Asset->Handle);
|
|
}
|
|
|
|
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);
|
|
} |