Files
coffee_crisis/source/lang.cpp
2026-04-15 23:26:43 +02:00

69 lines
1.6 KiB
C++

#include "lang.h"
#include <fstream> // for basic_ifstream, basic_istream, ifstream
#include <sstream>
#include "asset.h" // for Asset
#include "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];
}