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:
// misc
private {
Point pos(Rectangle r) => r.upperLeft;
}
// Alpha-blending functions
@safe pure nothrow @nogc {
// ==== Alpha-blending functions ====
///
public void alphaBlend(scope Pixel[] target, scope const Pixel[] source) @trusted
in (source.length == target.length) {
///
public void alphaBlend(scope Pixel[] target, scope const Pixel[] source) @trusted
in (source.length == target.length) {
foreach (immutable idx, ref pxtarget; target) {
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);
immutable alphaSource = (pxSource.a | (pxSource.a << 8));
@ -248,24 +248,22 @@ private {
immutable s = cast(ubyte)(((pxSource.components.ptr[ib] * alphaSource) + 0x8080) >> 16);
px = cast(ubyte)(d + s);
}
}
}
// Drawing functions
@safe pure nothrow @nogc {
// ==== Drawing functions ====
/++
/++
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);
target.data[offset] = color;
}
}
/++
/++
Draws a rectangle
+/
void drawRectangle(Pixmap target, Rectangle rectangle, Pixel color) {
void drawRectangle(Pixmap target, Rectangle rectangle, Pixel color) {
alias r = rectangle;
immutable tRect = OriginRectangle(
@ -292,12 +290,12 @@ private {
foreach (y; drawingTarget.y .. drawingEnd.y) {
target.sliceAt(Point(drawingTarget.x, y), drawingWidth)[] = color;
}
}
}
/++
/++
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;
// TODO: line width
@ -326,9 +324,9 @@ private {
immutable offsetEnd = linearOffset(b, target.width);
target.data[offsetEnd] = color;
}
}
/++
/++
Draws an image (a source pixmap) on a target pixmap
Params:
@ -336,7 +334,7 @@ private {
image = source 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;
immutable tRect = OriginRectangle(
@ -367,12 +365,12 @@ private {
target.sliceAt(Point(drawingTarget.x, y), drawingWidth)[] =
source.sliceAt(Point(drawingSource.x, y + drawingSource.y), drawingWidth);
}
}
}
/++
/++
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(
Size(target.width, target.height),
);
@ -405,5 +403,4 @@ private {
target.sliceAt(Point(drawingTarget.x, y), drawingWidth)[]
= sheet.pixmap.sliceAt(Point(drawingSource.x, y + drawingSource.y), drawingWidth);
}
}
}