add stb_image, fix texture asset loading

This commit is contained in:
Matthew 2025-04-22 08:41:32 +10:00
parent d01a2e03a8
commit 8a7d4b30bd
6 changed files with 8031 additions and 5 deletions

7988
external/stb/stb_image.h vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@ -14,6 +14,8 @@ static Asset Texture_Asset_Lookup[TEXTURE_ASSET_MAX];
static AssetFile Shader_Assets[SHADER_ASSET_MAX];
static Asset Shader_Asset_Lookup[SHADER_ASSET_MAX];
static b32 ASSET_HEADER_LOADED = false;
// ::Assets::Global::End::
@ -40,16 +42,31 @@ static void LoadAssetPackHeader()
static Asset LoadTextureAsset(TextureAsset asset_id)
{
if (!ASSET_HEADER_LOADED)
{
LoadAssetPackHeader();
}
Assert(asset_id < i32(TEXTURE_ASSET_MAX), "LoadTextureAsset failure: asset_id is higher than TEXTURE_ASSET_MAX");
Asset asset = Texture_Asset_Lookup[asset_id];
if (asset.bytes == NULL)
{
AssetFile *asset_info = Texture_Assets + asset_id;
asset.bytes = FLMemAlloc(asset_info->len);
MemCpy(asset.bytes, &ASSET_PACK[asset_info->data_offset], asset_info->len);
u8 *img = FLMemAlloc(asset_info->len);
MemCpy(img, &ASSET_PACK[asset_info->data_offset], asset_info->len);
int x, y, ch;
asset.bytes = stbi_load_from_memory(img, asset_info->len, &x, &y, &ch, 4);
asset.len = asset_info->len;
asset.texture_meta.w = u32(x);
asset.texture_meta.h = u32(y);
Texture_Asset_Lookup[asset_id] = asset;
FLMemFree(img);
}
return asset;
@ -57,6 +74,11 @@ static Asset LoadTextureAsset(TextureAsset asset_id)
static Asset LoadShaderAsset(ShaderAsset asset_id)
{
if (!ASSET_HEADER_LOADED)
{
LoadAssetPackHeader();
}
Assert(asset_id < SHADER_ASSET_MAX, "LoadShaderAsset failure: asset_id is higher than SHADER_ASSET_MAX");
Asset asset = Shader_Asset_Lookup[asset_id];
@ -82,7 +104,7 @@ static void UnloadTextureAsset(Asset asset)
{
Texture_Asset_Lookup[i].bytes = NULL;
Texture_Asset_Lookup[i].len = 0;
FLMemFree(asset.bytes);
stbi_image_free(asset.bytes);
break;
}
}

View File

@ -61,10 +61,20 @@ typedef enum ModelAssetTag_e
MODEL_ASSET_TAG_MAX,
} ModelAssetTag;
typedef struct TextureAssetMeta
{
u32 w;
u32 h;
} TextureAssetMeta;
typedef struct Asset
{
u8 *bytes;
u64 len;
union
{
TextureAssetMeta texture_meta;
};
} Asset;
typedef struct AssetTag

View File

@ -6,10 +6,13 @@
#define STB_SPRINTF_IMPLEMENTATION
#define STB_IMAGE_IMPLEMENTATION
#define WINDOW_NAME "Video Game"
// ::ThirdParty::Include::Header::
#include "stb/stb_sprintf.h"
#include "stb/stb_image.h"
#include "xxhash/xxhash.h"
#include "fastlz/fastlz.h"

View File

@ -4,9 +4,14 @@
#define STB_SPRINTF_IMPLEMENTATION
#define STB_IMAGE_IMPLEMENTATION
#define WINDOW_NAME "Video Game"
#include "stb/stb_sprintf.h"
#include "stb/stb_image.h"
#include "xxhash/xxhash.h"
#include "fastlz/fastlz.h"
#include "shared_types.h"
#include "ds.h"

View File

@ -18,8 +18,6 @@ i16 mouse_pos_y = 0;
static void InitializeGame(Arena *arena, GameContext *ctx, Arena *ctx_arena)
{
LoadAssetPackHeader();
Assert(InitRenderer(arena), "Failed to initialize the renderer");
ctx->gui.vertices = MakeArray(ctx_arena, GUIVertex, 128);