optimitzats els constructors per defecte en Options

This commit is contained in:
2025-10-31 23:46:07 +01:00
parent 5f802ed6ef
commit eb7d1bb014
4 changed files with 67 additions and 103 deletions

View File

@@ -42,6 +42,9 @@ Screen::Screen()
: palettes_(Asset::get()->getListByType(Asset::Type::PALETTE)) {
// Arranca SDL VIDEO, crea la ventana y el renderizador
initSDLVideo();
if (Options::video.fullscreen) {
SDL_HideCursor();
}
// Ajusta los tamaños
game_surface_dstrect_ = {.x = Options::video.border.width, .y = Options::video.border.height, .w = Options::game.width, .h = Options::game.height};
@@ -71,14 +74,17 @@ Screen::Screen()
}
SDL_SetTextureScaleMode(border_texture_, SDL_SCALEMODE_NEAREST);
// Cargar la paleta una sola vez
auto initial_palette = readPalFile(palettes_.at(current_palette_));
// Crea la surface donde se dibujan los graficos del juego
game_surface_ = std::make_shared<Surface>(Options::game.width, Options::game.height);
game_surface_->setPalette(readPalFile(palettes_.at(current_palette_)));
game_surface_->setPalette(initial_palette);
game_surface_->clear(static_cast<Uint8>(PaletteColor::BLACK));
// Crea la surface para el borde de colores
border_surface_ = std::make_shared<Surface>(Options::game.width + (Options::video.border.width * 2), Options::game.height + (Options::video.border.height * 2));
border_surface_->setPalette(readPalFile(palettes_.at(current_palette_)));
border_surface_->setPalette(initial_palette);
border_surface_->clear(border_color_);
// Establece la surface que actuará como renderer para recibir las llamadas a render()
@@ -243,7 +249,7 @@ void Screen::adjustRenderLogicalSize() {
}
// Establece el renderizador para las surfaces
void Screen::setRendererSurface(const std::shared_ptr<Surface> &surface) {
void Screen::setRendererSurface(const std::shared_ptr<Surface>& surface) {
(surface) ? renderer_surface_ = std::make_shared<std::shared_ptr<Surface>>(surface) : renderer_surface_ = std::make_shared<std::shared_ptr<Surface>>(game_surface_);
}
@@ -402,11 +408,9 @@ void Screen::loadShaders() {
// Si no existe versión ES, usar versión Desktop
vertex_file = "crtpi_vertex.glsl";
data = loadData(Asset::get()->get(vertex_file));
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
"Usando shaders OpenGL Desktop 3.3");
std::cout << "Usando shaders OpenGL Desktop 3.3\n";
} else {
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
"Usando shaders OpenGL ES 3.0 (Raspberry Pi)");
std::cout << "Usando shaders OpenGL ES 3.0 (Raspberry Pi)\n";
}
if (!data.empty()) {
@@ -444,8 +448,7 @@ void Screen::initShaders() {
#else
// En macOS, OpenGL está deprecated y rinde mal
// TODO: Implementar backend de Metal para shaders en macOS
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
"Shaders no disponibles en macOS (OpenGL deprecated). Usa Metal backend.");
std::cout << "WARNING: Shaders no disponibles en macOS (OpenGL deprecated). Usa Metal backend.\n";
#endif
}
@@ -459,7 +462,7 @@ void Screen::getDisplayInfo() {
SDL_DisplayID instance_id = displays[i];
const char* name = SDL_GetDisplayName(instance_id);
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Display %" SDL_PRIu32 ": %s", instance_id, (name != nullptr) ? name : "Unknown");
std::cout << "Display " << instance_id << ": " << ((name != nullptr) ? name : "Unknown") << '\n';
}
const auto* dm = SDL_GetCurrentDisplayMode(displays[0]);
@@ -476,9 +479,9 @@ void Screen::getDisplayInfo() {
Options::window.zoom = std::min(Options::window.zoom, Options::window.max_zoom);
// Muestra información sobre el tamaño de la pantalla y de la ventana de juego
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Current display mode: %dx%d @ %dHz", static_cast<int>(dm->w), static_cast<int>(dm->h), static_cast<int>(dm->refresh_rate));
std::cout << "Current display mode: " << static_cast<int>(dm->w) << "x" << static_cast<int>(dm->h) << " @ " << static_cast<int>(dm->refresh_rate) << "Hz\n";
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Window resolution: %dx%d x%d", static_cast<int>(Options::game.width), static_cast<int>(Options::game.height), Options::window.zoom);
std::cout << "Window resolution: " << static_cast<int>(Options::game.width) << "x" << static_cast<int>(Options::game.height) << " x" << Options::window.zoom << '\n';
Options::video.info = std::to_string(static_cast<int>(dm->w)) + "x" +
std::to_string(static_cast<int>(dm->h)) + " @ " +
@@ -498,9 +501,7 @@ void Screen::getDisplayInfo() {
auto Screen::initSDLVideo() -> bool {
// Inicializar SDL
if (!SDL_Init(SDL_INIT_VIDEO)) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"FATAL: Failed to initialize SDL_VIDEO! SDL Error: %s",
SDL_GetError());
std::cerr << "FATAL: Failed to initialize SDL_VIDEO! SDL Error: " << SDL_GetError() << '\n';
return false;
}
@@ -510,14 +511,12 @@ auto Screen::initSDLVideo() -> bool {
// Configurar hint para renderizado
#ifdef __APPLE__
if (!SDL_SetHint(SDL_HINT_RENDER_DRIVER, "metal")) {
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
"Warning: Failed to set Metal hint!");
std::cout << "WARNING: Failed to set Metal hint!\n";
}
#else
// Configurar hint de render driver
if (!SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl")) {
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
"Warning: Failed to set OpenGL hint!");
std::cout << "WARNING: Failed to set OpenGL hint!\n";
}
#ifdef _WIN32
@@ -525,10 +524,10 @@ auto Screen::initSDLVideo() -> bool {
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Solicitando OpenGL 3.3 Core Profile");
std::cout << "Solicitando OpenGL 3.3 Core Profile\n";
#else
// Linux: Dejar que SDL elija (Desktop 3.3 en PC, ES 3.0 en RPi automáticamente)
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Usando OpenGL por defecto del sistema");
std::cout << "Usando OpenGL por defecto del sistema\n";
#endif
#endif
@@ -546,9 +545,7 @@ auto Screen::initSDLVideo() -> bool {
window_ = SDL_CreateWindow(Options::window.caption.c_str(), WINDOW_WIDTH * Options::window.zoom, WINDOW_HEIGHT * Options::window.zoom, window_flags);
if (window_ == nullptr) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"FATAL: Failed to create window! SDL Error: %s",
SDL_GetError());
std::cerr << "FATAL: Failed to create window! SDL Error: " << SDL_GetError() << '\n';
SDL_Quit();
return false;
}
@@ -556,9 +553,7 @@ auto Screen::initSDLVideo() -> bool {
// Crear renderer
renderer_ = SDL_CreateRenderer(window_, nullptr);
if (renderer_ == nullptr) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"FATAL: Failed to create renderer! SDL Error: %s",
SDL_GetError());
std::cerr << "FATAL: Failed to create renderer! SDL Error: " << SDL_GetError() << '\n';
SDL_DestroyWindow(window_);
window_ = nullptr;
SDL_Quit();
@@ -577,6 +572,6 @@ auto Screen::initSDLVideo() -> bool {
SDL_SetRenderDrawBlendMode(renderer_, SDL_BLENDMODE_BLEND);
SDL_SetRenderVSync(renderer_, Options::video.vertical_sync ? 1 : SDL_RENDERER_VSYNC_DISABLED);
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "** Video system initialized successfully");
std::cout << "** Video system initialized successfully\n";
return true;
}

