Use wrap-safe TIME_BEFORE/TIME_AFTER macros to compare times with current_time, inste...
[kugel-rb.git] / apps / plugins / pong.c
blob1c6f02eb0da2219089e1125bfa8ae91a1746970a
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2004 Daniel Stenberg
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "plugin.h"
23 #ifdef HAVE_LCD_BITMAP
25 PLUGIN_HEADER
27 #define PAD_HEIGHT LCD_HEIGHT / 6 /* Recorder: 10 iRiver: 21 */
28 #define PAD_WIDTH LCD_WIDTH / 50 /* Recorder: 2 iRiver: 2 */
30 #define BALL_HEIGHT LCD_HEIGHT / 32 /* Recorder: 2 iRiver: 4 */
31 #define BALL_WIDTH LCD_HEIGHT / 32 /* We want a square ball */
33 #define SPEEDX ( LCD_WIDTH * 3 ) / 2 /* Recorder: 168 iRiver: 240 */
34 #define SPEEDY LCD_HEIGHT * 2 /* Recorder: 128 iRiver: 256 */
36 #define RES 100
38 #define MOVE_STEP LCD_HEIGHT / 32 /* move pad this many steps up/down each move */
40 /* variable button definitions */
41 #if CONFIG_KEYPAD == RECORDER_PAD
42 #define PONG_QUIT BUTTON_OFF
43 #define PONG_PAUSE BUTTON_ON
44 #define PONG_LEFT_UP BUTTON_F1
45 #define PONG_LEFT_DOWN BUTTON_LEFT
46 #define PONG_RIGHT_UP BUTTON_F3
47 #define PONG_RIGHT_DOWN BUTTON_RIGHT
49 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
50 #define PONG_QUIT BUTTON_OFF
51 #define PONG_PAUSE BUTTON_ON
52 #define PONG_LEFT_UP BUTTON_F1
53 #define PONG_LEFT_DOWN BUTTON_LEFT
54 #define PONG_RIGHT_UP BUTTON_F3
55 #define PONG_RIGHT_DOWN BUTTON_RIGHT
57 #elif CONFIG_KEYPAD == ONDIO_PAD
58 #define PONG_QUIT BUTTON_OFF
59 #define PONG_PAUSE BUTTON_RIGHT
60 #define PONG_LEFT_UP BUTTON_LEFT
61 #define PONG_LEFT_DOWN BUTTON_MENU
62 #define PONG_RIGHT_UP BUTTON_UP
63 #define PONG_RIGHT_DOWN BUTTON_DOWN
65 #elif CONFIG_KEYPAD == IRIVER_H100_PAD
66 #define PONG_QUIT BUTTON_OFF
67 #define PONG_LEFT_UP BUTTON_UP
68 #define PONG_LEFT_DOWN BUTTON_DOWN
69 #define PONG_RIGHT_UP BUTTON_ON
70 #define PONG_RIGHT_DOWN BUTTON_MODE
71 #define PONG_RC_QUIT BUTTON_RC_STOP
73 #elif CONFIG_KEYPAD == IRIVER_H300_PAD
74 #define PONG_QUIT BUTTON_OFF
75 #define PONG_LEFT_UP BUTTON_UP
76 #define PONG_LEFT_DOWN BUTTON_DOWN
77 #define PONG_RIGHT_UP BUTTON_REC
78 #define PONG_RIGHT_DOWN BUTTON_MODE
79 #define PONG_RC_QUIT BUTTON_RC_STOP
81 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
82 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
83 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
84 #define PONG_QUIT BUTTON_SELECT
85 #define PONG_LEFT_UP BUTTON_MENU
86 #define PONG_LEFT_DOWN BUTTON_LEFT
87 #define PONG_RIGHT_UP BUTTON_RIGHT
88 #define PONG_RIGHT_DOWN BUTTON_PLAY
90 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
91 #define PONG_QUIT BUTTON_POWER
92 #define PONG_LEFT_UP BUTTON_UP
93 #define PONG_LEFT_DOWN BUTTON_DOWN
94 #define PONG_RIGHT_UP BUTTON_REC
95 #define PONG_RIGHT_DOWN BUTTON_PLAY
97 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
98 #define PONG_QUIT BUTTON_POWER
99 #define PONG_PAUSE BUTTON_SELECT
100 #define PONG_LEFT_UP BUTTON_UP
101 #define PONG_LEFT_DOWN BUTTON_DOWN
102 #define PONG_RIGHT_UP BUTTON_VOL_UP
103 #define PONG_RIGHT_DOWN BUTTON_VOL_DOWN
105 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
106 (CONFIG_KEYPAD == SANSA_CLIP_PAD) || \
107 (CONFIG_KEYPAD == SANSA_M200_PAD)
108 #define PONG_QUIT BUTTON_POWER
109 #define PONG_PAUSE BUTTON_SELECT
110 #define PONG_LEFT_UP BUTTON_LEFT
111 #define PONG_LEFT_DOWN BUTTON_DOWN
112 #define PONG_RIGHT_UP BUTTON_UP
113 #define PONG_RIGHT_DOWN BUTTON_RIGHT
115 #elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
116 #define PONG_QUIT BUTTON_HOME
117 #define PONG_PAUSE BUTTON_SELECT
118 #define PONG_LEFT_UP BUTTON_LEFT
119 #define PONG_LEFT_DOWN BUTTON_DOWN
120 #define PONG_RIGHT_UP BUTTON_UP
121 #define PONG_RIGHT_DOWN BUTTON_RIGHT
123 #elif (CONFIG_KEYPAD == SANSA_C200_PAD)
124 #define PONG_QUIT BUTTON_POWER
125 #define PONG_PAUSE BUTTON_SELECT
126 #define PONG_LEFT_UP BUTTON_VOL_UP
127 #define PONG_LEFT_DOWN BUTTON_VOL_DOWN
128 #define PONG_RIGHT_UP BUTTON_UP
129 #define PONG_RIGHT_DOWN BUTTON_DOWN
131 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
132 #define PONG_QUIT BUTTON_POWER
133 #define PONG_LEFT_UP BUTTON_SCROLL_UP
134 #define PONG_LEFT_DOWN BUTTON_SCROLL_DOWN
135 #define PONG_RIGHT_UP BUTTON_REW
136 #define PONG_RIGHT_DOWN BUTTON_FF
138 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
139 #define PONG_QUIT BUTTON_BACK
140 #define PONG_LEFT_UP BUTTON_UP
141 #define PONG_LEFT_DOWN BUTTON_DOWN
142 #define PONG_RIGHT_UP BUTTON_VOL_UP
143 #define PONG_RIGHT_DOWN BUTTON_VOL_DOWN
145 #elif (CONFIG_KEYPAD == MROBE100_PAD)
146 #define PONG_QUIT BUTTON_POWER
147 #define PONG_PAUSE BUTTON_SELECT
148 #define PONG_LEFT_UP BUTTON_MENU
149 #define PONG_LEFT_DOWN BUTTON_LEFT
150 #define PONG_RIGHT_UP BUTTON_PLAY
151 #define PONG_RIGHT_DOWN BUTTON_RIGHT
153 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
154 #define PONG_QUIT BUTTON_RC_REC
155 #define PONG_PAUSE BUTTON_RC_PLAY
156 #define PONG_LEFT_UP BUTTON_RC_VOL_UP
157 #define PONG_LEFT_DOWN BUTTON_RC_VOL_DOWN
158 #define PONG_RIGHT_UP BUTTON_VOL_UP
159 #define PONG_RIGHT_DOWN BUTTON_VOL_DOWN
161 #elif (CONFIG_KEYPAD == COWOND2_PAD)
162 #define PONG_QUIT BUTTON_POWER
164 #elif CONFIG_KEYPAD == IAUDIO67_PAD
165 #define PONG_QUIT BUTTON_POWER
166 #define PONG_PAUSE BUTTON_MENU
167 #define PONG_LEFT_UP BUTTON_VOLUP
168 #define PONG_LEFT_DOWN BUTTON_VOLDOWN
169 #define PONG_RIGHT_UP BUTTON_RIGHT
170 #define PONG_RIGHT_DOWN BUTTON_LEFT
172 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
173 #define PONG_QUIT BUTTON_BACK
174 #define PONG_LEFT_UP BUTTON_UP
175 #define PONG_LEFT_DOWN BUTTON_DOWN
176 #define PONG_RIGHT_UP BUTTON_PLAY
177 #define PONG_RIGHT_DOWN BUTTON_MENU
179 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
180 #define PONG_QUIT BUTTON_POWER
181 #define PONG_PAUSE BUTTON_MENU
182 #define PONG_LEFT_UP BUTTON_UP
183 #define PONG_LEFT_DOWN BUTTON_DOWN
184 #define PONG_RIGHT_UP BUTTON_VOL_UP
185 #define PONG_RIGHT_DOWN BUTTON_VOL_DOWN
187 #elif CONFIG_KEYPAD == ONDAVX747_PAD || \
188 CONFIG_KEYPAD == ONDAVX777_PAD || \
189 CONFIG_KEYPAD == MROBE500_PAD
190 #define PONG_QUIT BUTTON_POWER
192 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
193 #define PONG_QUIT BUTTON_REC
194 #define PONG_PAUSE BUTTON_PLAY
195 #define PONG_LEFT_UP BUTTON_UP
196 #define PONG_LEFT_DOWN BUTTON_DOWN
197 #define PONG_RIGHT_UP BUTTON_FFWD
198 #define PONG_RIGHT_DOWN BUTTON_REW
200 #else
201 #error No keymap defined!
202 #endif
204 #ifdef HAVE_TOUCHSCREEN
205 #ifndef PONG_QUIT
206 #define PONG_QUIT BUTTON_TOPMIDDLE
207 #endif
208 #ifndef PONG_LEFT_UP
209 #define PONG_LEFT_UP BUTTON_TOPLEFT
210 #endif
211 #ifndef PONG_LEFT_DOWN
212 #define PONG_LEFT_DOWN BUTTON_BOTTOMLEFT
213 #endif
214 #ifndef PONG_RIGHT_UP
215 #define PONG_RIGHT_UP BUTTON_TOPRIGHT
216 #endif
217 #ifndef PONG_RIGHT_DOWN
218 #define PONG_RIGHT_DOWN BUTTON_BOTTOMRIGHT
219 #endif
220 #ifndef PONG_PAUSE
221 #define PONG_PAUSE BUTTON_CENTER
222 #endif
223 #endif
225 struct pong {
226 int ballx; /* current X*RES position of the ball */
227 int bally; /* current Y*RES position of the ball */
228 int w_pad[2]; /* wanted current Y positions of pads */
229 int e_pad[2]; /* existing current Y positions of pads */
230 int ballspeedx; /* */
231 int ballspeedy; /* */
233 int score[2];
236 void singlepad(int x, int y, int set)
238 if(set) {
239 rb->lcd_fillrect(x, y, PAD_WIDTH, PAD_HEIGHT);
241 else {
242 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
243 rb->lcd_fillrect(x, y, PAD_WIDTH, PAD_HEIGHT);
244 rb->lcd_set_drawmode(DRMODE_SOLID);
248 static int xpos[2]={0, LCD_WIDTH-PAD_WIDTH};
249 void pad(struct pong *p, int pad)
251 /* clear existing pad */
252 singlepad(xpos[pad], p->e_pad[pad], 0);
254 /* draw wanted pad */
255 singlepad(xpos[pad], p->w_pad[pad], 1);
257 /* existing is now the wanted */
258 p->e_pad[pad] = p->w_pad[pad];
261 bool wallcollide(struct pong *p, int pad)
263 /* we have already checked for pad-collision, just check if this hits
264 the wall */
265 if(pad) {
266 /* right-side */
267 if(p->ballx > LCD_WIDTH*RES)
268 return true;
270 else {
271 if(p->ballx < 0)
272 return true;
274 return false;
277 /* returns true if the ball has hit a pad, and then the info variable
278 will have extra angle info */
280 bool padcollide(struct pong *p, int pad, int *info)
282 int x = p->ballx/RES;
283 int y = p->bally/RES;
285 if((y < (p->e_pad[pad]+PAD_HEIGHT)) &&
286 (y + BALL_HEIGHT > p->e_pad[pad])) {
287 /* Y seems likely right */
289 /* store the delta between ball-middle MINUS pad-middle, so
290 it returns:
291 0 when the ball hits exactly the middle of the pad
292 positive numbers when the ball is below the middle of the pad
293 negative numbers when the ball is above the middle of the pad
295 max number is +- PAD_HEIGHT/2
298 *info = (y+BALL_HEIGHT/2) - (p->e_pad[pad] + PAD_HEIGHT/2);
300 if(pad) {
301 /* right-side */
302 if((x + BALL_WIDTH) >= (LCD_WIDTH - PAD_WIDTH))
303 return true; /* phump */
305 else {
306 if(x <= PAD_WIDTH)
307 return true;
310 return false; /* nah */
313 void bounce(struct pong *p, int pad, int info)
315 (void)pad; /* not used right now */
316 p->ballspeedx = -p->ballspeedx;
318 /* info is the hit-angle into the pad */
319 if(p->ballspeedy > 0) {
320 /* downwards */
321 if(info > 0) {
322 /* below the middle of the pad */
323 p->ballspeedy += info * RES/3;
325 else if(info < 0) {
326 /* above the middle */
327 p->ballspeedy = info * RES/2;
330 else {
331 /* upwards */
332 if(info > 0) {
333 /* below the middle of the pad */
334 p->ballspeedy = info * RES/2;
336 else if(info < 0) {
337 /* above the middle */
338 p->ballspeedy -= info * RES/3;
342 p->ballspeedy += rb->rand()%21-10;
344 #if 0
345 fprintf(stderr, "INFO: %d YSPEED: %d\n", info, p->ballspeedy);
346 #endif
349 void score(struct pong *p, int pad)
351 if(pad)
352 rb->splash(HZ/4, "right scores!");
353 else
354 rb->splash(HZ/4, "left scores!");
355 rb->lcd_clear_display();
356 p->score[pad]++;
358 /* then move the X-speed of the ball and give it a random Y position */
359 p->ballspeedx = -p->ballspeedx;
360 p->bally = rb->rand()%(LCD_HEIGHT*RES - BALL_HEIGHT);
362 /* avoid hitting the pad with the new ball */
363 p->ballx = (p->ballx < 0) ?
364 (RES * PAD_WIDTH) : (RES * (LCD_WIDTH - PAD_WIDTH - BALL_WIDTH));
366 /* restore Y-speed to default */
367 p->ballspeedy = (p->ballspeedy > 0) ? SPEEDY : -SPEEDY;
369 /* set the existing pad positions to something weird to force pad
370 updates */
371 p->e_pad[0] = -1;
372 p->e_pad[1] = -1;
375 void ball(struct pong *p)
377 int x = p->ballx/RES;
378 int y = p->bally/RES;
380 int newx;
381 int newy;
383 int info;
385 /* movement */
386 p->ballx += p->ballspeedx;
387 p->bally += p->ballspeedy;
389 newx = p->ballx/RES;
390 newy = p->bally/RES;
392 /* detect if ball hits a wall */
393 if(newy + BALL_HEIGHT > LCD_HEIGHT) {
394 /* hit floor, bounce */
395 p->ballspeedy = -p->ballspeedy;
396 newy = LCD_HEIGHT - BALL_HEIGHT;
397 p->bally = newy * RES;
399 else if(newy < 0) {
400 /* hit ceiling, bounce */
401 p->ballspeedy = -p->ballspeedy;
402 p->bally = 0;
403 newy = 0;
406 /* detect if ball hit pads */
407 if(padcollide(p, 0, &info))
408 bounce(p, 0, info);
409 else if(padcollide(p, 1, &info))
410 bounce(p, 1, info);
411 else if(wallcollide(p, 0))
412 score(p, 1);
413 else if(wallcollide(p, 1))
414 score(p, 0);
416 newx = p->ballx/RES;
417 newy = p->bally/RES;
419 /* clear old position */
420 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
421 rb->lcd_fillrect(x, y, BALL_WIDTH, BALL_HEIGHT);
422 rb->lcd_set_drawmode(DRMODE_SOLID);
424 /* draw the new ball position */
425 rb->lcd_fillrect(newx, newy, BALL_WIDTH, BALL_HEIGHT);
428 void padmove(int *pos, int dir)
430 *pos += dir;
431 if(*pos > (LCD_HEIGHT-PAD_HEIGHT))
432 *pos = (LCD_HEIGHT-PAD_HEIGHT);
433 else if(*pos < 0)
434 *pos = 0;
437 int keys(struct pong *p)
439 int key;
440 #ifdef PONG_PAUSE
441 static bool pause = false;
442 #endif
444 /* number of ticks this function will loop reading keys */
445 #ifndef HAVE_TOUCHSCREEN
446 int time = 4;
447 #else
448 int time = 1;
449 #endif
450 int start = *rb->current_tick;
451 int end = start + time;
453 while(TIME_BEFORE(*rb->current_tick, end)) {
454 key = rb->button_get_w_tmo(end - *rb->current_tick);
456 #ifdef HAVE_TOUCHSCREEN
457 short touch_x, touch_y;
458 if(key & BUTTON_TOUCHSCREEN)
460 touch_x = rb->button_get_data() >> 16;
461 touch_y = rb->button_get_data() & 0xFFFF;
462 if(touch_x >= xpos[0] && touch_x <= xpos[0]+(PAD_WIDTH*4))
463 padmove(&p->w_pad[0], touch_y-(p->e_pad[0]*2+PAD_HEIGHT)/2);
465 if(touch_x >= xpos[1]-(PAD_WIDTH*4) && touch_x <= xpos[1])
466 padmove(&p->w_pad[1], touch_y-(p->e_pad[1]*2+PAD_HEIGHT)/2);
468 #endif
470 #ifdef HAS_BUTTON_HOLD
471 if (rb->button_hold())
472 return 2; /* Pause game */
473 #endif
475 if(key & PONG_QUIT
476 #ifdef PONG_RC_QUIT
477 || key & PONG_RC_QUIT
478 #endif
480 return 0; /* exit game NOW */
482 #ifdef PONG_PAUSE
483 if(key == PONG_PAUSE)
484 pause = !pause;
485 if(pause)
486 return 2; /* Pause game */
487 #endif
489 key = rb->button_status(); /* ignore BUTTON_REPEAT */
491 if(key & PONG_LEFT_DOWN) /* player left goes down */
492 padmove(&p->w_pad[0], MOVE_STEP);
494 if(key & PONG_LEFT_UP) /* player left goes up */
495 padmove(&p->w_pad[0], -MOVE_STEP);
497 if(key & PONG_RIGHT_DOWN) /* player right goes down */
498 padmove(&p->w_pad[1], MOVE_STEP);
500 if(key & PONG_RIGHT_UP) /* player right goes up */
501 padmove(&p->w_pad[1], -MOVE_STEP);
503 if(rb->default_event_handler(key) == SYS_USB_CONNECTED)
504 return -1; /* exit game because of USB */
506 return 1; /* return 0 to exit game */
509 void showscore(struct pong *p)
511 static char buffer[20];
512 int w;
514 rb->snprintf(buffer, sizeof(buffer), "%d - %d", p->score[0], p->score[1]);
515 w = rb->lcd_getstringsize((unsigned char *)buffer, NULL, NULL);
516 rb->lcd_putsxy( (LCD_WIDTH / 2) - (w / 2), 0, (unsigned char *)buffer);
519 /* this is the plugin entry point */
520 enum plugin_status plugin_start(const void* parameter)
522 struct pong pong;
523 int game = 1;
525 /* init the struct with some silly values to start with */
527 pong.ballx = 20*RES;
528 pong.bally = 20*RES;
530 pong.e_pad[0] = 0;
531 pong.w_pad[0] = 7;
532 pong.e_pad[1] = 0;
533 pong.w_pad[1] = 40;
535 pong.ballspeedx = SPEEDX;
536 pong.ballspeedy = SPEEDY;
538 pong.score[0] = pong.score[1] = 0; /* lets start at 0 - 0 ;-) */
540 /* if you don't use the parameter, you can do like
541 this to avoid the compiler warning about it */
542 (void)parameter;
544 /* Clear screen */
545 rb->lcd_clear_display();
547 /* go go go */
548 while(game > 0) {
549 if (game == 2) { /* Game Paused */
550 rb->splash(0, "PAUSED");
551 while(game == 2)
552 game = keys(&pong); /* short circuit */
553 rb->lcd_clear_display();
555 showscore(&pong);
556 pad(&pong, 0); /* draw left pad */
557 pad(&pong, 1); /* draw right pad */
558 ball(&pong); /* move and draw ball */
560 rb->lcd_update();
562 game = keys(&pong); /* deal with keys */
565 return (game == 0) ? PLUGIN_OK : PLUGIN_USB_CONNECTED;
568 #endif /* HAVE_LCD_BITMAP */