Debian Codegraph @

src/engine/screen.h

raw symbols SCIP
1 /***************************************************************************
2 * fheroes2: https://github.com/ihhub/fheroes2 *
3 * Copyright (C) 2020 - 2026 *
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 <memory>
26 #include <string>
27 #include <tuple>
28 #include <vector>
29
30 #include "image.h"
31 #include "math_base.h"
32
33 namespace fheroes2
34 {
35 class Cursor;
36 class Display;
37
38 struct ResolutionInfo
39 {
40 ResolutionInfo() = default;
41
42 ResolutionInfo( const int32_t gameWidth_, const int32_t gameHeight_ )
43 : gameWidth( gameWidth_ )
44 , gameHeight( gameHeight_ )
45 , screenWidth( gameWidth_ )
46 , screenHeight( gameHeight_ )
47 {
48 // Do nothing.
49 }
50
51 ResolutionInfo( const int32_t gameWidth_, const int32_t gameHeight_, const int32_t screenWidth_, const int32_t screenHeight_ )
52 : gameWidth( gameWidth_ )
53 , gameHeight( gameHeight_ )
54 , screenWidth( screenWidth_ )
55 , screenHeight( screenHeight_ )
56 {
57 // Do nothing.
58 }
59
60 bool operator==( const ResolutionInfo & info ) const
61 {
62 return gameWidth == info.gameWidth && gameHeight == info.gameHeight && screenWidth == info.screenWidth && screenHeight == info.screenHeight;
63 }
64
65 bool operator!=( const ResolutionInfo & info ) const
66 {
67 return !operator==( info );
68 }
69
70 bool operator<( const ResolutionInfo & info ) const
71 {
72 return std::tie( gameWidth, gameHeight, screenWidth, screenHeight ) < std::tie( info.gameWidth, info.gameHeight, info.screenWidth, info.screenHeight );
73 }
74
75 int32_t gameWidth{ 0 };
76
77 int32_t gameHeight{ 0 };
78
79 int32_t screenWidth{ 0 };
80
81 int32_t screenHeight{ 0 };
82 };
83
84 class BaseRenderEngine
85 {
86 public:
87 friend class Cursor;
88 friend class Display;
89
90 virtual ~BaseRenderEngine() = default;
91
92 virtual void toggleFullScreen()
93 {
94 _isFullScreen = !_isFullScreen;
95 }
96
97 virtual bool isFullScreen() const
98 {
99 return _isFullScreen;
100 }
101
102 virtual std::vector<ResolutionInfo> getAvailableResolutions() const
103 {
104 return {};
105 }
106
107 virtual void setTitle( const std::string & /*unused*/ )
108 {
109 // Do nothing.
110 }
111
112 virtual void setIcon( const Image & /*unused*/ )
113 {
114 // Do nothing.
115 }
116
117 virtual Rect getActiveWindowROI() const
118 {
119 return {};
120 }
121
122 virtual Size getCurrentScreenResolution() const
123 {
124 return {};
125 }
126
127 virtual void setVSync( const bool /*unused*/ )
128 {
129 // Do nothing.
130 }
131
132 void setNearestScaling( const bool enable )
133 {
134 _nearestScaling = enable;
135 }
136
137 bool isNearestScaling() const
138 {
139 return _nearestScaling;
140 }
141
142 virtual Point getWindowPos() const
143 {
144 return { -1, -1 };
145 }
146
147 virtual void setWindowPos( const Point /* pos */ )
148 {
149 // Do nothing
150 }
151
152 protected:
153 BaseRenderEngine() = default;
154
155 virtual void clear()
156 {
157 // Do nothing.
158 }
159
160 virtual void render( const Display & /*unused*/, const Rect & /*unused*/ )
161 {
162 // Do nothing.
163 }
164
165 virtual bool allocate( ResolutionInfo & /*unused*/, bool /*unused*/ )
166 {
167 return false;
168 }
169
170 virtual bool isMouseCursorActive() const
171 {
172 return false;
173 }
174
175 // To support color cycling we need to update palette.
176 virtual void updatePalette( const std::vector<uint8_t> & )
177 {
178 // Do nothing.
179 }
180
181 void linkRenderSurface( uint8_t * surface ) const; // declaration of this method is in source file
182
183 private:
184 bool _isFullScreen{ false };
185
186 bool _nearestScaling{ false };
187 };
188
189 class Display final : public Image
190 {
191 public:
192 friend class BaseRenderEngine;
193
194 enum : int32_t
195 {
196 DEFAULT_WIDTH = 640,
197 DEFAULT_HEIGHT = 480
198 };
199
200 static Display & instance();
201
202 ~Display() override = default;
203
204 // Render an entire frame on screen.
205 void render()
206 {
207 render( { 0, 0, width(), height() } );
208 }
209
210 // Render a part of frame on screen.
211 void render( const Rect & roi );
212
213 // Update the area which will be rendered on the next render() call.
214 void updateNextRenderRoi( const Rect & roi );
215
216 // Do not call this method. It serves as a patch over the basic class.
217 void resize( int32_t width_, int32_t height_ ) override;
218
219 void setResolution( ResolutionInfo info );
220
221 // Call this method only if you need to reset renderer to update its parameters (e.g. screen scaling).
222 void resetRenderer();
223
224 bool isDefaultSize() const
225 {
226 return width() == DEFAULT_WIDTH && height() == DEFAULT_HEIGHT;
227 }
228
229 Point getWindowPos() const
230 {
231 return _engine->getWindowPos();
232 }
233
234 void setWindowPos( const Point point );
235
236 // this function must return true if new palette has been generated
237 using PreRenderProcessing = std::function<bool( std::vector<uint8_t> & )>;
238 using PostRenderProcessing = std::function<void()>;
239
240 void subscribe( const PreRenderProcessing & preprocessing, const PostRenderProcessing & postprocessing )
241 {
242 _preprocessing = preprocessing;
243 _postprocessing = postprocessing;
244 }
245
246 // For 8-bit mode we return a pointer to direct surface which we draw on screen
247 uint8_t * image() override;
248 const uint8_t * image() const override;
249
250 void release(); // to release all allocated resources. Should be used at the end of the application
251
252 // Change the whole color representation on the screen. Make sure that palette exists all the time!!!
253 // nullptr input parameter is used to reset palette to default one.
254 void changePalette( const uint8_t * palette = nullptr, const bool forceDefaultPaletteUpdate = false ) const;
255
256 Size screenSize() const
257 {
258 return _screenSize;
259 }
260
261 friend BaseRenderEngine & engine();
262 friend Cursor & cursor();
263
264 private:
265 std::unique_ptr<BaseRenderEngine> _engine;
266 std::unique_ptr<Cursor> _cursor;
267 PreRenderProcessing _preprocessing{ nullptr };
268 PostRenderProcessing _postprocessing{ nullptr };
269
270 uint8_t * _renderSurface{ nullptr };
271
272 // Previous area drawn on the screen.
273 Rect _prevRoi;
274
275 Size _screenSize;
276
277 // Only for cases of direct drawing on rendered 8-bit image.
278 void linkRenderSurface( uint8_t * surface )
279 {
280 _renderSurface = surface;
281 }
282
283 Display();
284
285 void _renderFrame( const Rect & roi ) const; // prepare and render a frame
286 };
287
288 class Cursor
289 {
290 public:
291 friend Display;
292 virtual ~Cursor() = default;
293
294 virtual void show( const bool enable )
295 {
296 _show = enable;
297 }
298
299 virtual bool isVisible() const
300 {
301 return _show;
302 }
303
304 static bool isFocusActive();
305
306 virtual void update( const Image & image, int32_t offsetX, int32_t offsetY )
307 {
308 _image = Sprite( image, offsetX, offsetY );
309 }
310
311 void setPosition( const int32_t x, const int32_t y )
312 {
313 _image.setPosition( x, y );
314 }
315
316 // Default implementation of Cursor uses software emulation.
317 virtual void enableSoftwareEmulation( const bool /*unused*/ )
318 {
319 // Do nothing.
320 }
321
322 bool isSoftwareEmulation() const
323 {
324 return _emulation;
325 }
326
327 void registerUpdater( void ( *cursorUpdater )() )
328 {
329 _cursorUpdater = cursorUpdater;
330 }
331
332 void keepInScreenArea( const bool value )
333 {
334 _keepInScreenArea = value;
335 }
336
337 protected:
338 Sprite _image;
339 void ( *_cursorUpdater )(){ nullptr };
340 bool _emulation{ false };
341 bool _show{ false };
342 bool _keepInScreenArea{ false };
343
344 Cursor() = default;
345 };
346
347 BaseRenderEngine & engine();
348 Cursor & cursor();
349 }