File: bubble.c

package info (click to toggle)
xlockmore 4.09-3
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 5,440 kB
  • ctags: 6,552
  • sloc: ansic: 54,180; sh: 1,624; makefile: 689; tcl: 439; java: 269; perl: 149
file content (279 lines) | stat: -rw-r--r-- 7,412 bytes parent folder | download
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
/* -*- Mode: C; tab-width: 4 -*- */
/* bubble --- simple exploding bubbles */

#if !defined( lint ) && !defined( SABER )
static const char sccsid[] = "@(#)bubble.c	4.07 98/01/08 xlockmore";

#endif

/*-
 * Copyright (c) 1998 by Charles Vidal <vidalc@club-internet.fr>
 *         http://www.chez.com/vidalc
 *         and David Bagley
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted,
 * provided that the above copyright notice appear in all copies and that
 * both that copyright notice and this permission notice appear in
 * supporting documentation.
 *
 * This file is provided AS IS with no warranties of any kind.  The author
 * shall have no liability with respect to the infringement of copyrights,
 * trade secrets or any patents by this file or any part thereof.  In no
 * event will the author be liable for any lost revenue or profits or
 * other special, indirect and consequential damages.
 *
 * Revision History:
 * 10-Jan-98: Written.
 */

#ifdef STANDALONE
#define PROGCLASS "Bubble"
#define HACK_INIT init_bubble
#define HACK_DRAW draw_bubble
#define bubble_opts xlockmore_opts
#define DEFAULTS "*delay: 100000 \n" \
 "*count: 25 \n" \
 "*size: 100 \n" \
 "*ncolors: 200 \n"
#define SPREAD_COLORS
#include "xlockmore.h"		/* in xscreensaver distribution */
#else /* STANDALONE */
#include "xlock.h"		/* in xlockmore distribution */
#endif /* STANDALONE */

#define DEF_BOIL  "False"
static Bool boil;

static XrmOptionDescRec opts[] =
{
	{"-boil", ".bubble.boil", XrmoptionNoArg, (caddr_t) "on"},
	{"+boil", ".bubble.boil", XrmoptionNoArg, (caddr_t) "off"}
};
static argtype vars[] =
{
	{(caddr_t *) & boil, "boil", "Boil", DEF_BOIL, t_Bool}
};
static OptionStruct desc[] =
{
	{"-/+boil", "turn on/off boil"}
};

ModeSpecOpt bubble_opts =
{sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc};

#ifdef USE_MODULES
ModStruct   bubble_description =
{"bubble", "init_bubble", "draw_bubble", "release_bubble",
 "refresh_bubble", "init_bubble", NULL, &bubble_opts,
 100000, 25, 1, 100, 64, 0.6, "",
 "Shows popping bubbles", 0, NULL};

#endif

typedef struct {
	int         x, y, life;
} bubbletype;

#define MINSIZE 20
#define MINBUBBLES 1

typedef struct {
	int         direction;
	int         color;
	int         width, height;
	int         nbubbles;
	Bool        boil;
	bubbletype *bubble;
	int         d;
	Pixmap      dbuf;
	GC          dbuf_gc;
} bubblestruct;

static bubblestruct *bubbles = NULL;

static void
updateBubble(ModeInfo * mi, int i)
{
	bubblestruct *bp = &bubbles[MI_SCREEN(mi)];
	int         x, diameter, x4, y4, diameter4;

	if (bp->bubble[i].life + 1 > bp->d - NRAND(16) ||
	    bp->bubble[i].y < 0) {
		bp->bubble[i].life = 0;
		return;
	}
	++bp->bubble[i].life;
	diameter = bp->bubble[i].life;
	x = bp->bubble[i].x;
	if (bp->boil) {
		bp->bubble[i].y -= diameter / 2;
		x += (int) (cos((double) (diameter +
		       bp->bubble[i].x) / (M_PI / 5.0)) * (double) diameter);
	}
	/* SunOS 4.1.X xnews server may crash without this */
	if (diameter < 4) {
		XFillRectangle(MI_DISPLAY(mi), bp->dbuf, bp->dbuf_gc,
			    x - diameter / 2, bp->bubble[i].y - diameter / 2,
			       diameter, diameter);
	} else {
		XDrawArc(MI_DISPLAY(mi), bp->dbuf, bp->dbuf_gc,
			 x - diameter / 2, bp->bubble[i].y - diameter / 2,
			 diameter, diameter, 0, 23040);
	}
	diameter4 = diameter / 4;
	if (diameter4 > 0) {
		x4 = x - diameter4 / 2 +
			((bp->direction / 2) ? (-1) : 1) * diameter / 6;
		y4 = bp->bubble[i].y - diameter4 / 2 +
			((bp->direction % 2) ? 1 : (-1)) * diameter / 6;
		/* SunOS 4.1.X xnews server may crash without this */
		if (diameter4 < 4) {
			XFillRectangle(MI_DISPLAY(mi), bp->dbuf, bp->dbuf_gc,
				       x4, y4, diameter4, diameter4);
		} else {
			XFillArc(MI_DISPLAY(mi), bp->dbuf, bp->dbuf_gc,
				 x4, y4, diameter4, diameter4, 0, 23040);
		}
	}
}

