as3525 usb: build as3525v2 file based on CONFIG_USBOTG, and fix warnings
[kugel-rb.git] / apps / plugins / snake.c
blobc3fe84e6d0455ac5893a0fbd1f04723195048184
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Itai Shaked
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
23 Snake!
25 by Itai Shaked
27 ok, a little explanation -
28 board holds the snake and apple position - 1+ - snake body (the number
29 represents the age [1 is the snake's head]).
30 -1 is an apple, and 0 is a clear spot.
31 dir is the current direction of the snake - 0=up, 1=right, 2=down, 3=left;
35 #include "plugin.h"
36 #ifdef HAVE_LCD_BITMAP
37 #include "lib/configfile.h"
38 #include "lib/highscore.h"
39 #include "lib/playback_control.h"
41 PLUGIN_HEADER
43 /* variable button definitions */
44 #if CONFIG_KEYPAD == RECORDER_PAD
45 #define SNAKE_QUIT BUTTON_OFF
46 #define SNAKE_LEFT BUTTON_LEFT
47 #define SNAKE_RIGHT BUTTON_RIGHT
48 #define SNAKE_UP BUTTON_UP
49 #define SNAKE_DOWN BUTTON_DOWN
50 #define SNAKE_PLAYPAUSE BUTTON_PLAY
52 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
53 #define SNAKE_QUIT BUTTON_OFF
54 #define SNAKE_LEFT BUTTON_LEFT
55 #define SNAKE_RIGHT BUTTON_RIGHT
56 #define SNAKE_UP BUTTON_UP
57 #define SNAKE_DOWN BUTTON_DOWN
58 #define SNAKE_PLAYPAUSE BUTTON_SELECT
60 #elif CONFIG_KEYPAD == ONDIO_PAD
61 #define SNAKE_QUIT BUTTON_OFF
62 #define SNAKE_LEFT BUTTON_LEFT
63 #define SNAKE_RIGHT BUTTON_RIGHT
64 #define SNAKE_UP BUTTON_UP
65 #define SNAKE_DOWN BUTTON_DOWN
66 #define SNAKE_PLAYPAUSE BUTTON_MENU
68 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
69 (CONFIG_KEYPAD == IRIVER_H300_PAD)
70 #define SNAKE_QUIT BUTTON_OFF
71 #define SNAKE_LEFT BUTTON_LEFT
72 #define SNAKE_RIGHT BUTTON_RIGHT
73 #define SNAKE_UP BUTTON_UP
74 #define SNAKE_DOWN BUTTON_DOWN
75 #define SNAKE_PLAYPAUSE BUTTON_ON
77 #define SNAKE_RC_QUIT BUTTON_RC_STOP
79 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
80 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
81 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
82 #define SNAKE_QUIT (BUTTON_SELECT|BUTTON_MENU)
83 #define SNAKE_LEFT BUTTON_LEFT
84 #define SNAKE_RIGHT BUTTON_RIGHT
85 #define SNAKE_UP BUTTON_MENU
86 #define SNAKE_DOWN BUTTON_PLAY
87 #define SNAKE_PLAYPAUSE BUTTON_SELECT
89 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
90 #define SNAKE_QUIT BUTTON_POWER
91 #define SNAKE_LEFT BUTTON_LEFT
92 #define SNAKE_RIGHT BUTTON_RIGHT
93 #define SNAKE_UP BUTTON_UP
94 #define SNAKE_DOWN BUTTON_DOWN
95 #define SNAKE_PLAYPAUSE BUTTON_PLAY
97 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
98 #define SNAKE_QUIT BUTTON_POWER
99 #define SNAKE_LEFT BUTTON_LEFT
100 #define SNAKE_RIGHT BUTTON_RIGHT
101 #define SNAKE_UP BUTTON_UP
102 #define SNAKE_DOWN BUTTON_DOWN
103 #define SNAKE_PLAYPAUSE BUTTON_SELECT
105 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
106 (CONFIG_KEYPAD == SANSA_C200_PAD) || \
107 (CONFIG_KEYPAD == SANSA_CLIP_PAD) || \
108 (CONFIG_KEYPAD == SANSA_M200_PAD)
109 #define SNAKE_QUIT BUTTON_POWER
110 #define SNAKE_LEFT BUTTON_LEFT
111 #define SNAKE_RIGHT BUTTON_RIGHT
112 #define SNAKE_UP BUTTON_UP
113 #define SNAKE_DOWN BUTTON_DOWN
114 #define SNAKE_PLAYPAUSE BUTTON_SELECT
116 #elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
117 #define SNAKE_QUIT (BUTTON_HOME|BUTTON_REPEAT)
118 #define SNAKE_LEFT BUTTON_LEFT
119 #define SNAKE_RIGHT BUTTON_RIGHT
120 #define SNAKE_UP BUTTON_UP
121 #define SNAKE_DOWN BUTTON_DOWN
122 #define SNAKE_PLAYPAUSE BUTTON_SELECT
124 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
125 #define SNAKE_QUIT BUTTON_POWER
126 #define SNAKE_LEFT BUTTON_LEFT
127 #define SNAKE_RIGHT BUTTON_RIGHT
128 #define SNAKE_UP BUTTON_SCROLL_UP
129 #define SNAKE_DOWN BUTTON_SCROLL_DOWN
130 #define SNAKE_PLAYPAUSE BUTTON_PLAY
132 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
133 #define SNAKE_QUIT BUTTON_BACK
134 #define SNAKE_LEFT BUTTON_LEFT
135 #define SNAKE_RIGHT BUTTON_RIGHT
136 #define SNAKE_UP BUTTON_UP
137 #define SNAKE_DOWN BUTTON_DOWN
138 #define SNAKE_PLAYPAUSE BUTTON_SELECT
140 #elif (CONFIG_KEYPAD == MROBE100_PAD)
141 #define SNAKE_QUIT BUTTON_POWER
142 #define SNAKE_LEFT BUTTON_LEFT
143 #define SNAKE_RIGHT BUTTON_RIGHT
144 #define SNAKE_UP BUTTON_UP
145 #define SNAKE_DOWN BUTTON_DOWN
146 #define SNAKE_PLAYPAUSE BUTTON_SELECT
148 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
149 #define SNAKE_QUIT BUTTON_RC_REC
150 #define SNAKE_LEFT BUTTON_RC_REW
151 #define SNAKE_RIGHT BUTTON_RC_FF
152 #define SNAKE_UP BUTTON_RC_VOL_UP
153 #define SNAKE_DOWN BUTTON_RC_VOL_DOWN
154 #define SNAKE_PLAYPAUSE BUTTON_RC_PLAY
156 #define SNAKE_RC_QUIT BUTTON_REC
158 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
159 #define SNAKE_QUIT BUTTON_BACK
160 #define SNAKE_LEFT BUTTON_LEFT
161 #define SNAKE_RIGHT BUTTON_RIGHT
162 #define SNAKE_UP BUTTON_UP
163 #define SNAKE_DOWN BUTTON_DOWN
164 #define SNAKE_PLAYPAUSE BUTTON_PLAY
166 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
167 #define SNAKE_QUIT BUTTON_POWER
168 #define SNAKE_LEFT BUTTON_LEFT
169 #define SNAKE_RIGHT BUTTON_RIGHT
170 #define SNAKE_UP BUTTON_UP
171 #define SNAKE_DOWN BUTTON_DOWN
172 #define SNAKE_PLAYPAUSE BUTTON_MENU
174 #elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD
175 #define SNAKE_QUIT BUTTON_POWER
176 #define SNAKE_LEFT BUTTON_PREV
177 #define SNAKE_RIGHT BUTTON_NEXT
178 #define SNAKE_UP BUTTON_UP
179 #define SNAKE_DOWN BUTTON_DOWN
180 #define SNAKE_PLAYPAUSE BUTTON_MENU
182 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
183 #define SNAKE_QUIT BUTTON_REC
184 #define SNAKE_LEFT BUTTON_LEFT
185 #define SNAKE_RIGHT BUTTON_RIGHT
186 #define SNAKE_UP BUTTON_UP
187 #define SNAKE_DOWN BUTTON_DOWN
188 #define SNAKE_PLAYPAUSE BUTTON_PLAY
190 #elif CONFIG_KEYPAD == MROBE500_PAD
191 #define SNAKE_QUIT BUTTON_POWER
192 #define SNAKE_RC_QUIT BUTTON_RC_DOWN
194 #elif (CONFIG_KEYPAD == ONDAVX747_PAD)
195 #define SNAKE_QUIT BUTTON_POWER
197 #elif (CONFIG_KEYPAD == ONDAVX777_PAD)
198 #define SNAKE_QUIT BUTTON_POWER
200 #elif CONFIG_KEYPAD == COWON_D2_PAD
201 #define SNAKE_QUIT BUTTON_POWER
203 #elif CONFIG_KEYPAD == PBELL_VIBE500_PAD
204 #define SNAKE_QUIT BUTTON_REC
205 #define SNAKE_LEFT BUTTON_PREV
206 #define SNAKE_RIGHT BUTTON_NEXT
207 #define SNAKE_UP BUTTON_UP
208 #define SNAKE_DOWN BUTTON_DOWN
209 #define SNAKE_PLAYPAUSE BUTTON_PLAY
211 #elif CONFIG_KEYPAD == MPIO_HD200_PAD
212 #define SNAKE_QUIT (BUTTON_REC|BUTTON_PLAY)
213 #define SNAKE_LEFT BUTTON_VOL_DOWN
214 #define SNAKE_RIGHT BUTTON_VOL_UP
215 #define SNAKE_UP BUTTON_PREV
216 #define SNAKE_DOWN BUTTON_NEXT
217 #define SNAKE_PLAYPAUSE BUTTON_PLAY
219 #else
220 #error No keymap defined!
221 #endif
223 #ifdef HAVE_TOUCHSCREEN
224 #ifndef SNAKE_QUIT
225 #define SNAKE_QUIT BUTTON_TOPLEFT
226 #endif
227 #ifndef SNAKE_LEFT
228 #define SNAKE_LEFT BUTTON_MIDLEFT
229 #endif
230 #ifndef SNAKE_RIGHT
231 #define SNAKE_RIGHT BUTTON_MIDRIGHT
232 #endif
233 #ifndef SNAKE_UP
234 #define SNAKE_UP BUTTON_TOPMIDDLE
235 #endif
236 #ifndef SNAKE_DOWN
237 #define SNAKE_DOWN BUTTON_BOTTOMMIDDLE
238 #endif
239 #ifndef SNAKE_PLAYPAUSE
240 #define SNAKE_PLAYPAUSE BUTTON_CENTER
241 #endif
242 #endif
244 #define BOARD_WIDTH (LCD_WIDTH/4)
245 #define BOARD_HEIGHT (LCD_HEIGHT/4)
247 #define CONFIG_FILE_NAME "snake.cfg"
248 #define SCORE_FILE PLUGIN_GAMES_DIR "/snake.score"
249 #define NUM_SCORES 5
251 static int board[BOARD_WIDTH][BOARD_HEIGHT],snakelength;
252 static unsigned int score, level = 1;
253 static int dir;
254 static bool apple, dead, ingame;
255 static struct highscore highscores[NUM_SCORES];
257 static struct configdata config[] = {
258 {TYPE_INT, 1, 9, { .int_p = &level }, "level", NULL},
261 static void snake_die (void)
263 char pscore[17];
264 rb->lcd_clear_display();
265 rb->snprintf(pscore,sizeof(pscore),"Your score: %d",score);
266 rb->lcd_puts(0,0,"Oops...");
267 rb->lcd_puts(0,1, pscore);
268 if (highscore_update(score, level, "", highscores, NUM_SCORES) == 0) {
269 rb->lcd_puts(0,2,"New High Score!");
271 else {
272 rb->snprintf(pscore, sizeof(pscore),
273 "High Score: %d", highscores[0].score);
274 rb->lcd_puts(0,2,pscore);
276 rb->lcd_update();
277 rb->sleep(3*HZ);
278 dead=true;
281 static void snake_colission (short x, short y)
283 if (x==BOARD_WIDTH || x<0 || y==BOARD_HEIGHT || y<0) {
284 snake_die();
285 return;
287 switch (board[x][y]) {
288 case 0:
289 break;
290 case -1:
291 snakelength+=2;
292 score+=level;
293 apple=false;
294 break;
295 default:
296 snake_die();
297 break;
301 static void snake_move_head (short x, short y)
303 switch (dir) {
304 case 0:
305 y-=1;
306 break;
307 case 1:
308 x+=1;
309 break;
310 case 2:
311 y+=1;
312 break;
313 case 3:
314 x-=1;
315 break;
317 snake_colission (x,y);
318 if (dead)
319 return;
320 board[x][y]=1;
321 rb->lcd_fillrect(x*4,y*4,4,4);
324 static void snake_redraw (void)
326 short x,y;
327 rb->lcd_clear_display();
328 for (x=0; x<BOARD_WIDTH; x++) {
329 for (y=0; y<BOARD_HEIGHT; y++) {
330 switch (board[x][y]) {
331 case -1:
332 rb->lcd_fillrect((x*4)+1,y*4,2,4);
333 rb->lcd_fillrect(x*4,(y*4)+1,4,2);
334 break;
335 case 0:
336 break;
337 default:
338 rb->lcd_fillrect(x*4,y*4,4,4);
339 break;
343 rb->lcd_update();
346 static void snake_frame (void)
348 short x,y,head=0;
349 for (x=0; x<BOARD_WIDTH; x++) {
350 for (y=0; y<BOARD_HEIGHT; y++) {
351 switch (board[x][y]) {
352 case 1:
353 if (!head) {
354 snake_move_head(x,y);
355 if (dead)
356 return;
357 board[x][y]++;
358 head=1;
360 break;
361 case 0:
362 break;
363 case -1:
364 break;
365 default:
366 if (board[x][y]==snakelength) {
367 board[x][y]=0;
368 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
369 rb->lcd_fillrect(x*4,y*4,4,4);
370 rb->lcd_set_drawmode(DRMODE_SOLID);
372 else
373 board[x][y]++;
374 break;
378 rb->lcd_update();
381 static void snake_game_init(void) {
382 short x,y;
383 rb->lcd_clear_display();
385 for (x=0; x<BOARD_WIDTH; x++) {
386 for (y=0; y<BOARD_HEIGHT; y++) {
387 board[x][y]=0;
390 apple=false;
391 dead=false;
392 snakelength=4;
393 score=0;
394 dir=1;
395 board[11][7]=1;
398 static int snake_menu_cb(int action, const struct menu_item_ex *this_item)
400 if(action == ACTION_REQUEST_MENUITEM
401 && !ingame && ((intptr_t)this_item)==0)
402 return ACTION_EXIT_MENUITEM;
403 return action;
406 static int snake_game_menu(void)
408 MENUITEM_STRINGLIST(main_menu,"Snake Menu",snake_menu_cb,
409 "Resume Game",
410 "Start New Game",
411 "Snake Speed",
412 "High Score",
413 "Playback Control",
414 "Quit");
415 int selected = 0;
417 rb->button_clear_queue();
419 while (true) {
420 switch (rb->do_menu(&main_menu, &selected, NULL, false)) {
421 case 0:
422 snake_redraw();
423 return 0;
424 case 1:
425 snake_game_init();
426 return 0;
427 case 2:
428 rb->set_int("Snake Speed", "", UNIT_INT, &level,
429 NULL, 1, 1, 9, NULL);
430 break;
431 case 3:
432 highscore_show(-1, highscores, NUM_SCORES, true);
433 rb->lcd_setfont(FONT_UI);
434 break;
435 case 4:
436 playback_control(NULL);
437 break;
438 case 5:
439 return 1;
440 case MENU_ATTACHED_USB:
441 return 1;
442 default:
443 break;
448 static int snake_game_loop (void) {
449 int button;
450 short x,y;
451 bool pause = false;
453 if (snake_game_menu())
454 return 1;
456 ingame = true;
457 while (true) {
458 if (!pause) {
459 snake_frame();
460 if (dead) {
461 ingame = false;
462 return 0;
464 if (!apple) {
465 do {
466 x=rb->rand() % BOARD_WIDTH;
467 y=rb->rand() % BOARD_HEIGHT;
468 } while (board[x][y]);
469 apple=true;
470 board[x][y]=-1;
471 rb->lcd_fillrect((x*4)+1,y*4,2,4);
472 rb->lcd_fillrect(x*4,(y*4)+1,4,2);
475 rb->sleep(HZ/level);
478 button=rb->button_get(false);
480 #ifdef HAS_BUTTON_HOLD
481 if (rb->button_hold() && !pause)
482 button = SNAKE_PLAYPAUSE;
483 #endif
484 switch (button) {
485 case SNAKE_UP:
486 if (dir!=2) dir=0;
487 break;
488 case SNAKE_RIGHT:
489 if (dir!=3) dir=1;
490 break;
491 case SNAKE_DOWN:
492 if (dir!=0) dir=2;
493 break;
494 case SNAKE_LEFT:
495 if (dir!=1) dir=3;
496 break;
497 case SNAKE_PLAYPAUSE:
498 pause = !pause;
499 if (pause)
500 rb->splash (HZ, "Paused");
501 else
502 snake_redraw();
503 break;
504 #ifdef SNAKE_RC_QUIT
505 case SNAKE_RC_QUIT:
506 #endif
507 case SNAKE_QUIT:
508 return 0;
509 break;
510 default:
511 if (rb->default_event_handler (button) == SYS_USB_CONNECTED)
512 return 1;
513 break;
518 enum plugin_status plugin_start(const void* parameter)
520 (void)parameter;
522 configfile_load(CONFIG_FILE_NAME, config, 1, 0);
523 highscore_load(SCORE_FILE, highscores, NUM_SCORES);
524 rb->lcd_clear_display();
525 ingame = false;
526 while(snake_game_loop() == 0)
528 configfile_save(CONFIG_FILE_NAME, config, 1, 0);
529 highscore_save(SCORE_FILE, highscores, NUM_SCORES);
530 return PLUGIN_OK;
532 #endif