File: paddle.c

package info (click to toggle)
xboing 2.4-19
  • links: PTS
  • area: main
  • in suites: potato
  • size: 3,076 kB
  • ctags: 1,516
  • sloc: ansic: 17,999; makefile: 40; sh: 24
file content (407 lines) | stat: -rw-r--r-- 10,270 bytes parent folder | download | duplicates (7)
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
/*
 * XBoing - An X11 blockout style computer game
 *
 * (c) Copyright 1993, 1994, 1995, Justin C. Kibell, All Rights Reserved
 *
 * The X Consortium, and any party obtaining a copy of these files from
 * the X Consortium, directly or indirectly, is granted, free of charge, a
 * full and unrestricted irrevocable, world-wide, paid up, royalty-free,
 * nonexclusive right and license to deal in this software and
 * documentation files (the "Software"), including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons who receive
 * copies from any such party to do so.  This license includes without
 * limitation a license to do the foregoing actions under any patents of
 * the party supplying this software to the X Consortium.
 *
 * In no event shall the author be liable to any party for direct, indirect,
 * special, incidental, or consequential damages arising out of the use of
 * this software and its documentation, even if the author has been advised
 * of the possibility of such damage.
 *
 * The author specifically disclaims any warranties, including, but not limited
 * to, the implied warranties of merchantability and fitness for a particular
 * purpose.  The software provided hereunder is on an "AS IS" basis, and the
 * author has no obligation to provide maintenance, support, updates,
 * enhancements, or modifications.
 */

/* 
 * =========================================================================
 *
 * $Id: paddle.c,v 1.1.1.1 1994/12/16 01:36:45 jck Exp $
 * $Source: /usr5/legends/jck/xb/master/xboing/paddle.c,v $
 * $Revision: 1.1.1.1 $
 * $Date: 1994/12/16 01:36:45 $
 *
 * $Log: paddle.c,v $
 * Revision 1.1.1.1  1994/12/16  01:36:45  jck
 * The XBoing distribution requires configuration management. This is why the
 * cvs utility is being used. This is the initial import of all source etc..
 *
 *
 * =========================================================================
 */

/*
 *  Include file dependencies:
 */

#include <stdio.h>
#include <xpm.h>

#include "bitmaps/paddle/padsml.xpm"
#include "bitmaps/paddle/padmed.xpm"
#include "bitmaps/paddle/padhuge.xpm"

#include "error.h"
#include "init.h"
#include "stage.h"
#include "blocks.h"
#include "misc.h"
#include "main.h"
#include "special.h"

#include "paddle.h"

/*
 *  Internal macro definitions:
 */

/*
 *  Internal type declarations:
 */

/*
 *  Internal variable declarations:
 */

static Pixmap paddleSmallPixmap, paddleMediumPixmap, paddleHugePixmap;
static Pixmap paddleSmallMask, paddleMediumMask, paddleHugeMask;

int	paddlePos;
int	currentPaddleSize;
static int	oldX;
static int xpos_old;
int reverseOn, stickyOn;

#if NeedFunctionPrototypes
void DrawPaddle(Display *display, Window window, int x, int y, int size)
#else
void DrawPaddle(display, window, x, y, size)
	Display *display;
	Window window;
	int x;
	int y;
	int size;
#endif
{
	/* Switch on the paddle size */
	switch (size)
	{
		case PADDLE_SMALL:
			RenderShape(display, window, paddleSmallPixmap, paddleSmallMask,
				x - 20, y, 40, 15, True);
			break;

		case PADDLE_MEDIUM:
			RenderShape(display, window, paddleMediumPixmap, paddleMediumMask,
				x - 25, y, 50, 15, True);
			break;

		case PADDLE_HUGE:
			RenderShape(display, window, paddleHugePixmap, paddleHugeMask,
				x - 35, y, 70, 15, True);
			break;
	}
}

#if NeedFunctionPrototypes
void InitialisePaddle(Display *display, Window window, Colormap colormap)
#else
void InitialisePaddle(display, window, colormap)
	Display *display;
	Window window;
	Colormap colormap;
