resource.pack
This commit is contained in:
135
source/menu.cpp
135
source/menu.cpp
@@ -7,6 +7,7 @@
|
||||
#include "asset.h" // for Asset
|
||||
#include "input.h" // for Input, REPEAT_FALSE, inputs_e
|
||||
#include "jail_audio.hpp" // for JA_LoadSound, JA_PlaySound, JA_DeleteSound
|
||||
#include "resource_helper.h"
|
||||
#include "text.h" // for Text
|
||||
|
||||
// Constructor
|
||||
@@ -61,16 +62,14 @@ Menu::Menu(SDL_Renderer *renderer, Asset *asset, Input *input, std::string file)
|
||||
selector.itemColor = {0, 0, 0};
|
||||
selector.a = 255;
|
||||
|
||||
// Inicializa las variables desde un fichero
|
||||
// Inicializa las variables desde un fichero. Si no se pasa fichero, el
|
||||
// llamante (p.ej. Resource::preloadAll) usará loadFromBytes después —
|
||||
// y ese método ya llama a setSelectorItemColors() y reset() al final.
|
||||
if (file != "") {
|
||||
load(file);
|
||||
setSelectorItemColors();
|
||||
reset();
|
||||
}
|
||||
|
||||
// Calcula los colores del selector para el degradado
|
||||
setSelectorItemColors();
|
||||
|
||||
// Deja el cursor en el primer elemento
|
||||
reset();
|
||||
}
|
||||
|
||||
Menu::~Menu() {
|
||||
@@ -95,82 +94,73 @@ Menu::~Menu() {
|
||||
}
|
||||
}
|
||||
|
||||
// Carga la configuración del menu desde un archivo de texto
|
||||
bool Menu::load(std::string file_path) {
|
||||
// Indicador de éxito en la carga
|
||||
// Parser compartido (recibe cualquier istream)
|
||||
bool Menu::parseFromStream(std::istream &file, const std::string &filename) {
|
||||
bool success = true;
|
||||
|
||||
// Indica si se ha creado ya el objeto de texto
|
||||
bool textAllocated = false;
|
||||
|
||||
const std::string filename = file_path.substr(file_path.find_last_of("\\/") + 1);
|
||||
std::string line;
|
||||
std::ifstream file(file_path);
|
||||
(void)filename;
|
||||
|
||||
// El fichero se puede abrir
|
||||
if (file.good()) {
|
||||
// Procesa el fichero linea a linea
|
||||
// std::cout << "Reading file " << filename.c_str() << std::endl;
|
||||
while (std::getline(file, line)) {
|
||||
if (line == "[item]") {
|
||||
item_t item;
|
||||
item.label = "";
|
||||
item.hPaddingDown = 1;
|
||||
item.selectable = true;
|
||||
item.greyed = false;
|
||||
item.linkedDown = false;
|
||||
item.visible = true;
|
||||
item.line = false;
|
||||
while (std::getline(file, line)) {
|
||||
if (line == "[item]") {
|
||||
item_t item;
|
||||
item.label = "";
|
||||
item.hPaddingDown = 1;
|
||||
item.selectable = true;
|
||||
item.greyed = false;
|
||||
item.linkedDown = false;
|
||||
item.visible = true;
|
||||
item.line = false;
|
||||
|
||||
do {
|
||||
// Lee la siguiente linea
|
||||
std::getline(file, line);
|
||||
|
||||
// Encuentra la posición del caracter '='
|
||||
int pos = line.find("=");
|
||||
|
||||
// Procesa las dos subcadenas
|
||||
if (!setItem(&item, line.substr(0, pos), line.substr(pos + 1, line.length()))) {
|
||||
// std::cout << "Warning: file " << filename.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << std::endl;
|
||||
success = false;
|
||||
}
|
||||
|
||||
} while (line != "[/item]");
|
||||
|
||||
addItem(item);
|
||||
}
|
||||
|
||||
// En caso contrario se parsea el fichero para buscar las variables y los valores
|
||||
else {
|
||||
// Encuentra la posición del caracter '='
|
||||
do {
|
||||
std::getline(file, line);
|
||||
int pos = line.find("=");
|
||||
// Procesa las dos subcadenas
|
||||
if (!setVars(line.substr(0, pos), line.substr(pos + 1, line.length()))) {
|
||||
// std::cout << "Warning: file " << filename.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << std::endl;
|
||||
if (!setItem(&item, line.substr(0, pos), line.substr(pos + 1, line.length()))) {
|
||||
success = false;
|
||||
}
|
||||
} while (line != "[/item]");
|
||||
|
||||
// Crea el objeto text tan pronto como se pueda. Necesario para añadir items
|
||||
if (font_png != "" && font_txt != "" && !textAllocated) {
|
||||
text = new Text(asset->get(font_png), asset->get(font_txt), renderer);
|
||||
textAllocated = true;
|
||||
}
|
||||
addItem(item);
|
||||
} else {
|
||||
int pos = line.find("=");
|
||||
if (!setVars(line.substr(0, pos), line.substr(pos + 1, line.length()))) {
|
||||
success = false;
|
||||
}
|
||||
|
||||
// Crea el objeto text tan pronto como se pueda. Necesario para añadir items.
|
||||
// Carga via ResourceHelper para que funcione tanto con pack como con filesystem.
|
||||
if (font_png != "" && font_txt != "" && !textAllocated) {
|
||||
auto pngBytes = ResourceHelper::loadFile(asset->get(font_png));
|
||||
auto txtBytes = ResourceHelper::loadFile(asset->get(font_txt));
|
||||
text = new Text(pngBytes, txtBytes, renderer);
|
||||
textAllocated = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Cierra el fichero
|
||||
// std::cout << "Closing file " << filename.c_str() << std::endl;
|
||||
file.close();
|
||||
}
|
||||
// El fichero no se puede abrir
|
||||
else {
|
||||
// std::cout << "Warning: Unable to open " << filename.c_str() << " file" << std::endl;
|
||||
success = false;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
// Carga la configuración del menu desde un archivo de texto
|
||||
bool Menu::load(std::string file_path) {
|
||||
const std::string filename = file_path.substr(file_path.find_last_of("\\/") + 1);
|
||||
std::ifstream file(file_path);
|
||||
if (!file.good()) {
|
||||
return false;
|
||||
}
|
||||
return parseFromStream(file, filename);
|
||||
}
|
||||
|
||||
// Carga el menu desde bytes en memoria
|
||||
bool Menu::loadFromBytes(const std::vector<uint8_t> &bytes, const std::string &nameForLogs) {
|
||||
if (bytes.empty()) return false;
|
||||
std::string content(reinterpret_cast<const char *>(bytes.data()), bytes.size());
|
||||
std::stringstream ss(content);
|
||||
bool ok = parseFromStream(ss, nameForLogs);
|
||||
setSelectorItemColors();
|
||||
reset();
|
||||
return ok;
|
||||
}
|
||||
|
||||
// Asigna variables a partir de dos cadenas
|
||||
bool Menu::setItem(item_t *item, std::string var, std::string value) {
|
||||
// Indicador de éxito en la asignación
|
||||
@@ -228,15 +218,18 @@ bool Menu::setVars(std::string var, std::string value) {
|
||||
}
|
||||
|
||||
else if (var == "sound_cancel") {
|
||||
soundCancel = JA_LoadSound(asset->get(value).c_str());
|
||||
auto bytes = ResourceHelper::loadFile(asset->get(value));
|
||||
if (!bytes.empty()) soundCancel = JA_LoadSound(bytes.data(), (uint32_t)bytes.size());
|
||||
}
|
||||
|
||||
else if (var == "sound_accept") {
|
||||
soundAccept = JA_LoadSound(asset->get(value).c_str());
|
||||
auto bytes = ResourceHelper::loadFile(asset->get(value));
|
||||
if (!bytes.empty()) soundAccept = JA_LoadSound(bytes.data(), (uint32_t)bytes.size());
|
||||
}
|
||||
|
||||
else if (var == "sound_move") {
|
||||
soundMove = JA_LoadSound(asset->get(value).c_str());
|
||||
auto bytes = ResourceHelper::loadFile(asset->get(value));
|
||||
if (!bytes.empty()) soundMove = JA_LoadSound(bytes.data(), (uint32_t)bytes.size());
|
||||
}
|
||||
|
||||
else if (var == "name") {
|
||||
|
||||
Reference in New Issue
Block a user