ja funciona el audio

This commit is contained in:
2025-03-27 18:34:04 +01:00
parent e2339bd54a
commit c6288918b2
10 changed files with 221 additions and 184 deletions

View File

@@ -98,11 +98,9 @@ void Director::init()
loadScoreFile(); // Carga el fichero de puntuaciones loadScoreFile(); // Carga el fichero de puntuaciones
// Inicializa y crea el resto de objetos // Inicializa y crea el resto de objetos
initSDL();
SDL_HideCursor();
initJailAudio();
lang::loadFromFile(getLangFile(static_cast<lang::Code>(options.game.language))); lang::loadFromFile(getLangFile(static_cast<lang::Code>(options.game.language)));
Screen::init(window_, renderer_); Screen::init();
initJailAudio();
Resource::init(); Resource::init();
Input::init(Asset::get()->get("gamecontrollerdb.txt")); Input::init(Asset::get()->get("gamecontrollerdb.txt"));
bindInputs(); bindInputs();
@@ -124,9 +122,6 @@ void Director::close()
JA_Quit(); JA_Quit();
SDL_DestroyRenderer(renderer_);
SDL_DestroyWindow(window_);
SDL_Quit(); SDL_Quit();
#ifdef ARCADE #ifdef ARCADE
@@ -267,109 +262,27 @@ void Director::bindInputs()
// Inicializa JailAudio // Inicializa JailAudio
void Director::initJailAudio() void Director::initJailAudio()
{ {
JA_Init(48000, SDL_AUDIO_S16LE, 2);
if (options.audio.enabled)
{
JA_SetMusicVolume(to_JA_volume(options.audio.music.volume));
JA_SetSoundVolume(to_JA_volume(options.audio.sound.volume));
}
else
{
JA_SetMusicVolume(0);
JA_SetSoundVolume(0);
}
}
// Arranca SDL y crea la ventana
bool Director::initSDL()
{
// Indicador de éxito
auto success = true;
// Inicializa SDL // Inicializa SDL
if (!SDL_Init(SDL_INIT_VIDEO || SDL_INIT_AUDIO || SDL_INIT_GAMEPAD)) if (!SDL_Init(SDL_INIT_AUDIO))
{ {
std::cout << "SDL could not initialize!\nSDL Error: " << SDL_GetError() << std::endl; SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_AUDIO could not initialize! SDL Error: %s", SDL_GetError());
success = false;
} }
else else
{ {
// Obtiene información sobre la pantalla JA_Init(48000, SDL_AUDIO_S16LE, 2);
int i, num_displays = 0; if (options.audio.enabled)
SDL_DisplayID *displays = SDL_GetDisplays(&num_displays);
if (displays)
{ {
for (i = 0; i < num_displays; ++i) JA_SetMusicVolume(to_JA_volume(options.audio.music.volume));
{ JA_SetSoundVolume(to_JA_volume(options.audio.sound.volume));
SDL_DisplayID instance_id = displays[i];
const char *name = SDL_GetDisplayName(instance_id);
SDL_Log("Display %" SDL_PRIu32 ": %s", instance_id, name ? name : "Unknown");
}
auto DM = SDL_GetCurrentDisplayMode(displays[0]);
// Calcula el máximo factor de zoom que se puede aplicar a la pantalla
options.video.window.max_zoom = std::min(DM->w / param.game.width, DM->h / param.game.height);
options.video.window.zoom = std::min(options.video.window.zoom, options.video.window.max_zoom);
// Muestra información sobre el tamaño de la pantalla y de la ventana de juego
std::cout << "\nCurrent display mode: " << DM->w << "x" << DM->h << " @ " << DM->refresh_rate << "Hz" << std::endl;
std::cout << "Window resolution : " << param.game.width << "x" << param.game.height << " x" << options.video.window.zoom << std::endl;
options.video.info = std::to_string(DM->w) + " X " + std::to_string(DM->h) + " AT " + std::to_string(DM->refresh_rate) + " HZ";
SDL_free(displays);
}
// Establece el filtro de la textura
/*if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, std::to_string(static_cast<int>(options.video.filter)).c_str()))
{
std::cout << "Warning: texture filtering not enabled!\n";
}*/
if (!SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl"))
{
std::cout << "Warning: opengl not enabled!\n";
}
// Crea la ventana
window_ = SDL_CreateWindow(WINDOW_CAPTION, param.game.width * options.video.window.zoom, param.game.height * options.video.window.zoom, SDL_WINDOW_OPENGL);
if (!window_)
{
std::cout << "Window could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
success = false;
} }
else else
{ {
// Crea un renderizador para la ventana. El vsync se activa en funcion de las opciones JA_SetMusicVolume(0);
// Uint32 flags = 0; JA_SetSoundVolume(0);
if (options.video.v_sync)
{
// flags = SDL_RENDERER_PRESENTVSYNC;
}
// La aceleración se activa según el define
// flags = flags | SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE;
renderer_ = SDL_CreateRenderer(window_, nullptr);
if (!renderer_)
{
std::cout << "Renderer could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
success = false;
}
else
{
SDL_SetRenderDrawColor(renderer_, 0x00, 0x00, 0x00, 0xFF);
SDL_SetRenderLogicalPresentation(renderer_, param.game.width, param.game.height, SDL_LOGICAL_PRESENTATION_INTEGER_SCALE);
SDL_SetWindowFullscreen(window_, static_cast<Uint32>(options.video.fullscreen));
SDL_SetRenderDrawBlendMode(renderer_, SDL_BLENDMODE_BLEND);
}
} }
}
std::cout << std::endl; SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "SDL_AUDIO: Initialization complete.");
return success; }
} }
// Crea el indice de ficheros // Crea el indice de ficheros

