Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
Canvas2D.h
Go to the documentation of this file.
1/* Copyright (C) 2023 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
28class CRect;
29class CTextRenderer;
30
31struct CColor;
32
33// Encapsulates 2D drawing functionality to hide and optimize
34// low level API calls.
36{
37public:
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;
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 */
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 * Adds the scissor rect to a scissor stack. The only top scissor is
94 * applied. It's recommended to use as few nested scissors as possible.
95 */
96 void PushScissor(const CRect& scissor);
97
98 /**
99 * Removes the top scissor rect from a scissor stack. The stack must not be
100 * empty.
101 */
102 void PopScissor();
103
105 {
106 public:
107 ScopedScissor(CCanvas2D& canvas, const CRect& scissor)
108 : m_Canvas(canvas)
109 {
110 m_Canvas.PushScissor(scissor);
111 }
112
114 {
116 }
117
118 private:
120 };
121
122 /**
123 * Unbinds all bound resources and clears caches. Frequent calls might
124 * affect performance. Useful to call a custom rendering code.
125 */
126 void Flush();
127
128private:
129 class Impl;
130 std::unique_ptr<Impl> m;
131};
132
133#endif // INCLUDED_CANVAS2D
Definition: Canvas2D.cpp:82
Definition: Canvas2D.h:105
CCanvas2D & m_Canvas
Definition: Canvas2D.h:119
~ScopedScissor()
Definition: Canvas2D.h:113
ScopedScissor(CCanvas2D &canvas, const CRect &scissor)
Definition: Canvas2D.h:107
Definition: Canvas2D.h:36
CCanvas2D & operator=(const CCanvas2D &)=delete
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:392
CCanvas2D(const uint32_t widthInPixels, const uint32_t heightInPixels, const float scale, Renderer::Backend::IDeviceCommandContext *deviceCommandContext)
Definition: Canvas2D.cpp:197
CCanvas2D(CCanvas2D &&)=delete
CCanvas2D(const CCanvas2D &)=delete
void DrawText(CTextRenderer &textRenderer)
Draws a text using canvas materials.
Definition: Canvas2D.cpp:460
std::unique_ptr< Impl > m
Definition: Canvas2D.h:130
void Flush()
Unbinds all bound resources and clears caches.
Definition: Canvas2D.cpp:485
~CCanvas2D()
Definition: Canvas2D.cpp:205
void PushScissor(const CRect &scissor)
Adds the scissor rect to a scissor stack.
Definition: Canvas2D.cpp:471
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:211
CCanvas2D & operator=(CCanvas2D &&)=delete
void DrawRect(const CRect &rect, const CColor &color)
Draws the rect filled with the color.
Definition: Canvas2D.cpp:360
void PopScissor()
Removes the top scissor rect from a scissor stack.
Definition: Canvas2D.cpp:478
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:421
Rectangle class used for screen rectangles.
Definition: Rect.h:31
Definition: TextRenderer.h:34
Definition: Vector2D.h:32
Definition: IDeviceCommandContext.h:42
std::shared_ptr< CTexture > CTexturePtr
Definition: Texture.h:23
Definition: Color.h:43
unsigned int uint32_t
Definition: wposix_types.h:53