1 /* pong, Copyright (c) 2003 Jeremy English <jenglish@myself.com>
4 * Modified by Trevor Blackwell <tlb@tlb.org> to use analogtv.[ch] display.
5 * Also added gradual acceleration of the ball, shrinking of paddles, and
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that
11 * copyright notice and this permission notice appear in supporting
12 * documentation. No representations are made about the suitability of this
13 * software for any purpose. It is provided "as is" without express or
18 * TLB sez: I haven't actually seen a pong game since I was about 9. Can
19 * someone who has one make this look more realistic? Issues:
21 * - the font for scores is wrong. For example '0' was square.
22 * - was there some kind of screen display when someone won?
23 * - did the ball move smoothly, or was the X or Y position quantized?
25 * It could also use better player logic: moving the paddle even when the ball
26 * is going away, and making mistakes instead of just not keeping up with the
29 * There is some info at http://www.mameworld.net/discrete/Atari/Atari.htm#Pong
31 * It says that the original Pong game did not have a microprocessor, or even a
32 * custom integrated circuit. It was all discrete logic.
36 #include "screenhack.h"
38 /* #define OUTPUT_POS */
40 typedef struct _paddle {
50 typedef struct _ball {
62 static int paddle_rate;
65 static analogtv_input *inp;
66 static analogtv_reception reception;
68 static int paddle_ntsc[4];
69 static int field_ntsc[4];
70 static int ball_ntsc[4];
71 static int score_ntsc[4];
72 static int net_ntsc[4];
74 analogtv_font score_font;
77 PONG_W = ANALOGTV_VIS_LEN,
78 PONG_H = ANALOGTV_VISLINES,
85 if ( (ball.y <= PONG_TMARG) ||
86 (ball.y+ball.h >= PONG_H) )
103 paddle_rate = m_unit-1;
105 if (l_paddle.h > 10) l_paddle.h= l_paddle.h*19/20;
106 if (r_paddle.h > 10) r_paddle.h= r_paddle.h*19/20;
112 if ( ball.x + ball.w >= r_paddle.x &&
113 bx > 0 ) /*we are traveling to the right*/
115 if ((ball.y + ball.h > r_paddle.y) &&
116 (ball.y < r_paddle.y + r_paddle.h))
131 if (ball.x <= l_paddle.x + l_paddle.w &&
132 bx < 0 ) /*we are traveling to the left*/
134 if ( ball.y + ball.h > l_paddle.y &&
135 ball.y < l_paddle.y + l_paddle.h)
152 init_pong (Display *dpy, Window window)
154 tv=analogtv_allocate(dpy, window);
155 analogtv_set_defaults(tv, "");
156 tv->event_handler = screenhack_handle_event;
158 analogtv_make_font(dpy, window, &score_font,
161 /* If you think we haven't learned anything since the early 70s,
162 look at this font for a while */
163 analogtv_font_set_char(&score_font, '0',
170 analogtv_font_set_char(&score_font, '1',
177 analogtv_font_set_char(&score_font, '2',
184 analogtv_font_set_char(&score_font, '3',
191 analogtv_font_set_char(&score_font, '4',
198 analogtv_font_set_char(&score_font, '5',
205 analogtv_font_set_char(&score_font, '6',
212 analogtv_font_set_char(&score_font, '7',
219 analogtv_font_set_char(&score_font, '8',
226 analogtv_font_set_char(&score_font, '9',
234 score_font.y_mult *= 2;
235 score_font.x_mult *= 2;
238 printf("screen(%d,%d,%d,%d)\n",0,0,PONG_W,PONG_H);
241 inp=analogtv_input_allocate();
242 analogtv_setup_sync(inp, 0, 0);
244 reception.input = inp;
245 reception.level = 2.0;
249 reception.multipath = frand(1.0);
252 reception.multipath=0.0;
261 l_paddle.h = PONG_H/4;
265 r_paddle.x = PONG_W - 8 - r_paddle.w;
273 m_unit = get_integer_resource ("speed", "Integer");
277 analogtv_lcp_to_ntsc(ANALOGTV_BLACK_LEVEL, 0.0, 0.0, field_ntsc);
278 analogtv_lcp_to_ntsc(100.0, 0.0, 0.0, ball_ntsc);
279 analogtv_lcp_to_ntsc(100.0, 0.0, 0.0, paddle_ntsc);
280 analogtv_lcp_to_ntsc(100.0, 0.0, 0.0, score_ntsc);
281 analogtv_lcp_to_ntsc(100.0, 0.0, 0.0, net_ntsc);
283 analogtv_draw_solid(inp,
284 ANALOGTV_VIS_START, ANALOGTV_VIS_END,
285 ANALOGTV_TOP, ANALOGTV_BOT,
294 targ = ball.y + by * (r_paddle.x-ball.x) / bx;
297 targ = ball.y - by * (ball.x - l_paddle.x - l_paddle.w) / bx;
302 if (targ > PONG_H) targ=PONG_H;
303 if (targ < 0) targ=0;
305 if (targ < p->y && !p->lock)
309 else if (targ > (p->y + p->h) && !p->lock)
315 int move=targ - (p->y + p->h/2);
316 if (move>paddle_rate) move=paddle_rate;
317 if (move<-paddle_rate) move=-paddle_rate;
324 p_hit_top_bottom(Paddle *p)
326 if(p->y <= PONG_TMARG)
330 if((p->y + p->h) >= PONG_H)
332 p->y = PONG_H - p->h;
337 XFillRectangle (dpy, window, gc, p->x, p->y, p->w, p->h);
340 XClearArea(dpy,window, p->x, p->y + p->h,
341 p->w, (old_v + p->h) - (p->y + p->h), 0);
343 else if (old_v < p->y)
345 XClearArea(dpy,window, p->x, old_v, p->w, p->y - old_v, 0);
349 paint_paddle(analogtv_input *inp, Paddle *p)
351 analogtv_draw_solid(inp,
352 ANALOGTV_VIS_START + p->x, ANALOGTV_VIS_START + p->x + p->w,
353 ANALOGTV_TOP, ANALOGTV_BOT,
356 analogtv_draw_solid(inp,
357 ANALOGTV_VIS_START + p->x, ANALOGTV_VIS_START + p->x + p->w,
358 ANALOGTV_TOP + p->y, ANALOGTV_TOP + p->y + p->h,
363 XClearArea(dpy,window, old_ballx, old_bally, ball.d, ball.d, 0);
364 XFillRectangle (dpy, window, gc, ball.x, ball.y, ball.d, ball.d);
365 XFillRectangle (dpy, window, gc, xgwa.width / 2, 0, ball.d, xgwa.height);
369 erase_ball(analogtv_input *inp)
371 analogtv_draw_solid(inp,
372 ANALOGTV_VIS_START + ball.x, ANALOGTV_VIS_START + ball.x + ball.w,
373 ANALOGTV_TOP + ball.y, ANALOGTV_TOP + ball.y + ball.h,
378 paint_ball(analogtv_input *inp)
380 analogtv_draw_solid(inp,
381 ANALOGTV_VIS_START + ball.x, ANALOGTV_VIS_START + ball.x + ball.w,
382 ANALOGTV_TOP + ball.y, ANALOGTV_TOP + ball.y + ball.h,
387 paint_score(analogtv_input *inp)
391 analogtv_draw_solid(inp,
392 ANALOGTV_VIS_START, ANALOGTV_VIS_END,
393 ANALOGTV_TOP, ANALOGTV_TOP + 10+ score_font.char_h * score_font.y_mult,
396 sprintf(buf, "%d",r_paddle.score%256);
397 analogtv_draw_string(inp, &score_font, buf,
398 ANALOGTV_VIS_START + 130, ANALOGTV_TOP + 8,
401 sprintf(buf, "%d",l_paddle.score%256);
402 analogtv_draw_string(inp, &score_font, buf,
403 ANALOGTV_VIS_END - 200, ANALOGTV_TOP + 8,
409 paint_net(analogtv_input *inp)
413 x=(ANALOGTV_VIS_START + ANALOGTV_VIS_END)/2;
415 for (y=ANALOGTV_TOP; y<ANALOGTV_BOT; y+=6) {
416 analogtv_draw_solid(inp, x-2, x+2, y, y+3,
418 analogtv_draw_solid(inp, x-2, x+2, y+3, y+6,
432 if ((random()%40)==0) {
433 if (bx>0) bx++; else bx--;
445 p_hit_top_bottom(&r_paddle);
446 p_hit_top_bottom(&l_paddle);
452 printf("(%d,%d,%d,%d)\n",ball.x,ball.y,ball.w,ball.h);
460 paint_paddle(inp, &r_paddle);
461 paint_paddle(inp, &l_paddle);
463 if (1) paint_ball(inp);
465 analogtv_handle_events(tv);
467 analogtv_init_signal(tv, 0.04);
468 analogtv_reception_update(&reception);
469 analogtv_add_signal(tv, &reception);
474 char *progclass = "pong";
476 char *defaults [] = {
477 ".background: black",
478 ".foreground: white",
485 XrmOptionDescRec options [] = {
486 { "-percent", ".percent", XrmoptionSepArg, 0 },
487 { "-speed", ".speed", XrmoptionSepArg, 0 },
493 screenhack (Display *dpy, Window window)
495 init_pong (dpy, window);