ftp://ftp.sunet.se/pub/os/Linux/distributions/ultrapenguin/ultrapenguin-1.1/SRPMS...
[xscreensaver] / utils / erase.c
1 /* erase.c: Erase the screen in various more or less interesting ways.
2  * (c) 1997 by Johannes Keukelaar <johannes@nada.kth.se>
3  * Permission to use in any way granted. Provided "as is" without expressed
4  * or implied warranty. NO WARRANTY, NO EXPRESSION OF SUITABILITY FOR ANY
5  * PURPOSE. (I.e.: Use in any way, but at your own risk!)
6  */
7
8 #include "utils.h"
9 #include "yarandom.h"
10 #include "usleep.h"
11
12 #define NUM_MODES 6
13
14 void
15 erase_window(Display *dpy, Window window, GC gc,
16              int width, int height, int mode, int delay)
17 {
18   int *clear_lines;
19   int i, j, line, num_lines=0, granularity, max_num;
20
21   max_num = 2*height;
22   if(2*width>max_num)
23     max_num = 2*width;
24
25   clear_lines = (int *)calloc(max_num, sizeof(int));
26   if(clear_lines)
27     {
28       if(mode<0 || mode>=NUM_MODES)
29         mode = random()%NUM_MODES;
30       granularity = 25;
31       switch(mode)
32         {
33         case 0:
34           for(i = 0; i < height; i++)
35             clear_lines[i] = i;
36           for(i = 0; i < height; i++)
37             {
38               int t, r;
39               t = clear_lines[i];
40               r = random()%height;
41               clear_lines[i] = clear_lines[r];
42               clear_lines[r] = t;
43             }
44           num_lines = height;
45           break;
46         case 1:
47           for(i = 0; i < width; i++)
48             clear_lines[i] = i+height;
49           for(i = 0; i < width; i++)
50             {
51               int t, r;
52               t = clear_lines[i];
53               r = random()%width;
54               clear_lines[i] = clear_lines[r];
55               clear_lines[r] = t;
56             }
57           num_lines = width;
58           break;
59         case 2:
60           for(i = 0; i < width/2; i++)
61             clear_lines[i] = i*2+height;
62           for(i = 0; i < height/2; i++)
63             clear_lines[i+width/2] = i*2;
64           for(i = 0; i < width/2; i++)
65             clear_lines[i+width/2+height/2] = width-i*2-(width%2?0:1)+height;
66           num_lines = width+height/2;
67           granularity = 4;
68           break;
69         case 3:
70           for(i = 0; i < max_num/4; i++)
71             {
72               clear_lines[i*4] = i*2;
73               clear_lines[i*4+1] = height-i*2-(height%2?0:1);
74               clear_lines[i*4+2] = height+i*2;
75               clear_lines[i*4+3] = height+width-i*2-(width%2?0:1);
76             }
77           num_lines = max_num;
78           granularity = 4;
79           break;
80         case 4:
81           j = 0;
82           for(i = 0; i < width*2; i++)
83             {
84               line = (i/16)*16-(i%16)*15;
85               if(line>=0 && line<width)
86                 {
87                   clear_lines[j] = height+line;
88                   j++;
89                 }
90             }
91           num_lines = width;
92           granularity = 4;
93           break;
94         case 5:
95           j = 0;
96           for(i = width*2; i >= 0; i--)
97             {
98               line = (i/16)*16-(i%16)*15;
99               if(line>=0 && line<width)
100                 {
101                   clear_lines[j] = height+line;
102                   j++;
103                 }
104             }
105           num_lines = width;
106           granularity = 4;
107           break;
108         }
109       for (i = 0; i < num_lines; i++)
110         { 
111           if(clear_lines[i] < height)
112             XDrawLine (dpy, window, gc, 0, clear_lines[i], width, 
113                        clear_lines[i]);
114           else
115             XDrawLine (dpy, window, gc, clear_lines[i]-height, 0,
116                        clear_lines[i]-height, height);
117           XFlush (dpy);
118           if ((i % granularity) == 0)
119             {
120               usleep (delay*granularity);
121             }
122         }
123       
124       free(clear_lines);
125     }
126
127   XClearWindow (dpy, window);
128 }