diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2025-01-31 19:31:10 -0500 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2025-01-31 19:31:10 -0500 |
| commit | 47d3d48ad76d6a4e40da1d945cbc8940ae2922c7 (patch) | |
| tree | 233963233206323b11e440394d61d97ee68b34f3 /src | |
| parent | 61cb835291af07cfc59c123241c3641a2fba185f (diff) | |
pke: first-pass font serialization
Diffstat (limited to 'src')
| -rw-r--r-- | src/asset-manager.cpp | 2 | ||||
| -rw-r--r-- | src/asset-manager.hpp | 2 | ||||
| -rw-r--r-- | src/font.cpp | 71 | ||||
| -rw-r--r-- | src/font.hpp | 13 | ||||
| -rw-r--r-- | src/game.cpp | 2 | ||||
| -rw-r--r-- | src/project.cpp | 16 |
6 files changed, 90 insertions, 16 deletions
diff --git a/src/asset-manager.cpp b/src/asset-manager.cpp index d12d860..507b9e6 100644 --- a/src/asset-manager.cpp +++ b/src/asset-manager.cpp @@ -195,7 +195,7 @@ const AssetHandle AM_GetHandle(AssetKey key) { return AssetHandle_MAX; } -uint64_t AM_GetBucketCount() { +pk_handle_bucket_index_T AM_GetBucketCount() { return Asset_BucketContainer.pkeHandle.bucketIndex + 1; } diff --git a/src/asset-manager.hpp b/src/asset-manager.hpp index fb922e9..42cf423 100644 --- a/src/asset-manager.hpp +++ b/src/asset-manager.hpp @@ -51,7 +51,7 @@ AssetHandle AM_Register(AssetKey key, AssetType type, const char *path); void AM_Release(AssetHandle assetHandle); const Asset *AM_Get(AssetHandle assetHandle); const AssetHandle AM_GetHandle(AssetKey key); -uint64_t AM_GetBucketCount(); +pk_handle_bucket_index_T AM_GetBucketCount(); Asset *AM_GetAssets(pk_handle_bucket_index_T bucketIndex, pk_handle_item_index_T &itemCount); void AM_Teardown(); void AM_DebugPrint(); diff --git a/src/font.cpp b/src/font.cpp index 3162362..45569cb 100644 --- a/src/font.cpp +++ b/src/font.cpp @@ -1,14 +1,19 @@ #include "font.hpp" #include "asset-manager.hpp" +#include "helpers.hpp" #include "pk.h" #include "window.hpp" #include "static-plane.hpp" #include "vendor-stb-image-include.h" -// #include "bucketed-array.hpp" +#include "game-settings.hpp" // TODO threading +const char *PKE_PROJECT_FONT_TITLE = "Font::Title: "; +const char *PKE_PROJECT_FONT_TEXTURE_HANDLE = "Font::TextureHandle: "; +const char *PKE_PROJECT_FONT_GLYPH_DETAILS_HANDLE = "Font::GlyphDetailsHandle: "; + TypeSafeInt_B(FontTypeIndex); TypeSafeInt_B(FontRenderIndex); @@ -36,8 +41,57 @@ void FontType_Teardown() { pk_delete(ftd.arr_ft, (FontTypeIndex_T)ftd.n_ft); } -FontType* FontType_GetFonts(FontTypeIndex &count) -{ +void FontType_Serialize(std::ofstream &stream, FontType *ft) { + NULL_CHAR_ARR(handleStr, 23); + stream << PKE_PROJECT_FONT_TITLE << ft->title.val << std::endl; + snprintf(handleStr, 22, "0x%08X 0x%08X", ft->fontTextureAssetHandle.bucketIndex, ft->fontTextureAssetHandle.itemIndex); + stream << PKE_PROJECT_FONT_TEXTURE_HANDLE << handleStr << std::endl; + snprintf(handleStr, 22, "0x%08X 0x%08X", ft->fontTextureAssetHandle.bucketIndex, ft->glyphDetailsAssetHandle.itemIndex); + stream << PKE_PROJECT_FONT_GLYPH_DETAILS_HANDLE << handleStr << std::endl; +} + +void FontType_Deserialize(std::ifstream &stream) { + NULL_CHAR_ARR(readLine, 128); + pk_cstr title; + AssetHandle fontTextureHandle; + AssetHandle glyphDetailsHandle; + while (memset(readLine, 0, 128), stream.getline(readLine, 128)) { + if (strcmp("", readLine) == 0) { + FontType_RegisterFont(title, fontTextureHandle, glyphDetailsHandle); + return; + } + if (strstr(readLine, PKE_PROJECT_FONT_TITLE)) { + uint64_t prefixLen = strlen(PKE_PROJECT_FONT_TITLE); + title.length = strlen(readLine + prefixLen); + title.reserved = title.length + 1; + title.val = pk_new<char>(title.reserved, pkeSettings.mem.bkt); + strncpy(const_cast<char*>(title.val), readLine + prefixLen, title.reserved); + continue; + } + if (strstr(readLine, PKE_PROJECT_FONT_TEXTURE_HANDLE)) { + uint64_t prefixLen = strlen(PKE_PROJECT_FONT_TEXTURE_HANDLE); + // 0x00000000 0x00000000 + readLine[prefixLen + 10] = '\0'; + STR2NUM_ERROR result1 = str2num(fontTextureHandle.bucketIndex, readLine + prefixLen + 2, 16); + STR2NUM_ERROR result2 = str2num(fontTextureHandle.itemIndex, readLine + prefixLen + 11, 16); + assert(result1 == STR2NUM_ERROR::SUCCESS); + assert(result2 == STR2NUM_ERROR::SUCCESS); + continue; + } + if (strstr(readLine, PKE_PROJECT_FONT_GLYPH_DETAILS_HANDLE)) { + uint64_t prefixLen = strlen(PKE_PROJECT_FONT_GLYPH_DETAILS_HANDLE); + // 0x00000000 0x00000000 + readLine[prefixLen + 10] = '\0'; + STR2NUM_ERROR result1 = str2num(glyphDetailsHandle.bucketIndex, readLine + prefixLen + 2, 16); + STR2NUM_ERROR result2 = str2num(glyphDetailsHandle.itemIndex, readLine + prefixLen + 11, 16); + assert(result1 == STR2NUM_ERROR::SUCCESS); + assert(result2 == STR2NUM_ERROR::SUCCESS); + continue; + } + } +} + +FontType* FontType_GetFonts(FontTypeIndex &count) { count = ftd.h_ft; return ftd.arr_ft; } @@ -74,8 +128,8 @@ FontTypeIndex FontType_RegisterFont(pk_cstr title, AssetHandle fontTextureHandle FontTypeIndex_T idx_t = (FontTypeIndex_T)idx; FontType *ft = &ftd.arr_ft[idx_t]; ft->title = title; - ft->fontTextureAssetKey = fontTextureHandle; - ft->glyphDetailsAssetKey = glyphsHandle; + ft->fontTextureAssetHandle = fontTextureHandle; + ft->glyphDetailsAssetHandle = glyphsHandle; ft->renders = pk_new<FontRender>(startingGlyphCount); ft->n_render = FontRenderIndex{startingGlyphCount}; @@ -575,8 +629,7 @@ FontTypeIndex FontType_RegisterFont(pk_cstr title, AssetHandle fontTextureHandle return idx; } -void FontType_Unload(FontTypeIndex idx) -{ +void FontType_Unload(FontTypeIndex idx) { FontType *ft = &ftd.arr_ft[(FontTypeIndex_T)idx]; if (ft->vkDescriptorSets != VK_NULL_HANDLE && ft->vkDescriptorPool != VK_NULL_HANDLE) { @@ -640,4 +693,8 @@ void FontType_Unload(FontTypeIndex idx) if (ft->deviceMemoryTexture != VK_NULL_HANDLE) vkFreeMemory(vkDevice, ft->deviceMemoryTexture, vkAllocator); ft->deviceMemoryTexture = VK_NULL_HANDLE; + + if (ft->title.reserved != 0 && ft->title.val != nullptr) { + pk_delete<char>(ft->title.val, ft->title.reserved); + } } diff --git a/src/font.hpp b/src/font.hpp index 48a7b40..494c365 100644 --- a/src/font.hpp +++ b/src/font.hpp @@ -6,6 +6,8 @@ #include "pk.h" #include <glm/vec4.hpp> +#include <ostream> +#include <istream> /* Vert: (two triangles) @@ -36,8 +38,8 @@ struct FontRender { }; struct FontType : public Entity_Base { pk_cstr title; - AssetHandle fontTextureAssetKey; - AssetHandle glyphDetailsAssetKey; + AssetHandle fontTextureAssetHandle; + AssetHandle glyphDetailsAssetHandle; VkDeviceMemory deviceMemoryVert = VK_NULL_HANDLE; VkDeviceMemory deviceMemoryTexture = VK_NULL_HANDLE; VkDeviceMemory deviceMemoryInst = VK_NULL_HANDLE; @@ -60,13 +62,12 @@ struct FontType : public Entity_Base { } bindings; }; - void FontType_Init(); void FontType_Teardown(); -// void FontType_Serialize(FontType *ft); // TODO -// void FontType_Deserialize(FontType **ft); // TODO +void FontType_Serialize(std::ofstream &stream, FontType *ft); +void FontType_Deserialize(std::ifstream &stream); FontType* FontType_GetFonts(FontTypeIndex &count); -FontTypeIndex FontType_RegisterFont(pk_cstr title, AssetKey fontTexture, AssetKey glyphs); +FontTypeIndex FontType_RegisterFont(pk_cstr title, AssetHandle fontTexture, AssetHandle glyphs); void FontType_Unload(FontTypeIndex idx); FontRender FontType_AddStringRender(FontTypeIndex idx_ft, pk_cstr cstr); void FontType_RemoveStringRender(FontRender fr); diff --git a/src/game.cpp b/src/game.cpp index 0fb2ccd..a95d2fd 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -594,8 +594,8 @@ void Game_Main(PKEWindowProperties windowProps, const char *executablePath) { Game_Init(); CreateWindow(windowProps); EntityType_Init(); - PkeProject_Load(pkeSettings.args.projectPath); PkeInput_Init(); + PkeProject_Load(pkeSettings.args.projectPath); if (pkeSettings.args.pluginPath != nullptr) { PkePlugin_Load(pkeSettings.args.pluginPath); } diff --git a/src/project.cpp b/src/project.cpp index ee8a0b1..891e5ea 100644 --- a/src/project.cpp +++ b/src/project.cpp @@ -4,6 +4,7 @@ #include "plugins.hpp" #include "entities.hpp" #include "helpers.hpp" +#include "font.hpp" #include <fstream> #include <ostream> @@ -18,6 +19,7 @@ const char* const PKE_PROJ_FILE_OBJ_END = ""; const char* const PKE_PROJ_FILE_OBJ_PROJECT_SETTINGS = "ProjectSettings:"; const char* const PKE_PROJ_FILE_OBJ_ENTITY_TYPE = "EntityType:"; const char* const PKE_PROJ_FILE_OBJ_ASSET = "Asset:"; +const char* const PKE_PROJ_FILE_OBJ_FONT = "Font:"; const char* const PKE_PROJ_FILE_PROJ_SETTINGS_DEFAULT_SCENE_NAME = "PkeSet::DefaultSceneName: "; const char* const PKE_PROJ_FILE_PROJ_SETTINGS_SCENE_COUNT = "PkeSet::SceneCount: "; @@ -332,6 +334,10 @@ void PkeProject_Load(const char *filePath) { Proj_ParseEntityType(f); continue; } + if (strcmp(PKE_PROJ_FILE_OBJ_FONT, projReadLine) == 0) { + FontType_Deserialize(f); + continue; + } } f.close(); @@ -388,6 +394,16 @@ void PkeProject_Save(const char *filePath) { } } + FontTypeIndex font_count; + FontType *fonts = FontType_GetFonts(font_count); + for (FontTypeIndex b = FontTypeIndex{0}; b < font_count; ++b) { + FontType *ft = &fonts[(FontTypeIndex_T)b]; + if (ft->title.val == nullptr) continue; + f << PKE_PROJ_FILE_OBJ_FONT << std::endl; + FontType_Serialize(f, ft); + f << PKE_PROJ_FILE_OBJ_END << std::endl; + } + f << PKE_PROJ_FILE_END << std::endl; f.flush(); |
