about summary refs log tree commit diff stats
path: root/p9c/pixel
diff options
context:
space:
mode:
authorelioat <elioat@tilde.institute>2024-08-25 19:25:59 -0400
committerelioat <elioat@tilde.institute>2024-08-25 19:25:59 -0400
commitd325adb321e3330a0f3e706dc0cd9eb2b2179c8a (patch)
tree480b9eb3cdede1fd010dbb47d2d4c73eb2dd0b07 /p9c/pixel
parenta04981476d9f3d282f76969bd55f7f32d65dda06 (diff)
downloadtour-d325adb321e3330a0f3e706dc0cd9eb2b2179c8a.tar.gz
*
Diffstat (limited to 'p9c/pixel')
-rwxr-xr-xp9c/pixel/build.sh4
-rw-r--r--p9c/pixel/grid.c49
2 files changed, 53 insertions, 0 deletions
diff --git a/p9c/pixel/build.sh b/p9c/pixel/build.sh
new file mode 100755
index 0000000..0bbd699
--- /dev/null
+++ b/p9c/pixel/build.sh
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+9 9c grid.c
+9 9l grid.o -o grid
\ No newline at end of file
diff --git a/p9c/pixel/grid.c b/p9c/pixel/grid.c
new file mode 100644
index 0000000..c0438c1
--- /dev/null
+++ b/p9c/pixel/grid.c
@@ -0,0 +1,49 @@
+#include <u.h>
+#include <libc.h>
+#include <draw.h>
+#include <event.h>
+
+Image *back;  // Background image for drawing
+int gridsize = 16;  // Size of each grid cell
+int gridwidth = 10;  // Number of grid columns
+int gridheight = 10;  // Number of grid rows
+Mouse mouse;
+
+void drawgrid(void) {
+    int x, y;
+    for (x = 0; x <= gridwidth; x++) {
+        line(back, Pt(x * gridsize, 0), Pt(x * gridsize, gridheight * gridsize), Endsquare, Endsquare, 0, display->black, ZP);
+    }
+    for (y = 0; y <= gridheight; y++) {
+        line(back, Pt(0, y * gridsize), Pt(gridwidth * gridsize, y * gridsize), Endsquare, Endsquare, 0, display->black, ZP);
+    }
+}
+
+void fillcell(int x, int y, ulong color) {
+    Rectangle r = Rect(x * gridsize, y * gridsize, (x + 1) * gridsize, (y + 1) * gridsize);
+    draw(back, r, display->black, nil, ZP);
+    flushimage(display, 1);
+}
+
+void main(void) {
+    if (initdraw(0, 0, "Pixel Art Tool") < 0)
+        sysfatal("initdraw failed: %r");
+
+    back = screen;
+    drawgrid(); // Draw the initial grid
+    flushimage(display, 1);
+
+    for (;;) {
+        mouse = emouse();  // Fetch mouse event
+
+        if (mouse.buttons & 1) {  // Left mouse button clicked
+            int cellx = mouse.xy.x / gridsize;
+            int celly = mouse.xy.y / gridsize;
+            fillcell(cellx, celly, display->black->chan);  // Fill the clicked cell
+        }
+
+        if (ekbd() == 'q') {  // Check for 'q' key press to exit
+            exits(0);
+        }
+    }
+}