From mboxrd@z Thu Jan 1 00:00:00 1970 To: 9fans@cse.psu.edu From: andrey mirtchovski MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="upas-zwucqaziljqhynhafwwwopmzkm" Message-Id: <20020117172218.2BECC1998C@mail.cse.psu.edu> Subject: [9fans] people who write little useless graphical programs should be shot Date: Thu, 17 Jan 2002 09:09:20 -0700 Topicbox-Message-UUID: 3e833022-eaca-11e9-9e20-41e7f4b1d025 This is a multi-part message in MIME format. --upas-zwucqaziljqhynhafwwwopmzkm Content-Disposition: inline Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit i have a laptop here running plan9.. i'm quite happy with it most of the time and i'm not happy with it when it hangs during window resize (when running p9 locally).. i suspected the vga driver so i wrote a small graphical program to see if it will force it to hang by displaying lines on the screen over and over again. i indeed succeeded in crashing it. then, last night, i decided to 'beautify' the program and foo.c was born.. it displays moving lines in a window.. it provides the user with the wonderful options to do 'more', 'less' and 'exit'.. it was hacked rather quickly to accomodate more than one line at a time, and this shows in the code quality... the performance is satisfactory even when running on a pure terminal (8bpp)... of course higher bpp means more visually pleasing lines, but the performance tradeoff is something you should decide for yourselves :) enjoy (if you enjoy such things :) andrey ps: could someone please explain how to use '|fmt' in acme mail? i have to press enter every line in order to make the mail somewhat readable :) --upas-zwucqaziljqhynhafwwwopmzkm Content-Disposition: attachment; filename=foo.c Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit #include #include #include #include Rectangle r; Image *img[256]; typedef struct { Point p1; Point p2; int b; int db; int p1dx; int p1dy; int p2dx; int p2dy; } mline; typedef struct mq mq; struct mq { mline *node; mq *next; }; void eresized(int new) { if(new && getwindow(display, Refnone) < 0) { fprint(2, "bez: can't reattach to window: %r\n"); exits("resized"); } draw(screen, screen->r, display->black, nil, ZP); r = screen->r; } char *buttons[] = { "more", "less", "exit", 0 }; Menu menu = { buttons }; void init(mline *m) { m->p1.x = r.min.x + nrand(r.max.x - r.min.x); m->p2.x = r.min.x + nrand(r.max.x - r.min.x); m->p1.y = r.min.y + nrand(r.max.y - r.min.y); m->p2.y = r.min.y + nrand(r.max.y - r.min.y); m->p1dx = (int)((frand() - 0.5)*5); m->p1dy = (int)((frand() - 0.5)*5); m->p2dx = (int)((frand() - 0.5)*5); m->p2dy = (int)((frand() - 0.5)*5); m->b = 1; m->db = 1; } void main(int argc, char **argv) { int i, b = 1, db = 1; Mouse ms; Point p1, p2; mq q; mq *p; mline *m; srand(time(0)); if(initdraw(nil, nil, "bez") < 0) sysfatal("initdraw failed: %r"); /* allocate colors */ for(i = 0; i < 256; i++) { img[i] = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, (i<<24)|(i<<16)|(i<<8)|0xFF); } r = screen->r; q.node = (mline *)malloc(sizeof (mline)); q.next = nil; init(q.node); einit(Emouse); eresized(0); for(;;) { if(ecanmouse()) { ms = emouse(); if(ms.buttons&4) switch(emenuhit(3, &ms, &menu)) { case 0: /* more */ p=&q; while(p->next != nil) p = p->next; p->next = (mq *)malloc(sizeof (mq)); p->next->node = (mline *)malloc(sizeof(mline)); init(p->next->node); p->next->next = nil; break; case 1: if(q.next == nil) break; p = &q; while(p->next->next != nil) p = p->next; free(p->next->node); free(p->next); p->next = nil; break; case 2: exits(0); } } p = &q; while(p != nil) { m = p->node; m->p1.x += m->p1dx; if(m->p1.x < r.min.x || m->p1.x > r.max.x) m->p1dx = -m->p1dx; m->p1.y += m->p1dy; if(m->p1.y < r.min.y || m->p1.y > r.max.y) m->p1dy = -m->p1dy; m->p2.x += m->p2dx; if(m->p2.x < r.min.x || m->p2.x > r.max.x) m->p2dx = -m->p2dx; m->p2.y += m->p2dy; if(m->p2.y < r.min.y || m->p2.y > r.max.y) m->p2dy = -m->p2dy; line(screen, m->p1, m->p2, Endsquare, Endsquare,2, img[m->b], ZP); if(m->b <= 0 || m->b >= 255) m->db *= -1; m->b+= m->db; p = p->next; } } } --upas-zwucqaziljqhynhafwwwopmzkm--