summaryrefslogtreecommitdiffstats
path: root/hacks/coral.c
blob: 9cfcf50a4f032815fa2e6790281ce2c342dc7d19 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/* coral, by "Frederick G.M. Roeber" <roeber@netscape.com>, 15-jul-97.
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation.  No representations are made about the suitability of this
 * software for any purpose.  It is provided "as is" without express or 
 * implied warranty.
 */

#include "screenhack.h"
#include "colors.h"
#include "erase.h"

#define NCOLORSMAX 200

struct state {
  Display *dpy;
  Window window;

   GC draw_gc, erase_gc;
   unsigned int default_fg_pixel;
   XColor colors[NCOLORSMAX];
   int ncolors;
   int colorindex;
   int colorsloth;

   XPoint *walkers;
   int nwalkers;
   int width, widthb;
   int height;
   int delay, delay2;
   int max_points;
   XPoint *pointbuf;

   unsigned int *board;

   int done, reset;
   int npoints;
   eraser_state *eraser;
};


#define getdot(x,y) (st->board[(y*st->widthb)+(x>>5)] &  (1<<(x & 31)))
#define setdot(x,y) (st->board[(y*st->widthb)+(x>>5)] |= (1<<(x & 31)))


static void
init_coral(struct state *st)
{
    XGCValues gcv;
    Colormap cmap;
    XWindowAttributes xgwa;
    Bool writeable = False;
    int seeds;
    int density;
    int i;

    XClearWindow(st->dpy, st->window);
    XGetWindowAttributes(st->dpy, st->window, &xgwa);
    st->width = xgwa.width;
    st->widthb = ((xgwa.width + 31) >> 5);
    st->height = xgwa.height;
    if (st->board) free(st->board);
    st->board = (unsigned int *)calloc(st->widthb * xgwa.height, sizeof(unsigned int));
    if(!st->board) exit(1);
    cmap = xgwa.colormap;
    if( st->ncolors ) {
        free_colors(xgwa.screen, cmap, st->colors, st->ncolors);
        st->ncolors = 0;
    }
    gcv.foreground = st->default_fg_pixel = get_pixel_resource(st->dpy, cmap, "foreground", "Foreground");
    st->draw_gc = XCreateGC(st->dpy, st->window, GCForeground, &gcv);
    gcv.foreground = get_pixel_resource (st->dpy, cmap, "background", "Background");
    st->erase_gc = XCreateGC (st->dpy, st->window, GCForeground, &gcv);
    st->ncolors = NCOLORSMAX;
    make_uniform_colormap(xgwa.screen, xgwa.visual, cmap,
                          st->colors, &st->ncolors, True, &writeable, False);
    if (st->ncolors <= 0) {
      st->ncolors = 2;
      st->colors[0].red = st->colors[0].green = st->colors[0].blue = 0;
      st->colors[1].red = st->colors[1].green = st->colors[1].blue = 0xFFFF;
      XAllocColor(st->dpy, cmap, &st->colors[0]);
      XAllocColor(st->dpy, cmap, &st->colors[1]);
   }
    st->colorindex = random()%st->ncolors;
    
    density = get_integer_resource(st->dpy, "density", "Integer");
    if( density < 1 ) density = 1;
    if( density > 100 ) density = 90; /* more like mold than coral */
    st->nwalkers = (st->width*st->height*density)/100;
    if (st->walkers) free(st->walkers);
    st->walkers = (XPoint *)calloc(st->nwalkers, sizeof(XPoint));
    if( (XPoint *)0 == st->walkers ) exit(1);

    seeds = get_integer_resource(st->dpy, "seeds", "Integer");
    if( seeds < 1 ) seeds = 1;
    if( seeds > 1000 ) seeds = 1000;

    st->colorsloth = st->nwalkers*2/st->ncolors;
    XSetForeground(st->dpy, st->draw_gc, st->colors[st->colorindex].pixel);

    if ((st->width <= 2) || (st->height <= 2)) return;

    for( i = 0; i < seeds; i++ ) {
        int x, y;
	int max_repeat = 10;
        do {
          x = 1 + random() % (st->width - 2);
          y = 1 + random() % (st->height - 2);
        } while( getdot(x, y) && max_repeat--);

        setdot((x-1), (y-1)); setdot(x, (y-1)); setdot((x+1), (y-1));
	setdot((x-1),  y   ); setdot(x,  y   ); setdot((x+1),  y   );
	setdot((x-1), (y+1)); setdot(x, (y+1)); setdot((x+1), (y+1));
        XDrawPoint(st->dpy, st->window, st->draw_gc, x, y);
    }

    for( i = 0; i < st->nwalkers; i++ ) {
        st->walkers[i].x = (random() % (st->width-2)) + 1;
        st->walkers[i].y = (random() % (st->height-2)) + 1;
    }
}


/* returns 2 bits of randomness (conserving calls to random()).
   This speeds things up a little, but not a lot (5-10% or so.)
 */
