summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-05-08 19:23:25 +0200
committerkdx <kikoodx@paranoici.org>2023-05-08 19:23:25 +0200
commit65d27473a29d29ebf7e05204f4d43805d490a317 (patch)
tree19ece26404c30cc31160b888b26fb13949eeb37a
parent760da5a3efd2e6ff166164a97a3b2cf3b6db6787 (diff)
downloadpx-65d27473a29d29ebf7e05204f4d43805d490a317.tar.gz
line drawing
-rw-r--r--src/main.c1
-rw-r--r--src/px.c31
-rw-r--r--src/px.h3
3 files changed, 35 insertions, 0 deletions
diff --git a/src/main.c b/src/main.c
index c3110a4..0e5e502 100644
--- a/src/main.c
+++ b/src/main.c
@@ -61,6 +61,7 @@ main(void)
pxPal(3, 11);
pxPalt(3, true);
pxSpr(&spr, x, y + 8, .flip_x=true, .flip_y=true);
+ pxLine(0, 0, x, y, 2);;
pxPset(0, 0, 10);
pxPset(1, 0, 10);
diff --git a/src/px.c b/src/px.c
index 267b2e0..7458bcd 100644
--- a/src/px.c
+++ b/src/px.c
@@ -181,6 +181,37 @@ pxRectfill(int x0, int y0, int x1, int y1, PxCol col)
}
void
+pxLine(int x0, int y0, int x1, int y1, PxCol col)
+{
+ int dx, dy, sx, sy, err, e2;
+
+ dx = x1 - x0;
+ dx = (dx < 0) ? (-dx) : (dx);
+ dy = y1 - y0;
+ dy = (dy < 0) ? (dy) : (-dy);
+ sx = (x0 < x1) ? (1) : (-1);
+ sy = (y0 < y1) ? (1) : (-1);
+ err = dx + dy;
+
+ for (;;) {
+ pxPset(x0, y0, col);
+
+ if (x0 == x1 && y0 == y1)
+ break;
+
+ e2 = 2 * err;
+ if (e2 >= dy) {
+ err += dy;
+ x0 += sx;
+ }
+ if (e2 <= dx) {
+ err += dx;
+ y0 += sy;
+ }
+ }
+}
+
+void
_pxSpr(const PxSprArgs *args)
{
int w = (args->w < 0) ? (int)(args->spr->w) : (args->w);
diff --git a/src/px.h b/src/px.h
index 713f245..9eb9610 100644
--- a/src/px.h
+++ b/src/px.h
@@ -88,6 +88,9 @@ void pxRect(int x0, int y0, int x1, int y1, PxCol col);
/* Draw filled rectangle. */
void pxRectfill(int x0, int y0, int x1, int y1, PxCol col);
+/* Draw line. */
+void pxLine(int x0, int y0, int x1, int y1, PxCol col);
+
/* Draw sprite. */
#define pxSpr(...) _pxSpr(&(const PxSprArgs){ \
.x=0, .y=0, .ix=0, .iy=0, .w=-1, .h=-1, .flip_x=false, .flip_x=false, \