about summary refs log tree commit diff stats
path: root/p9c/pixel/grid.c
blob: c0438c1629b6e1dfac8ce197077127327d64b2b3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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);
        }
    }
}