| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 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 |
|
| 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 |
|
| 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 & ) |
| 108 |
{ |
| 109 |
|
| 110 |
} |
| 111 |
|
| 112 |
virtual void setIcon( const Image & ) |
| 113 |
{ |
| 114 |
|
| 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 ) |
| 128 |
{ |
| 129 |
|
| 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 ) |
| 148 |
{ |
| 149 |
|
| 150 |
} |
| 151 |
|
| 152 |
protected: |
| 153 |
BaseRenderEngine() = default; |
| 154 |
|
| 155 |
virtual void clear() |
| 156 |
{ |
| 157 |
|
| 158 |
} |
| 159 |
|
| 160 |
virtual void render( const Display & , const Rect & ) |
| 161 |
{ |
| 162 |
|
| 163 |
} |
| 164 |
|
| 165 |
virtual bool allocate( ResolutionInfo & , bool ) |
| 166 |
{ |
| 167 |
return false; |
| 168 |
} |
| 169 |
|
| 170 |
virtual bool isMouseCursorActive() const |
| 171 |
{ |
| 172 |
return false; |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
virtual void updatePalette( const std::vector<uint8_t> & ) |
| 177 |
{ |
| 178 |
|
| 179 |
} |
| 180 |
|
| 181 |
void linkRenderSurface( uint8_t * surface ) const; |
| 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 |
|
| 205 |
void render() |
| 206 |
{ |
| 207 |
render( { 0, 0, width(), height() } ); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
void render( const Rect & roi ); |
| 212 |
|
| 213 |
|
| 214 |
void updateNextRenderRoi( const Rect & roi ); |
| 215 |
|
| 216 |
|
| 217 |
void resize( int32_t width_, int32_t height_ ) override; |
| 218 |
|
| 219 |
void setResolution( ResolutionInfo info ); |
| 220 |
|
| 221 |
|
| 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 |
|
| 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 |
|
| 247 |
uint8_t * image() override; |
| 248 |
const uint8_t * image() const override; |
| 249 |
|
| 250 |
void release(); |
| 251 |
|
| 252 |
|
| 253 |
|
| 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 |
|
| 273 |
Rect _prevRoi; |
| 274 |
|
| 275 |
Size _screenSize; |
| 276 |
|
| 277 |
|
| 278 |
void linkRenderSurface( uint8_t * surface ) |
| 279 |
{ |
| 280 |
_renderSurface = surface; |
| 281 |
} |
| 282 |
|
| 283 |
Display(); |
| 284 |
|
| 285 |
void _renderFrame( const Rect & roi ) const; |
| 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 |
|
| 317 |
virtual void enableSoftwareEmulation( const bool ) |
| 318 |
{ |
| 319 |
|
| 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 |
} |