View File

@@ -15,8 +15,7 @@ class Director
{ {
private: private:
// Objetos y punteros // Objetos y punteros
SDL_Window *window_; // La ventana donde dibujamos
SDL_Renderer *renderer_; // El renderizador de la ventana
#ifndef VERBOSE #ifndef VERBOSE
std::streambuf *orig_buf; // Puntero al buffer de flujo original para restaurar std::cout std::streambuf *orig_buf; // Puntero al buffer de flujo original para restaurar std::cout
#endif #endif
@@ -28,9 +27,6 @@ private:
// Inicializa jail_audio // Inicializa jail_audio
void initJailAudio(); void initJailAudio();
// Arranca SDL y crea la ventana
bool initSDL();
// Asigna los botones y teclas al objeto Input // Asigna los botones y teclas al objeto Input
void bindInputs(); void bindInputs();

View File

@@ -191,7 +191,7 @@ namespace globalInputs
{ {
if (Screen::get()->decWindowZoom()) if (Screen::get()->decWindowZoom())
{ {
Notifier::get()->show({lang::getText(131) + " x" + std::to_string(options.video.window.zoom)}); Notifier::get()->show({lang::getText(131) + " x" + std::to_string(options.window.zoom)});
} }
return; return;
} }
@@ -201,7 +201,7 @@ namespace globalInputs
{ {
if (Screen::get()->incWindowZoom()) if (Screen::get()->incWindowZoom())
{ {
Notifier::get()->show({lang::getText(131) + " x" + std::to_string(options.video.window.zoom)}); Notifier::get()->show({lang::getText(131) + " x" + std::to_string(options.window.zoom)});
} }
return; return;
} }

View File

@@ -1,5 +1,5 @@
#include "input.h" #include "input.h"
#include <SDL3/SDL_error.h> // Para SDL_GetError #include <SDL3/SDL.h> // Para SDL_GetError
#include <SDL3/SDL_init.h> // Para SDL_INIT_GAMEPAD, SDL_InitSubSystem #include <SDL3/SDL_init.h> // Para SDL_INIT_GAMEPAD, SDL_InitSubSystem
#include <SDL3/SDL_keyboard.h> // Para SDL_GetKeyboardState #include <SDL3/SDL_keyboard.h> // Para SDL_GetKeyboardState
#include <algorithm> // Para find #include <algorithm> // Para find
@@ -33,6 +33,9 @@ Input *Input::get()
Input::Input(const std::string &game_controller_db_path) Input::Input(const std::string &game_controller_db_path)
: game_controller_db_path_(game_controller_db_path) : game_controller_db_path_(game_controller_db_path)
{ {
// Inicializa el subsistema SDL_INIT_GAMEPAD
initSDL();
// Busca si hay mandos conectados // Busca si hay mandos conectados
discoverGameControllers(); discoverGameControllers();
@@ -221,11 +224,6 @@ bool Input::discoverGameControllers()
{ {
bool found = false; bool found = false;
if (SDL_WasInit(SDL_INIT_GAMEPAD) != 1)
{
SDL_InitSubSystem(SDL_INIT_GAMEPAD);
}
if (SDL_AddGamepadMappingsFromFile(game_controller_db_path_.c_str()) < 0) if (SDL_AddGamepadMappingsFromFile(game_controller_db_path_.c_str()) < 0)
{ {
std::cout << "Error, could not load " << game_controller_db_path_.c_str() << " file: " << SDL_GetError() << std::endl; std::cout << "Error, could not load " << game_controller_db_path_.c_str() << " file: " << SDL_GetError() << std::endl;
@@ -430,4 +428,19 @@ bool Input::checkAxisInput(InputAction input, int controller_index, bool repeat)
// Mantener el estado actual // Mantener el estado actual
return false; return false;
} }
}
void Input::initSDL()
{
if (SDL_WasInit(SDL_INIT_GAMEPAD) != 1)
{
if (!SDL_InitSubSystem(SDL_INIT_GAMEPAD))
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_GAMEPAD could not initialize! SDL Error: %s", SDL_GetError());
}
else
{
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "SDL_GAMEPAD: Initialization complete.");
}
}
} }

View File

@@ -100,6 +100,8 @@ private:
int num_gamepads_ = 0; // Número de mandos conectados int num_gamepads_ = 0; // Número de mandos conectados
std::string game_controller_db_path_; // Ruta al archivo gamecontrollerdb.txt std::string game_controller_db_path_; // Ruta al archivo gamecontrollerdb.txt
void initSDL();
// Comprueba el eje del mando // Comprueba el eje del mando
bool checkAxisInput(InputAction input, int controller_index, bool repeat); bool checkAxisInput(InputAction input, int controller_index, bool repeat);

View File

@@ -97,6 +97,7 @@ Uint32 JA_UpdateCallback(void *userdata, SDL_TimerID timerID, Uint32 interval)
if (time > (fade_start_time+fade_duration)) { if (time > (fade_start_time+fade_duration)) {
fading = false; fading = false;
JA_StopMusic(); JA_StopMusic();
return 30;
} else { } else {
const int time_passed = time - fade_start_time; const int time_passed = time - fade_start_time;
const float percent = (float)time_passed / (float)fade_duration; const float percent = (float)time_passed / (float)fade_duration;
@@ -150,7 +151,7 @@ void JA_Init(const int freq, const SDL_AudioFormat format, const int channels)
if (!sdlAudioDevice) SDL_CloseAudioDevice(sdlAudioDevice); if (!sdlAudioDevice) SDL_CloseAudioDevice(sdlAudioDevice);
sdlAudioDevice = SDL_OpenAudioDevice(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &JA_audioSpec); sdlAudioDevice = SDL_OpenAudioDevice(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &JA_audioSpec);
SDL_Log( (sdlAudioDevice==0) ? "Failed to initialize SDL audio!\n" : "OK!\n"); SDL_Log( (sdlAudioDevice==0) ? "Failed to initialize SDL audio!\n" : "OK!\n");
SDL_PauseAudioDevice(sdlAudioDevice); //SDL_PauseAudioDevice(sdlAudioDevice);
JA_timerID = SDL_AddTimer(30, JA_UpdateCallback, nullptr); JA_timerID = SDL_AddTimer(30, JA_UpdateCallback, nullptr);
} }
@@ -215,7 +216,7 @@ void JA_PlayMusic(JA_Music_t *music, const int loop)
if (!SDL_PutAudioStreamData(current_music->stream, current_music->buffer, current_music->length)) printf("[ERROR] SDL_PutAudioStreamData failed!\n"); if (!SDL_PutAudioStreamData(current_music->stream, current_music->buffer, current_music->length)) printf("[ERROR] SDL_PutAudioStreamData failed!\n");
SDL_SetAudioStreamGain(current_music->stream, JA_musicVolume); SDL_SetAudioStreamGain(current_music->stream, JA_musicVolume);
if (!SDL_BindAudioStream(sdlAudioDevice, current_music->stream)) printf("[ERROR] SDL_BindAudioStream failed!\n"); if (!SDL_BindAudioStream(sdlAudioDevice, current_music->stream)) printf("[ERROR] SDL_BindAudioStream failed!\n");
SDL_ResumeAudioStreamDevice(current_music->stream); //SDL_ResumeAudioStreamDevice(current_music->stream);
} }
void JA_PauseMusic() void JA_PauseMusic()
@@ -224,7 +225,8 @@ void JA_PauseMusic()
if (!current_music || current_music->state == JA_MUSIC_INVALID) return; if (!current_music || current_music->state == JA_MUSIC_INVALID) return;
current_music->state = JA_MUSIC_PAUSED; current_music->state = JA_MUSIC_PAUSED;
SDL_PauseAudioStreamDevice(current_music->stream); //SDL_PauseAudioStreamDevice(current_music->stream);
SDL_UnbindAudioStream(current_music->stream);
} }
void JA_ResumeMusic() void JA_ResumeMusic()
@@ -233,7 +235,8 @@ void JA_ResumeMusic()
if (!current_music || current_music->state == JA_MUSIC_INVALID) return; if (!current_music || current_music->state == JA_MUSIC_INVALID) return;
current_music->state = JA_MUSIC_PLAYING; current_music->state = JA_MUSIC_PLAYING;
SDL_ResumeAudioStreamDevice(current_music->stream); //SDL_ResumeAudioStreamDevice(current_music->stream);
SDL_BindAudioStream(sdlAudioDevice, current_music->stream);
} }
void JA_StopMusic() void JA_StopMusic()
@@ -243,7 +246,7 @@ void JA_StopMusic()
current_music->pos = 0; current_music->pos = 0;
current_music->state = JA_MUSIC_STOPPED; current_music->state = JA_MUSIC_STOPPED;
SDL_PauseAudioStreamDevice(current_music->stream); //SDL_PauseAudioStreamDevice(current_music->stream);
SDL_DestroyAudioStream(current_music->stream); SDL_DestroyAudioStream(current_music->stream);
current_music->stream = nullptr; current_music->stream = nullptr;
} }
@@ -388,7 +391,8 @@ void JA_PauseChannel(const int channel)
if (channels[i].state == JA_CHANNEL_PLAYING) if (channels[i].state == JA_CHANNEL_PLAYING)
{ {
channels[i].state = JA_CHANNEL_PAUSED; channels[i].state = JA_CHANNEL_PAUSED;
SDL_PauseAudioStreamDevice(channels[i].stream); //SDL_PauseAudioStreamDevice(channels[i].stream);
SDL_UnbindAudioStream(channels[i].stream);
} }
} }
else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS) else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS)
@@ -396,7 +400,8 @@ void JA_PauseChannel(const int channel)
if (channels[channel].state == JA_CHANNEL_PLAYING) if (channels[channel].state == JA_CHANNEL_PLAYING)
{ {
channels[channel].state = JA_CHANNEL_PAUSED; channels[channel].state = JA_CHANNEL_PAUSED;
SDL_PauseAudioStreamDevice(channels[channel].stream); //SDL_PauseAudioStreamDevice(channels[channel].stream);
SDL_UnbindAudioStream(channels[channel].stream);
} }
} }
} }
@@ -411,7 +416,8 @@ void JA_ResumeChannel(const int channel)
if (channels[i].state == JA_CHANNEL_PAUSED) if (channels[i].state == JA_CHANNEL_PAUSED)
{ {
channels[i].state = JA_CHANNEL_PLAYING; channels[i].state = JA_CHANNEL_PLAYING;
SDL_ResumeAudioStreamDevice(channels[i].stream); //SDL_ResumeAudioStreamDevice(channels[i].stream);
SDL_BindAudioStream(sdlAudioDevice, channels[i].stream);
} }
} }
else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS) else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS)
@@ -419,7 +425,8 @@ void JA_ResumeChannel(const int channel)
if (channels[channel].state == JA_CHANNEL_PAUSED) if (channels[channel].state == JA_CHANNEL_PAUSED)
{ {
channels[channel].state = JA_CHANNEL_PLAYING; channels[channel].state = JA_CHANNEL_PLAYING;
SDL_ResumeAudioStreamDevice(channels[channel].stream); //SDL_ResumeAudioStreamDevice(channels[channel].stream);
SDL_BindAudioStream(sdlAudioDevice, channels[channel].stream);
} }
} }
} }
@@ -432,6 +439,7 @@ void JA_StopChannel(const int channel)
{ {
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) { for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) {
if (channels[i].state != JA_CHANNEL_FREE) SDL_DestroyAudioStream(channels[i].stream); if (channels[i].state != JA_CHANNEL_FREE) SDL_DestroyAudioStream(channels[i].stream);
channels[channel].stream = nullptr;
channels[i].state = JA_CHANNEL_FREE; channels[i].state = JA_CHANNEL_FREE;
channels[i].pos = 0; channels[i].pos = 0;
channels[i].sound = NULL; channels[i].sound = NULL;
@@ -440,6 +448,7 @@ void JA_StopChannel(const int channel)
else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS) else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS)
{ {
if (channels[channel].state != JA_CHANNEL_FREE) SDL_DestroyAudioStream(channels[channel].stream); if (channels[channel].state != JA_CHANNEL_FREE) SDL_DestroyAudioStream(channels[channel].stream);
channels[channel].stream = nullptr;
channels[channel].state = JA_CHANNEL_FREE; channels[channel].state = JA_CHANNEL_FREE;
channels[channel].pos = 0; channels[channel].pos = 0;
channels[channel].sound = NULL; channels[channel].sound = NULL;

View File

@@ -18,18 +18,15 @@ bool setOptions(const std::string &var, const std::string &value);
// Inicializa las opciones del programa // Inicializa las opciones del programa
void initOptions() void initOptions()
{ {
options.window.caption = "Coffee Crisis Arcade Edition";
options.window.zoom = 2;
// Opciones de video // Opciones de video
#ifdef ANBERNIC
options.video.mode = false;
options.video.window.size = 3;
#else
options.video.fullscreen = false; options.video.fullscreen = false;
options.video.window.zoom = 2;
#endif
options.video.filter = ScreenFilter::NEAREST; options.video.filter = ScreenFilter::NEAREST;
options.video.v_sync = true; options.video.v_sync = true;
options.video.integer_scale = true; options.video.integer_scale = true;
options.video.shaders = true; options.video.shaders = false;
// Opciones de audio // Opciones de audio
options.audio.enabled = true; options.audio.enabled = true;
@@ -92,13 +89,12 @@ bool loadOptionsFile(std::string file_path)
// El fichero no existe // El fichero no existe
else else
{ // Crea el fichero con los valores por defecto {
// Crea el fichero con los valores por defecto
saveOptionsFile(file_path); saveOptionsFile(file_path);
} }
// Normaliza los valores // Normaliza los valores
options.video.window.zoom = std::clamp(options.video.window.zoom, 1, 4);
if (options.game.language != lang::Code::en_UK && if (options.game.language != lang::Code::en_UK &&
options.game.language != lang::Code::ba_BA && options.game.language != lang::Code::ba_BA &&
options.game.language != lang::Code::es_ES) options.game.language != lang::Code::es_ES)
@@ -127,8 +123,8 @@ bool saveOptionsFile(std::string file_path)
file << "## video.filter [" << static_cast<int>(ScreenFilter::NEAREST) << ": nearest, " << static_cast<int>(ScreenFilter::LINEAL) << ": lineal]\n"; file << "## video.filter [" << static_cast<int>(ScreenFilter::NEAREST) << ": nearest, " << static_cast<int>(ScreenFilter::LINEAL) << ": lineal]\n";
file << "\n"; file << "\n";
file << "window.zoom=" << options.window.zoom << "\n";
file << "video.fullscreen=" << boolToString(options.video.fullscreen) << "\n"; file << "video.fullscreen=" << boolToString(options.video.fullscreen) << "\n";
file << "video.window.size=" << options.video.window.zoom << "\n";
file << "video.filter=" << static_cast<int>(options.video.filter) << "\n"; file << "video.filter=" << static_cast<int>(options.video.filter) << "\n";
file << "video.v_sync=" << boolToString(options.video.v_sync) << "\n"; file << "video.v_sync=" << boolToString(options.video.v_sync) << "\n";
file << "video.integer_scale=" << boolToString(options.video.integer_scale) << "\n"; file << "video.integer_scale=" << boolToString(options.video.integer_scale) << "\n";
@@ -193,12 +189,12 @@ bool setOptions(const std::string &var, const std::string &value)
{ {
options.video.fullscreen = stringToBool(value); options.video.fullscreen = stringToBool(value);
} }
else if (var == "video.window.size") else if (var == "window.zoom")
{ {
options.video.window.zoom = std::stoi(value); options.window.zoom = std::stoi(value);
if ((options.video.window.zoom < 1) || (options.video.window.zoom > 4)) if ((options.window.zoom < 1) || (options.window.zoom > 4))
{ {
options.video.window.zoom = 3; options.window.zoom = 3;
} }
} }
else if (var == "video.filter") else if (var == "video.filter")

View File

@@ -22,20 +22,20 @@ enum class GameDifficulty
// Estructura para las opciones de la ventana // Estructura para las opciones de la ventana
struct WindowOptions struct WindowOptions
{ {
int zoom = 1; // Contiene el valor por el que se multiplica el tamaño de la ventana std::string caption; // Texto que aparece en la barra de titulo de la ventana
int max_zoom = 1; // Tamaño máximo para que el tamaño de la ventana no sea mayor que el tamaño de la pantalla int zoom = 1; // Contiene el valor por el que se multiplica el tamaño de la ventana
int max_zoom = 1; // Tamaño máximo para que el tamaño de la ventana no sea mayor que el tamaño de la pantalla
}; };
// Estructura con opciones para el video // Estructura con opciones para el video
struct VideoOptions struct VideoOptions
{ {
WindowOptions window; // Opciones para la ventana del programa ScreenFilter filter; // Filtro usado para el escalado de la imagen
ScreenFilter filter; // Filtro usado para el escalado de la imagen bool fullscreen; // Contiene el valor del modo de pantalla completa
bool fullscreen; // Contiene el valor del modo de pantalla completa bool v_sync; // Indica si se quiere usar vsync o no
bool v_sync; // Indica si se quiere usar vsync o no bool integer_scale; // Indica si se va a usar el escalado entero
bool integer_scale; // Indica si se va a usar el escalado entero bool shaders; // Indica si se van a usar shaders para los filtros de video
bool shaders; // Indica si se van a usar shaders para los filtros de video std::string info; // Información sobre el modo de video
std::string info; // Información sobre el modo de video
}; };
// Estructura para las opciones de musica // Estructura para las opciones de musica
@@ -99,6 +99,7 @@ struct GamepadOptions
// Estructura con todas las opciones de configuración del programa // Estructura con todas las opciones de configuración del programa
struct Options struct Options
{ {
WindowOptions window; // Opciones para la ventana del programa
GameOptions game; // Opciones para el propio juego GameOptions game; // Opciones para el propio juego
VideoOptions video; // Opciones relativas a la clase screen VideoOptions video; // Opciones relativas a la clase screen
AudioOptions audio; // Opciones para el audio AudioOptions audio; // Opciones para el audio

View File

@@ -1,6 +1,8 @@
#include "screen.h" #include "screen.h"
#include <SDL3/SDL.h> // Para SDL_PixelFormat
#include <SDL3/SDL_pixels.h> // Para SDL_PixelFormat #include <SDL3/SDL_pixels.h> // Para SDL_PixelFormat
#include <SDL3/SDL_timer.h> // Para SDL_GetTicks #include <SDL3/SDL_timer.h> // Para SDL_GetTicks
#include <SDL3/SDL_init.h> // Para SDL_Init, SDL_INIT_VIDEO
#include <algorithm> // Para max, min #include <algorithm> // Para max, min
#include <fstream> // Para basic_ifstream, ifstream #include <fstream> // Para basic_ifstream, ifstream
#include <iterator> // Para istreambuf_iterator, operator== #include <iterator> // Para istreambuf_iterator, operator==
@@ -19,7 +21,7 @@
Screen *Screen::screen_ = nullptr; Screen *Screen::screen_ = nullptr;
// [SINGLETON] Crearemos el objeto con esta función estática // [SINGLETON] Crearemos el objeto con esta función estática
void Screen::init(SDL_Window *window, SDL_Renderer *renderer) { Screen::screen_ = new Screen(window, renderer); } void Screen::init() { Screen::screen_ = new Screen(); }
// [SINGLETON] Destruiremos el objeto con esta función estática // [SINGLETON] Destruiremos el objeto con esta función estática
void Screen::destroy() { delete Screen::screen_; } void Screen::destroy() { delete Screen::screen_; }
@@ -28,13 +30,17 @@ void Screen::destroy() { delete Screen::screen_; }
Screen *Screen::get() { return Screen::screen_; } Screen *Screen::get() { return Screen::screen_; }
// Constructor // Constructor
Screen::Screen(SDL_Window *window, SDL_Renderer *renderer) Screen::Screen()
: window_(window), : src_rect_(SDL_FRect{0, 0, static_cast<float>(param.game.width), static_cast<float>(param.game.height)}),
renderer_(renderer),
game_canvas_(SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, param.game.width, param.game.height)),
src_rect_(SDL_FRect{0, 0, static_cast<float>(param.game.width), static_cast<float>(param.game.height)}),
dst_rect_(SDL_FRect{0, 0, static_cast<float>(param.game.width), static_cast<float>(param.game.height)}) dst_rect_(SDL_FRect{0, 0, static_cast<float>(param.game.width), static_cast<float>(param.game.height)})
{ {
// Arranca SDL VIDEO, crea la ventana y el renderizador
initSDL();
// Crea la textura de destino
game_canvas_ = SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, param.game.width, param.game.height);
SDL_SetTextureScaleMode(game_canvas_, SDL_SCALEMODE_NEAREST);
// Inicializa variables // Inicializa variables
adjustRenderLogicalSize(); adjustRenderLogicalSize();
@@ -46,7 +52,13 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer)
} }
// Destructor // Destructor
Screen::~Screen() { SDL_DestroyTexture(game_canvas_); } Screen::~Screen()
{
SDL_DestroyTexture(game_canvas_);
SDL_DestroyRenderer(renderer_);
SDL_DestroyWindow(window_);
}
// Limpia la pantalla // Limpia la pantalla
void Screen::clean(Color color) void Screen::clean(Color color)
@@ -109,7 +121,7 @@ void Screen::toggleFullscreen()
// Cambia el tamaño de la ventana // Cambia el tamaño de la ventana
void Screen::setWindowZoom(int zoom) void Screen::setWindowZoom(int zoom)
{ {
options.video.window.zoom = zoom; options.window.zoom = zoom;
adjustWindowSize(); adjustWindowSize();
} }
@@ -118,11 +130,11 @@ bool Screen::decWindowZoom()
{ {
if (!options.video.fullscreen) if (!options.video.fullscreen)
{ {
const int PREVIOUS_ZOOM = options.video.window.zoom; const int PREVIOUS_ZOOM = options.window.zoom;
--options.video.window.zoom; --options.window.zoom;
options.video.window.zoom = std::max(options.video.window.zoom, 1); options.window.zoom = std::max(options.window.zoom, 1);
if (options.video.window.zoom != PREVIOUS_ZOOM) if (options.window.zoom != PREVIOUS_ZOOM)
{ {
adjustWindowSize(); adjustWindowSize();
return true; return true;
@@ -137,11 +149,11 @@ bool Screen::incWindowZoom()
{ {
if (!options.video.fullscreen) if (!options.video.fullscreen)
{ {
const int PREVIOUS_ZOOM = options.video.window.zoom; const int PREVIOUS_ZOOM = options.window.zoom;
++options.video.window.zoom; ++options.window.zoom;
options.video.window.zoom = std::min(options.video.window.zoom, options.video.window.max_zoom); options.window.zoom = std::min(options.window.zoom, options.window.max_zoom);
if (options.video.window.zoom != PREVIOUS_ZOOM) if (options.window.zoom != PREVIOUS_ZOOM)
{ {
adjustWindowSize(); adjustWindowSize();
return true; return true;
@@ -236,13 +248,11 @@ void Screen::initShaders()
// Calcula el tamaño de la ventana // Calcula el tamaño de la ventana
void Screen::adjustWindowSize() void Screen::adjustWindowSize()
{ {
options.video.window.max_zoom = getMaxZoom();
// Establece el nuevo tamaño // Establece el nuevo tamaño
if (!options.video.fullscreen) if (!options.video.fullscreen)
{ {
const int WIDTH = param.game.width * options.video.window.zoom; const int WIDTH = param.game.width * options.window.zoom;
const int HEIGHT = param.game.height * options.video.window.zoom; const int HEIGHT = param.game.height * options.window.zoom;
int old_width, old_height; int old_width, old_height;
SDL_GetWindowSize(window_, &old_width, &old_height); SDL_GetWindowSize(window_, &old_width, &old_height);
@@ -263,21 +273,6 @@ void Screen::adjustWindowSize()
} }
} }
// Obtiene el tamaño máximo de zoom posible para la ventana
int Screen::getMaxZoom()
{
// Obtiene información sobre la pantalla
auto DM = SDL_GetCurrentDisplayMode(0);
// Calcula el máximo factor de zoom que se puede aplicar a la pantalla
const int MAX_ZOOM = std::min(DM->w / param.game.width, (DM->h - WINDOWS_DECORATIONS_) / param.game.height);
// Normaliza los valores de zoom
options.video.window.zoom = std::min(options.video.window.zoom, MAX_ZOOM);
return MAX_ZOOM;
}
// Renderiza todos los overlays y efectos // Renderiza todos los overlays y efectos
void Screen::renderOverlays() void Screen::renderOverlays()
{ {
@@ -298,4 +293,113 @@ void Screen::renderAttenuate()
SDL_SetRenderDrawColor(renderer_, 0, 0, 0, 64); SDL_SetRenderDrawColor(renderer_, 0, 0, 0, 64);
SDL_RenderFillRect(renderer_, nullptr); SDL_RenderFillRect(renderer_, nullptr);
} }
}
// Arranca SDL VIDEO y crea la ventana
bool Screen::initSDL()
{
// Indicador de éxito
auto success = true;
// Inicializa SDL
if (!SDL_Init(SDL_INIT_VIDEO))
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_VIDEO could not initialize! SDL Error: %s", SDL_GetError());
success = false;
}
else
{
getDisplayInfo();
// Establece el filtro de la textura
/*if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, std::to_string(static_cast<int>(options.video.filter)).c_str()))
{
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Warning: texture filtering not enabled!");
}*/
if (!SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl"))
{
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Warning: opengl not enabled!");
}
// Crea la ventana
window_ = SDL_CreateWindow(options.window.caption.c_str(), param.game.width * options.window.zoom, param.game.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());
success = false;
}
else
{
// Crea un renderizador para la ventana. El vsync se activa en función de las opciones
// Uint32 flags = 0;
if (options.video.v_sync)
{
// flags = SDL_RENDERER_PRESENTVSYNC;
}
// La aceleración se activa según el define
// flags = flags | SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE;
renderer_ = SDL_CreateRenderer(window_, nullptr);
if (!renderer_)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Renderer could not be created! SDL Error: %s", SDL_GetError());
success = false;
}
else
{
SDL_SetRenderDrawColor(renderer_, 0x00, 0x00, 0x00, 0xFF);
SDL_SetRenderLogicalPresentation(renderer_, param.game.width, param.game.height, SDL_LOGICAL_PRESENTATION_INTEGER_SCALE);
SDL_SetWindowFullscreen(window_, static_cast<Uint32>(options.video.fullscreen));
SDL_SetRenderDrawBlendMode(renderer_, SDL_BLENDMODE_BLEND);
}
}
}
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "SDL_VIDEO: Initialization complete.");
return success;
}
// Obtiene información sobre la pantalla
void Screen::getDisplayInfo()
{
int i, num_displays = 0;
SDL_DisplayID *displays = SDL_GetDisplays(&num_displays);
if (displays)
{
for (i = 0; i < num_displays; ++i)
{
SDL_DisplayID instance_id = displays[i];
const char *name = SDL_GetDisplayName(instance_id);
SDL_Log("Display %" SDL_PRIu32 ": %s", instance_id, name ? name : "Unknown");
}
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 / param.game.width, DM->h / param.game.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
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));
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Window resolution: %dx%d x%d",
static_cast<int>(param.game.width), static_cast<int>(param.game.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 / param.game.width, (DM->h - WINDOWS_DECORATIONS_) / param.game.height);
// Normaliza los valores de zoom
options.window.zoom = std::min(options.window.zoom, MAX_ZOOM);
SDL_free(displays);
}
} }

View File

@@ -169,6 +169,9 @@ private:
bool show_debug_info_ = false; // Indica si ha de mostrar/ocultar la información de la pantalla bool show_debug_info_ = false; // Indica si ha de mostrar/ocultar la información de la pantalla
#endif #endif
// Arranca SDL VIDEO y crea la ventana
bool initSDL();
// Dibuja el efecto de flash en la pantalla // Dibuja el efecto de flash en la pantalla
void renderFlash(); void renderFlash();
@@ -193,8 +196,8 @@ private:
// Ajusta el tamaño lógico del renderizador // Ajusta el tamaño lógico del renderizador
void adjustRenderLogicalSize() { SDL_SetRenderLogicalPresentation(renderer_, param.game.width, param.game.height, SDL_LOGICAL_PRESENTATION_INTEGER_SCALE); } void adjustRenderLogicalSize() { SDL_SetRenderLogicalPresentation(renderer_, param.game.width, param.game.height, SDL_LOGICAL_PRESENTATION_INTEGER_SCALE); }
// Obtiene el tamaño máximo de zoom posible para la ventana // Obtiene información sobre la pantalla
int getMaxZoom(); void getDisplayInfo();
// Renderiza todos los overlays y efectos // Renderiza todos los overlays y efectos
void renderOverlays(); void renderOverlays();
@@ -203,14 +206,14 @@ private:
void renderAttenuate(); void renderAttenuate();
// Constructor // Constructor
Screen(SDL_Window *window, SDL_Renderer *renderer); Screen();
// Destructor // Destructor
~Screen(); ~Screen();
public: public:
// [SINGLETON] Crearemos el objeto con esta función estática // [SINGLETON] Crearemos el objeto con esta función estática
static void init(SDL_Window *window, SDL_Renderer *renderer); static void init();
// [SINGLETON] Destruiremos el objeto con esta función estática // [SINGLETON] Destruiremos el objeto con esta función estática
static void destroy(); static void destroy();