commit c12549a0345b8fbf781680044d79651bbbf50a02
Author: glenda <glenda@9front.local>
Date: Fri, 20 Aug 2021 08:57:06 +0000
lets make some noise
Diffstat:
A | noise1.c | | | 62 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | noise2.c | | | 77 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 139 insertions(+), 0 deletions(-)
diff --git a/noise1.c b/noise1.c
@@ -0,0 +1,61 @@
+/* naive implementation */
+
+#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, GREY8, 0, DBlack);
+ bn = Dx(Ibuf->r) * Dy(Ibuf->r);
+ buf = realloc(buf, bn);
+}
+
+void
+threadmain(int, char**)
+{
+ int rd;
+ int fps, fn;
+ long t, ot;
+ Mousectl *mctl;
+ Image *It;
+ char text[16];
+ initdraw(nil, nil, "noise");
+ mctl = initmouse(0, screen);
+ rd = open("/dev/random", OREAD);
+ It = allocimage(display, Rect(0,0,1,1), RGB24, 1, DYellow);
+ ot = time(0);
+ fn = 0;
+ fps = 0;
+ resize();
+ for(;;){
+ if (nbrecv(mctl->resizec, nil) != 0) {
+ if (getwindow(display, Refnone) < 0)
+ sysfatal("resize failed: %r");
+ resize();
+ }
+ while(nbrecv(mctl->c, nil) != 0);
+ readn(rd, buf, bn);
+ 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);
+ }
+}
+\ No newline at end of file
diff --git a/noise2.c b/noise2.c
@@ -0,0 +1,76 @@
+/* attempt at optimisation */
+
+#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, GREY8, 0, DBlack);
+ bn = Dx(Ibuf->r) * Dy(Ibuf->r);
+ 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);
+ }
+}
+\ No newline at end of file