From 74ad09082610d27ef278fdbed892897f704168d6 Mon Sep 17 00:00:00 2001 From: rodri Date: Sun, 14 Jun 2020 12:26:08 +0000 Subject: create a canvas and layers with the mouse. fixed some issues. --- canvas.c | 1 + fns.h | 8 ++++++++ layer.c | 1 + main.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++------- mkfile | 1 + 5 files changed, 64 insertions(+), 7 deletions(-) diff --git a/canvas.c b/canvas.c index a017fb0..abd0089 100644 --- a/canvas.c +++ b/canvas.c @@ -27,4 +27,5 @@ newcanvas(Point2 p, Rectangle r, ulong chan) memset(&c->layers, 0, sizeof(Layer)); c->layers.next = &c->layers; c->layers.prev = &c->layers; + return c; } diff --git a/fns.h b/fns.h index ece9b68..6667415 100644 --- a/fns.h +++ b/fns.h @@ -1,3 +1,11 @@ +/* alloc */ void *emalloc(ulong); void *erealloc(void*, ulong); Image *eallocimage(Display*, Rectangle, ulong, int, ulong); + +/* canvas */ +Canvas *newcanvas(Point2, Rectangle, ulong); + +/* layer */ +Layer *newlayer(Canvas*); +void rmlayer(Layer*); diff --git a/layer.c b/layer.c index d238b99..b8a2742 100644 --- a/layer.c +++ b/layer.c @@ -19,6 +19,7 @@ newlayer(Canvas *c) l->next = &c->layers; c->layers.prev->next = l; c->layers.prev = l; + return l; } void diff --git a/main.c b/main.c index ad42fbb..5cb401d 100644 --- a/main.c +++ b/main.c @@ -41,18 +41,18 @@ initpalette(void) } Image* -gencheckerboard(int w, int h) +mkcheckerboard(int w, int h) { Image *i, *dark, *light; int x, y; i = eallocimage(display, Rect(0,0,2*w,2*h), screen->chan, 1, DNofill); - dark = eallocimage(display, Rect(0,0,w,h), screen->chan, 0, DBlack&~0x7f); - light = eallocimage(display, Rect(0,0,w,h), screen->chan, 0, DBlack&~0xdf); + dark = eallocimage(display, Rect(0,0,w,h), screen->chan, 0, 0x333333FF); + light = eallocimage(display, Rect(0,0,w,h), screen->chan, 0, 0xEEEEEEFF); - for(y = 0; y < h; y += h) - for(x = 0; x < w; x += w) - draw(i, Rect(x,y,x+w,y+h), (x+y) % 2? light: dark, ZP); + for(y = 0; y < i->r.max.y; y += h) + for(x = 0; x < i->r.max.x; x += w) + draw(i, Rect(x,y,x+w,y+h), (x/w + y/h) % 2 == 0? light: dark, nil, ZP); freeimage(dark); freeimage(light); @@ -68,7 +68,11 @@ drawlayer(Canvas *c, Layer *l) void drawcanvas(Canvas *c) { + Layer *l; + for(l = c->layers.next; l != &c->layers; l = l->next) + drawlayer(c, l); + draw(screen, screen->r, c->image, nil, ZP); } void @@ -76,6 +80,9 @@ redraw(void) { lockdisplay(display); draw(screen, screen->r, pal[PCBlack], nil, ZP); + draw(screen, curcanvas == nil? screen->r: rectaddpt(curcanvas->image->r, toscreen(curcanvas->p)), background, nil, ZP); + if(curcanvas != nil) + drawcanvas(curcanvas); flushimage(display, 1); unlockdisplay(display); } @@ -92,9 +99,47 @@ resized(void) } void -mouse(Mousectl *mc, Keyboardctl *) +rmb(Mousectl *mc, Keyboardctl *kc) { + enum { + NEW, + NEWLAYER + }; + static char *items[] = { + [NEW] "new", + [NEWLAYER] "new layer", + nil + }; + static Menu menu = { .item = items }; + char buf[256], chanstr[9+1], *s; + ulong w, h, chan; + Point2 cpos; + + switch(menuhit(3, mc, &menu, nil)){ + case NEW: + if(curcanvas != nil) + break; + snprint(buf, sizeof buf, "%d %d %s", Dx(screen->r), Dy(screen->r), chantostr(chanstr, screen->chan)); + enter("w h chan", buf, sizeof buf, mc, kc, nil); + w = strtoul(buf, &s, 10); + h = strtoul(s, &s, 10); + chan = strtochan(s); + cpos = Pt2(Dx(screen->r)/2 - w/2,Dy(screen->r)/2 + h/2,1); + curcanvas = newcanvas(cpos, Rect(0,0,w,h), chan); + break; + case NEWLAYER: + if(curcanvas == nil) + break; + newlayer(curcanvas); + break; + } +} +void +mouse(Mousectl *mc, Keyboardctl *kc) +{ + if((mc->buttons&4) != 0) + rmb(mc, kc); } void @@ -138,6 +183,7 @@ threadmain(int argc, char *argv[]) worldrf.bx = Vec2(1, 0); worldrf.by = Vec2(0,-1); initpalette(); + background = mkcheckerboard(4, 4); display->locking = 1; unlockdisplay(display); diff --git a/mkfile b/mkfile index 90d5b2e..e83852c 100644 --- a/mkfile +++ b/mkfile @@ -5,6 +5,7 @@ TARG=puppeteer OFILES=\ alloc.$O\ layer.$O\ + canvas.$O\ main.$O\ HFILES=\ -- cgit v1.2.3