From 0b0726f3a990ee241fd93b5c3f53631bfbb4c455 Mon Sep 17 00:00:00 2001 From: rodri Date: Fri, 12 Jun 2020 21:57:48 +0000 Subject: layout done. implemented some error-checking wrappers and started work on layers. --- alloc.c | 44 +++++++++++++++++++++++++ dat.h | 7 ++++ fns.h | 3 ++ layer.c | 5 +++ main.c | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ mkfile | 20 ++++++++++++ 6 files changed, 193 insertions(+) create mode 100644 alloc.c create mode 100644 dat.h create mode 100644 fns.h create mode 100644 layer.c diff --git a/alloc.c b/alloc.c new file mode 100644 index 0000000..2589e89 --- /dev/null +++ b/alloc.c @@ -0,0 +1,44 @@ +#include +#include +#include + +void* +emalloc(ulong n) +{ + void *p; + + p = malloc(n); + if(p == nil) + sysfatal("malloc: %r"); + setmalloctag(p, getcallerpc(&n)); + return p; +} + +void* +erealloc(void *p, ulong n) +{ + void *np; + + np = realloc(p, n); + if(np == nil){ + if(n == 0) + return nil; + sysfatal("realloc: %r"); + } + if(p == nil) + setmalloctag(np, getcallerpc(&p)); + else + setrealloctag(np, getcallerpc(&p)); + return np; +} + +Image* +eallocimage(Display *d, Rectangle r, ulong chan, int repl, ulong col) +{ + Image *i; + + i = allocimage(d, r, chan, repl, col); + if(i == nil) + sysfatal("allocimage: %r"); + return i; +} diff --git a/dat.h b/dat.h new file mode 100644 index 0000000..5426da9 --- /dev/null +++ b/dat.h @@ -0,0 +1,7 @@ +typedef struct Layer Layer; + +struct Layer +{ + RFrame rf; + Image *canvas; +}; diff --git a/fns.h b/fns.h new file mode 100644 index 0000000..ece9b68 --- /dev/null +++ b/fns.h @@ -0,0 +1,3 @@ +void *emalloc(ulong); +void *erealloc(void*, ulong); +Image *eallocimage(Display*, Rectangle, ulong, int, ulong); diff --git a/layer.c b/layer.c new file mode 100644 index 0000000..5673183 --- /dev/null +++ b/layer.c @@ -0,0 +1,5 @@ +#include +#include +#include +#include + diff --git a/main.c b/main.c index 42c2869..67b2832 100644 --- a/main.c +++ b/main.c @@ -4,6 +4,74 @@ #include #include #include +#include +#include "dat.h" +#include "fns.h" + +enum { + CBlack, + CWhite, + NCOLOR +}; + +RFrame worldrf; +Image *pal[NCOLOR]; + +Point +toscreen(Point2 p) +{ + p = invrframexform(p, worldrf); + return Pt(p.x,p.y); +} + +Point2 +fromscreen(Point p) +{ + return rframexform(Pt2(p.x,p.y,1), worldrf); +} + +void +initpalette(void) +{ + pal[CBlack] = eallocimage(display, Rect(0,0,1,1), screen->chan, 1, DBlack); + pal[CWhite] = eallocimage(display, Rect(0,0,1,1), screen->chan, 1, DWhite); +} + +void +redraw(void) +{ + lockdisplay(display); + draw(screen, screen->r, pal[CBlack], nil, ZP); + flushimage(display, 1); + unlockdisplay(display); +} + +void +resized(void) +{ + lockdisplay(display); + if(getwindow(display, Refnone) < 0) + sysfatal("resize failed"); + unlockdisplay(display); + worldrf.p = Pt2(screen->r.min.x,screen->r.max.y,1); + redraw(); +} + +void +mouse(Mousectl *mc, Keyboardctl *) +{ + +} + +void +key(Rune r) +{ + switch(r){ + case Kdel: + case 'q': + threadexitsall(nil); + } +} void usage(void) @@ -15,7 +83,53 @@ usage(void) void threadmain(int argc, char *argv[]) { + Mousectl *mc; + Keyboardctl *kc; + Rune r; + ARGBEGIN{ default: usage(); }ARGEND; + if(argc > 0) + usage(); + + if(initdraw(nil, nil, nil) < 0) + sysfatal("initdraw: %r"); + if((mc = initmouse(nil, screen)) == nil) + sysfatal("initmouse: %r"); + if((kc = initkeyboard(nil)) == nil) + sysfatal("initkeyboard: %r"); + + worldrf.p = Pt2(screen->r.min.x,screen->r.max.y,1); + worldrf.bx = Vec2(1, 0); + worldrf.by = Vec2(0,-1); + initpalette(); + + display->locking = 1; + unlockdisplay(display); + redraw(); + + for(;;){ + enum { MOUSE, RESIZE, KEYBOARD }; + Alt a[] = { + {mc->c, &mc->Mouse, CHANRCV}, + {mc->resizec, nil, CHANRCV}, + {kc->c, &r, CHANRCV}, + {nil, nil, CHANEND} + }; + + switch(alt(a)){ + case MOUSE: + mouse(mc, kc); + break; + case RESIZE: + resized(); + break; + case KEYBOARD: + key(r); + break; + } + + redraw(); + } } diff --git a/mkfile b/mkfile index 28d904b..90d5b2e 100644 --- a/mkfile +++ b/mkfile @@ -3,6 +3,26 @@ BIN=/$objtype/bin TARG=puppeteer OFILES=\ + alloc.$O\ + layer.$O\ main.$O\ +HFILES=\ + dat.h\ + fns.h\ + libgeometry/geometry.h\ + +LIB=\ + libgeometry/libgeometry.a$O\ + +CFLAGS=$CFLAGS -Ilibgeometry +