summaryrefslogtreecommitdiff
path: root/src/pong/ao_pong.c
blob: 2492468c2c58f65bc799235f91fae6be71a687ac (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
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
/*
 * Copyright © 2011 Keith Packard <keithp@keithp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

#include "ao.h"
#include <ao_exti.h>
#include <ao_event.h>
#include <ao_button.h>
#include <ao_boot.h>
#include <ao_vga.h>
#include "ao_pong_text.h"

// uint8_t ao_sensor_errors;

#define WIN_SCORE	11

#define BALL_WIDTH	5
#define BALL_HEIGHT	5

#define PADDLE_WIDTH	5
#define PADDLE_HEIGHT	25
#define PADDLE_OFFSET	20

struct point {
	int	x, y;
};

struct rect {
	int	x, y, w, h;
};

static struct ao_data	ao_data;

static const int	player_led[2] = { AO_LED_GREEN, AO_LED_BLUE };
static int		player_value[2];
static int		player_score[2];

#define player_filter(old, new)	(((old) + (new)) >> 1)

static struct rect	player[2];

static struct rect	ball;

static int	ball_speed;
static int	ball_e;
static int	ball_dx, ball_dy;
static int	ball_step_x, ball_step_y;

static int	volley_count;

static int
intersect(struct rect *a, struct rect *b)
{
	return (a->x <= b->x + b->w && b->x <= a->x + a->w &&
		a->y <= b->y + b->h && b->y <= a->y + a->h);
}

static int
ao_ball_step(void)
{
	int	p;
	int	s;

	/* Move the ball */
	for (s = 0; s < ball_speed; s++) {
		ball_e += ball_dy;
		ball.x += ball_step_x;
		while (ball_e >= ball_dx) {
			ball_e -= ball_dx;
			ball.y += ball_step_y;

			/* bounce off walls */
			if (ball.y < 0 || ball.y + ball.h > AO_VGA_HEIGHT) {
				ball_step_y = -ball_step_y;
				ball.y += ball_step_y;
			}
		}

		/* player missed */
		if (ball.x <= 0)
			return 1;

		if (ball.x >= AO_VGA_WIDTH - BALL_WIDTH)
			return -1;

		/* bounce off paddles */

		for (p = 0; p < 2; p++) {
			if (intersect(&ball, &player[p])) {

				volley_count++;

				ball_speed = 3 + volley_count / 4;

				int	dy = (2 * (ball.y - player[p].y) + ball.h - player[p].h);
				int	dx = 20;

				ball_step_x = -ball_step_x;
				ball.x += ball_step_x;
				ball_step_y = 1;
				if (dy < 0) {
					ball_step_y = -1;
					dy = -dy;
				}
				ball_e = 0;
				ball_dx = dx;
				ball_dy = dy;
			}
		}
	}

	return 0;
}

#define AO_ADC_MAX	4095

static void
ao_paddle_set(int p, int value)
{
	int	pos = value * (AO_VGA_HEIGHT - PADDLE_HEIGHT) / AO_ADC_MAX;

	player[p].y = pos;
}

enum pong_state {
	pong_start,
	pong_serve,
	pong_volley,
	pong_endgame,
};

static enum pong_state pong_state;
static int pong_timer;
static int pong_server;

#define PONG_SERVE_WAIT	90

static enum pong_state
ao_pong_start(void)
{
	int p;

	pong_timer = 1;

	pong_server = ao_time() & 1;

	for (p = 0; p < 2; p++) {
		ao_led_off(player_led[p]);
		player_score[p] = 0;
	}

	return pong_serve;
}

static enum pong_state
ao_pong_serve(void)
{
	int seed;
	if (--pong_timer > 0)
		return pong_serve;

	seed = ao_time();

	ball.y = (AO_VGA_HEIGHT - BALL_HEIGHT) / 2;

	ball_speed = 3;
	volley_count = 0;

	if (pong_server) {
		ball.x = player[1].x - BALL_WIDTH;
		ball_step_x = -1;
	} else {
		ball.x = player[0].x + PADDLE_WIDTH;
		ball_step_x = 1;
	}

	ball.w = BALL_WIDTH;
	ball.h = BALL_HEIGHT;

	ball_dx = 20;
	ball_dy = (seed & 7) * 2;
	ball_e = 0;

	seed >>= 3;

	if (seed & 1) {
		ball_step_y = 1;
	} else {
		ball_step_y = -1;
	}

	return pong_volley;
}

