afegides les normes de les guidelines jailerianes

This commit is contained in:
2025-04-07 12:42:24 +02:00
parent 0e2dff6647
commit 502ba7297a
6 changed files with 146 additions and 38 deletions

71
source/global_inputs.cpp Normal file
View File

@@ -0,0 +1,71 @@
#include "global_inputs.h"
#include <SDL3/SDL.h> // Para SDL_RendererLogicalPresentation, SDL_Se...
#include <string> // Para operator+, allocator, char_traits, string
#include <vector> // 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;
}
}
}
}

9
source/global_inputs.h Normal file
View File

@@ -0,0 +1,9 @@
#pragma once
#include <SDL3/SDL.h>
namespace globalInputs
{
// Comprueba los inputs que se pueden introducir en cualquier sección del juego
void check(const SDL_Event &event);
}

View File

@@ -4,26 +4,31 @@
#include "mouse.h" #include "mouse.h"
#include "surface.h" #include "surface.h"
#include "s_sprite.h" #include "s_sprite.h"
#include "global_inputs.h"
Logo::Logo() Logo::Logo()
{ {
init(); init();
} }
Logo::~Logo() Logo::~Logo()
{ {
close(); close();
} }
void Logo::init() 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(); Screen::get()->init();
logo_surface = std::make_shared<Surface>("jailgames.gif"); logo_surface = std::make_shared<Surface>("jailgames.gif");
logo_surface->scale(5); logo_surface->scale(5);
logo_sprite = std::make_unique<SSprite>(logo_surface); logo_sprite = std::make_unique<SSprite>(logo_surface);
logo_sprite->setPosition( logo_sprite->setPosition(
(options.game.width - logo_sprite->getWidth()) / 2, (options.logo.width - logo_sprite->getWidth()) / 2,
(options.game.height- logo_sprite->getHeight()) / 2); (options.logo.height - logo_sprite->getHeight()) / 2);
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Logo start"); SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Logo start");
} }
@@ -44,19 +49,22 @@ void Logo::checkEvents()
switch (event.type) switch (event.type)
{ {
case SDL_EVENT_QUIT: case SDL_EVENT_QUIT:
running = false; options.logo.running = false;
return; return;
} }
globalInputs::check(event);
Mouse::handleEvent(event); Mouse::handleEvent(event);
} }
} }
void Logo::update() void Logo::update()
{ {
if (SDL_GetTicks() - ticks > options.game.speed) if (SDL_GetTicks() - ticks > options.logo.speed)
{ {
ticks = SDL_GetTicks(); ticks = SDL_GetTicks();
Screen::get()->update();
} }
} }
@@ -69,11 +77,11 @@ void Logo::render()
int Logo::run() int Logo::run()
{ {
while (running) while (options.logo.running)
{ {
update(); update();
checkEvents(); checkEvents();
render(); render();
} }
return 0; return 0;
} }

View File

