diff --git a/source/director.cpp b/source/director.cpp index 7d48218..5dc4a70 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -244,6 +244,7 @@ void Director::initInput() Input::get()->bindKey(InputAction::NEXT_PALETTE, SDL_SCANCODE_F5); Input::get()->bindKey(InputAction::PREVIOUS_PALETTE, SDL_SCANCODE_F6); Input::get()->bindKey(InputAction::TOGGLE_INTEGER_SCALE, SDL_SCANCODE_F7); + Input::get()->bindKey(InputAction::SHOW_DEBUG_INFO, SDL_SCANCODE_F12); Input::get()->bindKey(InputAction::TOGGLE_MUSIC, SDL_SCANCODE_M); Input::get()->bindKey(InputAction::TOGGLE_BORDER, SDL_SCANCODE_B); diff --git a/source/global_inputs.cpp b/source/global_inputs.cpp index bd33e15..c5b0def 100644 --- a/source/global_inputs.cpp +++ b/source/global_inputs.cpp @@ -114,5 +114,10 @@ namespace globalInputs Screen::get()->setVideoMode(options.video.mode); Notifier::get()->show({"INTEGER SCALE " + std::string(options.video.integer_scale ? "ENABLED" : "DISABLED")}, NotificationText::CENTER); } + + else if (Input::get()->checkInput(InputAction::SHOW_DEBUG_INFO, INPUT_DO_NOT_ALLOW_REPEAT)) + { + Screen::get()->toggleDebugInfo(); + } } } \ No newline at end of file diff --git a/source/input.h b/source/input.h index deffc53..1c5dec0 100644 --- a/source/input.h +++ b/source/input.h @@ -41,6 +41,7 @@ enum class InputAction NEXT_PALETTE, PREVIOUS_PALETTE, TOGGLE_SHADERS, + SHOW_DEBUG_INFO, // Input obligatorio NONE, diff --git a/source/screen.cpp b/source/screen.cpp index 2a7f837..b593ff5 100644 --- a/source/screen.cpp +++ b/source/screen.cpp @@ -3,6 +3,7 @@ #include // Para SDL_DISABLE, SDL_ENABLE #include // Para SDL_ShowCursor #include // Para SDL_PIXELFORMAT_ARGB8888, SDL_PIXELFORM... +#include // Para SDL_GetTicks #include // Para toupper #include // Para max, min, transform #include // Para basic_ostream, operator<<, endl, basic_... @@ -16,6 +17,7 @@ #include "options.h" // Para Options, options, OptionsVideo, Border #include "resource.h" // Para Resource #include "surface.h" // Para Surface, readPalFile +#include "text.h" // Para Text // [SINGLETON] Screen *Screen::screen_ = nullptr; @@ -44,6 +46,11 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer) renderer_(renderer), palettes_(Asset::get()->getListByType(AssetType::PALETTE)) { + // Inicializa variables + SDL_DisplayMode DM; + SDL_GetCurrentDisplayMode(0, &DM); + info_resolution_ = std::to_string(DM.w) + " X " + std::to_string(DM.h) + " AT " + std::to_string(DM.refresh_rate) + " HZ"; + // Ajusta los tamaños adjustGameCanvasRect(); adjustWindowSize(); @@ -105,7 +112,7 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer) // Establece el modo de video setVideoMode(options.video.mode); - + // Muestra la ventana show(); resetShaders(); @@ -142,6 +149,8 @@ void Screen::start() // Vuelca el contenido del renderizador en pantalla void Screen::render() { + fps_.increment(); + // Renderiza todos los overlays renderOverlays(); @@ -262,6 +271,7 @@ void Screen::toggleShaders() // Actualiza la lógica de la clase void Screen::update() { + fps_.calculate(SDL_GetTicks()); Notifier::get()->update(); Mouse::updateCursorVisibility(); } @@ -442,6 +452,7 @@ void Screen::textureToRenderer() void Screen::renderOverlays() { renderNotifications(); + renderInfo(); } // Localiza la paleta dentro del vector de paletas @@ -479,4 +490,21 @@ void Screen::createShadersTexture() std::cerr << "Error: shaders_texture_ could not be created!\nSDL Error: " << SDL_GetError() << std::endl; } } +} + +// Muestra información por pantalla +void Screen::renderInfo() +{ + if (show_debug_info_ && Resource::get()) + { + auto text = Resource::get()->getText("smb2"); + auto color = static_cast(PaletteColor::YELLOW); + + // FPS + const std::string FPS_TEXT = std::to_string(fps_.lastValue) + " FPS"; + text->writeColored(options.game.width - text->lenght(FPS_TEXT), 0, FPS_TEXT, color); + + // Resolution + text->writeColored(0, 0, info_resolution_, color); + } } \ No newline at end of file diff --git a/source/screen.h b/source/screen.h index d066d75..3d806aa 100644 --- a/source/screen.h +++ b/source/screen.h @@ -22,6 +22,35 @@ enum class ScreenFilter : Uint32 class Screen { private: + // Estructuras + struct FPS + { + Uint32 ticks; // Tiempo en milisegundos desde que se comenzó a contar. + int frameCount; // Número acumulado de frames en el intervalo. + int lastValue; // Número de frames calculado en el último segundo. + + // Constructor para inicializar la estructura. + FPS() : ticks(0), frameCount(0), lastValue(0) {} + + // Incrementador que se llama en cada frame. + void increment() + { + frameCount++; + } + + // Método para calcular y devolver el valor de FPS. + int calculate(Uint32 currentTicks) + { + if (currentTicks - ticks >= 1000) // Si ha pasado un segundo o más. + { + lastValue = frameCount; // Actualizamos el valor del último FPS. + frameCount = 0; // Reiniciamos el contador de frames. + ticks = currentTicks; // Actualizamos el tiempo base. + } + return lastValue; + } + }; + // Constantes static constexpr int WINDOWS_DECORATIONS_ = 35; @@ -46,6 +75,14 @@ private: std::vector palettes_; // Listado de los ficheros de paletta disponibles Uint8 current_palette_ = 0; // Indice para el vector de paletas bool notifications_enabled_ = false; // indica si se muestran las notificaciones + FPS fps_; // Variable para gestionar los frames por segundo + std::string info_resolution_; // Texto con la informacion de la pantalla + +#ifdef DEBUG + bool show_debug_info_ = false; // Indica si ha de mostrar/ocultar la información de la pantalla +#else + bool show_debug_info_ = false; // Indica si ha de mostrar/ocultar la información de la pantalla +#endif // Dibuja las notificaciones void renderNotifications(); @@ -80,6 +117,9 @@ private: // Recrea la textura para los shaders void createShadersTexture(); + // Muestra información por pantalla + void renderInfo(); + // Constructor Screen(SDL_Window *window, SDL_Renderer *renderer); @@ -166,4 +206,7 @@ public: // Establece la visibilidad de las notificaciones void setNotificationsEnabled(bool value) { notifications_enabled_ = value; } + + // Activa / desactiva la información de debug + void toggleDebugInfo() { show_debug_info_ = !show_debug_info_; } }; \ No newline at end of file