about summary refs log blame commit diff stats
path: root/p9c/pixel/grid.c
blob: c0438c1629b6e1dfac8ce197077127327d64b2b3 (plain) (tree)
















































                                                                                                                              
#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);
        }
    }
}