| 1 | /*************************************************************************** |
| 2 | * fheroes2: https://github.com/ihhub/fheroes2 * |
| 3 | * Copyright (C) 2023 - 2025 * |
| 4 | * * |
| 5 | * This program is free software; you can redistribute it and/or modify * |
| 6 | * it under the terms of the GNU General Public License as published by * |
| 7 | * the Free Software Foundation; either version 2 of the License, or * |
| 8 | * (at your option) any later version. * |
| 9 | * * |
| 10 | * This program is distributed in the hope that it will be useful, * |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 13 | * GNU General Public License for more details. * |
| 14 | * * |
| 15 | * You should have received a copy of the GNU General Public License * |
| 16 | * along with this program; if not, write to the * |
| 17 | * Free Software Foundation, Inc., * |
| 18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 19 | ***************************************************************************/ |
| 20 | |
| 21 | #include "render_processor.h" |
| 22 | |
| 23 | #include <cstdint> |
| 24 | |
| 25 | #include "pal.h" |
| 26 | |
| 27 | namespace fheroes2 |
| 28 | { |
| 29 | RenderProcessor & RenderProcessor::instance() |
| 30 | { |
| 31 | static RenderProcessor processor; |
| 32 | return processor; |
| 33 | } |
| 34 | |
| 35 | bool RenderProcessor::preRenderAction( std::vector<uint8_t> & palette ) |
| 36 | { |
| 37 | // We consider the start of rendering to be the moment when we reset the timer for the next frame. |
| 38 | // This is because we have no control over how long the entire rendering process will take, |
| 39 | // but the start of rendering is always a consistent point in time. |
| 40 | _lastRenderCall.reset(); |
| 41 | |
| 42 | if ( !_enableCycling ) { |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | if ( _enableRenderers && _preRenderer ) { |
| 47 | _preRenderer(); |
| 48 | } |
| 49 | |
| 50 | const uint64_t cyclingTime = _previousCyclingInterval + _cyclingTimer.getMs(); |
| 51 | if ( cyclingTime < ( _cyclingInterval * 2 - _frameHalfInterval ) ) { |
| 52 | // If the current timer is less than cycling internal minus half of the frame generation then nothing is needed. |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | _previousCyclingInterval = cyclingTime - _cyclingInterval; |
| 57 | |
| 58 | if ( _previousCyclingInterval > _cyclingInterval + _frameHalfInterval * 2 ) { |
| 59 | // The cycling delay was more than assumed frame synchronization interval. |
| 60 | // It can be because of some small engine hang after doing some massive computations. |
| 61 | // So let's reset the synchronization then. |
| 62 | _previousCyclingInterval = _cyclingInterval; |
| 63 | } |
| 64 | |
| 65 | // TODO: here we need to deduct possible time difference from the current time to have consistent FPS. |
| 66 | _cyclingTimer.reset(); |
| 67 | palette = PAL::GetCyclingPalette( _cyclingCounter ); |
| 68 | ++_cyclingCounter; |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | void RenderProcessor::postRenderAction() const |
| 73 | { |
| 74 | if ( _enableCycling && _enableRenderers && _postRenderer ) { |
| 75 | _postRenderer(); |
| 76 | } |
| 77 | } |
| 78 | } |