Canvas.h 4.2 KB

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