Add platform file for Ipod 1G / 2G. Now only the front image is missing for building...
[Rockbox.git] / apps / plugins / snake.c
blobeeca6bd547bbb09b03b51a34c40f726f09a2db63
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Itai Shaked
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
21 Snake!
23 by Itai Shaked
25 ok, a little explanation -
26 board holds the snake and apple position - 1+ - snake body (the number
27 represents the age [1 is the snake's head]).
28 -1 is an apple, and 0 is a clear spot.
29 dir is the current direction of the snake - 0=up, 1=right, 2=down, 3=left;
33 #include "plugin.h"
34 #ifdef HAVE_LCD_BITMAP
36 PLUGIN_HEADER
38 /* variable button definitions */
39 #if CONFIG_KEYPAD == RECORDER_PAD
40 #define SNAKE_QUIT BUTTON_OFF
41 #define SNAKE_UP BUTTON_UP
42 #define SNAKE_DOWN BUTTON_DOWN
43 #define SNAKE_PLAYPAUSE BUTTON_PLAY
45 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
46 #define SNAKE_QUIT BUTTON_OFF
47 #define SNAKE_UP BUTTON_UP
48 #define SNAKE_DOWN BUTTON_DOWN
49 #define SNAKE_PLAYPAUSE BUTTON_SELECT
51 #elif CONFIG_KEYPAD == ONDIO_PAD
52 #define SNAKE_QUIT BUTTON_OFF
53 #define SNAKE_UP BUTTON_UP
54 #define SNAKE_DOWN BUTTON_DOWN
55 #define SNAKE_PLAYPAUSE BUTTON_MENU
57 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
58 (CONFIG_KEYPAD == IRIVER_H300_PAD)
59 #define SNAKE_QUIT BUTTON_OFF
60 #define SNAKE_UP BUTTON_UP
61 #define SNAKE_DOWN BUTTON_DOWN
62 #define SNAKE_PLAYPAUSE BUTTON_ON
64 #define SNAKE_RC_QUIT BUTTON_RC_STOP
66 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
67 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
68 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
69 #define SNAKE_QUIT (BUTTON_SELECT|BUTTON_MENU)
70 #define SNAKE_UP BUTTON_MENU
71 #define SNAKE_DOWN BUTTON_PLAY
72 #define SNAKE_PLAYPAUSE BUTTON_SELECT
74 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
75 #define SNAKE_QUIT BUTTON_POWER
76 #define SNAKE_UP BUTTON_UP
77 #define SNAKE_DOWN BUTTON_DOWN
78 #define SNAKE_PLAYPAUSE BUTTON_PLAY
80 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
81 #define SNAKE_QUIT BUTTON_POWER
82 #define SNAKE_UP BUTTON_UP
83 #define SNAKE_DOWN BUTTON_DOWN
84 #define SNAKE_PLAYPAUSE BUTTON_SELECT
86 #elif (CONFIG_KEYPAD == SANSA_E200_PAD)
87 #define SNAKE_QUIT BUTTON_POWER
88 #define SNAKE_UP BUTTON_UP
89 #define SNAKE_DOWN BUTTON_DOWN
90 #define SNAKE_PLAYPAUSE BUTTON_SELECT
92 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
93 #define SNAKE_QUIT BUTTON_POWER
94 #define SNAKE_UP BUTTON_SCROLL_UP
95 #define SNAKE_DOWN BUTTON_SCROLL_DOWN
96 #define SNAKE_PLAYPAUSE BUTTON_PLAY
98 #else
99 #error "lacks keymapping"
100 #endif
102 #define BOARD_WIDTH (LCD_WIDTH/4)
103 #define BOARD_HEIGHT (LCD_HEIGHT/4)
105 static int board[BOARD_WIDTH][BOARD_HEIGHT],snakelength;
106 static unsigned int score,hiscore=0,level=1;
107 static short dir,frames,apple,dead=0;
108 static struct plugin_api* rb;
110 void die (void)
112 char pscore[17];
113 rb->lcd_clear_display();
114 rb->snprintf(pscore,sizeof(pscore),"Your score: %d",score);
115 rb->lcd_puts(0,0,"Oops...");
116 rb->lcd_puts(0,1, pscore);
117 if (score>hiscore) {
118 hiscore=score;
119 rb->lcd_puts(0,2,"New High Score!");
121 else {
122 rb->snprintf(pscore,sizeof(pscore),"High Score: %d",hiscore);
123 rb->lcd_puts(0,2,pscore);
125 rb->lcd_update();
126 rb->sleep(3*HZ);
127 dead=1;
130 void colission (short x, short y)
132 switch (board[x][y]) {
133 case 0:
134 break;
135 case -1:
136 snakelength+=2;
137 score+=level;
138 apple=0;
139 break;
140 default:
141 die();
142 break;
144 if (x==BOARD_WIDTH || x<0 || y==BOARD_HEIGHT || y<0)
145 die();
148 void move_head (short x, short y)
150 switch (dir) {
151 case 0:
152 y-=1;
153 break;
154 case 1:
155 x+=1;
156 break;
157 case 2:
158 y+=1;
159 break;
160 case 3:
161 x-=1;
162 break;
164 colission (x,y);
165 if (dead)
166 return;
167 board[x][y]=1;
168 rb->lcd_fillrect(x*4,y*4,4,4);
171 void frame (void)
173 short x,y,head=0;
174 for (x=0; x<BOARD_WIDTH; x++) {
175 for (y=0; y<BOARD_HEIGHT; y++) {
176 switch (board[x][y]) {
177 case 1:
178 if (!head) {
179 move_head(x,y);
180 if (dead)
181 return;
182 board[x][y]++;
183 head=1;
185 break;
186 case 0:
187 break;
188 case -1:
189 break;
190 default:
191 if (board[x][y]==snakelength) {
192 board[x][y]=0;
193 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
194 rb->lcd_fillrect(x*4,y*4,4,4);
195 rb->lcd_set_drawmode(DRMODE_SOLID);
197 else
198 board[x][y]++;
199 break;
203 rb->lcd_update();
206 void redraw (void)
208 short x,y;
209 rb->lcd_clear_display();
210 for (x=0; x<BOARD_WIDTH; x++) {
211 for (y=0; y<BOARD_HEIGHT; y++) {
212 switch (board[x][y]) {
213 case -1:
214 rb->lcd_fillrect((x*4)+1,y*4,2,4);
215 rb->lcd_fillrect(x*4,(y*4)+1,4,2);
216 break;
217 case 0:
218 break;
219 default:
220 rb->lcd_fillrect(x*4,y*4,4,4);
221 break;
225 rb->lcd_update();
228 void game_pause (void) {
229 int button;
230 rb->lcd_clear_display();
231 rb->lcd_putsxy(3,12,"Game Paused");
232 #if CONFIG_KEYPAD == RECORDER_PAD
233 rb->lcd_putsxy(3,22,"[Play] to resume");
234 #elif CONFIG_KEYPAD == ONDIO_PAD
235 rb->lcd_putsxy(3,22,"[Mode] to resume");
236 #endif
237 rb->lcd_putsxy(3,32,"[Off] to quit");
238 rb->lcd_update();
239 while (1) {
240 button=rb->button_get(true);
241 switch (button) {
242 #ifdef SNAKE_RC_QUIT
243 case SNAKE_RC_QUIT:
244 #endif
245 case SNAKE_QUIT:
246 dead=1;
247 return;
248 case SNAKE_PLAYPAUSE:
249 redraw();
250 rb->sleep(HZ/2);
251 return;
252 default:
253 if (rb->default_event_handler(button)==SYS_USB_CONNECTED) {
254 dead=2;
255 return;
257 break;
263 void game (void) {
264 int button;
265 short x,y;
266 while (1) {
267 frame();
268 if (dead)
269 return;
270 frames++;
271 if (frames==10) {
272 frames=0;
273 if (!apple) {
274 do {
275 x=rb->rand() % BOARD_WIDTH;
276 y=rb->rand() % BOARD_HEIGHT;
277 } while (board[x][y]);
278 apple=1;
279 board[x][y]=-1;
280 rb->lcd_fillrect((x*4)+1,y*4,2,4);
281 rb->lcd_fillrect(x*4,(y*4)+1,4,2);
285 rb->sleep(HZ/level);
287 button=rb->button_get(false);
289 #ifdef HAS_BUTTON_HOLD
290 if (rb->button_hold())
291 button = SNAKE_PLAYPAUSE;
292 #endif
294 switch (button) {
295 case SNAKE_UP:
296 if (dir!=2) dir=0;
297 break;
298 case BUTTON_RIGHT:
299 if (dir!=3) dir=1;
300 break;
301 case SNAKE_DOWN:
302 if (dir!=0) dir=2;
303 break;
304 case BUTTON_LEFT:
305 if (dir!=1) dir=3;
306 break;
307 #ifdef SNAKE_RC_QUIT
308 case SNAKE_RC_QUIT:
309 #endif
310 case SNAKE_QUIT:
311 dead=1;
312 return;
313 case SNAKE_PLAYPAUSE:
314 game_pause();
315 break;
316 default:
317 if (rb->default_event_handler(button)==SYS_USB_CONNECTED) {
318 dead=2;
319 return;
321 break;
326 void game_init(void) {
327 int selection=0;
328 short x,y;
329 bool menu_quit = false;
331 for (x=0; x<BOARD_WIDTH; x++) {
332 for (y=0; y<BOARD_HEIGHT; y++) {
333 board[x][y]=0;
336 dead=0;
337 apple=0;
338 snakelength=4;
339 score=0;
340 board[11][7]=1;
342 MENUITEM_STRINGLIST(menu,"Snake Menu",NULL,"Start New Game","Starting Level",
343 "Quit");
345 while (!menu_quit) {
346 switch(rb->do_menu(&menu, &selection))
348 case 0:
349 menu_quit = true; /* start playing */
350 break;
352 case 1:
353 rb->set_int("Starting Level", "", UNIT_INT, &level, NULL,
354 1, 1, 9, NULL );
355 break;
357 default:
358 dead=1; /* quit program */
359 menu_quit = true;
360 break;
366 enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
368 (void)(parameter);
369 rb = api;
371 game_init();
372 rb->lcd_clear_display();
373 game();
374 return (dead==1)?PLUGIN_OK:PLUGIN_USB_CONNECTED;
377 #endif