Refactor blocks

This commit is contained in:
Elias Batek 2024-04-24 03:00:47 +02:00
parent 23e3d59538
commit 486195859c
1 changed files with 161 additions and 164 deletions

View File

@ -219,25 +219,25 @@ private struct OriginRectangle {
} }
} }
private {
@safe pure nothrow @nogc: @safe pure nothrow @nogc:
// misc
private {
Point pos(Rectangle r) => r.upperLeft; Point pos(Rectangle r) => r.upperLeft;
} }
// Alpha-blending functions // ==== Alpha-blending functions ====
@safe pure nothrow @nogc {
/// ///
public void alphaBlend(scope Pixel[] target, scope const Pixel[] source) @trusted public void alphaBlend(scope Pixel[] target, scope const Pixel[] source) @trusted
in (source.length == target.length) { in (source.length == target.length) {
foreach (immutable idx, ref pxtarget; target) { foreach (immutable idx, ref pxtarget; target) {
alphaBlend(pxtarget, source.ptr[idx]); alphaBlend(pxtarget, source.ptr[idx]);
} }
} }
/// ///
public void alphaBlend(ref Pixel pxTarget, const Pixel pxSource) @trusted { public void alphaBlend(ref Pixel pxTarget, const Pixel pxSource) @trusted {
pragma(inline, true); pragma(inline, true);
immutable alphaSource = (pxSource.a | (pxSource.a << 8)); immutable alphaSource = (pxSource.a | (pxSource.a << 8));
@ -248,24 +248,22 @@ private {
immutable s = cast(ubyte)(((pxSource.components.ptr[ib] * alphaSource) + 0x8080) >> 16); immutable s = cast(ubyte)(((pxSource.components.ptr[ib] * alphaSource) + 0x8080) >> 16);
px = cast(ubyte)(d + s); px = cast(ubyte)(d + s);
} }
}
} }
// Drawing functions // ==== Drawing functions ====
@safe pure nothrow @nogc {
/++ /++
Draws a single pixel Draws a single pixel
+/ +/
void drawPixel(Pixmap target, Point pos, Pixel color) { void drawPixel(Pixmap target, Point pos, Pixel color) {
immutable size_t offset = linearOffset(target.width, pos); immutable size_t offset = linearOffset(target.width, pos);
target.data[offset] = color; target.data[offset] = color;
} }
/++ /++
Draws a rectangle Draws a rectangle
+/ +/
void drawRectangle(Pixmap target, Rectangle rectangle, Pixel color) { void drawRectangle(Pixmap target, Rectangle rectangle, Pixel color) {
alias r = rectangle; alias r = rectangle;
immutable tRect = OriginRectangle( immutable tRect = OriginRectangle(
@ -292,12 +290,12 @@ private {
foreach (y; drawingTarget.y .. drawingEnd.y) { foreach (y; drawingTarget.y .. drawingEnd.y) {
target.sliceAt(Point(drawingTarget.x, y), drawingWidth)[] = color; target.sliceAt(Point(drawingTarget.x, y), drawingWidth)[] = color;
} }
} }
/++ /++
Draws a line Draws a line
+/ +/
void drawLine(Pixmap target, Point a, Point b, Pixel color) { void drawLine(Pixmap target, Point a, Point b, Pixel color) {
import std.math : round, sqrt; import std.math : round, sqrt;
// TODO: line width // TODO: line width
@ -326,9 +324,9 @@ private {
immutable offsetEnd = linearOffset(b, target.width); immutable offsetEnd = linearOffset(b, target.width);
target.data[offsetEnd] = color; target.data[offsetEnd] = color;
} }
/++ /++
Draws an image (a source pixmap) on a target pixmap Draws an image (a source pixmap) on a target pixmap
Params: Params:
@ -336,7 +334,7 @@ private {
image = source pixmap image = source pixmap
pos = top-left destination position (on the target pixmap) pos = top-left destination position (on the target pixmap)
+/ +/
void drawPixmap(Pixmap target, Pixmap image, Point pos) { void drawPixmap(Pixmap target, Pixmap image, Point pos) {
alias source = image; alias source = image;
immutable tRect = OriginRectangle( immutable tRect = OriginRectangle(
@ -367,12 +365,12 @@ private {
target.sliceAt(Point(drawingTarget.x, y), drawingWidth)[] = target.sliceAt(Point(drawingTarget.x, y), drawingWidth)[] =
source.sliceAt(Point(drawingSource.x, y + drawingSource.y), drawingWidth); source.sliceAt(Point(drawingSource.x, y + drawingSource.y), drawingWidth);
} }
} }
/++ /++
Draws a sprite from a spritesheet Draws a sprite from a spritesheet
+/ +/
void drawSprite(Pixmap target, const SpriteSheet sheet, int spriteIndex, Point pos) { void drawSprite(Pixmap target, const SpriteSheet sheet, int spriteIndex, Point pos) {
immutable tRect = OriginRectangle( immutable tRect = OriginRectangle(
Size(target.width, target.height), Size(target.width, target.height),
); );
@ -405,5 +403,4 @@ private {
target.sliceAt(Point(drawingTarget.x, y), drawingWidth)[] target.sliceAt(Point(drawingTarget.x, y), drawingWidth)[]
= sheet.pixmap.sliceAt(Point(drawingSource.x, y + drawingSource.y), drawingWidth); = sheet.pixmap.sliceAt(Point(drawingSource.x, y + drawingSource.y), drawingWidth);
} }
}
} }