afegit vsync toggle

This commit is contained in:
2025-12-02 09:44:58 +01:00
parent c26a4774a1
commit 8803fc3806
5 changed files with 55 additions and 4 deletions

View File

@@ -32,7 +32,7 @@ SDLManager::SDLManager()
// Calcular mida màxima des del display // Calcular mida màxima des del display
calculateMaxWindowSize(); calculateMaxWindowSize();
// Construir títol dinàmic igual que en pollo // Construir títol dinàmic
std::string window_title = std::format("{} v{} ({})", Project::LONG_NAME, Project::VERSION, Project::COPYRIGHT); std::string window_title = std::format("{} v{} ({})", Project::LONG_NAME, Project::VERSION, Project::COPYRIGHT);
// Crear finestra CENTRADA (SDL ho fa automàticament amb CENTERED) // Crear finestra CENTRADA (SDL ho fa automàticament amb CENTERED)
@@ -60,6 +60,9 @@ SDLManager::SDLManager()
return; return;
} }
// Aplicar configuració de V-Sync
SDL_SetRenderVSync(renderer_, Options::rendering.vsync);
// CRÍTIC: Configurar viewport scaling // CRÍTIC: Configurar viewport scaling
updateLogicalPresentation(); updateLogicalPresentation();
@@ -122,6 +125,9 @@ SDLManager::SDLManager(int width, int height, bool fullscreen)
return; return;
} }
// Aplicar configuració de V-Sync
SDL_SetRenderVSync(renderer_, Options::rendering.vsync);
// Configurar viewport scaling // Configurar viewport scaling
updateLogicalPresentation(); updateLogicalPresentation();
@@ -343,3 +349,17 @@ void SDLManager::setWindowTitle(const std::string& title) {
SDL_SetWindowTitle(finestra_, title.c_str()); SDL_SetWindowTitle(finestra_, title.c_str());
} }
} }
// [NUEVO] Toggle V-Sync (F4)
void SDLManager::toggleVSync() {
// Toggle: 1 → 0 → 1
Options::rendering.vsync = (Options::rendering.vsync == 1) ? 0 : 1;
// Aplicar a SDL
if (renderer_) {
SDL_SetRenderVSync(renderer_, Options::rendering.vsync);
}
// Guardar configuració
Options::saveToFile();
}

View File

