Finished localization

This commit is contained in:
2022-12-16 08:05:01 -08:00
parent 36ad2fecca
commit c6bf90ee13
16 changed files with 173 additions and 58 deletions

View File

@ -62,7 +62,6 @@ namespace Dawn {
* @return 0 if successful, otherwise unsuccessful. * @return 0 if successful, otherwise unsuccessful.
*/ */
int32_t rewind(); int32_t rewind();
/** /**
* Retreive the current byte position within the asset that the head is * Retreive the current byte position within the asset that the head is

View File

@ -19,10 +19,18 @@ void AssetManager::queueLoad(std::vector<Asset*> assets) {
vectorAppend(&this->assetsToLoad, &assets); vectorAppend(&this->assetsToLoad, &assets);
} }
void AssetManager::queueLoad(Asset *asset) {
this->assetsToLoad.push_back(asset);
}
void AssetManager::queueUnload(std::vector<Asset*> assets) { void AssetManager::queueUnload(std::vector<Asset*> assets) {
vectorAppend(&this->assetsToUnload, &assets); vectorAppend(&this->assetsToUnload, &assets);
} }
void AssetManager::queueUnload(Asset *asset) {
this->assetsToUnload.push_back(asset);
}
void AssetManager::queueSwap( void AssetManager::queueSwap(
std::vector<Asset*> newAssets, std::vector<Asset*> newAssets,
std::vector<Asset*> oldAssets std::vector<Asset*> oldAssets
@ -57,6 +65,7 @@ void AssetManager::syncTick() {
auto asset = *it; auto asset = *it;
if(asset->loaded) { if(asset->loaded) {
it = this->assetsToLoad.erase(it); it = this->assetsToLoad.erase(it);
asset->eventLoaded.invoke();
continue; continue;
} }
@ -65,6 +74,7 @@ void AssetManager::syncTick() {
if(asset->loaded) { if(asset->loaded) {
it = this->assetsToLoad.erase(it); it = this->assetsToLoad.erase(it);
asset->eventLoaded.invoke();
continue; continue;
} }

View File

@ -32,6 +32,7 @@ namespace Dawn {
* @param assets Assets to load. * @param assets Assets to load.
*/ */
void queueLoad(std::vector<Asset*> assets); void queueLoad(std::vector<Asset*> assets);
void queueLoad(Asset *assets);
/** /**
* Takes a list of lists to queue to unload. Does not immediately unload. * Takes a list of lists to queue to unload. Does not immediately unload.
@ -39,6 +40,7 @@ namespace Dawn {
* @param assets Assets to unload. * @param assets Assets to unload.
*/ */
void queueUnload(std::vector<Asset*> assets); void queueUnload(std::vector<Asset*> assets);
void queueUnload(Asset* assets);
/** /**
* Queues load and unload based on the difference between two sets of * Queues load and unload based on the difference between two sets of
@ -82,10 +84,26 @@ namespace Dawn {
return asset; return asset;
} }
/**
* Both gets an asset, and puts it into the load queue.
*
* @param name Name of the asset to load.
* @return The asset element to be loaded.
*/
template<class T> template<class T>
T * getAndLoad(std::string name) {
auto asset = this->get<T>(name);
this->queueLoad(asset);
return asset;
}
template<class T>
void unload(T *asset) {
assertUnreachable();
}
void unload(std::string name) { void unload(std::string name) {
assertUnreachable(); assertUnreachable();
//should delete the asset for you
} }
/** /**

View File

@ -24,7 +24,6 @@ void TextureAsset::updateSync() {
this->texture.buffer(this->colors); this->texture.buffer(this->colors);
this->state = 0x05; this->state = 0x05;
this->loaded = true; this->loaded = true;
this->eventLoaded.invoke();
} }
void TextureAsset::updateAsync() { void TextureAsset::updateAsync() {

View File

@ -95,7 +95,6 @@ void TilesetAsset::updateAsync() {
this->state = 0x07; this->state = 0x07;
this->loaded = true; this->loaded = true;
this->eventLoaded.invoke();
memoryFree(buffer); memoryFree(buffer);
} }

View File

@ -29,7 +29,6 @@ void TrueTypeAsset::updateSync() {
this->state = 0x05; this->state = 0x05;
this->loaded = true; this->loaded = true;
this->eventLoaded.invoke();
} }
void TrueTypeAsset::updateAsync() { void TrueTypeAsset::updateAsync() {

View File

@ -11,7 +11,7 @@
#include "input/InputManager.hpp" #include "input/InputManager.hpp"
#include "time/TimeManager.hpp" #include "time/TimeManager.hpp"
#include "input/InputBinds.hpp" #include "input/InputBinds.hpp"
#include "locale/LanguageManager.hpp" #include "locale/LocaleManager.hpp"
#define DAWN_GAME_INIT_RESULT_SUCCESS 0 #define DAWN_GAME_INIT_RESULT_SUCCESS 0
#define DAWN_GAME_UPDATE_RESULT_SUCCESS 0 #define DAWN_GAME_UPDATE_RESULT_SUCCESS 0

View File

@ -7,5 +7,5 @@
# Sources # Sources
target_sources(${DAWN_TARGET_NAME} target_sources(${DAWN_TARGET_NAME}
PRIVATE PRIVATE
LanguageManager.cpp LocaleManager.cpp
) )

View File

@ -1,19 +0,0 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "LanguageManager.hpp"
#include "game/DawnGame.hpp"
using namespace Dawn;
LanguageManager::LanguageManager(DawnGame *game) {
this->game = game;
auto lang = this->game->assetManager.get<LanguageAsset>("language_en");
while(!lang->loaded) {
lang->updateAsync();
}
std::cout << lang->getValue("test2");
}

View File

@ -1,29 +0,0 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "event/Event.hpp"
#include "asset/AssetManager.hpp"
namespace Dawn {
class DawnGame;
struct LocalizedString {
std::string data;
};
class LanguageManager {
private:
DawnGame *game;
LanguageAsset *asset;
public:
Event<> eventLanguageChanged;
LanguageManager(DawnGame *game);
struct LocalizedString getString(std::string key);
};
}

View File

@ -0,0 +1,70 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "LocaleManager.hpp"
#include "game/DawnGame.hpp"
using namespace Dawn;
LocaleManager::LocaleManager(DawnGame *game) {
this->game = game;
this->locale.language = "en";
}
void LocaleManager::init() {
// Begin loading
if(this->currentlyLoadedAsset == nullptr && this->loadingAsset == nullptr) {
this->loadingAsset = this->game->assetManager.getAndLoad<LanguageAsset>("language_" + this->locale.language);
this->loadingAsset->eventLoaded.addListener(this, &LocaleManager::onLanguageLoaded);
}
}
void LocaleManager::setLocale(struct Locale locale) {
auto oldLocale = this->getLocale();
this->locale = locale;
// Did the language change?
if(this->locale.language != oldLocale.language) {
// Yes, switch languages.
// Are we already loading something? Stop it.
if(this->loadingAsset != nullptr) {
this->loadingAsset->eventLoaded.removeListener(this, &LocaleManager::onLanguageLoaded);
this->game->assetManager.queueUnload(this->loadingAsset);
this->loadingAsset = nullptr;
}
this->loadingAsset = this->game->assetManager.getAndLoad<LanguageAsset>("language_" + this->locale.language);
this->loadingAsset->eventLoaded.addListener(this, &LocaleManager::onLanguageLoaded);
}
this->eventLocaleChanged.invoke();
}
struct Locale LocaleManager::getLocale() {
return this->locale;
}
std::string LocaleManager::getString(std::string key) {
assertNotNull(this->currentlyLoadedAsset);
return this->currentlyLoadedAsset->getValue(key);
}
void LocaleManager::onLanguageLoaded() {
// Unload the previously loaded language
if(this->currentlyLoadedAsset != nullptr) {
this->game->assetManager.queueUnload(this->currentlyLoadedAsset);
}
// Swap loaded languages
this->currentlyLoadedAsset = this->loadingAsset;
this->loadingAsset = nullptr;
// Notify.
this->eventLanguageUpdated.invoke();
}

View File

@ -0,0 +1,68 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "event/Event.hpp"
#include "asset/AssetManager.hpp"
namespace Dawn {
class DawnGame;
struct Locale {
std::string language;
};
class LocaleManager {
private:
DawnGame *game;
LanguageAsset *asset;
struct Locale locale;
LanguageAsset *currentlyLoadedAsset = nullptr;
LanguageAsset *loadingAsset = nullptr;
/** Listens for when the pending language loads. */
void onLanguageLoaded();
public:
Event<> eventLocaleChanged;
Event<> eventLanguageUpdated;
/**
* Initializes the Locale Manager Instance. Locale Manager is responsible
* for handling anything that will change depending on which region the
* player is in.
*
* @param game Game instance this manager belongs to.
*/
LocaleManager(DawnGame *game);
/**
* Initializes the LocaleManager and loads the default language.
*/
void init();
/**
* Change the locale and begin loading the new language.
*
* @param locale Locale to switch to.
*/
void setLocale(struct Locale locale);
/**
* Gets the current locale.
*
* @return Current locale.
*/
struct Locale getLocale();
/**
* Returns a language string from the language CSV file.
*
* @param key Key of the string to get.
* @return The translated string.
*/
std::string getString(std::string key);
};
}

