Initial commit of the Samsung YP-R0 port.
[maemo-rb.git] / apps / plugins / xobox.c
blob30fd5fde130f0eb239c1c969acb06afa426ab701
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 Eli Sherer
11 * 2007 Antoine Cellerier
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
23 #include "plugin.h"
24 #include "lib/helper.h"
25 #include "lib/playback_control.h"
29 #if (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
31 #define QUIT BUTTON_OFF
32 #define LEFT BUTTON_LEFT
33 #define RIGHT BUTTON_RIGHT
34 #define PAUSE BUTTON_MODE
35 #define UP BUTTON_UP
36 #define DOWN BUTTON_DOWN
38 #define RC_QUIT BUTTON_RC_STOP
40 #elif (CONFIG_KEYPAD == ARCHOS_AV300_PAD)
42 #define QUIT BUTTON_OFF
43 #define LEFT BUTTON_LEFT
44 #define RIGHT BUTTON_RIGHT
45 #define PAUSE BUTTON_ON
46 #define UP BUTTON_UP
47 #define DOWN BUTTON_DOWN
49 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
50 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
51 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
53 #define QUIT (BUTTON_SELECT | BUTTON_MENU)
54 #define LEFT BUTTON_LEFT
55 #define RIGHT BUTTON_RIGHT
56 #define PAUSE BUTTON_SELECT
57 #define MENU_UP BUTTON_SCROLL_FWD
58 #define MENU_DOWN BUTTON_SCROLL_BACK
59 #define UP BUTTON_MENU
60 #define DOWN BUTTON_PLAY
62 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
64 #define QUIT BUTTON_POWER
65 #define LEFT BUTTON_LEFT
66 #define RIGHT BUTTON_RIGHT
67 #define UP BUTTON_UP
68 #define DOWN BUTTON_DOWN
69 #define PAUSE BUTTON_PLAY
71 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
73 #define QUIT BUTTON_POWER
74 #define LEFT BUTTON_LEFT
75 #define RIGHT BUTTON_RIGHT
76 #define UP BUTTON_UP
77 #define DOWN BUTTON_DOWN
78 #define PAUSE BUTTON_A
80 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
81 (CONFIG_KEYPAD == SANSA_C200_PAD)
83 #define QUIT BUTTON_POWER
84 #define LEFT BUTTON_LEFT
85 #define RIGHT BUTTON_RIGHT
86 #define UP BUTTON_UP
87 #define DOWN BUTTON_DOWN
88 #define PAUSE BUTTON_REC
90 #elif (CONFIG_KEYPAD == SANSA_CLIP_PAD)
92 #define QUIT BUTTON_POWER
93 #define LEFT BUTTON_LEFT
94 #define RIGHT BUTTON_RIGHT
95 #define UP BUTTON_UP
96 #define DOWN BUTTON_DOWN
97 #define PAUSE BUTTON_HOME
99 #elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
101 #define QUIT (BUTTON_HOME|BUTTON_REPEAT)
102 #define LEFT BUTTON_LEFT
103 #define RIGHT BUTTON_RIGHT
104 #define UP BUTTON_UP
105 #define DOWN BUTTON_DOWN
106 #define PAUSE BUTTON_SELECT
108 #elif (CONFIG_KEYPAD == SANSA_M200_PAD)
110 #define QUIT BUTTON_POWER
111 #define LEFT BUTTON_LEFT
112 #define RIGHT BUTTON_RIGHT
113 #define UP BUTTON_UP
114 #define DOWN BUTTON_DOWN
115 #define PAUSE BUTTON_SELECT
117 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
119 #define QUIT BUTTON_POWER
120 #define LEFT BUTTON_LEFT
121 #define RIGHT BUTTON_RIGHT
122 #define UP BUTTON_SCROLL_UP
123 #define DOWN BUTTON_SCROLL_DOWN
124 #define PAUSE BUTTON_PLAY
126 #elif CONFIG_KEYPAD == RECORDER_PAD
128 #define QUIT BUTTON_OFF
129 #define LEFT BUTTON_LEFT
130 #define RIGHT BUTTON_RIGHT
131 #define DOWN BUTTON_DOWN
132 #define UP BUTTON_UP
133 #define PAUSE BUTTON_PLAY
135 #elif CONFIG_KEYPAD == ONDIO_PAD
137 #define QUIT BUTTON_OFF
138 #define LEFT BUTTON_LEFT
139 #define RIGHT BUTTON_RIGHT
140 #define DOWN BUTTON_DOWN
141 #define UP BUTTON_UP
142 #define PAUSE BUTTON_MENU
144 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
146 #define QUIT BUTTON_BACK
147 #define LEFT BUTTON_LEFT
148 #define RIGHT BUTTON_RIGHT
149 #define UP BUTTON_UP
150 #define DOWN BUTTON_DOWN
151 #define PAUSE BUTTON_PLAY
153 #elif (CONFIG_KEYPAD == MROBE100_PAD)
155 #define QUIT BUTTON_POWER
156 #define LEFT BUTTON_LEFT
157 #define RIGHT BUTTON_RIGHT
158 #define UP BUTTON_UP
159 #define DOWN BUTTON_DOWN
160 #define PAUSE BUTTON_DISPLAY
162 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
164 #define QUIT BUTTON_RC_REC
165 #define LEFT BUTTON_RC_REW
166 #define RIGHT BUTTON_RC_FF
167 #define UP BUTTON_RC_VOL_UP
168 #define DOWN BUTTON_RC_VOL_DOWN
169 #define PAUSE BUTTON_RC_PLAY
171 #elif CONFIG_KEYPAD == COWON_D2_PAD
173 #define QUIT BUTTON_POWER
175 #elif CONFIG_KEYPAD == IAUDIO67_PAD
177 #define QUIT BUTTON_POWER
178 #define LEFT BUTTON_LEFT
179 #define RIGHT BUTTON_RIGHT
180 #define UP BUTTON_STOP
181 #define DOWN BUTTON_PLAY
182 #define PAUSE BUTTON_MENU
184 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
186 #define QUIT BUTTON_BACK
187 #define LEFT BUTTON_LEFT
188 #define RIGHT BUTTON_RIGHT
189 #define UP BUTTON_UP
190 #define DOWN BUTTON_DOWN
191 #define PAUSE BUTTON_PLAY
193 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
195 #define QUIT BUTTON_POWER
196 #define LEFT BUTTON_LEFT
197 #define RIGHT BUTTON_RIGHT
198 #define UP BUTTON_UP
199 #define DOWN BUTTON_DOWN
200 #define PAUSE BUTTON_VIEW
202 #elif CONFIG_KEYPAD == PHILIPS_HDD6330_PAD
204 #define QUIT BUTTON_POWER
205 #define LEFT BUTTON_PREV
206 #define RIGHT BUTTON_NEXT
207 #define UP BUTTON_UP
208 #define DOWN BUTTON_DOWN
209 #define PAUSE BUTTON_MENU
211 #elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD
213 #define QUIT BUTTON_POWER
214 #define LEFT BUTTON_PREV
215 #define RIGHT BUTTON_NEXT
216 #define UP BUTTON_UP
217 #define DOWN BUTTON_DOWN
218 #define PAUSE BUTTON_MENU
220 #elif CONFIG_KEYPAD == ONDAVX747_PAD || \
221 CONFIG_KEYPAD == ONDAVX777_PAD || \
222 CONFIG_KEYPAD == MROBE500_PAD
224 #define QUIT BUTTON_POWER
226 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
228 #define QUIT BUTTON_PLAY
229 #define LEFT BUTTON_LEFT
230 #define RIGHT BUTTON_RIGHT
231 #define UP BUTTON_UP
232 #define DOWN BUTTON_DOWN
233 #define PAUSE BUTTON_FFWD
235 #elif CONFIG_KEYPAD == PBELL_VIBE500_PAD
237 #define QUIT BUTTON_REC
238 #define LEFT BUTTON_PREV
239 #define RIGHT BUTTON_NEXT
240 #define UP BUTTON_UP
241 #define DOWN BUTTON_DOWN
242 #define PAUSE BUTTON_PLAY
244 #elif CONFIG_KEYPAD == MPIO_HD200_PAD
246 #define QUIT (BUTTON_REC|BUTTON_PLAY)
247 #define LEFT BUTTON_VOL_DOWN
248 #define RIGHT BUTTON_VOL_UP
249 #define UP BUTTON_REW
250 #define DOWN BUTTON_FF
251 #define PAUSE BUTTON_PLAY
253 #elif CONFIG_KEYPAD == MPIO_HD300_PAD
255 #define QUIT BUTTON_REC
256 #define LEFT BUTTON_MENU
257 #define RIGHT BUTTON_ENTER
258 #define UP BUTTON_UP
259 #define DOWN BUTTON_DOWN
260 #define PAUSE BUTTON_PLAY
262 #elif CONFIG_KEYPAD == SANSA_FUZEPLUS_PAD
264 #define QUIT BUTTON_POWER
265 #define LEFT BUTTON_LEFT
266 #define RIGHT BUTTON_RIGHT
267 #define UP BUTTON_UP
268 #define DOWN BUTTON_DOWN
269 #define PAUSE BUTTON_PLAYPAUSE
271 #elif CONFIG_KEYPAD == SANSA_CONNECT_PAD
273 #define QUIT BUTTON_POWER
274 #define LEFT BUTTON_LEFT
275 #define RIGHT BUTTON_RIGHT
276 #define UP BUTTON_UP
277 #define DOWN BUTTON_DOWN
278 #define PAUSE BUTTON_SELECT
280 #elif (CONFIG_KEYPAD == SAMSUNG_YPR0_PAD)
282 #define QUIT BUTTON_BACK
283 #define LEFT BUTTON_LEFT
284 #define RIGHT BUTTON_RIGHT
285 #define UP BUTTON_UP
286 #define DOWN BUTTON_DOWN
287 #define PAUSE BUTTON_SELECT
289 #else
290 #error No keymap defined!
291 #endif
293 #ifdef HAVE_TOUCHSCREEN
294 #ifndef QUIT
295 #define QUIT BUTTON_TOPLEFT
296 #endif
297 #ifndef LEFT
298 #define LEFT BUTTON_MIDLEFT
299 #endif
300 #ifndef RIGHT
301 #define RIGHT BUTTON_MIDRIGHT
302 #endif
303 #ifndef UP
304 #define UP BUTTON_TOPMIDDLE
305 #endif
306 #ifndef DOWN
307 #define DOWN BUTTON_BOTTOMMIDDLE
308 #endif
309 #ifndef PAUSE
310 #define PAUSE BUTTON_CENTER
311 #endif
312 #endif
314 #define MOVE_NO 0 /* player movement */
315 #define MOVE_UP 1 /* 1 */
316 #define MOVE_DN 2 /* 3 0 4 */
317 #define MOVE_LT 3 /* 2 */
318 #define MOVE_RT 4
320 /* ball movement (12 ways) */
321 /* UUL UR */
322 /* UL UR */
323 /* ULL . URR */
324 /* DLL DRR */
325 /* DL DR */
326 /* DDL DDR */
328 #define DIR_UU (1<<7)
329 #define DIR_U (1<<6)
330 #define DIR_RR (1<<5)
331 #define DIR_R (1<<4)
332 #define DIR_DD (1<<3)
333 #define DIR_D (1<<2)
334 #define DIR_LL (1<<1)
335 #define DIR_L (1<<0)
337 #define MOVE_UUR ( DIR_UU | DIR_R )
338 #define MOVE_UR ( DIR_U | DIR_R )
339 #define MOVE_URR ( DIR_U | DIR_RR )
340 #define MOVE_DRR ( DIR_D | DIR_RR )
341 #define MOVE_DR ( DIR_D | DIR_R )
342 #define MOVE_DDR ( DIR_DD | DIR_R )
343 #define MOVE_DDL ( DIR_DD | DIR_L )
344 #define MOVE_DL ( DIR_D | DIR_L )
345 #define MOVE_DLL ( DIR_D | DIR_LL )
346 #define MOVE_ULL ( DIR_U | DIR_LL )
347 #define MOVE_UL ( DIR_U | DIR_L )
348 #define MOVE_UUL ( DIR_UU | DIR_L )
350 #if (LCD_WIDTH>112) && (LCD_HEIGHT>64)
351 # define CUBE_SIZE 8 /* 8x22=176 */
352 # define pos(a) ((a)>>3)
353 #else
354 # define CUBE_SIZE 4
355 # define pos(a) ((a)>>2)
356 #endif
358 #define STARTING_QIXES 2
359 #define MAX_LEVEL 10
360 #define MAX_QIXES MAX_LEVEL+STARTING_QIXES
361 #define BOARD_W ((int)(LCD_WIDTH/CUBE_SIZE))
362 #define BOARD_H ((int)(LCD_HEIGHT/CUBE_SIZE))
363 #define BOARD_X (LCD_WIDTH-BOARD_W*CUBE_SIZE)/2
364 #define BOARD_Y (LCD_HEIGHT-BOARD_H*CUBE_SIZE)/2
366 #ifdef HAVE_LCD_COLOR
367 #define CLR_RED LCD_RGBPACK(255,0,0) /* used to imply danger */
368 #define CLR_LTBLUE LCD_RGBPACK(125, 145, 180) /* used for frame and filling */
369 #define PLR_COL LCD_WHITE /* color used for the player */
370 #elif LCD_DEPTH>=2
371 #define CLR_RED LCD_DARKGRAY /* used to imply danger */
372 #define CLR_LTBLUE LCD_LIGHTGRAY /* used for frame and filling */
373 #define PLR_COL LCD_BLACK /* color used for the player */
374 #endif
376 #if LCD_DEPTH>=2
377 #define EMPTIED LCD_BLACK /* empty spot */
378 #define FILLED CLR_LTBLUE /* filled spot */
379 #define TRAIL CLR_RED /* the red trail of the player */
380 #define QIX LCD_WHITE
381 #else
382 #define EMPTIED 0
383 #define FILLED 1
384 #define TRAIL 2
385 #define QIX 3
386 #endif
387 #define UNCHECKED 0
388 #define CHECKED 1
389 #define PAINTED -1
390 #define PIC_QIX 0
391 #define PIC_PLAYER 1
393 /* The time (in ms) for one iteration through the game loop - decrease this
394 to speed up the game - note that current_tick is (currently) only accurate
395 to 10ms.
397 static int speed = 6; /* CYCLETIME = (11-speed)*10 ms */
398 static int difficulty = 75; /* Percentage of screen that needs to be filled
399 * in order to win the game */
401 static bool quit = false;
403 static unsigned int board[BOARD_H][BOARD_W];
404 static int testboard[BOARD_H][BOARD_W];
406 #if CUBE_SIZE == 8
408 00011000 0x18 - 11100111 0xe7
409 00111100 0x3c - 11100111 0xe7
410 01111110 0x7e - 11000011 0xc3
411 11111111 0xff - 00000000 0x00
412 11111111 0xff - 00000000 0x00
413 01111110 0x7e - 11000011 0xc3
414 00111100 0x3c - 11100111 0xe7
415 00011000 0x18 - 11100111 0xe7
417 const unsigned char pics[2][8] = {
418 {0x18, 0x3c, 0x7e, 0xff, 0xff, 0x7e, 0x3c, 0x18}, /* Alien (QIX) */
419 {0xe7, 0xe7, 0xc3, 0x00, 0x00, 0xc3, 0xe7, 0xe7} /* Player (XONIX) */
421 #elif CUBE_SIZE == 4
423 0110 0x6 - 1001 0x9
424 1111 0xf - 0110 0x6
425 1111 0xf - 0110 0x6
426 0110 0x6 - 1001 0x9
428 const unsigned char pics[2][4] = {
429 {0x6, 0xf, 0xf, 0x6}, /* Alien (QIX) */
430 {0x9, 0x6, 0x6, 0x9} /* Player (XONIX) */
432 #else
433 #error Incorrect CUBE_SIZE value.
434 #endif
436 static struct qix
438 int velocity; /* velocity */
439 int x, y; /* position on screen */
440 int angle; /* angle */
441 } qixes[MAX_QIXES]; /* black_qix */
443 static struct splayer
445 int i, j; /* position on board */
446 int move, score, level, lives;
447 bool drawing;
448 bool gameover;
449 } player;
451 static int percentage_cache;
453 /*************************** STACK STUFF **********************/
455 /* the stack */
456 #define STACK_SIZE (2*BOARD_W*BOARD_H)
457 static struct pos
459 int x, y; /* position on board */
460 } stack[STACK_SIZE];
461 static int stackPointer;
463 static inline bool pop (struct pos *p)
465 if (stackPointer > 0) {
466 p->x = stack[stackPointer].x;
467 p->y = stack[stackPointer].y;
468 stackPointer--;
469 return true;
470 } else
471 return false; /* SE */
474 static inline bool push (struct pos *p)
476 if (stackPointer < STACK_SIZE - 1) {
477 stackPointer++;
478 stack[stackPointer].x = p->x;
479 stack[stackPointer].y = p->y;
480 return true;
481 } else
482 return false; /* SOF */
485 static inline void emptyStack (void)
487 stackPointer = 0;
490 /*********************** END OF STACK STUFF *********************/
492 /* calculate the new x coordinate of the ball according to angle and speed */
493 static inline int get_newx (int x, int len, int deg)
495 if (deg & DIR_R)
496 return x + len;
497 else if (deg & DIR_L)
498 return x - len;
499 else if (deg & DIR_RR)
500 return x + len * 2;
501 else /* (def & DIR_LL) */
502 return x - len * 2;
505 /* calculate the new y coordinate of the ball according to angle and speed */
506 static inline int get_newy (int y, int len, int deg)
508 if (deg & DIR_D)
509 return y + len;
510 else if (deg & DIR_U)
511 return y - len;
512 else if (deg & DIR_DD)
513 return y + len * 2;
514 else /* (deg & DIR_UU) */
515 return y - len * 2;
518 /* make random function get it's value from the device ticker */
519 static inline void randomize (void)
521 rb->srand (*rb->current_tick);
524 /* get a random number between 0 and range-1 */
525 static int t_rand (int range)
527 return rb->rand () % range;
530 /* initializes the test help board */
531 static void init_testboard (void)
533 int j; /* testboard */
534 for (j = 0; j < BOARD_H; j++)
535 /* UNCHEKED == (int)0 */
536 rb->memset( testboard[j], 0, BOARD_W * sizeof( int ) );
539 /* initializes the game board on with the player,qix's and black qix */
540 static void init_board (void)
542 int i, j;
543 for (j = 0; j < BOARD_H; j++)
544 for (i = 0; i < BOARD_W; i++) { /* make a nice cyan frame */
545 if ((i == 0) || (j <= 1) || (i == BOARD_W - 1)
546 || (j >= BOARD_H - 2))
547 board[j][i] = FILLED;
548 else
549 board[j][i] = EMPTIED;
552 /* (level+2) is the number of qixes */
553 for (j = 0; j < player.level + STARTING_QIXES; j++) {
554 qixes[j].velocity = t_rand (2) + 1; /* 1 or 2 pix-per-sec */
556 /* not on frame */
557 qixes[j].x = CUBE_SIZE*2 + 2*t_rand (((BOARD_W-4)*CUBE_SIZE)/2);
558 qixes[j].y = CUBE_SIZE*2 + 2*t_rand (((BOARD_H-4)*CUBE_SIZE)/2);
560 const int angle_table[] = {
561 MOVE_UUR, MOVE_UR, MOVE_URR, MOVE_DRR, MOVE_DR, MOVE_DDR,
562 MOVE_UUL, MOVE_UL, MOVE_ULL, MOVE_DLL, MOVE_DL, MOVE_DDL };
563 qixes[j].angle = angle_table[t_rand (12)];
564 #if CUBE_SIZE == 4
565 /* Work arround a nasty bug. FIXME */
566 if( qixes[j].angle & (DIR_LL|DIR_RR|DIR_UU|DIR_DD) )
567 qixes[j].velocity = 1;
568 #endif
570 /*black_qix.velocity=1;
571 black_qix.x=BOARD_X+(BOARD_W*CUBE_SIZE)/2-CUBE_SIZE/2;
572 black_qix.y=BOARD_Y+(BOARD_H*CUBE_SIZE)-CUBE_SIZE-CUBE_SIZE/2;
573 black_qix.angle=MOVE_UR; */
574 player.move = MOVE_NO;
575 player.drawing = false;
576 player.i = BOARD_W / 2;
577 player.j = 1;
579 percentage_cache = 0;
582 /* calculates the percentage of the screen filling */
583 static int percentage (void)
585 int i, j, filled = 0;
586 for (j = 2; j < BOARD_H - 2; j++)
587 for (i = 1; i < BOARD_W - 1; i++)
588 if (board[j][i] == FILLED)
589 filled++;
590 return (filled * 100) / ((BOARD_W - 2) * (BOARD_H - 4));
593 /* draw the board on with all the game figures */
594 static void refresh_board (void)
596 int i, j;
597 int x;
599 #if LCD_DEPTH>=2
600 rb->lcd_set_background (LCD_BLACK);
601 #endif
602 rb->lcd_clear_display ();
603 for (j = 0; j < BOARD_H; j++)
605 unsigned last_color = board[j][0];
606 int last_i = 0;
607 for (i = 1; i < BOARD_W; i++) {
608 if( last_color != board[j][i] )
610 #if LCD_DEPTH>=2
611 rb->lcd_set_foreground (last_color);
612 #else
613 if (last_color != EMPTIED)
614 #endif
615 rb->lcd_fillrect (BOARD_X + CUBE_SIZE * (last_i),
616 BOARD_Y + CUBE_SIZE * j,
617 CUBE_SIZE * (i - last_i), CUBE_SIZE );
618 last_color = board[j][i];
619 last_i = i;
622 #if LCD_DEPTH>=2
623 rb->lcd_set_foreground (last_color);
624 #else
625 if (last_color != EMPTIED)
626 #endif
627 rb->lcd_fillrect (BOARD_X + CUBE_SIZE * (last_i),
628 BOARD_Y + CUBE_SIZE * j,
629 CUBE_SIZE * (i - last_i), CUBE_SIZE);
632 #if LCD_DEPTH>=2
633 rb->lcd_set_foreground (LCD_BLACK);
634 rb->lcd_set_background (CLR_LTBLUE);
635 #else
636 rb->lcd_set_drawmode (DRMODE_COMPLEMENT);
637 #endif
638 rb->lcd_putsxyf (BOARD_X, BOARD_Y, "Level %d", player.level + 1);
639 rb->lcd_putsxyf (BOARD_X + CUBE_SIZE * BOARD_W - 24, BOARD_Y, "%d%%",
640 percentage_cache);
641 rb->lcd_putsxyf (BOARD_X, BOARD_Y + CUBE_SIZE * BOARD_H - 8, "Score: %d",
642 player.score);
643 #if LCD_DEPTH>=2
644 x = BOARD_X + CUBE_SIZE * BOARD_W - 60;
645 #else
646 x = BOARD_X + CUBE_SIZE * BOARD_W - 40;
647 #endif
648 rb->lcd_putsxyf (x, BOARD_Y + CUBE_SIZE * BOARD_H - 8,
649 (player.lives != 1) ? "%d Lives" : "%d Life", player.lives);
651 #if LCD_DEPTH>=2
652 rb->lcd_set_foreground (PLR_COL);
653 rb->lcd_set_background (board[player.j][player.i]);
654 #endif
655 rb->lcd_mono_bitmap (pics[PIC_PLAYER], player.i * CUBE_SIZE + BOARD_X,
656 player.j * CUBE_SIZE + BOARD_Y, CUBE_SIZE, CUBE_SIZE);
658 #if LCD_DEPTH>=2
659 rb->lcd_set_background (EMPTIED);
660 rb->lcd_set_foreground (LCD_WHITE);
661 rb->lcd_set_drawmode (DRMODE_FG);
662 #else
663 rb->lcd_set_drawmode (DRMODE_FG);
664 #endif
665 for (j = 0; j < player.level + STARTING_QIXES; j++)
666 rb->lcd_mono_bitmap (pics[PIC_QIX], qixes[j].x + BOARD_X,
667 qixes[j].y + BOARD_Y, CUBE_SIZE, CUBE_SIZE);
668 #if LCD_DEPTH>=2
669 rb->lcd_set_foreground (LCD_BLACK);
670 #endif
671 rb->lcd_set_drawmode (DRMODE_SOLID);
673 rb->lcd_update ();
676 static inline int infested_area (int i, int j, int v)
678 struct pos p;
679 p.x = i;
680 p.y = j;
681 emptyStack ();
682 if (!push (&p))
683 return -1;
684 while (pop (&p)) {
685 if (testboard[p.y][p.x] == v) continue;
686 if (testboard[p.y][p.x] > UNCHECKED)
687 return 1; /* This area was previously flagged as infested */
688 testboard[p.y][p.x] = v;
689 if (board[p.y][p.x] == QIX)
690 return 1; /* Infested area */
692 struct pos p1 = { p.x+1, p.y };
693 if ((p1.x < BOARD_W)
694 && (board[p1.y][p1.x] != FILLED)
695 && (!push (&p1)))
696 return -1;
699 struct pos p1 = { p.x-1, p.y };
700 if ((p1.x >= 0)
701 && (board[p1.y][p1.x] != FILLED)
702 && (!push (&p1)))
703 return -1;
706 struct pos p1 = { p.x, p.y+1 };
707 if ((p1.y < BOARD_H)
708 && (board[p1.y][p1.x] != FILLED)
709 && (!push (&p1)))
710 return -1;
713 struct pos p1 = { p.x, p.y-1 };
714 if ((p1.y >= 0)
715 && (board[p1.y][p1.x] != FILLED)
716 && (!push (&p1)))
717 return -1;
720 return 0;
723 static inline int fill_area (int i, int j)
725 struct pos p;
726 p.x = i;
727 p.y = j;
728 int v = testboard[p.y][p.x];
729 emptyStack ();
730 if (!push (&p))
731 return -1;
732 while (pop (&p)) {
733 board[p.y][p.x] = FILLED;
734 testboard[p.y][p.x] = PAINTED;
736 struct pos p1 = { p.x+1, p.y };
737 if ((p1.x < BOARD_W)
738 && (testboard[p1.y][p1.x] == v)
739 && (!push (&p1)))
740 return -1;
743 struct pos p1 = { p.x-1, p.y };
744 if ((p1.x >= 0)
745 && (testboard[p1.y][p1.x] == v)
746 && (!push (&p1)))
747 return -1;
750 struct pos p1 = { p.x, p.y+1 };
751 if ((p1.y < BOARD_H)
752 && (testboard[p1.y][p1.x] == v)
753 && (!push (&p1)))
754 return -1;
757 struct pos p1 = { p.x, p.y-1 };
758 if ((p1.y >= 0)
759 && (testboard[p1.y][p1.x] == v)
760 && (!push (&p1)))
761 return -1;
764 return 0;
768 /* take care of stuff after xonix has landed on a filled spot */
769 static void complete_trail (int fill)
771 int i, j, ret;
772 for (j = 0; j < BOARD_H; j++) {
773 for (i = 0; i < BOARD_W; i++) {
774 if (board[j][i] == TRAIL) {
775 if (fill)
776 board[j][i] = FILLED;
777 else
778 board[j][i] = EMPTIED;
783 if (fill) {
784 int v = CHECKED;
785 for (i = 0; i < player.level + STARTING_QIXES; i++) /* add qixes to board */
786 board[pos(qixes[i].y - BOARD_Y)]
787 [pos(qixes[i].x - BOARD_X)] = QIX;
789 init_testboard();
790 for (j = 1; j < BOARD_H - 1; j++) {
791 for (i = 0; i < BOARD_W - 0; i++) {
792 if (board[j][i] != FILLED) {
793 ret = infested_area (i, j, v);
794 if (ret < 0 || ( ret == 0 && fill_area (i, j) ) )
795 quit = true;
796 v++;
801 for (i = 0; i < player.level + STARTING_QIXES; i++) /* add qixes to board */
802 board[pos(qixes[i].y - BOARD_Y)]
803 [pos(qixes[i].x - BOARD_X)] = EMPTIED;
804 percentage_cache = percentage();
807 rb->button_clear_queue();
810 /* returns the color the real pixel(x,y) on the lcd is pointing at */
811 static inline unsigned int getpixel (int x, int y)
813 const int a = pos (x - BOARD_X), b = pos (y - BOARD_Y);
814 if ((a > 0) && (a < BOARD_W) && (b > 0) && (b < BOARD_H)) /* if inside board */
815 return board[b][a];
816 else
817 return FILLED;
820 /* returns the color the ball on (newx,newy) is heading at *----*
821 checks the four edge points of the square if 1st of all | |
822 are a trail (cause it's a lose life situation) and 2nd | |
823 if it's filled so it needs to bounce. *____*
825 static inline unsigned int next_hit (int newx, int newy)
827 if ((getpixel (newx, newy) == TRAIL)
828 || (getpixel (newx, newy + CUBE_SIZE - 1) == TRAIL)
829 || (getpixel (newx + CUBE_SIZE - 1, newy) == TRAIL)
830 || (getpixel (newx + CUBE_SIZE - 1, newy + CUBE_SIZE - 1) == TRAIL))
831 return TRAIL;
832 else if ((getpixel (newx, newy) == FILLED)
833 || (getpixel (newx, newy + CUBE_SIZE - 1) == FILLED)
834 || (getpixel (newx + CUBE_SIZE - 1, newy) == FILLED)
835 || (getpixel (newx + CUBE_SIZE - 1, newy + CUBE_SIZE - 1) ==
836 FILLED))
837 return FILLED;
838 else
839 return EMPTIED;
842 static void die (void)
844 player.lives--;
845 if (player.lives == 0)
846 player.gameover = true;
847 else {
848 refresh_board ();
849 rb->splash (HZ, "Crash!");
850 complete_trail (false);
851 player.move = MOVE_NO;
852 player.drawing = false;
853 player.i = BOARD_W / 2;
854 player.j = 1;
858 /* returns true if the (side) of the block -***-
859 starting from (newx,newy) has any filled pixels * *
860 -***-
862 static inline bool line_check_lt (int newx, int newy)
864 return getpixel (newx, newy + CUBE_SIZE/2-1) == FILLED
865 && getpixel (newx, newy + CUBE_SIZE/2 ) == FILLED;
867 static inline bool line_check_rt (int newx, int newy)
869 return getpixel (newx + CUBE_SIZE-1, newy + CUBE_SIZE/2-1) == FILLED
870 && getpixel (newx + CUBE_SIZE-1, newy + CUBE_SIZE/2 ) == FILLED;
872 static inline bool line_check_up (int newx, int newy)
874 return getpixel (newx + CUBE_SIZE/2-1, newy) == FILLED
875 && getpixel (newx + CUBE_SIZE/2 , newy) == FILLED;
877 static inline bool line_check_dn (int newx, int newy)
879 return getpixel (newx + CUBE_SIZE/2-1, newy + CUBE_SIZE-1) == FILLED
880 && getpixel (newx + CUBE_SIZE/2 , newy + CUBE_SIZE-1) == FILLED;
883 static inline void move_qix (struct qix *q)
885 int newx, newy;
886 newx = get_newx (q->x, q->velocity, q->angle);
887 newy = get_newy (q->y, q->velocity, q->angle);
888 switch (next_hit (newx, newy))
890 case EMPTIED:
891 q->x = newx;
892 q->y = newy;
893 break;
894 case FILLED:
896 const int a = q->angle;
897 q->angle =
898 ((a&(DIR_UU|DIR_U))
899 ? (line_check_up (newx, newy) ? ((a&(DIR_UU|DIR_U))>>4)
900 : (a&(DIR_UU|DIR_U)))
901 : 0)
903 ((a&(DIR_RR|DIR_R))
904 ? (line_check_rt (newx, newy) ? ((a&(DIR_RR|DIR_R))>>4)
905 : (a&(DIR_RR|DIR_R)))
906 : 0)
908 ((a&(DIR_DD|DIR_D))
909 ? (line_check_dn (newx, newy) ? ((a&(DIR_DD|DIR_D))<<4)
910 : (a&(DIR_DD|DIR_D)))
911 : 0)
913 ((a&(DIR_LL|DIR_L))
914 ? (line_check_lt (newx, newy) ? ((a&(DIR_LL|DIR_L))<<4)
915 : (a&(DIR_LL|DIR_L)))
916 : 0);
917 q->x = get_newx (q->x, q->velocity, q->angle);
918 q->y = get_newy (q->y, q->velocity, q->angle);
919 break;
921 case TRAIL:
922 die();
923 break;
927 /* move the board forward timewise */
928 static inline void move_board (void)
930 int j, newi, newj;
932 for (j = 0; j < player.level + STARTING_QIXES; j++)
933 move_qix (&qixes[j]);
934 /* move_qix(&black_qix,true); */
935 if (player.move) {
936 newi = player.i;
937 newj = player.j;
938 switch (player.move) {
939 case MOVE_UP:
940 if (player.j > 1)
941 newj--;
942 break;
943 case MOVE_DN:
944 if (player.j < BOARD_H - 2)
945 newj++;
946 break;
947 case MOVE_LT:
948 if (player.i > 0)
949 newi--;
950 break;
951 case MOVE_RT:
952 if (player.i < BOARD_W - 1)
953 newi++;
954 break;
955 default:
956 break;
959 if ((player.drawing) && (board[newj][newi] == EMPTIED)) /* continue drawing */
960 board[newj][newi] = TRAIL;
961 else if ((player.drawing) && (board[newj][newi] == FILLED)) { /* finish drawing */
962 player.move = MOVE_NO; /* stop moving */
963 player.drawing = false;
964 complete_trail (true);
965 } else if ((board[player.j][player.i] == FILLED)
966 && (board[newj][newi] == EMPTIED)) {
967 /* start drawing */
968 player.drawing = true;
969 board[newj][newi] = TRAIL;
970 /* if the block after next is empty and we're moving onto filled, stop */
971 } else if ((board[newj][newi] == FILLED)
972 && (board[newj + newj-player.j][newi + newi-player.i] == EMPTIED)) {
973 player.move = MOVE_NO;
975 player.i = newi;
976 player.j = newj;
978 if (percentage_cache >= difficulty) { /* finished level */
979 refresh_board ();
980 rb->splashf (HZ * 2, "Level %d finished", player.level+1);
981 player.score += percentage_cache;
982 if (player.level < MAX_LEVEL)
983 player.level++;
984 init_board ();
985 refresh_board ();
986 rb->button_clear_queue();
987 rb->splash (HZ * 2, "Ready?");
991 /* init game's variables */
992 static void init_game (void)
994 player.level = 0;
995 player.score = 0;
996 player.lives = 3;
997 player.gameover = false;
998 player.drawing = false;
999 init_board ();
1000 refresh_board ();
1001 rb->splash (HZ * 2, "Ready?");
1004 /* the main menu */
1005 static bool _ingame;
1006 static int xobox_menu_cb(int action, const struct menu_item_ex *this_item)
1008 if(action == ACTION_REQUEST_MENUITEM
1009 && !_ingame && ((intptr_t)this_item)==0)
1010 return ACTION_EXIT_MENUITEM;
1011 return action;
1014 static int xobox_menu(bool ingame)
1016 rb->button_clear_queue();
1018 int selection = 0;
1019 MENUITEM_STRINGLIST(main_menu, "Xobox Menu", xobox_menu_cb,
1020 "Resume Game", "Start New Game",
1021 "Speed", "Difficulty",
1022 "Playback Control", "Quit");
1023 _ingame = ingame;
1025 while (true) {
1026 switch (rb->do_menu(&main_menu, &selection, NULL, false)) {
1027 case 0:
1028 return 0;
1029 case 1:
1030 init_game ();
1031 return 0;
1032 case 2:
1033 rb->set_int ("Speed", "", UNIT_INT, &speed, NULL, 1, 1, 10, NULL);
1034 break;
1035 case 3:
1036 rb->set_int ("Difficulty", "", UNIT_INT, &difficulty, NULL,
1037 5, 50, 95, NULL);
1038 break;
1039 case 4:
1040 playback_control(NULL);
1041 break;
1042 case 5:
1043 return 1;
1044 case MENU_ATTACHED_USB:
1045 return 1;
1046 default:
1047 break;
1052 /* general keypad handler loop */
1053 static int xobox_loop (void)
1055 int button = 0;
1056 bool pause = false;
1057 int end;
1059 if (xobox_menu(false)) {
1060 return PLUGIN_OK;
1063 while (!quit) {
1064 end = *rb->current_tick + ((11-speed)*HZ)/100;
1066 #ifdef HAS_BUTTON_HOLD
1067 if (rb->button_hold()) {
1068 pause = true;
1069 rb->splash (HZ, "Paused");
1071 #endif
1073 button = rb->button_get_w_tmo (1);
1074 switch (button) {
1075 case UP:
1076 case UP|BUTTON_REPEAT:
1077 player.move = MOVE_UP;
1078 break;
1079 case DOWN:
1080 case DOWN|BUTTON_REPEAT:
1081 player.move = MOVE_DN;
1082 break;
1083 case LEFT:
1084 case LEFT|BUTTON_REPEAT:
1085 player.move = MOVE_LT;
1086 break;
1087 case RIGHT:
1088 case RIGHT|BUTTON_REPEAT:
1089 player.move = MOVE_RT;
1090 break;
1091 case PAUSE:
1092 pause = !pause;
1093 if (pause)
1094 rb->splash (HZ, "Paused");
1095 break;
1096 case QUIT:
1097 if (!pause) {
1098 if (xobox_menu(true)) {
1099 quit = true;
1102 break;
1103 default:
1104 if (rb->default_event_handler (button) == SYS_USB_CONNECTED)
1105 return PLUGIN_USB_CONNECTED;
1106 break;
1108 if (!pause) {
1109 move_board ();
1110 refresh_board ();
1112 if (player.gameover) {
1113 rb->splash (HZ, "Game Over!");
1114 if (xobox_menu(false)) {
1115 quit = true;
1119 if (TIME_BEFORE(*rb->current_tick, end))
1120 rb->sleep (end - *rb->current_tick);
1121 else
1122 rb->yield ();
1124 } /* end while */
1125 return PLUGIN_OK; /* for no warnings on compiling */
1128 /* plugin main procedure */
1129 enum plugin_status plugin_start (const void *parameter)
1131 int ret = PLUGIN_OK;
1133 (void) parameter;
1135 rb->lcd_setfont (FONT_SYSFIXED);
1136 #if LCD_DEPTH>=2
1137 rb->lcd_set_backdrop(NULL);
1138 #endif
1140 /* Turn off backlight timeout */
1141 backlight_ignore_timeout();
1143 randomize ();
1144 ret = xobox_loop ();
1146 /* Turn on backlight timeout (revert to settings) */
1147 backlight_use_settings();
1148 rb->lcd_setfont (FONT_UI);
1150 return ret;