Line data Source code
1 : /* Copyright (C) 2025 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 : 23 : #include <cstdint> 24 : #include <memory> 25 : #include <vector> 26 : 27 : class CRect; 28 : class CTextRenderer; 29 : class CVector2D; 30 : namespace Renderer::Backend { class IDeviceCommandContext; } 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 : * 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 : 104 : class ScopedScissor 105 : { 106 : public: 107 0 : ScopedScissor(CCanvas2D& canvas, const CRect& scissor) 108 0 : : m_Canvas(canvas) 109 : { 110 0 : m_Canvas.PushScissor(scissor); 111 : } 112 : 113 0 : ~ScopedScissor() 114 : { 115 0 : m_Canvas.PopScissor(); 116 0 : } 117 : 118 : private: 119 : CCanvas2D& m_Canvas; 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 : 128 : private: 129 : class Impl; 130 : std::unique_ptr<Impl> m; 131 : }; 132 : 133 : #endif // INCLUDED_CANVAS2D