1 /* halftone, Copyright (c) 2002 by Peter Jaric <peter@jaric.org>
3 * Permission to use, copy, modify, distribute, and sell this software and its
4 * documentation for any purpose is hereby granted without fee, provided that
5 * the above copyright notice appear in all copies and that both that
6 * copyright notice and this permission notice appear in supporting
7 * documentation. No representations are made about the suitability of this
8 * software for any purpose. It is provided "as is" without express or
12 * Draws the gravitational force in each point on the screen seen
13 * through a halftone dot pattern. The force is calculated from a set
14 * of moving mass points. View it from a distance for best effect.
20 #include "screenhack.h"
22 #define DEFAULT_DELAY 10000
23 #define DEFAULT_SPACING 14
24 #define DEFAULT_SIZE_FACTOR 1.5
25 #define DEFAULT_COUNT 10
26 #define DEFAULT_MIN_MASS 0.001
27 #define DEFAULT_MAX_MASS 0.02
28 #define DEFAULT_MIN_SPEED 0.001
29 #define DEFAULT_MAX_SPEED 0.02
41 /* Moving gravity points */
42 int gravity_point_count;
44 double* gravity_point_x;
45 double* gravity_point_y;
46 double* gravity_point_mass;
47 double* gravity_point_x_inc;
48 double* gravity_point_y_inc;
58 int color_tick, cycle_speed;
60 /* Off screen buffer */
71 static void update_buffer(halftone_screen *halftone, XWindowAttributes * attrs)
73 if (halftone->buffer_width != attrs->width ||
74 halftone->buffer_height != attrs->height)
78 if (halftone->buffer_width != -1 &&
79 halftone->buffer_height != -1)
81 if (halftone->buffer == halftone->window)
82 XFreePixmap(halftone->dpy, halftone->buffer);
83 XFreeGC(halftone->dpy, halftone->buffer_gc);
86 halftone->buffer_width = attrs->width;
87 halftone->buffer_height = attrs->height;
88 #ifdef HAVE_COCOA /* Don't second-guess Quartz's double-buffering */
89 halftone->buffer = halftone->window;
91 halftone->buffer = XCreatePixmap(halftone->dpy, halftone->window, halftone->buffer_width, halftone->buffer_height, attrs->depth);
94 halftone->buffer_gc = XCreateGC(halftone->dpy, halftone->buffer, 0, &gc_values);
98 static void update_dot_attributes(halftone_screen *halftone, XWindowAttributes * attrs)
100 double dots_width = attrs->width / halftone->spacing + 1;
101 double dots_height = attrs->height / halftone->spacing + 1;
103 if (halftone->dots == NULL ||
104 (dots_width != halftone->dots_width ||
105 dots_height != halftone->dots_height))
107 if (halftone->dots != NULL)
108 free(halftone->dots);
110 halftone->dots_width = dots_width;
111 halftone->dots_height = dots_height;
112 halftone->dots = (double *) malloc(halftone->dots_width * halftone->dots_height * sizeof(double));
117 halftone_init (Display *dpy, Window window)
128 XWindowAttributes attrs;
129 halftone_screen *halftone;
131 halftone = (halftone_screen *) calloc (1, sizeof(halftone_screen));
134 halftone->window = window;
136 halftone->delay = get_integer_resource (dpy, "delay", "Integer");
137 halftone->delay = (halftone->delay < 0 ? DEFAULT_DELAY : halftone->delay);
139 halftone->gc = XCreateGC (halftone->dpy, halftone->window, 0, &gc_values);
141 halftone->buffer_width = -1;
142 halftone->buffer_height = -1;
143 halftone->dots = NULL;
145 /* Read command line arguments and set all settings. */
146 count = get_integer_resource (dpy, "count", "Count");
147 halftone->gravity_point_count = count < 1 ? DEFAULT_COUNT : count;
149 spacing = get_integer_resource (dpy, "spacing", "Integer");
150 halftone->spacing = spacing < 1 ? DEFAULT_SPACING : spacing;
152 factor = get_float_resource (dpy, "sizeFactor", "Double");
153 halftone->max_dot_size =
154 (factor < 0 ? DEFAULT_SIZE_FACTOR : factor) * halftone->spacing;
156 min_mass = get_float_resource (dpy, "minMass", "Double");
157 min_mass = min_mass < 0 ? DEFAULT_MIN_MASS : min_mass;
159 max_mass = get_float_resource (dpy, "maxMass", "Double");
160 max_mass = max_mass < 0 ? DEFAULT_MAX_MASS : max_mass;
161 max_mass = max_mass < min_mass ? min_mass : max_mass;
163 min_speed = get_float_resource (dpy, "minSpeed", "Double");
164 min_speed = min_speed < 0 ? DEFAULT_MIN_SPEED : min_speed;
166 max_speed = get_float_resource (dpy, "maxSpeed", "Double");
167 max_speed = max_speed < 0 ? DEFAULT_MAX_SPEED : max_speed;
168 max_speed = max_speed < min_speed ? min_speed : max_speed;
171 /* Set up the moving gravity points. */
172 halftone->gravity_point_x = (double *) malloc(halftone->gravity_point_count * sizeof(double));
173 halftone->gravity_point_y = (double *) malloc(halftone->gravity_point_count * sizeof(double));
174 halftone->gravity_point_mass = (double *) malloc(halftone->gravity_point_count * sizeof(double));
175 halftone->gravity_point_x_inc = (double *) malloc(halftone->gravity_point_count * sizeof(double));
176 halftone->gravity_point_y_inc = (double *) malloc(halftone->gravity_point_count * sizeof(double));
178 for (i = 0; i < halftone->gravity_point_count; i++)
180 halftone->gravity_point_x[i] = frand(1);
181 halftone->gravity_point_y[i] = frand(1);
182 halftone->gravity_point_mass[i] = min_mass + (max_mass - min_mass) * frand(1);
183 halftone->gravity_point_x_inc[i] = min_speed + (max_speed - min_speed) * frand(1);
184 halftone->gravity_point_y_inc[i] = min_speed + (max_speed - min_speed) * frand(1);
188 /* Set up the dots. */
189 XGetWindowAttributes(halftone->dpy, halftone->window, &attrs);
191 halftone->ncolors = get_integer_resource (dpy, "colors", "Colors");
192 if (halftone->ncolors < 4) halftone->ncolors = 4;
193 halftone->colors = (XColor *) calloc(halftone->ncolors, sizeof(XColor));
194 make_smooth_colormap (dpy, attrs.visual, attrs.colormap,
195 halftone->colors, &halftone->ncolors,
197 halftone->color0 = 0;
198 halftone->color1 = halftone->ncolors / 2;
199 halftone->cycle_speed = get_integer_resource (dpy, "cycleSpeed", "CycleSpeed");
200 halftone->color_tick = 0;
202 update_buffer(halftone, &attrs);
203 update_dot_attributes(halftone, &attrs);
205 for (x = 0; x < halftone->dots_width; x++)
206 for (y = 0; y < halftone->dots_height; y++)
208 halftone->dots[x + y * halftone->dots_width] = 0;
216 static void fill_circle(Display *dpy, Window window, GC gc, int x, int y, int size)
218 int start_x = x - (size / 2);
219 int start_y = y - (size / 2);
220 unsigned int width = size;
221 unsigned int height = size;
223 int angle2 = 360 * 64; /* A full circle */
225 XFillArc (dpy, window, gc,
226 start_x, start_y, width, height,
230 static void repaint_halftone(halftone_screen *halftone)
234 int x_offset = halftone->spacing / 2;
235 int y_offset = halftone->spacing / 2;
241 /* Fill buffer with background color */
242 XSetForeground (halftone->dpy, halftone->buffer_gc,
243 halftone->colors[halftone->color0].pixel);
244 XFillRectangle(halftone->dpy, halftone->buffer, halftone->buffer_gc, 0, 0, halftone->buffer_width, halftone->buffer_height);
246 /* Draw dots on buffer */
247 XSetForeground (halftone->dpy, halftone->buffer_gc,
248 halftone->colors[halftone->color1].pixel);
250 if (halftone->color_tick++ >= halftone->cycle_speed)
252 halftone->color_tick = 0;
253 halftone->color0 = (halftone->color0 + 1) % halftone->ncolors;
254 halftone->color1 = (halftone->color1 + 1) % halftone->ncolors;
257 for (x = 0; x < halftone->dots_width; x++)
258 for (y = 0; y < halftone->dots_height; y++)
259 fill_circle(halftone->dpy, halftone->buffer, halftone->buffer_gc,
260 x_offset + x * halftone->spacing, y_offset + y * halftone->spacing,
261 halftone->max_dot_size * halftone->dots[x + y * halftone->dots_width]);
263 /* Copy buffer to window */
264 if (halftone->buffer != halftone->window)
265 XCopyArea(halftone->dpy, halftone->buffer, halftone->window, halftone->gc, 0, 0, halftone->buffer_width, halftone->buffer_height, 0, 0);
268 static double calculate_gravity(halftone_screen *halftone, int x, int y)
274 for (i = 0; i < halftone->gravity_point_count; i++)
276 double dx = ((double) x) - halftone->gravity_point_x[i] * halftone->dots_width;
277 double dy = ((double) y) - halftone->gravity_point_y[i] * halftone->dots_height;
278 double distance = sqrt(dx * dx + dy * dy);
282 double gravity = halftone->gravity_point_mass[i] / (distance * distance / (halftone->dots_width * halftone->dots_height));
284 gx += (dx / distance) * gravity;
285 gy += (dy / distance) * gravity;
289 return sqrt(gx * gx + gy * gy);
292 static void update_halftone(halftone_screen *halftone)
295 XWindowAttributes attrs;
297 XGetWindowAttributes(halftone->dpy, halftone->window, &attrs);
299 /* Make sure we have a valid buffer */
300 update_buffer(halftone, &attrs);
302 /* Make sure all dot attributes (spacing, width, height, etc) are correct */
303 update_dot_attributes(halftone, &attrs);
305 /* Move gravity points */
306 for (i = 0; i < halftone->gravity_point_count; i++)
308 halftone->gravity_point_x_inc[i] =
309 (halftone->gravity_point_x[i] >= 1 || halftone->gravity_point_x[i] <= 0 ?
310 -halftone->gravity_point_x_inc[i] :
311 halftone->gravity_point_x_inc[i]);
312 halftone->gravity_point_y_inc[i] =
313 (halftone->gravity_point_y[i] >= 1 || halftone->gravity_point_y[i] <= 0 ?
314 -halftone->gravity_point_y_inc[i] :
315 halftone->gravity_point_y_inc[i]);
317 halftone->gravity_point_x[i] += halftone->gravity_point_x_inc[i];
318 halftone->gravity_point_y[i] += halftone->gravity_point_y_inc[i];
321 /* Update gravity in each dot .*/
322 for (x = 0; x < halftone->dots_width; x++)
323 for (y = 0; y < halftone->dots_height; y++)
325 double gravity = calculate_gravity(halftone, x, y);
327 halftone->dots[x + y * halftone->dots_width] = (gravity > 1 ? 1 : (gravity < 0 ? 0 : gravity));
333 halftone_draw (Display *dpy, Window window, void *closure)
335 halftone_screen *halftone = (halftone_screen *) closure;
337 repaint_halftone(halftone);
338 update_halftone(halftone);
340 return halftone->delay;
345 halftone_reshape (Display *dpy, Window window, void *closure,
346 unsigned int w, unsigned int h)
351 halftone_event (Display *dpy, Window window, void *closure, XEvent *event)
357 halftone_free (Display *dpy, Window window, void *closure)
359 halftone_screen *halftone = (halftone_screen *) closure;
364 static const char *halftone_defaults [] = {
365 ".background: Black",
379 static XrmOptionDescRec halftone_options [] = {
380 { "-delay", ".delay", XrmoptionSepArg, 0 },
381 { "-count", ".count", XrmoptionSepArg, 0 },
382 { "-minmass", ".minMass", XrmoptionSepArg, 0 },
383 { "-maxmass", ".maxMass", XrmoptionSepArg, 0 },
384 { "-minspeed", ".minSpeed", XrmoptionSepArg, 0 },
385 { "-maxspeed", ".maxSpeed", XrmoptionSepArg, 0 },
386 { "-spacing", ".spacing", XrmoptionSepArg, 0 },
387 { "-sizefactor", ".sizeFactor", XrmoptionSepArg, 0 },
388 { "-colors", ".colors", XrmoptionSepArg, 0 },
389 { "-cycle-speed", ".cycleSpeed", XrmoptionSepArg, 0 },
394 XSCREENSAVER_MODULE ("Halftone", halftone)