reestructuració
This commit is contained in:
69
source/core/locale/lang.cpp
Normal file
69
source/core/locale/lang.cpp
Normal file
@@ -0,0 +1,69 @@
|
||||
#include "core/locale/lang.h"
|
||||
|
||||
#include <fstream> // for basic_ifstream, basic_istream, ifstream
|
||||
#include <sstream>
|
||||
|
||||
#include "core/resources/asset.h" // for Asset
|
||||
#include "core/resources/resource_helper.h"
|
||||
|
||||
// Constructor
|
||||
Lang::Lang(Asset *mAsset) {
|
||||
this->mAsset = mAsset;
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Lang::~Lang() {
|
||||
}
|
||||
|
||||
// Inicializa los textos del juego en el idioma seleccionado
|
||||
bool Lang::setLang(Uint8 lang) {
|
||||
std::string file;
|
||||
|
||||
switch (lang) {
|
||||
case es_ES:
|
||||
file = mAsset->get("es_ES.txt");
|
||||
break;
|
||||
|
||||
case en_UK:
|
||||
file = mAsset->get("en_UK.txt");
|
||||
break;
|
||||
|
||||
case ba_BA:
|
||||
file = mAsset->get("ba_BA.txt");
|
||||
break;
|
||||
|
||||
default:
|
||||
file = mAsset->get("en_UK.txt");
|
||||
break;
|
||||
}
|
||||
|
||||
for (int i = 0; i < MAX_TEXT_STRINGS; i++)
|
||||
mTextStrings[i] = "";
|
||||
|
||||
// Lee el fichero via ResourceHelper (pack o filesystem)
|
||||
auto bytes = ResourceHelper::loadFile(file);
|
||||
if (bytes.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string content(reinterpret_cast<const char *>(bytes.data()), bytes.size());
|
||||
std::stringstream ss(content);
|
||||
std::string line;
|
||||
int index = 0;
|
||||
while (std::getline(ss, line)) {
|
||||
// Almacena solo las lineas que no empiezan por # o no esten vacias
|
||||
const bool test1 = line.substr(0, 1) != "#";
|
||||
const bool test2 = !line.empty();
|
||||
if (test1 && test2) {
|
||||
mTextStrings[index] = line;
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Obtiene la cadena de texto del indice
|
||||
std::string Lang::getText(int index) {
|
||||
return mTextStrings[index];
|
||||
}
|
||||
35
source/core/locale/lang.h
Normal file
35
source/core/locale/lang.h
Normal file
@@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <string> // for string, basic_string
|
||||
class Asset;
|
||||
|
||||
// Códigos de idioma
|
||||
constexpr int es_ES = 0;
|
||||
constexpr int ba_BA = 1;
|
||||
constexpr int en_UK = 2;
|
||||
constexpr int MAX_LANGUAGES = 3;
|
||||
|
||||
// Textos
|
||||
constexpr int MAX_TEXT_STRINGS = 100;
|
||||
|
||||
// Clase Lang
|
||||
class Lang {
|
||||
private:
|
||||
Asset *mAsset; // Objeto que gestiona todos los ficheros de recursos
|
||||
std::string mTextStrings[MAX_TEXT_STRINGS]; // Vector con los textos
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
Lang(Asset *mAsset);
|
||||
|
||||
// Destructor
|
||||
~Lang();
|
||||
|
||||
// Inicializa los textos del juego en el idioma seleccionado
|
||||
bool setLang(Uint8 lang);
|
||||
|
||||
// Obtiene la cadena de texto del indice
|
||||
std::string getText(int index);
|
||||
};
|
||||
Reference in New Issue
Block a user