canviat Options de struct a namespace

This commit is contained in:
2025-10-26 14:01:08 +01:00
parent 8f49e442de
commit df4965a84b
59 changed files with 1470 additions and 1533 deletions

View File

@@ -1,61 +1,52 @@
#include "core/resources/asset.hpp"
#include <algorithm> // Para find_if, max
#include <fstream> // Para basic_ostream, operator<<, basic_ifstream, endl
#include <iostream> // Para cout
#include <string> // Para allocator, char_traits, string, operator+, oper...
#include "utils/utils.hpp" // Para getFileName, printWithDots
#include <algorithm> // Para find_if, max
#include <fstream> // Para basic_ostream, operator<<, basic_ifstream, endl
#include <iostream> // Para cout
#include <string> // Para allocator, char_traits, string, operator+, oper...
#include "utils/utils.hpp" // Para getFileName, printWithDots
// [SINGLETON] Hay que definir las variables estáticas, desde el .h sólo la hemos declarado
Asset *Asset::asset_ = nullptr;
Asset* Asset::asset_ = nullptr;
// [SINGLETON] Crearemos el objeto asset con esta función estática
void Asset::init(const std::string &executable_path)
{
void Asset::init(const std::string& executable_path) {
Asset::asset_ = new Asset(executable_path);
}
// [SINGLETON] Destruiremos el objeto asset con esta función estática
void Asset::destroy()
{
void Asset::destroy() {
delete Asset::asset_;
}
// [SINGLETON] Con este método obtenemos el objeto asset y podemos trabajar con él
Asset *Asset::get()
{
Asset* Asset::get() {
return Asset::asset_;
}
// Añade un elemento a la lista
void Asset::add(const std::string &file, AssetType type, bool required, bool absolute)
{
void Asset::add(const std::string& file, AssetType type, bool required, bool absolute) {
file_list_.emplace_back(absolute ? file : executable_path_ + file, type, required);
longest_name_ = std::max(longest_name_, static_cast<int>(file_list_.back().file.size()));
}
// Devuelve la ruta completa a un fichero a partir de una cadena
std::string Asset::get(const std::string &text) const
{
auto it = std::find_if(file_list_.begin(), file_list_.end(),
[&text](const auto &f)
{
return getFileName(f.file) == text;
});
std::string Asset::get(const std::string& text) const {
auto it = std::find_if(file_list_.begin(), file_list_.end(), [&text](const auto& f) {
return getFileName(f.file) == text;
});
if (it != file_list_.end())
{
if (it != file_list_.end()) {
return it->file;
}
else
{
} else {
std::cout << "Warning: file " << text << " not found" << std::endl;
return "";
}
}
// Comprueba que existen todos los elementos
bool Asset::check() const
{
bool Asset::check() const {
bool success = true;
std::cout << "\n** CHECKING FILES" << std::endl;
@@ -64,28 +55,22 @@ bool Asset::check() const
// std::cout << "Sample filepath: " << file_list_.back().file << std::endl;
// Comprueba la lista de ficheros clasificandolos por tipo
for (int type = 0; type < static_cast<int>(AssetType::MAX_ASSET_TYPE); ++type)
{
for (int type = 0; type < static_cast<int>(AssetType::MAX_ASSET_TYPE); ++type) {
// Comprueba si hay ficheros de ese tipo
bool any = false;
for (const auto &f : file_list_)
{
if (f.required && f.type == static_cast<AssetType>(type))
{
for (const auto& f : file_list_) {
if (f.required && f.type == static_cast<AssetType>(type)) {
any = true;
}
}
// Si hay ficheros de ese tipo, comprueba si existen
if (any)
{
if (any) {
std::cout << "\n>> " << getTypeName(static_cast<AssetType>(type)).c_str() << " FILES" << std::endl;
for (const auto &f : file_list_)
{
if (f.required && f.type == static_cast<AssetType>(type))
{
for (const auto& f : file_list_) {
if (f.required && f.type == static_cast<AssetType>(type)) {
success &= checkFile(f.file);
}
}
@@ -101,14 +86,12 @@ bool Asset::check() const
}
// Comprueba que existe un fichero
bool Asset::checkFile(const std::string &path) const
{
bool Asset::checkFile(const std::string& path) const {
std::ifstream file(path);
bool success = file.good();
file.close();
if (!success)
{
if (!success) {
printWithDots("Checking file : ", getFileName(path), "[ ERROR ]");
}
@@ -116,61 +99,56 @@ bool Asset::checkFile(const std::string &path) const
}
// Devuelve el nombre del tipo de recurso
std::string Asset::getTypeName(AssetType type) const
{
switch (type)
{
case AssetType::DATA:
return "DATA";
break;
std::string Asset::getTypeName(AssetType type) const {
switch (type) {
case AssetType::DATA:
return "DATA";
break;
case AssetType::BITMAP:
return "BITMAP";
break;
case AssetType::BITMAP:
return "BITMAP";
break;
case AssetType::ANIMATION:
return "ANIMATION";
break;
case AssetType::ANIMATION:
return "ANIMATION";
break;
case AssetType::MUSIC:
return "MUSIC";
break;
case AssetType::MUSIC:
return "MUSIC";
break;
case AssetType::SOUND:
return "SOUND";
break;
case AssetType::SOUND:
return "SOUND";
break;
case AssetType::FONT:
return "FONT";
break;
case AssetType::FONT:
return "FONT";
break;
case AssetType::ROOM:
return "ROOM";
break;
case AssetType::ROOM:
return "ROOM";
break;
case AssetType::TILEMAP:
return "TILEMAP";
break;
case AssetType::TILEMAP:
return "TILEMAP";
break;
case AssetType::PALETTE:
return "PALETTE";
break;
case AssetType::PALETTE:
return "PALETTE";
break;
default:
return "ERROR";
break;
default:
return "ERROR";
break;
}
}
// Devuelve la lista de recursos de un tipo
std::vector<std::string> Asset::getListByType(AssetType type) const
{
std::vector<std::string> Asset::getListByType(AssetType type) const {
std::vector<std::string> list;
for (auto f : file_list_)
{
if (f.type == type)
{
for (auto f : file_list_) {
if (f.type == type) {
list.push_back(f.file);
}
}

View File

@@ -1,79 +1,79 @@
#pragma once
#include <string> // para string, basic_string
#include <vector> // para vector
#include <string> // para string, basic_string
#include <vector> // para vector
#include "utils/utils.hpp"
enum class AssetType : int
{
DATA,
BITMAP,
ANIMATION,
MUSIC,
SOUND,
FONT,
ROOM,
TILEMAP,
PALETTE,
MAX_ASSET_TYPE
enum class AssetType : int {
DATA,
BITMAP,
ANIMATION,
MUSIC,
SOUND,
FONT,
ROOM,
TILEMAP,
PALETTE,
MAX_ASSET_TYPE
};
// Clase Asset
class Asset
{
private:
// [SINGLETON] Objeto asset privado para Don Melitón
static Asset *asset_;
class Asset {
private:
// [SINGLETON] Objeto asset privado para Don Melitón
static Asset* asset_;
// Estructura para definir un item
struct AssetItem
{
std::string file; // Ruta del fichero desde la raíz del directorio
AssetType type; // Indica el tipo de recurso
bool required; // Indica si es un fichero que debe de existir
// Estructura para definir un item
struct AssetItem {
std::string file; // Ruta del fichero desde la raíz del directorio
AssetType type; // Indica el tipo de recurso
bool required; // Indica si es un fichero que debe de existir
// Constructor
AssetItem(const std::string &filePath, AssetType assetType, bool isRequired)
: file(filePath), type(assetType), required(isRequired) {}
};
// Constructor
AssetItem(const std::string& filePath, AssetType assetType, bool isRequired)
: file(filePath),
type(assetType),
required(isRequired) {}
};
// Variables
int longest_name_ = 0; // Contiene la longitud del nombre de fichero mas largo
std::vector<AssetItem> file_list_; // Listado con todas las rutas a los ficheros
std::string executable_path_; // Ruta al ejecutable
// Variables
int longest_name_ = 0; // Contiene la longitud del nombre de fichero mas largo
std::vector<AssetItem> file_list_; // Listado con todas las rutas a los ficheros
std::string executable_path_; // Ruta al ejecutable
// Comprueba que existe un fichero
bool checkFile(const std::string &path) const;
// Comprueba que existe un fichero
bool checkFile(const std::string& path) const;
// Devuelve el nombre del tipo de recurso
std::string getTypeName(AssetType type) const;
// Devuelve el nombre del tipo de recurso
std::string getTypeName(AssetType type) const;
// Constructor
explicit Asset(const std::string &executable_path)
: executable_path_(getPath(executable_path)) {}
// Constructor
explicit Asset(const std::string& executable_path)
: executable_path_(getPath(executable_path)) {}
// Destructor
~Asset() = default;
// Destructor
~Asset() = default;
public:
// [SINGLETON] Crearemos el objeto con esta función estática
static void init(const std::string &executable_path);
public:
// [SINGLETON] Crearemos el objeto con esta función estática
static void init(const std::string& executable_path);
// [SINGLETON] Destruiremos el objeto con esta función estática
static void destroy();
// [SINGLETON] Destruiremos el objeto con esta función estática
static void destroy();
// [SINGLETON] Con este método obtenemos el objeto y podemos trabajar con él
static Asset *get();
// [SINGLETON] Con este método obtenemos el objeto y podemos trabajar con él
static Asset* get();
// Añade un elemento a la lista
void add(const std::string &file, AssetType type, bool required = true, bool absolute = false);
// Añade un elemento a la lista
void add(const std::string& file, AssetType type, bool required = true, bool absolute = false);
// Devuelve la ruta completa a un fichero a partir de una cadena
std::string get(const std::string &text) const;
// Devuelve la ruta completa a un fichero a partir de una cadena
std::string get(const std::string& text) const;
// Comprueba que existen todos los elementos
bool check() const;
// Comprueba que existen todos los elementos
bool check() const;
// Devuelve la lista de recursos de un tipo
std::vector<std::string> getListByType(AssetType type) const;
// Devuelve la lista de recursos de un tipo
std::vector<std::string> getListByType(AssetType type) const;
};

View File

@@ -7,15 +7,15 @@
#include <iostream> // Para basic_ostream, operator<<, endl, cout
#include <stdexcept> // Para runtime_error
#include "core/resources/asset.hpp" // Para AssetType, Asset
#include "external/jail_audio.h" // Para JA_DeleteMusic, JA_DeleteSound, JA_Loa...
#include "game/gameplay/options.hpp" // Para Options, OptionsGame, options
#include "game/gameplay/room.hpp" // Para RoomData, loadRoomFile, loadRoomTileFile
#include "core/rendering/screen.hpp" // Para Screen
#include "core/rendering/text.hpp" // Para Text, loadTextFile
#include "utils/utils.hpp" // Para getFileName, printWithDots, PaletteColor
struct JA_Music_t; // lines 17-17
struct JA_Sound_t; // lines 18-18
#include "core/rendering/screen.hpp" // Para Screen
#include "core/rendering/text.hpp" // Para Text, loadTextFile
#include "core/resources/asset.hpp" // Para AssetType, Asset
#include "external/jail_audio.h" // Para JA_DeleteMusic, JA_DeleteSound, JA_Loa...
#include "game/gameplay/options.hpp" // Para Options, OptionsGame, options
#include "game/gameplay/room.hpp" // Para RoomData, loadRoomFile, loadRoomTileFile
#include "utils/utils.hpp" // Para getFileName, printWithDots, PaletteColor
struct JA_Music_t; // lines 17-17
struct JA_Sound_t; // lines 18-18
// [SINGLETON] Hay que definir las variables estáticas, desde el .h sólo la hemos declarado
Resource* Resource::resource_ = nullptr;
@@ -375,12 +375,12 @@ void Resource::renderProgress() {
constexpr float X_PADDING = 10;
constexpr float Y_PADDING = 10;
constexpr float BAR_HEIGHT = 10;
const float bar_position = options.game.height - BAR_HEIGHT - Y_PADDING;
const float bar_position = Options::game.height - BAR_HEIGHT - Y_PADDING;
Screen::get()->start();
Screen::get()->clearSurface(static_cast<Uint8>(PaletteColor::BLACK));
auto surface = Screen::get()->getRendererSurface();
const float WIRED_BAR_WIDTH = options.game.width - (X_PADDING * 2);
const float WIRED_BAR_WIDTH = Options::game.width - (X_PADDING * 2);
SDL_FRect rect_wired = {X_PADDING, bar_position, WIRED_BAR_WIDTH, X_PADDING};
surface->drawRectBorder(&rect_wired, static_cast<Uint8>(PaletteColor::WHITE));

View File

@@ -4,12 +4,12 @@
#include <string> // Para string
#include <vector> // Para vector
#include "game/gameplay/room.hpp" // Para room_t
#include "core/rendering/surface.hpp" // Para Surface
#include "core/rendering/surface_animated_sprite.hpp" // Para AnimationsFileBuffer
#include "core/rendering/surface.hpp" // Para Surface
#include "core/rendering/text.hpp" // Para Text, TextFile
struct JA_Music_t; // lines 11-11
struct JA_Sound_t; // lines 12-12
#include "core/rendering/text.hpp" // Para Text, TextFile
#include "game/gameplay/room.hpp" // Para room_t
struct JA_Music_t; // lines 11-11
struct JA_Sound_t; // lines 12-12
// Estructura para almacenar ficheros de sonido y su nombre
struct ResourceSound {