File: puzzle.c

package info (click to toggle)
xslideshow 3.1-7
  • links: PTS
  • area: non-free
  • in suites: sarge
  • size: 792 kB
  • ctags: 1,375
  • sloc: ansic: 14,006; makefile: 897; sh: 1
file content (425 lines) | stat: -rw-r--r-- 9,553 bytes parent folder | download | duplicates (2)
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
/*
	puzzle.c - animate puzzle

	Author:	Susumu Shiohara (shiohara@tpp.epson.co.jp)

		Copyright 1993-1997 by Susumu Shiohara

			All Rights Reserved

*/

#include "xslideshow.h"
#include "animproto.h"

typedef enum {
	UP=0,
	DOWN,
	LEFT,
	RIGHT,
	DMAX
}DIRS;

typedef struct {
	int	dir[DMAX];
	int	col;
	int	row;
}puzzleData;

typedef struct {
	DIRS	dir;
	int	count;
}historyData;

static	puzzleData current;
static	Window *wd=NULL;
static	Window firstw;
static	int xof,yof,pw,ph,pofx,pofy,steps,puzzleCount;
static	historyData *history=NULL;
static	int hasRes=False;
static	int mapsize;

/* Exchange the block at dir for current block */
#if defined(__STDC__) || defined(__cplusplus)
static void moveCurrent(DIRS dir,int count)
#else
static void moveCurrent(dir,count)
DIRS dir;
int	count;
#endif
{
int x1,y1,x2,y2,col,row;
Window tmpw;

	while(count--){
	switch(dir){
		case UP   : col=current.col;   row=current.row-1 ; break;
		case DOWN : col=current.col;   row=current.row+1 ; break;
		case LEFT : col=current.col-1; row=current.row   ; break;
		case RIGHT: col=current.col+1; row=current.row   ; break;
		default: break;
	}

	x1 = xof + pofx + current.col * steps;
	y1 = yof + pofy + current.row * steps;
	x2 = xof + pofx + col * steps;
	y2 = yof + pofy + row * steps;

	tmpw = wd[current.row * pw + current.col];
	XLowerWindow(theDisp,tmpw);

	/* Move the block at dir on the bresenham line */
	move_on_bresenham_line(
						wd[row * pw + col],
						x2, y2,
						x1, y1,
						2, app_data.moveOnTheLineTicks);

	/* Move the current block on the bresenham line */
	XMoveWindow(theDisp,tmpw,x2,y2);

	wd[current.row * pw + current.col] = wd[row * pw + col];
	wd[row * pw + col] = tmpw;

	/* Update current block informations */
	current.col = col;
	current.row = row;

	if(row == 0)		current.dir[UP]	  = 0;
	else				current.dir[UP]	  = 1;
	if(row == (ph - 1))	current.dir[DOWN] = 0;
	else				current.dir[DOWN] = 1;
	if(col == 0)		current.dir[LEFT] = 0;
	else				current.dir[LEFT] = 1;
	if(col == (pw - 1))	current.dir[RIGHT]= 0;
	else				current.dir[RIGHT]= 1;
	}
}

static void clearUnderPicture()
{
	XClearArea(
			theDisp,
			theWindow,
			xof + pofx,
			yof + pofy,
			pw * steps,
			ph * steps,
			False
			);
}

