Redistribuits els .cpp en carpetes

Actualitzat cmake
Modificats els include de SDL2 a SDL3
This commit is contained in:
2025-10-15 08:28:57 +02:00
parent c3415fd106
commit 78c5333144
86 changed files with 6757 additions and 7610 deletions

View File

@@ -1,24 +1,27 @@
#include "resource.h"
#include <SDL2/SDL_events.h> // Para SDL_PollEvent, SDL_Event, SDL_KEYDOWN
#include <SDL2/SDL_keycode.h> // Para SDLK_ESCAPE
#include <SDL2/SDL_rect.h> // Para SDL_Rect
#include <SDL2/SDL_stdinc.h> // Para Uint8
#include <stdlib.h> // Para exit, size_t
#include <algorithm> // Para find_if
#include <iostream> // Para basic_ostream, operator<<, endl, cout
#include <stdexcept> // Para runtime_error
#include "asset.h" // Para AssetType, Asset
#include "jail_audio.h" // Para JA_DeleteMusic, JA_DeleteSound, JA_Loa...
#include "options.h" // Para Options, OptionsGame, options
#include "room.h" // Para RoomData, loadRoomFile, loadRoomTileFile
#include "screen.h" // Para Screen
#include "text.h" // Para Text, loadTextFile
#include "utils.h" // Para getFileName, printWithDots, PaletteColor
struct JA_Music_t; // lines 17-17
struct JA_Sound_t; // lines 18-18
#include <SDL3/SDL_events.h> // Para SDL_PollEvent, SDL_Event, SDL_KEYDOWN
#include <SDL3/SDL_keycode.h> // Para SDLK_ESCAPE
#include <SDL3/SDL_rect.h> // Para SDL_Rect
#include <SDL3/SDL_stdinc.h> // Para Uint8
#include <stdlib.h> // Para exit, size_t
#include <algorithm> // Para find_if
#include <iostream> // Para basic_ostream, operator<<, endl, cout
#include <stdexcept> // Para runtime_error
#include "asset.h" // Para AssetType, Asset
#include "jail_audio.h" // Para JA_DeleteMusic, JA_DeleteSound, JA_Loa...
#include "options.h" // Para Options, OptionsGame, options
#include "room.h" // Para RoomData, loadRoomFile, loadRoomTileFile
#include "screen.h" // Para Screen
#include "text.h" // Para Text, loadTextFile
#include "utils.h" // 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;
Resource* Resource::resource_ = nullptr;
// [SINGLETON] Crearemos el objeto screen con esta función estática
void Resource::init() { Resource::resource_ = new Resource(); }
@@ -27,14 +30,13 @@ void Resource::init() { Resource::resource_ = new Resource(); }
void Resource::destroy() { delete Resource::resource_; }
// [SINGLETON] Con este método obtenemos el objeto screen y podemos trabajar con él
Resource *Resource::get() { return Resource::resource_; }
Resource* Resource::get() { return Resource::resource_; }
// Constructor
Resource::Resource() { load(); }
// Vacia todos los vectores de recursos
void Resource::clear()
{
void Resource::clear() {
clearSounds();
clearMusics();
surfaces_.clear();
@@ -45,8 +47,7 @@ void Resource::clear()
}
// Carga todos los recursos
void Resource::load()
{
void Resource::load() {
calculateTotal();
Screen::get()->show();
Screen::get()->setBorderColor(static_cast<Uint8>(PaletteColor::BLACK));
@@ -64,20 +65,16 @@ void Resource::load()
}
// Recarga todos los recursos
void Resource::reload()
{
void Resource::reload() {
clear();
load();
}
// Obtiene el sonido a partir de un nombre
JA_Sound_t *Resource::getSound(const std::string &name)
{
auto it = std::find_if(sounds_.begin(), sounds_.end(), [&name](const auto &s)
{ return s.name == name; });
JA_Sound_t* Resource::getSound(const std::string& name) {
auto it = std::find_if(sounds_.begin(), sounds_.end(), [&name](const auto& s) { return s.name == name; });
if (it != sounds_.end())
{
if (it != sounds_.end()) {
return it->sound;
}
@@ -86,13 +83,10 @@ JA_Sound_t *Resource::getSound(const std::string &name)
}
// Obtiene la música a partir de un nombre
JA_Music_t *Resource::getMusic(const std::string &name)
{
auto it = std::find_if(musics_.begin(), musics_.end(), [&name](const auto &m)
{ return m.name == name; });
JA_Music_t* Resource::getMusic(const std::string& name) {
auto it = std::find_if(musics_.begin(), musics_.end(), [&name](const auto& m) { return m.name == name; });
if (it != musics_.end())
{
if (it != musics_.end()) {
return it->music;
}
@@ -101,13 +95,10 @@ JA_Music_t *Resource::getMusic(const std::string &name)
}
// Obtiene la surface a partir de un nombre
std::shared_ptr<Surface> Resource::getSurface(const std::string &name)
{
auto it = std::find_if(surfaces_.begin(), surfaces_.end(), [&name](const auto &t)
{ return t.name == name; });
std::shared_ptr<Surface> Resource::getSurface(const std::string& name) {
auto it = std::find_if(surfaces_.begin(), surfaces_.end(), [&name](const auto& t) { return t.name == name; });
if (it != surfaces_.end())
{
if (it != surfaces_.end()) {
return it->surface;
}
@@ -116,13 +107,10 @@ std::shared_ptr<Surface> Resource::getSurface(const std::string &name)
}
// Obtiene la paleta a partir de un nombre
Palette Resource::getPalette(const std::string &name)
{
auto it = std::find_if(palettes_.begin(), palettes_.end(), [&name](const auto &t)
{ return t.name == name; });
Palette Resource::getPalette(const std::string& name) {
auto it = std::find_if(palettes_.begin(), palettes_.end(), [&name](const auto& t) { return t.name == name; });
if (it != palettes_.end())
{
if (it != palettes_.end()) {
return it->palette;
}
@@ -131,13 +119,10 @@ Palette Resource::getPalette(const std::string &name)
}
// Obtiene el fichero de texto a partir de un nombre
std::shared_ptr<TextFile> Resource::getTextFile(const std::string &name)
{
auto it = std::find_if(text_files_.begin(), text_files_.end(), [&name](const auto &t)
{ return t.name == name; });
std::shared_ptr<TextFile> Resource::getTextFile(const std::string& name) {
auto it = std::find_if(text_files_.begin(), text_files_.end(), [&name](const auto& t) { return t.name == name; });
if (it != text_files_.end())
{
if (it != text_files_.end()) {
return it->text_file;
}
@@ -146,13 +131,10 @@ std::shared_ptr<TextFile> Resource::getTextFile(const std::string &name)
}
// Obtiene el objeto de texto a partir de un nombre
std::shared_ptr<Text> Resource::getText(const std::string &name)
{
auto it = std::find_if(texts_.begin(), texts_.end(), [&name](const auto &t)
{ return t.name == name; });
std::shared_ptr<Text> Resource::getText(const std::string& name) {
auto it = std::find_if(texts_.begin(), texts_.end(), [&name](const auto& t) { return t.name == name; });
if (it != texts_.end())
{
if (it != texts_.end()) {
return it->text;
}
@@ -161,13 +143,10 @@ std::shared_ptr<Text> Resource::getText(const std::string &name)
}
// Obtiene la animación a partir de un nombre
Animations &Resource::getAnimations(const std::string &name)
{
auto it = std::find_if(animations_.begin(), animations_.end(), [&name](const auto &a)
{ return a.name == name; });
Animations& Resource::getAnimations(const std::string& name) {
auto it = std::find_if(animations_.begin(), animations_.end(), [&name](const auto& a) { return a.name == name; });
if (it != animations_.end())
{
if (it != animations_.end()) {
return it->animation;
}
@@ -176,13 +155,10 @@ Animations &Resource::getAnimations(const std::string &name)
}
// Obtiene el mapa de tiles a partir de un nombre
std::vector<int> &Resource::getTileMap(const std::string &name)
{
auto it = std::find_if(tile_maps_.begin(), tile_maps_.end(), [&name](const auto &t)
{ return t.name == name; });
std::vector<int>& Resource::getTileMap(const std::string& name) {
auto it = std::find_if(tile_maps_.begin(), tile_maps_.end(), [&name](const auto& t) { return t.name == name; });
if (it != tile_maps_.end())
{
if (it != tile_maps_.end()) {
return it->tileMap;
}
@@ -191,13 +167,10 @@ std::vector<int> &Resource::getTileMap(const std::string &name)
}
// Obtiene la habitación a partir de un nombre
std::shared_ptr<RoomData> Resource::getRoom(const std::string &name)
{
auto it = std::find_if(rooms_.begin(), rooms_.end(), [&name](const auto &r)
{ return r.name == name; });
std::shared_ptr<RoomData> Resource::getRoom(const std::string& name) {
auto it = std::find_if(rooms_.begin(), rooms_.end(), [&name](const auto& r) { return r.name == name; });
if (it != rooms_.end())
{
if (it != rooms_.end()) {
return it->room;
}
@@ -206,20 +179,17 @@ std::shared_ptr<RoomData> Resource::getRoom(const std::string &name)
}
// Obtiene todas las habitaciones
std::vector<ResourceRoom> &Resource::getRooms()
{
std::vector<ResourceRoom>& Resource::getRooms() {
return rooms_;
}
// Carga los sonidos
void Resource::loadSounds()
{
void Resource::loadSounds() {
std::cout << "\n>> SOUND FILES" << std::endl;
auto list = Asset::get()->getListByType(AssetType::SOUND);
sounds_.clear();
for (const auto &l : list)
{
for (const auto& l : list) {
auto name = getFileName(l);
sounds_.emplace_back(ResourceSound(name, JA_LoadSound(l.c_str())));
printWithDots("Sound : ", name, "[ LOADED ]");
@@ -228,14 +198,12 @@ void Resource::loadSounds()
}
// Carga las musicas
void Resource::loadMusics()
{
void Resource::loadMusics() {
std::cout << "\n>> MUSIC FILES" << std::endl;
auto list = Asset::get()->getListByType(AssetType::MUSIC);
musics_.clear();
for (const auto &l : list)
{
for (const auto& l : list) {
auto name = getFileName(l);
musics_.emplace_back(ResourceMusic(name, JA_LoadMusic(l.c_str())));
printWithDots("Music : ", name, "[ LOADED ]");
@@ -244,14 +212,12 @@ void Resource::loadMusics()
}
// Carga las texturas
void Resource::loadSurfaces()
{
void Resource::loadSurfaces() {
std::cout << "\n>> SURFACES" << std::endl;
auto list = Asset::get()->getListByType(AssetType::BITMAP);
surfaces_.clear();
for (const auto &l : list)
{
for (const auto& l : list) {
auto name = getFileName(l);
surfaces_.emplace_back(ResourceSurface(name, std::make_shared<Surface>(l)));
surfaces_.back().surface->setTransparentColor(0);
@@ -269,14 +235,12 @@ void Resource::loadSurfaces()
}
// Carga las paletas
void Resource::loadPalettes()
{
void Resource::loadPalettes() {
std::cout << "\n>> PALETTES" << std::endl;
auto list = Asset::get()->getListByType(AssetType::PALETTE);
palettes_.clear();
for (const auto &l : list)
{
for (const auto& l : list) {
auto name = getFileName(l);
palettes_.emplace_back(ResourcePalette(name, readPalFile(l)));
updateLoadingProgress();
@@ -284,14 +248,12 @@ void Resource::loadPalettes()
}
// Carga los ficheros de texto
void Resource::loadTextFiles()
{
void Resource::loadTextFiles() {
std::cout << "\n>> TEXT FILES" << std::endl;
auto list = Asset::get()->getListByType(AssetType::FONT);
text_files_.clear();
for (const auto &l : list)
{
for (const auto& l : list) {
auto name = getFileName(l);
text_files_.emplace_back(ResourceTextFile(name, loadTextFile(l)));
updateLoadingProgress();
@@ -299,14 +261,12 @@ void Resource::loadTextFiles()
}
// Carga las animaciones
void Resource::loadAnimations()
{
void Resource::loadAnimations() {
std::cout << "\n>> ANIMATIONS" << std::endl;
auto list = Asset::get()->getListByType(AssetType::ANIMATION);
animations_.clear();
for (const auto &l : list)
{
for (const auto& l : list) {
auto name = getFileName(l);
animations_.emplace_back(ResourceAnimation(name, loadAnimationsFromFile(l)));
updateLoadingProgress();
@@ -314,14 +274,12 @@ void Resource::loadAnimations()
}
// Carga los mapas de tiles
void Resource::loadTileMaps()
{
void Resource::loadTileMaps() {
std::cout << "\n>> TILE MAPS" << std::endl;
auto list = Asset::get()->getListByType(AssetType::TILEMAP);
tile_maps_.clear();
for (const auto &l : list)
{
for (const auto& l : list) {
auto name = getFileName(l);
tile_maps_.emplace_back(ResourceTileMap(name, loadRoomTileFile(l)));
printWithDots("TileMap : ", name, "[ LOADED ]");
@@ -330,14 +288,12 @@ void Resource::loadTileMaps()
}
// Carga las habitaciones
void Resource::loadRooms()
{
void Resource::loadRooms() {
std::cout << "\n>> ROOMS" << std::endl;
auto list = Asset::get()->getListByType(AssetType::ROOM);
rooms_.clear();
for (const auto &l : list)
{
for (const auto& l : list) {
auto name = getFileName(l);
rooms_.emplace_back(ResourceRoom(name, std::make_shared<RoomData>(loadRoomFile(l))));
printWithDots("Room : ", name, "[ LOADED ]");
@@ -345,17 +301,17 @@ void Resource::loadRooms()
}
}
void Resource::createText()
{
struct ResourceInfo
{
std::string key; // Identificador del recurso
std::string textureFile; // Nombre del archivo de textura
std::string textFile; // Nombre del archivo de texto
void Resource::createText() {
struct ResourceInfo {
std::string key; // Identificador del recurso
std::string textureFile; // Nombre del archivo de textura
std::string textFile; // Nombre del archivo de texto
// Constructor para facilitar la creación de objetos ResourceInfo
ResourceInfo(const std::string &k, const std::string &tFile, const std::string &txtFile)
: key(k), textureFile(tFile), textFile(txtFile) {}
// Constructor para facilitar la creación de objetos ResourceInfo
ResourceInfo(const std::string& k, const std::string& tFile, const std::string& txtFile)
: key(k),
textureFile(tFile),
textFile(txtFile) {}
};
std::cout << "\n>> CREATING TEXT_OBJECTS" << std::endl;
@@ -367,48 +323,38 @@ void Resource::createText()
{"subatomic", "subatomic.gif", "subatomic.txt"},
{"8bithud", "8bithud.gif", "8bithud.txt"}};
for (const auto &resource : resources)
{
texts_.emplace_back(ResourceText(resource.key, std::make_shared<Text>(
getSurface(resource.textureFile),
getTextFile(resource.textFile))));
for (const auto& resource : resources) {
texts_.emplace_back(ResourceText(resource.key, std::make_shared<Text>(getSurface(resource.textureFile), getTextFile(resource.textFile))));
printWithDots("Text : ", resource.key, "[ DONE ]");
}
}
// Vacía el vector de sonidos
void Resource::clearSounds()
{
void Resource::clearSounds() {
// Itera sobre el vector y libera los recursos asociados a cada JA_Sound_t
for (auto &sound : sounds_)
{
if (sound.sound)
{
for (auto& sound : sounds_) {
if (sound.sound) {
JA_DeleteSound(sound.sound);
sound.sound = nullptr;
}
}
sounds_.clear(); // Limpia el vector después de liberar todos los recursos
sounds_.clear(); // Limpia el vector después de liberar todos los recursos
}
// Vacía el vector de musicas
void Resource::clearMusics()
{
void Resource::clearMusics() {
// Itera sobre el vector y libera los recursos asociados a cada JA_Music_t
for (auto &music : musics_)
{
if (music.music)
{
for (auto& music : musics_) {
if (music.music) {
JA_DeleteMusic(music.music);
music.music = nullptr;
}
}
musics_.clear(); // Limpia el vector después de liberar todos los recursos
musics_.clear(); // Limpia el vector después de liberar todos los recursos
}
// Calcula el numero de recursos para cargar
void Resource::calculateTotal()
{
void Resource::calculateTotal() {
std::vector<AssetType> assetTypes = {
AssetType::SOUND,
AssetType::MUSIC,
@@ -420,8 +366,7 @@ void Resource::calculateTotal()
AssetType::ROOM};
size_t total = 0;
for (const auto &assetType : assetTypes)
{
for (const auto& assetType : assetTypes) {
auto list = Asset::get()->getListByType(assetType);
total += list.size();
}
@@ -430,8 +375,7 @@ void Resource::calculateTotal()
}
// Muestra el progreso de carga
void Resource::renderProgress()
{
void Resource::renderProgress() {
constexpr int X_PADDING = 10;
constexpr int Y_PADDING = 10;
constexpr int BAR_HEIGHT = 10;
@@ -452,32 +396,26 @@ void Resource::renderProgress()
}
// Comprueba los eventos de la pantalla de carga
void Resource::checkEvents()
{
void Resource::checkEvents() {
SDL_Event event;
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
exit(0);
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_ESCAPE)
{
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
exit(0);
}
break;
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_ESCAPE) {
exit(0);
}
break;
}
}
}
// Actualiza el progreso de carga
void Resource::updateLoadingProgress(int steps)
{
void Resource::updateLoadingProgress(int steps) {
count_.add(1);
if (count_.loaded % steps == 0 || count_.loaded == count_.total)
{
if (count_.loaded % steps == 0 || count_.loaded == count_.total) {
renderProgress();
}
checkEvents();