neteja tidy a source/core i encamina Texture::loadFromFile pel ResourceHelper

This commit is contained in:
2026-05-14 20:22:54 +02:00
parent 88fa3f296f
commit 1912200b21
40 changed files with 699 additions and 578 deletions
+59 -31
View File
@@ -17,14 +17,16 @@
Resource *Resource::instance_ = nullptr;
static std::string basename(const std::string &path) {
static auto basename(const std::string &path) -> std::string {
return path.substr(path.find_last_of("\\/") + 1);
}
static std::string stem(const std::string &path) {
static auto stem(const std::string &path) -> std::string {
std::string b = basename(path);
size_t dot = b.find_last_of('.');
if (dot == std::string::npos) return b;
if (dot == std::string::npos) {
return b;
}
return b.substr(0, dot);
}
@@ -40,7 +42,7 @@ void Resource::destroy() {
instance_ = nullptr;
}
Resource *Resource::get() {
auto Resource::get() -> Resource * {
return instance_;
}
@@ -48,19 +50,29 @@ Resource::Resource(SDL_Renderer *renderer)
: renderer_(renderer) {}
Resource::~Resource() {
for (auto &[name, m] : menus_) delete m;
for (auto &[name, m] : menus_) {
delete m;
}
menus_.clear();
for (auto &[name, t] : texts_) delete t;
for (auto &[name, t] : texts_) {
delete t;
}
texts_.clear();
for (auto &[name, t] : textures_) delete t;
for (auto &[name, t] : textures_) {
delete t;
}
textures_.clear();
for (auto &[name, s] : sounds_) JA_DeleteSound(s);
for (auto &[name, s] : sounds_) {
JA_DeleteSound(s);
}
sounds_.clear();
for (auto &[name, m] : musics_) JA_DeleteMusic(m);
for (auto &[name, m] : musics_) {
JA_DeleteMusic(m);
}
musics_.clear();
}
@@ -74,7 +86,9 @@ void Resource::preloadAll() {
continue;
}
auto bytes = ResourceHelper::loadFile(it.file);
if (bytes.empty()) continue;
if (bytes.empty()) {
continue;
}
const std::string bname = basename(it.file);
@@ -86,12 +100,16 @@ void Resource::preloadAll() {
}
case t_sound: {
JA_Sound_t *s = JA_LoadSound(bytes.data(), (uint32_t)bytes.size());
if (s) sounds_[bname] = s;
if (s != nullptr) {
sounds_[bname] = s;
}
break;
}
case t_music: {
JA_Music_t *m = JA_LoadMusic(bytes.data(), (Uint32)bytes.size());
if (m) musics_[bname] = m;
if (m != nullptr) {
musics_[bname] = m;
}
break;
}
case t_data: {
@@ -103,7 +121,9 @@ void Resource::preloadAll() {
while (std::getline(ss, line)) {
// Normalitza CRLF perquè loadFromVector compari línies amb literals
// ("[animation]", "[/animation]") sense \r residual.
if (!line.empty() && line.back() == '\r') line.pop_back();
if (!line.empty() && line.back() == '\r') {
line.pop_back();
}
lines.push_back(line);
}
animationLines_[bname] = std::move(lines);
@@ -114,12 +134,8 @@ void Resource::preloadAll() {
}
break;
}
case t_font:
// Fonts: se emparejan en pass 2
break;
case t_lang:
// Lenguaje: lo sigue leyendo la clase Lang a través de ResourceHelper
break;
case t_font: // Fonts: se emparejan en pass 2
case t_lang: // Lenguaje: lo sigue leyendo la clase Lang via ResourceHelper
default:
break;
}
@@ -131,9 +147,13 @@ void Resource::preloadAll() {
std::unordered_map<std::string, std::vector<uint8_t>> fontPngs;
std::unordered_map<std::string, std::vector<uint8_t>> fontTxts;
for (const auto &it : items) {
if (it.type != t_font) continue;
if (it.type != t_font) {
continue;
}
auto bytes = ResourceHelper::loadFile(it.file);
if (bytes.empty()) continue;
if (bytes.empty()) {
continue;
}
const std::string s = stem(it.file);
const std::string bname = basename(it.file);
if (bname.size() >= 4 && bname.substr(bname.size() - 4) == ".png") {
@@ -144,7 +164,9 @@ void Resource::preloadAll() {
}
for (const auto &[s, png] : fontPngs) {
auto itTxt = fontTxts.find(s);
if (itTxt == fontTxts.end()) continue;
if (itTxt == fontTxts.end()) {
continue;
}
Text *t = new Text(png, itTxt->second, renderer_);
texts_[s] = t;
}
@@ -154,11 +176,17 @@ void Resource::preloadAll() {
// requiere que Menu se adapte a cargar desde ResourceHelper. Por ahora
// lo dejamos así y será una migración del paso 7.
for (const auto &it : items) {
if (it.type != t_data) continue;
if (it.type != t_data) {
continue;
}
const std::string bname = basename(it.file);
if (bname.size() < 4 || bname.substr(bname.size() - 4) != ".men") continue;
if (bname.size() < 4 || bname.substr(bname.size() - 4) != ".men") {
continue;
}
auto bytes = ResourceHelper::loadFile(it.file);
if (bytes.empty()) continue;
if (bytes.empty()) {
continue;
}
Menu *m = new Menu(renderer_, "");
m->loadFromBytes(bytes, bname);
const std::string s = stem(it.file);
@@ -166,7 +194,7 @@ void Resource::preloadAll() {
}
}
Texture *Resource::getTexture(const std::string &name) {
auto Resource::getTexture(const std::string &name) -> Texture * {
auto it = textures_.find(name);
if (it == textures_.end()) {
std::cerr << "Resource::getTexture: missing " << name << '\n';
@@ -175,7 +203,7 @@ Texture *Resource::getTexture(const std::string &name) {
return it->second;
}
JA_Sound_t *Resource::getSound(const std::string &name) {
auto Resource::getSound(const std::string &name) -> JA_Sound_t * {
auto it = sounds_.find(name);
if (it == sounds_.end()) {
std::cerr << "Resource::getSound: missing " << name << '\n';
@@ -184,7 +212,7 @@ JA_Sound_t *Resource::getSound(const std::string &name) {
return it->second;
}
JA_Music_t *Resource::getMusic(const std::string &name) {
auto Resource::getMusic(const std::string &name) -> JA_Music_t * {
auto it = musics_.find(name);
if (it == musics_.end()) {
std::cerr << "Resource::getMusic: missing " << name << '\n';
@@ -193,7 +221,7 @@ JA_Music_t *Resource::getMusic(const std::string &name) {
return it->second;
}
std::vector<std::string> &Resource::getAnimationLines(const std::string &name) {
auto Resource::getAnimationLines(const std::string &name) -> std::vector<std::string> & {
auto it = animationLines_.find(name);
if (it == animationLines_.end()) {
static std::vector<std::string> empty;
@@ -203,7 +231,7 @@ std::vector<std::string> &Resource::getAnimationLines(const std::string &name) {
return it->second;
}
Text *Resource::getText(const std::string &name) {
auto Resource::getText(const std::string &name) -> Text * {
auto it = texts_.find(name);
if (it == texts_.end()) {
std::cerr << "Resource::getText: missing " << name << '\n';
@@ -212,7 +240,7 @@ Text *Resource::getText(const std::string &name) {
return it->second;
}
Menu *Resource::getMenu(const std::string &name) {
auto Resource::getMenu(const std::string &name) -> Menu * {
auto it = menus_.find(name);
if (it == menus_.end()) {
std::cerr << "Resource::getMenu: missing " << name << '\n';