noise

experiments with video performance in plan9
Log | Files | Refs

noise1.c (899B)


      1 /* naive implementation */
      2 
      3 #include <u.h>
      4 #include <libc.h>
      5 
      6 #include <thread.h>
      7 #include <draw.h>
      8 #include <mouse.h>
      9 
     10 #include "noise.h"
     11 
     12 Image *Ibuf;
     13 long bn;
     14 u8int *buf;
     15 
     16 void
     17 resize(void)
     18 {
     19 	freeimage(Ibuf);
     20 	Ibuf = allocimage(display, screen->r, GREY8, 0, DBlack);
     21 	bn = Dx(Ibuf->r) * Dy(Ibuf->r);
     22 	buf = realloc(buf, bn);
     23 }
     24 
     25 void
     26 threadmain(int, char**)
     27 {
     28 	int rd;
     29 	Mousectl *mctl;
     30 	Image *It;
     31 	initdraw(nil, nil, "noise");
     32 	mctl = initmouse(0, screen);
     33 	rd = open("/dev/random", OREAD);
     34 	It = allocimage(display, Rect(0,0,1,1), RGB24, 1, DYellow);
     35 	resize();
     36 	for(;;){
     37 		if (nbrecv(mctl->resizec, nil) != 0) {
     38 			if (getwindow(display, Refnone) < 0)
     39 				sysfatal("resize failed: %r");
     40 			resize();
     41 		}
     42 		while(nbrecv(mctl->c, nil) != 0);
     43 		readn(rd, buf, bn);
     44 		loadimage(Ibuf, Ibuf->r, buf, bn);
     45 		draw(screen, screen->r, Ibuf, 0, Ibuf->r.min);
     46 		drawfps(It);
     47 		flushimage(display, 1);
     48 	}
     49 }