| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2.1 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library 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 GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with this library; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 |
| 19 | #include "vpainter.h" |
| 20 | #include <algorithm> |
| 21 | #include "vdrawhelper.h" |
| 22 |
| 23 | V_BEGIN_NAMESPACE |
| 24 |
| 25 | class VPainterImpl { |
| 26 | public: |
| 27 | void drawRle(const VPoint &pos, const VRle &rle); |
| 28 | void drawRle(const VRle &rle, const VRle &clip); |
| 29 | void setCompositionMode(VPainter::CompositionMode mode) |
| 30 | { |
| 31 | mSpanData.mCompositionMode = mode; |
| 32 | } |
| 33 | void drawBitmapUntransform(const VRect &target, const VBitmap &bitmap, |
| 34 | const VRect &source, uint8_t const_alpha); |
| 35 |
| 36 | public: |
| 37 | VRasterBuffer mBuffer; |
| 38 | VSpanData mSpanData; |
| 39 | }; |
| 40 |
| 41 | void VPainterImpl::drawRle(const VPoint &, const VRle &rle) |
| 42 | { |
| 43 | if (rle.empty()) return; |
| 44 | // mSpanData.updateSpanFunc(); |
| 45 |
| 46 | if (!mSpanData.mUnclippedBlendFunc) return; |
| 47 |
| 48 | // do draw after applying clip. |
| 49 | rle.intersect(mSpanData.clipRect(), mSpanData.mUnclippedBlendFunc, |
| 50 | &mSpanData); |
| 51 | } |
| 52 |
| 53 | void VPainterImpl::drawRle(const VRle &rle, const VRle &clip) |
| 54 | { |
| 55 | if (rle.empty() || clip.empty()) return; |
| 56 |
| 57 | if (!mSpanData.mUnclippedBlendFunc) return; |
| 58 |
| 59 | rle.intersect(clip, mSpanData.mUnclippedBlendFunc, &mSpanData); |
| 60 | } |
| 61 |
| 62 | static void fillRect(const VRect &r, VSpanData *data) |
| 63 | { |
| 64 | int x1, x2, y1, y2; |
| 65 |
| 66 | x1 = std::max(r.x(), 0); |
| 67 | x2 = std::min(r.x() + r.width(), data->mDrawableSize.width()); |
| 68 | y1 = std::max(r.y(), 0); |
| 69 | y2 = std::min(r.y() + r.height(), data->mDrawableSize.height()); |
| 70 |
| 71 | if (x2 <= x1 || y2 <= y1) return; |
| 72 |
| 73 | const int nspans = 256; |
| 74 | VRle::Span spans[nspans]; |
| 75 |
| 76 | int y = y1; |
| 77 | while (y < y2) { |
| 78 | int n = std::min(nspans, y2 - y); |
| 79 | int i = 0; |
| 80 | while (i < n) { |
| 81 | spans[i].x = x1; |
| 82 | spans[i].len = x2 - x1; |
| 83 | spans[i].y = y + i; |
| 84 | spans[i].coverage = 255; |
| 85 | ++i; |
| 86 | } |
| 87 |
| 88 | data->mUnclippedBlendFunc(n, spans, data); |
| 89 | y += n; |
| 90 | } |
| 91 | } |
| 92 |
| 93 | void VPainterImpl::drawBitmapUntransform(const VRect & target, |
| 94 | const VBitmap &bitmap, |
| 95 | const VRect & source, |
| 96 | uint8_t const_alpha) |
| 97 | { |
| 98 | mSpanData.initTexture(&bitmap, const_alpha, VBitmapData::Plain, source); |
| 99 | if (!mSpanData.mUnclippedBlendFunc) return; |
| 100 | mSpanData.dx = -target.x(); |
| 101 | mSpanData.dy = -target.y(); |
| 102 |
| 103 | VRect rr = source.translated(target.x(), target.y()); |
| 104 |
| 105 | fillRect(rr, &mSpanData); |
| 106 | } |
| 107 |
| 108 | VPainter::~VPainter() |
| 109 | { |
| 110 | delete mImpl; |
| 111 | } |
| 112 |
| 113 | VPainter::VPainter() |
| 114 | { |
| 115 | mImpl = new VPainterImpl; |
| 116 | } |
| 117 |
| 118 | VPainter::VPainter(VBitmap *buffer, bool clear) |
| 119 | { |
| 120 | mImpl = new VPainterImpl; |
| 121 | begin(buffer, clear); |
| 122 | } |
| 123 | bool VPainter::begin(VBitmap *buffer, bool clear) |
| 124 | { |
| 125 | mImpl->mBuffer.prepare(buffer); |
| 126 | mImpl->mSpanData.init(&mImpl->mBuffer); |
| 127 | // TODO find a better api to clear the surface |
| 128 | if (clear) { |
| 129 | mImpl->mBuffer.clear(); |
| 130 | } |
| 131 | return true; |
| 132 | } |
| 133 | void VPainter::end() {} |
| 134 |
| 135 | void VPainter::setDrawRegion(const VRect ®ion) |
| 136 | { |
| 137 | mImpl->mSpanData.setDrawRegion(region); |
| 138 | } |
| 139 |
| 140 | void VPainter::setBrush(const VBrush &brush) |
| 141 | { |
| 142 | mImpl->mSpanData.setup(brush); |
| 143 | } |
| 144 |
| 145 | void VPainter::setCompositionMode(CompositionMode mode) |
| 146 | { |
| 147 | mImpl->setCompositionMode(mode); |
| 148 | } |
| 149 |
| 150 | void VPainter::drawRle(const VPoint &pos, const VRle &rle) |
| 151 | { |
| 152 | mImpl->drawRle(pos, rle); |
| 153 | } |
| 154 |
| 155 | void VPainter::drawRle(const VRle &rle, const VRle &clip) |
| 156 | { |
| 157 | mImpl->drawRle(rle, clip); |
| 158 | } |
| 159 |
| 160 | VRect VPainter::clipBoundingRect() const |
| 161 | { |
| 162 | return mImpl->mSpanData.clipRect(); |
| 163 | } |
| 164 |
| 165 | void VPainter::drawBitmap(const VPoint &point, const VBitmap &bitmap, |
| 166 | const VRect &source, uint8_t const_alpha) |
| 167 | { |
| 168 | if (!bitmap.valid()) return; |
| 169 |
| 170 | drawBitmap(VRect(point.x(), point.y(), bitmap.width(), bitmap.height()), |
| 171 | bitmap, source, const_alpha); |
| 172 | } |
| 173 |
| 174 | void VPainter::drawBitmap(const VRect &target, const VBitmap &bitmap, |
| 175 | const VRect &source, uint8_t const_alpha) |
| 176 | { |
| 177 | if (!bitmap.valid()) return; |
| 178 |
| 179 | // clear any existing brush data. |
| 180 | setBrush(VBrush()); |
| 181 |
| 182 | if (target.size() == source.size()) { |
| 183 | mImpl->drawBitmapUntransform(target, bitmap, source, const_alpha); |
| 184 | } else { |
| 185 | // @TODO scaling |
| 186 | } |
| 187 | } |
| 188 |
| 189 | void VPainter::drawBitmap(const VPoint &point, const VBitmap &bitmap, |
| 190 | uint8_t const_alpha) |
| 191 | { |
| 192 | if (!bitmap.valid()) return; |
| 193 |
| 194 | drawBitmap(VRect(point.x(), point.y(), bitmap.width(), bitmap.height()), |
| 195 | bitmap, VRect(0, 0, bitmap.width(), bitmap.height()), |
| 196 | const_alpha); |
| 197 | } |
| 198 |
| 199 | void VPainter::drawBitmap(const VRect &rect, const VBitmap &bitmap, |
| 200 | uint8_t const_alpha) |
| 201 | { |
| 202 | if (!bitmap.valid()) return; |
| 203 |
| 204 | drawBitmap(rect, bitmap, VRect(0, 0, bitmap.width(), bitmap.height()), |
| 205 | const_alpha); |
| 206 | } |
| 207 |
| 208 | V_END_NAMESPACE |
| 209 |