Fix red in bootloaders
[maemo-rb.git] / apps / plugins / xobox.c
blobb26c16eabf1e139786d163733bc5a8b8b3d3a813
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 #else
207 #error No keymap defined!
208 #endif
210 #ifdef HAVE_TOUCHSCREEN
211 #ifndef QUIT
212 #define QUIT BUTTON_TOPLEFT
213 #endif
214 #ifndef LEFT
215 #define LEFT BUTTON_MIDLEFT
216 #endif
217 #ifndef RIGHT
218 #define RIGHT BUTTON_MIDRIGHT
219 #endif
220 #ifndef UP
221 #define UP BUTTON_TOPMIDDLE
222 #endif
223 #ifndef DOWN
224 #define DOWN BUTTON_BOTTOMMIDDLE
225 #endif
226 #ifndef PAUSE
227 #define PAUSE BUTTON_CENTER
228 #endif
229 #endif
231 #define MOVE_NO 0 /* player movement */
232 #define MOVE_UP 1 /* 1 */
233 #define MOVE_DN 2 /* 3 0 4 */
234 #define MOVE_LT 3 /* 2 */
235 #define MOVE_RT 4
237 /* ball movement (12 ways) */
238 /* UUL UR */
239 /* UL UR */
240 /* ULL . URR */
241 /* DLL DRR */
242 /* DL DR */
243 /* DDL DDR */
245 #define DIR_UU (1<<7)
246 #define DIR_U (1<<6)
247 #define DIR_RR (1<<5)
248 #define DIR_R (1<<4)
249 #define DIR_DD (1<<3)
250 #define DIR_D (1<<2)
251 #define DIR_LL (1<<1)
252 #define DIR_L (1<<0)
254 #define MOVE_UUR ( DIR_UU | DIR_R )
255 #define MOVE_UR ( DIR_U | DIR_R )
256 #define MOVE_URR ( DIR_U | DIR_RR )
257 #define MOVE_DRR ( DIR_D | DIR_RR )
258 #define MOVE_DR ( DIR_D | DIR_R )
259 #define MOVE_DDR ( DIR_DD | DIR_R )
260 #define MOVE_DDL ( DIR_DD | DIR_L )
261 #define MOVE_DL ( DIR_D | DIR_L )
262 #define MOVE_DLL ( DIR_D | DIR_LL )
263 #define MOVE_ULL ( DIR_U | DIR_LL )
264 #define MOVE_UL ( DIR_U | DIR_L )
265 #define MOVE_UUL ( DIR_UU | DIR_L )
267 #if (LCD_WIDTH>112) && (LCD_HEIGHT>64)
268 # define CUBE_SIZE 8 /* 8x22=176 */
269 # define pos(a) ((a)>>3)
270 #else
271 # define CUBE_SIZE 4
272 # define pos(a) ((a)>>2)
273 #endif
275 #define STARTING_QIXES 2
276 #define MAX_LEVEL 10
277 #define MAX_QIXES MAX_LEVEL+STARTING_QIXES
278 #define BOARD_W ((int)(LCD_WIDTH/CUBE_SIZE))
279 #define BOARD_H ((int)(LCD_HEIGHT/CUBE_SIZE))
280 #define BOARD_X (LCD_WIDTH-BOARD_W*CUBE_SIZE)/2
281 #define BOARD_Y (LCD_HEIGHT-BOARD_H*CUBE_SIZE)/2
283 #ifdef HAVE_LCD_COLOR
284 #define CLR_RED LCD_RGBPACK(255,0,0) /* used to imply danger */
285 #define CLR_LTBLUE LCD_RGBPACK(125, 145, 180) /* used for frame and filling */
286 #define PLR_COL LCD_WHITE /* color used for the player */
287 #elif LCD_DEPTH>=2
288 #define CLR_RED LCD_DARKGRAY /* used to imply danger */
289 #define CLR_LTBLUE LCD_LIGHTGRAY /* used for frame and filling */
290 #define PLR_COL LCD_BLACK /* color used for the player */
291 #endif
293 #if LCD_DEPTH>=2
294 #define EMPTIED LCD_BLACK /* empty spot */
295 #define FILLED CLR_LTBLUE /* filled spot */
296 #define TRAIL CLR_RED /* the red trail of the player */
297 #define QIX LCD_WHITE
298 #else
299 #define EMPTIED 0
300 #define FILLED 1
301 #define TRAIL 2
302 #define QIX 3
303 #endif
304 #define UNCHECKED 0
305 #define CHECKED 1
306 #define PAINTED -1
307 #define PIC_QIX 0
308 #define PIC_PLAYER 1
310 /* The time (in ms) for one iteration through the game loop - decrease this
311 to speed up the game - note that current_tick is (currently) only accurate
312 to 10ms.
314 static int speed = 6; /* CYCLETIME = (11-speed)*10 ms */
315 static int difficulty = 75; /* Percentage of screen that needs to be filled
316 * in order to win the game */
318 static bool quit = false;
320 static unsigned int board[BOARD_H][BOARD_W];
321 static int testboard[BOARD_H][BOARD_W];
323 #if CUBE_SIZE == 8
325 00011000 0x18 - 11100111 0xe7
326 00111100 0x3c - 11100111 0xe7
327 01111110 0x7e - 11000011 0xc3
328 11111111 0xff - 00000000 0x00
329 11111111 0xff - 00000000 0x00
330 01111110 0x7e - 11000011 0xc3
331 00111100 0x3c - 11100111 0xe7
332 00011000 0x18 - 11100111 0xe7
334 const unsigned char pics[2][8] = {
335 {0x18, 0x3c, 0x7e, 0xff, 0xff, 0x7e, 0x3c, 0x18}, /* Alien (QIX) */
336 {0xe7, 0xe7, 0xc3, 0x00, 0x00, 0xc3, 0xe7, 0xe7} /* Player (XONIX) */
338 #elif CUBE_SIZE == 4
340 0110 0x6 - 1001 0x9
341 1111 0xf - 0110 0x6
342 1111 0xf - 0110 0x6
343 0110 0x6 - 1001 0x9
345 const unsigned char pics[2][4] = {
346 {0x6, 0xf, 0xf, 0x6}, /* Alien (QIX) */
347 {0x9, 0x6, 0x6, 0x9} /* Player (XONIX) */
349 #else
350 #error Incorrect CUBE_SIZE value.
351 #endif
353 static struct qix
355 int velocity; /* velocity */
356 int x, y; /* position on screen */
357 int angle; /* angle */
358 } qixes[MAX_QIXES]; /* black_qix */
360 static struct splayer
362 int i, j; /* position on board */
363 int move, score, level, lives;
364 bool drawing;
365 bool gameover;
366 } player;
368 static int percentage_cache;
370 /*************************** STACK STUFF **********************/
372 /* the stack */
373 #define STACK_SIZE (2*BOARD_W*BOARD_H)
374 static struct pos
376 int x, y; /* position on board */
377 } stack[STACK_SIZE];
378 static int stackPointer;
380 static inline bool pop (struct pos *p)
382 if (stackPointer > 0) {
383 p->x = stack[stackPointer].x;
384 p->y = stack[stackPointer].y;
385 stackPointer--;
386 return true;
387 } else
388 return false; /* SE */
391 static inline bool push (struct pos *p)
393 if (stackPointer < STACK_SIZE - 1) {
394 stackPointer++;
395 stack[stackPointer].x = p->x;
396 stack[stackPointer].y = p->y;
397 return true;
398 } else
399 return false; /* SOF */
402 static inline void emptyStack (void)
404 stackPointer = 0;
407 /*********************** END OF STACK STUFF *********************/
409 /* calculate the new x coordinate of the ball according to angle and speed */
410 static inline int get_newx (int x, int len, int deg)
412 if (deg & DIR_R)
413 return x + len;
414 else if (deg & DIR_L)
415 return x - len;
416 else if (deg & DIR_RR)
417 return x + len * 2;
418 else /* (def & DIR_LL) */
419 return x - len * 2;
422 /* calculate the new y coordinate of the ball according to angle and speed */
423 static inline int get_newy (int y, int len, int deg)
425 if (deg & DIR_D)
426 return y + len;
427 else if (deg & DIR_U)
428 return y - len;
429 else if (deg & DIR_DD)
430 return y + len * 2;
431 else /* (deg & DIR_UU) */
432 return y - len * 2;
435 /* make random function get it's value from the device ticker */
436 static inline void randomize (void)
438 rb->srand (*rb->current_tick);
441 /* get a random number between 0 and range-1 */
442 static int t_rand (int range)
444 return rb->rand () % range;
447 /* initializes the test help board */
448 static void init_testboard (void)
450 int j; /* testboard */
451 for (j = 0; j < BOARD_H; j++)
452 /* UNCHEKED == (int)0 */
453 rb->memset( testboard[j], 0, BOARD_W * sizeof( int ) );
456 /* initializes the game board on with the player,qix's and black qix */
457 static void init_board (void)
459 int i, j;
460 for (j = 0; j < BOARD_H; j++)
461 for (i = 0; i < BOARD_W; i++) { /* make a nice cyan frame */
462 if ((i == 0) || (j <= 1) || (i == BOARD_W - 1)
463 || (j >= BOARD_H - 2))
464 board[j][i] = FILLED;
465 else
466 board[j][i] = EMPTIED;
469 /* (level+2) is the number of qixes */
470 for (j = 0; j < player.level + STARTING_QIXES; j++) {
471 qixes[j].velocity = t_rand (2) + 1; /* 1 or 2 pix-per-sec */
473 /* not on frame */
474 qixes[j].x = CUBE_SIZE*2 + 2*t_rand (((BOARD_W-4)*CUBE_SIZE)/2);
475 qixes[j].y = CUBE_SIZE*2 + 2*t_rand (((BOARD_H-4)*CUBE_SIZE)/2);
477 const int angle_table[] = {
478 MOVE_UUR, MOVE_UR, MOVE_URR, MOVE_DRR, MOVE_DR, MOVE_DDR,
479 MOVE_UUL, MOVE_UL, MOVE_ULL, MOVE_DLL, MOVE_DL, MOVE_DDL };
480 qixes[j].angle = angle_table[t_rand (12)];
481 #if CUBE_SIZE == 4
482 /* Work arround a nasty bug. FIXME */
483 if( qixes[j].angle & (DIR_LL|DIR_RR|DIR_UU|DIR_DD) )
484 qixes[j].velocity = 1;
485 #endif
487 /*black_qix.velocity=1;
488 black_qix.x=BOARD_X+(BOARD_W*CUBE_SIZE)/2-CUBE_SIZE/2;
489 black_qix.y=BOARD_Y+(BOARD_H*CUBE_SIZE)-CUBE_SIZE-CUBE_SIZE/2;
490 black_qix.angle=MOVE_UR; */
491 player.move = MOVE_NO;
492 player.drawing = false;
493 player.i = BOARD_W / 2;
494 player.j = 1;
496 percentage_cache = 0;
499 /* calculates the percentage of the screen filling */
500 static int percentage (void)
502 int i, j, filled = 0;
503 for (j = 2; j < BOARD_H - 2; j++)
504 for (i = 1; i < BOARD_W - 1; i++)
505 if (board[j][i] == FILLED)
506 filled++;
507 return (filled * 100) / ((BOARD_W - 2) * (BOARD_H - 4));
510 /* draw the board on with all the game figures */
511 static void refresh_board (void)
513 int i, j;
514 char str[25];
516 #if LCD_DEPTH>=2
517 rb->lcd_set_background (LCD_BLACK);
518 #endif
519 rb->lcd_clear_display ();
520 for (j = 0; j < BOARD_H; j++)
522 unsigned last_color = board[j][0];
523 int last_i = 0;
524 for (i = 1; i < BOARD_W; i++) {
525 if( last_color != board[j][i] )
527 #if LCD_DEPTH>=2
528 rb->lcd_set_foreground (last_color);
529 #else
530 if (last_color != EMPTIED)
531 #endif
532 rb->lcd_fillrect (BOARD_X + CUBE_SIZE * (last_i),
533 BOARD_Y + CUBE_SIZE * j,
534 CUBE_SIZE * (i - last_i), CUBE_SIZE );
535 last_color = board[j][i];
536 last_i = i;
539 #if LCD_DEPTH>=2
540 rb->lcd_set_foreground (last_color);
541 #else
542 if (last_color != EMPTIED)
543 #endif
544 rb->lcd_fillrect (BOARD_X + CUBE_SIZE * (last_i),
545 BOARD_Y + CUBE_SIZE * j,
546 CUBE_SIZE * (i - last_i), CUBE_SIZE);
549 #if LCD_DEPTH>=2
550 rb->lcd_set_foreground (LCD_BLACK);
551 rb->lcd_set_background (CLR_LTBLUE);
552 #else
553 rb->lcd_set_drawmode (DRMODE_COMPLEMENT);
554 #endif
555 rb->snprintf (str, sizeof (str), "Level %d", player.level + 1);
556 rb->lcd_putsxy (BOARD_X, BOARD_Y, str);
557 rb->snprintf (str, sizeof (str), "%d%%", percentage_cache);
558 rb->lcd_putsxy (BOARD_X + CUBE_SIZE * BOARD_W - 24, BOARD_Y, str);
559 rb->snprintf (str, sizeof (str), "Score: %d", player.score);
560 rb->lcd_putsxy (BOARD_X, BOARD_Y + CUBE_SIZE * BOARD_H - 8, str);
561 rb->snprintf (str, sizeof (str),
562 (player.lives != 1) ? "%d Lives" : "%d Life", player.lives);
563 #if LCD_DEPTH>=2
564 rb->lcd_putsxy (BOARD_X + CUBE_SIZE * BOARD_W - 60,
565 BOARD_Y + CUBE_SIZE * BOARD_H - 8, str);
566 #else
567 rb->lcd_putsxy (BOARD_X + CUBE_SIZE * BOARD_W - 40,
568 BOARD_Y + CUBE_SIZE * BOARD_H - 8, str);
569 #endif
571 #if LCD_DEPTH>=2
572 rb->lcd_set_foreground (PLR_COL);
573 rb->lcd_set_background (board[player.j][player.i]);
574 #endif
575 rb->lcd_mono_bitmap (pics[PIC_PLAYER], player.i * CUBE_SIZE + BOARD_X,
576 player.j * CUBE_SIZE + BOARD_Y, CUBE_SIZE, CUBE_SIZE);
578 #if LCD_DEPTH>=2
579 rb->lcd_set_background (EMPTIED);
580 rb->lcd_set_foreground (LCD_WHITE);
581 rb->lcd_set_drawmode (DRMODE_FG);
582 #else
583 rb->lcd_set_drawmode (DRMODE_FG);
584 #endif
585 for (j = 0; j < player.level + STARTING_QIXES; j++)
586 rb->lcd_mono_bitmap (pics[PIC_QIX], qixes[j].x + BOARD_X,
587 qixes[j].y + BOARD_Y, CUBE_SIZE, CUBE_SIZE);
588 #if LCD_DEPTH>=2
589 rb->lcd_set_foreground (LCD_BLACK);
590 #endif
591 rb->lcd_set_drawmode (DRMODE_SOLID);
593 rb->lcd_update ();
596 static inline int infested_area (int i, int j, int v)
598 struct pos p;
599 p.x = i;
600 p.y = j;
601 emptyStack ();
602 if (!push (&p))
603 return -1;
604 while (pop (&p)) {
605 if (testboard[p.y][p.x] == v) continue;
606 if (testboard[p.y][p.x] > UNCHECKED)
607 return 1; /* This area was previously flagged as infested */
608 testboard[p.y][p.x] = v;
609 if (board[p.y][p.x] == QIX)
610 return 1; /* Infested area */
612 struct pos p1 = { p.x+1, p.y };
613 if ((p1.x < BOARD_W)
614 && (board[p1.y][p1.x] != FILLED)
615 && (!push (&p1)))
616 return -1;
619 struct pos p1 = { p.x-1, p.y };
620 if ((p1.x >= 0)
621 && (board[p1.y][p1.x] != FILLED)
622 && (!push (&p1)))
623 return -1;
626 struct pos p1 = { p.x, p.y+1 };
627 if ((p1.y < BOARD_H)
628 && (board[p1.y][p1.x] != FILLED)
629 && (!push (&p1)))
630 return -1;
633 struct pos p1 = { p.x, p.y-1 };
634 if ((p1.y >= 0)
635 && (board[p1.y][p1.x] != FILLED)
636 && (!push (&p1)))
637 return -1;
640 return 0;
643 static inline int fill_area (int i, int j)
645 struct pos p;
646 p.x = i;
647 p.y = j;
648 int v = testboard[p.y][p.x];
649 emptyStack ();
650 if (!push (&p))
651 return -1;
652 while (pop (&p)) {
653 board[p.y][p.x] = FILLED;
654 testboard[p.y][p.x] = PAINTED;
656 struct pos p1 = { p.x+1, p.y };
657 if ((p1.x < BOARD_W)
658 && (testboard[p1.y][p1.x] == v)
659 && (!push (&p1)))
660 return -1;
663 struct pos p1 = { p.x-1, p.y };
664 if ((p1.x >= 0)
665 && (testboard[p1.y][p1.x] == v)
666 && (!push (&p1)))
667 return -1;
670 struct pos p1 = { p.x, p.y+1 };
671 if ((p1.y < BOARD_H)
672 && (testboard[p1.y][p1.x] == v)
673 && (!push (&p1)))
674 return -1;
677 struct pos p1 = { p.x, p.y-1 };
678 if ((p1.y >= 0)
679 && (testboard[p1.y][p1.x] == v)
680 && (!push (&p1)))
681 return -1;
684 return 0;
688 /* take care of stuff after xonix has landed on a filled spot */
689 static void complete_trail (int fill)
691 int i, j, ret;
692 for (j = 0; j < BOARD_H; j++) {
693 for (i = 0; i < BOARD_W; i++) {
694 if (board[j][i] == TRAIL) {
695 if (fill)
696 board[j][i] = FILLED;
697 else
698 board[j][i] = EMPTIED;
703 if (fill) {
704 int v = CHECKED;
705 for (i = 0; i < player.level + STARTING_QIXES; i++) /* add qixes to board */
706 board[pos(qixes[i].y - BOARD_Y)]
707 [pos(qixes[i].x - BOARD_X)] = QIX;
709 init_testboard();
710 for (j = 1; j < BOARD_H - 1; j++) {
711 for (i = 0; i < BOARD_W - 0; i++) {
712 if (board[j][i] != FILLED) {
713 ret = infested_area (i, j, v);
714 if (ret < 0 || ( ret == 0 && fill_area (i, j) ) )
715 quit = true;
716 v++;
721 for (i = 0; i < player.level + STARTING_QIXES; i++) /* add qixes to board */
722 board[pos(qixes[i].y - BOARD_Y)]
723 [pos(qixes[i].x - BOARD_X)] = EMPTIED;
724 percentage_cache = percentage();
727 rb->button_clear_queue();
730 /* returns the color the real pixel(x,y) on the lcd is pointing at */
731 static inline unsigned int getpixel (int x, int y)
733 const int a = pos (x - BOARD_X), b = pos (y - BOARD_Y);
734 if ((a > 0) && (a < BOARD_W) && (b > 0) && (b < BOARD_H)) /* if inside board */
735 return board[b][a];
736 else
737 return FILLED;
740 /* returns the color the ball on (newx,newy) is heading at *----*
741 checks the four edge points of the square if 1st of all | |
742 are a trail (cause it's a lose life situation) and 2nd | |
743 if it's filled so it needs to bounce. *____*
745 static inline unsigned int next_hit (int newx, int newy)
747 if ((getpixel (newx, newy) == TRAIL)
748 || (getpixel (newx, newy + CUBE_SIZE - 1) == TRAIL)
749 || (getpixel (newx + CUBE_SIZE - 1, newy) == TRAIL)
750 || (getpixel (newx + CUBE_SIZE - 1, newy + CUBE_SIZE - 1) == TRAIL))
751 return TRAIL;
752 else if ((getpixel (newx, newy) == FILLED)
753 || (getpixel (newx, newy + CUBE_SIZE - 1) == FILLED)
754 || (getpixel (newx + CUBE_SIZE - 1, newy) == FILLED)
755 || (getpixel (newx + CUBE_SIZE - 1, newy + CUBE_SIZE - 1) ==
756 FILLED))
757 return FILLED;
758 else
759 return EMPTIED;
762 static void die (void)
764 player.lives--;
765 if (player.lives == 0)
766 player.gameover = true;
767 else {
768 refresh_board ();
769 rb->splash (HZ, "Crash!");
770 complete_trail (false);
771 player.move = MOVE_NO;
772 player.drawing = false;
773 player.i = BOARD_W / 2;
774 player.j = 1;
778 /* returns true if the (side) of the block -***-
779 starting from (newx,newy) has any filled pixels * *
780 -***-
782 static inline bool line_check_lt (int newx, int newy)
784 return getpixel (newx, newy + CUBE_SIZE/2-1) == FILLED
785 && getpixel (newx, newy + CUBE_SIZE/2 ) == FILLED;
787 static inline bool line_check_rt (int newx, int newy)
789 return getpixel (newx + CUBE_SIZE-1, newy + CUBE_SIZE/2-1) == FILLED
790 && getpixel (newx + CUBE_SIZE-1, newy + CUBE_SIZE/2 ) == FILLED;
792 static inline bool line_check_up (int newx, int newy)
794 return getpixel (newx + CUBE_SIZE/2-1, newy) == FILLED
795 && getpixel (newx + CUBE_SIZE/2 , newy) == FILLED;
797 static inline bool line_check_dn (int newx, int newy)
799 return getpixel (newx + CUBE_SIZE/2-1, newy + CUBE_SIZE-1) == FILLED
800 && getpixel (newx + CUBE_SIZE/2 , newy + CUBE_SIZE-1) == FILLED;
803 static inline void move_qix (struct qix *q)
805 int newx, newy;
806 newx = get_newx (q->x, q->velocity, q->angle);
807 newy = get_newy (q->y, q->velocity, q->angle);
808 switch (next_hit (newx, newy))
810 case EMPTIED:
811 q->x = newx;
812 q->y = newy;
813 break;
814 case FILLED:
816 const int a = q->angle;
817 q->angle =
818 ((a&(DIR_UU|DIR_U))
819 ? (line_check_up (newx, newy) ? ((a&(DIR_UU|DIR_U))>>4)
820 : (a&(DIR_UU|DIR_U)))
821 : 0)
823 ((a&(DIR_RR|DIR_R))
824 ? (line_check_rt (newx, newy) ? ((a&(DIR_RR|DIR_R))>>4)
825 : (a&(DIR_RR|DIR_R)))
826 : 0)
828 ((a&(DIR_DD|DIR_D))
829 ? (line_check_dn (newx, newy) ? ((a&(DIR_DD|DIR_D))<<4)
830 : (a&(DIR_DD|DIR_D)))
831 : 0)
833 ((a&(DIR_LL|DIR_L))
834 ? (line_check_lt (newx, newy) ? ((a&(DIR_LL|DIR_L))<<4)
835 : (a&(DIR_LL|DIR_L)))
836 : 0);
837 q->x = get_newx (q->x, q->velocity, q->angle);
838 q->y = get_newy (q->y, q->velocity, q->angle);
839 break;
841 case TRAIL:
842 die();
843 break;
847 /* move the board forward timewise */
848 static inline void move_board (void)
850 int j, newi, newj;
852 for (j = 0; j < player.level + STARTING_QIXES; j++)
853 move_qix (&qixes[j]);
854 /* move_qix(&black_qix,true); */
855 if (player.move) {
856 newi = player.i;
857 newj = player.j;
858 switch (player.move) {
859 case MOVE_UP:
860 if (player.j > 1)
861 newj--;
862 break;
863 case MOVE_DN:
864 if (player.j < BOARD_H - 2)
865 newj++;
866 break;
867 case MOVE_LT:
868 if (player.i > 0)
869 newi--;
870 break;
871 case MOVE_RT:
872 if (player.i < BOARD_W - 1)
873 newi++;
874 break;
875 default:
876 break;
879 if ((player.drawing) && (board[newj][newi] == EMPTIED)) /* continue drawing */
880 board[newj][newi] = TRAIL;
881 else if ((player.drawing) && (board[newj][newi] == FILLED)) { /* finish drawing */
882 player.move = MOVE_NO; /* stop moving */
883 player.drawing = false;
884 complete_trail (true);
885 } else if ((board[player.j][player.i] == FILLED)
886 && (board[newj][newi] == EMPTIED)) {
887 /* start drawing */
888 player.drawing = true;
889 board[newj][newi] = TRAIL;
890 /* if the block after next is empty and we're moving onto filled, stop */
891 } else if ((board[newj][newi] == FILLED)
892 && (board[newj + newj-player.j][newi + newi-player.i] == EMPTIED)) {
893 player.move = MOVE_NO;
895 player.i = newi;
896 player.j = newj;
898 if (percentage_cache >= difficulty) { /* finished level */
899 rb->splashf (HZ * 2, "Level %d finished", player.level+1);
900 player.score += percentage_cache;
901 if (player.level < MAX_LEVEL)
902 player.level++;
903 init_board ();
904 refresh_board ();
905 rb->splash (HZ * 2, "Ready?");
909 /* init game's variables */
910 static void init_game (void)
912 player.level = 0;
913 player.score = 0;
914 player.lives = 3;
915 player.gameover = false;
916 player.drawing = false;
917 init_board ();
918 refresh_board ();
919 rb->splash (HZ * 2, "Ready?");
922 /* the main menu */
923 static bool _ingame;
924 static int xobox_menu_cb(int action, const struct menu_item_ex *this_item)
926 if(action == ACTION_REQUEST_MENUITEM
927 && !_ingame && ((intptr_t)this_item)==0)
928 return ACTION_EXIT_MENUITEM;
929 return action;
932 static int xobox_menu(bool ingame)
934 rb->button_clear_queue();
936 int selection = 0;
937 MENUITEM_STRINGLIST(main_menu, "Xobox Menu", xobox_menu_cb,
938 "Resume Game", "Start New Game",
939 "Speed", "Difficulty",
940 "Playback Control", "Quit");
941 _ingame = ingame;
943 while (true) {
944 switch (rb->do_menu(&main_menu, &selection, NULL, false)) {
945 case 0:
946 return 0;
947 case 1:
948 init_game ();
949 return 0;
950 case 2:
951 rb->set_int ("Speed", "", UNIT_INT, &speed, NULL, 1, 1, 10, NULL);
952 break;
953 case 3:
954 rb->set_int ("Difficulty", "", UNIT_INT, &difficulty, NULL,
955 5, 50, 95, NULL);
956 break;
957 case 4:
958 playback_control(NULL);
959 break;
960 case 5:
961 return 1;
962 case MENU_ATTACHED_USB:
963 return 1;
964 default:
965 break;
970 /* general keypad handler loop */
971 static int xobox_loop (void)
973 int button = 0;
974 bool pause = false;
975 int end;
977 if (xobox_menu(false)) {
978 return PLUGIN_OK;
981 while (!quit) {
982 end = *rb->current_tick + ((11-speed)*HZ)/100;
984 #ifdef HAS_BUTTON_HOLD
985 if (rb->button_hold()) {
986 pause = true;
987 rb->splash (HZ, "Paused");
989 #endif
991 button = rb->button_get_w_tmo (1);
992 switch (button) {
993 case UP:
994 case UP|BUTTON_REPEAT:
995 player.move = MOVE_UP;
996 break;
997 case DOWN:
998 case DOWN|BUTTON_REPEAT:
999 player.move = MOVE_DN;
1000 break;
1001 case LEFT:
1002 case LEFT|BUTTON_REPEAT:
1003 player.move = MOVE_LT;
1004 break;
1005 case RIGHT:
1006 case RIGHT|BUTTON_REPEAT:
1007 player.move = MOVE_RT;
1008 break;
1009 case PAUSE:
1010 pause = !pause;
1011 if (pause)
1012 rb->splash (HZ, "Paused");
1013 break;
1014 case QUIT:
1015 if (!pause) {
1016 if (xobox_menu(true)) {
1017 quit = true;
1020 break;
1021 default:
1022 if (rb->default_event_handler (button) == SYS_USB_CONNECTED)
1023 return PLUGIN_USB_CONNECTED;
1024 break;
1026 if (!pause) {
1027 move_board ();
1028 refresh_board ();
1030 if (player.gameover) {
1031 rb->splash (HZ, "Game Over!");
1032 if (xobox_menu(false)) {
1033 quit = true;
1037 if (end > *rb->current_tick)
1038 rb->sleep (end - *rb->current_tick);
1039 else
1040 rb->yield ();
1042 } /* end while */
1043 return PLUGIN_OK; /* for no warnings on compiling */
1046 /* plugin main procedure */
1047 enum plugin_status plugin_start (const void *parameter)
1049 int ret = PLUGIN_OK;
1051 (void) parameter;
1053 rb->lcd_setfont (FONT_SYSFIXED);
1054 #if LCD_DEPTH>=2
1055 rb->lcd_set_backdrop(NULL);
1056 #endif
1058 /* Turn off backlight timeout */
1059 backlight_force_on(); /* backlight control in lib/helper.c */
1061 randomize ();
1062 ret = xobox_loop ();
1064 /* Turn on backlight timeout (revert to settings) */
1065 backlight_use_settings(); /* backlight control in lib/helper.c */
1066 rb->lcd_setfont (FONT_UI);
1068 return ret;