About to relearn this version of the language.
This commit is contained in:
@ -22,11 +22,9 @@ target_link_libraries(${DAWN_TARGET_NAME}
|
||||
)
|
||||
|
||||
# Compile support targets
|
||||
if(DAWN_TARGET_LINUX)
|
||||
add_subdirectory(dawnglfw)
|
||||
add_subdirectory(dawnopengl)
|
||||
add_subdirectory(dawnlinux)
|
||||
endif()
|
||||
add_subdirectory(dawnglfw)
|
||||
add_subdirectory(dawnopengl)
|
||||
add_subdirectory(dawnlinux)
|
||||
|
||||
# Compress the game assets.
|
||||
add_dependencies(${DAWN_TARGET_NAME} dawnassets)
|
@ -36,4 +36,10 @@ add_subdirectory(scene)
|
||||
# add_subdirectory(state)
|
||||
add_subdirectory(time)
|
||||
add_subdirectory(util)
|
||||
add_subdirectory(ui)
|
||||
add_subdirectory(ui)
|
||||
|
||||
|
||||
# Assets
|
||||
tool_copy(en en.json)
|
||||
|
||||
add_dependencies(${DAWN_TARGET_NAME} dawnassets)
|
@ -4,7 +4,6 @@
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "AssetManager.hpp"
|
||||
#include "loaders/TextureLoader.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
|
@ -83,7 +83,16 @@ namespace Dawn {
|
||||
* @return The asset loader for the given asset.
|
||||
*/
|
||||
template<class T>
|
||||
std::shared_ptr<T> get(const std::string filename);
|
||||
std::shared_ptr<T> get(const std::string filename) {
|
||||
auto existing = this->getExisting<T>(filename);
|
||||
if(existing) return existing;
|
||||
|
||||
std::shared_ptr<T> loader = std::make_shared<T>(filename);
|
||||
pendingAssetLoaders.push_back(
|
||||
std::static_pointer_cast<AssetLoader>(loader)
|
||||
);
|
||||
return loader;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the asset loader for the given asset.
|
||||
|
@ -12,4 +12,4 @@ target_sources(${DAWN_TARGET_NAME}
|
||||
)
|
||||
|
||||
# Subdirs
|
||||
add_subdirectory(loaders)
|
||||
add_subdirectory(loader)
|
@ -14,6 +14,7 @@ Game::Game() {
|
||||
}
|
||||
|
||||
void Game::init() {
|
||||
localeManager.init(shared_from_this());
|
||||
renderHost.init(shared_from_this());
|
||||
inputManager.init(shared_from_this());
|
||||
saveManager.init(shared_from_this());
|
||||
|
@ -1,8 +1,62 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
// Copyright (c) 2024 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "LocaleManager.hpp"
|
||||
#include "assert/assert.hpp"
|
||||
#include "game/Game.hpp"
|
||||
#include "util/String.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
using namespace Dawn;
|
||||
|
||||
LocaleManager::LocaleManager() {
|
||||
}
|
||||
|
||||
void LocaleManager::init(const std::shared_ptr<Game> &game) {
|
||||
assertNotNull(game, "Game cannot be null.");
|
||||
this->game = game;
|
||||
|
||||
languageAsset = game->assetManager.get<JSONLoader>("en.json");
|
||||
}
|
||||
|
||||
std::string LocaleManager::getString(
|
||||
const std::string &key,
|
||||
const std::unordered_map<std::string, std::string> replacements
|
||||
) {
|
||||
assertNotNull(languageAsset, "Language asset cannot be null.");
|
||||
|
||||
// Key comes in like "main_menu.new_game", which would be;
|
||||
// data["main_menu"]["new_game"], so we need to split by dot and iterate.
|
||||
auto keyParts = String::split(key, ".");
|
||||
int32_t currentPart = 0;
|
||||
json dataCurrent = languageAsset->data;
|
||||
|
||||
while(currentPart < keyParts.size()) {
|
||||
auto &part = keyParts[currentPart];
|
||||
assertTrue(
|
||||
dataCurrent.contains(part),
|
||||
"Key '%s' does not exist in the language asset.", key.c_str()
|
||||
);
|
||||
dataCurrent = dataCurrent[part];
|
||||
currentPart++;
|
||||
}
|
||||
|
||||
// For each replacement, replace "{{ key }}" with the value.
|
||||
std::string output = dataCurrent.get<std::string>();// overrides?
|
||||
for(auto &replacement : replacements) {
|
||||
std::string key = "{{ " + replacement.first + " }}";
|
||||
size_t pos = 0;
|
||||
while((pos = output.find(key, pos)) != std::string::npos) {
|
||||
output.replace(pos, key.length(), replacement.second);
|
||||
pos += replacement.second.length();
|
||||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
LocaleManager::~LocaleManager() {
|
||||
languageAsset = nullptr;
|
||||
game.reset();
|
||||
}
|
||||
|
@ -1,15 +1,48 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
// Copyright (c) 2024 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "dawnlibs.hpp"
|
||||
#include "asset/loader/JSONLoader.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
class LocaleManager final {
|
||||
class Game;
|
||||
|
||||
class LocaleManager {
|
||||
private:
|
||||
std::weak_ptr<Game> game;
|
||||
std::shared_ptr<JSONLoader> languageAsset;
|
||||
|
||||
public:
|
||||
/**
|
||||
* Creates a new LocaleManager.
|
||||
*/
|
||||
LocaleManager();
|
||||
|
||||
/**
|
||||
* Initializes the LocaleManager.
|
||||
*
|
||||
* @param game The game to initialize with.
|
||||
*/
|
||||
void init(const std::shared_ptr<Game> &game);
|
||||
|
||||
/**
|
||||
* Returns a string from the locale.
|
||||
*
|
||||
* @param key The key of the string.
|
||||
* @param replacements The replacements to make in the string.
|
||||
* @return The string.
|
||||
*/
|
||||
std::string getString(
|
||||
const std::string &key,
|
||||
const std::unordered_map<std::string, std::string> replacements =
|
||||
std::unordered_map<std::string, std::string>()
|
||||
);
|
||||
|
||||
/**
|
||||
* Destroys the LocaleManager.
|
||||
*/
|
||||
~LocaleManager();
|
||||
};
|
||||
}
|
@ -3,20 +3,10 @@
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
if(NOT DEFINED DAWN_OPENGL_EXCLUDE_LIBRARIES)
|
||||
# Libraries
|
||||
find_package(OpenGL REQUIRED)
|
||||
|
||||
target_include_directories(${DAWN_TARGET_NAME}
|
||||
PUBLIC
|
||||
${OPENGL_INCLUDE_DIR}
|
||||
)
|
||||
|
||||
target_link_libraries(${DAWN_TARGET_NAME}
|
||||
PUBLIC
|
||||
${OPENGL_LIBRARIES}
|
||||
)
|
||||
endif()
|
||||
# Libraries
|
||||
target_link_libraries(${DAWN_TARGET_NAME}
|
||||
PRIVATE
|
||||
)
|
||||
|
||||
# Includes
|
||||
target_include_directories(${DAWN_TARGET_NAME}
|
||||
@ -24,7 +14,7 @@ target_include_directories(${DAWN_TARGET_NAME}
|
||||
${CMAKE_CURRENT_LIST_DIR}
|
||||
)
|
||||
|
||||
|
||||
# Subdirs
|
||||
add_subdirectory(assert)
|
||||
add_subdirectory(display)
|
||||
# add_subdirectory(scene)
|
||||
add_subdirectory(display)
|
Reference in New Issue
Block a user