Fix red in bootloaders
[maemo-rb.git] / apps / plugins / snake.c
bloba5d34e710692e33be292f186098647939125c0cd
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
38 PLUGIN_HEADER
40 /* variable button definitions */
41 #if CONFIG_KEYPAD == RECORDER_PAD
42 #define SNAKE_QUIT BUTTON_OFF
43 #define SNAKE_LEFT BUTTON_LEFT
44 #define SNAKE_RIGHT BUTTON_RIGHT
45 #define SNAKE_UP BUTTON_UP
46 #define SNAKE_DOWN BUTTON_DOWN
47 #define SNAKE_PLAYPAUSE BUTTON_PLAY
49 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
50 #define SNAKE_QUIT BUTTON_OFF
51 #define SNAKE_LEFT BUTTON_LEFT
52 #define SNAKE_RIGHT BUTTON_RIGHT
53 #define SNAKE_UP BUTTON_UP
54 #define SNAKE_DOWN BUTTON_DOWN
55 #define SNAKE_PLAYPAUSE BUTTON_SELECT
57 #elif CONFIG_KEYPAD == ONDIO_PAD
58 #define SNAKE_QUIT BUTTON_OFF
59 #define SNAKE_LEFT BUTTON_LEFT
60 #define SNAKE_RIGHT BUTTON_RIGHT
61 #define SNAKE_UP BUTTON_UP
62 #define SNAKE_DOWN BUTTON_DOWN
63 #define SNAKE_PLAYPAUSE BUTTON_MENU
65 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
66 (CONFIG_KEYPAD == IRIVER_H300_PAD)
67 #define SNAKE_QUIT BUTTON_OFF
68 #define SNAKE_LEFT BUTTON_LEFT
69 #define SNAKE_RIGHT BUTTON_RIGHT
70 #define SNAKE_UP BUTTON_UP
71 #define SNAKE_DOWN BUTTON_DOWN
72 #define SNAKE_PLAYPAUSE BUTTON_ON
74 #define SNAKE_RC_QUIT BUTTON_RC_STOP
76 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
77 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
78 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
79 #define SNAKE_QUIT (BUTTON_SELECT|BUTTON_MENU)
80 #define SNAKE_LEFT BUTTON_LEFT
81 #define SNAKE_RIGHT BUTTON_RIGHT
82 #define SNAKE_UP BUTTON_MENU
83 #define SNAKE_DOWN BUTTON_PLAY
84 #define SNAKE_PLAYPAUSE BUTTON_SELECT
86 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
87 #define SNAKE_QUIT BUTTON_POWER
88 #define SNAKE_LEFT BUTTON_LEFT
89 #define SNAKE_RIGHT BUTTON_RIGHT
90 #define SNAKE_UP BUTTON_UP
91 #define SNAKE_DOWN BUTTON_DOWN
92 #define SNAKE_PLAYPAUSE BUTTON_PLAY
94 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
95 #define SNAKE_QUIT BUTTON_POWER
96 #define SNAKE_LEFT BUTTON_LEFT
97 #define SNAKE_RIGHT BUTTON_RIGHT
98 #define SNAKE_UP BUTTON_UP
99 #define SNAKE_DOWN BUTTON_DOWN
100 #define SNAKE_PLAYPAUSE BUTTON_SELECT
102 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
103 (CONFIG_KEYPAD == SANSA_C200_PAD) || \
104 (CONFIG_KEYPAD == SANSA_CLIP_PAD) || \
105 (CONFIG_KEYPAD == SANSA_M200_PAD)
106 #define SNAKE_QUIT BUTTON_POWER
107 #define SNAKE_LEFT BUTTON_LEFT
108 #define SNAKE_RIGHT BUTTON_RIGHT
109 #define SNAKE_UP BUTTON_UP
110 #define SNAKE_DOWN BUTTON_DOWN
111 #define SNAKE_PLAYPAUSE BUTTON_SELECT
113 #elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
114 #define SNAKE_QUIT (BUTTON_HOME|BUTTON_REPEAT)
115 #define SNAKE_LEFT BUTTON_LEFT
116 #define SNAKE_RIGHT BUTTON_RIGHT
117 #define SNAKE_UP BUTTON_UP
118 #define SNAKE_DOWN BUTTON_DOWN
119 #define SNAKE_PLAYPAUSE BUTTON_SELECT
121 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
122 #define SNAKE_QUIT BUTTON_POWER
123 #define SNAKE_LEFT BUTTON_LEFT
124 #define SNAKE_RIGHT BUTTON_RIGHT
125 #define SNAKE_UP BUTTON_SCROLL_UP
126 #define SNAKE_DOWN BUTTON_SCROLL_DOWN
127 #define SNAKE_PLAYPAUSE BUTTON_PLAY
129 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
130 #define SNAKE_QUIT BUTTON_BACK
131 #define SNAKE_LEFT BUTTON_LEFT
132 #define SNAKE_RIGHT BUTTON_RIGHT
133 #define SNAKE_UP BUTTON_UP
134 #define SNAKE_DOWN BUTTON_DOWN
135 #define SNAKE_PLAYPAUSE BUTTON_SELECT
137 #elif (CONFIG_KEYPAD == MROBE100_PAD)
138 #define SNAKE_QUIT BUTTON_POWER
139 #define SNAKE_LEFT BUTTON_LEFT
140 #define SNAKE_RIGHT BUTTON_RIGHT
141 #define SNAKE_UP BUTTON_UP
142 #define SNAKE_DOWN BUTTON_DOWN
143 #define SNAKE_PLAYPAUSE BUTTON_SELECT
145 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
146 #define SNAKE_QUIT BUTTON_RC_REC
147 #define SNAKE_LEFT BUTTON_RC_REW
148 #define SNAKE_RIGHT BUTTON_RC_FF
149 #define SNAKE_UP BUTTON_RC_VOL_UP
150 #define SNAKE_DOWN BUTTON_RC_VOL_DOWN
151 #define SNAKE_PLAYPAUSE BUTTON_RC_PLAY
153 #define SNAKE_RC_QUIT BUTTON_REC
155 #elif (CONFIG_KEYPAD == COWOND2_PAD)
156 #define SNAKE_QUIT BUTTON_POWER
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 == MROBE500_PAD
175 #define SNAKE_QUIT BUTTON_POWER
176 #define SNAKE_RC_QUIT BUTTON_RC_DOWN
178 #elif (CONFIG_KEYPAD == ONDAVX747_PAD)
179 #define SNAKE_QUIT BUTTON_POWER
181 #else
182 #error No keymap defined!
183 #endif
185 #ifdef HAVE_TOUCHSCREEN
186 #ifndef SNAKE_QUIT
187 #define SNAKE_QUIT BUTTON_TOPLEFT
188 #endif
189 #ifndef SNAKE_LEFT
190 #define SNAKE_LEFT BUTTON_MIDLEFT
191 #endif
192 #ifndef SNAKE_RIGHT
193 #define SNAKE_RIGHT BUTTON_MIDRIGHT
194 #endif
195 #ifndef SNAKE_UP
196 #define SNAKE_UP BUTTON_TOPMIDDLE
197 #endif
198 #ifndef SNAKE_DOWN
199 #define SNAKE_DOWN BUTTON_BOTTOMMIDDLE
200 #endif
201 #ifndef SNAKE_PLAYPAUSE
202 #define SNAKE_PLAYPAUSE BUTTON_CENTER
203 #endif
204 #endif
206 #define BOARD_WIDTH (LCD_WIDTH/4)
207 #define BOARD_HEIGHT (LCD_HEIGHT/4)
209 static int board[BOARD_WIDTH][BOARD_HEIGHT],snakelength;
210 static unsigned int score,hiscore=0,level=1;
211 static int dir,dead=0;
212 static bool apple;
214 void die (void)
216 char pscore[17];
217 rb->lcd_clear_display();
218 rb->snprintf(pscore,sizeof(pscore),"Your score: %d",score);
219 rb->lcd_puts(0,0,"Oops...");
220 rb->lcd_puts(0,1, pscore);
221 if (score>hiscore) {
222 hiscore=score;
223 rb->lcd_puts(0,2,"New High Score!");
225 else {
226 rb->snprintf(pscore,sizeof(pscore),"High Score: %d",hiscore);
227 rb->lcd_puts(0,2,pscore);
229 rb->lcd_update();
230 rb->sleep(3*HZ);
231 dead=1;
234 void colission (short x, short y)
236 switch (board[x][y]) {
237 case 0:
238 break;
239 case -1:
240 snakelength+=2;
241 score+=level;
242 apple=false;
243 break;
244 default:
245 die();
246 break;
248 if (x==BOARD_WIDTH || x<0 || y==BOARD_HEIGHT || y<0)
249 die();
252 void move_head (short x, short y)
254 switch (dir) {
255 case 0:
256 y-=1;
257 break;
258 case 1:
259 x+=1;
260 break;
261 case 2:
262 y+=1;
263 break;
264 case 3:
265 x-=1;
266 break;
268 colission (x,y);
269 if (dead)
270 return;
271 board[x][y]=1;
272 rb->lcd_fillrect(x*4,y*4,4,4);
275 void frame (void)
277 short x,y,head=0;
278 for (x=0; x<BOARD_WIDTH; x++) {
279 for (y=0; y<BOARD_HEIGHT; y++) {
280 switch (board[x][y]) {
281 case 1:
282 if (!head) {
283 move_head(x,y);
284 if (dead)
285 return;
286 board[x][y]++;
287 head=1;
289 break;
290 case 0:
291 break;
292 case -1:
293 break;
294 default:
295 if (board[x][y]==snakelength) {
296 board[x][y]=0;
297 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
298 rb->lcd_fillrect(x*4,y*4,4,4);
299 rb->lcd_set_drawmode(DRMODE_SOLID);
301 else
302 board[x][y]++;
303 break;
307 rb->lcd_update();
310 void redraw (void)
312 short x,y;
313 rb->lcd_clear_display();
314 for (x=0; x<BOARD_WIDTH; x++) {
315 for (y=0; y<BOARD_HEIGHT; y++) {
316 switch (board[x][y]) {
317 case -1:
318 rb->lcd_fillrect((x*4)+1,y*4,2,4);
319 rb->lcd_fillrect(x*4,(y*4)+1,4,2);
320 break;
321 case 0:
322 break;
323 default:
324 rb->lcd_fillrect(x*4,y*4,4,4);
325 break;
329 rb->lcd_update();
332 void game_pause (void) {
333 int button;
334 rb->lcd_clear_display();
335 rb->lcd_putsxy(3,12,"Game Paused");
336 #if CONFIG_KEYPAD == RECORDER_PAD
337 rb->lcd_putsxy(3,22,"[Play] to resume");
338 #elif CONFIG_KEYPAD == ONDIO_PAD
339 rb->lcd_putsxy(3,22,"[Mode] to resume");
340 #endif
341 rb->lcd_putsxy(3,32,"[Off] to quit");
342 rb->lcd_update();
343 while (1) {
344 button=rb->button_get(true);
345 switch (button) {
346 #ifdef SNAKE_RC_QUIT
347 case SNAKE_RC_QUIT:
348 #endif
349 case SNAKE_QUIT:
350 dead=1;
351 return;
352 case SNAKE_PLAYPAUSE:
353 redraw();
354 rb->sleep(HZ/2);
355 return;
356 default:
357 if (rb->default_event_handler(button)==SYS_USB_CONNECTED) {
358 dead=2;
359 return;
361 break;
367 void game (void) {
368 int button;
369 short x,y;
370 while (1) {
371 frame();
372 if (dead)
373 return;
374 if (!apple) {
375 do {
376 x=rb->rand() % BOARD_WIDTH;
377 y=rb->rand() % BOARD_HEIGHT;
378 } while (board[x][y]);
379 apple=true;
380 board[x][y]=-1;
381 rb->lcd_fillrect((x*4)+1,y*4,2,4);
382 rb->lcd_fillrect(x*4,(y*4)+1,4,2);
385 rb->sleep(HZ/level);
387 button=rb->button_get(false);
389 #ifdef HAS_BUTTON_HOLD
390 if (rb->button_hold())
391 button = SNAKE_PLAYPAUSE;
392 #endif
394 switch (button) {
395 case SNAKE_UP:
396 if (dir!=2) dir=0;
397 break;
398 case SNAKE_RIGHT:
399 if (dir!=3) dir=1;
400 break;
401 case SNAKE_DOWN:
402 if (dir!=0) dir=2;
403 break;
404 case SNAKE_LEFT:
405 if (dir!=1) dir=3;
406 break;
407 #ifdef SNAKE_RC_QUIT
408 case SNAKE_RC_QUIT:
409 #endif
410 case SNAKE_QUIT:
411 dead=1;
412 return;
413 case SNAKE_PLAYPAUSE:
414 game_pause();
415 break;
416 default:
417 if (rb->default_event_handler(button)==SYS_USB_CONNECTED) {
418 dead=2;
419 return;
421 break;
426 void game_init(void) {
427 int selection=0;
428 short x,y;
429 bool menu_quit = false;
431 for (x=0; x<BOARD_WIDTH; x++) {
432 for (y=0; y<BOARD_HEIGHT; y++) {
433 board[x][y]=0;
436 dead=0;
437 apple=false;
438 snakelength=4;
439 score=0;
440 board[11][7]=1;
442 MENUITEM_STRINGLIST(menu,"Snake Menu",NULL,"Start New Game","Starting Level",
443 "Quit");
445 while (!menu_quit) {
446 switch(rb->do_menu(&menu, &selection, NULL, false))
448 case 0:
449 menu_quit = true; /* start playing */
450 break;
452 case 1:
453 rb->set_int("Starting Level", "", UNIT_INT, &level, NULL,
454 1, 1, 9, NULL );
455 break;
457 default:
458 dead=1; /* quit program */
459 menu_quit = true;
460 break;
466 enum plugin_status plugin_start(const void* parameter)
468 (void)(parameter);
470 game_init();
471 rb->lcd_clear_display();
472 game();
473 return (dead==1)?PLUGIN_OK:PLUGIN_USB_CONNECTED;
476 #endif