ScreenHandler.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. ColorScheme colorScheme = ColorScheme::NONE;
  46. /// Dimensions of target surfaces/textures, this value is what game logic views as screen size
  47. Point getPreferredLogicalResolution() const;
  48. /// Dimensions of output window, if different from logicalResolution SDL will perform scaling
  49. /// This value is what player views as window size
  50. Point getPreferredWindowResolution() const;
  51. EWindowMode getPreferredWindowMode() const;
  52. /// Returns index of display on which window should be created
  53. int getPreferredDisplayIndex() const;
  54. /// Returns index of rendering driver preferred by player or -1 if no preference
  55. int getPreferredRenderingDriver() const;
  56. /// Creates SDL window with specified parameters
  57. SDL_Window * createWindowImpl(Point dimensions, int flags, bool center);
  58. /// Creates SDL window using OS-specific settings & user-specific config
  59. SDL_Window * createWindow();
  60. /// Manages window and SDL renderer
  61. void initializeWindow();
  62. void destroyWindow();
  63. /// Manages surfaces & textures used for
  64. void initializeScreenBuffers();
  65. void destroyScreenBuffers();
  66. /// Updates state (e.g. position) of game window after resolution/fullscreen change
  67. void updateWindowState();
  68. /// Initializes or reiniitalizes all screen state
  69. void recreateWindowAndScreenBuffers();
  70. /// Performs validation of settings and updates them to valid values if necessary
  71. void validateSettings();
  72. EUpscalingFilter loadUpscalingFilter() const;
  73. void selectDownscalingFilter();
  74. void selectUpscalingFilter();
  75. public:
  76. /// Creates and initializes screen, window and SDL state
  77. ScreenHandler();
  78. ~ScreenHandler();
  79. /// Updates and potentially recreates target screen to match selected fullscreen status
  80. bool onScreenResize(bool keepWindowResolution) final;
  81. /// Fills screen with black color, erasing any existing content
  82. void clearScreen() final;
  83. /// Dimensions of render output, usually same as window size except for high-DPI screens on macOS / iOS
  84. Point getRenderResolution() const final;
  85. /// Window has focus
  86. bool hasFocus() final;
  87. Point getLogicalResolution() const final;
  88. int getScalingFactor() const final;
  89. int getInterfaceScalingPercentage() const final;
  90. Canvas getScreenCanvas() const final;
  91. void updateScreenTexture() final;
  92. void presentScreenTexture() final;
  93. std::vector<Point> getSupportedResolutions() const final;
  94. std::vector<Point> getSupportedResolutions(int displayIndex) const;
  95. std::tuple<int, int> getSupportedScalingRange() const final;
  96. Rect convertLogicalPointsToWindow(const Rect & input) const final;
  97. void setColorScheme(ColorScheme filter) final;
  98. };