Adding basic language support unfinished
This commit is contained in:
@ -21,6 +21,7 @@ add_subdirectory(assert)
|
||||
add_subdirectory(asset)
|
||||
add_subdirectory(display)
|
||||
add_subdirectory(input)
|
||||
add_subdirectory(locale)
|
||||
add_subdirectory(poker)
|
||||
add_subdirectory(scene)
|
||||
add_subdirectory(time)
|
||||
|
@ -91,6 +91,12 @@ size_t AssetLoader::loadRaw(uint8_t **buffer) {
|
||||
return read;
|
||||
}
|
||||
|
||||
size_t AssetLoader::setPosition(size_t position) {
|
||||
assertTrue(position >= 0);
|
||||
this->rewind();
|
||||
return this->skip(position);
|
||||
}
|
||||
|
||||
AssetLoader::~AssetLoader() {
|
||||
if(this->handle != nullptr) {
|
||||
this->close();
|
||||
|
@ -71,6 +71,14 @@ namespace Dawn {
|
||||
*/
|
||||
size_t getPosition();
|
||||
|
||||
/**
|
||||
* Sets the position of the file read head.
|
||||
*
|
||||
* @param position Position to set to.
|
||||
* @return How many bytes were skipped / rewound.
|
||||
*/
|
||||
size_t setPosition(size_t position);
|
||||
|
||||
/**
|
||||
* Loads the entire file into a raw buffer.
|
||||
* @param buffer Pointer to where a pointer to the buffer will be stored.
|
||||
|
@ -7,6 +7,7 @@
|
||||
#include "Asset.hpp"
|
||||
#include "util/array.hpp"
|
||||
|
||||
#include "assets/LanguageAsset.hpp"
|
||||
#include "assets/TextureAsset.hpp"
|
||||
#include "assets/TilesetAsset.hpp"
|
||||
#include "assets/TrueTypeAsset.hpp"
|
||||
|
@ -6,6 +6,7 @@
|
||||
# Sources
|
||||
target_sources(${DAWN_TARGET_NAME}
|
||||
PRIVATE
|
||||
LanguageAsset.cpp
|
||||
TextureAsset.cpp
|
||||
TilesetAsset.cpp
|
||||
TrueTypeAsset.cpp
|
||||
|
84
src/dawn/asset/assets/LanguageAsset.cpp
Normal file
84
src/dawn/asset/assets/LanguageAsset.cpp
Normal file
@ -0,0 +1,84 @@
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "LanguageAsset.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
LanguageAsset::LanguageAsset(AssetManager *man, std::string name) :
|
||||
Asset(man, name),
|
||||
loader(name + ".language")
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void LanguageAsset::updateSync() {
|
||||
|
||||
}
|
||||
|
||||
void LanguageAsset::updateAsync() {
|
||||
assertTrue(this->state == 0x00);
|
||||
|
||||
// Open Asset
|
||||
this->state = 0x01;
|
||||
this->loader.open();
|
||||
|
||||
// Get Length
|
||||
this->state = 0x02;
|
||||
this->loader.end();
|
||||
this->state = 0x03;
|
||||
size_t len = this->loader.getPosition();
|
||||
this->state = 0x04;
|
||||
this->loader.rewind();
|
||||
|
||||
// Create buffer
|
||||
this->state = 0x05;
|
||||
size_t position = 0;
|
||||
|
||||
// Loop over CSV
|
||||
while(position < len) {
|
||||
this->loader.read(buffer, 1024);
|
||||
|
||||
// Get strings
|
||||
uint8_t *keyStart = buffer;
|
||||
uint8_t *keyEnd = (uint8_t*)strchr((char*)keyStart, '|');
|
||||
*keyEnd = '\0';
|
||||
uint8_t *valueStart = keyEnd + 1;
|
||||
uint8_t *valueEnd = (uint8_t*)strchr((char*)valueStart, '|');
|
||||
|
||||
// Load value positions
|
||||
struct AssetLanguageValue value;
|
||||
value.begin = position + (size_t)(valueStart - buffer);
|
||||
value.length = (size_t)(valueEnd - valueStart);
|
||||
|
||||
// Prepare for next string.
|
||||
position = position + (size_t)(valueEnd - buffer + 1);
|
||||
this->loader.rewind();
|
||||
this->loader.skip(position);
|
||||
|
||||
// Store strings.
|
||||
std::string key((char *)keyStart);
|
||||
this->values[key] = value;
|
||||
}
|
||||
|
||||
this->state = 0x06;
|
||||
this->loader.rewind();
|
||||
|
||||
this->state = 0x07;
|
||||
this->loaded = true;
|
||||
}
|
||||
|
||||
std::string LanguageAsset::getValue(std::string key) {
|
||||
assertTrue(this->state == 0x07);
|
||||
|
||||
// Get the positions
|
||||
struct AssetLanguageValue value = this->values[key];
|
||||
|
||||
this->loader.setPosition(value.begin);
|
||||
this->loader.read(buffer, value.length);
|
||||
buffer[value.length] = '\0';
|
||||
|
||||
return std::string((char *)buffer, value.length + 1);
|
||||
}
|
31
src/dawn/asset/assets/LanguageAsset.hpp
Normal file
31
src/dawn/asset/assets/LanguageAsset.hpp
Normal file
@ -0,0 +1,31 @@
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "../Asset.hpp"
|
||||
#include "../AssetLoader.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
struct AssetLanguageValue {
|
||||
size_t begin;
|
||||
size_t length;
|
||||
};
|
||||
|
||||
class LanguageAsset : public Asset {
|
||||
protected:
|
||||
AssetLoader loader;
|
||||
uint8_t state = 0x00;
|
||||
std::map<std::string, struct AssetLanguageValue> values;
|
||||
uint8_t buffer[1024];
|
||||
|
||||
public:
|
||||
LanguageAsset(AssetManager *man, std::string name);
|
||||
|
||||
void updateSync() override;
|
||||
void updateAsync() override;
|
||||
|
||||
std::string getValue(std::string key);
|
||||
};
|
||||
}
|
@ -11,6 +11,7 @@
|
||||
#include "input/InputManager.hpp"
|
||||
#include "time/TimeManager.hpp"
|
||||
#include "input/InputBinds.hpp"
|
||||
#include "locale/LanguageManager.hpp"
|
||||
|
||||
#define DAWN_GAME_INIT_RESULT_SUCCESS 0
|
||||
#define DAWN_GAME_UPDATE_RESULT_SUCCESS 0
|
||||
|
11
src/dawn/locale/CMakeLists.txt
Normal file
11
src/dawn/locale/CMakeLists.txt
Normal file
@ -0,0 +1,11 @@
|
||||
# Copyright (c) 2022 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
|
||||
# Sources
|
||||
target_sources(${DAWN_TARGET_NAME}
|
||||
PRIVATE
|
||||
LanguageManager.cpp
|
||||
)
|
19
src/dawn/locale/LanguageManager.cpp
Normal file
19
src/dawn/locale/LanguageManager.cpp
Normal file
@ -0,0 +1,19 @@
|
||||
// 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");
|
||||
}
|
29
src/dawn/locale/LanguageManager.hpp
Normal file
29
src/dawn/locale/LanguageManager.hpp
Normal file
@ -0,0 +1,29 @@
|
||||
// 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);
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user