1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
/*
* Canvas.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "Canvas.h"
#include "../gui/CGuiHandler.h"
#include "../media/IVideoPlayer.h"
#include "../render/IRenderHandler.h"
#include "../render/IScreenHandler.h"
#include "../renderSDL/SDL_Extensions.h"
#include "Colors.h"
#include "IImage.h"
#include "Graphics.h"
#include "IFont.h"
#include <SDL_surface.h>
#include <SDL_pixels.h>
Canvas::Canvas(SDL_Surface * surface, CanvasScalingPolicy scalingPolicy):
scalingPolicy(scalingPolicy),
surface(surface),
renderArea(0,0, surface->w, surface->h)
{
surface->refcount++;
}
Canvas::Canvas(const Canvas & other):
scalingPolicy(other.scalingPolicy),
surface(other.surface),
renderArea(other.renderArea)
{
surface->refcount++;
}
Canvas::Canvas(Canvas && other):
scalingPolicy(other.scalingPolicy),
surface(other.surface),
renderArea(other.renderArea)
{
surface->refcount++;
}
Canvas::Canvas(const Canvas & other, const Rect & newClipRect):
Canvas(other)
{
Rect scaledClipRect( transformPos(newClipRect.topLeft()), transformPos(newClipRect.dimensions()));
renderArea = other.renderArea.intersect(scaledClipRect + other.renderArea.topLeft());
}
Canvas::Canvas(const Point & size, CanvasScalingPolicy scalingPolicy):
scalingPolicy(scalingPolicy),
surface(CSDL_Ext::newSurface(size * getScalingFactor())),
renderArea(Point(0,0), size * getScalingFactor())
{
CSDL_Ext::fillSurface(surface, CSDL_Ext::toSDL(Colors::TRANSPARENCY) );
SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_NONE);
}
int Canvas::getScalingFactor() const
{
if (scalingPolicy == CanvasScalingPolicy::IGNORE)
return 1;
return GH.screenHandler().getScalingFactor();
}
Point Canvas::transformPos(const Point & input)
{
return renderArea.topLeft() + input * getScalingFactor();
}
Point Canvas::transformSize(const Point & input)
{
return input * getScalingFactor();
}
Canvas Canvas::createFromSurface(SDL_Surface * surface, CanvasScalingPolicy scalingPolicy)
{
return Canvas(surface, scalingPolicy);
}
void Canvas::applyTransparency(bool on)
{
if (on)
SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND);
else
SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_NONE);
}
void Canvas::applyGrayscale()
{
CSDL_Ext::convertToGrayscale(surface, renderArea);
}
Canvas::~Canvas()
{
SDL_FreeSurface(surface);
}
void Canvas::draw(IVideoInstance & video, const Point & pos)
{
video.show(pos, surface);
}
void Canvas::draw(const IImage& image, const Point & pos)
{
image.draw(surface, transformPos(pos), nullptr, getScalingFactor());
}
void Canvas::draw(const std::shared_ptr<IImage>& image, const Point & pos)
{
assert(image);
if (image)
image->draw(surface, transformPos(pos), nullptr, getScalingFactor());
}
void Canvas::draw(const std::shared_ptr<IImage>& image, const Point & pos, const Rect & sourceRect)
{
Rect realSourceRect = sourceRect * getScalingFactor();
assert(image);
if (image)
image->draw(surface, transformPos(pos), &realSourceRect, getScalingFactor());
}
void Canvas::draw(const Canvas & image, const Point & pos)
{
CSDL_Ext::blitSurface(image.surface, image.renderArea, surface, transformPos(pos));
}
void Canvas::drawTransparent(const Canvas & image, const Point & pos, double transparency)
{
SDL_BlendMode oldMode;
SDL_GetSurfaceBlendMode(image.surface, &oldMode);
SDL_SetSurfaceBlendMode(image.surface, SDL_BLENDMODE_BLEND);
SDL_SetSurfaceAlphaMod(image.surface, 255 * transparency);
CSDL_Ext::blitSurface(image.surface, image.renderArea, surface, transformPos(pos));
SDL_SetSurfaceAlphaMod(image.surface, 255);
SDL_SetSurfaceBlendMode(image.surface, oldMode);
}
void Canvas::drawScaled(const Canvas & image, const Point & pos, const Point & targetSize)
{
SDL_Rect targetRect = CSDL_Ext::toSDL(Rect(transformPos(pos), transformSize(targetSize)));
SDL_BlitScaled(image.surface, nullptr, surface, &targetRect);
}
void Canvas::drawPoint(const Point & dest, const ColorRGBA & color)
{
Point point = transformPos(dest);
CSDL_Ext::putPixelWithoutRefreshIfInSurf(surface, point.x, point.y, color.r, color.g, color.b, color.a);
}
void Canvas::drawLine(const Point & from, const Point & dest, const ColorRGBA & colorFrom, const ColorRGBA & colorDest)
{
CSDL_Ext::drawLine(surface, transformPos(from), transformPos(dest), CSDL_Ext::toSDL(colorFrom), CSDL_Ext::toSDL(colorDest), getScalingFactor());
}
void Canvas::drawBorder(const Rect & target, const ColorRGBA & color, int width)
{
Rect realTarget = target * getScalingFactor() + renderArea.topLeft();
CSDL_Ext::drawBorder(surface, realTarget.x, realTarget.y, realTarget.w, realTarget.h, CSDL_Ext::toSDL(color), width * getScalingFactor());
}
void Canvas::drawBorderDashed(const Rect & target, const ColorRGBA & color)
{
Rect realTarget = target * getScalingFactor() + renderArea.topLeft();
CSDL_Ext::drawLineDashed(surface, realTarget.topLeft(), realTarget.topRight(), CSDL_Ext::toSDL(color));
CSDL_Ext::drawLineDashed(surface, realTarget.bottomLeft(), realTarget.bottomRight(), CSDL_Ext::toSDL(color));
CSDL_Ext::drawLineDashed(surface, realTarget.topLeft(), realTarget.bottomLeft(), CSDL_Ext::toSDL(color));
CSDL_Ext::drawLineDashed(surface, realTarget.topRight(), realTarget.bottomRight(), CSDL_Ext::toSDL(color));
}
void Canvas::drawText(const Point & position, const EFonts & font, const ColorRGBA & colorDest, ETextAlignment alignment, const std::string & text )
{
const auto & fontPtr = GH.renderHandler().loadFont(font);
switch (alignment)
{
case ETextAlignment::TOPLEFT: return fontPtr->renderTextLeft (surface, text, colorDest, transformPos(position));
case ETextAlignment::TOPCENTER: return fontPtr->renderTextCenter(surface, text, colorDest, transformPos(position));
case ETextAlignment::CENTER: return fontPtr->renderTextCenter(surface, text, colorDest, transformPos(position));
case ETextAlignment::BOTTOMRIGHT: return fontPtr->renderTextRight (surface, text, colorDest, transformPos(position));
}
}
void Canvas::drawText(const Point & position, const EFonts & font, const ColorRGBA & colorDest, ETextAlignment alignment, const std::vector<std::string> & text )
{
const auto & fontPtr = GH.renderHandler().loadFont(font);
switch (alignment)
{
case ETextAlignment::TOPLEFT: return fontPtr->renderTextLinesLeft (surface, text, colorDest, transformPos(position));
case ETextAlignment::TOPCENTER: return fontPtr->renderTextLinesCenter(surface, text, colorDest, transformPos(position));
case ETextAlignment::CENTER: return fontPtr->renderTextLinesCenter(surface, text, colorDest, transformPos(position));
case ETextAlignment::BOTTOMRIGHT: return fontPtr->renderTextLinesRight (surface, text, colorDest, transformPos(position));
}
}
void Canvas::drawColor(const Rect & target, const ColorRGBA & color)
{
Rect realTarget = target * getScalingFactor() + renderArea.topLeft();
CSDL_Ext::fillRect(surface, realTarget, CSDL_Ext::toSDL(color));
}
void Canvas::drawColorBlended(const Rect & target, const ColorRGBA & color)
{
Rect realTarget = target * getScalingFactor() + renderArea.topLeft();
CSDL_Ext::fillRectBlended(surface, realTarget, CSDL_Ext::toSDL(color));
}
void Canvas::fillTexture(const std::shared_ptr<IImage>& image)
{
assert(image);
if (!image)
return;
Rect imageArea(Point(0, 0), image->dimensions());
for (int y=0; y < surface->h; y+= imageArea.h)
{
for (int x=0; x < surface->w; x+= imageArea.w)
image->draw(surface, Point(renderArea.x + x * getScalingFactor(), renderArea.y + y * getScalingFactor()), nullptr, getScalingFactor());
}
}
Rect Canvas::getRenderArea() const
{
return renderArea;
}
CanvasClipRectGuard::CanvasClipRectGuard(Canvas & canvas, const Rect & rect): surf(canvas.surface)
{
CSDL_Ext::getClipRect(surf, oldRect);
CSDL_Ext::setClipRect(surf, rect * GH.screenHandler().getScalingFactor());
}
CanvasClipRectGuard::~CanvasClipRectGuard()
{
CSDL_Ext::setClipRect(surf, oldRect);
}
|