diff options
-rw-r--r-- | alloc.c | 44 | ||||
-rw-r--r-- | dat.h | 7 | ||||
-rw-r--r-- | fns.h | 3 | ||||
-rw-r--r-- | layer.c | 5 | ||||
-rw-r--r-- | main.c | 114 | ||||
-rw-r--r-- | mkfile | 20 |
6 files changed, 193 insertions, 0 deletions
@@ -0,0 +1,44 @@ +#include <u.h> +#include <libc.h> +#include <draw.h> + +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; +} @@ -0,0 +1,7 @@ +typedef struct Layer Layer; + +struct Layer +{ + RFrame rf; + Image *canvas; +}; @@ -0,0 +1,3 @@ +void *emalloc(ulong); +void *erealloc(void*, ulong); +Image *eallocimage(Display*, Rectangle, ulong, int, ulong); @@ -0,0 +1,5 @@ +#include <u.h> +#include <libc.h> +#include <draw.h> +#include <geometry.h> + @@ -4,6 +4,74 @@ #include <draw.h> #include <mouse.h> #include <keyboard.h> +#include <geometry.h> +#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(); + } } @@ -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 + </sys/src/cmd/mkone + +libgeometry/libgeometry.a$O: + cd libgeometry + mk install + +clean nuke:V: + rm -f *.[$OS] [$OS].??* $TARG + @{cd libgeometry; mk $target} |