ScreenHandler.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * ScreenHandler.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "../../lib/Point.h"
  12. #include "../render/IScreenHandler.h"
  13. struct SDL_Texture;
  14. struct SDL_Window;
  15. struct SDL_Renderer;
  16. struct SDL_Surface;
  17. enum class EWindowMode
  18. {
  19. // game runs in a window that covers part of the screen
  20. WINDOWED,
  21. // game runs in a 'window' that always covers entire screen and uses unmodified desktop resolution
  22. // The only mode that is available on mobile devices
  23. FULLSCREEN_BORDERLESS_WINDOWED,
  24. // game runs in a fullscreen mode with resolution selected by player
  25. FULLSCREEN_EXCLUSIVE
  26. };
  27. enum class EUpscalingFilter
  28. {
  29. AUTO, // used only for loading from config, replaced with autoselected value on init
  30. NONE,
  31. //BILINEAR, // TODO?
  32. //BICUBIC, // TODO?
  33. XBRZ_2,
  34. XBRZ_3,
  35. XBRZ_4,
  36. // NOTE: xbrz also provides x5 and x6 filters, but those would require high-end gaming PC's due to huge memory usage with no visible gain
  37. };
  38. /// This class is responsible for management of game window and its main rendering surface
  39. class ScreenHandler final : public IScreenHandler
  40. {
  41. SDL_Window * mainWindow = nullptr;
  42. SDL_Texture * screenTexture = nullptr;
  43. SDL_Surface * screen = nullptr;
  44. EUpscalingFilter upscalingFilter = EUpscalingFilter::AUTO;
  45. /// Dimensions of target surfaces/textures, this value is what game logic views as screen size
  46. Point getPreferredLogicalResolution() const;
  47. /// Dimensions of output window, if different from logicalResolution SDL will perform scaling
  48. /// This value is what player views as window size
  49. Point getPreferredWindowResolution() const;
  50. EWindowMode getPreferredWindowMode() const;
  51. /// Returns index of display on which window should be created
  52. int getPreferredDisplayIndex() const;
  53. /// Returns index of rendering driver preferred by player or -1 if no preference
  54. int getPreferredRenderingDriver() const;
  55. /// Creates SDL window with specified parameters
  56. SDL_Window * createWindowImpl(Point dimensions, int flags, bool center);
  57. /// Creates SDL window using OS-specific settings & user-specific config
  58. SDL_Window * createWindow();
  59. /// Manages window and SDL renderer
  60. void initializeWindow();
  61. void destroyWindow();
  62. /// Manages surfaces & textures used for
  63. void initializeScreenBuffers();
  64. void destroyScreenBuffers();
  65. /// Updates state (e.g. position) of game window after resolution/fullscreen change
  66. void updateWindowState();
  67. /// Initializes or reiniitalizes all screen state
  68. void recreateWindowAndScreenBuffers();
  69. /// Performs validation of settings and updates them to valid values if necessary
  70. void validateSettings();
  71. EUpscalingFilter loadUpscalingFilter() const;
  72. void selectDownscalingFilter();
  73. void selectUpscalingFilter();
  74. public:
  75. /// Creates and initializes screen, window and SDL state
  76. ScreenHandler();
  77. ~ScreenHandler();
  78. /// Updates and potentially recreates target screen to match selected fullscreen status
  79. void onScreenResize() final;
  80. /// Fills screen with black color, erasing any existing content
  81. void clearScreen() final;
  82. /// Dimensions of render output, usually same as window size except for high-DPI screens on macOS / iOS
  83. Point getRenderResolution() const final;
  84. /// Window has focus
  85. bool hasFocus() final;
  86. Point getLogicalResolution() const final;
  87. int getScalingFactor() const final;
  88. int getInterfaceScalingPercentage() const final;
  89. Canvas getScreenCanvas() const final;
  90. void updateScreenTexture() final;
  91. void presentScreenTexture() final;
  92. std::vector<Point> getSupportedResolutions() const final;
  93. std::vector<Point> getSupportedResolutions(int displayIndex) const;
  94. std::tuple<int, int> getSupportedScalingRange() const final;
  95. Rect convertLogicalPointsToWindow(const Rect & input) const final;
  96. };