Pyrogenesis  trunk
Canvas2D.h
Go to the documentation of this file.
1 /* Copyright (C) 2022 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef INCLUDED_CANVAS2D
19 #define INCLUDED_CANVAS2D
20 
21 #include "graphics/Texture.h"
22 #include "maths/Vector2D.h"
24 
25 #include <memory>
26 #include <vector>
27 
28 class CRect;
29 class CTextRenderer;
30 
31 struct CColor;
32 
33 // Encapsulates 2D drawing functionality to hide and optimize
34 // low level API calls.
35 class CCanvas2D
36 {
37 public:
38  CCanvas2D(
39  const uint32_t widthInPixels, const uint32_t heightInPixels, const float scale,
40  Renderer::Backend::IDeviceCommandContext* deviceCommandContext);
41  ~CCanvas2D();
42 
43  CCanvas2D(const CCanvas2D&) = delete;
44  CCanvas2D& operator=(const CCanvas2D&) = delete;
45  CCanvas2D(CCanvas2D&&) = delete;
46  CCanvas2D& operator=(CCanvas2D&&) = delete;
47 
48  /**
49  * Draws a line by the given points with the width and color.
50  */
51  void DrawLine(
52  const std::vector<CVector2D>& points,
53  const float width, const CColor& color);
54 
55  /**
56  * Draws the rect filled with the color.
57  */
58  void DrawRect(const CRect& rect, const CColor& color);
59 
60  /**
61  * Draws a piece of the texture from the source rect into the
62  * destination rect. The result color is set by the following formula:
63  * TEXTURE_COLOR * COLOR_MULTIPLY + COLOR_ADD
64  * The texture color is blended with its own grayscale version according to
65  * the grayscale factor.
66  */
67  void DrawTexture(
68  const CTexturePtr& texture, const CRect& destination, const CRect& source,
69  const CColor& multiply, const CColor& add, const float grayscaleFactor);
70 
71  /**
72  * A simpler version of the previous one, draws the texture into the
73  * destination rect without color modifications.
74  */
75  void DrawTexture(const CTexturePtr& texture, const CRect& destination);
76 
77  /**
78  * A similar to the original one, draws the texture into the
79  * destination rect but rotates it first around the origin point by angle
80  * radians (a positive angle denotes a clockwise rotation).
81  */
82  void DrawRotatedTexture(
83  const CTexturePtr& texture, const CRect& destination, const CRect& source,
84  const CColor& multiply, const CColor& add, const float grayscaleFactor,
85  const CVector2D& origin, const float angle);
86 
87  /**
88  * Draws a text using canvas materials.
89  */
90  void DrawText(CTextRenderer& textRenderer);
91 
92  /**
93  * Unbinds all binded resources and clears caches. Frequent calls might
94  * affect performance. Useful to call a custom rendering code.
95  */
96  void Flush();
97 
98 private:
99  class Impl;
100  std::unique_ptr<Impl> m;
101 };
102 
103 #endif // INCLUDED_CANVAS2D
Definition: Canvas2D.cpp:80
std::unique_ptr< Impl > m
Definition: Canvas2D.h:99
void DrawRotatedTexture(const CTexturePtr &texture, const CRect &destination, const CRect &source, const CColor &multiply, const CColor &add, const float grayscaleFactor, const CVector2D &origin, const float angle)
A similar to the original one, draws the texture into the destination rect but rotates it first aroun...
Definition: Canvas2D.cpp:401
Definition: Color.h:42
void Flush()
Unbinds all binded resources and clears caches.
Definition: Canvas2D.cpp:451
Definition: TextRenderer.h:33
void DrawRect(const CRect &rect, const CColor &color)
Draws the rect filled with the color.
Definition: Canvas2D.cpp:340
void DrawText(CTextRenderer &textRenderer)
Draws a text using canvas materials.
Definition: Canvas2D.cpp:440
Definition: Canvas2D.h:35
void DrawTexture(const CTexturePtr &texture, const CRect &destination, const CRect &source, const CColor &multiply, const CColor &add, const float grayscaleFactor)
Draws a piece of the texture from the source rect into the destination rect.
Definition: Canvas2D.cpp:372
Definition: Vector2D.h:31
CCanvas2D & operator=(const CCanvas2D &)=delete
unsigned int uint32_t
Definition: wposix_types.h:53
CCanvas2D(const uint32_t widthInPixels, const uint32_t heightInPixels, const float scale, Renderer::Backend::IDeviceCommandContext *deviceCommandContext)
Definition: Canvas2D.cpp:178
Definition: IDeviceCommandContext.h:40
std::shared_ptr< CTexture > CTexturePtr
Definition: Texture.h:22
~CCanvas2D()
Definition: Canvas2D.cpp:186
void DrawLine(const std::vector< CVector2D > &points, const float width, const CColor &color)
Draws a line by the given points with the width and color.
Definition: Canvas2D.cpp:191
Rectangle class used for screen rectangles.
Definition: Rect.h:30