/* * Brian's Theme * ------------- * This version copyright (c) 1996 by Lachlan Patrick. * Written using GraphApp. * * This is a complex example not for beginners. It makes use of * the timer functions, colour, windows and keyboard call-backs. */ #include #include #include #include #include /* * Global variables. */ int width = 400; int height = 240; int banner_height = 40; char *banner = "Press a number to change the step size, Q to stop, R,G,B,C,M,Y,K for colours."; window w; int step_size = 3; int step_location = 0; point origin; rgb colour = Blue; /* * Functions. */ void change_location(void) { time_t t; srand((unsigned) time(&t)); origin.x = rand() % width; origin.y = rand() % height; step_location = 0; } void handle_mouse(window w, int buttons, point xy) { origin = xy; step_location = 0; redraw(w); } void handle_keydown(window w, int key) { if (isdigit(key)) step_size = key - '0'; else if ((key == 'q') || (key == 'Q') || (key == ESC)) exitapp(); else switch (key) { case 'r': case 'R': colour = Red; break; case 'g': case 'G': colour = Green; break; case 'b': case 'B': colour = Blue; break; case 'c': case 'C': colour = Cyan; break; case 'm': case 'M': colour = Magenta; break; case 'y': case 'Y': colour = Yellow; break; case 'k': case 'K': colour = Black; break; default: break; } } void draw_window(window w, rect r) { if (banner_height > 0) { r.y = height; r.height = banner_height; setcolour(Black); drawtext(r, Center + VCenter, banner); } } void resize_window(window w, rect r) { banner_height = getheight(SystemFont)*2+8; if (r.height < 4*banner_height) /* too small for banner */ banner_height = 0; height = r.height - banner_height; width = r.width; change_location(); } /* create and display the window */ window init_window(void) { banner_height = getheight(SystemFont)*2+8; w = newwindow("Brian's Theme", rect(0,0,width,height+banner_height), StandardWindow | Centered ); setmouseup(w, handle_mouse); setkeydown(w, handle_keydown); setredraw(w, draw_window); setresize(w, resize_window); show(w); return w; } void draw_and_step(window w) { drawto(w); setcolour(colour); if (step_location <= height) { drawline(origin, pt(0, height-step_location)); drawline(origin, pt(width, step_location)); } if (step_location <= width) { drawline(origin, pt(step_location, 0)); drawline(origin, pt(width-step_location, height)); } step_location += step_size; if ((step_location > width+100) && (step_location > height+100)) { change_location(); redraw(w); } } void main(void) { window win; initapp(0,0); win = init_window(); settimerfn(draw_and_step, win); settimer(80); mainloop(); }