@@ -9,6 +9,7 @@
constexpr int DEFAULT_GAME_WIDTH = 480; // Ancho de la ventana por defecto 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 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 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_WINDOW_ZOOM = 2; // Zoom de la ventana por defecto
constexpr int DEFAULT_VIDEO_FULLSCREEN = false; // Modo de pantalla completa 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 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 width; // Ancho de la resolucion del logo
int height; // Alto de la resolucion del logo int height; // Alto de la resolucion del logo
Uint64 speed; // Velocidad de ejecución del logo Uint64 speed; // Velocidad de ejecución del logo
bool running; // Para gestionar el bucle principal
// Constructor por defecto // Constructor por defecto
OptionsLogo() OptionsLogo()
: width(DEFAULT_GAME_WIDTH), : width(DEFAULT_GAME_WIDTH),
height(DEFAULT_GAME_HEIGHT), height(DEFAULT_GAME_HEIGHT),
speed(DEFAUL_LOGO_SPEED) {} speed(DEFAUL_LOGO_SPEED),
running(DEFAUL_LOGO_RUNNING) {}
// Constructor // Constructor
OptionsLogo(int w, int h, Uint64 s) OptionsLogo(int w, int h, Uint64 s, bool r)
: width(w), : width(w),
height(h), height(h),
speed(s) {} speed(s),
running(r) {}
}; };
// Estructura con todas las opciones de configuración del programa // Estructura con todas las opciones de configuración del programa
struct Options struct Options
{ {
bool console; // Indica si ha de mostrar información por la consola de texto OptionsLogo logo; // Opciones de juego
OptionsLogo game; // Opciones de juego
OptionsVideo video; // Opciones de video OptionsVideo video; // Opciones de video
OptionsWindow window; // Opciones relativas a la ventana OptionsWindow window; // Opciones de la ventana
OptionsAudio audio; // Opciones relativas al audio OptionsAudio audio; // Opciones del audio
// Constructor por defecto // Constructor por defecto
Options() Options()
: console(DEFAULT_CONSOLE), : logo(OptionsLogo()),
game(OptionsLogo()),
video(OptionsVideo()), video(OptionsVideo()),
window(OptionsWindow()), window(OptionsWindow()),
audio(OptionsAudio()) {} audio(OptionsAudio()) {}
// Constructor // Constructor
Options(std::string cv, bool c, OptionsLogo g, OptionsVideo v, OptionsWindow sw, OptionsAudio a) Options(OptionsLogo l, OptionsVideo v, OptionsWindow sw, OptionsAudio a)
: console(c), : logo(l),
game(g),
video(v), video(v),
window(sw), window(sw),
audio(a) {} audio(a) {}

View File

@@ -37,23 +37,22 @@ Screen::Screen()
// Arranca SDL VIDEO, crea la ventana y el renderizador // Arranca SDL VIDEO, crea la ventana y el renderizador
initSDL(); initSDL();
// Obtiene información sobre la pantalla
getDisplayInfo();
// Ajusta los tamaños // Ajusta los tamaños
adjustWindowSize(); adjustWindowSize();
// Crea la textura donde se dibujan los graficos del juego // 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_) if (!game_texture_)
{ {
// Registrar el error si está habilitado SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: game_texture_ could not be created! SDL Error: %s", SDL_GetError());
if (options.console)
{
std::cerr << "Error: game_texture_ could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
}
} }
SDL_SetTextureScaleMode(game_texture_, options.video.scale_mode); SDL_SetTextureScaleMode(game_texture_, options.video.scale_mode);
// Crea la surface donde se dibujan los graficos del juego // Crea la surface donde se dibujan los graficos del juego
game_surface_ = std::make_shared<Surface>(options.game.width, options.game.height); game_surface_ = std::make_shared<Surface>(options.logo.width, options.logo.height);
game_surface_->setPalette(readPalFile("jailgames.pal")); game_surface_->setPalette(readPalFile("jailgames.pal"));
game_surface_->clear(0); game_surface_->clear(0);
@@ -115,7 +114,7 @@ void Screen::toggleFullscreen()
// Reduce el tamaño de la ventana // Reduce el tamaño de la ventana
bool Screen::decWindowZoom() bool Screen::decWindowZoom()
{ {
if (options.video.fullscreen == 0) if (!options.video.fullscreen)
{ {
const int PREVIOUS_ZOOM = options.window.zoom; const int PREVIOUS_ZOOM = options.window.zoom;
--options.window.zoom; --options.window.zoom;
@@ -123,7 +122,7 @@ bool Screen::decWindowZoom()
if (options.window.zoom != PREVIOUS_ZOOM) if (options.window.zoom != PREVIOUS_ZOOM)
{ {
setFullscreenMode(options.video.fullscreen); adjustWindowSize();
return true; return true;
} }
} }
@@ -134,7 +133,7 @@ bool Screen::decWindowZoom()
// Aumenta el tamaño de la ventana // Aumenta el tamaño de la ventana
bool Screen::incWindowZoom() bool Screen::incWindowZoom()
{ {
if (options.video.fullscreen == 0) if (!options.video.fullscreen)
{ {
const int PREVIOUS_ZOOM = options.window.zoom; const int PREVIOUS_ZOOM = options.window.zoom;
++options.window.zoom; ++options.window.zoom;
@@ -142,7 +141,7 @@ bool Screen::incWindowZoom()
if (options.window.zoom != PREVIOUS_ZOOM) if (options.window.zoom != PREVIOUS_ZOOM)
{ {
setFullscreenMode(options.video.fullscreen); adjustWindowSize();
return true; return true;
} }
} }
@@ -159,8 +158,8 @@ void Screen::update()
// Calcula el tamaño de la ventana // Calcula el tamaño de la ventana
void Screen::adjustWindowSize() void Screen::adjustWindowSize()
{ {
window_width_ = options.game.width; window_width_ = options.logo.width;
window_height_ = options.game.height; window_height_ = options.logo.height;
// Establece el nuevo tamaño // Establece el nuevo tamaño
if (!options.video.fullscreen) if (!options.video.fullscreen)
@@ -237,7 +236,7 @@ bool Screen::initSDL()
} }
// Crea la ventana // 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_) if (!window_)
{ {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Window could not be created! SDL Error: %s", SDL_GetError()); SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Window could not be created! SDL Error: %s", SDL_GetError());
@@ -255,7 +254,7 @@ bool Screen::initSDL()
else else
{ {
SDL_SetRenderDrawColor(renderer_, 0x00, 0x00, 0x00, 0xFF); 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_SetWindowFullscreen(window_, options.video.fullscreen);
SDL_SetRenderDrawBlendMode(renderer_, SDL_BLENDMODE_BLEND); SDL_SetRenderDrawBlendMode(renderer_, SDL_BLENDMODE_BLEND);
SDL_SetRenderVSync(renderer_, options.video.vertical_sync ? 1 : SDL_RENDERER_VSYNC_DISABLED); 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]); auto DM = SDL_GetCurrentDisplayMode(displays[0]);
// Calcula el máximo factor de zoom que se puede aplicar a la pantalla // 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); 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 // 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<int>(DM->w), static_cast<int>(DM->h), static_cast<int>(DM->refresh_rate)); static_cast<int>(DM->w), static_cast<int>(DM->h), static_cast<int>(DM->refresh_rate));
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Window resolution: %dx%d x%d", 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); static_cast<int>(options.logo.width), static_cast<int>(options.logo.height), options.window.zoom);
options.video.info = std::to_string(static_cast<int>(DM->w)) + " X " + options.video.info = std::to_string(static_cast<int>(DM->w)) + " X " +
std::to_string(static_cast<int>(DM->h)) + " AT " + std::to_string(static_cast<int>(DM->h)) + " AT " +
std::to_string(static_cast<int>(DM->refresh_rate)) + " HZ"; std::to_string(static_cast<int>(DM->refresh_rate)) + " HZ";
// Calcula el máximo factor de zoom que se puede aplicar a la pantalla // 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 // Normaliza los valores de zoom
options.window.zoom = std::min(options.window.zoom, MAX_ZOOM); options.window.zoom = std::min(options.window.zoom, MAX_ZOOM);
SDL_free(displays); 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);
} }

View File

@@ -98,6 +98,12 @@ public:
// Establece el renderizador para las surfaces // Establece el renderizador para las surfaces
void setRendererSurface(std::shared_ptr<Surface> surface = nullptr); void setRendererSurface(std::shared_ptr<Surface> surface = nullptr);
// Activa / desactiva el escalado entero
void toggleIntegerScale();
// Activa / desactiva el vsync
void toggleVSync();
// Getters // Getters
SDL_Renderer *getRenderer(); SDL_Renderer *getRenderer();
std::shared_ptr<Surface> getRendererSurface(); std::shared_ptr<Surface> getRendererSurface();