keybox: fix issue when deleted all items.
[kugel-rb.git] / apps / plugins / xobox.c
blobdaf627d59033432ae8f402e4fa2b1e6eb46cae1d
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 == COWOND2_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 == ONDAVX747_PAD || CONFIG_KEYPAD == MROBE500_PAD
204 #define QUIT BUTTON_POWER
206 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
208 #define QUIT BUTTON_PLAY
209 #define LEFT BUTTON_LEFT
210 #define RIGHT BUTTON_RIGHT
211 #define UP BUTTON_UP
212 #define DOWN BUTTON_DOWN
213 #define PAUSE BUTTON_FFWD
215 #else
216 #error No keymap defined!
217 #endif
219 #ifdef HAVE_TOUCHSCREEN
220 #ifndef QUIT
221 #define QUIT BUTTON_TOPLEFT
222 #endif
223 #ifndef LEFT
224 #define LEFT BUTTON_MIDLEFT
225 #endif
226 #ifndef RIGHT
227 #define RIGHT BUTTON_MIDRIGHT
228 #endif
229 #ifndef UP
230 #define UP BUTTON_TOPMIDDLE
231 #endif
232 #ifndef DOWN
233 #define DOWN BUTTON_BOTTOMMIDDLE
234 #endif
235 #ifndef PAUSE
236 #define PAUSE BUTTON_CENTER
237 #endif
238 #endif
240 #define MOVE_NO 0 /* player movement */
241 #define MOVE_UP 1 /* 1 */
242 #define MOVE_DN 2 /* 3 0 4 */
243 #define MOVE_LT 3 /* 2 */
244 #define MOVE_RT 4
246 /* ball movement (12 ways) */
247 /* UUL UR */
248 /* UL UR */
249 /* ULL . URR */
250 /* DLL DRR */
251 /* DL DR */
252 /* DDL DDR */
254 #define DIR_UU (1<<7)
255 #define DIR_U (1<<6)
256 #define DIR_RR (1<<5)
257 #define DIR_R (1<<4)
258 #define DIR_DD (1<<3)
259 #define DIR_D (1<<2)
260 #define DIR_LL (1<<1)
261 #define DIR_L (1<<0)
263 #define MOVE_UUR ( DIR_UU | DIR_R )
264 #define MOVE_UR ( DIR_U | DIR_R )
265 #define MOVE_URR ( DIR_U | DIR_RR )
266 #define MOVE_DRR ( DIR_D | DIR_RR )
267 #define MOVE_DR ( DIR_D | DIR_R )
268 #define MOVE_DDR ( DIR_DD | DIR_R )
269 #define MOVE_DDL ( DIR_DD | DIR_L )
270 #define MOVE_DL ( DIR_D | DIR_L )
271 #define MOVE_DLL ( DIR_D | DIR_LL )
272 #define MOVE_ULL ( DIR_U | DIR_LL )
273 #define MOVE_UL ( DIR_U | DIR_L )
274 #define MOVE_UUL ( DIR_UU | DIR_L )
276 #if (LCD_WIDTH>112) && (LCD_HEIGHT>64)
277 # define CUBE_SIZE 8 /* 8x22=176 */
278 # define pos(a) ((a)>>3)
279 #else
280 # define CUBE_SIZE 4
281 # define pos(a) ((a)>>2)
282 #endif
284 #define STARTING_QIXES 2
285 #define MAX_LEVEL 10
286 #define MAX_QIXES MAX_LEVEL+STARTING_QIXES
287 #define BOARD_W ((int)(LCD_WIDTH/CUBE_SIZE))
288 #define BOARD_H ((int)(LCD_HEIGHT/CUBE_SIZE))
289 #define BOARD_X (LCD_WIDTH-BOARD_W*CUBE_SIZE)/2
290 #define BOARD_Y (LCD_HEIGHT-BOARD_H*CUBE_SIZE)/2
292 #ifdef HAVE_LCD_COLOR
293 #define CLR_RED LCD_RGBPACK(255,0,0) /* used to imply danger */
294 #define CLR_LTBLUE LCD_RGBPACK(125, 145, 180) /* used for frame and filling */
295 #define PLR_COL LCD_WHITE /* color used for the player */
296 #elif LCD_DEPTH>=2
297 #define CLR_RED LCD_DARKGRAY /* used to imply danger */
298 #define CLR_LTBLUE LCD_LIGHTGRAY /* used for frame and filling */
299 #define PLR_COL LCD_BLACK /* color used for the player */
300 #endif
302 #if LCD_DEPTH>=2
303 #define EMPTIED LCD_BLACK /* empty spot */
304 #define FILLED CLR_LTBLUE /* filled spot */
305 #define TRAIL CLR_RED /* the red trail of the player */
306 #define QIX LCD_WHITE
307 #else
308 #define EMPTIED 0
309 #define FILLED 1
310 #define TRAIL 2
311 #define QIX 3
312 #endif
313 #define UNCHECKED 0
314 #define CHECKED 1
315 #define PAINTED -1
316 #define PIC_QIX 0
317 #define PIC_PLAYER 1
319 /* The time (in ms) for one iteration through the game loop - decrease this
320 to speed up the game - note that current_tick is (currently) only accurate
321 to 10ms.
323 static int speed = 6; /* CYCLETIME = (11-speed)*10 ms */
324 static int difficulty = 75; /* Percentage of screen that needs to be filled
325 * in order to win the game */
327 static bool quit = false;
329 static unsigned int board[BOARD_H][BOARD_W];
330 static int testboard[BOARD_H][BOARD_W];
332 #if CUBE_SIZE == 8
334 00011000 0x18 - 11100111 0xe7
335 00111100 0x3c - 11100111 0xe7
336 01111110 0x7e - 11000011 0xc3
337 11111111 0xff - 00000000 0x00
338 11111111 0xff - 00000000 0x00
339 01111110 0x7e - 11000011 0xc3
340 00111100 0x3c - 11100111 0xe7
341 00011000 0x18 - 11100111 0xe7
343 const unsigned char pics[2][8] = {
344 {0x18, 0x3c, 0x7e, 0xff, 0xff, 0x7e, 0x3c, 0x18}, /* Alien (QIX) */
345 {0xe7, 0xe7, 0xc3, 0x00, 0x00, 0xc3, 0xe7, 0xe7} /* Player (XONIX) */
347 #elif CUBE_SIZE == 4
349 0110 0x6 - 1001 0x9
350 1111 0xf - 0110 0x6
351 1111 0xf - 0110 0x6
352 0110 0x6 - 1001 0x9
354 const unsigned char pics[2][4] = {
355 {0x6, 0xf, 0xf, 0x6}, /* Alien (QIX) */
356 {0x9, 0x6, 0x6, 0x9} /* Player (XONIX) */
358 #else
359 #error Incorrect CUBE_SIZE value.
360 #endif
362 static struct qix
364 int velocity; /* velocity */
365 int x, y; /* position on screen */
366 int angle; /* angle */
367 } qixes[MAX_QIXES]; /* black_qix */
369 static struct splayer
371 int i, j; /* position on board */
372 int move, score, level, lives;
373 bool drawing;
374 bool gameover;
375 } player;
377 static int percentage_cache;
379 /*************************** STACK STUFF **********************/
381 /* the stack */
382 #define STACK_SIZE (2*BOARD_W*BOARD_H)
383 static struct pos
385 int x, y; /* position on board */
386 } stack[STACK_SIZE];
387 static int stackPointer;
389 static inline bool pop (struct pos *p)
391 if (stackPointer > 0) {
392 p->x = stack[stackPointer].x;
393 p->y = stack[stackPointer].y;
394 stackPointer--;
395 return true;
396 } else
397 return false; /* SE */
400 static inline bool push (struct pos *p)
402 if (stackPointer < STACK_SIZE - 1) {
403 stackPointer++;
404 stack[stackPointer].x = p->x;
405 stack[stackPointer].y = p->y;
406 return true;
407 } else
408 return false; /* SOF */
411 static inline void emptyStack (void)
413 stackPointer = 0;
416 /*********************** END OF STACK STUFF *********************/
418 /* calculate the new x coordinate of the ball according to angle and speed */
419 static inline int get_newx (int x, int len, int deg)
421 if (deg & DIR_R)
422 return x + len;
423 else if (deg & DIR_L)
424 return x - len;
425 else if (deg & DIR_RR)
426 return x + len * 2;
427 else /* (def & DIR_LL) */
428 return x - len * 2;
431 /* calculate the new y coordinate of the ball according to angle and speed */
432 static inline int get_newy (int y, int len, int deg)
434 if (deg & DIR_D)
435 return y + len;
436 else if (deg & DIR_U)
437 return y - len;
438 else if (deg & DIR_DD)
439 return y + len * 2;
440 else /* (deg & DIR_UU) */
441 return y - len * 2;
444 /* make random function get it's value from the device ticker */
445 static inline void randomize (void)
447 rb->srand (*rb->current_tick);
450 /* get a random number between 0 and range-1 */
451 static int t_rand (int range)
453 return rb->rand () % range;
456 /* initializes the test help board */
457 static void init_testboard (void)
459 int j; /* testboard */
460 for (j = 0; j < BOARD_H; j++)
461 /* UNCHEKED == (int)0 */
462 rb->memset( testboard[j], 0, BOARD_W * sizeof( int ) );
465 /* initializes the game board on with the player,qix's and black qix */
466 static void init_board (void)
468 int i, j;
469 for (j = 0; j < BOARD_H; j++)
470 for (i = 0; i < BOARD_W; i++) { /* make a nice cyan frame */
471 if ((i == 0) || (j <= 1) || (i == BOARD_W - 1)
472 || (j >= BOARD_H - 2))
473 board[j][i] = FILLED;
474 else
475 board[j][i] = EMPTIED;
478 /* (level+2) is the number of qixes */
479 for (j = 0; j < player.level + STARTING_QIXES; j++) {
480 qixes[j].velocity = t_rand (2) + 1; /* 1 or 2 pix-per-sec */
482 /* not on frame */
483 qixes[j].x = CUBE_SIZE*2 + 2*t_rand (((BOARD_W-4)*CUBE_SIZE)/2);
484 qixes[j].y = CUBE_SIZE*2 + 2*t_rand (((BOARD_H-4)*CUBE_SIZE)/2);
486 const int angle_table[] = {
487 MOVE_UUR, MOVE_UR, MOVE_URR, MOVE_DRR, MOVE_DR, MOVE_DDR,
488 MOVE_UUL, MOVE_UL, MOVE_ULL, MOVE_DLL, MOVE_DL, MOVE_DDL };
489 qixes[j].angle = angle_table[t_rand (12)];
490 #if CUBE_SIZE == 4
491 /* Work arround a nasty bug. FIXME */
492 if( qixes[j].angle & (DIR_LL|DIR_RR|DIR_UU|DIR_DD) )
493 qixes[j].velocity = 1;
494 #endif
496 /*black_qix.velocity=1;
497 black_qix.x=BOARD_X+(BOARD_W*CUBE_SIZE)/2-CUBE_SIZE/2;
498 black_qix.y=BOARD_Y+(BOARD_H*CUBE_SIZE)-CUBE_SIZE-CUBE_SIZE/2;
499 black_qix.angle=MOVE_UR; */
500 player.move = MOVE_NO;
501 player.drawing = false;
502 player.i = BOARD_W / 2;
503 player.j = 1;
505 percentage_cache = 0;
508 /* calculates the percentage of the screen filling */
509 static int percentage (void)
511 int i, j, filled = 0;
512 for (j = 2; j < BOARD_H - 2; j++)
513 for (i = 1; i < BOARD_W - 1; i++)
514 if (board[j][i] == FILLED)
515 filled++;
516 return (filled * 100) / ((BOARD_W - 2) * (BOARD_H - 4));
519 /* draw the board on with all the game figures */
520 static void refresh_board (void)
522 int i, j;
523 char str[25];
525 #if LCD_DEPTH>=2
526 rb->lcd_set_background (LCD_BLACK);
527 #endif
528 rb->lcd_clear_display ();
529 for (j = 0; j < BOARD_H; j++)
531 unsigned last_color = board[j][0];
532 int last_i = 0;
533 for (i = 1; i < BOARD_W; i++) {
534 if( last_color != board[j][i] )
536 #if LCD_DEPTH>=2
537 rb->lcd_set_foreground (last_color);
538 #else
539 if (last_color != EMPTIED)
540 #endif
541 rb->lcd_fillrect (BOARD_X + CUBE_SIZE * (last_i),
542 BOARD_Y + CUBE_SIZE * j,
543 CUBE_SIZE * (i - last_i), CUBE_SIZE );
544 last_color = board[j][i];
545 last_i = i;
548 #if LCD_DEPTH>=2
549 rb->lcd_set_foreground (last_color);
550 #else
551 if (last_color != EMPTIED)
552 #endif
553 rb->lcd_fillrect (BOARD_X + CUBE_SIZE * (last_i),
554 BOARD_Y + CUBE_SIZE * j,
555 CUBE_SIZE * (i - last_i), CUBE_SIZE);
558 #if LCD_DEPTH>=2
559 rb->lcd_set_foreground (LCD_BLACK);
560 rb->lcd_set_background (CLR_LTBLUE);
561 #else
562 rb->lcd_set_drawmode (DRMODE_COMPLEMENT);
563 #endif
564 rb->snprintf (str, sizeof (str), "Level %d", player.level + 1);
565 rb->lcd_putsxy (BOARD_X, BOARD_Y, str);
566 rb->snprintf (str, sizeof (str), "%d%%", percentage_cache);
567 rb->lcd_putsxy (BOARD_X + CUBE_SIZE * BOARD_W - 24, BOARD_Y, str);
568 rb->snprintf (str, sizeof (str), "Score: %d", player.score);
569 rb->lcd_putsxy (BOARD_X, BOARD_Y + CUBE_SIZE * BOARD_H - 8, str);
570 rb->snprintf (str, sizeof (str),
571 (player.lives != 1) ? "%d Lives" : "%d Life", player.lives);
572 #if LCD_DEPTH>=2
573 rb->lcd_putsxy (BOARD_X + CUBE_SIZE * BOARD_W - 60,
574 BOARD_Y + CUBE_SIZE * BOARD_H - 8, str);
575 #else
576 rb->lcd_putsxy (BOARD_X + CUBE_SIZE * BOARD_W - 40,
577 BOARD_Y + CUBE_SIZE * BOARD_H - 8, str);
578 #endif
580 #if LCD_DEPTH>=2
581 rb->lcd_set_foreground (PLR_COL);
582 rb->lcd_set_background (board[player.j][player.i]);
583 #endif
584 rb->lcd_mono_bitmap (pics[PIC_PLAYER], player.i * CUBE_SIZE + BOARD_X,
585 player.j * CUBE_SIZE + BOARD_Y, CUBE_SIZE, CUBE_SIZE);
587 #if LCD_DEPTH>=2
588 rb->lcd_set_background (EMPTIED);
589 rb->lcd_set_foreground (LCD_WHITE);
590 rb->lcd_set_drawmode (DRMODE_FG);
591 #else
592 rb->lcd_set_drawmode (DRMODE_FG);
593 #endif
594 for (j = 0; j < player.level + STARTING_QIXES; j++)
595 rb->lcd_mono_bitmap (pics[PIC_QIX], qixes[j].x + BOARD_X,
596 qixes[j].y + BOARD_Y, CUBE_SIZE, CUBE_SIZE);
597 #if LCD_DEPTH>=2
598 rb->lcd_set_foreground (LCD_BLACK);
599 #endif
600 rb->lcd_set_drawmode (DRMODE_SOLID);
602 rb->lcd_update ();
605 static inline int infested_area (int i, int j, int v)
607 struct pos p;
608 p.x = i;
609 p.y = j;
610 emptyStack ();
611 if (!push (&p))
612 return -1;
613 while (pop (&p)) {
614 if (testboard[p.y][p.x] == v) continue;
615 if (testboard[p.y][p.x] > UNCHECKED)
616 return 1; /* This area was previously flagged as infested */
617 testboard[p.y][p.x] = v;
618 if (board[p.y][p.x] == QIX)
619 return 1; /* Infested area */
621 struct pos p1 = { p.x+1, p.y };
622 if ((p1.x < BOARD_W)
623 && (board[p1.y][p1.x] != FILLED)
624 && (!push (&p1)))
625 return -1;
628 struct pos p1 = { p.x-1, p.y };
629 if ((p1.x >= 0)
630 && (board[p1.y][p1.x] != FILLED)
631 && (!push (&p1)))
632 return -1;
635 struct pos p1 = { p.x, p.y+1 };
636 if ((p1.y < BOARD_H)
637 && (board[p1.y][p1.x] != FILLED)
638 && (!push (&p1)))
639 return -1;
642 struct pos p1 = { p.x, p.y-1 };
643 if ((p1.y >= 0)
644 && (board[p1.y][p1.x] != FILLED)
645 && (!push (&p1)))
646 return -1;
649 return 0;
652 static inline int fill_area (int i, int j)
654 struct pos p;
655 p.x = i;
656 p.y = j;
657 int v = testboard[p.y][p.x];
658 emptyStack ();
659 if (!push (&p))
660 return -1;
661 while (pop (&p)) {
662 board[p.y][p.x] = FILLED;
663 testboard[p.y][p.x] = PAINTED;
665 struct pos p1 = { p.x+1, p.y };
666 if ((p1.x < BOARD_W)
667 && (testboard[p1.y][p1.x] == v)
668 && (!push (&p1)))
669 return -1;
672 struct pos p1 = { p.x-1, p.y };
673 if ((p1.x >= 0)
674 && (testboard[p1.y][p1.x] == v)
675 && (!push (&p1)))
676 return -1;
679 struct pos p1 = { p.x, p.y+1 };
680 if ((p1.y < BOARD_H)
681 && (testboard[p1.y][p1.x] == v)
682 && (!push (&p1)))
683 return -1;
686 struct pos p1 = { p.x, p.y-1 };
687 if ((p1.y >= 0)
688 && (testboard[p1.y][p1.x] == v)
689 && (!push (&p1)))
690 return -1;
693 return 0;
697 /* take care of stuff after xonix has landed on a filled spot */
698 static void complete_trail (int fill)
700 int i, j, ret;
701 for (j = 0; j < BOARD_H; j++) {
702 for (i = 0; i < BOARD_W; i++) {
703 if (board[j][i] == TRAIL) {
704 if (fill)
705 board[j][i] = FILLED;
706 else
707 board[j][i] = EMPTIED;
712 if (fill) {
713 int v = CHECKED;
714 for (i = 0; i < player.level + STARTING_QIXES; i++) /* add qixes to board */
715 board[pos(qixes[i].y - BOARD_Y)]
716 [pos(qixes[i].x - BOARD_X)] = QIX;
718 init_testboard();
719 for (j = 1; j < BOARD_H - 1; j++) {
720 for (i = 0; i < BOARD_W - 0; i++) {
721 if (board[j][i] != FILLED) {
722 ret = infested_area (i, j, v);
723 if (ret < 0 || ( ret == 0 && fill_area (i, j) ) )
724 quit = true;
725 v++;
730 for (i = 0; i < player.level + STARTING_QIXES; i++) /* add qixes to board */
731 board[pos(qixes[i].y - BOARD_Y)]
732 [pos(qixes[i].x - BOARD_X)] = EMPTIED;
733 percentage_cache = percentage();
736 rb->button_clear_queue();
739 /* returns the color the real pixel(x,y) on the lcd is pointing at */
740 static inline unsigned int getpixel (int x, int y)
742 const int a = pos (x - BOARD_X), b = pos (y - BOARD_Y);
743 if ((a > 0) && (a < BOARD_W) && (b > 0) && (b < BOARD_H)) /* if inside board */
744 return board[b][a];
745 else
746 return FILLED;
749 /* returns the color the ball on (newx,newy) is heading at *----*
750 checks the four edge points of the square if 1st of all | |
751 are a trail (cause it's a lose life situation) and 2nd | |
752 if it's filled so it needs to bounce. *____*
754 static inline unsigned int next_hit (int newx, int newy)
756 if ((getpixel (newx, newy) == TRAIL)
757 || (getpixel (newx, newy + CUBE_SIZE - 1) == TRAIL)
758 || (getpixel (newx + CUBE_SIZE - 1, newy) == TRAIL)
759 || (getpixel (newx + CUBE_SIZE - 1, newy + CUBE_SIZE - 1) == TRAIL))
760 return TRAIL;
761 else if ((getpixel (newx, newy) == FILLED)
762 || (getpixel (newx, newy + CUBE_SIZE - 1) == FILLED)
763 || (getpixel (newx + CUBE_SIZE - 1, newy) == FILLED)
764 || (getpixel (newx + CUBE_SIZE - 1, newy + CUBE_SIZE - 1) ==
765 FILLED))
766 return FILLED;
767 else
768 return EMPTIED;
771 static void die (void)
773 player.lives--;
774 if (player.lives == 0)
775 player.gameover = true;
776 else {
777 refresh_board ();
778 rb->splash (HZ, "Crash!");
779 complete_trail (false);
780 player.move = MOVE_NO;
781 player.drawing = false;
782 player.i = BOARD_W / 2;
783 player.j = 1;
787 /* returns true if the (side) of the block -***-
788 starting from (newx,newy) has any filled pixels * *
789 -***-
791 static inline bool line_check_lt (int newx, int newy)
793 return getpixel (newx, newy + CUBE_SIZE/2-1) == FILLED
794 && getpixel (newx, newy + CUBE_SIZE/2 ) == FILLED;
796 static inline bool line_check_rt (int newx, int newy)
798 return getpixel (newx + CUBE_SIZE-1, newy + CUBE_SIZE/2-1) == FILLED
799 && getpixel (newx + CUBE_SIZE-1, newy + CUBE_SIZE/2 ) == FILLED;
801 static inline bool line_check_up (int newx, int newy)
803 return getpixel (newx + CUBE_SIZE/2-1, newy) == FILLED
804 && getpixel (newx + CUBE_SIZE/2 , newy) == FILLED;
806 static inline bool line_check_dn (int newx, int newy)
808 return getpixel (newx + CUBE_SIZE/2-1, newy + CUBE_SIZE-1) == FILLED
809 && getpixel (newx + CUBE_SIZE/2 , newy + CUBE_SIZE-1) == FILLED;
812 static inline void move_qix (struct qix *q)
814 int newx, newy;
815 newx = get_newx (q->x, q->velocity, q->angle);
816 newy = get_newy (q->y, q->velocity, q->angle);
817 switch (next_hit (newx, newy))
819 case EMPTIED:
820 q->x = newx;
821 q->y = newy;
822 break;
823 case FILLED:
825 const int a = q->angle;
826 q->angle =
827 ((a&(DIR_UU|DIR_U))
828 ? (line_check_up (newx, newy) ? ((a&(DIR_UU|DIR_U))>>4)
829 : (a&(DIR_UU|DIR_U)))
830 : 0)
832 ((a&(DIR_RR|DIR_R))
833 ? (line_check_rt (newx, newy) ? ((a&(DIR_RR|DIR_R))>>4)
834 : (a&(DIR_RR|DIR_R)))
835 : 0)
837 ((a&(DIR_DD|DIR_D))
838 ? (line_check_dn (newx, newy) ? ((a&(DIR_DD|DIR_D))<<4)
839 : (a&(DIR_DD|DIR_D)))
840 : 0)
842 ((a&(DIR_LL|DIR_L))
843 ? (line_check_lt (newx, newy) ? ((a&(DIR_LL|DIR_L))<<4)
844 : (a&(DIR_LL|DIR_L)))
845 : 0);
846 q->x = get_newx (q->x, q->velocity, q->angle);
847 q->y = get_newy (q->y, q->velocity, q->angle);
848 break;
850 case TRAIL:
851 die();
852 break;
856 /* move the board forward timewise */
857 static inline void move_board (void)
859 int j, newi, newj;
861 for (j = 0; j < player.level + STARTING_QIXES; j++)
862 move_qix (&qixes[j]);
863 /* move_qix(&black_qix,true); */
864 if (player.move) {
865 newi = player.i;
866 newj = player.j;
867 switch (player.move) {
868 case MOVE_UP:
869 if (player.j > 1)
870 newj--;
871 break;
872 case MOVE_DN:
873 if (player.j < BOARD_H - 2)
874 newj++;
875 break;
876 case MOVE_LT:
877 if (player.i > 0)
878 newi--;
879 break;
880 case MOVE_RT:
881 if (player.i < BOARD_W - 1)
882 newi++;
883 break;
884 default:
885 break;
888 if ((player.drawing) && (board[newj][newi] == EMPTIED)) /* continue drawing */
889 board[newj][newi] = TRAIL;
890 else if ((player.drawing) && (board[newj][newi] == FILLED)) { /* finish drawing */
891 player.move = MOVE_NO; /* stop moving */
892 player.drawing = false;
893 complete_trail (true);
894 } else if ((board[player.j][player.i] == FILLED)
895 && (board[newj][newi] == EMPTIED)) {
896 /* start drawing */
897 player.drawing = true;
898 board[newj][newi] = TRAIL;
899 /* if the block after next is empty and we're moving onto filled, stop */
900 } else if ((board[newj][newi] == FILLED)
901 && (board[newj + newj-player.j][newi + newi-player.i] == EMPTIED)) {
902 player.move = MOVE_NO;
904 player.i = newi;
905 player.j = newj;
907 if (percentage_cache >= difficulty) { /* finished level */
908 rb->splashf (HZ * 2, "Level %d finished", player.level+1);
909 player.score += percentage_cache;
910 if (player.level < MAX_LEVEL)
911 player.level++;
912 init_board ();
913 refresh_board ();
914 rb->splash (HZ * 2, "Ready?");
918 /* init game's variables */
919 static void init_game (void)
921 player.level = 0;
922 player.score = 0;
923 player.lives = 3;
924 player.gameover = false;
925 player.drawing = false;
926 init_board ();
927 refresh_board ();
928 rb->splash (HZ * 2, "Ready?");
931 /* the main menu */
932 static bool _ingame;
933 static int xobox_menu_cb(int action, const struct menu_item_ex *this_item)
935 if(action == ACTION_REQUEST_MENUITEM
936 && !_ingame && ((intptr_t)this_item)==0)
937 return ACTION_EXIT_MENUITEM;
938 return action;
941 static int xobox_menu(bool ingame)
943 rb->button_clear_queue();
945 int selection = 0;
946 MENUITEM_STRINGLIST(main_menu, "Xobox Menu", xobox_menu_cb,
947 "Resume Game", "Start New Game",
948 "Speed", "Difficulty",
949 "Playback Control", "Quit");
950 _ingame = ingame;
952 while (true) {
953 switch (rb->do_menu(&main_menu, &selection, NULL, false)) {
954 case 0:
955 return 0;
956 case 1:
957 init_game ();
958 return 0;
959 case 2:
960 rb->set_int ("Speed", "", UNIT_INT, &speed, NULL, 1, 1, 10, NULL);
961 break;
962 case 3:
963 rb->set_int ("Difficulty", "", UNIT_INT, &difficulty, NULL,
964 5, 50, 95, NULL);
965 break;
966 case 4:
967 playback_control(NULL);
968 break;
969 case 5:
970 return 1;
971 case MENU_ATTACHED_USB:
972 return 1;
973 default:
974 break;
979 /* general keypad handler loop */
980 static int xobox_loop (void)
982 int button = 0;
983 bool pause = false;
984 int end;
986 if (xobox_menu(false)) {
987 return PLUGIN_OK;
990 while (!quit) {
991 end = *rb->current_tick + ((11-speed)*HZ)/100;
993 #ifdef HAS_BUTTON_HOLD
994 if (rb->button_hold()) {
995 pause = true;
996 rb->splash (HZ, "Paused");
998 #endif
1000 button = rb->button_get_w_tmo (1);
1001 switch (button) {
1002 case UP:
1003 case UP|BUTTON_REPEAT:
1004 player.move = MOVE_UP;
1005 break;
1006 case DOWN:
1007 case DOWN|BUTTON_REPEAT:
1008 player.move = MOVE_DN;
1009 break;
1010 case LEFT:
1011 case LEFT|BUTTON_REPEAT:
1012 player.move = MOVE_LT;
1013 break;
1014 case RIGHT:
1015 case RIGHT|BUTTON_REPEAT:
1016 player.move = MOVE_RT;
1017 break;
1018 case PAUSE:
1019 pause = !pause;
1020 if (pause)
1021 rb->splash (HZ, "Paused");
1022 break;
1023 case QUIT:
1024 if (!pause) {
1025 if (xobox_menu(true)) {
1026 quit = true;
1029 break;
1030 default:
1031 if (rb->default_event_handler (button) == SYS_USB_CONNECTED)
1032 return PLUGIN_USB_CONNECTED;
1033 break;
1035 if (!pause) {
1036 move_board ();
1037 refresh_board ();
1039 if (player.gameover) {
1040 rb->splash (HZ, "Game Over!");
1041 if (xobox_menu(false)) {
1042 quit = true;
1046 if (end > *rb->current_tick)
1047 rb->sleep (end - *rb->current_tick);
1048 else
1049 rb->yield ();
1051 } /* end while */
1052 return PLUGIN_OK; /* for no warnings on compiling */
1055 /* plugin main procedure */
1056 enum plugin_status plugin_start (const void *parameter)
1058 int ret = PLUGIN_OK;
1060 (void) parameter;
1062 rb->lcd_setfont (FONT_SYSFIXED);
1063 #if LCD_DEPTH>=2
1064 rb->lcd_set_backdrop(NULL);
1065 #endif
1067 /* Turn off backlight timeout */
1068 backlight_force_on(); /* backlight control in lib/helper.c */
1070 randomize ();
1071 ret = xobox_loop ();
1073 /* Turn on backlight timeout (revert to settings) */
1074 backlight_use_settings(); /* backlight control in lib/helper.c */
1075 rb->lcd_setfont (FONT_UI);
1077 return ret;