Packard Bell Vibe 500: More plugin keymaps (second portion).
[kugel-rb.git] / apps / plugins / xobox.c
blob6edc629144db3ffd62197a0923b1c30f04d09bff
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"
27 PLUGIN_HEADER
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_SA9200_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 == ONDAVX747_PAD || \
212 CONFIG_KEYPAD == ONDAVX777_PAD || \
213 CONFIG_KEYPAD == MROBE500_PAD
215 #define QUIT BUTTON_POWER
217 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
219 #define QUIT BUTTON_PLAY
220 #define LEFT BUTTON_LEFT
221 #define RIGHT BUTTON_RIGHT
222 #define UP BUTTON_UP
223 #define DOWN BUTTON_DOWN
224 #define PAUSE BUTTON_FFWD
226 #elif CONFIG_KEYPAD == PBELL_VIBE500_PAD
228 #define QUIT BUTTON_REC
229 #define LEFT BUTTON_PREV
230 #define RIGHT BUTTON_NEXT
231 #define UP BUTTON_UP
232 #define DOWN BUTTON_DOWN
233 #define PAUSE BUTTON_PLAY
235 #else
236 #error No keymap defined!
237 #endif
239 #ifdef HAVE_TOUCHSCREEN
240 #ifndef QUIT
241 #define QUIT BUTTON_TOPLEFT
242 #endif
243 #ifndef LEFT
244 #define LEFT BUTTON_MIDLEFT
245 #endif
246 #ifndef RIGHT
247 #define RIGHT BUTTON_MIDRIGHT
248 #endif
249 #ifndef UP
250 #define UP BUTTON_TOPMIDDLE
251 #endif
252 #ifndef DOWN
253 #define DOWN BUTTON_BOTTOMMIDDLE
254 #endif
255 #ifndef PAUSE
256 #define PAUSE BUTTON_CENTER
257 #endif
258 #endif
260 #define MOVE_NO 0 /* player movement */
261 #define MOVE_UP 1 /* 1 */
262 #define MOVE_DN 2 /* 3 0 4 */
263 #define MOVE_LT 3 /* 2 */
264 #define MOVE_RT 4
266 /* ball movement (12 ways) */
267 /* UUL UR */
268 /* UL UR */
269 /* ULL . URR */
270 /* DLL DRR */
271 /* DL DR */
272 /* DDL DDR */
274 #define DIR_UU (1<<7)
275 #define DIR_U (1<<6)
276 #define DIR_RR (1<<5)
277 #define DIR_R (1<<4)
278 #define DIR_DD (1<<3)
279 #define DIR_D (1<<2)
280 #define DIR_LL (1<<1)
281 #define DIR_L (1<<0)
283 #define MOVE_UUR ( DIR_UU | DIR_R )
284 #define MOVE_UR ( DIR_U | DIR_R )
285 #define MOVE_URR ( DIR_U | DIR_RR )
286 #define MOVE_DRR ( DIR_D | DIR_RR )
287 #define MOVE_DR ( DIR_D | DIR_R )
288 #define MOVE_DDR ( DIR_DD | DIR_R )
289 #define MOVE_DDL ( DIR_DD | DIR_L )
290 #define MOVE_DL ( DIR_D | DIR_L )
291 #define MOVE_DLL ( DIR_D | DIR_LL )
292 #define MOVE_ULL ( DIR_U | DIR_LL )
293 #define MOVE_UL ( DIR_U | DIR_L )
294 #define MOVE_UUL ( DIR_UU | DIR_L )
296 #if (LCD_WIDTH>112) && (LCD_HEIGHT>64)
297 # define CUBE_SIZE 8 /* 8x22=176 */
298 # define pos(a) ((a)>>3)
299 #else
300 # define CUBE_SIZE 4
301 # define pos(a) ((a)>>2)
302 #endif
304 #define STARTING_QIXES 2
305 #define MAX_LEVEL 10
306 #define MAX_QIXES MAX_LEVEL+STARTING_QIXES
307 #define BOARD_W ((int)(LCD_WIDTH/CUBE_SIZE))
308 #define BOARD_H ((int)(LCD_HEIGHT/CUBE_SIZE))
309 #define BOARD_X (LCD_WIDTH-BOARD_W*CUBE_SIZE)/2
310 #define BOARD_Y (LCD_HEIGHT-BOARD_H*CUBE_SIZE)/2
312 #ifdef HAVE_LCD_COLOR
313 #define CLR_RED LCD_RGBPACK(255,0,0) /* used to imply danger */
314 #define CLR_LTBLUE LCD_RGBPACK(125, 145, 180) /* used for frame and filling */
315 #define PLR_COL LCD_WHITE /* color used for the player */
316 #elif LCD_DEPTH>=2
317 #define CLR_RED LCD_DARKGRAY /* used to imply danger */
318 #define CLR_LTBLUE LCD_LIGHTGRAY /* used for frame and filling */
319 #define PLR_COL LCD_BLACK /* color used for the player */
320 #endif
322 #if LCD_DEPTH>=2
323 #define EMPTIED LCD_BLACK /* empty spot */
324 #define FILLED CLR_LTBLUE /* filled spot */
325 #define TRAIL CLR_RED /* the red trail of the player */
326 #define QIX LCD_WHITE
327 #else
328 #define EMPTIED 0
329 #define FILLED 1
330 #define TRAIL 2
331 #define QIX 3
332 #endif
333 #define UNCHECKED 0
334 #define CHECKED 1
335 #define PAINTED -1
336 #define PIC_QIX 0
337 #define PIC_PLAYER 1
339 /* The time (in ms) for one iteration through the game loop - decrease this
340 to speed up the game - note that current_tick is (currently) only accurate
341 to 10ms.
343 static int speed = 6; /* CYCLETIME = (11-speed)*10 ms */
344 static int difficulty = 75; /* Percentage of screen that needs to be filled
345 * in order to win the game */
347 static bool quit = false;
349 static unsigned int board[BOARD_H][BOARD_W];
350 static int testboard[BOARD_H][BOARD_W];
352 #if CUBE_SIZE == 8
354 00011000 0x18 - 11100111 0xe7
355 00111100 0x3c - 11100111 0xe7
356 01111110 0x7e - 11000011 0xc3
357 11111111 0xff - 00000000 0x00
358 11111111 0xff - 00000000 0x00
359 01111110 0x7e - 11000011 0xc3
360 00111100 0x3c - 11100111 0xe7
361 00011000 0x18 - 11100111 0xe7
363 const unsigned char pics[2][8] = {
364 {0x18, 0x3c, 0x7e, 0xff, 0xff, 0x7e, 0x3c, 0x18}, /* Alien (QIX) */
365 {0xe7, 0xe7, 0xc3, 0x00, 0x00, 0xc3, 0xe7, 0xe7} /* Player (XONIX) */
367 #elif CUBE_SIZE == 4
369 0110 0x6 - 1001 0x9
370 1111 0xf - 0110 0x6
371 1111 0xf - 0110 0x6
372 0110 0x6 - 1001 0x9
374 const unsigned char pics[2][4] = {
375 {0x6, 0xf, 0xf, 0x6}, /* Alien (QIX) */
376 {0x9, 0x6, 0x6, 0x9} /* Player (XONIX) */
378 #else
379 #error Incorrect CUBE_SIZE value.
380 #endif
382 static struct qix
384 int velocity; /* velocity */
385 int x, y; /* position on screen */
386 int angle; /* angle */
387 } qixes[MAX_QIXES]; /* black_qix */
389 static struct splayer
391 int i, j; /* position on board */
392 int move, score, level, lives;
393 bool drawing;
394 bool gameover;
395 } player;
397 static int percentage_cache;
399 /*************************** STACK STUFF **********************/
401 /* the stack */
402 #define STACK_SIZE (2*BOARD_W*BOARD_H)
403 static struct pos
405 int x, y; /* position on board */
406 } stack[STACK_SIZE];
407 static int stackPointer;
409 static inline bool pop (struct pos *p)
411 if (stackPointer > 0) {
412 p->x = stack[stackPointer].x;
413 p->y = stack[stackPointer].y;
414 stackPointer--;
415 return true;
416 } else
417 return false; /* SE */
420 static inline bool push (struct pos *p)
422 if (stackPointer < STACK_SIZE - 1) {
423 stackPointer++;
424 stack[stackPointer].x = p->x;
425 stack[stackPointer].y = p->y;
426 return true;
427 } else
428 return false; /* SOF */
431 static inline void emptyStack (void)
433 stackPointer = 0;
436 /*********************** END OF STACK STUFF *********************/
438 /* calculate the new x coordinate of the ball according to angle and speed */
439 static inline int get_newx (int x, int len, int deg)
441 if (deg & DIR_R)
442 return x + len;
443 else if (deg & DIR_L)
444 return x - len;
445 else if (deg & DIR_RR)
446 return x + len * 2;
447 else /* (def & DIR_LL) */
448 return x - len * 2;
451 /* calculate the new y coordinate of the ball according to angle and speed */
452 static inline int get_newy (int y, int len, int deg)
454 if (deg & DIR_D)
455 return y + len;
456 else if (deg & DIR_U)
457 return y - len;
458 else if (deg & DIR_DD)
459 return y + len * 2;
460 else /* (deg & DIR_UU) */
461 return y - len * 2;
464 /* make random function get it's value from the device ticker */
465 static inline void randomize (void)
467 rb->srand (*rb->current_tick);
470 /* get a random number between 0 and range-1 */
471 static int t_rand (int range)
473 return rb->rand () % range;
476 /* initializes the test help board */
477 static void init_testboard (void)
479 int j; /* testboard */
480 for (j = 0; j < BOARD_H; j++)
481 /* UNCHEKED == (int)0 */
482 rb->memset( testboard[j], 0, BOARD_W * sizeof( int ) );
485 /* initializes the game board on with the player,qix's and black qix */
486 static void init_board (void)
488 int i, j;
489 for (j = 0; j < BOARD_H; j++)
490 for (i = 0; i < BOARD_W; i++) { /* make a nice cyan frame */
491 if ((i == 0) || (j <= 1) || (i == BOARD_W - 1)
492 || (j >= BOARD_H - 2))
493 board[j][i] = FILLED;
494 else
495 board[j][i] = EMPTIED;
498 /* (level+2) is the number of qixes */
499 for (j = 0; j < player.level + STARTING_QIXES; j++) {
500 qixes[j].velocity = t_rand (2) + 1; /* 1 or 2 pix-per-sec */
502 /* not on frame */
503 qixes[j].x = CUBE_SIZE*2 + 2*t_rand (((BOARD_W-4)*CUBE_SIZE)/2);
504 qixes[j].y = CUBE_SIZE*2 + 2*t_rand (((BOARD_H-4)*CUBE_SIZE)/2);
506 const int angle_table[] = {
507 MOVE_UUR, MOVE_UR, MOVE_URR, MOVE_DRR, MOVE_DR, MOVE_DDR,
508 MOVE_UUL, MOVE_UL, MOVE_ULL, MOVE_DLL, MOVE_DL, MOVE_DDL };
509 qixes[j].angle = angle_table[t_rand (12)];
510 #if CUBE_SIZE == 4
511 /* Work arround a nasty bug. FIXME */
512 if( qixes[j].angle & (DIR_LL|DIR_RR|DIR_UU|DIR_DD) )
513 qixes[j].velocity = 1;
514 #endif
516 /*black_qix.velocity=1;
517 black_qix.x=BOARD_X+(BOARD_W*CUBE_SIZE)/2-CUBE_SIZE/2;
518 black_qix.y=BOARD_Y+(BOARD_H*CUBE_SIZE)-CUBE_SIZE-CUBE_SIZE/2;
519 black_qix.angle=MOVE_UR; */
520 player.move = MOVE_NO;
521 player.drawing = false;
522 player.i = BOARD_W / 2;
523 player.j = 1;
525 percentage_cache = 0;
528 /* calculates the percentage of the screen filling */
529 static int percentage (void)
531 int i, j, filled = 0;
532 for (j = 2; j < BOARD_H - 2; j++)
533 for (i = 1; i < BOARD_W - 1; i++)
534 if (board[j][i] == FILLED)
535 filled++;
536 return (filled * 100) / ((BOARD_W - 2) * (BOARD_H - 4));
539 /* draw the board on with all the game figures */
540 static void refresh_board (void)
542 int i, j;
543 char str[25];
545 #if LCD_DEPTH>=2
546 rb->lcd_set_background (LCD_BLACK);
547 #endif
548 rb->lcd_clear_display ();
549 for (j = 0; j < BOARD_H; j++)
551 unsigned last_color = board[j][0];
552 int last_i = 0;
553 for (i = 1; i < BOARD_W; i++) {
554 if( last_color != board[j][i] )
556 #if LCD_DEPTH>=2
557 rb->lcd_set_foreground (last_color);
558 #else
559 if (last_color != EMPTIED)
560 #endif
561 rb->lcd_fillrect (BOARD_X + CUBE_SIZE * (last_i),
562 BOARD_Y + CUBE_SIZE * j,
563 CUBE_SIZE * (i - last_i), CUBE_SIZE );
564 last_color = board[j][i];
565 last_i = i;
568 #if LCD_DEPTH>=2
569 rb->lcd_set_foreground (last_color);
570 #else
571 if (last_color != EMPTIED)
572 #endif
573 rb->lcd_fillrect (BOARD_X + CUBE_SIZE * (last_i),
574 BOARD_Y + CUBE_SIZE * j,
575 CUBE_SIZE * (i - last_i), CUBE_SIZE);
578 #if LCD_DEPTH>=2
579 rb->lcd_set_foreground (LCD_BLACK);
580 rb->lcd_set_background (CLR_LTBLUE);
581 #else
582 rb->lcd_set_drawmode (DRMODE_COMPLEMENT);
583 #endif
584 rb->snprintf (str, sizeof (str), "Level %d", player.level + 1);
585 rb->lcd_putsxy (BOARD_X, BOARD_Y, str);
586 rb->snprintf (str, sizeof (str), "%d%%", percentage_cache);
587 rb->lcd_putsxy (BOARD_X + CUBE_SIZE * BOARD_W - 24, BOARD_Y, str);
588 rb->snprintf (str, sizeof (str), "Score: %d", player.score);
589 rb->lcd_putsxy (BOARD_X, BOARD_Y + CUBE_SIZE * BOARD_H - 8, str);
590 rb->snprintf (str, sizeof (str),
591 (player.lives != 1) ? "%d Lives" : "%d Life", player.lives);
592 #if LCD_DEPTH>=2
593 rb->lcd_putsxy (BOARD_X + CUBE_SIZE * BOARD_W - 60,
594 BOARD_Y + CUBE_SIZE * BOARD_H - 8, str);
595 #else
596 rb->lcd_putsxy (BOARD_X + CUBE_SIZE * BOARD_W - 40,
597 BOARD_Y + CUBE_SIZE * BOARD_H - 8, str);
598 #endif
600 #if LCD_DEPTH>=2
601 rb->lcd_set_foreground (PLR_COL);
602 rb->lcd_set_background (board[player.j][player.i]);
603 #endif
604 rb->lcd_mono_bitmap (pics[PIC_PLAYER], player.i * CUBE_SIZE + BOARD_X,
605 player.j * CUBE_SIZE + BOARD_Y, CUBE_SIZE, CUBE_SIZE);
607 #if LCD_DEPTH>=2
608 rb->lcd_set_background (EMPTIED);
609 rb->lcd_set_foreground (LCD_WHITE);
610 rb->lcd_set_drawmode (DRMODE_FG);
611 #else
612 rb->lcd_set_drawmode (DRMODE_FG);
613 #endif
614 for (j = 0; j < player.level + STARTING_QIXES; j++)
615 rb->lcd_mono_bitmap (pics[PIC_QIX], qixes[j].x + BOARD_X,
616 qixes[j].y + BOARD_Y, CUBE_SIZE, CUBE_SIZE);
617 #if LCD_DEPTH>=2
618 rb->lcd_set_foreground (LCD_BLACK);
619 #endif
620 rb->lcd_set_drawmode (DRMODE_SOLID);
622 rb->lcd_update ();
625 static inline int infested_area (int i, int j, int v)
627 struct pos p;
628 p.x = i;
629 p.y = j;
630 emptyStack ();
631 if (!push (&p))
632 return -1;
633 while (pop (&p)) {
634 if (testboard[p.y][p.x] == v) continue;
635 if (testboard[p.y][p.x] > UNCHECKED)
636 return 1; /* This area was previously flagged as infested */
637 testboard[p.y][p.x] = v;
638 if (board[p.y][p.x] == QIX)
639 return 1; /* Infested area */
641 struct pos p1 = { p.x+1, p.y };
642 if ((p1.x < BOARD_W)
643 && (board[p1.y][p1.x] != FILLED)
644 && (!push (&p1)))
645 return -1;
648 struct pos p1 = { p.x-1, p.y };
649 if ((p1.x >= 0)
650 && (board[p1.y][p1.x] != FILLED)
651 && (!push (&p1)))
652 return -1;
655 struct pos p1 = { p.x, p.y+1 };
656 if ((p1.y < BOARD_H)
657 && (board[p1.y][p1.x] != FILLED)
658 && (!push (&p1)))
659 return -1;
662 struct pos p1 = { p.x, p.y-1 };
663 if ((p1.y >= 0)
664 && (board[p1.y][p1.x] != FILLED)
665 && (!push (&p1)))
666 return -1;
669 return 0;
672 static inline int fill_area (int i, int j)
674 struct pos p;
675 p.x = i;
676 p.y = j;
677 int v = testboard[p.y][p.x];
678 emptyStack ();
679 if (!push (&p))
680 return -1;
681 while (pop (&p)) {
682 board[p.y][p.x] = FILLED;
683 testboard[p.y][p.x] = PAINTED;
685 struct pos p1 = { p.x+1, p.y };
686 if ((p1.x < BOARD_W)
687 && (testboard[p1.y][p1.x] == v)
688 && (!push (&p1)))
689 return -1;
692 struct pos p1 = { p.x-1, p.y };
693 if ((p1.x >= 0)
694 && (testboard[p1.y][p1.x] == v)
695 && (!push (&p1)))
696 return -1;
699 struct pos p1 = { p.x, p.y+1 };
700 if ((p1.y < BOARD_H)
701 && (testboard[p1.y][p1.x] == v)
702 && (!push (&p1)))
703 return -1;
706 struct pos p1 = { p.x, p.y-1 };
707 if ((p1.y >= 0)
708 && (testboard[p1.y][p1.x] == v)
709 && (!push (&p1)))
710 return -1;
713 return 0;
717 /* take care of stuff after xonix has landed on a filled spot */
718 static void complete_trail (int fill)
720 int i, j, ret;
721 for (j = 0; j < BOARD_H; j++) {
722 for (i = 0; i < BOARD_W; i++) {
723 if (board[j][i] == TRAIL) {
724 if (fill)
725 board[j][i] = FILLED;
726 else
727 board[j][i] = EMPTIED;
732 if (fill) {
733 int v = CHECKED;
734 for (i = 0; i < player.level + STARTING_QIXES; i++) /* add qixes to board */
735 board[pos(qixes[i].y - BOARD_Y)]
736 [pos(qixes[i].x - BOARD_X)] = QIX;
738 init_testboard();
739 for (j = 1; j < BOARD_H - 1; j++) {
740 for (i = 0; i < BOARD_W - 0; i++) {
741 if (board[j][i] != FILLED) {
742 ret = infested_area (i, j, v);
743 if (ret < 0 || ( ret == 0 && fill_area (i, j) ) )
744 quit = true;
745 v++;
750 for (i = 0; i < player.level + STARTING_QIXES; i++) /* add qixes to board */
751 board[pos(qixes[i].y - BOARD_Y)]
752 [pos(qixes[i].x - BOARD_X)] = EMPTIED;
753 percentage_cache = percentage();
756 rb->button_clear_queue();
759 /* returns the color the real pixel(x,y) on the lcd is pointing at */
760 static inline unsigned int getpixel (int x, int y)
762 const int a = pos (x - BOARD_X), b = pos (y - BOARD_Y);
763 if ((a > 0) && (a < BOARD_W) && (b > 0) && (b < BOARD_H)) /* if inside board */
764 return board[b][a];
765 else
766 return FILLED;
769 /* returns the color the ball on (newx,newy) is heading at *----*
770 checks the four edge points of the square if 1st of all | |
771 are a trail (cause it's a lose life situation) and 2nd | |
772 if it's filled so it needs to bounce. *____*
774 static inline unsigned int next_hit (int newx, int newy)
776 if ((getpixel (newx, newy) == TRAIL)
777 || (getpixel (newx, newy + CUBE_SIZE - 1) == TRAIL)
778 || (getpixel (newx + CUBE_SIZE - 1, newy) == TRAIL)
779 || (getpixel (newx + CUBE_SIZE - 1, newy + CUBE_SIZE - 1) == TRAIL))
780 return TRAIL;
781 else if ((getpixel (newx, newy) == FILLED)
782 || (getpixel (newx, newy + CUBE_SIZE - 1) == FILLED)
783 || (getpixel (newx + CUBE_SIZE - 1, newy) == FILLED)
784 || (getpixel (newx + CUBE_SIZE - 1, newy + CUBE_SIZE - 1) ==
785 FILLED))
786 return FILLED;
787 else
788 return EMPTIED;
791 static void die (void)
793 player.lives--;
794 if (player.lives == 0)
795 player.gameover = true;
796 else {
797 refresh_board ();
798 rb->splash (HZ, "Crash!");
799 complete_trail (false);
800 player.move = MOVE_NO;
801 player.drawing = false;
802 player.i = BOARD_W / 2;
803 player.j = 1;
807 /* returns true if the (side) of the block -***-
808 starting from (newx,newy) has any filled pixels * *
809 -***-
811 static inline bool line_check_lt (int newx, int newy)
813 return getpixel (newx, newy + CUBE_SIZE/2-1) == FILLED
814 && getpixel (newx, newy + CUBE_SIZE/2 ) == FILLED;
816 static inline bool line_check_rt (int newx, int newy)
818 return getpixel (newx + CUBE_SIZE-1, newy + CUBE_SIZE/2-1) == FILLED
819 && getpixel (newx + CUBE_SIZE-1, newy + CUBE_SIZE/2 ) == FILLED;
821 static inline bool line_check_up (int newx, int newy)
823 return getpixel (newx + CUBE_SIZE/2-1, newy) == FILLED
824 && getpixel (newx + CUBE_SIZE/2 , newy) == FILLED;
826 static inline bool line_check_dn (int newx, int newy)
828 return getpixel (newx + CUBE_SIZE/2-1, newy + CUBE_SIZE-1) == FILLED
829 && getpixel (newx + CUBE_SIZE/2 , newy + CUBE_SIZE-1) == FILLED;
832 static inline void move_qix (struct qix *q)
834 int newx, newy;
835 newx = get_newx (q->x, q->velocity, q->angle);
836 newy = get_newy (q->y, q->velocity, q->angle);
837 switch (next_hit (newx, newy))
839 case EMPTIED:
840 q->x = newx;
841 q->y = newy;
842 break;
843 case FILLED:
845 const int a = q->angle;
846 q->angle =
847 ((a&(DIR_UU|DIR_U))
848 ? (line_check_up (newx, newy) ? ((a&(DIR_UU|DIR_U))>>4)
849 : (a&(DIR_UU|DIR_U)))
850 : 0)
852 ((a&(DIR_RR|DIR_R))
853 ? (line_check_rt (newx, newy) ? ((a&(DIR_RR|DIR_R))>>4)
854 : (a&(DIR_RR|DIR_R)))
855 : 0)
857 ((a&(DIR_DD|DIR_D))
858 ? (line_check_dn (newx, newy) ? ((a&(DIR_DD|DIR_D))<<4)
859 : (a&(DIR_DD|DIR_D)))
860 : 0)
862 ((a&(DIR_LL|DIR_L))
863 ? (line_check_lt (newx, newy) ? ((a&(DIR_LL|DIR_L))<<4)
864 : (a&(DIR_LL|DIR_L)))
865 : 0);
866 q->x = get_newx (q->x, q->velocity, q->angle);
867 q->y = get_newy (q->y, q->velocity, q->angle);
868 break;
870 case TRAIL:
871 die();
872 break;
876 /* move the board forward timewise */
877 static inline void move_board (void)
879 int j, newi, newj;
881 for (j = 0; j < player.level + STARTING_QIXES; j++)
882 move_qix (&qixes[j]);
883 /* move_qix(&black_qix,true); */
884 if (player.move) {
885 newi = player.i;
886 newj = player.j;
887 switch (player.move) {
888 case MOVE_UP:
889 if (player.j > 1)
890 newj--;
891 break;
892 case MOVE_DN:
893 if (player.j < BOARD_H - 2)
894 newj++;
895 break;
896 case MOVE_LT:
897 if (player.i > 0)
898 newi--;
899 break;
900 case MOVE_RT:
901 if (player.i < BOARD_W - 1)
902 newi++;
903 break;
904 default:
905 break;
908 if ((player.drawing) && (board[newj][newi] == EMPTIED)) /* continue drawing */
909 board[newj][newi] = TRAIL;
910 else if ((player.drawing) && (board[newj][newi] == FILLED)) { /* finish drawing */
911 player.move = MOVE_NO; /* stop moving */
912 player.drawing = false;
913 complete_trail (true);
914 } else if ((board[player.j][player.i] == FILLED)
915 && (board[newj][newi] == EMPTIED)) {
916 /* start drawing */
917 player.drawing = true;
918 board[newj][newi] = TRAIL;
919 /* if the block after next is empty and we're moving onto filled, stop */
920 } else if ((board[newj][newi] == FILLED)
921 && (board[newj + newj-player.j][newi + newi-player.i] == EMPTIED)) {
922 player.move = MOVE_NO;
924 player.i = newi;
925 player.j = newj;
927 if (percentage_cache >= difficulty) { /* finished level */
928 refresh_board ();
929 rb->splashf (HZ * 2, "Level %d finished", player.level+1);
930 player.score += percentage_cache;
931 if (player.level < MAX_LEVEL)
932 player.level++;
933 init_board ();
934 refresh_board ();
935 rb->splash (HZ * 2, "Ready?");
939 /* init game's variables */
940 static void init_game (void)
942 player.level = 0;
943 player.score = 0;
944 player.lives = 3;
945 player.gameover = false;
946 player.drawing = false;
947 init_board ();
948 refresh_board ();
949 rb->splash (HZ * 2, "Ready?");
952 /* the main menu */
953 static bool _ingame;
954 static int xobox_menu_cb(int action, const struct menu_item_ex *this_item)
956 if(action == ACTION_REQUEST_MENUITEM
957 && !_ingame && ((intptr_t)this_item)==0)
958 return ACTION_EXIT_MENUITEM;
959 return action;
962 static int xobox_menu(bool ingame)
964 rb->button_clear_queue();
966 int selection = 0;
967 MENUITEM_STRINGLIST(main_menu, "Xobox Menu", xobox_menu_cb,
968 "Resume Game", "Start New Game",
969 "Speed", "Difficulty",
970 "Playback Control", "Quit");
971 _ingame = ingame;
973 while (true) {
974 switch (rb->do_menu(&main_menu, &selection, NULL, false)) {
975 case 0:
976 return 0;
977 case 1:
978 init_game ();
979 return 0;
980 case 2:
981 rb->set_int ("Speed", "", UNIT_INT, &speed, NULL, 1, 1, 10, NULL);
982 break;
983 case 3:
984 rb->set_int ("Difficulty", "", UNIT_INT, &difficulty, NULL,
985 5, 50, 95, NULL);
986 break;
987 case 4:
988 playback_control(NULL);
989 break;
990 case 5:
991 return 1;
992 case MENU_ATTACHED_USB:
993 return 1;
994 default:
995 break;
1000 /* general keypad handler loop */
1001 static int xobox_loop (void)
1003 int button = 0;
1004 bool pause = false;
1005 int end;
1007 if (xobox_menu(false)) {
1008 return PLUGIN_OK;
1011 while (!quit) {
1012 end = *rb->current_tick + ((11-speed)*HZ)/100;
1014 #ifdef HAS_BUTTON_HOLD
1015 if (rb->button_hold()) {
1016 pause = true;
1017 rb->splash (HZ, "Paused");
1019 #endif
1021 button = rb->button_get_w_tmo (1);
1022 switch (button) {
1023 case UP:
1024 case UP|BUTTON_REPEAT:
1025 player.move = MOVE_UP;
1026 break;
1027 case DOWN:
1028 case DOWN|BUTTON_REPEAT:
1029 player.move = MOVE_DN;
1030 break;
1031 case LEFT:
1032 case LEFT|BUTTON_REPEAT:
1033 player.move = MOVE_LT;
1034 break;
1035 case RIGHT:
1036 case RIGHT|BUTTON_REPEAT:
1037 player.move = MOVE_RT;
1038 break;
1039 case PAUSE:
1040 pause = !pause;
1041 if (pause)
1042 rb->splash (HZ, "Paused");
1043 break;
1044 case QUIT:
1045 if (!pause) {
1046 if (xobox_menu(true)) {
1047 quit = true;
1050 break;
1051 default:
1052 if (rb->default_event_handler (button) == SYS_USB_CONNECTED)
1053 return PLUGIN_USB_CONNECTED;
1054 break;
1056 if (!pause) {
1057 move_board ();
1058 refresh_board ();
1060 if (player.gameover) {
1061 rb->splash (HZ, "Game Over!");
1062 if (xobox_menu(false)) {
1063 quit = true;
1067 if (TIME_BEFORE(*rb->current_tick, end))
1068 rb->sleep (end - *rb->current_tick);
1069 else
1070 rb->yield ();
1072 } /* end while */
1073 return PLUGIN_OK; /* for no warnings on compiling */
1076 /* plugin main procedure */
1077 enum plugin_status plugin_start (const void *parameter)
1079 int ret = PLUGIN_OK;
1081 (void) parameter;
1083 rb->lcd_setfont (FONT_SYSFIXED);
1084 #if LCD_DEPTH>=2
1085 rb->lcd_set_backdrop(NULL);
1086 #endif
1088 /* Turn off backlight timeout */
1089 backlight_force_on(); /* backlight control in lib/helper.c */
1091 randomize ();
1092 ret = xobox_loop ();
1094 /* Turn on backlight timeout (revert to settings) */
1095 backlight_use_settings(); /* backlight control in lib/helper.c */
1096 rb->lcd_setfont (FONT_UI);
1098 return ret;