You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
187 lines
5.2 KiB
187 lines
5.2 KiB
|
1 year ago
|
#include "engine/render/primitive_render.hpp"
|
||
|
5 years ago
|
|
||
|
4 years ago
|
#include <cassert>
|
||
|
|
#include <cstdint>
|
||
|
4 years ago
|
#include <cstring>
|
||
|
5 years ago
|
|
||
|
3 years ago
|
#include "engine/palette.h"
|
||
|
1 year ago
|
#include "engine/point.hpp"
|
||
|
|
#include "engine/size.hpp"
|
||
|
|
#include "engine/surface.hpp"
|
||
|
8 years ago
|
|
||
|
5 years ago
|
namespace devilution {
|
||
|
5 years ago
|
namespace {
|
||
|
|
|
||
|
4 years ago
|
void DrawHalfTransparentUnalignedBlendedRectTo(const Surface &out, unsigned sx, unsigned sy, unsigned width, unsigned height)
|
||
|
|
{
|
||
|
|
uint8_t *pix = out.at(static_cast<int>(sx), static_cast<int>(sy));
|
||
|
3 years ago
|
const uint8_t *const lookupTable = paletteTransparencyLookup[0];
|
||
|
4 years ago
|
const unsigned skipX = out.pitch() - width;
|
||
|
|
for (unsigned y = 0; y < height; ++y) {
|
||
|
|
for (unsigned x = 0; x < width; ++x, ++pix) {
|
||
|
|
*pix = lookupTable[*pix];
|
||
|
|
}
|
||
|
|
pix += skipX;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#if DEVILUTIONX_PALETTE_TRANSPARENCY_BLACK_16_LUT
|
||
|
4 years ago
|
// Expects everything to be 4-byte aligned.
|
||
|
|
void DrawHalfTransparentAligned32BlendedRectTo(const Surface &out, unsigned sx, unsigned sy, unsigned width, unsigned height)
|
||
|
5 years ago
|
{
|
||
|
4 years ago
|
assert(out.pitch() % 4 == 0);
|
||
|
5 years ago
|
|
||
|
4 years ago
|
auto *pix = reinterpret_cast<uint32_t *>(out.at(static_cast<int>(sx), static_cast<int>(sy)));
|
||
|
|
assert(reinterpret_cast<intptr_t>(pix) % 4 == 0);
|
||
|
|
|
||
|
|
const uint16_t *lookupTable = paletteTransparencyLookupBlack16;
|
||
|
|
|
||
|
|
const unsigned skipX = (out.pitch() - width) / 4;
|
||
|
|
width /= 4;
|
||
|
|
while (height-- > 0) {
|
||
|
|
for (unsigned i = 0; i < width; ++i, ++pix) {
|
||
|
|
const uint32_t v = *pix;
|
||
|
|
*pix = lookupTable[v & 0xFFFF] | (lookupTable[(v >> 16) & 0xFFFF] << 16);
|
||
|
5 years ago
|
}
|
||
|
4 years ago
|
pix += skipX;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void DrawHalfTransparentBlendedRectTo(const Surface &out, unsigned sx, unsigned sy, unsigned width, unsigned height)
|
||
|
|
{
|
||
|
|
// All SDL surfaces are 4-byte aligned and divisible by 4.
|
||
|
|
// However, our coordinates and widths may not be.
|
||
|
|
|
||
|
|
// First, draw the leading unaligned part.
|
||
|
|
if (sx % 4 != 0) {
|
||
|
|
const unsigned w = 4 - sx % 4;
|
||
|
|
DrawHalfTransparentUnalignedBlendedRectTo(out, sx, sy, w, height);
|
||
|
|
sx += w;
|
||
|
|
width -= w;
|
||
|
5 years ago
|
}
|
||
|
4 years ago
|
|
||
|
|
if (static_cast<int>(sx + width) == out.w()) {
|
||
|
|
// The pitch is 4-byte aligned, so we can simply extend the width to the pitch.
|
||
|
|
width = out.pitch() - sx;
|
||
|
|
} else if (width % 4 != 0) {
|
||
|
|
// Draw the trailing unaligned part.
|
||
|
|
const unsigned w = width % 4;
|
||
|
|
DrawHalfTransparentUnalignedBlendedRectTo(out, sx + (width / 4) * 4, sy, w, height);
|
||
|
|
width -= w;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Now everything is divisible by 4. Draw the aligned part.
|
||
|
|
DrawHalfTransparentAligned32BlendedRectTo(out, sx, sy, width, height);
|
||
|
5 years ago
|
}
|
||
|
4 years ago
|
#else
|
||
|
|
#define DrawHalfTransparentBlendedRectTo DrawHalfTransparentUnalignedBlendedRectTo
|
||
|
|
#endif
|
||
|
|
|
||
|
5 years ago
|
} // namespace
|
||
|
7 years ago
|
|
||
|
2 years ago
|
void FillRect(const Surface &out, int x, int y, int width, int height, uint8_t colorIndex)
|
||
|
|
{
|
||
|
|
for (int j = 0; j < height; j++) {
|
||
|
|
DrawHorizontalLine(out, { x, y + j }, width, colorIndex);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
5 years ago
|
void DrawHorizontalLine(const Surface &out, Point from, int width, std::uint8_t colorIndex)
|
||
|
8 years ago
|
{
|
||
|
5 years ago
|
if (from.y < 0 || from.y >= out.h() || from.x >= out.w() || width <= 0 || from.x + width <= 0)
|
||
|
|
return;
|
||
|
|
if (from.x < 0) {
|
||
|
|
width += from.x;
|
||
|
|
from.x = 0;
|
||
|
|
}
|
||
|
|
if (from.x + width > out.w())
|
||
|
5 years ago
|
width = out.w() - from.x;
|
||
|
5 years ago
|
return UnsafeDrawHorizontalLine(out, from, width, colorIndex);
|
||
|
|
}
|
||
|
|
|
||
|
5 years ago
|
void UnsafeDrawHorizontalLine(const Surface &out, Point from, int width, std::uint8_t colorIndex)
|
||
|
5 years ago
|
{
|
||
|
5 years ago
|
std::memset(&out[from], colorIndex, width);
|
||
|
5 years ago
|
}
|
||
|
|
|
||
|
5 years ago
|
void DrawVerticalLine(const Surface &out, Point from, int height, std::uint8_t colorIndex)
|
||
|
5 years ago
|
{
|
||
|
|
if (from.x < 0 || from.x >= out.w() || from.y >= out.h() || height <= 0 || from.y + height <= 0)
|
||
|
|
return;
|
||
|
|
if (from.y < 0) {
|
||
|
|
height += from.y;
|
||
|
|
from.y = 0;
|
||
|
|
}
|
||
|
|
if (from.y + height > out.h())
|
||
|
|
height = (from.y + height) - out.h();
|
||
|
|
return UnsafeDrawVerticalLine(out, from, height, colorIndex);
|
||
|
|
}
|
||
|
|
|
||
|
5 years ago
|
void UnsafeDrawVerticalLine(const Surface &out, Point from, int height, std::uint8_t colorIndex)
|
||
|
5 years ago
|
{
|
||
|
5 years ago
|
auto *dst = &out[from];
|
||
|
5 years ago
|
const auto pitch = out.pitch();
|
||
|
|
while (height-- > 0) {
|
||
|
|
*dst = colorIndex;
|
||
|
|
dst += pitch;
|
||
|
8 years ago
|
}
|
||
|
|
}
|
||
|
|
|
||
|
5 years ago
|
void DrawHalfTransparentRectTo(const Surface &out, int sx, int sy, int width, int height)
|
||
|
5 years ago
|
{
|
||
|
5 years ago
|
if (sx + width < 0)
|
||
|
|
return;
|
||
|
|
if (sy + height < 0)
|
||
|
|
return;
|
||
|
|
if (sx >= out.w())
|
||
|
|
return;
|
||
|
|
if (sy >= out.h())
|
||
|
|
return;
|
||
|
|
|
||
|
|
if (sx < 0) {
|
||
|
|
width += sx;
|
||
|
|
sx = 0;
|
||
|
|
} else if (sx + width >= out.w()) {
|
||
|
|
width = out.w() - sx;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (sy < 0) {
|
||
|
|
height += sy;
|
||
|
|
sy = 0;
|
||
|
|
} else if (sy + height >= out.h()) {
|
||
|
|
height = out.h() - sy;
|
||
|
|
}
|
||
|
|
|
||
|
4 years ago
|
DrawHalfTransparentBlendedRectTo(out, sx, sy, width, height);
|
||
|
5 years ago
|
}
|
||
|
|
|
||
|
3 years ago
|
void SetHalfTransparentPixel(const Surface &out, Point position, uint8_t color)
|
||
|
|
{
|
||
|
|
if (out.InBounds(position)) {
|
||
|
|
uint8_t *pix = out.at(position.x, position.y);
|
||
|
|
const auto &lookupTable = paletteTransparencyLookup[color];
|
||
|
|
*pix = lookupTable[*pix];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
3 years ago
|
void UnsafeDrawBorder2px(const Surface &out, Rectangle rect, uint8_t color)
|
||
|
|
{
|
||
|
|
const size_t width = rect.size.width;
|
||
|
|
const size_t height = rect.size.height;
|
||
|
|
uint8_t *buf = &out[rect.position];
|
||
|
|
std::memset(buf, color, width);
|
||
|
|
buf += out.pitch();
|
||
|
|
std::memset(buf, color, width);
|
||
|
|
buf += out.pitch();
|
||
|
|
for (size_t i = 4; i < height; ++i) {
|
||
|
|
buf[0] = buf[1] = color;
|
||
|
|
buf[width - 2] = buf[width - 1] = color;
|
||
|
|
buf += out.pitch();
|
||
|
|
}
|
||
|
|
std::memset(buf, color, width);
|
||
|
|
buf += out.pitch();
|
||
|
|
std::memset(buf, color, width);
|
||
|
|
}
|
||
|
|
|
||
|
5 years ago
|
} // namespace devilution
|