fix the icons so the correct ones are shown
[kugel-rb.git] / apps / plugins / snake2.c
blobc6115217521ecb19b67166fb1b58a1d97cbb3096
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2003 Mat Holton
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 Snake2!
25 Board consists of a WIDTHxHEIGHT grid. If board element is 0 then nothing is
26 there otherwise it is part of the snake or a wall.
28 Head and Tail are stored
32 #include "plugin.h"
33 #ifdef HAVE_LCD_BITMAP
34 #include "lib/highscore.h"
35 #include "lib/playback_control.h"
37 PLUGIN_HEADER
39 #define WIDTH 28
40 #define HEIGHT 16
42 #if (LCD_WIDTH >= 160) && (LCD_HEIGHT >= 128)
43 #include "pluginbitmaps/snake2_header1.h"
44 #include "pluginbitmaps/snake2_header2.h"
45 #include "pluginbitmaps/snake2_left.h"
46 #include "pluginbitmaps/snake2_right.h"
47 #include "pluginbitmaps/snake2_bottom.h"
48 #define BMPHEIGHT_snake2_header BMPHEIGHT_snake2_header1
49 #define BMPWIDTH_snake2_header BMPWIDTH_snake2_header1
50 #endif
52 #if (LCD_WIDTH >= 640) && (LCD_HEIGHT >= 480)
53 #define MULTIPLIER 20 /*Modifier for porting on other screens*/
54 #define MODIFIER_1 20
55 #define MODIFIER_2 16
56 #define CENTER_X 40
57 #define CENTER_Y 110
58 #define TOP_X1 68 /* x-coord of the upperleft item (game type) */
59 #define TOP_X2 562 /* x-coord of the upperright item (maze type) */
60 #define TOP_X3 84 /* x-coord of the lowerleft item (speed) */
61 #define TOP_X4 548 /* x-coord of the lowerright item (hi-score) */
62 #define TOP_Y1 8 /* y-coord of the top row of items */
63 #define TOP_Y2 50 /* y-coord of the bottom row of items */
64 #elif (LCD_WIDTH >= 320) && (LCD_HEIGHT >= 240)
65 #define MULTIPLIER 10 /*Modifier for porting on other screens*/
66 #define MODIFIER_1 10
67 #define MODIFIER_2 8
68 #define CENTER_X 20
69 #define CENTER_Y 55
70 #define TOP_X1 34 /* x-coord of the upperleft item (game type) */
71 #define TOP_X2 281 /* x-coord of the upperright item (maze type) */
72 #define TOP_X3 42 /* x-coord of the lowerleft item (speed) */
73 #define TOP_X4 274 /* x-coord of the lowerright item (hi-score) */
74 #define TOP_Y1 4 /* y-coord of the top row of items */
75 #define TOP_Y2 25 /* y-coord of the bottom row of items */
76 #elif (LCD_WIDTH >= 240) && (LCD_HEIGHT >= 168)
77 #define MULTIPLIER 8
78 #define MODIFIER_1 8
79 #define MODIFIER_2 6
80 #define CENTER_X 8
81 #define CENTER_Y 34
82 #define TOP_X1 34
83 #define TOP_X2 201
84 #define TOP_X3 42
85 #define TOP_X4 194
86 #define TOP_Y1 4
87 #define TOP_Y2 25
88 #elif (LCD_WIDTH >= 220) && (LCD_HEIGHT >= 176)
89 #define MULTIPLIER 7
90 #define MODIFIER_1 7
91 #define MODIFIER_2 5
92 #define CENTER_X 12
93 #define CENTER_Y 46
94 #define TOP_X1 34
95 #define TOP_X2 181
96 #define TOP_X3 42
97 #define TOP_X4 174
98 #define TOP_Y1 4
99 #define TOP_Y2 25
100 #elif (LCD_WIDTH >= 176) && (LCD_HEIGHT >= 132)
101 #define MULTIPLIER 5
102 #define MODIFIER_1 5
103 #define MODIFIER_2 3
104 #define CENTER_X 18
105 #define CENTER_Y 40
106 #define TOP_X1 34
107 #define TOP_X2 137
108 #define TOP_X3 42
109 #define TOP_X4 130
110 #define TOP_Y1 4
111 #define TOP_Y2 25
112 #elif (LCD_WIDTH >= 160) && (LCD_HEIGHT >= 128)
113 #define MULTIPLIER 5
114 #define MODIFIER_1 5
115 #define MODIFIER_2 3
116 #define CENTER_X 10
117 #define CENTER_Y 38
118 #define TOP_X1 34
119 #define TOP_X2 121
120 #define TOP_X3 42
121 #define TOP_X4 114
122 #define TOP_Y1 4
123 #define TOP_Y2 25
124 #else
125 #define MULTIPLIER 4
126 #define MODIFIER_1 4
127 #define MODIFIER_2 2
128 #define CENTER_X 0
129 #define CENTER_Y 0
131 #endif
133 /* variable button definitions */
134 #if CONFIG_KEYPAD == RECORDER_PAD
135 #define SNAKE2_LEFT BUTTON_LEFT
136 #define SNAKE2_RIGHT BUTTON_RIGHT
137 #define SNAKE2_UP BUTTON_UP
138 #define SNAKE2_DOWN BUTTON_DOWN
139 #define SNAKE2_QUIT BUTTON_OFF
140 #define SNAKE2_PLAYPAUSE BUTTON_PLAY
141 #define SNAKE2_PLAYPAUSE_TEXT "Play"
143 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
144 #define SNAKE2_LEFT BUTTON_LEFT
145 #define SNAKE2_RIGHT BUTTON_RIGHT
146 #define SNAKE2_UP BUTTON_UP
147 #define SNAKE2_DOWN BUTTON_DOWN
148 #define SNAKE2_QUIT BUTTON_OFF
149 #define SNAKE2_PLAYPAUSE BUTTON_SELECT
150 #define SNAKE2_PLAYPAUSE_TEXT "Select"
152 #elif CONFIG_KEYPAD == ONDIO_PAD
153 #define SNAKE2_LEFT BUTTON_LEFT
154 #define SNAKE2_RIGHT BUTTON_RIGHT
155 #define SNAKE2_UP BUTTON_UP
156 #define SNAKE2_DOWN BUTTON_DOWN
157 #define SNAKE2_QUIT BUTTON_OFF
158 #define SNAKE2_PLAYPAUSE BUTTON_MENU
159 #define SNAKE2_PLAYPAUSE_TEXT "Menu"
161 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
162 (CONFIG_KEYPAD == IRIVER_H300_PAD)
163 #define SNAKE2_LEFT BUTTON_LEFT
164 #define SNAKE2_RIGHT BUTTON_RIGHT
165 #define SNAKE2_UP BUTTON_UP
166 #define SNAKE2_DOWN BUTTON_DOWN
167 #define SNAKE2_QUIT BUTTON_OFF
168 #define SNAKE2_PLAYPAUSE BUTTON_ON
169 #define SNAKE2_PLAYPAUSE_TEXT "Play"
171 #define SNAKE2_RC_QUIT BUTTON_RC_STOP
173 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
174 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
175 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
176 #define SNAKE2_LEFT BUTTON_LEFT
177 #define SNAKE2_RIGHT BUTTON_RIGHT
178 #define SNAKE2_UP BUTTON_MENU
179 #define SNAKE2_DOWN BUTTON_PLAY
180 #define SNAKE2_QUIT (BUTTON_SELECT | BUTTON_MENU)
181 #define SNAKE2_PLAYPAUSE BUTTON_SELECT
182 #define SNAKE2_PLAYPAUSE_TEXT "Select"
184 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
185 #define SNAKE2_LEFT BUTTON_LEFT
186 #define SNAKE2_RIGHT BUTTON_RIGHT
187 #define SNAKE2_UP BUTTON_UP
188 #define SNAKE2_DOWN BUTTON_DOWN
189 #define SNAKE2_QUIT BUTTON_POWER
190 #define SNAKE2_PLAYPAUSE BUTTON_SELECT
191 #define SNAKE2_PLAYPAUSE_TEXT "Select"
193 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
194 #define SNAKE2_LEFT BUTTON_LEFT
195 #define SNAKE2_RIGHT BUTTON_RIGHT
196 #define SNAKE2_UP BUTTON_UP
197 #define SNAKE2_DOWN BUTTON_DOWN
198 #define SNAKE2_QUIT BUTTON_POWER
199 #define SNAKE2_PLAYPAUSE BUTTON_SELECT
200 #define SNAKE2_PLAYPAUSE_TEXT "Select"
202 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
203 (CONFIG_KEYPAD == SANSA_C200_PAD)
204 #define SNAKE2_LEFT BUTTON_LEFT
205 #define SNAKE2_RIGHT BUTTON_RIGHT
206 #define SNAKE2_UP BUTTON_UP
207 #define SNAKE2_DOWN BUTTON_DOWN
208 #define SNAKE2_QUIT BUTTON_POWER
209 #define SNAKE2_PLAYPAUSE BUTTON_SELECT
210 #define SNAKE2_PLAYPAUSE_TEXT "Select"
212 #elif (CONFIG_KEYPAD == SANSA_CLIP_PAD) || \
213 (CONFIG_KEYPAD == SANSA_M200_PAD)
214 #define SNAKE2_LEFT BUTTON_LEFT
215 #define SNAKE2_RIGHT BUTTON_RIGHT
216 #define SNAKE2_UP BUTTON_UP
217 #define SNAKE2_DOWN BUTTON_DOWN
218 #define SNAKE2_QUIT BUTTON_POWER
219 #define SNAKE2_PLAYPAUSE BUTTON_SELECT
220 #define SNAKE2_PLAYPAUSE_TEXT "Select"
222 #elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
223 #define SNAKE2_LEFT BUTTON_LEFT
224 #define SNAKE2_RIGHT BUTTON_RIGHT
225 #define SNAKE2_UP BUTTON_UP
226 #define SNAKE2_DOWN BUTTON_DOWN
227 #define SNAKE2_QUIT (BUTTON_HOME|BUTTON_REPEAT)
228 #define SNAKE2_PLAYPAUSE BUTTON_SELECT|BUTTON_REPEAT
229 #define SNAKE2_PLAYPAUSE_TEXT "Hold Select"
231 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
232 #define SNAKE2_LEFT BUTTON_LEFT
233 #define SNAKE2_RIGHT BUTTON_RIGHT
234 #define SNAKE2_UP BUTTON_SCROLL_UP
235 #define SNAKE2_DOWN BUTTON_SCROLL_DOWN
236 #define SNAKE2_QUIT BUTTON_POWER
237 #define SNAKE2_PLAYPAUSE BUTTON_FF
238 #define SNAKE2_PLAYPAUSE_TEXT "FF"
240 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
241 #define SNAKE2_LEFT BUTTON_LEFT
242 #define SNAKE2_RIGHT BUTTON_RIGHT
243 #define SNAKE2_UP BUTTON_UP
244 #define SNAKE2_DOWN BUTTON_DOWN
245 #define SNAKE2_QUIT BUTTON_BACK
246 #define SNAKE2_PLAYPAUSE BUTTON_SELECT
247 #define SNAKE2_PLAYPAUSE_TEXT "Select"
249 #elif (CONFIG_KEYPAD == MROBE100_PAD)
250 #define SNAKE2_LEFT BUTTON_LEFT
251 #define SNAKE2_RIGHT BUTTON_RIGHT
252 #define SNAKE2_UP BUTTON_UP
253 #define SNAKE2_DOWN BUTTON_DOWN
254 #define SNAKE2_QUIT BUTTON_POWER
255 #define SNAKE2_PLAYPAUSE BUTTON_SELECT
256 #define SNAKE2_PLAYPAUSE_TEXT "Select"
258 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
259 #define SNAKE2_LEFT BUTTON_RC_REW
260 #define SNAKE2_RIGHT BUTTON_RC_FF
261 #define SNAKE2_UP BUTTON_RC_VOL_UP
262 #define SNAKE2_DOWN BUTTON_RC_VOL_DOWN
263 #define SNAKE2_QUIT BUTTON_RC_REC
264 #define SNAKE2_PLAYPAUSE BUTTON_RC_PLAY
265 #define SNAKE2_PLAYPAUSE_TEXT "Play"
267 #elif (CONFIG_KEYPAD == COWOND2_PAD)
268 #define SNAKE2_QUIT BUTTON_POWER
270 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
271 #define SNAKE2_LEFT BUTTON_LEFT
272 #define SNAKE2_RIGHT BUTTON_RIGHT
273 #define SNAKE2_UP BUTTON_UP
274 #define SNAKE2_DOWN BUTTON_DOWN
275 #define SNAKE2_QUIT BUTTON_BACK
276 #define SNAKE2_PLAYPAUSE BUTTON_PLAY
277 #define SNAKE2_PLAYPAUSE_TEXT "Play"
279 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
280 #define SNAKE2_LEFT BUTTON_LEFT
281 #define SNAKE2_RIGHT BUTTON_RIGHT
282 #define SNAKE2_UP BUTTON_UP
283 #define SNAKE2_DOWN BUTTON_DOWN
284 #define SNAKE2_QUIT BUTTON_POWER
285 #define SNAKE2_PLAYPAUSE BUTTON_VIEW
286 #define SNAKE2_PLAYPAUSE_TEXT "View"
288 #elif (CONFIG_KEYPAD == ONDAVX747_PAD) || \
289 (CONFIG_KEYPAD == ONDAVX777_PAD) || \
290 CONFIG_KEYPAD == MROBE500_PAD
291 #define SNAKE2_QUIT BUTTON_POWER
293 #elif (CONFIG_KEYPAD == SAMSUNG_YH_PAD)
294 #define SNAKE2_LEFT BUTTON_LEFT
295 #define SNAKE2_RIGHT BUTTON_RIGHT
296 #define SNAKE2_UP BUTTON_UP
297 #define SNAKE2_DOWN BUTTON_DOWN
298 #define SNAKE2_QUIT BUTTON_REC
299 #define SNAKE2_PLAYPAUSE BUTTON_PLAY
300 #define SNAKE2_PLAYPAUSE_TEXT "Play"
302 #else
303 #error No keymap defined!
304 #endif
306 #ifdef HAVE_TOUCHSCREEN
307 #ifndef SNAKE2_LEFT
308 #define SNAKE2_LEFT BUTTON_MIDLEFT
309 #endif
310 #ifndef SNAKE2_RIGHT
311 #define SNAKE2_RIGHT BUTTON_MIDRIGHT
312 #endif
313 #ifndef SNAKE2_UP
314 #define SNAKE2_UP BUTTON_TOPMIDDLE
315 #endif
316 #ifndef SNAKE2_DOWN
317 #define SNAKE2_DOWN BUTTON_BOTTOMMIDDLE
318 #endif
319 #ifndef SNAKE2_QUIT
320 #define SNAKE2_QUIT BUTTON_TOPLEFT
321 #endif
322 #ifndef SNAKE2_PLAYPAUSE
323 #define SNAKE2_PLAYPAUSE BUTTON_CENTER
324 #endif
325 #ifndef SNAKE2_PLAYPAUSE_TEXT
326 #define SNAKE2_PLAYPAUSE_TEXT "CENTER"
327 #endif
328 #endif
330 static int max_levels = 0;
331 static char (*level_cache)[HEIGHT][WIDTH];
333 /*Board itself - 2D int array*/
334 static int board[WIDTH][HEIGHT];
336 Buffer for sorting movement (in case user presses two movements during a
337 single frame
339 static int ardirectionbuffer[2];
340 static int score;
341 static int applex;
342 static int appley;
343 static int dir;
344 static int frames;
345 static int apple;
346 static int level = 4, speed = 5,dead = 0, quit = 0;
347 static int sillydir = 0, num_levels = 0;
348 static int level_from_file = 0;
349 static int headx, heady, tailx, taily, applecountdown = 5;
350 static int game_type = 0;
351 static int num_apples_to_get=1;
352 static int num_apples_to_got=0;
353 static int game_b_level=0;
354 static int applecount=0;
355 /* used for string width, height for orientation purposes */
356 static int strwdt, strhgt;
357 static char strbuf[32];
359 #define NUM_SCORES 5
360 static struct highscore highscores[NUM_SCORES];
362 #define NORTH 1
363 #define EAST 2
364 #define SOUTH 4
365 #define WEST 8
366 #define HEAD 16
368 #define EAST_NORTH 32
369 #define EAST_SOUTH 64
370 #define WEST_NORTH 128
371 #define WEST_SOUTH 256
373 #define NORTH_EAST 512
374 #define NORTH_WEST 1024
375 #define SOUTH_EAST 2048
376 #define SOUTH_WEST 4096
378 #define LEVELS_FILE PLUGIN_GAMES_DIR "/snake2.levels"
379 #define SCORE_FILE PLUGIN_GAMES_DIR "/snake2.score"
381 int load_all_levels(void)
383 int linecnt = 0;
384 int fd;
385 ssize_t size;
386 char buf[64]; /* Larger than WIDTH, to allow for whitespace after the
387 lines */
389 /* Init the level_cache pointer and
390 calculate how many levels that will fit */
391 level_cache = rb->plugin_get_buffer((size_t *)&size);
392 max_levels = size / (HEIGHT*WIDTH);
394 num_levels = 0;
396 /* open file */
397 if ((fd = rb->open(LEVELS_FILE, O_RDONLY)) < 0)
399 return -1;
402 while(rb->read_line(fd, buf, 64) > 0)
404 if(rb->strlen(buf) == 0) /* Separator? */
406 num_levels++;
407 if(num_levels > max_levels)
409 rb->splash(HZ, "Too many levels in file");
410 break;
412 continue;
415 rb->memcpy(level_cache[num_levels][linecnt], buf, WIDTH);
416 linecnt++;
417 if(linecnt == HEIGHT)
419 linecnt = 0;
423 rb->close(fd);
424 return 0;
428 ** Completely clear the board of walls and/or snake
431 void clear_board( void)
433 int x,y;
435 for (x = 0; x < WIDTH; x++)
437 for (y = 0; y < HEIGHT; y++)
439 board[x][y] = 0;
444 int load_level( int level_number )
446 int x,y;
447 clear_board();
448 for(y = 0;y < HEIGHT;y++)
450 for(x = 0;x < WIDTH;x++)
452 switch(level_cache[level_number][y][x])
454 case '|':
455 board[x][y] = NORTH;
456 break;
458 case '-':
459 board[x][y] = EAST;
460 break;
462 case '+':
463 board[x][y] = HEAD;
464 break;
468 return 1;
472 ** Gets the currently chosen direction from the first place
473 ** in the direction buffer. If there is something in the
474 ** next part of the buffer then that is moved to the first place
476 void get_direction( void )
478 /*if 1st place is empty*/
479 if(ardirectionbuffer[0] != -1)
481 /*return this direction*/
482 dir = ardirectionbuffer[0];
483 ardirectionbuffer[0]=-1;
484 /*now see if one needs moving:*/
485 if(ardirectionbuffer[1] != -1)
487 /*there's a move waiting to be done
488 so move it into the space:*/
489 ardirectionbuffer[0] = ardirectionbuffer[1];
490 ardirectionbuffer[1] = -1;
496 ** Sets the direction
498 void set_direction(int newdir)
500 if(ardirectionbuffer[0] != newdir)
502 /*if 1st place is empty*/
503 if(ardirectionbuffer[0] == -1)
505 /*use 1st space:*/
506 ardirectionbuffer[0] = newdir;
508 else
510 /*use 2nd space:*/
511 if(ardirectionbuffer[0] != newdir) ardirectionbuffer[1] = newdir;
514 if(frames < 0) ardirectionbuffer[0] = newdir;
518 void new_level(int level)
520 load_level(level);
522 ardirectionbuffer[0] = -1;
523 ardirectionbuffer[1] = -1;
524 dir = EAST;
525 headx = WIDTH/2;
526 heady = HEIGHT/2;
527 tailx = headx - 4;
528 taily = heady;
529 applecountdown = 0;
530 /*Create a small snake to start off with*/
531 board[headx][heady] = dir;
532 board[headx-1][heady] = dir;
533 board[headx-2][heady] = dir;
534 board[headx-3][heady] = dir;
535 board[headx-4][heady] = dir;
536 num_apples_to_got=0;
539 void init_snake(void)
541 num_apples_to_get=1;
542 if(game_type == 1)
543 level_from_file = 1;
544 game_b_level=1;
545 new_level(level_from_file);
548 #if (LCD_WIDTH >= 160) && (LCD_HEIGHT >= 128)
549 void draw_frame_bitmap(int header_type)
551 rb->lcd_bitmap(header_type==1? snake2_header1: snake2_header2, 0, 0,
552 BMPWIDTH_snake2_header, BMPHEIGHT_snake2_header);
553 rb->lcd_bitmap(snake2_left, 0, BMPHEIGHT_snake2_header,
554 BMPWIDTH_snake2_left, BMPHEIGHT_snake2_left);
555 rb->lcd_bitmap(snake2_right,
556 LCD_WIDTH - BMPWIDTH_snake2_right, BMPHEIGHT_snake2_header,
557 BMPWIDTH_snake2_right, BMPHEIGHT_snake2_right);
558 rb->lcd_bitmap(snake2_bottom,
559 0, BMPHEIGHT_snake2_header + BMPHEIGHT_snake2_left,
560 BMPWIDTH_snake2_bottom, BMPHEIGHT_snake2_bottom);
562 #endif
565 ** Draws the apple. If it doesn't exist then
566 ** a new one get's created.
568 void draw_apple_bit(int x, int y)
570 rb->lcd_fillrect((CENTER_X+x*MULTIPLIER)+1, CENTER_Y+y*MULTIPLIER,
571 MODIFIER_2, MODIFIER_1);
572 rb->lcd_fillrect(CENTER_X+x*MULTIPLIER, (CENTER_Y+y*MULTIPLIER)+1,
573 MODIFIER_1, MODIFIER_2);
576 void draw_apple( void )
578 int x,y;
580 #if LCD_WIDTH >= 160 && LCD_HEIGHT >= 128
581 draw_frame_bitmap(2);
583 rb->snprintf(strbuf, sizeof(strbuf), "%d", applecount);
584 rb->lcd_getstringsize(strbuf, &strwdt, &strhgt);
585 rb->lcd_putsxy(TOP_X3 - strwdt/2, TOP_Y2, strbuf);
587 rb->snprintf(strbuf, sizeof(strbuf), "%d", score);
588 rb->lcd_getstringsize(strbuf, &strwdt, &strhgt);
589 rb->lcd_putsxy(TOP_X4 - strwdt/2, TOP_Y2, strbuf);
590 #endif
592 if (!apple)
596 x = (rb->rand() % (WIDTH-1))+1;
597 y = (rb->rand() % (HEIGHT-1))+1;
598 } while (board[x][y]);
599 apple = 1;
600 board[x][y] = -1;
601 applex = x;appley = y;
603 draw_apple_bit(applex, appley);
607 * x x *
608 * x x *
609 * x x *
610 * x x *
612 void draw_vertical_bit(int x, int y)
614 rb->lcd_fillrect(CENTER_X+x*MULTIPLIER+1, CENTER_Y+y*MULTIPLIER,
615 MODIFIER_2, MODIFIER_1);
619 * * * *
620 X X X X
621 X X X X
622 * * * *
624 void draw_horizontal_bit(int x, int y)
626 rb->lcd_fillrect(CENTER_X+x*MULTIPLIER, CENTER_Y+y*MULTIPLIER+1,
627 MODIFIER_1, MODIFIER_2);
631 * * * *
632 * * X X
633 * X X X
634 * X X *
636 void draw_n_to_e_bit(int x, int y)
638 rb->lcd_fillrect(CENTER_X+x*MULTIPLIER+1, CENTER_Y+y*MULTIPLIER+2,
639 MODIFIER_2, MODIFIER_2);
640 rb->lcd_fillrect(CENTER_X+x*MULTIPLIER+2, CENTER_Y+y*MULTIPLIER+1,
641 MODIFIER_2, MODIFIER_2);
645 * * * *
646 * * X X
647 * X X X
648 * X X *
650 void draw_w_to_s_bit(int x, int y)
652 draw_n_to_e_bit(x,y);
656 * * * *
657 X X * *
658 X X X *
659 * X X *
661 void draw_n_to_w_bit(int x, int y)
663 rb->lcd_fillrect(CENTER_X+x*MULTIPLIER, CENTER_Y+y*MULTIPLIER+1,
664 MODIFIER_2, MODIFIER_2);
665 rb->lcd_fillrect(CENTER_X+x*MULTIPLIER+1, CENTER_Y+y*MULTIPLIER+2,
666 MODIFIER_2, MODIFIER_2);
670 * * * *
671 X X * *
672 X X X *
673 * X X *
675 void draw_e_to_s_bit(int x, int y)
677 draw_n_to_w_bit(x, y);
681 * X X *
682 * X X X
683 * * X X
684 * * * *
686 void draw_s_to_e_bit(int x, int y)
688 rb->lcd_fillrect(CENTER_X+x*MULTIPLIER+1, CENTER_Y+y*MULTIPLIER,
689 MODIFIER_2, MODIFIER_2);
690 rb->lcd_fillrect(CENTER_X+x*MULTIPLIER+2, CENTER_Y+y*MULTIPLIER+1,
691 MODIFIER_2, MODIFIER_2);
695 * X X *
696 * X X X
697 * * X X
698 * * * *
700 void draw_w_to_n_bit(int x, int y)
702 draw_s_to_e_bit(x,y);
706 * X X *
707 X X X *
708 X X * *
709 * * * *
711 void draw_e_to_n_bit(int x, int y)
713 rb->lcd_fillrect(CENTER_X+x*MULTIPLIER+1, CENTER_Y+y*MULTIPLIER,
714 MODIFIER_2, MODIFIER_2);
715 rb->lcd_fillrect(CENTER_X+x*MULTIPLIER, CENTER_Y+y*MULTIPLIER+1,
716 MODIFIER_2, MODIFIER_2);
720 * X X *
721 X X X *
722 X X * *
723 * * * *
725 void draw_s_to_w_bit(int x, int y)
727 draw_e_to_n_bit(x, y);
730 void draw_head_bit(int x, int y)
732 rb->lcd_fillrect(CENTER_X+x*MULTIPLIER, CENTER_Y+y*MULTIPLIER,
733 MODIFIER_1, MODIFIER_1);
737 ** Draws a wall/obsticals
739 void draw_boundary ( void )
741 int x, y;
743 /*TODO: Load levels from file!*/
745 /*top and bottom line*/
746 for(x=0; x < WIDTH; x++)
748 board[x][0] = EAST;
749 board[x][HEIGHT-1] = WEST;
752 /*left and right lines*/
753 for(y=0; y < HEIGHT; y++)
755 board[0][y] = NORTH;
756 board[WIDTH-1][y] = SOUTH;
759 /*corners:*/
760 board[0][0] = NORTH_EAST;
761 board[WIDTH-1][0] = EAST_SOUTH;
762 board[0][HEIGHT-1] = SOUTH_EAST;
763 board[WIDTH-1][HEIGHT-1] = EAST_NORTH;
767 ** Redraw the entire board
769 void redraw (void)
771 int x,y;
773 #ifdef HAVE_LCD_COLOR
774 rb->lcd_set_foreground(LCD_BLACK);
775 rb->lcd_set_background(LCD_WHITE);
776 #endif
778 rb->lcd_clear_display();
780 for (x = 0; x < WIDTH; x++)
782 for (y = 0; y < HEIGHT; y++)
784 switch (board[x][y])
786 case -1:
787 draw_apple_bit(x, y);
788 break;
789 case 0:
790 break;
792 case NORTH:
793 case SOUTH:
794 draw_vertical_bit(x,y);
795 break;
797 case EAST:
798 case WEST:
799 draw_horizontal_bit(x,y);
800 break;
802 default:
803 draw_head_bit(x, y);
804 break;
809 #if LCD_WIDTH >= 160 && LCD_HEIGHT >= 128
810 draw_frame_bitmap(2);
812 rb->snprintf(strbuf, sizeof(strbuf), "%d", applecount);
813 rb->lcd_getstringsize(strbuf, &strwdt, &strhgt);
814 rb->lcd_putsxy(TOP_X3 - strwdt/2, TOP_Y2, strbuf);
816 rb->snprintf(strbuf, sizeof(strbuf), "%d", score);
817 rb->lcd_getstringsize(strbuf, &strwdt, &strhgt);
818 rb->lcd_putsxy(TOP_X4 - strwdt/2, TOP_Y2, strbuf);
819 #endif
823 ** Draws the snake bit described by nCurrentBit at position x/y
824 ** deciding whether it's a corner bit by examing the nPrevious bit
826 void draw_snake_bit(int currentbit, int previousbit, int x, int y)
828 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
829 draw_head_bit(x, y);
830 rb->lcd_set_drawmode(DRMODE_SOLID);
832 switch(currentbit)
834 case(NORTH):
835 switch(previousbit)
837 case(SOUTH):
838 case(NORTH):
839 draw_vertical_bit(x,y);
840 break;
842 case(EAST):
843 draw_e_to_n_bit(x,y);
844 break;
846 case(WEST):
847 draw_w_to_n_bit(x,y);
848 break;
850 break;
852 case(EAST):
853 switch(previousbit)
855 case(WEST):
856 case(EAST):
857 draw_horizontal_bit(x,y);
858 break;
860 case(NORTH):
861 draw_n_to_e_bit(x,y);
862 break;
864 case(SOUTH):
865 draw_s_to_e_bit(x,y);
866 break;
868 break;
870 case(SOUTH):
871 switch(previousbit)
873 case(SOUTH):
874 case(NORTH):
875 draw_vertical_bit(x,y);
876 break;
878 case(EAST):
879 draw_e_to_s_bit(x,y);
880 break;
882 case(WEST):
883 draw_w_to_s_bit(x,y);
884 break;
886 break;
888 case(WEST):
889 switch(previousbit)
891 case(EAST):
892 case(WEST):
893 draw_horizontal_bit(x,y);
894 break;
896 case(SOUTH):
897 draw_s_to_w_bit(x,y);
898 break;
900 case(NORTH):
901 draw_n_to_w_bit(x,y);
902 break;
904 break;
908 void redraw_snake(void)
910 int x = tailx, y = taily;
911 int olddir, newdir = board[x][y];
913 while (x != headx || y != heady)
915 olddir = newdir;
917 switch (olddir)
919 case(NORTH):
920 y--;
921 break;
923 case(EAST):
924 x++;
925 break;
927 case(SOUTH):
928 y++;
929 break;
931 case(WEST):
932 x--;
933 break;
936 if(x == WIDTH)
937 x = 0;
938 else if(x < 0)
939 x = WIDTH-1;
941 if(y == HEIGHT)
942 y = 0;
943 else if(y < 0)
944 y = HEIGHT-1;
946 newdir = board[x][y];
947 if(olddir != newdir)
948 draw_snake_bit(newdir, olddir, x, y);
953 ** Death 'sequence' and end game stuff.
955 void die (void)
957 int button;
958 bool done=false;
960 rb->splash(HZ*2, "Oops!");
962 rb->lcd_clear_display();
964 applecount=0;
966 rb->lcd_getstringsize("You died!",&strwdt,&strhgt);
967 rb->lcd_putsxy((LCD_WIDTH - strwdt)/2,strhgt,"You died!");
969 rb->snprintf(strbuf, sizeof(strbuf), "Your score: %d", score);
970 rb->lcd_getstringsize(strbuf, &strwdt, &strhgt);
971 rb->lcd_putsxy((LCD_WIDTH - strwdt)/2, strhgt * 2 + 2, strbuf);
973 if (highscore_update(score, level_from_file, game_type==0?"Type A":"Type B",
974 highscores, NUM_SCORES) == 0)
976 rb->lcd_getstringsize("New high score!",&strwdt,&strhgt);
977 rb->lcd_putsxy((LCD_WIDTH - strwdt)/2,strhgt * 4 + 2,"New high score!");
979 else
981 rb->snprintf(strbuf, sizeof(strbuf), "High score: %d", highscores[0].score);
982 rb->lcd_getstringsize(strbuf, &strwdt, &strhgt);
983 rb->lcd_putsxy((LCD_WIDTH - strwdt)/2, strhgt * 5, strbuf);
986 rb->snprintf(strbuf, sizeof(strbuf), "Press %s...", SNAKE2_PLAYPAUSE_TEXT);
987 rb->lcd_getstringsize(strbuf, &strwdt, &strhgt);
988 rb->lcd_putsxy((LCD_WIDTH - strwdt)/2, strhgt * 7, strbuf);
990 rb->lcd_update();
992 while(!done)
994 button=rb->button_get(true);
995 switch(button)
997 case SNAKE2_PLAYPAUSE:
998 done = true;
999 break;
1003 dead=1;
1007 ** Check for collision. TODO: Currently this
1008 ** sets of the death sequence. What we want is it to only return a true/false
1009 ** depending on whether a collision occured.
1011 void collision ( int x, int y )
1013 int bdeath=0;
1016 switch (board[x][y])
1018 case 0:
1020 break;
1021 case -1:
1022 score = score + (1 * level);
1023 apple=0;
1024 applecountdown=2;
1025 applecount++;
1027 if(game_type==1)
1029 if(num_apples_to_get == num_apples_to_got)
1031 level_from_file++;
1032 if(level_from_file >= num_levels)
1034 level_from_file = 1;
1035 /*and increase the number of apples to pick up
1036 before level changes*/
1037 num_apples_to_get+=2;
1038 game_b_level++;
1040 rb->splash(HZ, "Level Completed!");
1041 new_level(level_from_file);
1042 redraw();
1043 rb->lcd_update();
1045 else
1046 num_apples_to_got++;
1048 break;
1049 default:
1050 bdeath=1;
1051 break;
1054 if(bdeath==1)
1056 die();
1057 sillydir = dir;
1058 frames = -110;
1062 void move( void )
1064 int taildir;
1065 /*this actually sets the dir variable.*/
1066 get_direction();
1067 /*draw head*/
1068 switch (dir)
1070 case (NORTH):
1071 board[headx][heady]=NORTH;
1072 heady--;
1073 break;
1074 case (EAST):
1075 board[headx][heady]=EAST;
1076 headx++;
1077 break;
1078 case (SOUTH):
1079 board[headx][heady]=SOUTH;
1080 heady++;
1081 break;
1082 case (WEST):
1083 board[headx][heady]=WEST;
1084 headx--;
1085 break;
1088 if(headx == WIDTH)
1089 headx = 0;
1090 else if(headx < 0)
1091 headx = WIDTH-1;
1093 if(heady == HEIGHT)
1094 heady = 0;
1095 else if(heady < 0)
1096 heady = HEIGHT-1;
1098 draw_head_bit(headx, heady);
1100 /*clear tail*/
1101 if(applecountdown <= 0)
1103 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
1104 draw_head_bit(tailx, taily);
1105 rb->lcd_set_drawmode(DRMODE_SOLID);
1107 taildir = board[tailx][taily];
1108 board[tailx][taily] = 0;
1110 switch (taildir)
1112 case(NORTH):
1113 taily--;
1114 break;
1116 case(EAST):
1117 tailx++;
1118 break;
1120 case(SOUTH):
1121 taily++;
1122 break;
1124 case(WEST):
1125 tailx--;
1126 break;
1129 if(tailx == WIDTH)
1130 tailx = 0;
1131 else if(tailx < 0)
1132 tailx = WIDTH-1;
1134 if(taily == HEIGHT)
1135 taily = 0;
1136 else if(taily < 0)
1137 taily = HEIGHT-1;
1139 else
1140 applecountdown--;
1143 void frame (void)
1145 int olddir, noldx, noldy, temp;
1146 noldx = headx;
1147 noldy = heady;
1148 olddir = 0;
1149 switch(dir)
1151 case(NORTH):
1152 if(heady == HEIGHT-1)
1153 temp = 0;
1154 else
1155 temp = heady + 1;
1157 olddir = board[headx][temp];
1158 break;
1160 case(EAST):
1161 if(headx == 0)
1162 temp = WIDTH-1;
1163 else
1164 temp = headx - 1;
1166 olddir = board[temp][heady];
1167 break;
1169 case(SOUTH):
1170 if(heady == 0)
1171 temp = HEIGHT-1;
1172 else
1173 temp = heady - 1;
1175 olddir = board[headx][temp];
1176 break;
1178 case(WEST):
1179 if(headx == WIDTH-1)
1180 temp = 0;
1181 else
1182 temp = headx + 1;
1184 olddir = board[temp][heady];
1185 break;
1188 move();
1191 now redraw the bit that was
1192 the tail, to something snake-like:
1194 draw_snake_bit(dir, olddir, noldx, noldy);
1196 collision(headx, heady);
1198 rb->lcd_update();
1201 void game_pause (void)
1203 int button;
1205 rb->lcd_clear_display();
1206 rb->lcd_getstringsize("Paused",&strwdt,&strhgt);
1207 rb->lcd_putsxy((LCD_WIDTH - strwdt)/2,LCD_HEIGHT/2,"Paused");
1209 rb->lcd_update();
1210 while (1)
1212 button = rb->button_get(true);
1213 switch (button)
1215 case SNAKE2_PLAYPAUSE:
1216 redraw();
1217 redraw_snake();
1218 draw_head_bit(headx, heady);
1219 rb->lcd_update();
1220 rb->sleep(HZ/2);
1221 return;
1223 #ifdef SNAKE2_RC_QUIT
1224 case SNAKE2_RC_QUIT:
1225 #endif
1226 case SNAKE2_QUIT:
1227 dead = 1;
1228 quit = 1;
1229 return;
1231 default:
1232 if (rb->default_event_handler(button)==SYS_USB_CONNECTED) {
1233 dead = 1;
1234 quit = 2;
1235 return;
1237 break;
1242 void game (void)
1244 int button;
1246 redraw();
1247 rb->lcd_update();
1248 /*main loop:*/
1249 while (1)
1251 if(frames==5)
1253 frame();
1254 if(frames > 0) frames = 0;
1256 frames++;
1258 if(frames == 0)
1260 die();
1262 else
1264 if(frames < 0)
1266 if(sillydir != dir)
1268 /*it has, great set frames to a positive value again:*/
1269 frames = 1;
1274 if (dead) return;
1276 draw_apple();
1278 rb->sleep(HZ/speed);
1280 button = rb->button_get(false);
1282 #ifdef HAS_BUTTON_HOLD
1283 if (rb->button_hold())
1284 button = SNAKE2_PLAYPAUSE;
1285 #endif
1287 switch (button)
1289 case SNAKE2_UP:
1290 case SNAKE2_UP | BUTTON_REPEAT:
1291 if (dir != SOUTH) set_direction(NORTH);
1292 break;
1294 case SNAKE2_RIGHT:
1295 case SNAKE2_RIGHT | BUTTON_REPEAT:
1296 if (dir != WEST) set_direction(EAST);
1297 break;
1299 case SNAKE2_DOWN:
1300 case SNAKE2_DOWN | BUTTON_REPEAT:
1301 if (dir != NORTH) set_direction(SOUTH);
1302 break;
1304 case SNAKE2_LEFT:
1305 case SNAKE2_LEFT | BUTTON_REPEAT:
1306 if (dir != EAST) set_direction(WEST);
1307 break;
1309 #ifdef SNAKE2_RC_QUIT
1310 case SNAKE2_RC_QUIT:
1311 #endif
1312 case SNAKE2_QUIT:
1313 quit = 1;
1314 return;
1316 case SNAKE2_PLAYPAUSE:
1317 game_pause();
1318 break;
1320 default:
1321 if (rb->default_event_handler(button)==SYS_USB_CONNECTED) {
1322 quit = 2;
1323 return;
1325 break;
1331 void select_maze(void)
1333 int button;
1335 clear_board();
1336 load_level( level_from_file );
1337 redraw();
1338 rb->lcd_update();
1340 while (1)
1342 #if LCD_WIDTH >= 160 && LCD_HEIGHT >= 128
1343 draw_frame_bitmap(1);
1345 rb->snprintf(strbuf, sizeof(strbuf), "%d", level);
1346 rb->lcd_getstringsize(strbuf, &strwdt, &strhgt);
1347 rb->lcd_putsxy(TOP_X3 - strwdt/2, TOP_Y2, strbuf);
1349 rb->snprintf(strbuf, sizeof(strbuf), "%d", level_from_file);
1350 rb->lcd_getstringsize(strbuf, &strwdt, &strhgt);
1351 rb->lcd_putsxy(TOP_X2 - strwdt/2, TOP_Y1, strbuf);
1353 rb->strcpy(strbuf, game_type==0? "A": "B");
1354 rb->lcd_getstringsize(strbuf, &strwdt, &strhgt);
1355 rb->lcd_putsxy(TOP_X1, TOP_Y1, strbuf);
1357 rb->snprintf(strbuf, sizeof(strbuf), "%d", highscores[0].score);
1358 rb->lcd_getstringsize(strbuf, &strwdt, &strhgt);
1359 rb->lcd_putsxy(TOP_X4 - strwdt/2, TOP_Y2, strbuf);
1361 #else
1362 rb->snprintf(strbuf, sizeof(strbuf), "Maze: %d", level_from_file);
1363 rb->lcd_getstringsize(strbuf, &strwdt, &strhgt);
1364 rb->lcd_putsxy((WIDTH*MULTIPLIER - strwdt)/2,
1365 (HEIGHT*MULTIPLIER - strhgt)/2, strbuf);
1366 #endif
1368 rb->lcd_update();
1370 button = rb->button_get(true);
1371 switch (button)
1373 case SNAKE2_QUIT:
1374 case SNAKE2_PLAYPAUSE:
1375 return;
1376 break;
1377 case SNAKE2_UP:
1378 case SNAKE2_RIGHT:
1379 if(level_from_file < num_levels)
1380 level_from_file++;
1381 else
1382 level_from_file = 0;
1383 load_level( level_from_file );
1384 redraw();
1385 break;
1386 case SNAKE2_DOWN:
1387 case SNAKE2_LEFT:
1388 if(level_from_file > 0)
1389 level_from_file--;
1390 else
1391 level_from_file = num_levels;
1392 load_level( level_from_file );
1393 redraw();
1394 break;
1395 default:
1396 if (rb->default_event_handler(button)==SYS_USB_CONNECTED) {
1397 quit = 2;
1398 return;
1400 break;
1406 void game_init(void)
1408 int selection = 0;
1410 static const struct opt_items type_options[] = {
1411 { "Type A", -1 },
1412 { "Type B", -1 },
1415 MENUITEM_STRINGLIST(menu, "Snake2 Menu", NULL,
1416 "Start New Game",
1417 "Game Type", "Select Maze", "Speed",
1418 "High Scores",
1419 "Playback Control", "Quit");
1421 rb->button_clear_queue();
1423 dead = 0;
1424 apple = 0;
1425 score = 0;
1426 applecount = 0;
1428 while (1) {
1429 switch (rb->do_menu(&menu, &selection, NULL, false)) {
1430 case 0:
1431 speed = level*20;
1432 return;
1433 case 1:
1434 rb->set_option("Game Type", &game_type, INT,
1435 type_options, 2, NULL);
1436 break;
1437 case 2:
1438 select_maze();
1439 if(quit) return;
1440 break;
1441 case 3:
1442 rb->set_int("Speed", "", UNIT_INT, &level,
1443 NULL, 1, 1, 10, NULL);
1444 break;
1445 case 4:
1446 highscore_show(NUM_SCORES, highscores, NUM_SCORES, true);
1447 break;
1448 case 5:
1449 playback_control(NULL);
1450 break;
1451 case 6:
1452 quit = 1;
1453 return;
1454 case MENU_ATTACHED_USB:
1455 quit = 2;
1456 return;
1457 default:
1458 break;
1463 enum plugin_status plugin_start(const void* parameter)
1465 (void)(parameter);
1467 /* Lets use the default font */
1468 rb->lcd_setfont(FONT_SYSFIXED);
1469 #if LCD_DEPTH > 1
1470 rb->lcd_set_backdrop(NULL);
1471 #endif
1473 load_all_levels();
1475 if (num_levels == 0) {
1476 rb->splash(HZ*2, "Failed loading levels!");
1477 return PLUGIN_OK;
1480 highscore_load(SCORE_FILE, highscores, NUM_SCORES);
1482 while(quit==0)
1484 game_init();
1485 if(quit)
1486 break;
1488 rb->lcd_clear_display();
1489 frames=1;
1491 init_snake();
1493 /*Start Game:*/
1494 game();
1497 highscore_save(SCORE_FILE, highscores, NUM_SCORES);
1499 return (quit==1) ? PLUGIN_OK : PLUGIN_USB_CONNECTED;
1502 #endif