From 9a4b3b04a537cedfefb256fe34fb1ac9cabe7e08 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Thu, 16 Oct 2025 10:12:03 +0200 Subject: [PATCH] Corregits mil warnings de int a float Corregit getDisplayInfo per al calcul del zoomMax --- source/options.h | 1 + source/screen.cpp | 71 ++++++++++++++++------- source/screen.h | 15 ++++- source/sections/credits.cpp | 2 +- source/sections/ending.cpp | 14 ++--- source/sprite/surface_animated_sprite.cpp | 6 +- source/surface.cpp | 8 +-- source/text.cpp | 4 +- source/texture.cpp | 5 +- source/texture.h | 6 +- source/ui/notifier.cpp | 10 ++-- source/ui/notifier.h | 4 +- source/utils.cpp | 4 +- 13 files changed, 95 insertions(+), 55 deletions(-) diff --git a/source/options.h b/source/options.h index 9dd3e90..39d75e0 100644 --- a/source/options.h +++ b/source/options.h @@ -251,6 +251,7 @@ struct OptionsVideo { bool keep_aspect; // Indica si se ha de mantener la relación de aspecto al poner el modo a pantalla completa Border border; // Borde de la pantalla std::string palette; // Paleta de colores a usar en el juego + std::string info; // Información sobre el modo de vídeo // Constructor por defecto OptionsVideo() diff --git a/source/screen.cpp b/source/screen.cpp index c1538b1..9a9ac94 100644 --- a/source/screen.cpp +++ b/source/screen.cpp @@ -42,8 +42,7 @@ Screen::Screen(SDL_Window* window, SDL_Renderer* renderer) renderer_(renderer), palettes_(Asset::get()->getListByType(AssetType::PALETTE)) { // Inicializa variables - auto display_mode = SDL_GetCurrentDisplayMode(0); - info_resolution_ = std::to_string(display_mode->w) + " X " + std::to_string(display_mode->h) + " AT " + std::to_string(display_mode->refresh_rate) + " HZ"; + getDisplayInfo(); // Ajusta los tamaños game_surface_dstrect_ = {options.video.border.width, options.video.border.height, options.game.width, options.game.height}; @@ -225,7 +224,6 @@ void Screen::adjustWindowSize() { window_width_ = options.game.width + (options.video.border.enabled ? options.video.border.width * 2 : 0); window_height_ = options.game.height + (options.video.border.enabled ? options.video.border.height * 2 : 0); - options.window.max_zoom = getMaxZoom(); // Establece el nuevo tamaño if (options.video.fullscreen == 0) { @@ -239,7 +237,7 @@ void Screen::adjustWindowSize() { const int NEW_POS_Y = old_pos_y + (old_height - (window_height_ * options.window.zoom)) / 2; SDL_SetWindowSize(window_, window_width_ * options.window.zoom, window_height_ * options.window.zoom); - SDL_SetWindowPosition(window_, std::max(NEW_POS_X, WINDOWS_DECORATIONS_), std::max(NEW_POS_Y, 0)); + SDL_SetWindowPosition(window_, std::max(NEW_POS_X, WINDOWS_DECORATIONS), std::max(NEW_POS_Y, 0)); } } @@ -248,20 +246,6 @@ void Screen::adjustRenderLogicalSize() { SDL_SetRenderLogicalPresentation(renderer_, window_width_, window_height_, options.video.integer_scale ? SDL_LOGICAL_PRESENTATION_INTEGER_SCALE : SDL_LOGICAL_PRESENTATION_LETTERBOX); } -// Obtiene el tamaño máximo de zoom posible para la ventana -int Screen::getMaxZoom() { - // Obtiene información sobre la pantalla - auto display_mode = SDL_GetCurrentDisplayMode(0); - - // Calcula el máximo factor de zoom que se puede aplicar a la pantalla - const int MAX_ZOOM = std::min(display_mode->w / window_width_, (display_mode->h - WINDOWS_DECORATIONS_) / window_height_); - - // Normaliza los valores de zoom - options.window.zoom = std::min(options.window.zoom, MAX_ZOOM); - - return MAX_ZOOM; -} - // Establece el renderizador para las surfaces void Screen::setRendererSurface(std::shared_ptr surface) { (surface) ? renderer_surface_ = std::make_shared>(surface) : renderer_surface_ = std::make_shared>(game_surface_); @@ -457,10 +441,10 @@ void Screen::loadShaders() { VERTEX_FILE = "crtpi_vertex.glsl"; data = loadData(Asset::get()->get(VERTEX_FILE)); SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, - "Usando shaders OpenGL Desktop 3.3"); + "Usando shaders OpenGL Desktop 3.3"); } else { SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, - "Usando shaders OpenGL ES 3.0 (Raspberry Pi)"); + "Usando shaders OpenGL ES 3.0 (Raspberry Pi)"); } if (!data.empty()) { @@ -498,6 +482,51 @@ void Screen::initShaders() { // En macOS, OpenGL está deprecated y rinde mal // TODO: Implementar backend de Metal para shaders en macOS SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, - "Shaders no disponibles en macOS (OpenGL deprecated). Usa Metal backend."); + "Shaders no disponibles en macOS (OpenGL deprecated). Usa Metal backend."); #endif +} + +// Obtiene información sobre la pantalla +void Screen::getDisplayInfo() { + int i; + int num_displays = 0; + SDL_DisplayID *displays = SDL_GetDisplays(&num_displays); + if (displays != nullptr) { + for (i = 0; i < num_displays; ++i) { + SDL_DisplayID instance_id = displays[i]; + const char *name = SDL_GetDisplayName(instance_id); + + SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Display %" SDL_PRIu32 ": %s", instance_id, (name != nullptr) ? name : "Unknown"); + } + + const auto *dm = SDL_GetCurrentDisplayMode(displays[0]); + + // Guarda información del monitor en display_monitor_ + const char *first_display_name = SDL_GetDisplayName(displays[0]); + display_monitor_.name = (first_display_name != nullptr) ? first_display_name : "Unknown"; + display_monitor_.width = static_cast(dm->w); + display_monitor_.height = static_cast(dm->h); + display_monitor_.refresh_rate = static_cast(dm->refresh_rate); + + // 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.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(dm->w), static_cast(dm->h), static_cast(dm->refresh_rate)); + + SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Window resolution: %dx%d x%d", static_cast(options.game.width), static_cast(options.game.height), options.window.zoom); + + options.video.info = std::to_string(static_cast(dm->w)) + "x" + + std::to_string(static_cast(dm->h)) + " @ " + + std::to_string(static_cast(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); + + // Normaliza los valores de zoom + options.window.zoom = std::min(options.window.zoom, MAX_ZOOM); + + SDL_free(displays); + } } \ No newline at end of file diff --git a/source/screen.h b/source/screen.h index 1cedc91..21ed88b 100644 --- a/source/screen.h +++ b/source/screen.h @@ -22,9 +22,16 @@ enum class ScreenFilter : Uint32 { class Screen { private: // Constantes - static constexpr int WINDOWS_DECORATIONS_ = 35; + static constexpr int WINDOWS_DECORATIONS = 35; // Decoraciones de la ventana + + + struct DisplayMonitor { + std::string name; + int width; + int height; + int refresh_rate; + }; - // Estructuras struct FPS { Uint32 ticks; // Tiempo en milisegundos desde que se comenzó a contar. int frameCount; // Número acumulado de frames en el intervalo. @@ -79,6 +86,7 @@ class Screen { std::string info_resolution_; // Texto con la informacion de la pantalla std::string vertex_shader_source_; // Almacena el vertex shader std::string fragment_shader_source_; // Almacena el fragment shader + DisplayMonitor display_monitor_; // Informacion de la pantalla #ifdef DEBUG bool show_debug_info_ = false; // Indica si ha de mostrar/ocultar la información de la pantalla @@ -119,6 +127,9 @@ class Screen { // Muestra información por pantalla void renderInfo(); + // Obtiene información sobre la pantalla + void getDisplayInfo(); + // Constructor Screen(SDL_Window* window, SDL_Renderer* renderer); diff --git a/source/sections/credits.cpp b/source/sections/credits.cpp index 97a3202..e249a0d 100644 --- a/source/sections/credits.cpp +++ b/source/sections/credits.cpp @@ -236,7 +236,7 @@ void Credits::render() { // Dibuja la textura que cubre el texto const int offset = std::min(counter_ / 8, 192 / 2); - SDL_FRect srcRect = {0, 0, 256, 192 - (offset * 2)}; + SDL_FRect srcRect = {0.0F, 0.0F, 256.0F, 192.0F - (offset * 2.0F)}; cover_surface_->render(0, offset * 2, &srcRect); // Dibuja el sprite con el brillo diff --git a/source/sections/ending.cpp b/source/sections/ending.cpp index c995e25..809b23b 100644 --- a/source/sections/ending.cpp +++ b/source/sections/ending.cpp @@ -152,8 +152,8 @@ void Ending::iniTexts() { for (const auto& txt : texts) { auto text = Resource::get()->getText("smb2"); - const int WIDTH = text->lenght(txt.caption, 1) + 2 + 2; - const int HEIGHT = text->getCharacterSize() + 2 + 2; + const float WIDTH = text->lenght(txt.caption, 1) + 2 + 2; + const float HEIGHT = text->getCharacterSize() + 2 + 2; auto text_color = static_cast(PaletteColor::WHITE); auto shadow_color = static_cast(PaletteColor::BLACK); @@ -227,8 +227,8 @@ void Ending::iniPics() { // Crea la texture sp.image_surface = Resource::get()->getSurface(pic.caption); sp.image_surface->setTransparentColor(); - const int WIDTH = sp.image_surface->getWidth(); - const int HEIGHT = sp.image_surface->getHeight(); + const float WIDTH = sp.image_surface->getWidth(); + const float HEIGHT = sp.image_surface->getHeight(); // Crea el sprite sp.image_sprite = std::make_shared(sp.image_surface, 0, 0, WIDTH, HEIGHT); @@ -256,7 +256,7 @@ void Ending::iniPics() { } // El resto se rellena de color sólido - SDL_FRect rect = {0, 8, WIDTH, HEIGHT}; + SDL_FRect rect = {0.0F, 8.0F, WIDTH, HEIGHT}; surface->fillRect(&rect, color); // Crea el sprite @@ -464,8 +464,8 @@ void Ending::renderCoverTexture() { if (cover_counter_ > 0) { // Dibuja la textura que cubre el texto const int OFFSET = std::min(cover_counter_, 100); - SDL_FRect srcRect = {0, 200 - (cover_counter_ * 2), 256, OFFSET * 2}; - SDL_FRect dstRect = {0, 0, 256, OFFSET * 2}; + SDL_FRect srcRect = {0.0F, 200.0F - (cover_counter_ * 2.0F), 256.0F, OFFSET * 2.0F}; + SDL_FRect dstRect = {0.0F, 0.0F, 256.0F, OFFSET * 2.0F}; cover_surface_->render(&srcRect, &dstRect); } } diff --git a/source/sprite/surface_animated_sprite.cpp b/source/sprite/surface_animated_sprite.cpp index 6987f71..f4bcfd4 100644 --- a/source/sprite/surface_animated_sprite.cpp +++ b/source/sprite/surface_animated_sprite.cpp @@ -136,8 +136,8 @@ void SAnimatedSprite::resetAnimation() { // Carga la animación desde un vector de cadenas void SAnimatedSprite::setAnimations(const Animations& animations) { - int frame_width = 1; - int frame_height = 1; + float frame_width = 1.0F; + float frame_height = 1.0F; int frames_per_row = 1; int max_tiles = 1; @@ -190,7 +190,7 @@ void SAnimatedSprite::setAnimations(const Animations& animations) { // Se introducen los valores separados por comas en un vector std::stringstream ss(value); std::string tmp; - SDL_FRect rect = {0, 0, frame_width, frame_height}; + SDL_FRect rect = {0.0F, 0.0F, frame_width, frame_height}; while (getline(ss, tmp, ',')) { // Comprueba que el tile no sea mayor que el maximo indice permitido const int num_tile = std::stoi(tmp); diff --git a/source/surface.cpp b/source/surface.cpp index 781d94b..33219b0 100644 --- a/source/surface.cpp +++ b/source/surface.cpp @@ -439,7 +439,7 @@ void Surface::copyToTexture(SDL_Renderer* renderer, SDL_Texture* texture) { int pitch = 0; // Bloquea la textura para modificar los píxeles directamente - if (SDL_LockTexture(texture, nullptr, reinterpret_cast(&pixels), &pitch) != 0) { + if (!SDL_LockTexture(texture, nullptr, reinterpret_cast(&pixels), &pitch)) { throw std::runtime_error("Failed to lock texture: " + std::string(SDL_GetError())); } @@ -459,7 +459,7 @@ void Surface::copyToTexture(SDL_Renderer* renderer, SDL_Texture* texture) { SDL_UnlockTexture(texture); // Desbloquea la textura // Renderiza la textura en la pantalla completa - if (SDL_RenderTexture(renderer, texture, nullptr, nullptr) != 0) { + if (!SDL_RenderTexture(renderer, texture, nullptr, nullptr)) { throw std::runtime_error("Failed to copy texture to renderer: " + std::string(SDL_GetError())); } } @@ -486,7 +486,7 @@ void Surface::copyToTexture(SDL_Renderer* renderer, SDL_Texture* texture, SDL_FR } // Usa lockRect solo si destRect no es nulo - if (SDL_LockTexture(texture, destRect ? &lockRect : nullptr, reinterpret_cast(&pixels), &pitch) != 0) { + if (!SDL_LockTexture(texture, destRect ? &lockRect : nullptr, reinterpret_cast(&pixels), &pitch)) { throw std::runtime_error("Failed to lock texture: " + std::string(SDL_GetError())); } @@ -504,7 +504,7 @@ void Surface::copyToTexture(SDL_Renderer* renderer, SDL_Texture* texture, SDL_FR SDL_UnlockTexture(texture); // Renderiza la textura con los rectángulos especificados - if (SDL_RenderTexture(renderer, texture, srcRect, destRect) != 0) { + if (!SDL_RenderTexture(renderer, texture, srcRect, destRect)) { throw std::runtime_error("Failed to copy texture to renderer: " + std::string(SDL_GetError())); } } diff --git a/source/text.cpp b/source/text.cpp index 7a0de79..adaf1b3 100644 --- a/source/text.cpp +++ b/source/text.cpp @@ -88,7 +88,7 @@ Text::Text(std::shared_ptr surface, const std::string& text_file) { } // Crea los objetos - sprite_ = std::make_unique(surface, (SDL_FRect){0, 0, box_width_, box_height_}); + sprite_ = std::make_unique(surface, (SDL_FRect){0.0F, 0.0F, static_cast(box_width_), static_cast(box_height_)}); // Inicializa variables fixed_width_ = false; @@ -106,7 +106,7 @@ Text::Text(std::shared_ptr surface, std::shared_ptr text_file } // Crea los objetos - sprite_ = std::make_unique(surface, (SDL_FRect){0, 0, box_width_, box_height_}); + sprite_ = std::make_unique(surface, (SDL_FRect){0.0F, 0.0F, static_cast(box_width_), static_cast(box_height_)}); // Inicializa variables fixed_width_ = false; diff --git a/source/texture.cpp b/source/texture.cpp index 0bff2ca..fc9e838 100644 --- a/source/texture.cpp +++ b/source/texture.cpp @@ -51,10 +51,9 @@ bool Texture::loadFromFile(const std::string& file_path) { printWithDots("Image : ", getFileName(file_path), "[ LOADED ]"); } - int depth, pitch; + int pitch; SDL_PixelFormat pixel_format; // STBI_rgb_alpha (RGBA) - depth = 32; pitch = 4 * width; pixel_format = SDL_PIXELFORMAT_RGBA32; @@ -125,7 +124,7 @@ void Texture::setBlendMode(SDL_BlendMode blending) { SDL_SetTextureBlendMode(tex void Texture::setAlpha(Uint8 alpha) { SDL_SetTextureAlphaMod(texture_, alpha); } // Renderiza la textura en un punto específico -void Texture::render(int x, int y, SDL_FRect* clip, float zoomW, float zoomH, double angle, SDL_FPoint* center, SDL_FlipMode flip) { +void Texture::render(float x, float y, SDL_FRect* clip, float zoomW, float zoomH, double angle, SDL_FPoint* center, SDL_FlipMode flip) { // Establece el destino de renderizado en la pantalla SDL_FRect render_quad = {x, y, width_, height_}; diff --git a/source/texture.h b/source/texture.h index 4dd0e40..f5a9212 100644 --- a/source/texture.h +++ b/source/texture.h @@ -14,8 +14,8 @@ class Texture { // Variables std::string path_; // Ruta de la imagen de la textura - int width_ = 0; // Ancho de la imagen - int height_ = 0; // Alto de la imagen + float width_ = 0.0F; // Ancho de la imagen + float height_ = 0.0F; // Alto de la imagen std::vector> palettes_; // Vector con las diferentes paletas // Libera la memoria de la textura @@ -45,7 +45,7 @@ class Texture { void setAlpha(Uint8 alpha); // Renderiza la textura en un punto específico - void render(int x, int y, SDL_FRect* clip = nullptr, float zoomW = 1, float zoomH = 1, double angle = 0.0, SDL_FPoint* center = nullptr, SDL_FlipMode flip = SDL_FLIP_NONE); + void render(float x, float y, SDL_FRect* clip = nullptr, float zoomW = 1, float zoomH = 1, double angle = 0.0, SDL_FPoint* center = nullptr, SDL_FlipMode flip = SDL_FLIP_NONE); // Establece la textura como objetivo de renderizado void setAsRenderTarget(SDL_Renderer* renderer); diff --git a/source/ui/notifier.cpp b/source/ui/notifier.cpp index e8cb1d7..ce1e2a1 100644 --- a/source/ui/notifier.cpp +++ b/source/ui/notifier.cpp @@ -140,12 +140,12 @@ void Notifier::show(std::vector texts, NotificationText text_is, Ui const auto PADDING_IN_V = text_size / 2; const int ICON_SPACE = icon >= 0 ? ICON_SIZE_ + PADDING_IN_H : 0; text_is = ICON_SPACE > 0 ? NotificationText::LEFT : text_is; - const int WIDTH = options.game.width - (PADDING_OUT_ * 2); - const int HEIGHT = (text_size * texts.size()) + (PADDING_IN_V * 2); + const float WIDTH = options.game.width - (PADDING_OUT_ * 2); + const float HEIGHT = (text_size * texts.size()) + (PADDING_IN_V * 2); const auto SHAPE = NotificationShape::SQUARED; // Posición horizontal - int desp_h = 0; + float desp_h = 0; switch (options.notifications.getHorizontalPosition()) { case NotificationPosition::LEFT: desp_h = PADDING_OUT_; @@ -183,7 +183,7 @@ void Notifier::show(std::vector texts, NotificationText text_is, Ui n.texts = texts; n.shape = SHAPE; n.display_duration = display_duration; - const int Y_POS = OFFSET + ((options.notifications.getVerticalPosition() == NotificationPosition::TOP) ? -TRAVEL_DIST : TRAVEL_DIST); + const float Y_POS = OFFSET + ((options.notifications.getVerticalPosition() == NotificationPosition::TOP) ? -TRAVEL_DIST : TRAVEL_DIST); n.rect = {desp_h, Y_POS, WIDTH, HEIGHT}; // Crea la textura @@ -219,7 +219,7 @@ void Notifier::show(std::vector texts, NotificationText text_is, Ui if (has_icons_ && icon >= 0 && texts.size() >= 2) { auto sp = std::make_unique(icon_surface_, (SDL_FRect){0, 0, ICON_SIZE_, ICON_SIZE_}); sp->setPosition({PADDING_IN_H, PADDING_IN_V, ICON_SIZE_, ICON_SIZE_}); - sp->setClip({ICON_SIZE_ * (icon % 10), ICON_SIZE_ * (icon / 10), ICON_SIZE_, ICON_SIZE_}); + sp->setClip((SDL_FRect){ICON_SIZE_ * (icon % 10), ICON_SIZE_ * (icon / 10), ICON_SIZE_, ICON_SIZE_}); sp->render(); } diff --git a/source/ui/notifier.h b/source/ui/notifier.h index d601cf4..33a589b 100644 --- a/source/ui/notifier.h +++ b/source/ui/notifier.h @@ -22,8 +22,8 @@ enum class NotificationText { class Notifier { private: // Constantes - static constexpr int ICON_SIZE_ = 16; - static constexpr int PADDING_OUT_ = 0; + static constexpr float ICON_SIZE_ = 16.0F; + static constexpr float PADDING_OUT_ = 0.0F; // [SINGLETON] Objeto notifier static Notifier* notifier_; diff --git a/source/utils.cpp b/source/utils.cpp index 78507b9..22049bc 100644 --- a/source/utils.cpp +++ b/source/utils.cpp @@ -232,7 +232,7 @@ SDL_FPoint checkCollision(const Line& l1, const Line& l2) { const float x = x1 + (uA * (x2 - x1)); const float y = y1 + (uA * (y2 - y1)); - return {(int)round(x), (int)round(y)}; + return {(float)round(x), (float)round(y)}; } return {-1, -1}; } @@ -259,7 +259,7 @@ SDL_FPoint checkCollision(const LineDiagonal& l1, const LineVertical& l2) { const float x = x1 + (uA * (x2 - x1)); const float y = y1 + (uA * (y2 - y1)); - return {(int)x, (int)y}; + return {(float)x, (float)y}; } return {-1, -1}; }