From 502ba7297a13bb86d8e893774b1e7fda4d0995d9 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Mon, 7 Apr 2025 12:42:24 +0200 Subject: [PATCH] afegides les normes de les guidelines jailerianes --- source/global_inputs.cpp | 71 ++++++++++++++++++++++++++++++++++++++++ source/global_inputs.h | 9 +++++ source/logo.cpp | 24 +++++++++----- source/options.h | 25 +++++++------- source/screen.cpp | 49 +++++++++++++++++---------- source/screen.h | 6 ++++ 6 files changed, 146 insertions(+), 38 deletions(-) create mode 100644 source/global_inputs.cpp create mode 100644 source/global_inputs.h diff --git a/source/global_inputs.cpp b/source/global_inputs.cpp new file mode 100644 index 0000000..f4d2a98 --- /dev/null +++ b/source/global_inputs.cpp @@ -0,0 +1,71 @@ +#include "global_inputs.h" +#include // Para SDL_RendererLogicalPresentation, SDL_Se... +#include // Para operator+, allocator, char_traits, string +#include // Para vector +#include "audio.h" // Para JA_SetMusicVolume, JA_SetSoundVolume +#include "options.h" // Para Options, options, VideoOptions, GameOpt... +#include "screen.h" // Para Screen +#include "utils.h" // Para boolToOnOff + +namespace globalInputs +{ + + // Activa o desactiva el audio + void toggleAudio() + { + options.audio.enabled = !options.audio.enabled; + Audio::get()->enable(options.audio.enabled); + } + + // Cambia el modo de escalado entero + void toggleIntegerScale() + { + Screen::get()->toggleIntegerScale(); + } + + // Activa / desactiva el vsync + void toggleVSync() + { + Screen::get()->toggleVSync(); + } + + // Comprueba los inputs que se pueden introducir en cualquier sección del juego + void check(const SDL_Event &event) + { + if (event.type == SDL_EVENT_KEY_DOWN && event.key.repeat == 0) + { + switch (event.key.key) + { + // Salir + case SDLK_ESCAPE: + options.logo.running = false; + return; + + // Decrementar el tamaño de la ventana + case SDLK_F1: + Screen::get()->decWindowZoom(); + return; + + // Incrementar el tamaño de la ventana + case SDLK_F2: + Screen::get()->incWindowZoom(); + return; + + // Cambiar entre pantalla completa y ventana + case SDLK_F3: + Screen::get()->toggleFullscreen(); + return; + + // Integer Scale + case SDLK_F5: + toggleIntegerScale(); + return; + + // VSync + case SDLK_F6: + toggleVSync(); + return; + } + } + } +} \ No newline at end of file diff --git a/source/global_inputs.h b/source/global_inputs.h new file mode 100644 index 0000000..5f2b367 --- /dev/null +++ b/source/global_inputs.h @@ -0,0 +1,9 @@ +#pragma once + +#include + +namespace globalInputs +{ + // Comprueba los inputs que se pueden introducir en cualquier sección del juego + void check(const SDL_Event &event); +} \ No newline at end of file diff --git a/source/logo.cpp b/source/logo.cpp index ab50d4c..aa037e8 100644 --- a/source/logo.cpp +++ b/source/logo.cpp @@ -4,26 +4,31 @@ #include "mouse.h" #include "surface.h" #include "s_sprite.h" +#include "global_inputs.h" Logo::Logo() { - init(); + init(); } Logo::~Logo() { - close(); + close(); } void Logo::init() { + SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); + SDL_SetLogPriority(SDL_LOG_CATEGORY_TEST, SDL_LOG_PRIORITY_ERROR); + Screen::get()->init(); logo_surface = std::make_shared("jailgames.gif"); logo_surface->scale(5); + logo_sprite = std::make_unique(logo_surface); logo_sprite->setPosition( - (options.game.width - logo_sprite->getWidth()) / 2, - (options.game.height- logo_sprite->getHeight()) / 2); + (options.logo.width - logo_sprite->getWidth()) / 2, + (options.logo.height - logo_sprite->getHeight()) / 2); SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Logo start"); } @@ -44,19 +49,22 @@ void Logo::checkEvents() switch (event.type) { case SDL_EVENT_QUIT: - running = false; + options.logo.running = false; return; } + globalInputs::check(event); Mouse::handleEvent(event); } } void Logo::update() { - if (SDL_GetTicks() - ticks > options.game.speed) + if (SDL_GetTicks() - ticks > options.logo.speed) { ticks = SDL_GetTicks(); + + Screen::get()->update(); } } @@ -69,11 +77,11 @@ void Logo::render() int Logo::run() { - while (running) + while (options.logo.running) { update(); checkEvents(); render(); } - return 0; + return 0; } \ No newline at end of file diff --git a/source/options.h b/source/options.h index 6b18e9e..7b3e2e4 100644 --- a/source/options.h +++ b/source/options.h @@ -9,6 +9,7 @@ constexpr int DEFAULT_GAME_WIDTH = 480; // Ancho de la ventana por defecto constexpr int DEFAULT_GAME_HEIGHT = 270; // Alto de la ventana por defecto constexpr Uint64 DEFAUL_LOGO_SPEED = 1000 / 60; // Velocidad a la que se ejecuta el logo +constexpr bool DEFAUL_LOGO_RUNNING = true; // Flag para el bucle principal constexpr int DEFAULT_WINDOW_ZOOM = 2; // Zoom de la ventana por defecto constexpr int DEFAULT_VIDEO_FULLSCREEN = false; // Modo de pantalla completa por defecto constexpr SDL_ScaleMode DEFAULT_SCALE_MODE = SDL_SCALEMODE_NEAREST; // Modo de pantalla completa por defecto @@ -162,41 +163,41 @@ struct OptionsLogo int width; // Ancho de la resolucion del logo int height; // Alto de la resolucion del logo Uint64 speed; // Velocidad de ejecución del logo + bool running; // Para gestionar el bucle principal // Constructor por defecto OptionsLogo() : width(DEFAULT_GAME_WIDTH), height(DEFAULT_GAME_HEIGHT), - speed(DEFAUL_LOGO_SPEED) {} + speed(DEFAUL_LOGO_SPEED), + running(DEFAUL_LOGO_RUNNING) {} // Constructor - OptionsLogo(int w, int h, Uint64 s) + OptionsLogo(int w, int h, Uint64 s, bool r) : width(w), height(h), - speed(s) {} + speed(s), + running(r) {} }; // Estructura con todas las opciones de configuración del programa struct Options { - bool console; // Indica si ha de mostrar información por la consola de texto - OptionsLogo game; // Opciones de juego + OptionsLogo logo; // Opciones de juego OptionsVideo video; // Opciones de video - OptionsWindow window; // Opciones relativas a la ventana - OptionsAudio audio; // Opciones relativas al audio + OptionsWindow window; // Opciones de la ventana + OptionsAudio audio; // Opciones del audio // Constructor por defecto Options() - : console(DEFAULT_CONSOLE), - game(OptionsLogo()), + : logo(OptionsLogo()), video(OptionsVideo()), window(OptionsWindow()), audio(OptionsAudio()) {} // Constructor - Options(std::string cv, bool c, OptionsLogo g, OptionsVideo v, OptionsWindow sw, OptionsAudio a) - : console(c), - game(g), + Options(OptionsLogo l, OptionsVideo v, OptionsWindow sw, OptionsAudio a) + : logo(l), video(v), window(sw), audio(a) {} diff --git a/source/screen.cpp b/source/screen.cpp index 9c8f0a7..ea350d8 100644 --- a/source/screen.cpp +++ b/source/screen.cpp @@ -37,23 +37,22 @@ Screen::Screen() // Arranca SDL VIDEO, crea la ventana y el renderizador initSDL(); + // Obtiene información sobre la pantalla + getDisplayInfo(); + // Ajusta los tamaños adjustWindowSize(); // Crea la textura donde se dibujan los graficos del juego - game_texture_ = SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, options.game.width, options.game.height); + game_texture_ = SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, options.logo.width, options.logo.height); if (!game_texture_) { - // Registrar el error si está habilitado - if (options.console) - { - std::cerr << "Error: game_texture_ could not be created!\nSDL Error: " << SDL_GetError() << std::endl; - } + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: game_texture_ could not be created! SDL Error: %s", SDL_GetError()); } SDL_SetTextureScaleMode(game_texture_, options.video.scale_mode); // Crea la surface donde se dibujan los graficos del juego - game_surface_ = std::make_shared(options.game.width, options.game.height); + game_surface_ = std::make_shared(options.logo.width, options.logo.height); game_surface_->setPalette(readPalFile("jailgames.pal")); game_surface_->clear(0); @@ -115,7 +114,7 @@ void Screen::toggleFullscreen() // Reduce el tamaño de la ventana bool Screen::decWindowZoom() { - if (options.video.fullscreen == 0) + if (!options.video.fullscreen) { const int PREVIOUS_ZOOM = options.window.zoom; --options.window.zoom; @@ -123,7 +122,7 @@ bool Screen::decWindowZoom() if (options.window.zoom != PREVIOUS_ZOOM) { - setFullscreenMode(options.video.fullscreen); + adjustWindowSize(); return true; } } @@ -134,7 +133,7 @@ bool Screen::decWindowZoom() // Aumenta el tamaño de la ventana bool Screen::incWindowZoom() { - if (options.video.fullscreen == 0) + if (!options.video.fullscreen) { const int PREVIOUS_ZOOM = options.window.zoom; ++options.window.zoom; @@ -142,7 +141,7 @@ bool Screen::incWindowZoom() if (options.window.zoom != PREVIOUS_ZOOM) { - setFullscreenMode(options.video.fullscreen); + adjustWindowSize(); return true; } } @@ -159,8 +158,8 @@ void Screen::update() // Calcula el tamaño de la ventana void Screen::adjustWindowSize() { - window_width_ = options.game.width; - window_height_ = options.game.height; + window_width_ = options.logo.width; + window_height_ = options.logo.height; // Establece el nuevo tamaño if (!options.video.fullscreen) @@ -237,7 +236,7 @@ bool Screen::initSDL() } // Crea la ventana - window_ = SDL_CreateWindow(options.window.caption.c_str(), options.game.width * options.window.zoom, options.game.height * options.window.zoom, SDL_WINDOW_OPENGL); + window_ = SDL_CreateWindow(options.window.caption.c_str(), options.logo.width * options.window.zoom, options.logo.height * options.window.zoom, SDL_WINDOW_OPENGL); if (!window_) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Window could not be created! SDL Error: %s", SDL_GetError()); @@ -255,7 +254,7 @@ bool Screen::initSDL() else { SDL_SetRenderDrawColor(renderer_, 0x00, 0x00, 0x00, 0xFF); - SDL_SetRenderLogicalPresentation(renderer_, options.game.width, options.game.height, SDL_LOGICAL_PRESENTATION_INTEGER_SCALE); + SDL_SetRenderLogicalPresentation(renderer_, options.logo.width, options.logo.height, SDL_LOGICAL_PRESENTATION_INTEGER_SCALE); SDL_SetWindowFullscreen(window_, options.video.fullscreen); SDL_SetRenderDrawBlendMode(renderer_, SDL_BLENDMODE_BLEND); SDL_SetRenderVSync(renderer_, options.video.vertical_sync ? 1 : SDL_RENDERER_VSYNC_DISABLED); @@ -285,7 +284,7 @@ void Screen::getDisplayInfo() auto DM = SDL_GetCurrentDisplayMode(displays[0]); // Calcula el máximo factor de zoom que se puede aplicar a la pantalla - options.window.max_zoom = std::min(DM->w / options.game.width, DM->h / options.game.height); + options.window.max_zoom = std::min(DM->w / options.logo.width, DM->h / options.logo.height); 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 @@ -293,18 +292,32 @@ void Screen::getDisplayInfo() static_cast(DM->w), static_cast(DM->h), static_cast(DM->refresh_rate)); SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Window resolution: %dx%d x%d", - static_cast(options.game.width), static_cast(options.game.height), options.window.zoom); + static_cast(options.logo.width), static_cast(options.logo.height), options.window.zoom); options.video.info = std::to_string(static_cast(DM->w)) + " X " + std::to_string(static_cast(DM->h)) + " AT " + std::to_string(static_cast(DM->refresh_rate)) + " HZ"; // Calcula el máximo factor de zoom que se puede aplicar a la pantalla - const int MAX_ZOOM = std::min(DM->w / options.game.width, (DM->h - WINDOWS_DECORATIONS_) / options.game.height); + const int MAX_ZOOM = std::min(DM->w / options.logo.width, (DM->h - WINDOWS_DECORATIONS_) / options.logo.height); // Normaliza los valores de zoom options.window.zoom = std::min(options.window.zoom, MAX_ZOOM); SDL_free(displays); } +} + +// Activa / desactiva el escalado entero +void Screen::toggleIntegerScale() +{ + options.video.integer_scale = !options.video.integer_scale; + SDL_SetRenderLogicalPresentation(Screen::get()->getRenderer(), options.logo.width, options.logo.height, options.video.integer_scale ? SDL_LOGICAL_PRESENTATION_INTEGER_SCALE : SDL_LOGICAL_PRESENTATION_LETTERBOX); +} + +// Activa / desactiva el vsync +void Screen::toggleVSync() +{ + options.video.vertical_sync = !options.video.vertical_sync; + SDL_SetRenderVSync(renderer_, options.video.vertical_sync ? 1 : SDL_RENDERER_VSYNC_DISABLED); } \ No newline at end of file diff --git a/source/screen.h b/source/screen.h index adfba8b..ca51487 100644 --- a/source/screen.h +++ b/source/screen.h @@ -98,6 +98,12 @@ public: // Establece el renderizador para las surfaces void setRendererSurface(std::shared_ptr surface = nullptr); + // Activa / desactiva el escalado entero + void toggleIntegerScale(); + + // Activa / desactiva el vsync + void toggleVSync(); + // Getters SDL_Renderer *getRenderer(); std::shared_ptr getRendererSurface();