static void
changeBubble(ModeInfo * mi)
{
	bubblestruct *bp = &bubbles[MI_SCREEN(mi)];
	int         i;

	for (i = 0; i < bp->nbubbles; i++) {
		if (bp->bubble[i].life != 0)
			updateBubble(mi, i);
	}
	i = NRAND(bp->nbubbles);
	if (bp->bubble[i].life == 0) {
		bp->bubble[i].x = NRAND(bp->width);
		if (bp->boil)
			bp->bubble[i].y = bp->height -
				((bp->height >= 16) ? NRAND(bp->height / 16) : 0);
		else
			bp->bubble[i].y = NRAND(bp->height);
		updateBubble(mi, i);
	}
}

void
init_bubble(ModeInfo * mi)
{
	bubblestruct *bp;
	int         size = MI_SIZE(mi);
	Display    *display = MI_DISPLAY(mi);
	Window      window = MI_WINDOW(mi);
	XGCValues   gcv;

	if (bubbles == NULL) {
		if ((bubbles = (bubblestruct *) calloc(MI_NUM_SCREENS(mi),
					     sizeof (bubblestruct))) == NULL)
			return;
	}
	bp = &bubbles[MI_SCREEN(mi)];

	bp->width = MI_WIN_WIDTH(mi);
	bp->height = MI_WIN_HEIGHT(mi);
	bp->direction = NRAND(4);
	if (MI_WIN_IS_FULLRANDOM(mi))
		bp->boil = (Bool) (LRAND() & 1);
	else
		bp->boil = boil;

	if (size < -MINSIZE)
		bp->d = NRAND(MIN(-size, MAX(MINSIZE,
		   MIN(bp->width, bp->height) / 2)) - MINSIZE + 1) + MINSIZE;
	else if (size < MINSIZE) {
		if (!size)
			bp->d = MAX(MINSIZE, MIN(bp->width, bp->height) / 2);
		else
			bp->d = MINSIZE;
	} else
		bp->d = MIN(size, MAX(MINSIZE,
				      MIN(bp->width, bp->height) / 2));
	bp->nbubbles = MI_BATCHCOUNT(mi);
	if (bp->nbubbles < -MINBUBBLES) {
		bp->nbubbles = NRAND(-bp->nbubbles - MINBUBBLES + 1) + MINBUBBLES;
	} else if (bp->nbubbles < MINBUBBLES)
		bp->nbubbles = MINBUBBLES;
	if (bp->bubble != NULL)
		(void) free((void *) bp->bubble);
	bp->bubble = (bubbletype *) calloc(bp->nbubbles, sizeof (bubbletype));

	if (MI_NPIXELS(mi) > 2)
		bp->color = NRAND(MI_NPIXELS(mi));

	if (bp->dbuf)
		XFreePixmap(display, bp->dbuf);
	bp->dbuf = XCreatePixmap(display, window, bp->width, bp->height, 1);
	/* Do not want any exposure events from XCopyPlane */
	XSetGraphicsExposures(display, MI_GC(mi), False);

	gcv.foreground = 1;
	gcv.background = 0;
	gcv.function = GXcopy;
	gcv.graphics_exposures = False;
	gcv.line_width = 2;
	if (bp->dbuf_gc)
		XFreeGC(display, bp->dbuf_gc);
	bp->dbuf_gc = XCreateGC(display, bp->dbuf,
	       GCForeground | GCBackground | GCLineWidth | GCFunction, &gcv);

	MI_CLEARWINDOW(mi);
}

void
draw_bubble(ModeInfo * mi)
{
	Display    *display = MI_DISPLAY(mi);
	Window      window = MI_WINDOW(mi);
	GC          gc = MI_GC(mi);
	bubblestruct *bp = &bubbles[MI_SCREEN(mi)];

	if (MI_NPIXELS(mi) <= 2)
		XSetForeground(display, gc, MI_WHITE_PIXEL(mi));
	else {
		bp->color = (bp->color + 1) % MI_NPIXELS(mi);
		XSetForeground(display, gc, MI_PIXEL(mi, bp->color));
	}
	if (bp->dbuf) {
		XSetForeground(display, bp->dbuf_gc, 0);
		XFillRectangle(display, bp->dbuf, bp->dbuf_gc,
			       0, 0, bp->width, bp->height);
		XSetForeground(display, bp->dbuf_gc, 1);
		changeBubble(mi);
		XCopyPlane(display, bp->dbuf, window, gc,
			   0, 0, bp->width, bp->height, 0, 0, 1);
	}
}

void
release_bubble(ModeInfo * mi)
{
	if (bubbles != NULL) {
		int         screen;

		for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
			bubblestruct *bp = &bubbles[screen];

			if (bp->dbuf)
				XFreePixmap(MI_DISPLAY(mi), bp->dbuf);
			if (bp->dbuf_gc)
				XFreeGC(MI_DISPLAY(mi), bp->dbuf_gc);
			if (bp->bubble != NULL)
				(void) free((void *) bp->bubble);
		}
		(void) free((void *) bubbles);
		bubbles = NULL;
	}
}

void
refresh_bubble(ModeInfo * mi)
{
	/* Do nothing, it will refresh by itself */
}