Debian Codegraph @

src/engine/render_processor.h

raw symbols SCIP
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 #pragma once
22
23 #include <cstdint>
24 #include <functional>
25 #include <vector>
26
27 #include "timing.h"
28
29 namespace fheroes2
30 {
31 class RenderProcessor
32 {
33 public:
34 RenderProcessor( const RenderProcessor & ) = delete;
35
36 ~RenderProcessor() = default;
37
38 RenderProcessor & operator=( const RenderProcessor & ) = delete;
39
40 static RenderProcessor & instance();
41
42 void registerRenderers( const std::function<void()> & preRenderer, const std::function<void()> & postRenderer )
43 {
44 _preRenderer = preRenderer;
45 _postRenderer = postRenderer;
46 }
47
48 void unregisterRenderers()
49 {
50 _preRenderer = {};
51 _postRenderer = {};
52 }
53
54 void enableRenderers()
55 {
56 _enableRenderers = true;
57 }
58
59 void disableRenderers()
60 {
61 _enableRenderers = false;
62 }
63
64 bool preRenderAction( std::vector<uint8_t> & palette );
65
66 void postRenderAction() const;
67
68 void startColorCycling()
69 {
70 if ( _enableCycling ) {
71 return;
72 }
73
74 _enableCycling = true;
75
76 // Since we are restarting the cycling, we have to reset the timer.
77 _cyclingTimer.reset();
78 }
79
80 void stopColorCycling()
81 {
82 _enableCycling = false;
83 }
84
85 bool isCyclingUpdateRequired() const
86 {
87 return _enableCycling && _cyclingTimer.getMs() + _previousCyclingInterval >= 2 * _cyclingInterval && _lastRenderCall.getMs() > _frameHalfInterval;
88 }
89
90 private:
91 RenderProcessor() = default;
92
93 std::function<void()> _preRenderer;
94 std::function<void()> _postRenderer;
95
96 fheroes2::Time _cyclingTimer;
97 fheroes2::Time _lastRenderCall;
98
99 // This variable represents the inaccuracy of the previous color cycling rendering to be taken into account in the next one.
100 uint64_t _previousCyclingInterval{ _cyclingInterval };
101
102 uint32_t _cyclingCounter{ 0 };
103
104 bool _enableRenderers{ false };
105 bool _enableCycling{ false };
106
107 static const uint64_t _cyclingInterval{ 220 };
108
109 // Lets assume that the desynchronization of one frame is not noticeable on 60 FPS.
110 // To reduce the render calls we may do color cycling update 8 ms earlier or later.
111 static constexpr uint64_t _frameHalfInterval{ 8 };
112 };
113 }