afegides les normes de les guidelines jailerianes
This commit is contained in:
71
source/global_inputs.cpp
Normal file
71
source/global_inputs.cpp
Normal 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
9
source/global_inputs.h
Normal 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);
|
||||
}
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "mouse.h"
|
||||
#include "surface.h"
|
||||
#include "s_sprite.h"
|
||||
#include "global_inputs.h"
|
||||
|
||||
Logo::Logo()
|
||||
{
|
||||
@@ -17,13 +18,17 @@ Logo::~Logo()
|
||||
|
||||
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<Surface>("jailgames.gif");
|
||||
logo_surface->scale(5);
|
||||
|
||||
logo_sprite = std::make_unique<SSprite>(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,7 +77,7 @@ void Logo::render()
|
||||
|
||||
int Logo::run()
|
||||
{
|
||||
while (running)
|
||||
while (options.logo.running)
|
||||
{
|
||||
update();
|
||||
checkEvents();
|
||||
|
||||
@@ -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) {}
|
||||
|
||||
@@ -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<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_->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,14 +292,14 @@ void Screen::getDisplayInfo()
|
||||
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",
|
||||
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 " +
|
||||
std::to_string(static_cast<int>(DM->h)) + " AT " +
|
||||
std::to_string(static_cast<int>(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);
|
||||
@@ -308,3 +307,17 @@ void Screen::getDisplayInfo()
|
||||
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);
|
||||
}
|
||||
@@ -98,6 +98,12 @@ public:
|
||||
// Establece el renderizador para las surfaces
|
||||
void setRendererSurface(std::shared_ptr<Surface> surface = nullptr);
|
||||
|
||||
// Activa / desactiva el escalado entero
|
||||
void toggleIntegerScale();
|
||||
|
||||
// Activa / desactiva el vsync
|
||||
void toggleVSync();
|
||||
|
||||
// Getters
|
||||
SDL_Renderer *getRenderer();
|
||||
std::shared_ptr<Surface> getRendererSurface();
|
||||
|
||||
Reference in New Issue
Block a user