#endif
{
    XpmAttributes   attributes;
	int		    XpmErrorStatus;

    attributes.valuemask = XpmColormap;
	attributes.colormap = colormap;

	/* Create the xpm pixmap paddles */
	XpmErrorStatus = XpmCreatePixmapFromData(display, window, paddlesmall_xpm,
		&paddleSmallPixmap, &paddleSmallMask, &attributes);
	HandleXPMError(display, XpmErrorStatus, "InitialisePaddle()");

	XpmErrorStatus = XpmCreatePixmapFromData(display, window, paddlemedium_xpm,
		&paddleMediumPixmap, &paddleMediumMask, &attributes);
	HandleXPMError(display, XpmErrorStatus, "InitialisePaddle()");

	XpmErrorStatus = XpmCreatePixmapFromData(display, window, paddlehuge_xpm,
		&paddleHugePixmap, &paddleHugeMask, &attributes);
	HandleXPMError(display, XpmErrorStatus, "InitialisePaddle()");

	/* Free the xpm pixmap attributes */
	XpmFreeAttributes(&attributes);
}

#if NeedFunctionPrototypes
void SetReverseOff(void)
#else
void SetReverseOff()
#endif
{
	/* Set the reverse state off */
	reverseOn = False;
}

#if NeedFunctionPrototypes
void ToggleReverse(Display *display)
#else
void ToggleReverse(display)
	Display *display;
#endif
{
	/* Set the reverse state */
	if (reverseOn == True)
		reverseOn = False;
	else
		reverseOn = True;

	/* Update the display */
	DrawSpecials(display);
}

#if NeedFunctionPrototypes
void FreePaddle(Display *display)
#else
void FreePaddle(display)
	Display *display;
#endif
{
	/* Free the paddle pixmaps and masks */
	if (paddleSmallPixmap)	XFreePixmap(display, paddleSmallPixmap);
	if (paddleMediumPixmap)	XFreePixmap(display, paddleMediumPixmap);
	if (paddleHugePixmap)	XFreePixmap(display, paddleHugePixmap);

	if (paddleSmallMask)	XFreePixmap(display, paddleSmallMask);
	if (paddleMediumMask)	XFreePixmap(display, paddleMediumMask);
	if (paddleHugeMask)		XFreePixmap(display, paddleHugeMask);
}

#if NeedFunctionPrototypes
void MovePaddle(Display *display, Window window, int direction, int size, 
	int xpos)
#else
void MovePaddle(display, window, direction, size, xpos)
	Display *display;
	Window window;
	int direction;
	int size;
	int xpos;
#endif
{
	static int	y = (PLAY_HEIGHT - DIST_BASE);

	/* Handle the paddle moving either left or right */
	switch (direction)
	{
		case PADDLE_LEFT:
			/* Move to the left or if reverse to right*/
			if (reverseOn)
				paddlePos += PADDLE_VEL;
			else
				paddlePos -= PADDLE_VEL;
			break;

		case PADDLE_RIGHT:
			/* Move to the right or if reverse to left*/
			if (reverseOn)
				paddlePos -= PADDLE_VEL;
			else
				paddlePos += PADDLE_VEL;
			break;

		case PADDLE_NONE:
			if (reverseOn)
			{
				xpos = PLAY_WIDTH - xpos;
			}
			break;
	}

	/* Switch on the size of the paddle */
	switch (size)
	{
		case PADDLE_SMALL:
			/* Handle the small paddle */
			if (xpos > 0)
				paddlePos = xpos - (MAIN_WIDTH / 2) + 20;

			/* Stop the paddle from going past the walls */
			if (paddlePos < 20) paddlePos = 20;
			if (paddlePos > (PLAY_WIDTH - 20)) 
				paddlePos = (PLAY_WIDTH - 20);

			/* Clear the old position */
			XClearArea(display, window, oldX, y, 40, 15, False);
			oldX = paddlePos - 20; 

			DrawPaddle(display, window, paddlePos, PLAY_HEIGHT - DIST_BASE, 
				PADDLE_SMALL);
			break;

		case PADDLE_MEDIUM:
			/* Handle the medium paddle */
			if (xpos > 0)
				paddlePos = xpos - (MAIN_WIDTH / 2) + 25;

			/* Stop the paddle from going past the walls */
			if (paddlePos < 25) paddlePos = 25;
			if (paddlePos > (PLAY_WIDTH - 25)) 
				paddlePos = (PLAY_WIDTH - 25);

			/* Clear the old position */
			XClearArea(display, window, oldX, y, 50, 15, False);
			oldX = paddlePos - 25; 

			DrawPaddle(display, window, paddlePos, 
				PLAY_HEIGHT - DIST_BASE, PADDLE_MEDIUM);
			break;

		case PADDLE_HUGE:
			/* Handle the large paddle */
			if (xpos > 0)
				paddlePos = xpos - (MAIN_WIDTH / 2) + 35;

			/* Stop the paddle from going past the walls */
			if (paddlePos < 35) paddlePos = 35;
			if (paddlePos > (PLAY_WIDTH - 35)) 
				paddlePos = (PLAY_WIDTH - 35);

			/* Clear the old position */
			XClearArea(display, window, oldX, y, 70, 15, False);
			oldX = paddlePos - 35; 

			DrawPaddle(display, window, paddlePos, 
				PLAY_HEIGHT - DIST_BASE, PADDLE_HUGE);
			break;
	}
}