static int 
rand_2(void)
{
  static int i = 0;
  static int r = 0;
  if (i != 0) {
    i--;
  } else {
    i = 15;
    r = random();
  }

  {
    register int j = (r & 3);
    r = r >> 2;
    return j;
  }
}


static int
coral(struct state *st)
{
  int i = 0;

  for( i = 0; i < st->nwalkers; i++ ) {
    int x = st->walkers[i].x;
    int y = st->walkers[i].y;

    if( getdot(x, y) ) {

      Bool flush = False;
      Bool color = False;

      /* XDrawPoint(dpy, window, draw_gc, x, y); */
      st->pointbuf[st->npoints].x = x;
      st->pointbuf[st->npoints].y = y;
      st->npoints++;

      /* Mark the surrounding area as "sticky" */
      setdot((x-1), (y-1)); setdot(x, (y-1)); setdot((x+1), (y-1));
      setdot((x-1),  y   );                   setdot((x+1),  y   );
      setdot((x-1), (y+1)); setdot(x, (y+1)); setdot((x+1), (y+1));
      st->nwalkers--;
      st->walkers[i].x = st->walkers[st->nwalkers].x;
      st->walkers[i].y = st->walkers[st->nwalkers].y;
      if( 0 == 
	  ((st->colorsloth ? st->nwalkers%st->colorsloth : 0)) ) {
        color = True;
      }
		  
      if (flush || color || 0 == st->nwalkers || st->npoints >= st->max_points) {
        XDrawPoints(st->dpy, st->window, st->draw_gc, st->pointbuf, st->npoints,
                    CoordModeOrigin);
        st->npoints = 0;
      }

      if (color) {
        st->colorindex++;
        if( st->colorindex == st->ncolors )
          st->colorindex = 0;
        XSetForeground(st->dpy, st->draw_gc, st->colors[st->colorindex].pixel);
      }
    } else {
      /* move it a notch */
      do {
        switch(rand_2()) {
        case 0:
          if( 1 == x ) continue;
          st->walkers[i].x--;
          break;
        case 1:
          if( st->width-2 == x ) continue;
          st->walkers[i].x++;
          break;
        case 2:
          if( 1 == y ) continue;
          st->walkers[i].y--;
          break;
        default: /* case 3: */
          if( st->height-2 == y ) continue;
          st->walkers[i].y++;
          break;
          /* default:
             abort(); */
        }
      } while(0);
    }
  }

 return (0 == st->nwalkers);
}

static void *
coral_init (Display *dpy, Window window)
{
  struct state *st = (struct state *) calloc (1, sizeof(*st));
  st->dpy = dpy;
  st->window = window;
  st->max_points = 200;
  st->pointbuf = (XPoint *) calloc(sizeof(XPoint), st->max_points+2);
  if (!st->pointbuf) exit(-1);

  st->delay = get_integer_resource (st->dpy, "delay", "Integer");
  st->delay2 = get_integer_resource (st->dpy, "delay2", "Integer");
  st->reset = 1;
  return st;
}


static unsigned long
coral_draw (Display *dpy, Window window, void *closure)
{
  struct state *st = (struct state *) closure;

  if (st->eraser || st->done)
    {
      st->done = 0;
      st->eraser = erase_window (st->dpy, st->window, st->eraser);
      return st->delay2;
    }

  if (st->reset)
    init_coral(st);
  st->reset = st->done = coral(st);

  return (st->reset
          ? (st->delay * 1000000)
          : st->delay2);
}

static void
coral_reshape (Display *dpy, Window window, void *closure, 
                 unsigned int w, unsigned int h)
{
  struct state *st = (struct state *) closure;
  init_coral(st);
}

static Bool
coral_event (Display *dpy, Window window, void *closure, XEvent *event)
{
  struct state *st = (struct state *) closure;
  if (screenhack_event_helper (dpy, window, event))
    {
      st->reset = 1;
      return True;
    }
  return False;
}

static void
coral_free (Display *dpy, Window window, void *closure)
{
  struct state *st = (struct state *) closure;
  free (st->pointbuf);
  if (st->walkers) free (st->walkers);
  if (st->board) free (st->board);
  free (st);
}

static const char *coral_defaults[] = {
  ".lowrez:	true",
  ".background:	black",
  ".foreground:	white",
  "*fpsSolid:	true",
  "*density:	25",
  "*seeds:	20", /* too many for 640x480, too few for 1280x1024 */
  "*delay:	5",
  "*delay2:	20000",
#ifdef HAVE_MOBILE
  "*ignoreRotation: True",
#endif
  0
};

static XrmOptionDescRec coral_options[] = {
    { "-density", ".density", XrmoptionSepArg, 0 },
    { "-seeds", ".seeds", XrmoptionSepArg, 0 },
    { "-delay", ".delay", XrmoptionSepArg, 0 },
    { "-delay2", ".delay2", XrmoptionSepArg, 0 },
    { 0, 0, 0, 0 }
};

XSCREENSAVER_MODULE ("Coral", coral)