dnoise.c (1024B)
1 /* simple/naive noise fill scene */ 2 3 #include <u.h> 4 #include <libc.h> 5 #include <draw.h> 6 7 #include "noise.h" 8 9 #include "dnoise.h" 10 11 struct DNoise_State { 12 uchar *buf; 13 Image *Ibuf; 14 long n; 15 int f; 16 } dns; 17 18 void dn_init(void *) 19 { 20 dns.n = Dx(screen->r) * Dy(screen->r) * sizeof(u32int); 21 dns.buf = malloc(dns.n); 22 dns.f = open("/dev/random", OREAD); 23 if (dns.buf == nil) sysfatal("dn_init failed to malloc buf: %r"); 24 if (dns.f < 0) sysfatal("dn_init couldn't open /dev/random: %r"); 25 lockdisplay(display); 26 dns.Ibuf = allocimage(display, 27 Rect(0, 0, Dx(screen->r), Dy(screen->r)), RGB24, 0, DBlack); 28 unlockdisplay(display); 29 } 30 31 void dn_draw(void *) 32 { 33 read(dns.f, dns.buf, dns.n); 34 loadimage(dns.Ibuf, dns.Ibuf->r, dns.buf, dns.n); 35 draw(screen, screen->r, dns.Ibuf, nil, ZP); 36 } 37 38 void dn_stop(void *) 39 { 40 close(dns.f); 41 dns.n = 0; 42 close(dns.f); 43 free(dns.buf); 44 dns.buf = nil; 45 lockdisplay(display); 46 freeimage(dns.Ibuf); 47 unlockdisplay(display); 48 } 49 50 Scene DNoiseScene = { 51 "dumb noise", 52 dn_init, 53 dn_draw, 54 dn_stop, 55 nil};