#if NeedFunctionPrototypes
void FlushPaddleBackingStore(Display *display, Window window)
#else
void FlushPaddleBackingStore(display, window)
	Display *display;
	Window window;
#endif
{
	static int y = (PLAY_HEIGHT - DIST_BASE);

	/* Clear the entire paddle area */
	XClearArea(display, window, 0, y, PLAY_WIDTH, 15, False);
}

#if NeedFunctionPrototypes
int GetPaddleSize(void)
#else
int GetPaddleSize()
#endif
{
	/* Switch on the current paddle size */
	switch (currentPaddleSize)
	{
		case PADDLE_SMALL:
			/* Return the size of the small paddle in pixels */
			return 40;

		case PADDLE_MEDIUM:
			/* Return the size of the medium paddle in pixels */
			return 50;

		case PADDLE_HUGE:
			/* Return the size of the huge paddle in pixels */
			return 70;
	}

	/* Bug if this happens */
	return 0;
}

#if NeedFunctionPrototypes
void ResetPaddleStart(Display *display, Window window)
#else
void ResetPaddleStart(display, window)
	Display *display;
	Window window;
#endif
{
	paddlePos = PLAY_WIDTH / 2;
	oldX = PLAY_WIDTH / 2;
	xpos_old = oldX;

	/* Get rid of old paddle image and start new one */
	FlushPaddleBackingStore(display, window);
	MovePaddle(display, window, PADDLE_NONE, currentPaddleSize, 0);
}

#if NeedFunctionPrototypes
void RedrawPaddle(Display *display, Window window)
#else
void RedrawPaddle(display, window)
	Display *display;
	Window window;
#endif
{
	MovePaddle(display, window, PADDLE_NONE, currentPaddleSize, 0);
}

#if NeedFunctionPrototypes
void ChangePaddleSize(Display *display, Window window, int type)
#else
void ChangePaddleSize(display, window, type)
	Display *display;
	Window window;
	int type;
#endif
{
	/* 
	 * This function will erase the paddle and then change its size and
	 * redraw it. If the paddle cannot grow as it is at the biggest state
	 * then it will stay the same. Likewise for the smallest paddle.
	 */

	FlushPaddleBackingStore(display, window);

	if (type == PAD_SHRINK_BLK)
	{
		if (currentPaddleSize == PADDLE_MEDIUM)
		{
			/* Shrink the paddle */
        	currentPaddleSize = PADDLE_SMALL;
		} else if (currentPaddleSize == PADDLE_HUGE)
		{
			/* Shrink the paddle */
        	currentPaddleSize = PADDLE_MEDIUM;
		}
	}
	else
	{
		if (currentPaddleSize == PADDLE_SMALL)
		{
			/* Grow the paddle */
        	currentPaddleSize = PADDLE_MEDIUM;
		} else if (currentPaddleSize == PADDLE_MEDIUM)
		{
			/* Grow the paddle */
        	currentPaddleSize = PADDLE_HUGE;
		}
	}

	/* Draw the new paddle in its new size */
	RedrawPaddle(display, window);
}