/*************************************************************************** * fheroes2: https://github.com/ihhub/fheroes2 * * Copyright (C) 2023 - 2025 * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #pragma once #include #include #include #include "timing.h" namespace fheroes2 { class RenderProcessor { public: RenderProcessor( const RenderProcessor & ) = delete; ~RenderProcessor() = default; RenderProcessor & operator=( const RenderProcessor & ) = delete; static RenderProcessor & instance(); void registerRenderers( const std::function & preRenderer, const std::function & postRenderer ) { _preRenderer = preRenderer; _postRenderer = postRenderer; } void unregisterRenderers() { _preRenderer = {}; _postRenderer = {}; } void enableRenderers() { _enableRenderers = true; } void disableRenderers() { _enableRenderers = false; } bool preRenderAction( std::vector & palette ); void postRenderAction() const; void startColorCycling() { if ( _enableCycling ) { return; } _enableCycling = true; // Since we are restarting the cycling, we have to reset the timer. _cyclingTimer.reset(); } void stopColorCycling() { _enableCycling = false; } bool isCyclingUpdateRequired() const { return _enableCycling && _cyclingTimer.getMs() + _previousCyclingInterval >= 2 * _cyclingInterval && _lastRenderCall.getMs() > _frameHalfInterval; } private: RenderProcessor() = default; std::function _preRenderer; std::function _postRenderer; fheroes2::Time _cyclingTimer; fheroes2::Time _lastRenderCall; // This variable represents the inaccuracy of the previous color cycling rendering to be taken into account in the next one. uint64_t _previousCyclingInterval{ _cyclingInterval }; uint32_t _cyclingCounter{ 0 }; bool _enableRenderers{ false }; bool _enableCycling{ false }; static const uint64_t _cyclingInterval{ 220 }; // Lets assume that the desynchronization of one frame is not noticeable on 60 FPS. // To reduce the render calls we may do color cycling update 8 ms earlier or later. static constexpr uint64_t _frameHalfInterval{ 8 }; }; }