@@ -26,6 +26,7 @@ class SDLManager {
void increaseWindowSize(); // F2: +100px void increaseWindowSize(); // F2: +100px
void decreaseWindowSize(); // F1: -100px void decreaseWindowSize(); // F1: -100px
void toggleFullscreen(); // F3 void toggleFullscreen(); // F3
void toggleVSync(); // F4
bool bool
handleWindowEvent(const SDL_Event& event); // Per a SDL_EVENT_WINDOW_RESIZED handleWindowEvent(const SDL_Event& event); // Per a SDL_EVENT_WINDOW_RESIZED

View File

@@ -13,6 +13,7 @@
#include "../../core/rendering/sdl_manager.hpp" #include "../../core/rendering/sdl_manager.hpp"
#include "../../core/types.hpp" #include "../../core/types.hpp"
#include "../effects/debris_manager.hpp" #include "../effects/debris_manager.hpp"
#include "core/defaults.hpp"
class EscenaLogo { class EscenaLogo {
public: public:
@@ -64,8 +65,8 @@ class EscenaLogo {
// Constants d'animació seqüencial // Constants d'animació seqüencial
static constexpr float THRESHOLD_LETRA = 0.6f; // Umbral per activar següent lletra (0.0-1.0) static constexpr float THRESHOLD_LETRA = 0.6f; // Umbral per activar següent lletra (0.0-1.0)
static constexpr float ORIGEN_ZOOM_X = 640.0f / 2.0f; // Punt inicial X del zoom (320) static constexpr float ORIGEN_ZOOM_X = Defaults::Game::WIDTH * 0.5f; // Punt inicial X del zoom
static constexpr float ORIGEN_ZOOM_Y = 480.0f * 0.4f; // Punt inicial Y del zoom (240) static constexpr float ORIGEN_ZOOM_Y = Defaults::Game::HEIGHT * 0.4f; // Punt inicial Y del zoom
// Mètodes privats // Mètodes privats
void inicialitzar_lletres(); void inicialitzar_lletres();

View File

@@ -36,6 +36,9 @@ void init() {
gameplay.max_enemies = Defaults::Entities::MAX_ORNIS; gameplay.max_enemies = Defaults::Entities::MAX_ORNIS;
gameplay.max_bullets = Defaults::Entities::MAX_BALES; gameplay.max_bullets = Defaults::Entities::MAX_BALES;
// Rendering
rendering.vsync = Defaults::Rendering::VSYNC_DEFAULT;
// Version // Version
version = std::string(Project::VERSION); version = std::string(Project::VERSION);
} }
@@ -181,6 +184,22 @@ static void loadGameplayConfigFromYaml(const fkyaml::node& yaml) {
} }
} }
static void loadRenderingConfigFromYaml(const fkyaml::node& yaml) {
if (yaml.contains("rendering")) {
const auto& rend = yaml["rendering"];
if (rend.contains("vsync")) {
try {
int val = rend["vsync"].get_value<int>();
// Validar: només 0 o 1
rendering.vsync = (val == 0 || val == 1) ? val : Defaults::Rendering::VSYNC_DEFAULT;
} catch (...) {
rendering.vsync = Defaults::Rendering::VSYNC_DEFAULT;
}
}
}
}
// Carregar configuració des del fitxer YAML // Carregar configuració des del fitxer YAML
auto loadFromFile() -> bool { auto loadFromFile() -> bool {
const std::string CONFIG_VERSION = std::string(Project::VERSION); const std::string CONFIG_VERSION = std::string(Project::VERSION);
@@ -226,6 +245,7 @@ auto loadFromFile() -> bool {
loadWindowConfigFromYaml(yaml); loadWindowConfigFromYaml(yaml);
loadPhysicsConfigFromYaml(yaml); loadPhysicsConfigFromYaml(yaml);
loadGameplayConfigFromYaml(yaml); loadGameplayConfigFromYaml(yaml);
loadRenderingConfigFromYaml(yaml);
if (console) { if (console) {
std::cout << "Config carregada correctament des de: " << config_file_path std::cout << "Config carregada correctament des de: " << config_file_path
@@ -285,7 +305,11 @@ auto saveToFile() -> bool {
file << "# GAMEPLAY\n"; file << "# GAMEPLAY\n";
file << "gameplay:\n"; file << "gameplay:\n";
file << " max_enemies: " << gameplay.max_enemies << "\n"; file << " max_enemies: " << gameplay.max_enemies << "\n";
file << " max_bullets: " << gameplay.max_bullets << "\n"; file << " max_bullets: " << gameplay.max_bullets << "\n\n";
file << "# RENDERITZACIÓ\n";
file << "rendering:\n";
file << " vsync: " << rendering.vsync << " # 0=disabled, 1=enabled\n";
file.close(); file.close();

View File

@@ -27,6 +27,10 @@ struct Gameplay {
int max_bullets{3}; int max_bullets{3};
}; };
struct Rendering {
int vsync{1}; // 0=disabled, 1=enabled
};
// Variables globals (inline per evitar ODR violations) // Variables globals (inline per evitar ODR violations)
inline std::string version{}; // Versió del config per validació inline std::string version{}; // Versió del config per validació
@@ -34,6 +38,7 @@ inline bool console{false}; // Eixida de debug
inline Window window{}; inline Window window{};
inline Physics physics{}; inline Physics physics{};
inline Gameplay gameplay{}; inline Gameplay gameplay{};
inline Rendering rendering{};
inline std::string config_file_path{}; // Establert per setConfigFile() inline std::string config_file_path{}; // Establert per setConfigFile()