static void PuzzleIt()
{
int i,j,count,countmax,m;
DIRS predir,dir;
Window tmpw;
XImage *subImage;
Pixmap pic,subPic;
XSetWindowAttributes xswa;
XGCValues gcv;
GC gc;

	if(app_data.puzzleSteps > MIN(gim.subImageList->width,gim.subImageList->height))
		steps = MIN(gim.subImageList->width,gim.subImageList->height);
	else
		steps = app_data.puzzleSteps;

	if(steps == 0) steps = 1;

	xof = (windowWidth - gim.subImageList->width) / 2;
	yof = (windowHeight - gim.subImageList->height) / 2;

	if((windowWidth > gim.subImageList->width) && (windowHeight > gim.subImageList->height)){

		pw = gim.subImageList->width / steps;
		ph = gim.subImageList->height / steps;
		pofx = (gim.subImageList->width % steps) / 2;
		pofy = (gim.subImageList->height % steps) / 2;

	}
	else if ((windowWidth > gim.subImageList->width) && (windowHeight <= gim.subImageList->height)){

		pw = gim.subImageList->width / steps;
		ph = windowHeight / steps;
		pofx = (gim.subImageList->width % steps) / 2;
		pofy = (windowHeight % steps + gim.subImageList->height - windowHeight) / 2;

	}
	else if ((windowWidth <= gim.subImageList->width) && (windowHeight > gim.subImageList->height)){

		pw = windowWidth / steps;
		ph = gim.subImageList->height / steps;
		pofx = (windowWidth % steps + gim.subImageList->width - windowWidth) / 2;
		pofy = (gim.subImageList->height % steps) / 2;

	}
	else /* if ((windowWidth <= gim.subImageList->width) && (windowHeight <= gim.subImageList->height)) */ {

		pw = windowWidth / steps;
		ph = windowHeight / steps;
		pofx = (windowWidth % steps + gim.subImageList->width - windowWidth) / 2;
		pofy = (windowHeight % steps + gim.subImageList->height - windowHeight) / 2;
	}

	/* Can't puzzle 1x?, ?x1, 2x2 blocks */
	if(pw==1 || ph==1 || (pw==2 && ph==2)) {
		hasRes = False;
		return; 
	}

	gcv.function = GXcopy;
	gcv.foreground = bcol;
	gcv.background = bcol;
	if((pic = XCreatePixmap( theDisp,theWindow,
							gim.subImageList->width,gim.subImageList->height,
							DefaultDepth(theDisp,theScreenNumber))) == (Pixmap)0){
		fprintf(stderr,"xslideshow: Pixmap creation error.\n");
		goodbyekiss();
	}

	gc = XCreateGC(theDisp,pic,GCFunction | GCForeground | GCBackground,&gcv );
	XPutImage(theDisp,pic,gc,theImage,0,0,0,0,gim.subImageList->width,gim.subImageList->height);
	XFreeGC(theDisp,gc);

	m = pw * ph;
	puzzleCount = m * 2;
	history = (historyData *)XtMalloc(sizeof(historyData) * puzzleCount);
	if(history == (historyData *)NULL){
		fprintf(stderr,"xslideshow: memory allocation error.\n");
		goodbyekiss();
	}

	wd = (Window *)XtMalloc(sizeof(Window) * m);
	if(wd == (Window *)NULL){
		fprintf(stderr,"xslideshow: memory allocation error.\n");
		goodbyekiss();
	}

	/* Separate the picture into small blocks */
	firstw = XCreateSimpleWindow( theDisp,theWindow,
								xof + pofx,yof + pofy,
								steps - app_data.borderWidth * 2,
								steps - app_data.borderWidth * 2,
								app_data.borderWidth,
								bcol, bcol	);

	if(firstw == (Window)0) {
		XFreePixmap(theDisp,pic);

		XtFree((char *)history);
		history = NULL;

		XtFree((char *)wd);
		wd = NULL;

		hasRes = False;
		return;
	}

	xswa.override_redirect = True;
	XChangeWindowAttributes(theDisp,firstw,CWOverrideRedirect,&xswa);

	for(i = 0; i < m; i++){

		wd[i] = XCreateSimpleWindow( theDisp,theWindow,
									xof + pofx + (i % pw) * steps,
									yof + pofy + (i / pw) * steps,
									steps - app_data.borderWidth * 2,
									steps - app_data.borderWidth * 2,
									app_data.borderWidth,
									bcol, bcol	);

		if(wd[i] == (Window)0) {

			for(j = 0; j < i; j++)
				XDestroyWindow(theDisp,wd[j]);

			XDestroyWindow(theDisp,firstw);
			XFreePixmap(theDisp,pic);

			XtFree((char *)history);
			history = NULL;

			XtFree((char *)wd);
			wd = NULL;

			hasRes = False;
			return;

		}

		xswa.override_redirect = True;
		XChangeWindowAttributes(theDisp,wd[i],CWOverrideRedirect,&xswa);

		subPic = XCreatePixmap( theDisp,wd[i],
								steps, steps,
								DefaultDepth(theDisp,theScreenNumber)	);

		subImage = XGetImage( theDisp,pic,
								pofx + (i % pw) * steps,
								pofy + (i / pw) * steps,	
								steps, steps,
								XAllPlanes(), ZPixmap	);

		gc = XCreateGC(theDisp,wd[i],GCFunction | GCForeground | GCBackground,&gcv );
		XPutImage(theDisp,subPic,gc,subImage,0,0,0,0,steps,steps);
		XSetWindowBackgroundPixmap(theDisp,wd[i],subPic);

		XDestroyImage(subImage);
		XFreePixmap(theDisp,subPic);
		XFreeGC(theDisp,gc);

		myevent();	/* Get X event */
	}

	XFreePixmap(theDisp,pic);
	tmpw   = wd[0];
	wd[0]  = firstw;
	firstw = tmpw;
	XLowerWindow(theDisp,firstw);


	/* Puzzle (Move block randomly) */
	current.dir[UP]   =0;
	current.dir[DOWN] =1;
	current.dir[LEFT] =0;
	current.dir[RIGHT]=1;
	current.col=0;
	current.row=0;
	predir=dir=UP;

	for(i = 0; i < puzzleCount; i++){

		while(True){
			dir = myrandom() % DMAX;
			if(predir != dir && current.dir[dir]){
				switch(dir){
				case UP	  : countmax = current.row;	       predir = DOWN ; break;
				case DOWN : countmax = ph - current.row-1; predir = UP   ; break;
				case LEFT : countmax = current.col;	       predir = RIGHT; break;
				case RIGHT: countmax = pw - current.col-1; predir = LEFT ; break;
				default: break;
				}
				break;
			}
		}

		count = (myrandom() % countmax) + 1;
		moveCurrent(dir,count);
		history[i].dir  = dir;
		history[i].count = count;
	}

	/* Display all blocks */
	for(i = 0; i < m; i++) {
		XLowerWindow(theDisp,wd[i]);
		XMapWindow(theDisp,wd[i]);
	}

	hasRes = True;
	return;
}

