neteja tidy a source/core/system i audio amb fixes d'arrel

This commit is contained in:
2026-05-14 21:02:43 +02:00
parent dc622c7bae
commit 0ee117135c
11 changed files with 145 additions and 115 deletions
+23 -18
View File
@@ -1,9 +1,10 @@
#include "core/system/director.h"
#include <SDL3/SDL.h>
#include <errno.h> // for errno, EEXIST, EACCES, ENAMETOO...
#include <stdio.h> // for printf, perror
#include <string.h> // for strcmp
#include <cerrno> // for errno, EEXIST, EACCES, ENAMETOO...
#include <cstdio> // for printf, perror
#include <cstring> // for strcmp
#ifndef __EMSCRIPTEN__
#include <sys/stat.h> // for mkdir, stat, S_IRWXU
#include <unistd.h> // for getuid
@@ -40,7 +41,7 @@
// Constructor
Director::Director(int argc, const char *argv[]) {
std::cout << "Game start" << std::endl;
std::cout << "Game start" << '\n';
// Inicializa variables
section = new section_t();
section->name = SECTION_PROG_LOGO;
@@ -97,7 +98,7 @@ Director::Director(int argc, const char *argv[]) {
const std::string pack_path = executablePath + "resources.pack";
#endif
if (!ResourceHelper::initializeResourceSystem(pack_path, enable_fallback)) {
std::cerr << "Fatal: resource system init failed (missing resources.pack?)" << std::endl;
std::cerr << "Fatal: resource system init failed (missing resources.pack?)" << '\n';
exit(EXIT_FAILURE);
}
}
@@ -197,7 +198,7 @@ Director::~Director() {
ResourceHelper::shutdownResourceSystem();
std::cout << "\nBye!" << std::endl;
std::cout << "\nBye!" << '\n';
}
// Inicializa el objeto input
@@ -256,14 +257,14 @@ void Director::initJailAudio() {
}
// Arranca SDL y crea la ventana
bool Director::initSDL() {
auto Director::initSDL() -> bool {
// Indicador de éxito
bool success = true;
// Inicializa SDL
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_GAMEPAD)) {
if (Options::settings.console) {
std::cout << "SDL could not initialize!\nSDL Error: " << SDL_GetError() << std::endl;
std::cout << "SDL could not initialize!\nSDL Error: " << SDL_GetError() << '\n';
}
success = false;
} else {
@@ -278,18 +279,18 @@ bool Director::initSDL() {
0);
if (window == nullptr) {
if (Options::settings.console) {
std::cout << "Window could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
std::cout << "Window could not be created!\nSDL Error: " << SDL_GetError() << '\n';
}
success = false;
} else {
SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
// Crea un renderizador para la ventana
renderer = SDL_CreateRenderer(window, NULL);
renderer = SDL_CreateRenderer(window, nullptr);
if (renderer == nullptr) {
if (Options::settings.console) {
std::cout << "Renderer could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
std::cout << "Renderer could not be created!\nSDL Error: " << SDL_GetError() << '\n';
}
success = false;
} else {
@@ -315,17 +316,17 @@ bool Director::initSDL() {
}
if (Options::settings.console) {
std::cout << std::endl;
std::cout << '\n';
}
return success;
}
// Crea el indice de ficheros
bool Director::setFileList() {
auto Director::setFileList() -> bool {
#ifdef MACOS_BUNDLE
const std::string prefix = "/../Resources";
#else
const std::string prefix = "";
const std::string prefix;
#endif
// Ficheros de configuración
@@ -488,7 +489,7 @@ void Director::createSystemFolder(const std::string &folder) {
// En Emscripten no necesitamos crear carpetas (MEMFS las crea automáticamente)
(void)folder;
#else
struct stat st = {0};
struct stat st = {.st_dev = 0};
if (stat(systemFolder.c_str(), &st) == -1) {
errno = 0;
#ifdef _WIN32
@@ -537,10 +538,14 @@ void Director::handleSectionTransition() {
case SECTION_PROG_GAME:
targetSection = ActiveSection::Game;
break;
default:
break;
}
// Si no ha cambiado, no hay nada que hacer
if (targetSection == activeSection) return;
if (targetSection == activeSection) {
return;
}
// Destruye la sección anterior
logo.reset();
@@ -571,7 +576,7 @@ void Director::handleSectionTransition() {
}
// Ejecuta un frame del juego
SDL_AppResult Director::iterate() {
auto Director::iterate() -> SDL_AppResult {
#ifdef __EMSCRIPTEN__
// En WASM no se puede salir: reinicia al logo
if (section->name == SECTION_PROG_QUIT) {
@@ -611,7 +616,7 @@ SDL_AppResult Director::iterate() {
}
// Procesa un evento
SDL_AppResult Director::handleEvent(SDL_Event *event) {
auto Director::handleEvent(SDL_Event *event) -> SDL_AppResult {
#ifndef __EMSCRIPTEN__
// Evento de salida de la aplicación
if (event->type == SDL_EVENT_QUIT) {
+10 -9
View File
@@ -2,6 +2,7 @@
#include <SDL3/SDL.h>
#include <cstdint> // for uint8_t
#include <memory>
#include <string> // for string, basic_string
class Game;
@@ -11,7 +12,7 @@ class Title;
struct section_t;
// Secciones activas del Director
enum class ActiveSection { None,
enum class ActiveSection : std::uint8_t { None,
Logo,
Intro,
Title,
@@ -36,19 +37,19 @@ class Director {
std::string systemFolder; // Carpeta del sistema donde guardar datos
// Inicializa jail_audio
void initJailAudio();
static void initJailAudio();
// Arranca SDL y crea la ventana
bool initSDL();
auto initSDL() -> bool;
// Inicializa el objeto input
void initInput();
static void initInput();
// Crea el indice de ficheros
bool setFileList();
auto setFileList() -> bool;
// Comprueba los parametros del programa
void checkProgramArguments(int argc, const char *argv[]);
static void checkProgramArguments(int argc, const char *argv[]);
// Crea la carpeta del sistema donde guardar datos
void createSystemFolder(const std::string &folder);
@@ -64,11 +65,11 @@ class Director {
~Director();
Director(const Director &) = delete;
Director &operator=(const Director &) = delete;
auto operator=(const Director &) -> Director & = delete;
// Ejecuta un frame del juego
SDL_AppResult iterate();
auto iterate() -> SDL_AppResult;
// Procesa un evento
SDL_AppResult handleEvent(SDL_Event *event);
auto handleEvent(SDL_Event *event) -> SDL_AppResult;
};