View File

@@ -27,7 +27,7 @@ auto Debug::get() -> Debug* {
// Dibuja en pantalla
void Debug::render() {
auto text = Resource::get()->getText("debug");
auto text = Resource::get()->getText("aseprite");
int y = y_;
int w = 0;

View File

@@ -124,7 +124,6 @@ Director::Director(std::vector<std::string> const& args) {
// Crea los objetos
Screen::init();
SDL_HideCursor();
// Initialize resources (works for both release and development)
Resource::init();

View File

@@ -37,13 +37,11 @@ namespace Options {
// Estructura para las opciones de las notificaciones
struct Notification {
bool sound; // Indica si las notificaciones suenan
Uint8 color; // Color de las notificaciones
bool sound{GameDefaults::NOTIFICATION_SOUND}; // Indica si las notificaciones suenan
Uint8 color{GameDefaults::NOTIFICATION_COLOR}; // Color de las notificaciones
// Constructor por defecto
Notification()
: sound(GameDefaults::NOTIFICATION_SOUND),
color(GameDefaults::NOTIFICATION_COLOR) {}
Notification() = default;
// Constructor
Notification(bool s, Uint8 c)
@@ -58,17 +56,13 @@ struct Cheat {
ENABLED = true
};
State infinite_lives; // Indica si el jugador dispone de vidas infinitas
State invincible; // Indica si el jugador puede morir
State jail_is_open; // Indica si la Jail está abierta
State alternate_skin; // Indica si se usa una skin diferente para el jugador
State infinite_lives{State::DISABLED}; // Indica si el jugador dispone de vidas infinitas
State invincible{State::DISABLED}; // Indica si el jugador puede morir
State jail_is_open{State::DISABLED}; // Indica si la Jail está abierta
State alternate_skin{State::DISABLED}; // Indica si se usa una skin diferente para el jugador
// Constructor por defecto
Cheat()
: infinite_lives(State::DISABLED),
invincible(State::DISABLED),
jail_is_open(State::DISABLED),
alternate_skin(State::DISABLED) {}
Cheat() = default;
// Constructor
Cheat(State inf_lives, State is_invincible, State jail_enabled, State alt_skin)
@@ -87,14 +81,12 @@ struct Cheat {
// Estructura para almacenar estadísticas
struct Stats {
int rooms; // Cantidad de habitaciones visitadas
int items; // Cantidad de items obtenidos
int rooms{0}; // Cantidad de habitaciones visitadas
int items{0}; // Cantidad de items obtenidos
std::string worst_nightmare; // Habitación con más muertes acumuladas
// Constructor por defecto
Stats()
: rooms(0),
items(0) {}
Stats() = default;
// Constructor
Stats(int room_count, int item_count, std::string worst_nightmare_room)
@@ -105,15 +97,12 @@ struct Stats {
// Estructura con opciones de la ventana
struct Window {
std::string caption; // Texto que aparece en la barra de título de la ventana
int zoom; // Zoom de la ventana
int max_zoom; // Máximo tamaño de zoom para la ventana
std::string caption{"JailDoctor's Dilemma"}; // Texto que aparece en la barra de título de la ventana
int zoom{GameDefaults::WINDOW_ZOOM}; // Zoom de la ventana
int max_zoom{GameDefaults::WINDOW_ZOOM}; // Máximo tamaño de zoom para la ventana
// Constructor por defecto
Window()
: caption("JailDoctor's Dilemma"),
zoom(GameDefaults::WINDOW_ZOOM),
max_zoom(GameDefaults::WINDOW_ZOOM) {}
Window() = default;
// Constructor
Window(int window_zoom, int maximum_zoom)
@@ -124,15 +113,12 @@ struct Window {
// Estructura para gestionar el borde de la pantalla
struct Border {
bool enabled; // Indica si se ha de mostrar el borde
float width; // Ancho del borde
float height; // Alto del borde
bool enabled{GameDefaults::BORDER_ENABLED}; // Indica si se ha de mostrar el borde
float width{GameDefaults::BORDER_WIDTH}; // Ancho del borde
float height{GameDefaults::BORDER_HEIGHT}; // Alto del borde
// Constructor por defecto
Border()
: enabled(GameDefaults::BORDER_ENABLED),
width(GameDefaults::BORDER_WIDTH),
height(GameDefaults::BORDER_HEIGHT) {}
Border() = default;
// Constructor
Border(bool is_enabled, float border_width, float border_height)
@@ -143,26 +129,18 @@ struct Border {
// Estructura para las opciones de video
struct Video {
bool fullscreen; // Contiene el valor del modo de pantalla completa
ScreenFilter filter; // Filtro usado para el escalado de la imagen
bool vertical_sync; // Indica si se quiere usar vsync o no
bool shaders; // Indica si se van a usar shaders o no
bool integer_scale; // Indica si el escalado de la imagen ha de ser entero en el modo a pantalla completa
bool keep_aspect; // Indica si se ha de mantener la relación de aspecto al poner el modo a pantalla completa
Border border; // Borde de la pantalla
std::string palette; // Paleta de colores a usar en el juego
std::string info; // Información sobre el modo de vídeo
bool fullscreen{GameDefaults::VIDEO_MODE}; // Contiene el valor del modo de pantalla completa
ScreenFilter filter{GameDefaults::VIDEO_FILTER}; // Filtro usado para el escalado de la imagen
bool vertical_sync{GameDefaults::VIDEO_VERTICAL_SYNC}; // Indica si se quiere usar vsync o no
bool shaders{GameDefaults::VIDEO_SHADERS}; // Indica si se van a usar shaders o no
bool integer_scale{GameDefaults::VIDEO_INTEGER_SCALE}; // Indica si el escalado de la imagen ha de ser entero en el modo a pantalla completa
bool keep_aspect{GameDefaults::VIDEO_KEEP_ASPECT}; // Indica si se ha de mantener la relación de aspecto al poner el modo a pantalla completa
Border border; // Borde de la pantalla
std::string palette{GameDefaults::PALETTE_NAME}; // Paleta de colores a usar en el juego
std::string info; // Información sobre el modo de vídeo
// Constructor por defecto
Video()
: fullscreen(GameDefaults::VIDEO_MODE),
filter(GameDefaults::VIDEO_FILTER),
vertical_sync(GameDefaults::VIDEO_VERTICAL_SYNC),
shaders(GameDefaults::VIDEO_SHADERS),
integer_scale(GameDefaults::VIDEO_INTEGER_SCALE),
keep_aspect(GameDefaults::VIDEO_KEEP_ASPECT),
palette(GameDefaults::PALETTE_NAME) {}
Video() = default;
// Constructor
Video(bool is_fullscreen, ScreenFilter screen_filter, bool vsync, bool use_shaders, bool int_scale, bool keep_aspect_ratio, Border video_border, std::string palette_name)
@@ -178,13 +156,11 @@ struct Video {
// Estructura para las opciones de musica
struct Music {
bool enabled; // Indica si la música suena o no
int volume; // Volumen al que suena la música (0 a 128 internamente)
bool enabled{GameDefaults::MUSIC_ENABLED}; // Indica si la música suena o no
int volume{VolumeHelpers::convertVolume(GameDefaults::MUSIC_VOLUME)}; // Volumen al que suena la música (0 a 128 internamente)
// Constructor por defecto
Music()
: enabled(GameDefaults::MUSIC_ENABLED),
volume(VolumeHelpers::convertVolume(GameDefaults::MUSIC_VOLUME)) {}
Music() = default;
// Constructor con parámetros
Music(bool is_enabled, int volume_percent)
@@ -200,13 +176,11 @@ struct Music {
// Estructura para las opciones de sonido
struct Sound {
bool enabled; // Indica si los sonidos suenan o no
int volume; // Volumen al que suenan los sonidos (0 a 128 internamente)
bool enabled{GameDefaults::SOUND_ENABLED}; // Indica si los sonidos suenan o no
int volume{VolumeHelpers::convertVolume(GameDefaults::SOUND_VOLUME)}; // Volumen al que suenan los sonidos (0 a 128 internamente)
// Constructor por defecto
Sound()
: enabled(GameDefaults::SOUND_ENABLED),
volume(VolumeHelpers::convertVolume(GameDefaults::SOUND_VOLUME)) {}
Sound() = default;
// Constructor con parámetros
Sound(bool is_enabled, int volume_percent)
@@ -222,15 +196,13 @@ struct Sound {
// Estructura para las opciones de audio
struct Audio {
Music music; // Opciones para la música
Sound sound; // Opciones para los efectos de sonido
bool enabled; // Indica si el audio está activo o no
int volume; // Volumen al que suenan el audio (0-128 internamente)
Music music; // Opciones para la música
Sound sound; // Opciones para los efectos de sonido
bool enabled{GameDefaults::AUDIO_ENABLED}; // Indica si el audio está activo o no
int volume{VolumeHelpers::convertVolume(GameDefaults::AUDIO_VOLUME)}; // Volumen al que suenan el audio (0-128 internamente)
// Constructor por defecto
Audio()
: enabled(GameDefaults::AUDIO_ENABLED),
volume(VolumeHelpers::convertVolume(GameDefaults::AUDIO_VOLUME)) {}
Audio() = default;
// Constructor
Audio(Music audio_music, Sound audio_sound, bool is_enabled, int volume_percent)
@@ -242,13 +214,11 @@ struct Audio {
// Estructura para las opciones de juego
struct Game {
float width; // Ancho de la resolucion del juego
float height; // Alto de la resolucion del juego
float width{GameDefaults::GAME_WIDTH}; // Ancho de la resolucion del juego
float height{GameDefaults::GAME_HEIGHT}; // Alto de la resolucion del juego
// Constructor por defecto
Game()
: width(GameDefaults::GAME_WIDTH),
height(GameDefaults::GAME_HEIGHT) {}
Game() = default;
// Constructor
Game(float game_width, float game_height)