2022-11-25 11:46:47 +02:00
|
|
|
/*
|
2022-12-11 22:09:57 +02:00
|
|
|
* Canvas.cpp, part of VCMI engine
|
2022-11-25 11:46:47 +02:00
|
|
|
*
|
|
|
|
* 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"
|
2022-12-11 22:09:57 +02:00
|
|
|
#include "Canvas.h"
|
2022-11-25 11:46:47 +02:00
|
|
|
|
2023-02-01 20:42:06 +02:00
|
|
|
#include "../renderSDL/SDL_Extensions.h"
|
2023-02-22 17:24:42 +02:00
|
|
|
#include "Colors.h"
|
2023-02-01 20:42:06 +02:00
|
|
|
#include "IImage.h"
|
|
|
|
#include "Graphics.h"
|
2023-07-31 18:50:55 +02:00
|
|
|
#include "IFont.h"
|
2022-11-26 23:12:20 +02:00
|
|
|
|
2023-01-17 22:01:35 +02:00
|
|
|
#include <SDL_surface.h>
|
2023-07-31 18:50:55 +02:00
|
|
|
#include <SDL_pixels.h>
|
2023-01-17 22:01:35 +02:00
|
|
|
|
2022-12-11 22:09:57 +02:00
|
|
|
Canvas::Canvas(SDL_Surface * surface):
|
2023-01-05 14:16:01 +02:00
|
|
|
surface(surface),
|
2023-02-14 23:49:12 +02:00
|
|
|
renderArea(0,0, surface->w, surface->h)
|
2022-11-25 11:46:47 +02:00
|
|
|
{
|
|
|
|
surface->refcount++;
|
|
|
|
}
|
|
|
|
|
2023-02-10 15:29:30 +02:00
|
|
|
Canvas::Canvas(const Canvas & other):
|
2023-01-05 14:16:01 +02:00
|
|
|
surface(other.surface),
|
2023-02-14 23:49:12 +02:00
|
|
|
renderArea(other.renderArea)
|
2022-12-11 22:09:57 +02:00
|
|
|
{
|
|
|
|
surface->refcount++;
|
|
|
|
}
|
|
|
|
|
2023-06-02 15:42:18 +02:00
|
|
|
Canvas::Canvas(Canvas && other):
|
|
|
|
surface(other.surface),
|
|
|
|
renderArea(other.renderArea)
|
|
|
|
{
|
|
|
|
surface->refcount++;
|
|
|
|
}
|
|
|
|
|
2023-02-10 15:29:30 +02:00
|
|
|
Canvas::Canvas(const Canvas & other, const Rect & newClipRect):
|
2023-01-05 14:16:01 +02:00
|
|
|
Canvas(other)
|
|
|
|
{
|
2023-02-14 23:49:12 +02:00
|
|
|
renderArea = other.renderArea.intersect(newClipRect + other.renderArea.topLeft());
|
2023-01-05 14:16:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Canvas::Canvas(const Point & size):
|
2023-02-14 23:49:12 +02:00
|
|
|
renderArea(Point(0,0), size),
|
2023-02-10 15:29:30 +02:00
|
|
|
surface(CSDL_Ext::newSurface(size.x, size.y))
|
2022-11-25 11:46:47 +02:00
|
|
|
{
|
2023-07-31 18:50:55 +02:00
|
|
|
CSDL_Ext::fillSurface(surface, CSDL_Ext::toSDL(Colors::TRANSPARENCY) );
|
2023-02-20 16:31:14 +02:00
|
|
|
SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_NONE);
|
2022-11-25 11:46:47 +02:00
|
|
|
}
|
|
|
|
|
2023-06-02 15:42:18 +02:00
|
|
|
Canvas Canvas::createFromSurface(SDL_Surface * surface)
|
|
|
|
{
|
|
|
|
return Canvas(surface);
|
|
|
|
}
|
|
|
|
|
2023-02-22 17:24:42 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-12-11 22:09:57 +02:00
|
|
|
Canvas::~Canvas()
|
2022-11-25 11:46:47 +02:00
|
|
|
{
|
|
|
|
SDL_FreeSurface(surface);
|
|
|
|
}
|
|
|
|
|
2023-02-15 23:13:25 +02:00
|
|
|
void Canvas::draw(const std::shared_ptr<IImage>& image, const Point & pos)
|
2022-11-25 11:46:47 +02:00
|
|
|
{
|
2022-12-11 22:09:57 +02:00
|
|
|
assert(image);
|
|
|
|
if (image)
|
2023-02-14 23:49:12 +02:00
|
|
|
image->draw(surface, renderArea.x + pos.x, renderArea.y + pos.y);
|
2022-11-25 11:46:47 +02:00
|
|
|
}
|
|
|
|
|
2023-02-15 23:13:25 +02:00
|
|
|
void Canvas::draw(const std::shared_ptr<IImage>& image, const Point & pos, const Rect & sourceRect)
|
2022-11-27 22:50:18 +02:00
|
|
|
{
|
2022-12-11 22:09:57 +02:00
|
|
|
assert(image);
|
|
|
|
if (image)
|
2023-02-14 23:49:12 +02:00
|
|
|
image->draw(surface, renderArea.x + pos.x, renderArea.y + pos.y, &sourceRect);
|
2022-11-27 22:50:18 +02:00
|
|
|
}
|
|
|
|
|
2023-02-15 23:13:25 +02:00
|
|
|
void Canvas::draw(const Canvas & image, const Point & pos)
|
2022-11-25 11:46:47 +02:00
|
|
|
{
|
2023-02-14 23:49:12 +02:00
|
|
|
CSDL_Ext::blitSurface(image.surface, image.renderArea, surface, renderArea.topLeft() + pos);
|
2022-11-25 11:46:47 +02:00
|
|
|
}
|
|
|
|
|
2023-02-20 16:31:14 +02:00
|
|
|
void Canvas::drawTransparent(const Canvas & image, const Point & pos, double transparency)
|
|
|
|
{
|
2023-03-01 19:25:51 +02:00
|
|
|
SDL_BlendMode oldMode;
|
|
|
|
|
|
|
|
SDL_GetSurfaceBlendMode(image.surface, &oldMode);
|
|
|
|
SDL_SetSurfaceBlendMode(image.surface, SDL_BLENDMODE_BLEND);
|
2023-02-20 16:31:14 +02:00
|
|
|
SDL_SetSurfaceAlphaMod(image.surface, 255 * transparency);
|
|
|
|
CSDL_Ext::blitSurface(image.surface, image.renderArea, surface, renderArea.topLeft() + pos);
|
|
|
|
SDL_SetSurfaceAlphaMod(image.surface, 255);
|
2023-03-01 19:25:51 +02:00
|
|
|
SDL_SetSurfaceBlendMode(image.surface, oldMode);
|
2023-02-20 16:31:14 +02:00
|
|
|
}
|
|
|
|
|
2023-02-15 23:13:25 +02:00
|
|
|
void Canvas::drawScaled(const Canvas & image, const Point & pos, const Point & targetSize)
|
2023-02-10 15:29:30 +02:00
|
|
|
{
|
2023-02-20 16:31:14 +02:00
|
|
|
SDL_Rect targetRect = CSDL_Ext::toSDL(Rect(pos + renderArea.topLeft(), targetSize));
|
|
|
|
SDL_BlitScaled(image.surface, nullptr, surface, &targetRect);
|
2023-02-10 15:29:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Canvas::drawPoint(const Point & dest, const ColorRGBA & color)
|
|
|
|
{
|
|
|
|
CSDL_Ext::putPixelWithoutRefreshIfInSurf(surface, dest.x, dest.y, color.r, color.g, color.b, color.a);
|
|
|
|
}
|
|
|
|
|
2023-01-30 00:12:43 +02:00
|
|
|
void Canvas::drawLine(const Point & from, const Point & dest, const ColorRGBA & colorFrom, const ColorRGBA & colorDest)
|
2022-11-25 16:32:23 +02:00
|
|
|
{
|
2023-02-14 23:49:12 +02:00
|
|
|
CSDL_Ext::drawLine(surface, renderArea.topLeft() + from, renderArea.topLeft() + dest, CSDL_Ext::toSDL(colorFrom), CSDL_Ext::toSDL(colorDest));
|
2023-02-10 15:29:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Canvas::drawLineDashed(const Point & from, const Point & dest, const ColorRGBA & color)
|
|
|
|
{
|
2023-02-14 23:49:12 +02:00
|
|
|
CSDL_Ext::drawLineDashed(surface, renderArea.topLeft() + from, renderArea.topLeft() + dest, CSDL_Ext::toSDL(color));
|
2023-02-10 15:29:30 +02:00
|
|
|
}
|
|
|
|
|
2023-07-31 18:50:55 +02:00
|
|
|
void Canvas::drawBorder(const Rect & target, const ColorRGBA & color, int width)
|
2023-06-02 15:42:18 +02:00
|
|
|
{
|
|
|
|
Rect realTarget = target + renderArea.topLeft();
|
|
|
|
|
2023-07-31 18:50:55 +02:00
|
|
|
CSDL_Ext::drawBorder(surface, realTarget.x, realTarget.y, realTarget.w, realTarget.h, CSDL_Ext::toSDL(color), width);
|
2023-06-02 15:42:18 +02:00
|
|
|
}
|
|
|
|
|
2023-02-10 15:29:30 +02:00
|
|
|
void Canvas::drawBorderDashed(const Rect & target, const ColorRGBA & color)
|
|
|
|
{
|
2023-02-14 23:49:12 +02:00
|
|
|
Rect realTarget = target + renderArea.topLeft();
|
2023-02-10 15:29:30 +02:00
|
|
|
|
|
|
|
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));
|
2022-11-25 16:32:23 +02:00
|
|
|
}
|
|
|
|
|
2023-07-31 18:50:55 +02:00
|
|
|
void Canvas::drawText(const Point & position, const EFonts & font, const ColorRGBA & colorDest, ETextAlignment alignment, const std::string & text )
|
2022-11-26 23:12:20 +02:00
|
|
|
{
|
|
|
|
switch (alignment)
|
|
|
|
{
|
2023-02-14 23:49:12 +02:00
|
|
|
case ETextAlignment::TOPLEFT: return graphics->fonts[font]->renderTextLeft (surface, text, colorDest, renderArea.topLeft() + position);
|
2023-08-30 00:35:31 +02:00
|
|
|
case ETextAlignment::TOPCENTER: return graphics->fonts[font]->renderTextCenter(surface, text, colorDest, renderArea.topLeft() + position);
|
2023-02-14 23:49:12 +02:00
|
|
|
case ETextAlignment::CENTER: return graphics->fonts[font]->renderTextCenter(surface, text, colorDest, renderArea.topLeft() + position);
|
|
|
|
case ETextAlignment::BOTTOMRIGHT: return graphics->fonts[font]->renderTextRight (surface, text, colorDest, renderArea.topLeft() + position);
|
2022-11-26 23:12:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-31 18:50:55 +02:00
|
|
|
void Canvas::drawText(const Point & position, const EFonts & font, const ColorRGBA & colorDest, ETextAlignment alignment, const std::vector<std::string> & text )
|
2022-11-26 23:12:20 +02:00
|
|
|
{
|
|
|
|
switch (alignment)
|
|
|
|
{
|
2023-02-14 23:49:12 +02:00
|
|
|
case ETextAlignment::TOPLEFT: return graphics->fonts[font]->renderTextLinesLeft (surface, text, colorDest, renderArea.topLeft() + position);
|
2023-08-30 00:35:31 +02:00
|
|
|
case ETextAlignment::TOPCENTER: return graphics->fonts[font]->renderTextLinesCenter(surface, text, colorDest, renderArea.topLeft() + position);
|
2023-02-14 23:49:12 +02:00
|
|
|
case ETextAlignment::CENTER: return graphics->fonts[font]->renderTextLinesCenter(surface, text, colorDest, renderArea.topLeft() + position);
|
|
|
|
case ETextAlignment::BOTTOMRIGHT: return graphics->fonts[font]->renderTextLinesRight (surface, text, colorDest, renderArea.topLeft() + position);
|
2022-11-26 23:12:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-31 18:50:55 +02:00
|
|
|
void Canvas::drawColor(const Rect & target, const ColorRGBA & color)
|
2023-06-02 15:42:18 +02:00
|
|
|
{
|
|
|
|
Rect realTarget = target + renderArea.topLeft();
|
|
|
|
|
2023-07-31 18:50:55 +02:00
|
|
|
CSDL_Ext::fillRect(surface, realTarget, CSDL_Ext::toSDL(color));
|
2023-06-02 15:42:18 +02:00
|
|
|
}
|
|
|
|
|
2023-09-10 20:52:35 +02:00
|
|
|
void Canvas::drawColorBlended(const Rect & target, const ColorRGBA & color)
|
|
|
|
{
|
|
|
|
Rect realTarget = target + renderArea.topLeft();
|
|
|
|
|
|
|
|
CSDL_Ext::fillRectBlended(surface, realTarget, CSDL_Ext::toSDL(color));
|
2023-06-02 15:42:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SDL_Surface * Canvas::getInternalSurface()
|
|
|
|
{
|
|
|
|
return surface;
|
|
|
|
}
|