Bump plugin API version. This should have been done in r24587. Also, because the...
[kugel-rb.git] / apps / plugins / snake2.c
bloba5b9f00d8e1cf62428810c105edbbdedc1d65730
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
229 #define SNAKE2_PLAYPAUSE_TEXT "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 == COWON_D2_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 == PHILIPS_SA9200_PAD
289 #define SNAKE2_LEFT BUTTON_PREV
290 #define SNAKE2_RIGHT BUTTON_NEXT
291 #define SNAKE2_UP BUTTON_UP
292 #define SNAKE2_DOWN BUTTON_DOWN
293 #define SNAKE2_QUIT BUTTON_POWER
294 #define SNAKE2_PLAYPAUSE BUTTON_RIGHT
295 #define SNAKE2_PLAYPAUSE_TEXT "Right"
297 #elif (CONFIG_KEYPAD == ONDAVX747_PAD) || \
298 (CONFIG_KEYPAD == ONDAVX777_PAD) || \
299 CONFIG_KEYPAD == MROBE500_PAD
300 #define SNAKE2_QUIT BUTTON_POWER
302 #elif (CONFIG_KEYPAD == SAMSUNG_YH_PAD)
303 #define SNAKE2_LEFT BUTTON_LEFT
304 #define SNAKE2_RIGHT BUTTON_RIGHT
305 #define SNAKE2_UP BUTTON_UP
306 #define SNAKE2_DOWN BUTTON_DOWN
307 #define SNAKE2_QUIT BUTTON_REC
308 #define SNAKE2_PLAYPAUSE BUTTON_PLAY
309 #define SNAKE2_PLAYPAUSE_TEXT "Play"
311 #else
312 #error No keymap defined!
313 #endif
315 #ifdef HAVE_TOUCHSCREEN
316 #ifndef SNAKE2_LEFT
317 #define SNAKE2_LEFT BUTTON_MIDLEFT
318 #endif
319 #ifndef SNAKE2_RIGHT
320 #define SNAKE2_RIGHT BUTTON_MIDRIGHT
321 #endif
322 #ifndef SNAKE2_UP
323 #define SNAKE2_UP BUTTON_TOPMIDDLE
324 #endif
325 #ifndef SNAKE2_DOWN
326 #define SNAKE2_DOWN BUTTON_BOTTOMMIDDLE
327 #endif
328 #ifndef SNAKE2_QUIT
329 #define SNAKE2_QUIT BUTTON_TOPLEFT
330 #endif
331 #ifndef SNAKE2_PLAYPAUSE
332 #define SNAKE2_PLAYPAUSE BUTTON_CENTER
333 #endif
334 #ifndef SNAKE2_PLAYPAUSE_TEXT
335 #define SNAKE2_PLAYPAUSE_TEXT "CENTER"
336 #endif
337 #endif
339 static int max_levels = 0;
340 static char (*level_cache)[HEIGHT][WIDTH];
342 /*Board itself - 2D int array*/
343 static int board[WIDTH][HEIGHT];
345 Buffer for sorting movement (in case user presses two movements during a
346 single frame
348 static int ardirectionbuffer[2];
349 static int score;
350 static int applex;
351 static int appley;
352 static int dir;
353 static int frames;
354 static int apple;
355 static int level = 4, speed = 5,dead = 0, quit = 0;
356 static int sillydir = 0, num_levels = 0;
357 static int level_from_file = 0;
358 static int headx, heady, tailx, taily, applecountdown = 5;
359 static int game_type = 0;
360 static int num_apples_to_get=1;
361 static int num_apples_to_got=0;
362 static int game_b_level=0;
363 static int applecount=0;
364 /* used for string width, height for orientation purposes */
365 static int strwdt, strhgt;
366 static char strbuf[32];
368 #define NUM_SCORES 5
369 static struct highscore highscores[NUM_SCORES];
371 #define NORTH 1
372 #define EAST 2
373 #define SOUTH 4
374 #define WEST 8
375 #define HEAD 16
377 #define EAST_NORTH 32
378 #define EAST_SOUTH 64
379 #define WEST_NORTH 128
380 #define WEST_SOUTH 256
382 #define NORTH_EAST 512
383 #define NORTH_WEST 1024
384 #define SOUTH_EAST 2048
385 #define SOUTH_WEST 4096
387 #define LEVELS_FILE PLUGIN_GAMES_DIR "/snake2.levels"
388 #define SCORE_FILE PLUGIN_GAMES_DIR "/snake2.score"
390 int load_all_levels(void)
392 int linecnt = 0;
393 int fd;
394 ssize_t size;
395 char buf[64]; /* Larger than WIDTH, to allow for whitespace after the
396 lines */
398 /* Init the level_cache pointer and
399 calculate how many levels that will fit */
400 level_cache = rb->plugin_get_buffer((size_t *)&size);
401 max_levels = size / (HEIGHT*WIDTH);
403 num_levels = 0;
405 /* open file */
406 if ((fd = rb->open(LEVELS_FILE, O_RDONLY)) < 0)
408 return -1;
411 while(rb->read_line(fd, buf, 64) > 0)
413 if(rb->strlen(buf) == 0) /* Separator? */
415 num_levels++;
416 if(num_levels > max_levels)
418 rb->splash(HZ, "Too many levels in file");
419 break;
421 continue;
424 rb->memcpy(level_cache[num_levels][linecnt], buf, WIDTH);
425 linecnt++;
426 if(linecnt == HEIGHT)
428 linecnt = 0;
432 rb->close(fd);
433 return 0;
437 ** Completely clear the board of walls and/or snake
440 void clear_board( void)
442 int x,y;
444 for (x = 0; x < WIDTH; x++)
446 for (y = 0; y < HEIGHT; y++)
448 board[x][y] = 0;
453 int load_level( int level_number )
455 int x,y;
456 clear_board();
457 for(y = 0;y < HEIGHT;y++)
459 for(x = 0;x < WIDTH;x++)
461 switch(level_cache[level_number][y][x])
463 case '|':
464 board[x][y] = NORTH;
465 break;
467 case '-':
468 board[x][y] = EAST;
469 break;
471 case '+':
472 board[x][y] = HEAD;
473 break;
477 return 1;
481 ** Gets the currently chosen direction from the first place
482 ** in the direction buffer. If there is something in the
483 ** next part of the buffer then that is moved to the first place
485 void get_direction( void )
487 /*if 1st place is empty*/
488 if(ardirectionbuffer[0] != -1)
490 /*return this direction*/
491 dir = ardirectionbuffer[0];
492 ardirectionbuffer[0]=-1;
493 /*now see if one needs moving:*/
494 if(ardirectionbuffer[1] != -1)
496 /*there's a move waiting to be done
497 so move it into the space:*/
498 ardirectionbuffer[0] = ardirectionbuffer[1];
499 ardirectionbuffer[1] = -1;
505 ** Sets the direction
507 void set_direction(int newdir)
509 if(ardirectionbuffer[0] != newdir)
511 /*if 1st place is empty*/
512 if(ardirectionbuffer[0] == -1)
514 /*use 1st space:*/
515 ardirectionbuffer[0] = newdir;
517 else
519 /*use 2nd space:*/
520 if(ardirectionbuffer[0] != newdir) ardirectionbuffer[1] = newdir;
523 if(frames < 0) ardirectionbuffer[0] = newdir;
527 void new_level(int level)
529 load_level(level);
531 ardirectionbuffer[0] = -1;
532 ardirectionbuffer[1] = -1;
533 dir = EAST;
534 headx = WIDTH/2;
535 heady = HEIGHT/2;
536 tailx = headx - 4;
537 taily = heady;
538 applecountdown = 0;
539 /*Create a small snake to start off with*/
540 board[headx][heady] = dir;
541 board[headx-1][heady] = dir;
542 board[headx-2][heady] = dir;
543 board[headx-3][heady] = dir;
544 board[headx-4][heady] = dir;
545 num_apples_to_got=0;
548 void init_snake(void)
550 num_apples_to_get=1;
551 if(game_type == 1)
552 level_from_file = 1;
553 game_b_level=1;
554 new_level(level_from_file);
557 #if (LCD_WIDTH >= 160) && (LCD_HEIGHT >= 128)
558 void draw_frame_bitmap(int header_type)
560 rb->lcd_bitmap(header_type==1? snake2_header1: snake2_header2, 0, 0,
561 BMPWIDTH_snake2_header, BMPHEIGHT_snake2_header);
562 rb->lcd_bitmap(snake2_left, 0, BMPHEIGHT_snake2_header,
563 BMPWIDTH_snake2_left, BMPHEIGHT_snake2_left);
564 rb->lcd_bitmap(snake2_right,
565 LCD_WIDTH - BMPWIDTH_snake2_right, BMPHEIGHT_snake2_header,
566 BMPWIDTH_snake2_right, BMPHEIGHT_snake2_right);
567 rb->lcd_bitmap(snake2_bottom,
568 0, BMPHEIGHT_snake2_header + BMPHEIGHT_snake2_left,
569 BMPWIDTH_snake2_bottom, BMPHEIGHT_snake2_bottom);
571 #endif
574 ** Draws the apple. If it doesn't exist then
575 ** a new one get's created.
577 void draw_apple_bit(int x, int y)
579 rb->lcd_fillrect((CENTER_X+x*MULTIPLIER)+1, CENTER_Y+y*MULTIPLIER,
580 MODIFIER_2, MODIFIER_1);
581 rb->lcd_fillrect(CENTER_X+x*MULTIPLIER, (CENTER_Y+y*MULTIPLIER)+1,
582 MODIFIER_1, MODIFIER_2);
585 void draw_apple( void )
587 int x,y;
589 #if LCD_WIDTH >= 160 && LCD_HEIGHT >= 128
590 draw_frame_bitmap(2);
592 rb->snprintf(strbuf, sizeof(strbuf), "%d", applecount);
593 rb->lcd_getstringsize(strbuf, &strwdt, &strhgt);
594 rb->lcd_putsxy(TOP_X3 - strwdt/2, TOP_Y2, strbuf);
596 rb->snprintf(strbuf, sizeof(strbuf), "%d", score);
597 rb->lcd_getstringsize(strbuf, &strwdt, &strhgt);
598 rb->lcd_putsxy(TOP_X4 - strwdt/2, TOP_Y2, strbuf);
599 #endif
601 if (!apple)
605 x = (rb->rand() % (WIDTH-1))+1;
606 y = (rb->rand() % (HEIGHT-1))+1;
607 } while (board[x][y]);
608 apple = 1;
609 board[x][y] = -1;
610 applex = x;appley = y;
612 draw_apple_bit(applex, appley);
616 * x x *
617 * x x *
618 * x x *
619 * x x *
621 void draw_vertical_bit(int x, int y)
623 rb->lcd_fillrect(CENTER_X+x*MULTIPLIER+1, CENTER_Y+y*MULTIPLIER,
624 MODIFIER_2, MODIFIER_1);
628 * * * *
629 X X X X
630 X X X X
631 * * * *
633 void draw_horizontal_bit(int x, int y)
635 rb->lcd_fillrect(CENTER_X+x*MULTIPLIER, CENTER_Y+y*MULTIPLIER+1,
636 MODIFIER_1, MODIFIER_2);
640 * * * *
641 * * X X
642 * X X X
643 * X X *
645 void draw_n_to_e_bit(int x, int y)
647 rb->lcd_fillrect(CENTER_X+x*MULTIPLIER+1, CENTER_Y+y*MULTIPLIER+2,
648 MODIFIER_2, MODIFIER_2);
649 rb->lcd_fillrect(CENTER_X+x*MULTIPLIER+2, CENTER_Y+y*MULTIPLIER+1,
650 MODIFIER_2, MODIFIER_2);
654 * * * *
655 * * X X
656 * X X X
657 * X X *
659 void draw_w_to_s_bit(int x, int y)
661 draw_n_to_e_bit(x,y);
665 * * * *
666 X X * *
667 X X X *
668 * X X *
670 void draw_n_to_w_bit(int x, int y)
672 rb->lcd_fillrect(CENTER_X+x*MULTIPLIER, CENTER_Y+y*MULTIPLIER+1,
673 MODIFIER_2, MODIFIER_2);
674 rb->lcd_fillrect(CENTER_X+x*MULTIPLIER+1, CENTER_Y+y*MULTIPLIER+2,
675 MODIFIER_2, MODIFIER_2);
679 * * * *
680 X X * *
681 X X X *
682 * X X *
684 void draw_e_to_s_bit(int x, int y)
686 draw_n_to_w_bit(x, y);
690 * X X *
691 * X X X
692 * * X X
693 * * * *
695 void draw_s_to_e_bit(int x, int y)
697 rb->lcd_fillrect(CENTER_X+x*MULTIPLIER+1, CENTER_Y+y*MULTIPLIER,
698 MODIFIER_2, MODIFIER_2);
699 rb->lcd_fillrect(CENTER_X+x*MULTIPLIER+2, CENTER_Y+y*MULTIPLIER+1,
700 MODIFIER_2, MODIFIER_2);
704 * X X *
705 * X X X
706 * * X X
707 * * * *
709 void draw_w_to_n_bit(int x, int y)
711 draw_s_to_e_bit(x,y);
715 * X X *
716 X X X *
717 X X * *
718 * * * *
720 void draw_e_to_n_bit(int x, int y)
722 rb->lcd_fillrect(CENTER_X+x*MULTIPLIER+1, CENTER_Y+y*MULTIPLIER,
723 MODIFIER_2, MODIFIER_2);
724 rb->lcd_fillrect(CENTER_X+x*MULTIPLIER, CENTER_Y+y*MULTIPLIER+1,
725 MODIFIER_2, MODIFIER_2);
729 * X X *
730 X X X *
731 X X * *
732 * * * *
734 void draw_s_to_w_bit(int x, int y)
736 draw_e_to_n_bit(x, y);
739 void draw_head_bit(int x, int y)
741 rb->lcd_fillrect(CENTER_X+x*MULTIPLIER, CENTER_Y+y*MULTIPLIER,
742 MODIFIER_1, MODIFIER_1);
746 ** Draws a wall/obsticals
748 void draw_boundary ( void )
750 int x, y;
752 /*TODO: Load levels from file!*/
754 /*top and bottom line*/
755 for(x=0; x < WIDTH; x++)
757 board[x][0] = EAST;
758 board[x][HEIGHT-1] = WEST;
761 /*left and right lines*/
762 for(y=0; y < HEIGHT; y++)
764 board[0][y] = NORTH;
765 board[WIDTH-1][y] = SOUTH;
768 /*corners:*/
769 board[0][0] = NORTH_EAST;
770 board[WIDTH-1][0] = EAST_SOUTH;
771 board[0][HEIGHT-1] = SOUTH_EAST;
772 board[WIDTH-1][HEIGHT-1] = EAST_NORTH;
776 ** Redraw the entire board
778 void redraw (void)
780 int x,y;
782 #ifdef HAVE_LCD_COLOR
783 rb->lcd_set_foreground(LCD_BLACK);
784 rb->lcd_set_background(LCD_WHITE);
785 #endif
787 rb->lcd_clear_display();
789 for (x = 0; x < WIDTH; x++)
791 for (y = 0; y < HEIGHT; y++)
793 switch (board[x][y])
795 case -1:
796 draw_apple_bit(x, y);
797 break;
798 case 0:
799 break;
801 case NORTH:
802 case SOUTH:
803 draw_vertical_bit(x,y);
804 break;
806 case EAST:
807 case WEST:
808 draw_horizontal_bit(x,y);
809 break;
811 default:
812 draw_head_bit(x, y);
813 break;
818 #if LCD_WIDTH >= 160 && LCD_HEIGHT >= 128
819 draw_frame_bitmap(2);
821 rb->snprintf(strbuf, sizeof(strbuf), "%d", applecount);
822 rb->lcd_getstringsize(strbuf, &strwdt, &strhgt);
823 rb->lcd_putsxy(TOP_X3 - strwdt/2, TOP_Y2, strbuf);
825 rb->snprintf(strbuf, sizeof(strbuf), "%d", score);
826 rb->lcd_getstringsize(strbuf, &strwdt, &strhgt);
827 rb->lcd_putsxy(TOP_X4 - strwdt/2, TOP_Y2, strbuf);
828 #endif
832 ** Draws the snake bit described by nCurrentBit at position x/y
833 ** deciding whether it's a corner bit by examing the nPrevious bit
835 void draw_snake_bit(int currentbit, int previousbit, int x, int y)
837 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
838 draw_head_bit(x, y);
839 rb->lcd_set_drawmode(DRMODE_SOLID);
841 switch(currentbit)
843 case(NORTH):
844 switch(previousbit)
846 case(SOUTH):
847 case(NORTH):
848 draw_vertical_bit(x,y);
849 break;
851 case(EAST):
852 draw_e_to_n_bit(x,y);
853 break;
855 case(WEST):
856 draw_w_to_n_bit(x,y);
857 break;
859 break;
861 case(EAST):
862 switch(previousbit)
864 case(WEST):
865 case(EAST):
866 draw_horizontal_bit(x,y);
867 break;
869 case(NORTH):
870 draw_n_to_e_bit(x,y);
871 break;
873 case(SOUTH):
874 draw_s_to_e_bit(x,y);
875 break;
877 break;
879 case(SOUTH):
880 switch(previousbit)
882 case(SOUTH):
883 case(NORTH):
884 draw_vertical_bit(x,y);
885 break;
887 case(EAST):
888 draw_e_to_s_bit(x,y);
889 break;
891 case(WEST):
892 draw_w_to_s_bit(x,y);
893 break;
895 break;
897 case(WEST):
898 switch(previousbit)
900 case(EAST):
901 case(WEST):
902 draw_horizontal_bit(x,y);
903 break;
905 case(SOUTH):
906 draw_s_to_w_bit(x,y);
907 break;
909 case(NORTH):
910 draw_n_to_w_bit(x,y);
911 break;
913 break;
917 void redraw_snake(void)
919 int x = tailx, y = taily;
920 int olddir, newdir = board[x][y];
922 while (x != headx || y != heady)
924 olddir = newdir;
926 switch (olddir)
928 case(NORTH):
929 y--;
930 break;
932 case(EAST):
933 x++;
934 break;
936 case(SOUTH):
937 y++;
938 break;
940 case(WEST):
941 x--;
942 break;
945 if(x == WIDTH)
946 x = 0;
947 else if(x < 0)
948 x = WIDTH-1;
950 if(y == HEIGHT)
951 y = 0;
952 else if(y < 0)
953 y = HEIGHT-1;
955 newdir = board[x][y];
956 if(olddir != newdir)
957 draw_snake_bit(newdir, olddir, x, y);
962 ** Death 'sequence' and end game stuff.
964 void die (void)
966 int button;
967 bool done=false;
969 rb->splash(HZ*2, "Oops!");
971 rb->lcd_clear_display();
973 applecount=0;
975 rb->lcd_getstringsize("You died!",&strwdt,&strhgt);
976 rb->lcd_putsxy((LCD_WIDTH - strwdt)/2,strhgt,"You died!");
978 rb->snprintf(strbuf, sizeof(strbuf), "Your score: %d", score);
979 rb->lcd_getstringsize(strbuf, &strwdt, &strhgt);
980 rb->lcd_putsxy((LCD_WIDTH - strwdt)/2, strhgt * 2 + 2, strbuf);
982 if (highscore_update(score, level_from_file, game_type==0?"Type A":"Type B",
983 highscores, NUM_SCORES) == 0)
985 rb->lcd_getstringsize("New high score!",&strwdt,&strhgt);
986 rb->lcd_putsxy((LCD_WIDTH - strwdt)/2,strhgt * 4 + 2,"New high score!");
988 else
990 rb->snprintf(strbuf, sizeof(strbuf), "High score: %d", highscores[0].score);
991 rb->lcd_getstringsize(strbuf, &strwdt, &strhgt);
992 rb->lcd_putsxy((LCD_WIDTH - strwdt)/2, strhgt * 5, strbuf);
995 rb->snprintf(strbuf, sizeof(strbuf), "Press %s...", SNAKE2_PLAYPAUSE_TEXT);
996 rb->lcd_getstringsize(strbuf, &strwdt, &strhgt);
997 rb->lcd_putsxy((LCD_WIDTH - strwdt)/2, strhgt * 7, strbuf);
999 rb->lcd_update();
1001 while(!done)
1003 button=rb->button_get(true);
1004 switch(button)
1006 case SNAKE2_PLAYPAUSE:
1007 done = true;
1008 break;
1012 dead=1;
1016 ** Check for collision. TODO: Currently this
1017 ** sets of the death sequence. What we want is it to only return a true/false
1018 ** depending on whether a collision occured.
1020 void collision ( int x, int y )
1022 int bdeath=0;
1025 switch (board[x][y])
1027 case 0:
1029 break;
1030 case -1:
1031 score = score + (1 * level);
1032 apple=0;
1033 applecountdown=2;
1034 applecount++;
1036 if(game_type==1)
1038 if(num_apples_to_get == num_apples_to_got)
1040 level_from_file++;
1041 if(level_from_file >= num_levels)
1043 level_from_file = 1;
1044 /*and increase the number of apples to pick up
1045 before level changes*/
1046 num_apples_to_get+=2;
1047 game_b_level++;
1049 rb->splash(HZ, "Level Completed!");
1050 new_level(level_from_file);
1051 redraw();
1052 rb->lcd_update();
1054 else
1055 num_apples_to_got++;
1057 break;
1058 default:
1059 bdeath=1;
1060 break;
1063 if(bdeath==1)
1065 die();
1066 sillydir = dir;
1067 frames = -110;
1071 void move( void )
1073 int taildir;
1074 /*this actually sets the dir variable.*/
1075 get_direction();
1076 /*draw head*/
1077 switch (dir)
1079 case (NORTH):
1080 board[headx][heady]=NORTH;
1081 heady--;
1082 break;
1083 case (EAST):
1084 board[headx][heady]=EAST;
1085 headx++;
1086 break;
1087 case (SOUTH):
1088 board[headx][heady]=SOUTH;
1089 heady++;
1090 break;
1091 case (WEST):
1092 board[headx][heady]=WEST;
1093 headx--;
1094 break;
1097 if(headx == WIDTH)
1098 headx = 0;
1099 else if(headx < 0)
1100 headx = WIDTH-1;
1102 if(heady == HEIGHT)
1103 heady = 0;
1104 else if(heady < 0)
1105 heady = HEIGHT-1;
1107 draw_head_bit(headx, heady);
1109 /*clear tail*/
1110 if(applecountdown <= 0)
1112 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
1113 draw_head_bit(tailx, taily);
1114 rb->lcd_set_drawmode(DRMODE_SOLID);
1116 taildir = board[tailx][taily];
1117 board[tailx][taily] = 0;
1119 switch (taildir)
1121 case(NORTH):
1122 taily--;
1123 break;
1125 case(EAST):
1126 tailx++;
1127 break;
1129 case(SOUTH):
1130 taily++;
1131 break;
1133 case(WEST):
1134 tailx--;
1135 break;
1138 if(tailx == WIDTH)
1139 tailx = 0;
1140 else if(tailx < 0)
1141 tailx = WIDTH-1;
1143 if(taily == HEIGHT)
1144 taily = 0;
1145 else if(taily < 0)
1146 taily = HEIGHT-1;
1148 else
1149 applecountdown--;
1152 void frame (void)
1154 int olddir, noldx, noldy, temp;
1155 noldx = headx;
1156 noldy = heady;
1157 olddir = 0;
1158 switch(dir)
1160 case(NORTH):
1161 if(heady == HEIGHT-1)
1162 temp = 0;
1163 else
1164 temp = heady + 1;
1166 olddir = board[headx][temp];
1167 break;
1169 case(EAST):
1170 if(headx == 0)
1171 temp = WIDTH-1;
1172 else
1173 temp = headx - 1;
1175 olddir = board[temp][heady];
1176 break;
1178 case(SOUTH):
1179 if(heady == 0)
1180 temp = HEIGHT-1;
1181 else
1182 temp = heady - 1;
1184 olddir = board[headx][temp];
1185 break;
1187 case(WEST):
1188 if(headx == WIDTH-1)
1189 temp = 0;
1190 else
1191 temp = headx + 1;
1193 olddir = board[temp][heady];
1194 break;
1197 move();
1200 now redraw the bit that was
1201 the tail, to something snake-like:
1203 draw_snake_bit(dir, olddir, noldx, noldy);
1205 collision(headx, heady);
1207 rb->lcd_update();
1210 void game_pause (void)
1212 int button;
1214 rb->lcd_clear_display();
1215 rb->lcd_getstringsize("Paused",&strwdt,&strhgt);
1216 rb->lcd_putsxy((LCD_WIDTH - strwdt)/2,LCD_HEIGHT/2,"Paused");
1218 rb->lcd_update();
1219 while (1)
1221 button = rb->button_get(true);
1222 switch (button)
1224 case SNAKE2_PLAYPAUSE:
1225 redraw();
1226 redraw_snake();
1227 draw_head_bit(headx, heady);
1228 rb->lcd_update();
1229 rb->sleep(HZ/2);
1230 return;
1232 #ifdef SNAKE2_RC_QUIT
1233 case SNAKE2_RC_QUIT:
1234 #endif
1235 case SNAKE2_QUIT:
1236 dead = 1;
1237 quit = 1;
1238 return;
1240 default:
1241 if (rb->default_event_handler(button)==SYS_USB_CONNECTED) {
1242 dead = 1;
1243 quit = 2;
1244 return;
1246 break;
1251 void game (void)
1253 int button;
1255 redraw();
1256 rb->lcd_update();
1257 /*main loop:*/
1258 while (1)
1260 if(frames==5)
1262 frame();
1263 if(frames > 0) frames = 0;
1265 frames++;
1267 if(frames == 0)
1269 die();
1271 else
1273 if(frames < 0)
1275 if(sillydir != dir)
1277 /*it has, great set frames to a positive value again:*/
1278 frames = 1;
1283 if (dead) return;
1285 draw_apple();
1287 rb->sleep(HZ/speed);
1289 button = rb->button_get(false);
1291 #ifdef HAS_BUTTON_HOLD
1292 if (rb->button_hold())
1293 button = SNAKE2_PLAYPAUSE;
1294 #endif
1296 switch (button)
1298 case SNAKE2_UP:
1299 case SNAKE2_UP | BUTTON_REPEAT:
1300 if (dir != SOUTH) set_direction(NORTH);
1301 break;
1303 case SNAKE2_RIGHT:
1304 case SNAKE2_RIGHT | BUTTON_REPEAT:
1305 if (dir != WEST) set_direction(EAST);
1306 break;
1308 case SNAKE2_DOWN:
1309 case SNAKE2_DOWN | BUTTON_REPEAT:
1310 if (dir != NORTH) set_direction(SOUTH);
1311 break;
1313 case SNAKE2_LEFT:
1314 case SNAKE2_LEFT | BUTTON_REPEAT:
1315 if (dir != EAST) set_direction(WEST);
1316 break;
1318 #ifdef SNAKE2_RC_QUIT
1319 case SNAKE2_RC_QUIT:
1320 #endif
1321 case SNAKE2_QUIT:
1322 quit = 1;
1323 return;
1325 case SNAKE2_PLAYPAUSE:
1326 game_pause();
1327 break;
1329 default:
1330 if (rb->default_event_handler(button)==SYS_USB_CONNECTED) {
1331 quit = 2;
1332 return;
1334 break;
1340 void select_maze(void)
1342 int button;
1344 clear_board();
1345 load_level( level_from_file );
1346 redraw();
1347 rb->lcd_update();
1349 while (1)
1351 #if LCD_WIDTH >= 160 && LCD_HEIGHT >= 128
1352 draw_frame_bitmap(1);
1354 rb->snprintf(strbuf, sizeof(strbuf), "%d", level);
1355 rb->lcd_getstringsize(strbuf, &strwdt, &strhgt);
1356 rb->lcd_putsxy(TOP_X3 - strwdt/2, TOP_Y2, strbuf);
1358 rb->snprintf(strbuf, sizeof(strbuf), "%d", level_from_file);
1359 rb->lcd_getstringsize(strbuf, &strwdt, &strhgt);
1360 rb->lcd_putsxy(TOP_X2 - strwdt/2, TOP_Y1, strbuf);
1362 rb->strcpy(strbuf, game_type==0? "A": "B");
1363 rb->lcd_getstringsize(strbuf, &strwdt, &strhgt);
1364 rb->lcd_putsxy(TOP_X1, TOP_Y1, strbuf);
1366 rb->snprintf(strbuf, sizeof(strbuf), "%d", highscores[0].score);
1367 rb->lcd_getstringsize(strbuf, &strwdt, &strhgt);
1368 rb->lcd_putsxy(TOP_X4 - strwdt/2, TOP_Y2, strbuf);
1370 #else
1371 rb->snprintf(strbuf, sizeof(strbuf), "Maze: %d", level_from_file);
1372 rb->lcd_getstringsize(strbuf, &strwdt, &strhgt);
1373 rb->lcd_putsxy((WIDTH*MULTIPLIER - strwdt)/2,
1374 (HEIGHT*MULTIPLIER - strhgt)/2, strbuf);
1375 #endif
1377 rb->lcd_update();
1379 button = rb->button_get(true);
1380 switch (button)
1382 case SNAKE2_QUIT:
1383 case SNAKE2_PLAYPAUSE:
1384 return;
1385 break;
1386 case SNAKE2_UP:
1387 case SNAKE2_RIGHT:
1388 if(level_from_file < num_levels)
1389 level_from_file++;
1390 else
1391 level_from_file = 0;
1392 load_level( level_from_file );
1393 redraw();
1394 break;
1395 case SNAKE2_DOWN:
1396 case SNAKE2_LEFT:
1397 if(level_from_file > 0)
1398 level_from_file--;
1399 else
1400 level_from_file = num_levels;
1401 load_level( level_from_file );
1402 redraw();
1403 break;
1404 default:
1405 if (rb->default_event_handler(button)==SYS_USB_CONNECTED) {
1406 quit = 2;
1407 return;
1409 break;
1415 void game_init(void)
1417 int selection = 0;
1419 static const struct opt_items type_options[] = {
1420 { "Type A", -1 },
1421 { "Type B", -1 },
1424 MENUITEM_STRINGLIST(menu, "Snake2 Menu", NULL,
1425 "Start New Game",
1426 "Game Type", "Select Maze", "Speed",
1427 "High Scores",
1428 "Playback Control", "Quit");
1430 rb->button_clear_queue();
1432 dead = 0;
1433 apple = 0;
1434 score = 0;
1435 applecount = 0;
1437 while (1) {
1438 switch (rb->do_menu(&menu, &selection, NULL, false)) {
1439 case 0:
1440 speed = level*20;
1441 return;
1442 case 1:
1443 rb->set_option("Game Type", &game_type, INT,
1444 type_options, 2, NULL);
1445 break;
1446 case 2:
1447 select_maze();
1448 if(quit) return;
1449 break;
1450 case 3:
1451 rb->set_int("Speed", "", UNIT_INT, &level,
1452 NULL, 1, 1, 10, NULL);
1453 break;
1454 case 4:
1455 highscore_show(NUM_SCORES, highscores, NUM_SCORES, true);
1456 break;
1457 case 5:
1458 playback_control(NULL);
1459 break;
1460 case 6:
1461 quit = 1;
1462 return;
1463 case MENU_ATTACHED_USB:
1464 quit = 2;
1465 return;
1466 default:
1467 break;
1472 enum plugin_status plugin_start(const void* parameter)
1474 (void)(parameter);
1476 /* Lets use the default font */
1477 rb->lcd_setfont(FONT_SYSFIXED);
1478 #if LCD_DEPTH > 1
1479 rb->lcd_set_backdrop(NULL);
1480 #endif
1482 load_all_levels();
1484 if (num_levels == 0) {
1485 rb->splash(HZ*2, "Failed loading levels!");
1486 return PLUGIN_OK;
1489 highscore_load(SCORE_FILE, highscores, NUM_SCORES);
1491 while(quit==0)
1493 game_init();
1494 if(quit)
1495 break;
1497 rb->lcd_clear_display();
1498 frames=1;
1500 init_snake();
1502 /*Start Game:*/
1503 game();
1506 highscore_save(SCORE_FILE, highscores, NUM_SCORES);
1508 return (quit==1) ? PLUGIN_OK : PLUGIN_USB_CONNECTED;
1511 #endif