| 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 | #include <algorithm> |
| 22 | #include <cassert> |
| 23 | #include <cmath> |
| 24 | #include <cstdint> |
| 25 | #include <cstring> |
| 26 | #include <iterator> |
| 27 | #include <optional> |
| 28 | #include <ostream> |
| 29 | #include <set> |
| 30 | #include <utility> |
| 31 | |
| 32 | // Managing compiler warnings for SDL headers |
| 33 | #if defined( __GNUC__ ) |
| 34 | #pragma GCC diagnostic push |
| 35 | |
| 36 | #pragma GCC diagnostic ignored "-Wdouble-promotion" |
| 37 | #pragma GCC diagnostic ignored "-Wold-style-cast" |
| 38 | #pragma GCC diagnostic ignored "-Wswitch-default" |
| 39 | #endif |
| 40 | |
| 41 | #include <SDL_error.h> |
| 42 | #include <SDL_events.h> |
| 43 | #include <SDL_hints.h> |
| 44 | #include <SDL_mouse.h> |
| 45 | #include <SDL_pixels.h> |
| 46 | #include <SDL_rect.h> |
| 47 | #include <SDL_render.h> |
| 48 | #include <SDL_stdinc.h> |
| 49 | #include <SDL_surface.h> |
| 50 | #include <SDL_version.h> |
| 51 | #include <SDL_video.h> |
| 52 | |
| 53 | // Managing compiler warnings for SDL headers |
| 54 | #if defined( __GNUC__ ) |
| 55 | #pragma GCC diagnostic pop |
| 56 | #endif |
| 57 | |
| 58 | #if defined( TARGET_PS_VITA ) |
| 59 | #include <vita2d.h> |
| 60 | #endif |
| 61 | |
| 62 | #include "image_palette.h" |
| 63 | #include "logging.h" |
| 64 | #include "math_tools.h" |
| 65 | #include "screen.h" |
| 66 | #include "system.h" |
| 67 | |
| 68 | namespace |
| 69 | { |
| 70 | // Returns nearest screen supported resolution |
| 71 | fheroes2::ResolutionInfo GetNearestResolution( fheroes2::ResolutionInfo resolutionInfo, const std::vector<fheroes2::ResolutionInfo> & resolutions ) |
| 72 | { |
| 73 | if ( resolutions.empty() ) { |
| 74 | return resolutionInfo; |
| 75 | } |
| 76 | |
| 77 | if ( resolutionInfo.gameWidth < 1 ) { |
| 78 | resolutionInfo.gameWidth = 1; |
| 79 | } |
| 80 | |
| 81 | if ( resolutionInfo.gameHeight < 1 ) { |
| 82 | resolutionInfo.gameHeight = 1; |
| 83 | } |
| 84 | |
| 85 | if ( resolutionInfo.screenWidth < resolutionInfo.gameWidth ) { |
| 86 | resolutionInfo.screenWidth = resolutionInfo.gameWidth; |
| 87 | } |
| 88 | |
| 89 | if ( resolutionInfo.screenHeight < resolutionInfo.gameHeight ) { |
| 90 | resolutionInfo.screenHeight = resolutionInfo.gameHeight; |
| 91 | } |
| 92 | |
| 93 | const double gameX = resolutionInfo.gameWidth; |
| 94 | const double gameY = resolutionInfo.gameHeight; |
| 95 | const double screenX = resolutionInfo.screenWidth; |
| 96 | const double screenY = resolutionInfo.screenHeight; |
| 97 | |
| 98 | std::vector<double> similarity( resolutions.size(), 0 ); |
| 99 | for ( size_t i = 0; i < resolutions.size(); ++i ) { |
| 100 | similarity[i] = std::fabs( resolutions[i].gameWidth - gameX ) / gameX + std::fabs( resolutions[i].gameHeight - gameY ) / gameY |
| 101 | + std::fabs( resolutions[i].screenWidth - screenX ) / screenX + std::fabs( resolutions[i].screenHeight - screenY ) / screenY; |
| 102 | } |
| 103 | |
| 104 | const std::vector<double>::difference_type id = std::distance( similarity.begin(), std::min_element( similarity.begin(), similarity.end() ) ); |
| 105 | |
| 106 | return resolutions[id]; |
| 107 | } |
| 108 | |
| 109 | #if !defined( TARGET_PS_VITA ) |
| 110 | bool IsLowerThanDefaultRes( const fheroes2::ResolutionInfo & value ) |
| 111 | { |
| 112 | return value.gameWidth < fheroes2::Display::DEFAULT_WIDTH || value.gameHeight < fheroes2::Display::DEFAULT_HEIGHT; |
| 113 | } |
| 114 | |
| 115 | std::set<fheroes2::ResolutionInfo> FilterResolutions( const std::set<fheroes2::ResolutionInfo> & resolutionSet ) |
| 116 | { |
| 117 | static_assert( fheroes2::Display::DEFAULT_WIDTH == 640 && fheroes2::Display::DEFAULT_HEIGHT == 480, "Default resolution must be 640 x 480" ); |
| 118 | |
| 119 | if ( resolutionSet.empty() ) { |
| 120 | return { { fheroes2::Display::DEFAULT_WIDTH, fheroes2::Display::DEFAULT_HEIGHT } }; |
| 121 | } |
| 122 | |
| 123 | std::set<fheroes2::ResolutionInfo> resolutions; |
| 124 | |
| 125 | for ( const fheroes2::ResolutionInfo & resolution : resolutionSet ) { |
| 126 | if ( IsLowerThanDefaultRes( resolution ) ) { |
| 127 | continue; |
| 128 | } |
| 129 | |
| 130 | resolutions.emplace( resolution ); |
| 131 | } |
| 132 | |
| 133 | if ( resolutions.empty() ) { |
| 134 | return { { fheroes2::Display::DEFAULT_WIDTH, fheroes2::Display::DEFAULT_HEIGHT } }; |
| 135 | } |
| 136 | |
| 137 | { |
| 138 | // TODO: add resolutions which are close to the current screen aspect ratio. |
| 139 | // Some operating systems do not work well with SDL so they return very limited number of high resolutions. |
| 140 | // Populate missing resolutions into the list. |
| 141 | const std::set<fheroes2::ResolutionInfo> possibleResolutions |
| 142 | = { { 640, 480 }, { 800, 600 }, { 1024, 768 }, { 1152, 864 }, { 1280, 600 }, { 1280, 720 }, { 1280, 768 }, { 1280, 960 }, |
| 143 | { 1280, 1024 }, { 1360, 768 }, { 1400, 1050 }, { 1440, 900 }, { 1600, 900 }, { 1680, 1050 }, { 1920, 1080 } }; |
| 144 | |
| 145 | assert( !resolutions.empty() ); |
| 146 | const fheroes2::ResolutionInfo lowestResolution = *( resolutions.begin() ); |
| 147 | |
| 148 | for ( const fheroes2::ResolutionInfo & resolution : possibleResolutions ) { |
| 149 | if ( lowestResolution.gameWidth < resolution.gameWidth || lowestResolution.gameHeight < resolution.gameHeight || lowestResolution == resolution ) { |
| 150 | continue; |
| 151 | } |
| 152 | |
| 153 | resolutions.emplace( resolution ); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | std::set<fheroes2::ResolutionInfo> scaledResolutions; |
| 158 | |
| 159 | { |
| 160 | // Widescreen devices support much higher resolutions but items on such resolutions are too tiny. In order to improve user |
| 161 | // experience on these devices we are adding a special non-standard resolution with (potentially) non-integer scale. |
| 162 | assert( !resolutions.empty() ); |
| 163 | const fheroes2::ResolutionInfo biggestResolution = *( std::prev( resolutions.end() ) ); |
| 164 | assert( biggestResolution.gameWidth == biggestResolution.screenWidth && biggestResolution.gameHeight == biggestResolution.screenHeight ); |
| 165 | |
| 166 | // This resolution should be really "widescreen" (or at least square), i.e. the width should be not less than the height, otherwise |
| 167 | // the scaled resolution's in-game width will become less than DEFAULT_WIDTH |
| 168 | if ( biggestResolution.gameWidth > fheroes2::Display::DEFAULT_WIDTH && biggestResolution.gameWidth >= biggestResolution.gameHeight ) { |
| 169 | assert( biggestResolution.gameHeight >= fheroes2::Display::DEFAULT_HEIGHT ); |
| 170 | |
| 171 | scaledResolutions.emplace( biggestResolution.gameWidth * fheroes2::Display::DEFAULT_HEIGHT / biggestResolution.gameHeight, |
| 172 | fheroes2::Display::DEFAULT_HEIGHT, biggestResolution.gameWidth, biggestResolution.gameHeight ); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | // Try to find and add scaled resolutions with integer scale (x2, x3, x4, etc) which correspond to existing "hardware" resolutions |
| 177 | for ( const fheroes2::ResolutionInfo & resolution : resolutions ) { |
| 178 | assert( resolution.gameWidth == resolution.screenWidth && resolution.gameHeight == resolution.screenHeight ); |
| 179 | |
| 180 | const int32_t maxScale = std::min( resolution.gameWidth / fheroes2::Display::DEFAULT_WIDTH, resolution.gameHeight / fheroes2::Display::DEFAULT_HEIGHT ); |
| 181 | |
| 182 | for ( int32_t scale = 2; scale <= maxScale; ++scale ) { |
| 183 | if ( resolution.gameWidth % scale != 0 || resolution.gameHeight % scale != 0 ) { |
| 184 | continue; |
| 185 | } |
| 186 | |
| 187 | scaledResolutions.emplace( resolution.gameWidth / scale, resolution.gameHeight / scale, resolution.gameWidth, resolution.gameHeight ); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | resolutions.merge( scaledResolutions ); |
| 192 | |
| 193 | return resolutions; |
| 194 | } |
| 195 | #endif |
| 196 | |
| 197 | std::vector<uint8_t> StandardPaletteIndexes() |
| 198 | { |
| 199 | std::vector<uint8_t> indexes( 256 ); |
| 200 | for ( uint32_t i = 0; i < 256; ++i ) { |
| 201 | indexes[i] = static_cast<uint8_t>( i ); |
| 202 | } |
| 203 | return indexes; |
| 204 | } |
| 205 | |
| 206 | const uint8_t * PALPalette( const bool forceDefaultPaletteUpdate = false ) |
| 207 | { |
| 208 | static std::vector<uint8_t> palette; |
| 209 | if ( palette.empty() || forceDefaultPaletteUpdate ) { |
| 210 | const uint8_t * gamePalette = fheroes2::getGamePalette(); |
| 211 | |
| 212 | palette.resize( 256 * 3 ); |
| 213 | for ( size_t i = 0; i < palette.size(); ++i ) { |
| 214 | palette[i] = gamePalette[i] << 2; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | return palette.data(); |
| 219 | } |
| 220 | |
| 221 | // Updates `roi` to be only inside {0, 0, width, height} rectangle. |
| 222 | // Returns false if `roi` is out of such rectangle. |
| 223 | bool getActiveArea( fheroes2::Rect & roi, const int32_t width, const int32_t height ) |
| 224 | { |
| 225 | if ( roi.width <= 0 || roi.height <= 0 || roi.x >= width || roi.y >= height ) { |
| 226 | return false; |
| 227 | } |
| 228 | |
| 229 | if ( roi.x < 0 ) { |
| 230 | if ( -roi.x >= roi.width ) { |
| 231 | return false; |
| 232 | } |
| 233 | |
| 234 | roi.width += roi.x; |
| 235 | roi.x = 0; |
| 236 | } |
| 237 | |
| 238 | if ( roi.y < 0 ) { |
| 239 | if ( -roi.y >= roi.height ) { |
| 240 | return false; |
| 241 | } |
| 242 | |
| 243 | roi.height += roi.y; |
| 244 | roi.y = 0; |
| 245 | } |
| 246 | |
| 247 | if ( const int32_t offsetX = roi.x + roi.width - width; offsetX > 0 ) { |
| 248 | if ( offsetX >= roi.width ) { |
| 249 | return false; |
| 250 | } |
| 251 | |
| 252 | roi.width -= offsetX; |
| 253 | } |
| 254 | |
| 255 | if ( const int32_t offsetY = roi.y + roi.height - height; offsetY > 0 ) { |
| 256 | if ( offsetY >= roi.height ) { |
| 257 | return false; |
| 258 | } |
| 259 | |
| 260 | roi.height -= offsetY; |
| 261 | } |
| 262 | |
| 263 | return true; |
| 264 | } |
| 265 | |
| 266 | const uint8_t * currentPalette = PALPalette(); |
| 267 | |
| 268 | // If SDL library is used |
| 269 | #if !defined( TARGET_PS_VITA ) |
| 270 | class BaseSDLRenderer |
| 271 | { |
| 272 | protected: |
| 273 | std::vector<uint32_t> _palette32Bit; |
| 274 | std::vector<SDL_Color> _palette8Bit; |
| 275 | |
| 276 | void copyImageToSurface( const fheroes2::Image & image, SDL_Surface * surface, const fheroes2::Rect & roi ) |
| 277 | { |
| 278 | assert( surface != nullptr && !image.empty() ); |
| 279 | |
| 280 | if ( SDL_MUSTLOCK( surface ) ) { |
| 281 | const int returnCode = SDL_LockSurface( surface ); |
| 282 | if ( returnCode < 0 ) { |
| 283 | ERROR_LOG( "Failed to lock surface. The error value: " << returnCode << ", description: " << SDL_GetError() ) |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | const int32_t imageWidth = image.width(); |
| 288 | const int32_t imageHeight = image.height(); |
| 289 | |
| 290 | const bool fullFrame = ( roi.width == imageWidth ) && ( roi.height == imageHeight ); |
| 291 | |
| 292 | const uint8_t * imageIn = image.image(); |
| 293 | |
| 294 | if ( fullFrame ) { |
| 295 | if ( surface->format->BitsPerPixel == 32 ) { |
| 296 | uint32_t * out = static_cast<uint32_t *>( surface->pixels ); |
| 297 | const uint32_t * outEnd = out + imageWidth * imageHeight; |
| 298 | const uint8_t * in = imageIn; |
| 299 | const uint32_t * transform = _palette32Bit.data(); |
| 300 | |
| 301 | for ( ; out != outEnd; ++out, ++in ) |
| 302 | *out = *( transform + *in ); |
| 303 | } |
| 304 | else if ( ( surface->format->BitsPerPixel == 8 ) && ( surface->pixels != imageIn ) ) { |
| 305 | if ( imageWidth % 4 != 0 ) { |
| 306 | const int32_t screenWidth = ( imageWidth / 4 ) * 4 + 4; |
| 307 | for ( int32_t i = 0; i < imageHeight; ++i ) { |
| 308 | memcpy( static_cast<uint8_t *>( surface->pixels ) + screenWidth * i, imageIn + imageWidth * i, static_cast<size_t>( imageWidth ) ); |
| 309 | } |
| 310 | } |
| 311 | else { |
| 312 | memcpy( surface->pixels, imageIn, static_cast<size_t>( imageWidth ) * imageHeight ); |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | else { |
| 317 | if ( surface->format->BitsPerPixel == 32 ) { |
| 318 | uint32_t * outY = static_cast<uint32_t *>( surface->pixels ); |
| 319 | const uint32_t * outYEnd = outY + imageWidth * roi.height; |
| 320 | const uint8_t * inY = imageIn + roi.x + roi.y * imageWidth; |
| 321 | const uint32_t * transform = _palette32Bit.data(); |
| 322 | |
| 323 | for ( ; outY != outYEnd; outY += imageWidth, inY += imageWidth ) { |
| 324 | uint32_t * outX = outY; |
| 325 | const uint32_t * outXEnd = outX + roi.width; |
| 326 | const uint8_t * inX = inY; |
| 327 | |
| 328 | for ( ; outX != outXEnd; ++outX, ++inX ) |
| 329 | *outX = *( transform + *inX ); |
| 330 | } |
| 331 | } |
| 332 | else if ( ( surface->format->BitsPerPixel == 8 ) && ( surface->pixels != imageIn ) ) { |
| 333 | const int32_t screenWidth = ( imageWidth / 4 ) * 4 + 4; |
| 334 | const int32_t screenOffset = roi.x + roi.y * screenWidth; |
| 335 | const int32_t imageOffset = roi.x + roi.y * imageWidth; |
| 336 | for ( int32_t i = 0; i < roi.height; ++i ) { |
| 337 | memcpy( static_cast<uint8_t *>( surface->pixels ) + screenWidth * i + screenOffset, imageIn + imageOffset + imageWidth * i, |
| 338 | static_cast<size_t>( roi.width ) ); |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | if ( SDL_MUSTLOCK( surface ) ) |
| 344 | SDL_UnlockSurface( surface ); |
| 345 | } |
| 346 | |
| 347 | void generatePalette( const std::vector<uint8_t> & colorIds, const SDL_Surface * surface ) |
| 348 | { |
| 349 | assert( surface != nullptr ); |
| 350 | |
| 351 | if ( surface->format->BitsPerPixel == 32 ) { |
| 352 | _palette32Bit.resize( 256u ); |
| 353 | |
| 354 | if ( surface->format->Amask > 0 ) { |
| 355 | for ( size_t i = 0; i < 256u; ++i ) { |
| 356 | const uint8_t * value = currentPalette + colorIds[i] * 3; |
| 357 | _palette32Bit[i] = SDL_MapRGBA( surface->format, *value, *( value + 1 ), *( value + 2 ), 255 ); |
| 358 | } |
| 359 | } |
| 360 | else { |
| 361 | for ( size_t i = 0; i < 256u; ++i ) { |
| 362 | const uint8_t * value = currentPalette + colorIds[i] * 3; |
| 363 | _palette32Bit[i] = SDL_MapRGB( surface->format, *value, *( value + 1 ), *( value + 2 ) ); |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | else if ( surface->format->BitsPerPixel == 8 ) { |
| 368 | _palette8Bit.resize( 256 ); |
| 369 | for ( uint32_t i = 0; i < 256; ++i ) { |
| 370 | const uint8_t * value = currentPalette + colorIds[i] * 3; |
| 371 | SDL_Color & col = _palette8Bit[i]; |
| 372 | |
| 373 | col.r = *value; |
| 374 | col.g = *( value + 1 ); |
| 375 | col.b = *( value + 2 ); |
| 376 | } |
| 377 | } |
| 378 | else { |
| 379 | // This is unsupported format. Please implement it. |
| 380 | assert( 0 ); |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | static SDL_Surface * generateIconSurface( const fheroes2::Image & icon ) |
| 385 | { |
| 386 | if ( icon.empty() || icon.singleLayer() ) { |
| 387 | // What are you trying to do? Icon should have not empty both image and transform layers. |
| 388 | assert( 0 ); |
| 389 | return nullptr; |
| 390 | } |
| 391 | |
| 392 | SDL_Surface * surface = SDL_CreateRGBSurface( 0, icon.width(), icon.height(), 32, 0xFF, 0xFF00, 0xFF0000, 0xFF000000 ); |
| 393 | if ( surface == nullptr ) { |
| 394 | ERROR_LOG( "Failed to create a surface of " << icon.width() << " x " << icon.height() << " size for cursor. The error: " << SDL_GetError() ) |
| 395 | return nullptr; |
| 396 | } |
| 397 | |
| 398 | const uint32_t width = icon.width(); |
| 399 | const uint32_t height = icon.height(); |
| 400 | |
| 401 | uint32_t * out = static_cast<uint32_t *>( surface->pixels ); |
| 402 | const uint32_t * outEnd = out + width * height; |
| 403 | const uint8_t * in = icon.image(); |
| 404 | const uint8_t * transform = icon.transform(); |
| 405 | |
| 406 | if ( surface->format->Amask > 0 ) { |
| 407 | for ( ; out != outEnd; ++out, ++in, ++transform ) { |
| 408 | if ( *transform == 0 ) { |
| 409 | const uint8_t * value = currentPalette + *in * 3; |
| 410 | *out = SDL_MapRGBA( surface->format, *value, *( value + 1 ), *( value + 2 ), 255 ); |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | else { |
| 415 | for ( ; out != outEnd; ++out, ++in, ++transform ) { |
| 416 | if ( *transform == 0 ) { |
| 417 | const uint8_t * value = currentPalette + *in * 3; |
| 418 | *out = SDL_MapRGB( surface->format, *value, *( value + 1 ), *( value + 2 ) ); |
| 419 | } |
| 420 | else { |
| 421 | *out = SDL_MapRGB( surface->format, 0, 0, 0 ); |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | return surface; |
| 427 | } |
| 428 | }; |
| 429 | #endif |
| 430 | |
| 431 | class RenderCursor final : public fheroes2::Cursor |
| 432 | { |
| 433 | public: |
| 434 | RenderCursor( const RenderCursor & ) = delete; |
| 435 | |
| 436 | ~RenderCursor() override |
| 437 | { |
| 438 | clear(); |
| 439 | } |
| 440 | |
| 441 | RenderCursor & operator=( const RenderCursor & ) = delete; |
| 442 | |
| 443 | void show( const bool enable ) override |
| 444 | { |
| 445 | fheroes2::Cursor::show( enable ); |
| 446 | |
| 447 | if ( !_emulation ) { |
| 448 | const int returnCode = SDL_ShowCursor( _show ? SDL_ENABLE : SDL_DISABLE ); |
| 449 | if ( returnCode < 0 ) { |
| 450 | ERROR_LOG( "Failed to set cursor. The error value: " << returnCode << ", description: " << SDL_GetError() ) |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | bool isVisible() const override |
| 456 | { |
| 457 | if ( _emulation ) { |
| 458 | return fheroes2::Cursor::isVisible(); |
| 459 | } |
| 460 | |
| 461 | return fheroes2::Cursor::isVisible() && ( SDL_ShowCursor( SDL_QUERY ) == SDL_ENABLE ); |
| 462 | } |
| 463 | |
| 464 | void update( const fheroes2::Image & image, int32_t offsetX, int32_t offsetY ) override |
| 465 | { |
| 466 | if ( image.empty() || image.singleLayer() ) { |
| 467 | // What are you trying to do? Set an invisible cursor? Use hide() method! |
| 468 | assert( 0 ); |
| 469 | return; |
| 470 | } |
| 471 | |
| 472 | if ( _emulation ) { |
| 473 | fheroes2::Cursor::update( image, offsetX, offsetY ); |
| 474 | return; |
| 475 | } |
| 476 | |
| 477 | SDL_Surface * surface = SDL_CreateRGBSurface( 0, image.width(), image.height(), 32, 0xFF, 0xFF00, 0xFF0000, 0xFF000000 ); |
| 478 | if ( surface == nullptr ) { |
| 479 | ERROR_LOG( "Failed to create a surface of " << image.width() << " x " << image.height() << " size for cursor. The error: " << SDL_GetError() ) |
| 480 | return; |
| 481 | } |
| 482 | |
| 483 | const uint32_t width = image.width(); |
| 484 | const uint32_t height = image.height(); |
| 485 | |
| 486 | uint32_t * out = static_cast<uint32_t *>( surface->pixels ); |
| 487 | const uint32_t * outEnd = out + width * height; |
| 488 | const uint8_t * in = image.image(); |
| 489 | const uint8_t * transform = image.transform(); |
| 490 | |
| 491 | if ( surface->format->Amask > 0 ) { |
| 492 | for ( ; out != outEnd; ++out, ++in, ++transform ) { |
| 493 | if ( *transform == 0 ) { |
| 494 | const uint8_t * value = currentPalette + *in * 3; |
| 495 | *out = SDL_MapRGBA( surface->format, *value, *( value + 1 ), *( value + 2 ), 255 ); |
| 496 | } |
| 497 | else if ( *transform > 1 ) { |
| 498 | // SDL2 uses RGBA image on OS level separately from frame rendering. |
| 499 | // Here we are trying to simulate cursor's shadow as close as possible to the original game. |
| 500 | *out = SDL_MapRGBA( surface->format, 0, 0, 0, 64 ); |
| 501 | } |
| 502 | } |
| 503 | } |
| 504 | else { |
| 505 | for ( ; out != outEnd; ++out, ++in, ++transform ) { |
| 506 | if ( *transform == 0 ) { |
| 507 | const uint8_t * value = currentPalette + *in * 3; |
| 508 | *out = SDL_MapRGB( surface->format, *value, *( value + 1 ), *( value + 2 ) ); |
| 509 | } |
| 510 | else { |
| 511 | *out = SDL_MapRGB( surface->format, 0, 0, 0 ); |
| 512 | } |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | SDL_Cursor * tempCursor = SDL_CreateColorCursor( surface, offsetX, offsetY ); |
| 517 | if ( tempCursor == nullptr ) { |
| 518 | ERROR_LOG( "Failed to create a cursor. The error description: " << SDL_GetError() ) |
| 519 | } |
| 520 | else { |
| 521 | SDL_SetCursor( tempCursor ); |
| 522 | } |
| 523 | |
| 524 | const int returnCode = SDL_ShowCursor( _show ? SDL_ENABLE : SDL_DISABLE ); |
| 525 | if ( returnCode < 0 ) { |
| 526 | ERROR_LOG( "Failed to set cursor state. The error value: " << returnCode << ", description: " << SDL_GetError() ) |
| 527 | } |
| 528 | SDL_FreeSurface( surface ); |
| 529 | |
| 530 | if ( tempCursor != nullptr ) { |
| 531 | clear(); |
| 532 | std::swap( _cursor, tempCursor ); |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | void enableSoftwareEmulation( const bool enable ) override |
| 537 | { |
| 538 | if ( enable == _emulation ) { |
| 539 | return; |
| 540 | } |
| 541 | |
| 542 | if ( enable ) { |
| 543 | clear(); |
| 544 | |
| 545 | const int returnCode = SDL_ShowCursor( SDL_DISABLE ); |
| 546 | if ( returnCode < 0 ) { |
| 547 | ERROR_LOG( "Failed to disable cursor. The error value: " << returnCode << ", description: " << SDL_GetError() ) |
| 548 | } |
| 549 | |
| 550 | _emulation = true; |
| 551 | } |
| 552 | else { |
| 553 | const int returnCode = SDL_ShowCursor( _show ? SDL_ENABLE : SDL_DISABLE ); |
| 554 | if ( returnCode < 0 ) { |
| 555 | ERROR_LOG( "Failed to set cursor state. The error value: " << returnCode << ", description: " << SDL_GetError() ) |
| 556 | } |
| 557 | |
| 558 | _emulation = false; |
| 559 | } |
| 560 | |
| 561 | if ( _cursorUpdater != nullptr ) { |
| 562 | _cursorUpdater(); |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | static RenderCursor * create() |
| 567 | { |
| 568 | return new RenderCursor; |
| 569 | } |
| 570 | |
| 571 | private: |
| 572 | SDL_Cursor * _cursor{ nullptr }; |
| 573 | |
| 574 | RenderCursor() |
| 575 | { |
| 576 | _emulation = false; |
| 577 | |
| 578 | const int returnCode = SDL_ShowCursor( _show ? SDL_ENABLE : SDL_DISABLE ); |
| 579 | if ( returnCode < 0 ) { |
| 580 | ERROR_LOG( "Failed to set cursor state. The error value: " << returnCode << ", description: " << SDL_GetError() ) |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | void clear() |
| 585 | { |
| 586 | if ( _cursor != nullptr ) { |
| 587 | SDL_FreeCursor( _cursor ); |
| 588 | _cursor = nullptr; |
| 589 | } |
| 590 | } |
| 591 | }; |
| 592 | |
| 593 | #if defined( TARGET_PS_VITA ) |
| 594 | class RenderEngine final : public fheroes2::BaseRenderEngine |
| 595 | { |
| 596 | public: |
| 597 | RenderEngine( const RenderEngine & ) = delete; |
| 598 | |
| 599 | ~RenderEngine() override |
| 600 | { |
| 601 | clear(); |
| 602 | } |
| 603 | |
| 604 | RenderEngine & operator=( const RenderEngine & ) = delete; |
| 605 | |
| 606 | void toggleFullScreen() override |
| 607 | { |
| 608 | BaseRenderEngine::toggleFullScreen(); |
| 609 | |
| 610 | const fheroes2::Display & display = fheroes2::Display::instance(); |
| 611 | _calculateScreenScaling( display.width(), display.height(), isFullScreen() ); |
| 612 | } |
| 613 | |
| 614 | static RenderEngine * create() |
| 615 | { |
| 616 | return new RenderEngine; |
| 617 | } |
| 618 | |
| 619 | fheroes2::Rect getActiveWindowROI() const override |
| 620 | { |
| 621 | return _destRect; |
| 622 | } |
| 623 | |
| 624 | fheroes2::Size getCurrentScreenResolution() const override |
| 625 | { |
| 626 | return { VITA_FULLSCREEN_WIDTH, VITA_FULLSCREEN_HEIGHT }; |
| 627 | } |
| 628 | |
| 629 | std::vector<fheroes2::ResolutionInfo> getAvailableResolutions() const override |
| 630 | { |
| 631 | static const std::vector<fheroes2::ResolutionInfo> filteredResolutions = []() { |
| 632 | std::set<fheroes2::ResolutionInfo> resolutionSet; |
| 633 | resolutionSet.emplace( fheroes2::Display::DEFAULT_WIDTH, fheroes2::Display::DEFAULT_HEIGHT ); |
| 634 | resolutionSet.emplace( VITA_ASPECT_CORRECTED_WIDTH, fheroes2::Display::DEFAULT_HEIGHT ); |
| 635 | resolutionSet.emplace( VITA_FULLSCREEN_WIDTH, VITA_FULLSCREEN_HEIGHT ); |
| 636 | |
| 637 | return std::vector<fheroes2::ResolutionInfo>{ resolutionSet.rbegin(), resolutionSet.rend() }; |
| 638 | }(); |
| 639 | |
| 640 | return filteredResolutions; |
| 641 | } |
| 642 | |
| 643 | private: |
| 644 | SDL_Window * _window{ nullptr }; |
| 645 | SDL_Surface * _surface{ nullptr }; |
| 646 | vita2d_texture * _texBuffer{ nullptr }; |
| 647 | uint8_t * _palettedTexturePointer{ nullptr }; |
| 648 | fheroes2::Rect _destRect; |
| 649 | |
| 650 | RenderEngine() = default; |
| 651 | |
| 652 | enum : int32_t |
| 653 | { |
| 654 | VITA_FULLSCREEN_WIDTH = 960, |
| 655 | VITA_FULLSCREEN_HEIGHT = 544, |
| 656 | VITA_ASPECT_CORRECTED_WIDTH = 848 |
| 657 | }; |
| 658 | |
| 659 | void clear() override |
| 660 | { |
| 661 | if ( _window != nullptr ) { |
| 662 | SDL_DestroyWindow( _window ); |
| 663 | _window = nullptr; |
| 664 | } |
| 665 | |
| 666 | if ( _surface != nullptr ) { |
| 667 | SDL_FreeSurface( _surface ); |
| 668 | _surface = nullptr; |
| 669 | } |
| 670 | |
| 671 | vita2d_fini(); |
| 672 | |
| 673 | if ( _texBuffer != nullptr ) { |
| 674 | vita2d_free_texture( _texBuffer ); |
| 675 | _texBuffer = nullptr; |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | bool allocate( fheroes2::ResolutionInfo & resolutionInfo, bool isFullScreen ) override |
| 680 | { |
| 681 | clear(); |
| 682 | |
| 683 | const std::vector<fheroes2::ResolutionInfo> resolutions = getAvailableResolutions(); |
| 684 | assert( !resolutions.empty() ); |
| 685 | if ( !resolutions.empty() ) { |
| 686 | resolutionInfo = GetNearestResolution( resolutionInfo, resolutions ); |
| 687 | } |
| 688 | |
| 689 | vita2d_init(); |
| 690 | |
| 691 | _window = SDL_CreateWindow( "", 0, 0, resolutionInfo.gameWidth, resolutionInfo.gameHeight, 0 ); |
| 692 | if ( _window == nullptr ) { |
| 693 | ERROR_LOG( "Failed to create an application window of " << resolutionInfo.gameWidth << " x " << resolutionInfo.gameHeight |
| 694 | << " size. The error: " << SDL_GetError() ) |
| 695 | |
| 696 | clear(); |
| 697 | return false; |
| 698 | } |
| 699 | |
| 700 | _surface = SDL_CreateRGBSurface( 0, 1, 1, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 ); |
| 701 | |
| 702 | if ( _surface == nullptr || _surface->w <= 0 || _surface->h <= 0 ) { |
| 703 | ERROR_LOG( "Failed to create a surface of " << resolutionInfo.gameWidth << " x " << resolutionInfo.gameHeight << " size. The error: " << SDL_GetError() ) |
| 704 | |
| 705 | clear(); |
| 706 | return false; |
| 707 | } |
| 708 | |
| 709 | vita2d_texture_set_alloc_memblock_type( SCE_KERNEL_MEMBLOCK_TYPE_USER_CDRAM_RW ); |
| 710 | _texBuffer = vita2d_create_empty_texture_format( resolutionInfo.gameWidth, resolutionInfo.gameHeight, SCE_GXM_TEXTURE_FORMAT_P8_ABGR ); |
| 711 | _palettedTexturePointer = static_cast<uint8_t *>( vita2d_texture_get_datap( _texBuffer ) ); |
| 712 | memset( _palettedTexturePointer, 0, resolutionInfo.gameWidth * resolutionInfo.gameHeight * sizeof( uint8_t ) ); |
| 713 | _createPalette(); |
| 714 | |
| 715 | _calculateScreenScaling( resolutionInfo.gameWidth, resolutionInfo.gameHeight, isFullScreen ); |
| 716 | |
| 717 | return true; |
| 718 | } |
| 719 | |
| 720 | void render( const fheroes2::Display & display, const fheroes2::Rect & roi ) override |
| 721 | { |
| 722 | (void)roi; |
| 723 | |
| 724 | if ( _texBuffer == nullptr ) |
| 725 | return; |
| 726 | |
| 727 | const int32_t width = display.width(); |
| 728 | const int32_t height = display.height(); |
| 729 | |
| 730 | SDL_memcpy( _palettedTexturePointer, display.image(), width * height * sizeof( uint8_t ) ); |
| 731 | |
| 732 | vita2d_start_drawing(); |
| 733 | vita2d_draw_rectangle( 0, 0, VITA_FULLSCREEN_WIDTH, VITA_FULLSCREEN_HEIGHT, 0xff000000 ); |
| 734 | vita2d_draw_texture_scale( _texBuffer, _destRect.x, _destRect.y, static_cast<float>( _destRect.width ) / width, |
| 735 | static_cast<float>( _destRect.height ) / height ); |
| 736 | vita2d_end_drawing(); |
| 737 | vita2d_swap_buffers(); |
| 738 | } |
| 739 | |
| 740 | void updatePalette( const std::vector<uint8_t> & colorIds ) override |
| 741 | { |
| 742 | if ( _surface == nullptr || colorIds.size() != 256 || _texBuffer == nullptr ) |
| 743 | return; |
| 744 | |
| 745 | uint32_t palette32Bit[256u]; |
| 746 | |
| 747 | for ( size_t i = 0; i < 256u; ++i ) { |
| 748 | const uint8_t * value = currentPalette + colorIds[i] * 3; |
| 749 | palette32Bit[i] = SDL_MapRGBA( _surface->format, *value, *( value + 1 ), *( value + 2 ), 255 ); |
| 750 | } |
| 751 | |
| 752 | memcpy( vita2d_texture_get_palette( _texBuffer ), palette32Bit, sizeof( uint32_t ) * 256 ); |
| 753 | } |
| 754 | |
| 755 | bool isMouseCursorActive() const override |
| 756 | { |
| 757 | return true; |
| 758 | } |
| 759 | |
| 760 | void _createPalette() |
| 761 | { |
| 762 | updatePalette( StandardPaletteIndexes() ); |
| 763 | } |
| 764 | |
| 765 | void _calculateScreenScaling( const int32_t width_, const int32_t height_, const bool isFullScreen ) |
| 766 | { |
| 767 | _destRect.x = 0; |
| 768 | _destRect.y = 0; |
| 769 | _destRect.width = width_; |
| 770 | _destRect.height = height_; |
| 771 | |
| 772 | if ( width_ == VITA_FULLSCREEN_WIDTH && height_ == VITA_FULLSCREEN_HEIGHT ) { |
| 773 | // Nothing to do more. |
| 774 | return; |
| 775 | } |
| 776 | |
| 777 | if ( !isFullScreen ) { |
| 778 | // Center game area. |
| 779 | _destRect.x = ( VITA_FULLSCREEN_WIDTH - width_ ) / 2; |
| 780 | _destRect.y = ( VITA_FULLSCREEN_HEIGHT - height_ ) / 2; |
| 781 | return; |
| 782 | } |
| 783 | |
| 784 | const SceGxmTextureFilter textureFilter = isNearestScaling() ? SCE_GXM_TEXTURE_FILTER_POINT : SCE_GXM_TEXTURE_FILTER_LINEAR; |
| 785 | |
| 786 | vita2d_texture_set_filters( _texBuffer, textureFilter, textureFilter ); |
| 787 | if ( ( static_cast<float>( VITA_FULLSCREEN_WIDTH ) / VITA_FULLSCREEN_HEIGHT ) >= ( static_cast<float>( width_ ) / height_ ) ) { |
| 788 | const float scale = static_cast<float>( VITA_FULLSCREEN_HEIGHT ) / height_; |
| 789 | _destRect.width = static_cast<int32_t>( static_cast<float>( width_ ) * scale ); |
| 790 | _destRect.height = VITA_FULLSCREEN_HEIGHT; |
| 791 | _destRect.x = ( VITA_FULLSCREEN_WIDTH - _destRect.width ) / 2; |
| 792 | } |
| 793 | else { |
| 794 | const float scale = static_cast<float>( VITA_FULLSCREEN_WIDTH ) / width_; |
| 795 | _destRect.width = VITA_FULLSCREEN_WIDTH; |
| 796 | _destRect.height = static_cast<int32_t>( static_cast<float>( height_ ) * scale ); |
| 797 | _destRect.y = ( VITA_FULLSCREEN_HEIGHT - _destRect.height ) / 2; |
| 798 | } |
| 799 | } |
| 800 | }; |
| 801 | #else |
| 802 | bool shouldUseFullscreenDesktopMode() |
| 803 | { |
| 804 | return fheroes2::cursor().isSoftwareEmulation(); |
| 805 | } |
| 806 | |
| 807 | std::optional<bool> isWindowInAnyDisplay( const fheroes2::ResolutionInfo & resolutionInfo, const fheroes2::Point & windowPos ) |
| 808 | { |
| 809 | const int numDisplays = SDL_GetNumVideoDisplays(); |
| 810 | if ( numDisplays < 1 ) { |
| 811 | ERROR_LOG( "Failed to get the number of video displays. The error value: " << numDisplays << ", description: " << SDL_GetError() ) |
| 812 | return std::nullopt; |
| 813 | } |
| 814 | |
| 815 | for ( int displayIndex = 0; displayIndex < numDisplays; ++displayIndex ) { |
| 816 | SDL_Rect displayBounds; |
| 817 | if ( SDL_GetDisplayBounds( displayIndex, &displayBounds ) != 0 ) { |
| 818 | ERROR_LOG( "Failed to get display bounds for display #" << displayIndex << ". The error: " << SDL_GetError() ) |
| 819 | continue; |
| 820 | } |
| 821 | const int displayMaxX = displayBounds.x + displayBounds.w; |
| 822 | const int displayMaxY = displayBounds.y + displayBounds.h; |
| 823 | const int32_t windowMaxX = windowPos.x + resolutionInfo.screenWidth; |
| 824 | const int32_t windowMaxY = windowPos.y + resolutionInfo.screenHeight; |
| 825 | if ( windowPos.x >= displayBounds.x && windowMaxX < displayMaxX && windowPos.y >= displayBounds.y && windowMaxY < displayMaxY ) { |
| 826 | return true; |
| 827 | } |
| 828 | } |
| 829 | return false; |
| 830 | } |
| 831 | |
| 832 | class RenderEngine final : public fheroes2::BaseRenderEngine, public BaseSDLRenderer |
| 833 | { |
| 834 | public: |
| 835 | RenderEngine( const RenderEngine & ) = delete; |
| 836 | |
| 837 | ~RenderEngine() override |
| 838 | { |
| 839 | clear(); |
| 840 | } |
| 841 | |
| 842 | RenderEngine & operator=( const RenderEngine & ) = delete; |
| 843 | |
| 844 | void toggleFullScreen() override |
| 845 | { |
| 846 | if ( _window == nullptr ) { |
| 847 | BaseRenderEngine::toggleFullScreen(); |
| 848 | return; |
| 849 | } |
| 850 | |
| 851 | uint32_t flags = SDL_GetWindowFlags( _window ); |
| 852 | if ( flags & ( SDL_WINDOW_FULLSCREEN | SDL_WINDOW_FULLSCREEN_DESKTOP ) ) { |
| 853 | flags &= ~( SDL_WINDOW_FULLSCREEN | SDL_WINDOW_FULLSCREEN_DESKTOP ); |
| 854 | } |
| 855 | else { |
| 856 | #if defined( _WIN32 ) |
| 857 | // We force fullscreen at desktop resolution for nearest scaling to disable hardware scaling of game resolution by the monitor. |
| 858 | if ( isNearestScaling() || fheroes2::cursor().isSoftwareEmulation() ) { |
| 859 | flags |= SDL_WINDOW_FULLSCREEN_DESKTOP; |
| 860 | } |
| 861 | else { |
| 862 | flags |= SDL_WINDOW_FULLSCREEN; |
| 863 | } |
| 864 | #else |
| 865 | flags |= SDL_WINDOW_FULLSCREEN_DESKTOP; |
| 866 | #endif |
| 867 | |
| 868 | // Update the window position, in case we get queried for it |
| 869 | SDL_GetWindowPosition( _window, &_prevWindowPos.x, &_prevWindowPos.y ); |
| 870 | |
| 871 | // If the window size has been manually changed to one that does not match any of the resolutions supported by the display, then after switching |
| 872 | // to full-screen mode, the in-game display area may not occupy the entire screen, and black bars will remain on the sides of the screen. In this |
| 873 | // case, even if it is specified to use the SDL_WINDOW_FULLSCREEN, the SDL_WINDOW_FULLSCREEN_DESKTOP will still be used under the hood. To avoid |
| 874 | // this, we need to remember the window size (to restore it later when turning off full-screen mode) and then set the window size to the size of |
| 875 | // the in-game display area before switching to the full-screen mode. |
| 876 | SDL_GetWindowSize( _window, &_windowedSize.width, &_windowedSize.height ); |
| 877 | |
| 878 | if ( const fheroes2::Display & display = fheroes2::Display::instance(); display.width() != 0 && display.height() != 0 ) { |
| 879 | assert( display.screenSize().width >= display.width() && display.screenSize().height >= display.height() ); |
| 880 | |
| 881 | SDL_SetWindowSize( _window, display.screenSize().width, display.screenSize().height ); |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | if ( const int returnCode = SDL_SetWindowFullscreen( _window, flags ); returnCode < 0 ) { |
| 886 | ERROR_LOG( "Failed to set fullscreen mode flags. The error value: " << returnCode << ", description: " << SDL_GetError() ) |
| 887 | } |
| 888 | |
| 889 | _syncFullScreen(); |
| 890 | |
| 891 | // If the full-screen mode has been turned off, then restore the remembered window size. |
| 892 | if ( !isFullScreen() && _windowedSize.width != 0 && _windowedSize.height != 0 ) { |
| 893 | SDL_SetWindowSize( _window, _windowedSize.width, _windowedSize.height ); |
| 894 | } |
| 895 | |
| 896 | _retrieveWindowInfo(); |
| 897 | _toggleMouseCaptureMode(); |
| 898 | } |
| 899 | |
| 900 | bool isFullScreen() const override |
| 901 | { |
| 902 | if ( _window == nullptr ) { |
| 903 | return BaseRenderEngine::isFullScreen(); |
| 904 | } |
| 905 | |
| 906 | const uint32_t flags = SDL_GetWindowFlags( _window ); |
| 907 | return ( flags & ( SDL_WINDOW_FULLSCREEN | SDL_WINDOW_FULLSCREEN_DESKTOP ) ); |
| 908 | } |
| 909 | |
| 910 | std::vector<fheroes2::ResolutionInfo> getAvailableResolutions() const override |
| 911 | { |
| 912 | std::set<fheroes2::ResolutionInfo> resolutionSet; |
| 913 | |
| 914 | const int displayCount = SDL_GetNumVideoDisplays(); |
| 915 | if ( displayCount >= 1 ) { |
| 916 | for ( int displayId = 0; displayId < displayCount; ++displayId ) { |
| 917 | const int displayModeCount = SDL_GetNumDisplayModes( displayId ); |
| 918 | if ( displayModeCount >= 1 ) { |
| 919 | for ( int i = 0; i < displayModeCount; ++i ) { |
| 920 | SDL_DisplayMode videoMode; |
| 921 | const int returnCode = SDL_GetDisplayMode( displayId, i, &videoMode ); |
| 922 | if ( returnCode != 0 ) { |
| 923 | ERROR_LOG( "Failed to get display mode on display " << displayId << ". The error value: " << returnCode |
| 924 | << ", description: " << SDL_GetError() ) |
| 925 | } |
| 926 | else { |
| 927 | resolutionSet.emplace( videoMode.w, videoMode.h ); |
| 928 | } |
| 929 | } |
| 930 | } |
| 931 | else { |
| 932 | ERROR_LOG( "Failed to get the number of display modes for display" << displayId << ". The error value: " << displayModeCount |
| 933 | << ", description: " << SDL_GetError() ) |
| 934 | } |
| 935 | } |
| 936 | } |
| 937 | else { |
| 938 | ERROR_LOG( "Failed to get the number of displays. The error value: " << displayCount << ", description: " << SDL_GetError() ) |
| 939 | } |
| 940 | |
| 941 | #if defined( TARGET_NINTENDO_SWITCH ) |
| 942 | // Nintendo Switch supports arbitrary resolutions via the HW scaler |
| 943 | // 848x480 is the smallest resolution supported by fheroes2 |
| 944 | resolutionSet.emplace( 848, 480 ); |
| 945 | #endif |
| 946 | resolutionSet = FilterResolutions( resolutionSet ); |
| 947 | |
| 948 | if ( displayCount < 1 ) { |
| 949 | return std::vector<fheroes2::ResolutionInfo>{ resolutionSet.rbegin(), resolutionSet.rend() }; |
| 950 | } |
| 951 | |
| 952 | if ( !shouldUseFullscreenDesktopMode() ) { |
| 953 | // If software emulation is not enabled it means that we need to use resolutions supported |
| 954 | // by the hardware. |
| 955 | return std::vector<fheroes2::ResolutionInfo>{ resolutionSet.rbegin(), resolutionSet.rend() }; |
| 956 | } |
| 957 | |
| 958 | // We should limit all available resolutions to the one which is currently chosen on the system |
| 959 | // to avoid ending up having application window which is bigger than the screen resolution. |
| 960 | SDL_DisplayMode maxDisplayMode{}; |
| 961 | |
| 962 | for ( int displayId = 0; displayId < displayCount; ++displayId ) { |
| 963 | SDL_DisplayMode displayMode; |
| 964 | |
| 965 | const int returnValue = SDL_GetCurrentDisplayMode( displayId, &displayMode ); |
| 966 | if ( returnValue < 0 ) { |
| 967 | ERROR_LOG( "Failed to retrieve the current display mode for display " << displayId << ". The error value: " << returnValue |
| 968 | << ", description: " << SDL_GetError() ) |
| 969 | continue; |
| 970 | } |
| 971 | |
| 972 | // There is no an ideal formula of how to choose the biggest resolution among multiple displays |
| 973 | // so let's use a simple and well-known approach. |
| 974 | maxDisplayMode.w = std::max( maxDisplayMode.w, displayMode.w ); |
| 975 | maxDisplayMode.h = std::max( maxDisplayMode.h, displayMode.h ); |
| 976 | } |
| 977 | |
| 978 | if ( maxDisplayMode.w == 0 || maxDisplayMode.h == 0 ) { |
| 979 | // None of displays returned a value display mode. |
| 980 | return std::vector<fheroes2::ResolutionInfo>{ resolutionSet.rbegin(), resolutionSet.rend() }; |
| 981 | } |
| 982 | |
| 983 | // If the current display resolution is somehow very small, ignore it. |
| 984 | // It could happen when a device has smaller than the required by the engine resolution. |
| 985 | if ( maxDisplayMode.w < fheroes2::Display::DEFAULT_WIDTH || maxDisplayMode.h < fheroes2::Display::DEFAULT_HEIGHT ) { |
| 986 | return std::vector<fheroes2::ResolutionInfo>{ resolutionSet.rbegin(), resolutionSet.rend() }; |
| 987 | } |
| 988 | |
| 989 | std::vector<fheroes2::ResolutionInfo> temp; |
| 990 | for ( auto iter = resolutionSet.rbegin(); iter != resolutionSet.rend(); ++iter ) { |
| 991 | if ( iter->screenWidth <= maxDisplayMode.w && iter->screenHeight <= maxDisplayMode.h ) { |
| 992 | temp.emplace_back( *iter ); |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | return temp; |
| 997 | } |
| 998 | |
| 999 | void setTitle( const std::string & title ) override |
| 1000 | { |
| 1001 | if ( _window == nullptr ) { |
| 1002 | return; |
| 1003 | } |
| 1004 | |
| 1005 | SDL_SetWindowTitle( _window, System::encLocalToUTF8( title ).c_str() ); |
| 1006 | } |
| 1007 | |
| 1008 | void setIcon( const fheroes2::Image & icon ) override |
| 1009 | { |
| 1010 | if ( _window == nullptr ) |
| 1011 | return; |
| 1012 | |
| 1013 | SDL_Surface * surface = generateIconSurface( icon ); |
| 1014 | if ( surface == nullptr ) |
| 1015 | return; |
| 1016 | |
| 1017 | SDL_SetWindowIcon( _window, surface ); |
| 1018 | |
| 1019 | SDL_FreeSurface( surface ); |
| 1020 | } |
| 1021 | |
| 1022 | fheroes2::Rect getActiveWindowROI() const override |
| 1023 | { |
| 1024 | return _activeWindowROI; |
| 1025 | } |
| 1026 | |
| 1027 | fheroes2::Size getCurrentScreenResolution() const override |
| 1028 | { |
| 1029 | return _currentScreenResolution; |
| 1030 | } |
| 1031 | |
| 1032 | static RenderEngine * create() |
| 1033 | { |
| 1034 | return new RenderEngine; |
| 1035 | } |
| 1036 | |
| 1037 | void setVSync( const bool enable ) override |
| 1038 | { |
| 1039 | _isVSyncEnabled = enable; |
| 1040 | |
| 1041 | if ( _renderer == nullptr ) { |
| 1042 | return; |
| 1043 | } |
| 1044 | |
| 1045 | _toggleVSync(); |
| 1046 | } |
| 1047 | |
| 1048 | fheroes2::Point getWindowPos() const override |
| 1049 | { |
| 1050 | if ( isFullScreen() ) { |
| 1051 | return _prevWindowPos; |
| 1052 | } |
| 1053 | if ( _window == nullptr ) { |
| 1054 | return { -1, -1 }; |
| 1055 | } |
| 1056 | fheroes2::Point result; |
| 1057 | SDL_GetWindowPosition( _window, &result.x, &result.y ); |
| 1058 | return result; |
| 1059 | } |
| 1060 | |
| 1061 | void setWindowPos( const fheroes2::Point pos ) override |
| 1062 | { |
| 1063 | if ( pos == fheroes2::Point{ -1, -1 } ) { |
| 1064 | _prevWindowPos = { SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED }; |
| 1065 | } |
| 1066 | else { |
| 1067 | _prevWindowPos = pos; |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | private: |
| 1072 | SDL_Window * _window{ nullptr }; |
| 1073 | SDL_Surface * _surface{ nullptr }; |
| 1074 | SDL_Renderer * _renderer{ nullptr }; |
| 1075 | SDL_Texture * _texture{ nullptr }; |
| 1076 | int _driverIndex{ -1 }; |
| 1077 | |
| 1078 | std::string _previousWindowTitle; |
| 1079 | fheroes2::Point _prevWindowPos{ SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED }; |
| 1080 | fheroes2::Size _currentScreenResolution; |
| 1081 | fheroes2::Rect _activeWindowROI; |
| 1082 | |
| 1083 | fheroes2::Size _windowedSize; |
| 1084 | |
| 1085 | bool _isVSyncEnabled{ false }; |
| 1086 | |
| 1087 | RenderEngine() = default; |
| 1088 | |
| 1089 | void clear() override |
| 1090 | { |
| 1091 | if ( _texture != nullptr ) { |
| 1092 | SDL_DestroyTexture( _texture ); |
| 1093 | _texture = nullptr; |
| 1094 | } |
| 1095 | |
| 1096 | if ( _renderer != nullptr ) { |
| 1097 | SDL_DestroyRenderer( _renderer ); |
| 1098 | _renderer = nullptr; |
| 1099 | } |
| 1100 | |
| 1101 | if ( _window != nullptr ) { |
| 1102 | // Let's collect needed info about previous setup |
| 1103 | if ( !isFullScreen() ) { |
| 1104 | SDL_GetWindowPosition( _window, &_prevWindowPos.x, &_prevWindowPos.y ); |
| 1105 | } |
| 1106 | _previousWindowTitle = System::encUTF8ToLocal( SDL_GetWindowTitle( _window ) ); |
| 1107 | |
| 1108 | SDL_DestroyWindow( _window ); |
| 1109 | _window = nullptr; |
| 1110 | } |
| 1111 | |
| 1112 | if ( _surface != nullptr ) { |
| 1113 | SDL_FreeSurface( _surface ); |
| 1114 | _surface = nullptr; |
| 1115 | } |
| 1116 | |
| 1117 | _driverIndex = -1; |
| 1118 | _windowedSize = {}; |
| 1119 | } |
| 1120 | |
| 1121 | void render( const fheroes2::Display & display, const fheroes2::Rect & roi ) override |
| 1122 | { |
| 1123 | if ( _surface == nullptr ) { |
| 1124 | return; |
| 1125 | } |
| 1126 | |
| 1127 | assert( _renderer != nullptr && _texture != nullptr ); |
| 1128 | |
| 1129 | copyImageToSurface( display, _surface, roi ); |
| 1130 | |
| 1131 | const bool fullFrame = ( roi.width == display.width() ) && ( roi.height == display.height() ); |
| 1132 | if ( fullFrame ) { |
| 1133 | const int returnCode = SDL_UpdateTexture( _texture, nullptr, _surface->pixels, _surface->pitch ); |
| 1134 | if ( returnCode < 0 ) { |
| 1135 | ERROR_LOG( "Failed to update texture. The error value: " << returnCode << ", description: " << SDL_GetError() ) |
| 1136 | } |
| 1137 | } |
| 1138 | else { |
| 1139 | SDL_Rect area; |
| 1140 | area.x = roi.x; |
| 1141 | area.y = roi.y; |
| 1142 | area.w = roi.width; |
| 1143 | area.h = roi.height; |
| 1144 | |
| 1145 | const int returnCode = SDL_UpdateTexture( _texture, &area, _surface->pixels, _surface->pitch ); |
| 1146 | if ( returnCode < 0 ) { |
| 1147 | ERROR_LOG( "Failed to update texture. The error value: " << returnCode << ", description: " << SDL_GetError() ) |
| 1148 | } |
| 1149 | } |
| 1150 | |
| 1151 | int returnCode = SDL_RenderClear( _renderer ); |
| 1152 | if ( returnCode < 0 ) { |
| 1153 | ERROR_LOG( "Failed to clear renderer. The error value: " << returnCode << ", description: " << SDL_GetError() ) |
| 1154 | return; |
| 1155 | } |
| 1156 | |
| 1157 | returnCode = SDL_RenderCopy( _renderer, _texture, nullptr, nullptr ); |
| 1158 | if ( returnCode < 0 ) { |
| 1159 | ERROR_LOG( "Failed to copy render.The error value: " << returnCode << ", description: " << SDL_GetError() ) |
| 1160 | return; |
| 1161 | } |
| 1162 | |
| 1163 | SDL_RenderPresent( _renderer ); |
| 1164 | } |
| 1165 | |
| 1166 | bool allocate( fheroes2::ResolutionInfo & resolutionInfo, bool isFullScreen ) override |
| 1167 | { |
| 1168 | clear(); |
| 1169 | |
| 1170 | const std::vector<fheroes2::ResolutionInfo> resolutions = getAvailableResolutions(); |
| 1171 | assert( !resolutions.empty() ); |
| 1172 | if ( !resolutions.empty() ) { |
| 1173 | resolutionInfo = GetNearestResolution( resolutionInfo, resolutions ); |
| 1174 | } |
| 1175 | |
| 1176 | #if defined( ANDROID ) || defined( __IPHONEOS__ ) |
| 1177 | // Same as ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE |
| 1178 | if ( SDL_SetHint( SDL_HINT_ORIENTATIONS, "LandscapeLeft LandscapeRight" ) == SDL_FALSE ) { |
| 1179 | ERROR_LOG( "Failed to set the " SDL_HINT_ORIENTATIONS " hint." ) |
| 1180 | } |
| 1181 | #endif |
| 1182 | |
| 1183 | uint32_t flags = SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE; |
| 1184 | if ( isFullScreen ) { |
| 1185 | #if defined( _WIN32 ) |
| 1186 | // We force fullscreen at desktop resolution for nearest scaling to disable hardware scaling of game resolution by the monitor. |
| 1187 | if ( isNearestScaling() || fheroes2::cursor().isSoftwareEmulation() ) { |
| 1188 | flags |= SDL_WINDOW_FULLSCREEN_DESKTOP; |
| 1189 | } |
| 1190 | else { |
| 1191 | flags |= SDL_WINDOW_FULLSCREEN; |
| 1192 | } |
| 1193 | #else |
| 1194 | flags |= SDL_WINDOW_FULLSCREEN_DESKTOP; |
| 1195 | #endif |
| 1196 | } |
| 1197 | |
| 1198 | std::optional<bool> isInDisplay = isWindowInAnyDisplay( resolutionInfo, _prevWindowPos ); |
| 1199 | |
| 1200 | // check if the previous window position is within display or error occured |
| 1201 | if ( !isInDisplay || !*isInDisplay ) { |
| 1202 | // reset position if it is not within the bounds of any display |
| 1203 | _prevWindowPos = { SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED }; |
| 1204 | } |
| 1205 | |
| 1206 | _window = SDL_CreateWindow( System::encLocalToUTF8( _previousWindowTitle ).c_str(), _prevWindowPos.x, _prevWindowPos.y, resolutionInfo.screenWidth, |
| 1207 | resolutionInfo.screenHeight, flags ); |
| 1208 | if ( _window == nullptr ) { |
| 1209 | ERROR_LOG( "Failed to create an application window of " << resolutionInfo.screenWidth << " x " << resolutionInfo.screenHeight |
| 1210 | << " size. The error: " << SDL_GetError() ) |
| 1211 | clear(); |
| 1212 | return false; |
| 1213 | } |
| 1214 | |
| 1215 | _syncFullScreen(); |
| 1216 | |
| 1217 | bool isPaletteModeSupported = false; |
| 1218 | |
| 1219 | SDL_RendererInfo rendererInfo; |
| 1220 | _driverIndex = -1; |
| 1221 | |
| 1222 | if ( const int driverCount = SDL_GetNumRenderDrivers(); driverCount >= 0 ) { |
| 1223 | const uint32_t requiredFlags = SDL_RENDERER_ACCELERATED; |
| 1224 | |
| 1225 | for ( int driverId = 0; driverId < driverCount; ++driverId ) { |
| 1226 | const int returnCode = SDL_GetRenderDriverInfo( driverId, &rendererInfo ); |
| 1227 | if ( returnCode < 0 ) { |
| 1228 | ERROR_LOG( "Failed to get renderer driver info. The error value: " << returnCode << ", description: " << SDL_GetError() ) |
| 1229 | continue; |
| 1230 | } |
| 1231 | |
| 1232 | if ( ( rendererInfo.flags & requiredFlags ) != requiredFlags ) { |
| 1233 | continue; |
| 1234 | } |
| 1235 | |
| 1236 | for ( uint32_t i = 0; i < rendererInfo.num_texture_formats; ++i ) { |
| 1237 | if ( rendererInfo.texture_formats[i] == SDL_PIXELFORMAT_INDEX8 ) { |
| 1238 | // Bingo! This is the best driver and format. |
| 1239 | isPaletteModeSupported = true; |
| 1240 | _driverIndex = driverId; |
| 1241 | break; |
| 1242 | } |
| 1243 | } |
| 1244 | |
| 1245 | if ( isPaletteModeSupported ) { |
| 1246 | break; |
| 1247 | } |
| 1248 | |
| 1249 | if ( _driverIndex < 0 ) { |
| 1250 | _driverIndex = driverId; |
| 1251 | } |
| 1252 | } |
| 1253 | } |
| 1254 | else { |
| 1255 | ERROR_LOG( "Failed to get the number of render drivers. The error value: " << driverCount << ", description: " << SDL_GetError() ) |
| 1256 | } |
| 1257 | |
| 1258 | _surface = SDL_CreateRGBSurface( 0, resolutionInfo.gameWidth, resolutionInfo.gameHeight, isPaletteModeSupported ? 8 : 32, 0, 0, 0, 0 ); |
| 1259 | if ( _surface == nullptr ) { |
| 1260 | ERROR_LOG( "Failed to create a surface of " << resolutionInfo.gameWidth << " x " << resolutionInfo.gameHeight << " size. The error: " << SDL_GetError() ) |
| 1261 | clear(); |
| 1262 | return false; |
| 1263 | } |
| 1264 | |
| 1265 | if ( _surface->w <= 0 || _surface->h <= 0 || _surface->w != resolutionInfo.gameWidth || _surface->h != resolutionInfo.gameHeight ) { |
| 1266 | clear(); |
| 1267 | return false; |
| 1268 | } |
| 1269 | |
| 1270 | _createPalette(); |
| 1271 | |
| 1272 | _renderer = SDL_CreateRenderer( _window, _driverIndex, SDL_RENDERER_ACCELERATED ); |
| 1273 | if ( _renderer == nullptr ) { |
| 1274 | ERROR_LOG( "Failed to create a window renderer of " << resolutionInfo.gameWidth << " x " << resolutionInfo.gameHeight |
| 1275 | << " size. The error: " << SDL_GetError() ) |
| 1276 | clear(); |
| 1277 | return false; |
| 1278 | } |
| 1279 | |
| 1280 | if ( const int returnCode = SDL_SetRenderDrawColor( _renderer, 0, 0, 0, SDL_ALPHA_OPAQUE ); returnCode < 0 ) { |
| 1281 | ERROR_LOG( "Failed to set default color for renderer. The error value: " << returnCode << ", description: " << SDL_GetError() ) |
| 1282 | } |
| 1283 | |
| 1284 | if ( const int returnCode = SDL_SetRenderTarget( _renderer, nullptr ); returnCode < 0 ) { |
| 1285 | ERROR_LOG( "Failed to set render target to window. The error value: " << returnCode << ", description: " << SDL_GetError() ) |
| 1286 | } |
| 1287 | |
| 1288 | if ( SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, ( isNearestScaling() ? "nearest" : "linear" ) ) == SDL_FALSE ) { |
| 1289 | ERROR_LOG( "Failed to set the " SDL_HINT_RENDER_SCALE_QUALITY " hint." ) |
| 1290 | } |
| 1291 | |
| 1292 | // Setting this hint prevents the window to regain focus after losing it in fullscreen mode. |
| 1293 | // It also fixes issues when SDL_UpdateTexture() calls fail because of refocusing. |
| 1294 | if ( SDL_SetHint( SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "0" ) == SDL_FALSE ) { |
| 1295 | ERROR_LOG( "Failed to set the " SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS " hint." ) |
| 1296 | } |
| 1297 | |
| 1298 | if ( const int returnCode = SDL_RenderSetLogicalSize( _renderer, resolutionInfo.gameWidth, resolutionInfo.gameHeight ); returnCode < 0 ) { |
| 1299 | ERROR_LOG( "Failed to create logical size of " << resolutionInfo.gameWidth << " x " << resolutionInfo.gameHeight |
| 1300 | << " size. The error value: " << returnCode << ", description: " << SDL_GetError() ) |
| 1301 | clear(); |
| 1302 | return false; |
| 1303 | } |
| 1304 | |
| 1305 | _texture = SDL_CreateTextureFromSurface( _renderer, _surface ); |
| 1306 | if ( _texture == nullptr ) { |
| 1307 | ERROR_LOG( "Failed to create a texture from a surface of " << resolutionInfo.gameWidth << " x " << resolutionInfo.gameHeight |
| 1308 | << " size. The error: " << SDL_GetError() ) |
| 1309 | clear(); |
| 1310 | return false; |
| 1311 | } |
| 1312 | |
| 1313 | if ( !_retrieveWindowInfo() ) { |
| 1314 | clear(); |
| 1315 | return false; |
| 1316 | } |
| 1317 | |
| 1318 | _toggleMouseCaptureMode(); |
| 1319 | _toggleVSync(); |
| 1320 | |
| 1321 | return true; |
| 1322 | } |
| 1323 | |
| 1324 | void updatePalette( const std::vector<uint8_t> & colorIds ) override |
| 1325 | { |
| 1326 | if ( _surface == nullptr || colorIds.size() != 256 ) |
| 1327 | return; |
| 1328 | |
| 1329 | generatePalette( colorIds, _surface ); |
| 1330 | if ( _surface->format->BitsPerPixel == 8 ) { |
| 1331 | const int returnCode = SDL_SetPaletteColors( _surface->format->palette, _palette8Bit.data(), 0, 256 ); |
| 1332 | if ( returnCode < 0 ) { |
| 1333 | ERROR_LOG( "Failed to set palette color. The error value: " << returnCode << ", description: " << SDL_GetError() ) |
| 1334 | } |
| 1335 | } |
| 1336 | } |
| 1337 | |
| 1338 | bool isMouseCursorActive() const override |
| 1339 | { |
| 1340 | return ( _window != nullptr ) && ( ( SDL_GetWindowFlags( _window ) & SDL_WINDOW_MOUSE_FOCUS ) == SDL_WINDOW_MOUSE_FOCUS ); |
| 1341 | } |
| 1342 | |
| 1343 | void _createPalette() |
| 1344 | { |
| 1345 | if ( _surface == nullptr ) |
| 1346 | return; |
| 1347 | |
| 1348 | updatePalette( StandardPaletteIndexes() ); |
| 1349 | |
| 1350 | if ( _surface->format->BitsPerPixel == 8 ) { |
| 1351 | if ( !SDL_MUSTLOCK( _surface ) ) { |
| 1352 | // copy the image from display buffer to SDL surface |
| 1353 | const fheroes2::Display & display = fheroes2::Display::instance(); |
| 1354 | if ( _surface->w == display.width() && _surface->h == display.height() ) { |
| 1355 | memcpy( _surface->pixels, display.image(), static_cast<size_t>( display.width() * display.height() ) ); |
| 1356 | } |
| 1357 | |
| 1358 | // Display class doesn't have support for image pitch so we mustn't link display to surface if width is not divisible by 4. |
| 1359 | if ( _surface->w % 4 == 0 ) { |
| 1360 | linkRenderSurface( static_cast<uint8_t *>( _surface->pixels ) ); |
| 1361 | } |
| 1362 | } |
| 1363 | } |
| 1364 | } |
| 1365 | |
| 1366 | bool _retrieveWindowInfo() |
| 1367 | { |
| 1368 | assert( _window != nullptr ); |
| 1369 | |
| 1370 | const int displayIndex = SDL_GetWindowDisplayIndex( _window ); |
| 1371 | if ( displayIndex < 0 ) { |
| 1372 | ERROR_LOG( "Failed to get window display index. The error value: " << displayIndex << ", description: " << SDL_GetError() ) |
| 1373 | return false; |
| 1374 | } |
| 1375 | |
| 1376 | SDL_DisplayMode displayMode; |
| 1377 | |
| 1378 | const int returnCode = SDL_GetCurrentDisplayMode( displayIndex, &displayMode ); |
| 1379 | if ( returnCode < 0 ) { |
| 1380 | ERROR_LOG( "Failed to retrieve current display mode. The error value: " << returnCode << ", description: " << SDL_GetError() ) |
| 1381 | return false; |
| 1382 | } |
| 1383 | |
| 1384 | _currentScreenResolution.width = displayMode.w; |
| 1385 | _currentScreenResolution.height = displayMode.h; |
| 1386 | |
| 1387 | #if defined( TARGET_NINTENDO_SWITCH ) |
| 1388 | // On a Nintendo Switch the game is always fullscreen |
| 1389 | _activeWindowROI = { 0, 0, _currentScreenResolution.width, _currentScreenResolution.height }; |
| 1390 | #else |
| 1391 | SDL_GetWindowPosition( _window, &_activeWindowROI.x, &_activeWindowROI.y ); |
| 1392 | SDL_GetWindowSize( _window, &_activeWindowROI.width, &_activeWindowROI.height ); |
| 1393 | #endif |
| 1394 | |
| 1395 | return true; |
| 1396 | } |
| 1397 | |
| 1398 | void _toggleVSync() |
| 1399 | { |
| 1400 | assert( _renderer != nullptr ); |
| 1401 | |
| 1402 | #if !SDL_VERSION_ATLEAST( 2, 0, 18 ) |
| 1403 | #error SDL_RenderSetVSync() is only supported since SDL 2.0.18 |
| 1404 | #endif |
| 1405 | |
| 1406 | if ( const int returnCode = SDL_RenderSetVSync( _renderer, _isVSyncEnabled ? SDL_ENABLE : SDL_DISABLE ); returnCode != 0 ) { |
| 1407 | ERROR_LOG( "Failed to " << ( _isVSyncEnabled ? "enable" : "disable" ) << " vsync mode for renderer. The error value: " << returnCode |
| 1408 | << ", description: " << SDL_GetError() ) |
| 1409 | } |
| 1410 | } |
| 1411 | |
| 1412 | void _toggleMouseCaptureMode() |
| 1413 | { |
| 1414 | assert( _window != nullptr ); |
| 1415 | |
| 1416 | // To properly support fullscreen mode on devices with multiple displays or devices with notch, |
| 1417 | // it is important to lock the mouse in the application window area. |
| 1418 | SDL_SetWindowGrab( _window, isFullScreen() ? SDL_TRUE : SDL_FALSE ); |
| 1419 | } |
| 1420 | |
| 1421 | void _syncFullScreen() |
| 1422 | { |
| 1423 | if ( isFullScreen() == BaseRenderEngine::isFullScreen() ) { |
| 1424 | return; |
| 1425 | } |
| 1426 | |
| 1427 | BaseRenderEngine::toggleFullScreen(); |
| 1428 | |
| 1429 | assert( isFullScreen() == BaseRenderEngine::isFullScreen() ); |
| 1430 | } |
| 1431 | }; |
| 1432 | #endif |
| 1433 | } |
| 1434 | |
| 1435 | namespace fheroes2 |
| 1436 | { |
| 1437 | void BaseRenderEngine::linkRenderSurface( uint8_t * surface ) const |
| 1438 | { |
| 1439 | Display::instance().linkRenderSurface( surface ); |
| 1440 | } |
| 1441 | |
| 1442 | Display::Display() |
| 1443 | : _engine( RenderEngine::create() ) |
| 1444 | , _cursor( RenderCursor::create() ) |
| 1445 | { |
| 1446 | _disableTransformLayer(); |
| 1447 | } |
| 1448 | |
| 1449 | void Display::resize( int32_t width_, int32_t height_ ) |
| 1450 | { |
| 1451 | assert( width_ == width() && height_ == height() ); |
| 1452 | |
| 1453 | #ifdef NDEBUG |
| 1454 | (void)width_; |
| 1455 | (void)height_; |
| 1456 | #endif |
| 1457 | } |
| 1458 | |
| 1459 | void Display::setResolution( ResolutionInfo info ) |
| 1460 | { |
| 1461 | if ( width() > 0 && height() > 0 && info.gameWidth == width() && info.gameHeight == height() && info.screenWidth == _screenSize.width |
| 1462 | && info.screenHeight == _screenSize.height ) // nothing to resize |
| 1463 | return; |
| 1464 | |
| 1465 | const bool isFullScreen = _engine->isFullScreen(); |
| 1466 | |
| 1467 | // deallocate engine resources |
| 1468 | _engine->clear(); |
| 1469 | |
| 1470 | _prevRoi = {}; |
| 1471 | |
| 1472 | // allocate engine resources |
| 1473 | if ( !_engine->allocate( info, isFullScreen ) ) { |
| 1474 | clear(); |
| 1475 | } |
| 1476 | |
| 1477 | Image::resize( info.gameWidth, info.gameHeight ); |
| 1478 | Image::reset(); |
| 1479 | |
| 1480 | _screenSize = { info.screenWidth, info.screenHeight }; |
| 1481 | } |
| 1482 | |
| 1483 | void Display::resetRenderer() |
| 1484 | { |
| 1485 | const bool isFullScreen = _engine->isFullScreen(); |
| 1486 | |
| 1487 | // deallocate engine resources |
| 1488 | _engine->clear(); |
| 1489 | |
| 1490 | _prevRoi = {}; |
| 1491 | |
| 1492 | ResolutionInfo res( width(), height(), _screenSize.width, _screenSize.height ); |
| 1493 | |
| 1494 | // allocate engine resources |
| 1495 | if ( !_engine->allocate( res, isFullScreen ) ) { |
| 1496 | clear(); |
| 1497 | } |
| 1498 | } |
| 1499 | |
| 1500 | void Display::setWindowPos( const Point point ) |
| 1501 | { |
| 1502 | _engine->setWindowPos( point ); |
| 1503 | } |
| 1504 | |
| 1505 | Display & Display::instance() |
| 1506 | { |
| 1507 | static Display display; |
| 1508 | return display; |
| 1509 | } |
| 1510 | |
| 1511 | void Display::render( const Rect & roi ) |
| 1512 | { |
| 1513 | Rect temp( roi ); |
| 1514 | if ( !getActiveArea( temp, width(), height() ) ) { |
| 1515 | return; |
| 1516 | } |
| 1517 | |
| 1518 | if ( _cursor->isVisible() && _cursor->isSoftwareEmulation() && !_cursor->_image.empty() ) { |
| 1519 | const Sprite & cursorImage = _cursor->_image; |
| 1520 | Rect cursorROI( cursorImage.x(), cursorImage.y(), cursorImage.width(), cursorImage.height() ); |
| 1521 | |
| 1522 | if ( _cursor->_keepInScreenArea ) { |
| 1523 | cursorROI.x = std::clamp( cursorROI.x, 0, width() - cursorROI.width ); |
| 1524 | cursorROI.y = std::clamp( cursorROI.y, 0, height() - cursorROI.height ); |
| 1525 | } |
| 1526 | |
| 1527 | const Sprite backup = Crop( *this, cursorROI.x, cursorROI.y, cursorROI.width, cursorROI.height ); |
| 1528 | Blit( cursorImage, 0, 0, *this, cursorROI.x, cursorROI.y, cursorROI.width, cursorROI.height ); |
| 1529 | |
| 1530 | // ROI must include cursor's area as well, otherwise cursor won't be rendered. |
| 1531 | if ( !backup.empty() && getActiveArea( cursorROI, width(), height() ) ) { |
| 1532 | temp = getBoundaryRect( temp, cursorROI ); |
| 1533 | } |
| 1534 | |
| 1535 | _renderFrame( temp ); |
| 1536 | |
| 1537 | if ( _postprocessing ) { |
| 1538 | _postprocessing(); |
| 1539 | } |
| 1540 | |
| 1541 | Copy( backup, 0, 0, *this, backup.x(), backup.y(), backup.width(), backup.height() ); |
| 1542 | } |
| 1543 | else { |
| 1544 | _renderFrame( temp ); |
| 1545 | |
| 1546 | if ( _postprocessing ) { |
| 1547 | _postprocessing(); |
| 1548 | } |
| 1549 | } |
| 1550 | |
| 1551 | _prevRoi = temp; |
| 1552 | } |
| 1553 | |
| 1554 | void Display::updateNextRenderRoi( const Rect & roi ) |
| 1555 | { |
| 1556 | _prevRoi = getBoundaryRect( _prevRoi, roi ); |
| 1557 | getActiveArea( _prevRoi, width(), height() ); |
| 1558 | } |
| 1559 | |
| 1560 | void Display::_renderFrame( const Rect & roi ) const |
| 1561 | { |
| 1562 | bool updateImage = true; |
| 1563 | if ( _preprocessing ) { |
| 1564 | std::vector<uint8_t> palette; |
| 1565 | if ( _preprocessing( palette ) ) { |
| 1566 | _engine->updatePalette( palette ); |
| 1567 | // when we change a palette for 8-bit image we unwillingly call render so we don't need to re-render the same frame again |
| 1568 | updateImage = ( _renderSurface == nullptr ); |
| 1569 | if ( updateImage ) { |
| 1570 | // Pre-processing step is applied to the whole image so we forcefully render the full frame. |
| 1571 | _engine->render( *this, { 0, 0, width(), height() } ); |
| 1572 | return; |
| 1573 | } |
| 1574 | } |
| 1575 | } |
| 1576 | |
| 1577 | if ( updateImage ) { |
| 1578 | // Make sure that we update the previously rendered area to avoid any ghost effect artefacts. |
| 1579 | _engine->render( *this, getBoundaryRect( roi, _prevRoi ) ); |
| 1580 | } |
| 1581 | } |
| 1582 | |
| 1583 | uint8_t * Display::image() |
| 1584 | { |
| 1585 | return _renderSurface != nullptr ? _renderSurface : Image::image(); |
| 1586 | } |
| 1587 | |
| 1588 | const uint8_t * Display::image() const |
| 1589 | { |
| 1590 | return _renderSurface != nullptr ? _renderSurface : Image::image(); |
| 1591 | } |
| 1592 | |
| 1593 | void Display::release() |
| 1594 | { |
| 1595 | _engine->clear(); |
| 1596 | _cursor.reset(); |
| 1597 | clear(); |
| 1598 | |
| 1599 | _prevRoi = {}; |
| 1600 | } |
| 1601 | |
| 1602 | void Display::changePalette( const uint8_t * palette, const bool forceDefaultPaletteUpdate ) const |
| 1603 | { |
| 1604 | if ( currentPalette == palette || ( palette == nullptr && currentPalette == PALPalette() && !forceDefaultPaletteUpdate ) ) |
| 1605 | return; |
| 1606 | |
| 1607 | currentPalette = ( palette == nullptr ) ? PALPalette( forceDefaultPaletteUpdate ) : palette; |
| 1608 | |
| 1609 | _engine->updatePalette( StandardPaletteIndexes() ); |
| 1610 | } |
| 1611 | |
| 1612 | bool Cursor::isFocusActive() |
| 1613 | { |
| 1614 | return engine().isMouseCursorActive(); |
| 1615 | } |
| 1616 | |
| 1617 | BaseRenderEngine & engine() |
| 1618 | { |
| 1619 | const fheroes2::Display & display = Display::instance(); |
| 1620 | |
| 1621 | assert( display._engine ); |
| 1622 | |
| 1623 | return *( display._engine ); |
| 1624 | } |
| 1625 | |
| 1626 | Cursor & cursor() |
| 1627 | { |
| 1628 | const fheroes2::Display & display = Display::instance(); |
| 1629 | |
| 1630 | assert( display._cursor ); |
| 1631 | |
| 1632 | return *( display._cursor ); |
| 1633 | } |
| 1634 | } |