main.c (2572B)
1 #include <u.h> 2 #include <libc.h> 3 #include <thread.h> 4 #include <draw.h> 5 #include <mouse.h> 6 #include <cursor.h> 7 8 #include "noise.h" 9 10 #include "config.h" 11 12 Scene *cs; 13 14 struct FPSWidget { 15 char buf[256]; 16 Image *img, *fg; 17 int count; 18 Rectangle r; 19 } fps; 20 21 int killdraw; 22 23 24 void 25 procdraw(void *v) 26 { 27 long OT, NT, DT; 28 Scene *sp = v; 29 threadsetname("draw-loop"); 30 OT = nsec(); 31 for (killdraw = 0; killdraw == 0;) { 32 lockdisplay(display); 33 if (sp->draw != nil) sp->draw(sp->aux); 34 draw(screen, rectaddpt(fps.r, screen->r.min), fps.img, nil, ZP); 35 fps.count++; 36 NT = nsec(); 37 DT = NT - OT; 38 if (DT >= 1000000000) { 39 OT = NT; 40 snprint(fps.buf, 256, "%d", fps.count); 41 fps.r.max.x = stringbg(fps.img, ZP, fps.fg, ZP, font, 42 fps.buf, display->black, ZP).x; 43 fps.count = 0; 44 } 45 flushimage(display, 1); 46 unlockdisplay(display); 47 } 48 } 49 50 char * 51 getmenulabel(int n) 52 { 53 if (plist[n] == nil) return nil; 54 return plist[n]->name; 55 } 56 57 void 58 threadmain(int, char **) 59 { 60 cs = plist[0]; 61 62 int dtid; 63 Mousectl *mctl; 64 Mouse mouse; 65 int resize[2]; 66 initdraw(nil, nil, "ШУМ"); 67 display->locking = 1; 68 mctl = initmouse(nil, screen); 69 70 Menu menu = { 71 nil, 72 getmenulabel, 73 0 74 }; 75 76 fps.img = allocimage(display, Rect(0, 0, 512, font->height), RGB24, 0, DBlack); 77 fps.fg = allocimage(display, Rect(0, 0, 1, 1), RGB24, 1, DYellow); 78 fps.r = fps.img->r; 79 fps.r.max.x = 0; 80 81 draw(screen, screen->r, display->black, nil, ZP); 82 flushimage(display, Refnone); 83 unlockdisplay(display); 84 85 if (cs->init != nil) cs->init(cs->aux); 86 dtid = proccreate(procdraw, cs, 8 * 1024); 87 88 Alt alts[4] = { 89 {mctl->c, &mouse, CHANRCV}, 90 {mctl->resizec, resize, CHANRCV}, 91 {nil, nil, CHANEND}, 92 }; 93 for (;;) { 94 switch(alt(alts)) { 95 case 0: // mouse 96 if (mouse.buttons == 1) { 97 lockdisplay(display); 98 int hit = menuhit(1, mctl, &menu, nil); 99 unlockdisplay(display); 100 if (hit >= 0) { 101 // threadkill(dtid); 102 killdraw = 1; 103 lockdisplay(display); 104 unlockdisplay(display); 105 if (cs->stop != nil) cs->stop(cs->aux); 106 cs = plist[hit]; 107 if (cs->init != nil) cs->init(cs->aux); 108 dtid = proccreate(procdraw, cs, 8 * 1024); 109 } 110 } 111 break; 112 case 1: // resize 113 // threadkill(dtid); 114 killdraw = 1; 115 lockdisplay(display); 116 if (getwindow(display, Refnone) < 0) 117 sysfatal("resize failed: %r"); 118 119 draw(screen, screen->r, display->black, nil, ZP); 120 unlockdisplay(display); 121 122 if (cs->stop != nil) cs->stop(cs->aux); 123 if (cs->init != nil) cs->init(cs->aux); 124 dtid = proccreate(procdraw, cs, 8 * 1024); 125 126 break; 127 case 2: //noop 128 break; 129 } 130 } 131 }