ref: c40d66e369235ec021af570553b89bd101a42109
author: glenda <glenda@9front.local>
date: Sat Jan 2 05:00:38 EST 2021
"init"
--- /dev/null
+++ b/canvas.c
@@ -1,0 +1,42 @@
+#include <u.h>
+#include <libc.h>
+#include <draw.h>
+#include "dormer.h"
+
+Canvas*
+canvas(int sn, int tn, long bgcol)
+{
+ int size;
+ size = sizeof(Canvas)
+ + sizeof(Sprite) * sn
+ + sizeof(Text) * tn;
+ Canvas *c = malloc(size);
+ c->si = 0;
+ c->ti = 0;
+ c->spritev = (Sprite*)(c+1);
+ c->textv = (Text*)(c->spritev+sn);
+ c->bg = allocimagemix(display, bgcol, bgcol);
+
+ vs.canvas = c;
+ return c;
+}
+
+void
+present(void)
+{
+ if(getwindow(display, Refmesg) < 0)
+ fprint(2, "can't reattach to window");
+ draw(screen, screen->r, vs.canvas->bg, 0, ZP);
+
+ int i;
+ for(i=0;i<vs.canvas->si;++i){
+ Point p = {vs.canvas->spritev[i].x, vs.canvas->spritev[i].y};
+ draw(screen, rectaddpt(screen->r, p), vs.canvas->spritev[i].image, 0, ZP);
+ }
+ for(i=0;i<vs.canvas->ti;++i){
+ Point p = {vs.canvas->textv[i].x, vs.canvas->textv[i].y};
+ draw(screen, rectaddpt(screen->r, p), vs.canvas->textv[i].image, 0, ZP);
+ }
+
+ flushimage(display, 1);
+}
\ No newline at end of file
--- /dev/null
+++ b/dormer.c
@@ -1,0 +1,42 @@
+#include <u.h>
+#include <libc.h>
+#include <draw.h>
+#include <event.h>
+#include "dormer.h"
+
+static ulong keys;
+static Event e;
+
+void
+eresized(int new)
+{
+ USED(new);
+ present();
+}
+
+void
+winit()
+{
+ if(initdraw(0, 0, dm.title) < 0){
+ fprint(2, "%s: initdraw failed: %r\n", dm.title);
+ exits("initdraw");
+ }
+
+ keys = Emouse|Ekeyboard;
+ einit(keys);
+}
+
+void
+readev()
+{
+ if(ecanread(keys)){
+ dm.ev = eread(keys, &e);
+ dm.key = e.kbdc;
+ dm.btn = e.mouse.buttons;
+ dm.x = e.mouse.xy.x;
+ dm.y = e.mouse.xy.y;
+ }else{
+ dm.ev = 0;
+ }
+}
+
--- /dev/null
+++ b/dormer.h
@@ -1,0 +1,59 @@
+typedef struct Dormer Dormer;
+typedef struct Visage Visage;
+typedef struct Canvas Canvas;
+typedef struct Sprite Sprite;
+typedef struct Text Text;
+
+struct Dormer
+{
+ void *dis;
+ void *win;
+ char *title;
+ int ev;
+ int key;
+ int btn;
+ int x, y;
+ int width;
+ int height;
+ int screen;
+ int cursor;
+} dm;
+
+struct Visage
+{
+ void *ctx;
+ Canvas *canvas;
+} vs;
+
+struct Sprite
+{
+ void *image;
+ int x, y;
+};
+
+struct Text
+{
+ void *image;
+ void *ft;
+ char *str;
+ int x, y;
+};
+
+struct Canvas
+{
+ void *bg;
+ Sprite *spritev;
+ Text *textv;
+ int si, ti;
+};
+
+extern void winit(void);
+extern void readev(void);
+
+extern void present(void);
+extern Canvas *canvas(int sn, int tn, long fgcol);
+extern Sprite *mksprite(Canvas *c, char *path);
+extern void mvsprite(Sprite *s, int x, int y);
+extern Text *mktext(Canvas *c, char *str, char *ft, int x, int y, long bgcol);
+
+
--- /dev/null
+++ b/mkfile
@@ -1,0 +1,11 @@
+</$objtype/mkfile
+TARG=test
+OFILES=\
+ test.$O\
+ dormer.$O\
+ canvas.$O\
+ sprite.$O\
+ text.$O
+HFILES=\
+ dormer.h
+</sys/src/cmd/mkone
\ No newline at end of file
--- /dev/null
+++ b/sprite.c
@@ -1,0 +1,20 @@
+#include <u.h>
+#include <libc.h>
+#include <draw.h>
+#include <stdio.h>
+#include "dormer.h"
+
+Sprite*
+mksprite(Canvas *c, char *path)
+{
+ c->spritev[c->si].image = readimage(display, open(path, OREAD), 0);
+ Sprite *s = &c->spritev[c->si++];
+ return s;
+}
+
+void
+mvsprite(Sprite *s, int x, int y)
+{
+ s->x = x;
+ s->y = y;
+}
--- /dev/null
+++ b/test.c
@@ -1,0 +1,54 @@
+#include <u.h>
+#include <libc.h>
+#include <draw.h>
+#include <event.h>
+#include <keyboard.h>
+#include "dormer.h"
+
+/* TODO: resize and rotate sprites,
+ * handle rgba,
+ * play audio,
+ * timer (?),
+ * animate sprites
+ */
+
+void
+main()
+{
+ int y = 300;
+
+ winit();
+
+ Canvas *c = canvas(3, 2, DBlue);
+ Sprite *black = mksprite(c, "res/glenda4.im");
+ Sprite *white = mksprite(c, "res/glenda.im");
+ Sprite *space = mksprite(c, "res/glenda3.im");
+ mvsprite(black, 100, 100);
+ mvsprite(white, 200, y--);
+ mvsprite(space, 400, 100);
+
+ mktext(c, "helo rofik", "/lib/font/bit/lucidasans/unicode.7.font", 100, 100, DRed);
+ mktext(c, "second", "/lib/font/bit/lucidasans/unicode.7.font", 0, 0, DTransparent);
+
+ present();
+
+ for(;;){
+ readev();
+ switch(dm.ev){
+ case 0:
+ break;
+ case Emouse:
+ break;
+ case Ekeyboard:
+ if(dm.key == 'q'){
+ free(c);
+ exits(0);
+ }
+ if(dm.key == 'w'){
+ mvsprite(white, 200, y--);
+ present();
+ }
+ break;
+ }
+ }
+}
--- /dev/null
+++ b/text.c
@@ -1,0 +1,23 @@
+#include <u.h>
+#include <libc.h>
+#include <draw.h>
+#include "dormer.h"
+
+Text*
+mktext(Canvas *c, char *str, char *ft, int x, int y, long fgcol)
+{
+ Image *fg;
+ Image *im;
+ c->textv[c->ti].ft = openfont(display, ft);
+ c->textv[c->ti].x = x;
+ c->textv[c->ti].y = y;
+ fg = allocimage(display, Rect(0,0,1,1), CMAP8, 1, fgcol);
+
+ Point size = stringsize(c->textv[c->ti].ft, str);
+ c->textv[c->ti].image = allocimage(display, (Rectangle){(Point){0,0},size}, RGBA32, 0, DTransparent);
+ im = c->textv[c->ti].image;
+
+ string(im, (Point){0,0}, fg, ZP, c->textv[c->ti].ft, str);
+ Text *tx = &c->textv[c->ti++];
+ return tx;
+}