From de7504a4b4cfc3270fc9c6b36df4dcb0bbdda20d Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Sun, 18 Jun 2023 10:00:28 -0700 Subject: [PATCH] Fixed bug with VN scrolling --- src/dawn/asset/assets/TrueTypeAsset.cpp | 39 ++++++++++++++++--- src/dawn/asset/assets/TrueTypeAsset.hpp | 3 ++ .../font/truetype/TrueTypeFaceTexture.cpp | 4 -- .../font/truetype/TrueTypeFaceTexture.hpp | 2 - .../games/vn/components/VNTextboxScroller.cpp | 1 + src/dawnshared/util/UsageLock.cpp | 10 ++++- src/dawnshared/util/UsageLock.hpp | 11 +++++- 7 files changed, 56 insertions(+), 14 deletions(-) diff --git a/src/dawn/asset/assets/TrueTypeAsset.cpp b/src/dawn/asset/assets/TrueTypeAsset.cpp index d71343f2..baaae3a4 100644 --- a/src/dawn/asset/assets/TrueTypeAsset.cpp +++ b/src/dawn/asset/assets/TrueTypeAsset.cpp @@ -12,7 +12,38 @@ TrueTypeAsset::TrueTypeAsset(AssetManager *assMan, std::string name) : Asset(assMan, name), loader(name + ".truetype") { + this->locks.onLockRemoved = [&](usagelockid_t id){ + auto texture = this->textureByLock[id]; + assertNotNull(texture); + + std::vector &lbt = this->locksByTexture[texture]; + auto it0 = std::find(lbt.begin(), lbt.end(), id); + assertTrue(it0 != lbt.end()); + lbt.erase(it0); + + auto it1 = this->textureByLock.find(id); + assertTrue(it1 != this->textureByLock.end()); + this->textureByLock.erase(it1); + + if(lbt.empty()) { + auto it2 = locksByTexture.find(texture); + assertTrue(it2 != locksByTexture.end()); + locksByTexture.erase(it2); + + auto it3 = textureByStyle.begin(); + while(it3 != textureByStyle.end()) { + if(it3->second == texture) it3 = textureByStyle.erase(it3); + ++it3; + } + + auto it4 = std::find(textures.begin(), textures.end(), texture); + assertTrue(it4 != textures.end()); + textures.erase(it4); + + delete texture; + } + }; } void TrueTypeAsset::updateSync() { @@ -161,8 +192,9 @@ usagelockid_t TrueTypeAsset::lock(struct TrueTypeFaceTextureStyle style) { texture = it->second; } - auto lock = texture->locks.createLock(); + auto lock = this->locks.createLock(); this->textureByLock[lock] = texture; + this->locksByTexture[texture].push_back(lock); return lock; } @@ -173,10 +205,7 @@ TrueTypeFaceTexture * TrueTypeAsset::getTexture(usagelockid_t id) { } void TrueTypeAsset::unlock(usagelockid_t id) { - auto it = this->textureByLock.find(id); - assertTrue(it != this->textureByLock.end()); - it->second->locks.removeLock(id); - this->textureByLock.erase(it); + this->locks.removeLock(id); } TrueTypeAsset::~TrueTypeAsset() { diff --git a/src/dawn/asset/assets/TrueTypeAsset.hpp b/src/dawn/asset/assets/TrueTypeAsset.hpp index 8e0903c7..eb5f4a02 100644 --- a/src/dawn/asset/assets/TrueTypeAsset.hpp +++ b/src/dawn/asset/assets/TrueTypeAsset.hpp @@ -8,6 +8,7 @@ #include "../AssetLoader.hpp" #include "util/flag.hpp" #include "display/font/truetype/TrueTypeFaceTexture.hpp" +#include "util/UsageLock.hpp" namespace Dawn { enum TrueTypeAssetState { @@ -30,6 +31,7 @@ namespace Dawn { class TrueTypeAsset : public Asset { protected: + UsageLock locks; AssetLoader loader; FT_Library fontLibrary; enum TrueTypeAssetState state = TRUE_TYPE_ASSET_STATE_INITIAL; @@ -37,6 +39,7 @@ namespace Dawn { std::vector textures; std::map textureByLock; std::map textureByStyle; + std::map> locksByTexture; public: TrueTypeAsset(AssetManager *assMan, std::string name); diff --git a/src/dawn/display/font/truetype/TrueTypeFaceTexture.cpp b/src/dawn/display/font/truetype/TrueTypeFaceTexture.cpp index 1ce998a2..fb0dc514 100644 --- a/src/dawn/display/font/truetype/TrueTypeFaceTexture.cpp +++ b/src/dawn/display/font/truetype/TrueTypeFaceTexture.cpp @@ -15,10 +15,6 @@ TrueTypeFaceTexture::TrueTypeFaceTexture( this->face = face; this->style = style; - - this->locks.onEmpty = [&]() { - assertUnreachable(); - }; // Set freetype font size prior to baking. if(FT_Set_Pixel_Sizes(face, 0, style.fontSize)) { diff --git a/src/dawn/display/font/truetype/TrueTypeFaceTexture.hpp b/src/dawn/display/font/truetype/TrueTypeFaceTexture.hpp index 2a968ce9..2de32f93 100644 --- a/src/dawn/display/font/truetype/TrueTypeFaceTexture.hpp +++ b/src/dawn/display/font/truetype/TrueTypeFaceTexture.hpp @@ -7,7 +7,6 @@ #include "display/font/truetype/TrueTypeShared.hpp" #include "util/mathutils.hpp" #include "display/Texture.hpp" -#include "util/UsageLock.hpp" namespace Dawn { class TrueTypeAsset; @@ -34,7 +33,6 @@ namespace Dawn { FT_Face face; std::map characterData; struct TrueTypeFaceTextureStyle style; - UsageLock locks; Texture texture; /** diff --git a/src/dawn/games/vn/components/VNTextboxScroller.cpp b/src/dawn/games/vn/components/VNTextboxScroller.cpp index fd3e5be7..15f06673 100644 --- a/src/dawn/games/vn/components/VNTextboxScroller.cpp +++ b/src/dawn/games/vn/components/VNTextboxScroller.cpp @@ -23,6 +23,7 @@ void VNTextboxScroller::onStart() { this->timeCharacter = 0; this->label->quadStart = 0; this->label->quadCount = 0; + this->label->textOffset = glm::vec2(); this->readyToClose = false; }; x(); diff --git a/src/dawnshared/util/UsageLock.cpp b/src/dawnshared/util/UsageLock.cpp index d54154ac..088f80d5 100644 --- a/src/dawnshared/util/UsageLock.cpp +++ b/src/dawnshared/util/UsageLock.cpp @@ -7,13 +7,18 @@ using namespace Dawn; -UsageLock::UsageLock() { +UsageLock::UsageLock() : UsageLock(&this->internalPool) { +} + +UsageLock::UsageLock(usagelockid_t *lockPool) { + assertNotNull(lockPool); + this->pool = lockPool; this->onEmpty = []() {}; this->onFirstLock = []() {}; } usagelockid_t UsageLock::createLock() { - usagelockid_t lock = this->nextLock++; + usagelockid_t lock = (*this->pool)++; this->locks.push_back(lock); if(this->locks.size() == 1) this->onFirstLock(); return lock; @@ -23,5 +28,6 @@ void UsageLock::removeLock(usagelockid_t lock) { auto it = std::find(this->locks.begin(), this->locks.end(), lock); if(it == this->locks.end()) return; this->locks.erase(it); + this->onLockRemoved(lock); if(this->locks.size() == 0) this->onEmpty(); } \ No newline at end of file diff --git a/src/dawnshared/util/UsageLock.hpp b/src/dawnshared/util/UsageLock.hpp index 9ffe27f0..9fa0aa53 100644 --- a/src/dawnshared/util/UsageLock.hpp +++ b/src/dawnshared/util/UsageLock.hpp @@ -10,18 +10,27 @@ namespace Dawn { class UsageLock { protected: - usagelockid_t nextLock = 0; + usagelockid_t internalPool = 0; + usagelockid_t *pool = nullptr; std::vector locks; public: std::function onEmpty; std::function onFirstLock; + std::function onLockRemoved; /** * Construct a new usage lock object. */ UsageLock(); + /** + * Construct a new Usage Lock object + * + * @param lockPool Pool that will be used to create locks. + */ + UsageLock(usagelockid_t *lockPool); + /** * Creates a new lock. *