File: drawcard.c

package info (click to toggle)
kgames 1.0-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 2,820 kB
  • sloc: ansic: 26,802; yacc: 1,067; lex: 216; makefile: 6
file content (447 lines) | stat: -rw-r--r-- 10,067 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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
 * drawcard.c
 *
 * display cards on the table
 */

# include	"mille.h"
# include	"uiXt.h"
# include	"card.h"
# include	"background.h"

extern char	_go_bits[], go_label_bits [], go_mask_bits[];
extern char	_stop_bits[], stop_label_bits [], stop_mask_bits[];
extern char	_right_bits[], right_label_bits[], right_mask_bits[];
extern char	_speed_bits[], speed_mask_bits[];
extern char	_end_bits[], end_mask_bits[];
extern char	_accident_bits[], accident_label_bits [], accident_mask_bits[];
extern char	_repairs_bits[], repairs_label_bits [], repairs_mask_bits[];
extern char	_ace_bits[], ace_label_bits [], ace_mask_bits[];
extern char	_flat_bits[], flat_label_bits[], flat_mask_bits[];
extern char	_spare_bits[], spare_label_bits [], spare_mask_bits[];
extern char	_puncture_bits[], puncture_label_bits [], puncture_mask_bits[];
extern char	_out_bits[], out_label_bits[], out_mask_bits[];
extern char	_gas_bits[], gas_mask_bits[];
extern char	_extra_bits[], extra_label_bits [], extra_mask_bits[];
extern char	miles_mask_bits[];
extern char	_25_bits[], _50_bits[], _75_bits[], _100_bits[], _200_bits[];

extern char	deck_both_bits[];

struct card color_cards[NUM_CARDS] = {
	{
		{
			{ fill_bits,		WHITE_COLOR, },
			{ miles_mask_bits,	BLACK_COLOR, },
			{ _25_bits,		BLUE_COLOR, },
			{ NULL,			0, },
		},
		"25",	0,
	},
	{
		{
			{ fill_bits,		WHITE_COLOR, },
			{ miles_mask_bits,	BLACK_COLOR, },
			{ _50_bits,		BLUE_COLOR, },
			{ NULL,			0, },
		},
		"50",	0,
	}, {
		{
			{ fill_bits,		WHITE_COLOR, },
			{ miles_mask_bits,	BLACK_COLOR, },
			{ _75_bits,		BLUE_COLOR, },
			{ 0,			0, },
		},
		"75",	0,

	}, {
		{
			{ fill_bits,		WHITE_COLOR, },
			{ miles_mask_bits,	BLACK_COLOR, },
			{ _100_bits,		BLUE_COLOR, },
			{ 0,			0, },
		},
		"100",	0,
	}, {
		{
			{ fill_bits,		WHITE_COLOR, },
			{ miles_mask_bits,	BLACK_COLOR, },
			{ _200_bits,		BLUE_COLOR, },
			{ 0,			0, },
		},
		"200",	0,
	}, {
		{
			{ fill_bits,		WHITE_COLOR, },
			{ out_mask_bits,	BLACK_COLOR, },
			{ _out_bits,		RED_COLOR, },
			{ out_label_bits,	RED_COLOR, },
		},
		"Panne D'Essence",	0,
	}, {
		{
			{ fill_bits,		WHITE_COLOR, },
			{ flat_mask_bits,	BLACK_COLOR, },
			{ _flat_bits,		RED_COLOR, },
			{ flat_label_bits,	RED_COLOR, },
		},
		"Crev\351",	0,
	}, {
		{
			{ fill_bits,		WHITE_COLOR, },
			{ accident_mask_bits,	BLACK_COLOR, },
			{ _accident_bits,	RED_COLOR, },
			{ accident_label_bits,	RED_COLOR, },
		},
		"Accident",	0,
	}, {
		{
			{ fill_bits,		WHITE_COLOR, },
			{ stop_mask_bits,	BLACK_COLOR, },
			{ _stop_bits,		RED_COLOR, },
			{ stop_label_bits,	RED_COLOR, },
		},
		"Stop!",	0,
	}, {
		{
			{ fill_bits,		WHITE_COLOR, },
			{ speed_mask_bits,	BLACK_COLOR, },
			{ _speed_bits,		RED_COLOR, },
			{ 0,			0, },
		},
		"Limite De Vitesse",	0,
	}, {
		{
			{ fill_bits,		WHITE_COLOR, },
			{ gas_mask_bits,	BLACK_COLOR, },
			{ _gas_bits,		GREEN_COLOR, },
			{ 0,			0, },
		},
		"Essence",	0,
	}, {
		{
			{ fill_bits,		WHITE_COLOR, },
			{ spare_mask_bits,	BLACK_COLOR, },
			{ _spare_bits,		GREEN_COLOR, },
			{ spare_label_bits,	GREEN_COLOR, },
		},
		"Roue de secours",	0,
	}, {
		{
			{ fill_bits,		WHITE_COLOR, },
			{ repairs_mask_bits,	BLACK_COLOR, },
			{ _repairs_bits,	GREEN_COLOR, },
			{ repairs_label_bits,	GREEN_COLOR, },
		},
		"R\351parations",	0,

	}, {
		{
			{ fill_bits,		WHITE_COLOR, },
			{ go_mask_bits,		BLACK_COLOR, },
			{ _go_bits,		GREEN_COLOR, },
			{ go_label_bits,	GREEN_COLOR, },
		},
		"Roulez",	0,
	}, {
		{
			{ fill_bits,		WHITE_COLOR, },
			{ end_mask_bits,	BLACK_COLOR, },
			{ _end_bits,		GREEN_COLOR, },
			{ 0,			0, },
		},
		"Fin de limite",	0,
	}, {
		{
			{ fill_bits,		WHITE_COLOR, },
			{ extra_mask_bits,	BLACK_COLOR, },
			{ _extra_bits,		BLUE_COLOR, },
			{ extra_label_bits,	BLUE_COLOR, },
		},
		"Citerne d'essence",	0,
	}, {
		{
			{ fill_bits,		WHITE_COLOR, },
			{ puncture_mask_bits,	BLACK_COLOR, },
			{ _puncture_bits,	BLUE_COLOR, },
			{ puncture_label_bits,	BLUE_COLOR, },
		},
		"Increvable",	0,
	}, {
		{
			{ fill_bits,		WHITE_COLOR, },
			{ ace_mask_bits,	BLACK_COLOR, },
			{ _ace_bits,		BLUE_COLOR, },
			{ ace_label_bits,	BLUE_COLOR, },
		},
		"As du volant",	0,
	}, {
		{
			{ fill_bits,		WHITE_COLOR, },
			{ right_mask_bits,	BLACK_COLOR, },
			{ _right_bits,		RED_COLOR, },
			{ right_label_bits,	RED_COLOR, },
		},
		"V\351hicule prioritaire",	0,
	}
};