View File

@ -23,7 +23,7 @@ add_subdirectory(visualnovel)
add_subdirectory(scenes) add_subdirectory(scenes)
# Assets # Assets
tool_language(language_en en.csv) tool_language(language_en locale/en.csv)
tool_texture(texture_test texture_test.png) tool_texture(texture_test texture_test.png)
tool_texture(texture_city_day borrowed/city_day.png) tool_texture(texture_city_day borrowed/city_day.png)
tool_texture(texture_city_night borrowed/city_night.png) tool_texture(texture_city_night borrowed/city_night.png)

View File

@ -15,12 +15,13 @@ DawnGame::DawnGame(DawnHost *host) :
host(host), host(host),
renderManager(this), renderManager(this),
inputManager(this), inputManager(this),
languageManager(this) localeManager(this)
{ {
} }
int32_t DawnGame::init() { int32_t DawnGame::init() {
this->assetManager.init(); this->assetManager.init();
this->localeManager.init();
this->renderManager.init(); this->renderManager.init();
this->scene = new TestScene(this); this->scene = new TestScene(this);

View File

@ -15,7 +15,7 @@ namespace Dawn {
AssetManager assetManager; AssetManager assetManager;
InputManager inputManager; InputManager inputManager;
TimeManager timeManager; TimeManager timeManager;
LanguageManager languageManager; LocaleManager localeManager;
DawnGame(DawnHost *host); DawnGame(DawnHost *host);
int32_t init() override; int32_t init() override;