arreglos en screen

This commit is contained in:
2026-04-15 06:30:23 +02:00
parent e0498d642d
commit f9b0f64b81
10 changed files with 256 additions and 255 deletions

View File

@@ -45,11 +45,6 @@ EM_BOOL onEmFullscreenChange(int /*eventType*/, const EmscriptenFullscreenChange
return EM_FALSE;
}
EM_BOOL onEmResize(int /*eventType*/, const EmscriptenUiEvent * /*event*/, void * /*userData*/) {
emscripten_async_call(deferredCanvasResize, nullptr, 0);
return EM_FALSE;
}
EM_BOOL onEmOrientationChange(int /*eventType*/, const EmscriptenOrientationChangeEvent * /*event*/, void * /*userData*/) {
emscripten_async_call(deferredCanvasResize, nullptr, 0);
return EM_FALSE;
@@ -80,12 +75,12 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, options
}
if (gameCanvas == nullptr) {
if (options->console) {
std::cout << "TitleSurface could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
std::cout << "gameCanvas could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
}
}
// Establece el modo de video
setVideoMode(options->videoMode);
setVideoMode(options->videoMode != 0);
// Inicializa el sistema de notificaciones
notificationText = new Text(asset->get("8bithud.png"), asset->get("8bithud.txt"), renderer);
@@ -95,7 +90,7 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, options
notificationEndTime = 0;
notificationY = 2;
// Registra callbacks natius d'Emscripten per a fullscreen/resize/orientation
// Registra callbacks natius d'Emscripten per a fullscreen/orientation
registerEmscriptenEventCallbacks();
}
@@ -137,126 +132,77 @@ void Screen::blit() {
SDL_RenderPresent(renderer);
}
// ============================================================================
// Video y ventana
// ============================================================================
// Establece el modo de video
void Screen::setVideoMode(int videoMode) {
// Aplica el modo de video
SDL_SetWindowFullscreen(window, videoMode != 0);
// Si está activo el modo ventana quita el borde
if (videoMode == 0) {
// Muestra el puntero y reinicia el temporizador de inactividad
SDL_ShowCursor();
Mouse::cursorVisible = true;
Mouse::lastMouseMoveTime = SDL_GetTicks();
// Esconde la ventana
// SDL_HideWindow(window);
if (options->borderEnabled) {
windowWidth = gameCanvasWidth + borderWidth;
windowHeight = gameCanvasHeight + borderHeight;
dest = {0 + (borderWidth / 2), 0 + (borderHeight / 2), gameCanvasWidth, gameCanvasHeight};
}
else {
windowWidth = gameCanvasWidth;
windowHeight = gameCanvasHeight;
dest = {0, 0, gameCanvasWidth, gameCanvasHeight};
}
#ifdef __EMSCRIPTEN__
// En WASM el tamaño de ventana está fijado a 1x, así que
// escalamos el renderizado por 3 aprovechando el modo NEAREST
// de la textura del juego para que los píxeles salgan nítidos.
constexpr int WASM_RENDER_SCALE = 3;
windowWidth *= WASM_RENDER_SCALE;
windowHeight *= WASM_RENDER_SCALE;
dest.w *= WASM_RENDER_SCALE;
dest.h *= WASM_RENDER_SCALE;
#endif
// Modifica el tamaño de la ventana
SDL_SetWindowSize(window, windowWidth * options->windowSize, windowHeight * options->windowSize);
SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
// Muestra la ventana
// SDL_ShowWindow(window);
void Screen::setVideoMode(bool fullscreen) {
applyFullscreen(fullscreen);
if (fullscreen) {
applyFullscreenLayout();
} else {
applyWindowedLayout();
}
// Si está activo el modo de pantalla completa añade el borde
else if (videoMode == SDL_WINDOW_FULLSCREEN) {
// Oculta el puntero
SDL_HideCursor();
Mouse::cursorVisible = false;
// Obten el alto y el ancho de la ventana
SDL_GetWindowSize(window, &windowWidth, &windowHeight);
// Aplica el escalado al rectangulo donde se pinta la textura del juego
if (options->integerScale) {
// Calcula el tamaño de la escala máxima
int scale = 0;
while (((gameCanvasWidth * (scale + 1)) <= windowWidth) && ((gameCanvasHeight * (scale + 1)) <= windowHeight)) {
scale++;
}
dest.w = gameCanvasWidth * scale;
dest.h = gameCanvasHeight * scale;
dest.x = (windowWidth - dest.w) / 2;
dest.y = (windowHeight - dest.h) / 2;
} else if (options->keepAspect) {
float ratio = (float)gameCanvasWidth / (float)gameCanvasHeight;
if ((windowWidth - gameCanvasWidth) >= (windowHeight - gameCanvasHeight)) {
dest.h = windowHeight;
dest.w = (int)((windowHeight * ratio) + 0.5f);
dest.x = (windowWidth - dest.w) / 2;
dest.y = (windowHeight - dest.h) / 2;
} else {
dest.w = windowWidth;
dest.h = (int)((windowWidth / ratio) + 0.5f);
dest.x = (windowWidth - dest.w) / 2;
dest.y = (windowHeight - dest.h) / 2;
}
} else {
dest.w = windowWidth;
dest.h = windowHeight;
dest.x = dest.y = 0;
}
}
// Modifica el tamaño del renderizador
SDL_SetRenderLogicalPresentation(renderer, windowWidth, windowHeight, SDL_LOGICAL_PRESENTATION_LETTERBOX);
// Actualiza las opciones
options->videoMode = videoMode;
options->screen.windowWidth = windowWidth;
options->screen.windowHeight = windowHeight;
applyLogicalPresentation(fullscreen);
}
// Camibia entre pantalla completa y ventana
void Screen::switchVideoMode() {
options->videoMode = (options->videoMode == 0) ? SDL_WINDOW_FULLSCREEN : 0;
setVideoMode(options->videoMode);
// Cambia entre pantalla completa y ventana
void Screen::toggleVideoMode() {
setVideoMode(options->videoMode == 0);
}
// Cambia el tamaño de la ventana
void Screen::setWindowSize(int size) {
options->windowSize = size;
setVideoMode(0);
// Reduce el zoom de la ventana
auto Screen::decWindowZoom() -> bool {
if (options->videoMode != 0) { return false; }
const int PREV = options->windowSize;
options->windowSize = std::max(options->windowSize - 1, WINDOW_ZOOM_MIN);
if (options->windowSize == PREV) { return false; }
setVideoMode(false);
return true;
}
// Reduce el tamaño de la ventana
void Screen::decWindowSize() {
--options->windowSize;
options->windowSize = std::max(options->windowSize, 1);
setVideoMode(0);
// Aumenta el zoom de la ventana
auto Screen::incWindowZoom() -> bool {
if (options->videoMode != 0) { return false; }
const int PREV = options->windowSize;
options->windowSize = std::min(options->windowSize + 1, WINDOW_ZOOM_MAX);
if (options->windowSize == PREV) { return false; }
setVideoMode(false);
return true;
}
// Aumenta el tamaño de la ventana
void Screen::incWindowSize() {
++options->windowSize;
options->windowSize = std::min(options->windowSize, 4);
setVideoMode(0);
// Establece el zoom de la ventana directamente
auto Screen::setWindowZoom(int zoom) -> bool {
if (options->videoMode != 0) { return false; }
if (zoom < WINDOW_ZOOM_MIN || zoom > WINDOW_ZOOM_MAX) { return false; }
if (zoom == options->windowSize) { return false; }
options->windowSize = zoom;
setVideoMode(false);
return true;
}
// Establece el escalado entero
void Screen::setIntegerScale(bool enabled) {
if (options->integerScale == enabled) { return; }
options->integerScale = enabled;
setVideoMode(options->videoMode != 0);
}
// Alterna el escalado entero
void Screen::toggleIntegerScale() {
setIntegerScale(!options->integerScale);
}
// Establece el V-Sync
void Screen::setVSync(bool enabled) {
options->vSync = enabled;
SDL_SetRenderVSync(renderer, enabled ? 1 : SDL_RENDERER_VSYNC_DISABLED);
}
// Alterna el V-Sync
void Screen::toggleVSync() {
setVSync(!options->vSync);
}
// Cambia el color del borde
@@ -264,32 +210,100 @@ void Screen::setBorderColor(color_t color) {
borderColor = color;
}
// Cambia el tipo de mezcla
void Screen::setBlendMode(SDL_BlendMode blendMode) {
SDL_SetRenderDrawBlendMode(renderer, blendMode);
// ============================================================================
// Helpers privados de setVideoMode
// ============================================================================
// SDL_SetWindowFullscreen + visibilidad del cursor
void Screen::applyFullscreen(bool fullscreen) {
SDL_SetWindowFullscreen(window, fullscreen);
if (fullscreen) {
SDL_HideCursor();
Mouse::cursorVisible = false;
} else {
SDL_ShowCursor();
Mouse::cursorVisible = true;
Mouse::lastMouseMoveTime = SDL_GetTicks();
}
}
// Establece el tamaño del borde
void Screen::setBorderWidth(int s) {
options->borderWidth = s;
// Calcula windowWidth/Height/dest para el modo ventana y aplica SDL_SetWindowSize
void Screen::applyWindowedLayout() {
if (options->borderEnabled) {
windowWidth = gameCanvasWidth + borderWidth;
windowHeight = gameCanvasHeight + borderHeight;
dest = {0 + (borderWidth / 2), 0 + (borderHeight / 2), gameCanvasWidth, gameCanvasHeight};
} else {
windowWidth = gameCanvasWidth;
windowHeight = gameCanvasHeight;
dest = {0, 0, gameCanvasWidth, gameCanvasHeight};
}
#ifdef __EMSCRIPTEN__
windowWidth *= WASM_RENDER_SCALE;
windowHeight *= WASM_RENDER_SCALE;
dest.w *= WASM_RENDER_SCALE;
dest.h *= WASM_RENDER_SCALE;
#endif
// Modifica el tamaño de la ventana
SDL_SetWindowSize(window, windowWidth * options->windowSize, windowHeight * options->windowSize);
SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
}
// Establece el tamaño del borde
void Screen::setBorderHeight(int s) {
options->borderHeight = s;
// Obtiene el tamaño de la ventana en fullscreen y calcula el rect del juego
void Screen::applyFullscreenLayout() {
SDL_GetWindowSize(window, &windowWidth, &windowHeight);
computeFullscreenGameRect();
}
// Establece si se ha de ver el borde en el modo ventana
void Screen::setBorderEnabled(bool value) {
options->borderEnabled = value;
// Calcula el rectángulo dest para fullscreen: integerScale / keepAspect / stretched
void Screen::computeFullscreenGameRect() {
if (options->integerScale) {
// Calcula el tamaño de la escala máxima
int scale = 0;
while (((gameCanvasWidth * (scale + 1)) <= windowWidth) && ((gameCanvasHeight * (scale + 1)) <= windowHeight)) {
scale++;
}
dest.w = gameCanvasWidth * scale;
dest.h = gameCanvasHeight * scale;
dest.x = (windowWidth - dest.w) / 2;
dest.y = (windowHeight - dest.h) / 2;
} else if (options->keepAspect) {
float ratio = (float)gameCanvasWidth / (float)gameCanvasHeight;
if ((windowWidth - gameCanvasWidth) >= (windowHeight - gameCanvasHeight)) {
dest.h = windowHeight;
dest.w = (int)((windowHeight * ratio) + 0.5f);
dest.x = (windowWidth - dest.w) / 2;
dest.y = (windowHeight - dest.h) / 2;
} else {
dest.w = windowWidth;
dest.h = (int)((windowWidth / ratio) + 0.5f);
dest.x = (windowWidth - dest.w) / 2;
dest.y = (windowHeight - dest.h) / 2;
}
} else {
dest.w = windowWidth;
dest.h = windowHeight;
dest.x = dest.y = 0;
}
}
// Cambia entre borde visible y no visible
void Screen::switchBorder() {
options->borderEnabled = !options->borderEnabled;
setVideoMode(0);
// Aplica la logical presentation y persiste el estado en options
void Screen::applyLogicalPresentation(bool fullscreen) {
SDL_SetRenderLogicalPresentation(renderer, windowWidth, windowHeight, SDL_LOGICAL_PRESENTATION_LETTERBOX);
// Actualiza las opciones
options->videoMode = fullscreen ? SDL_WINDOW_FULLSCREEN : 0;
options->screen.windowWidth = windowWidth;
options->screen.windowHeight = windowHeight;
}
// ============================================================================
// Notificaciones
// ============================================================================
// Muestra una notificación en la línea superior durante durationMs
void Screen::notify(const std::string &text, color_t textColor, color_t outlineColor, Uint32 durationMs) {
notificationMessage = text;
@@ -304,35 +318,6 @@ void Screen::clearNotification() {
notificationMessage.clear();
}
// --- Fix per a fullscreen/resize en Emscripten ---
// Vore el bloc de comentaris a dalt i l'anonymous namespace amb els callbacks.
void Screen::handleCanvasResized() {
#ifdef __EMSCRIPTEN__
// La crida a SDL_SetWindowFullscreen + SDL_SetRenderLogicalPresentation
// que fa setVideoMode és l'única manera de resincronitzar l'estat intern
// de SDL amb el canvas HTML real.
setVideoMode(options->videoMode);
#endif
}
void Screen::syncFullscreenFlagFromBrowser(bool isFullscreen) {
#ifdef __EMSCRIPTEN__
options->videoMode = isFullscreen ? SDL_WINDOW_FULLSCREEN : 0;
#else
(void)isFullscreen;
#endif
}
void Screen::registerEmscriptenEventCallbacks() {
#ifdef __EMSCRIPTEN__
g_screen_instance = this;
emscripten_set_fullscreenchange_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, nullptr, EM_FALSE, onEmFullscreenChange);
emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, nullptr, EM_FALSE, onEmResize);
emscripten_set_orientationchange_callback(nullptr, EM_FALSE, onEmOrientationChange);
#endif
}
// Dibuja la notificación activa (si la hay) sobre el gameCanvas
void Screen::renderNotification() {
if (SDL_GetTicks() >= notificationEndTime) {
@@ -346,4 +331,38 @@ void Screen::renderNotification() {
notificationTextColor,
1,
notificationOutlineColor);
}
}
// ============================================================================
// Emscripten — fix per a fullscreen/resize (veure el bloc de comentaris al
// principi del fitxer i l'anonymous namespace amb els callbacks natius).
// ============================================================================
void Screen::handleCanvasResized() {
#ifdef __EMSCRIPTEN__
// La crida a SDL_SetWindowFullscreen + SDL_SetRenderLogicalPresentation
// que fa setVideoMode és l'única manera de resincronitzar l'estat intern
// de SDL amb el canvas HTML real.
setVideoMode(options->videoMode != 0);
#endif
}
void Screen::syncFullscreenFlagFromBrowser(bool isFullscreen) {
#ifdef __EMSCRIPTEN__
options->videoMode = isFullscreen ? SDL_WINDOW_FULLSCREEN : 0;
#else
(void)isFullscreen;
#endif
}
void Screen::registerEmscriptenEventCallbacks() {
#ifdef __EMSCRIPTEN__
// IMPORTANT: NO registrem resize callback. En mòbil, fer scroll fa que el
// navegador oculti/mostri la barra d'URL, disparant un resize del DOM per
// cada scroll. Això portava a cridar setVideoMode per cada scroll, que
// re-aplicava la logical presentation i corrompia el viewport intern de SDL.
g_screen_instance = this;
emscripten_set_fullscreenchange_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, nullptr, EM_TRUE, onEmFullscreenChange);
emscripten_set_orientationchange_callback(nullptr, EM_TRUE, onEmOrientationChange);
#endif
}