struct card	*cards;

extern char	deck_red_bits[], deck_blue_bits[], deck_mask_bits[];

struct card color_deck = {
	{
		{ fill_bits,		GREEN_COLOR, },
		{ deck_mask_bits,	BLACK_COLOR, },
		{ NULL,			0, },
		{ NULL,			0, },
	},
	"",
	0,
};

struct card mono_deck = {
	{
		{ fill_bits,		GREY_COLOR, },
		{ deck_mask_bits,	BLACK_COLOR, },
		{ NULL,			0, },
		{ NULL,			0, },
	},
	"",
	0,
};

extern char	blank_bits[];

struct card color_blank = {
	{
		{ blank_bits,	GREY_COLOR,  },
		{ NULL,		0 },
		{ NULL,		0 },
		{ NULL,		0 },
	},
	"",
	0
};

struct card mono_blank =  {
	{
		{ blank_bits,	WHITE_COLOR,  },
		{ NULL,		0 },
		{ NULL,		0 },
		{ NULL,		0 },
	},
	"",
	0
};

struct card	deck;
struct card	blank;

static void
BitmapBitsPut (Display *dpy, Drawable d, GC gc, Pixmap mask, GC mask_gc, void *bits)
{
	XGCValues	gc_values;
	XImage		ximage;

	ximage.height = HEIGHT;
	ximage.width = WIDTH;
	ximage.depth = 1;
	ximage.bits_per_pixel = 1;
	ximage.xoffset = 0;
	ximage.format = XYPixmap;
	ximage.data = bits;
	ximage.byte_order = LSBFirst;
	ximage.bitmap_unit = 8;
	ximage.bitmap_bit_order = LSBFirst;
	ximage.bitmap_pad = 8;
	ximage.bytes_per_line = (WIDTH+7) / 8;
	XPutImage (dpy, mask, mask_gc, &ximage, 0, 0, 0, 0, WIDTH, HEIGHT);
	gc_values.clip_mask = mask;
	XChangeGC (dpy, gc, GCClipMask, &gc_values);
	XFillRectangle (dpy, d, gc, 0, 0, WIDTH, HEIGHT);
	gc_values.clip_mask = None;
	XChangeGC (dpy, gc, GCClipMask, &gc_values);
}

static void
init_card (struct card *card, GC back_gc)
{
	int		i;
	GC		mask_gc;
	Pixmap		mask;
	int		len, width;
	int		x;

	mask = XCreatePixmap (dpy, DefaultRootWindow (dpy), WIDTH, HEIGHT, 1);

	mask_gc = XCreateGC (dpy, mask, 0, 0);

	card->bits = XCreatePixmap(dpy, DefaultRootWindow(dpy), WIDTH, HEIGHT,
				      DefaultDepth(dpy, screen));
	XFillRectangle (dpy, card->bits, back_gc,
			0, 0, WIDTH, HEIGHT);
	for (i = 0; i < MAXBITMAPS && card->init[i].bits != 0; i++) {
		BitmapBitsPut (dpy, card->bits,
				colorMap[card->init[i].color].gc,
				mask, mask_gc,
				card->init[i].bits);
	}
	len = strlen (card->label);
	width = XTextWidth (font, card->label, len);
	x = WIDTH/20;
	if (x + width > (WIDTH - x)) {
		x = (WIDTH - width) / 2;
	}
	XDrawString (dpy, card->bits, text_gc, x,
		     HEIGHT / 20 + font->ascent, card->label, len);
	x = WIDTH - WIDTH/20 - width;
	if (x < WIDTH/20) {
		x = (WIDTH - width) / 2;
	}
	XDrawString (dpy, card->bits, text_gc, x,
		      HEIGHT - HEIGHT/20 - font->descent,
		      card->label, len);
	XFreeGC (dpy, mask_gc);
	XFreePixmap (dpy, mask);
}

