commit ee9b0e36a68efd9c821a0200df1b608542cb9bbd
parent c12549a0345b8fbf781680044d79651bbbf50a02
Author: glenda <glenda@9front.local>
Date: Fri, 20 Aug 2021 09:22:08 +0000
add version with colors
Diffstat:
A | mkfile | | | 7 | +++++++ |
A | noise2rgb.c | | | 76 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 83 insertions(+), 0 deletions(-)
diff --git a/mkfile b/mkfile
@@ -0,0 +1,7 @@
+</$objtype/mkfile
+
+TARG=noise1 noise2 noise2rgb
+OFILES=
+BIN=/$objtype/bin
+
+</sys/src/cmd/mkmany
diff --git a/noise2rgb.c b/noise2rgb.c
@@ -0,0 +1,76 @@
+/* now in color */
+
+#include <u.h>
+#include <libc.h>
+
+#include <thread.h>
+#include <draw.h>
+#include <mouse.h>
+
+Image *Ibuf;
+long bn;
+u8int *buf;
+
+void
+resize(void)
+{
+ freeimage(Ibuf);
+ Ibuf = allocimage(display, screen->r, RGB24, 0, DBlack);
+ bn = Dx(Ibuf->r) * Dy(Ibuf->r) * sizeof(u32int);
+ buf = realloc(buf, bn);
+}
+
+void
+procread(void *v)
+{
+ int rd;
+ Channel *c;
+ c = v;
+ rd = open("/dev/random", OREAD);
+ while(1){
+ readn(rd, buf, bn);
+ if (send(c, nil) < 0) break;
+ };
+ close(rd);
+}
+
+void
+threadmain(int, char**)
+{
+ int fps, fn;
+ long t, ot;
+ Mousectl *mctl;
+ Image *It;
+ char text[16];
+ Channel *c;
+ initdraw(nil, nil, "noise");
+ mctl = initmouse(0, screen);
+ It = allocimage(display, Rect(0,0,1,1), RGB24, 1, DYellow);
+ ot = time(0);
+ fn = 0;
+ fps = 0;
+ resize();
+ c = chancreate(sizeof(int), 0);
+ proccreate(procread, c, 8 * 1024);
+ for(;;){
+ if (nbrecv(mctl->resizec, nil) != 0) {
+ if (getwindow(display, Refnone) < 0)
+ sysfatal("resize failed: %r");
+ resize();
+ }
+ while(nbrecv(mctl->c, nil) != 0);
+ recv(c, nil);
+ loadimage(Ibuf, Ibuf->r, buf, bn);
+ draw(screen, screen->r, Ibuf, 0, Ibuf->r.min);
+ fn++;
+ t = time(0);
+ if (t > ot) {
+ ot = t;
+ fps = fn;
+ fn = 0;
+ };
+ snprint(text, 16, "fps: %d", fps);
+ string(screen, screen->r.min, It, ZP, font, text);
+ flushimage(display, 1);
+ }
+}