static void Resolve()
{
DIRS dir;
int	i;

	/* The current block was placed on history[puzzleCount-1] */
	for(i = puzzleCount - 1; i >= 0; i--){
		switch(history[i].dir){
		case UP	  : dir = DOWN ; break;
		case DOWN : dir = UP   ; break;
		case LEFT : dir = RIGHT; break;
		case RIGHT: dir = LEFT ; break;
		default: break;
		}
		moveCurrent(dir,history[i].count);
		myusleep(app_data.puzzleTicks);
		if(gotSomeAction == True)
			return;
	}
	/* Display the first block, Then finished */
	XMapWindow(theDisp,firstw);
}

static void freeResources()
{
int i,m;

	m = pw * ph;
	for(i = 0; i < m; i++)
		XDestroyWindow(theDisp,wd[i]);

	XDestroyWindow(theDisp,firstw);

	XtFree((char *)history);
	history = NULL;

	XtFree((char *)wd);
	wd = NULL;

	hasRes = False;

	myusleep(0); /* Flush */
}

#if defined(__STDC__) || defined(__cplusplus)
ActionStatus xpuzzleshow(char *fname)
#else
ActionStatus xpuzzleshow(fname)
char *fname;
#endif
{
	mapsize = gim.subImageList->mapsize;

	PreDisplay();

	if(app_data.showFileName)
		createFileNameWindow(fname);

	PuzzleIt();

	ShowImage(	(windowWidth - gim.subImageList->width) / 2,
				(windowHeight - gim.subImageList->height) / 2,
				gim.subImageList->width, gim.subImageList->height);

	if(app_data.puzzleFade){
		PreFadeColors(mapsize);
		FadeColors(False,mapsize);
	}

	StoreColors(xcolors,mapsize);

	if(hasRes) {
		clearUnderPicture();
		myusleep(3000000); /* wait 3 sec */
		Resolve();
	}

	if(app_data.showFileName)
		raiseFileNameWindow();

	return(mywait());
}

#if defined(__STDC__) || defined(__cplusplus)
void postxpuzzleshow(char *fname)
#else
void postxpuzzleshow(fname)
char *fname;
#endif
{
	if(app_data.showFileName)
		removeFileNameWindow();

	if(app_data.puzzleFade)
		FadeColors(True,mapsize);

	if(hasRes) {
		freeResources();
		XClearWindow(theDisp,theWindow);
	}
}