Pixmap	card_border;

static void
center_text(Drawable d, GC gc, XFontStruct *f, int y, char *l)
{
    int	    len, width;
    int	    x;

    len = strlen (l);
    width = XTextWidth (f, l, len);
    x = (WIDTH - width) / 2;
    XDrawString (dpy, d, gc, x, y, l, len);
}

static void
init_cards (GC back_gc)
{
	int		i;
	GC		gc;
	XGCValues	gc_values;
	Pixmap		rest;

	setbuf(stderr, NULL);

	card_border = XCreateBitmapFromData (dpy, DefaultRootWindow(dpy), fill_bits, WIDTH, HEIGHT);
	rest = XCreateBitmapFromData (dpy, DefaultRootWindow (dpy), deck_mask_bits, WIDTH, HEIGHT);
	gc_values.function = GXor;
	gc = XCreateGC (dpy, card_border, GCFunction, &gc_values);
	XCopyArea (dpy, rest, card_border, gc, 0, 0, WIDTH, HEIGHT, 0, 0);
	XFreePixmap (dpy, rest);
	XFreeGC (dpy, gc);

	init_card (&blank, back_gc);
	init_card (&deck, back_gc);
	gc_values.font = backFont->fid;
	gc_values.foreground = colorMap[RED_COLOR].pixel;
	gc = XCreateGC (dpy, deck.bits, GCFont|GCForeground, &gc_values);
	center_text (deck.bits, gc, backFont, HEIGHT / 2 - backFont->descent,
		     "Mille");
	XSetForeground (dpy, gc, colorMap[BLUE_COLOR].pixel);
	center_text (deck.bits, gc, backFont, HEIGHT / 2 + backFont->ascent,
		     "Bornes");
	XFreeGC (dpy, gc);

	for (i = 0; i < (NUM_CARDS - 1); i++) {
		init_card (&cards[i], back_gc);
	}
}

void
init_color_cards (void)
{
	deck = color_deck;
	blank = color_blank;
	cards = color_cards;
	init_cards (colorMap[GREY_COLOR].gc);
}

void
init_mono_cards(void)
{
	deck = mono_deck;
	blank = mono_blank;
	cards = color_cards;
	init_cards (colorMap[WHITE_COLOR].gc);
}

static int	oldx, oldy;
static GC	copyGC;
int		clip_cards = 1;

void
drawIm (Display *d, Window w, struct card *c, int x, int y, XRectangle *clip)
{
    XGCValues	gc_values;
    int	mask;
    int	srcx, srcy;
    int	dstx, dsty;
    int	width, height;

    if (!copyGC)
    {
	mask = GCGraphicsExposures;
	if (clip_cards) {
	    gc_values.clip_mask = card_border;
	    gc_values.clip_x_origin = x;
	    gc_values.clip_y_origin = y;
	    mask |= GCClipXOrigin|GCClipYOrigin|GCClipMask;
	}
	gc_values.graphics_exposures = False;
	copyGC = XCreateGC (d, w, mask, &gc_values);
    }
    else
    {
	if (clip_cards && (oldx != x || oldy != y))
	{
	    gc_values.clip_x_origin = x;
	    gc_values.clip_y_origin = y;
	    XChangeGC (d, copyGC, GCClipXOrigin|GCClipYOrigin, &gc_values);
	}
    }
    srcx = 0;
    srcy = 0;
    dstx = x;
    dsty = y;
    width = WIDTH;
    height = HEIGHT;
    if (clip) {
	if (dstx < clip->x) {
	    srcx = clip->x - dstx;
	    dstx += srcx;
	    width -= srcx;
	}
	if (clip->x + clip->width < dstx + width)
	    width = clip->x + clip->width - dstx;
	if (dsty < clip->y) {
	    srcy = clip->y - dsty;
	    dsty += srcy;
	    height -= srcy;
	}
	if (clip->y + clip->height < dsty + height)
	    height = clip->y + clip->height - dsty;
    }
    XCopyArea(d, c->bits, w, copyGC, srcx, srcy, width, height, dstx, dsty);
    oldx = x;
    oldy = y;
}

void
displayCard (Display *d, Window w, int card, int x, int y, XRectangle *clip)
{
    if (card < 0 || card >= NUM_CARDS) {
	drawIm (d, w, &blank, x, y, clip);
    } else {
	drawIm (d, w, &cards[card], x, y, clip);
    }
}