static enum pong_state
ao_pong_volley(void)
{
	int	miss = ao_ball_step();
	int	point;

	if (miss == 0)
		return pong_volley;

	point = (miss + 1) >> 1;
	player_score[point]++;
	if (player_score[point] == WIN_SCORE) {
		ao_led_on(player_led[point]);
		return pong_endgame;
	}

	pong_server = point;
	pong_timer = PONG_SERVE_WAIT;
	return pong_serve;
}

static void
ao_pong_step(void)
{
	int p;

	/* Paddles are always active */
	for (p = 0; p < 2; p++) {
		player_value[p] = player_filter(player_value[p], ao_data.adc.player[p]);
		ao_paddle_set(p, player_value[p]);
	}
	switch (pong_state) {
	case pong_start:
		pong_state = ao_pong_start();
		break;
	case pong_serve:
		pong_state = ao_pong_serve();
		break;
	case pong_volley:
		pong_state = ao_pong_volley();
		break;
	case pong_endgame:
		break;
	}
}

static void
ao_pong_rect(struct rect *r, uint32_t fill)
{
	ao_rect(&ao_vga_bitmap,
		r->x, r->y, r->w, r->h, fill, AO_COPY);
}

static void
ao_pong_score(int p, int value, uint32_t fill)
{
	int	x = AO_VGA_WIDTH / 2 + (p * 2 - 1) * 50 - 24;
	int	a, b;
	char	c[4];

	if (fill != 0) {
		a = value % 10;
		b = value / 10;
		if (b)
			c[0] = b + '0';
		else
			c[0] = ' ';
		c[1] = a + '0';
		c[2] = '\0';
		ao_pong_text(&ao_vga_bitmap, x, 30, c);
	}
}

#define NET_WIDTH	2
#define NET_HEIGHT	8

static void
ao_pong_net(uint32_t fill)
{
	int n;

	if (fill == 0)
		return;

	for (n = NET_HEIGHT/2; n < AO_VGA_HEIGHT - NET_HEIGHT; n += NET_HEIGHT * 2) {
		ao_rect(&ao_vga_bitmap,
			(AO_VGA_WIDTH - NET_WIDTH) >> 1,
			n,
			NET_WIDTH,
			NET_HEIGHT,
			1, AO_COPY);
	}
}

static void
ao_pong_draw(uint32_t fill)
{
	int p;

	for (p = 0; p < 2; p++) {
		ao_pong_rect(&player[p], fill);
		ao_pong_score(p, player_score[p], fill);
	}
	if (fill == 0 || pong_state == pong_volley)
		ao_pong_rect(&ball, fill);
	ao_pong_net(fill);
}

static void
ao_pong_redraw(void)
{
	ao_pong_draw(0);
	ao_pong_step();
	ao_pong_draw(1);
}

static void
ao_pong_setup(void)
{
	int	p;

	ao_rect(&ao_vga_bitmap,
		0, 0, AO_VGA_WIDTH, AO_VGA_HEIGHT,
		0, AO_COPY);

	ball.w = BALL_WIDTH;
	ball.h = BALL_HEIGHT;

	for (p = 0; p < 2; p++) {
		if (p)
			player[p].x = AO_VGA_WIDTH - PADDLE_OFFSET - PADDLE_WIDTH;
		else
			player[p].x = PADDLE_OFFSET;
		player[p].y = (AO_VGA_HEIGHT - PADDLE_HEIGHT) / 2;
		player[p].w = PADDLE_WIDTH;
		player[p].h = PADDLE_HEIGHT;
	}

	pong_state = pong_endgame;
}

static struct ao_task pong_task;

static void
ao_pong(void)
{
	ao_pong_setup();
	for (;;) {
		ao_vga_vblank = 0;
		while (ao_vga_vblank == 0)
			ao_sleep(&ao_vga_vblank);
		ao_data = ao_data_ring[ao_data_ring_prev(ao_data_head)];
		ao_pong_redraw();
	}
}

static struct ao_task event_task;

static void
ao_event(void) {
	struct ao_event event;

	for (;;) {
		ao_event_get(&event);
		if (event.value == 0)
			pong_state = pong_start;
	}
}

int
main(void)
{
	ao_clock_init();

	ao_task_init();

	ao_led_init(LEDS_AVAILABLE);
	ao_led_on(AO_LED_GREEN);
	ao_led_off(AO_LED_BLUE);
	ao_timer_init();
	ao_dma_init();
	ao_cmd_init();
	ao_spi_init();
	ao_exti_init();
	ao_button_init();
	ao_vga_init();

	ao_timer_set_adc_interval(1);

	ao_adc_init();
	ao_usb_init();

	ao_add_task(&pong_task, ao_pong, "pong");

	ao_add_task(&event_task, ao_event, "event");

	ao_vga_enable(1);

	ao_start_scheduler();
	return 0;
}