Canvas.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Canvas.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 "../gui/TextAlignment.h"
  12. #include "../../lib/Rect.h"
  13. #include "../../lib/Color.h"
  14. struct SDL_Surface;
  15. class IImage;
  16. class IVideoInstance;
  17. enum EFonts : int8_t;
  18. enum class CanvasScalingPolicy
  19. {
  20. AUTO, // automatically scale canvas operations by global scaling factor
  21. IGNORE // disable any scaling processing. Scaling factor will be set to 1
  22. };
  23. /// Class that represents surface for drawing on
  24. class Canvas
  25. {
  26. friend class CanvasClipRectGuard;
  27. /// Upscaler awareness. Must be first member for initialization
  28. CanvasScalingPolicy scalingPolicy;
  29. /// Target surface
  30. SDL_Surface * surface;
  31. /// Current rendering area, all rendering operations will be moved into selected area
  32. Rect renderArea;
  33. /// constructs canvas using existing surface. Caller maintains ownership on the surface
  34. explicit Canvas(SDL_Surface * surface, CanvasScalingPolicy scalingPolicy);
  35. /// copy constructor
  36. Canvas(const Canvas & other);
  37. Point transformPos(const Point & input);
  38. Point transformSize(const Point & input);
  39. public:
  40. Canvas & operator = (const Canvas & other) = delete;
  41. Canvas & operator = (Canvas && other) = delete;
  42. /// move constructor
  43. Canvas(Canvas && other);
  44. /// creates canvas that only covers specified subsection of a surface
  45. Canvas(const Canvas & other, const Rect & clipRect);
  46. /// constructs canvas of specified size
  47. explicit Canvas(const Point & size, CanvasScalingPolicy scalingPolicy);
  48. /// constructs canvas using existing surface. Caller maintains ownership on the surface
  49. /// Compatibility method. AVOID USAGE. To be removed once SDL abstraction layer is finished.
  50. static Canvas createFromSurface(SDL_Surface * surface, CanvasScalingPolicy scalingPolicy);
  51. ~Canvas();
  52. /// if set to true, drawing this canvas onto another canvas will use alpha channel information
  53. void applyTransparency(bool on);
  54. /// applies grayscale filter onto current image
  55. void applyGrayscale();
  56. /// renders image onto this canvas at specified position
  57. void draw(const std::shared_ptr<IImage>& image, const Point & pos);
  58. void draw(const IImage& image, const Point & pos);
  59. void draw(IVideoInstance & video, const Point & pos);
  60. /// renders section of image bounded by sourceRect at specified position
  61. void draw(const std::shared_ptr<IImage>& image, const Point & pos, const Rect & sourceRect);
  62. /// renders another canvas onto this canvas
  63. void draw(const Canvas &image, const Point & pos);
  64. /// renders another canvas onto this canvas with transparency
  65. void drawTransparent(const Canvas & image, const Point & pos, double transparency);
  66. /// renders another canvas onto this canvas with scaling
  67. void drawScaled(const Canvas &image, const Point & pos, const Point & targetSize);
  68. /// renders single pixels with specified color
  69. void drawPoint(const Point & dest, const ColorRGBA & color);
  70. /// renders continuous, 1-pixel wide line with color gradient
  71. void drawLine(const Point & from, const Point & dest, const ColorRGBA & colorFrom, const ColorRGBA & colorDest);
  72. /// renders rectangular, solid-color border in specified location
  73. void drawBorder(const Rect & target, const ColorRGBA & color, int width = 1);
  74. /// renders rectangular, dashed border in specified location
  75. void drawBorderDashed(const Rect & target, const ColorRGBA & color);
  76. /// renders single line of text with specified parameters
  77. void drawText(const Point & position, const EFonts & font, const ColorRGBA & colorDest, ETextAlignment alignment, const std::string & text );
  78. /// renders multiple lines of text with specified parameters
  79. void drawText(const Point & position, const EFonts & font, const ColorRGBA & colorDest, ETextAlignment alignment, const std::vector<std::string> & text );
  80. /// fills selected area with solid color
  81. void drawColor(const Rect & target, const ColorRGBA & color);
  82. /// fills selected area with blended color
  83. void drawColorBlended(const Rect & target, const ColorRGBA & color);
  84. /// fills canvas with texture
  85. void fillTexture(const std::shared_ptr<IImage>& image);
  86. int getScalingFactor() const;
  87. /// get the render area
  88. Rect getRenderArea() const;
  89. /// get pixel color
  90. ColorRGBA getPixel(const Point & position) const;
  91. };
  92. class CanvasClipRectGuard : boost::noncopyable
  93. {
  94. SDL_Surface * surf;
  95. Rect oldRect;
  96. public:
  97. CanvasClipRectGuard(Canvas & canvas, const Rect & rect);
  98. ~CanvasClipRectGuard();
  99. };