Re-add the lseek to the beginning of the file which was accidentally removed.
[kugel-rb.git] / apps / plugins / snake2.c
bloba147f7cd232a04c3d0d14a3aeb7f4e05741c4f90
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"
34 #include "lib/highscore.h"
35 #include "lib/playback_control.h"
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 (CONFIG_KEYPAD == PHILIPS_HDD6330_PAD)
281 #define SNAKE2_LEFT BUTTON_LEFT
282 #define SNAKE2_RIGHT BUTTON_RIGHT
283 #define SNAKE2_UP BUTTON_UP
284 #define SNAKE2_DOWN BUTTON_DOWN
285 #define SNAKE2_QUIT BUTTON_POWER
286 #define SNAKE2_PLAYPAUSE BUTTON_VIEW
287 #define SNAKE2_PLAYPAUSE_TEXT "View"
289 #elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD
290 #define SNAKE2_LEFT BUTTON_PREV
291 #define SNAKE2_RIGHT BUTTON_NEXT
292 #define SNAKE2_UP BUTTON_UP
293 #define SNAKE2_DOWN BUTTON_DOWN
294 #define SNAKE2_QUIT BUTTON_POWER
295 #define SNAKE2_PLAYPAUSE BUTTON_RIGHT
296 #define SNAKE2_PLAYPAUSE_TEXT "Right"
298 #elif (CONFIG_KEYPAD == ONDAVX747_PAD) || \
299 (CONFIG_KEYPAD == ONDAVX777_PAD) || \
300 CONFIG_KEYPAD == MROBE500_PAD
301 #define SNAKE2_QUIT BUTTON_POWER
303 #elif (CONFIG_KEYPAD == SAMSUNG_YH_PAD)
304 #define SNAKE2_LEFT BUTTON_LEFT
305 #define SNAKE2_RIGHT BUTTON_RIGHT
306 #define SNAKE2_UP BUTTON_UP
307 #define SNAKE2_DOWN BUTTON_DOWN
308 #define SNAKE2_QUIT BUTTON_REC
309 #define SNAKE2_PLAYPAUSE BUTTON_PLAY
310 #define SNAKE2_PLAYPAUSE_TEXT "Play"
312 #elif CONFIG_KEYPAD == PBELL_VIBE500_PAD
313 #define SNAKE2_LEFT BUTTON_PREV
314 #define SNAKE2_RIGHT BUTTON_NEXT
315 #define SNAKE2_UP BUTTON_UP
316 #define SNAKE2_DOWN BUTTON_DOWN
317 #define SNAKE2_QUIT BUTTON_REC
318 #define SNAKE2_PLAYPAUSE BUTTON_PLAY
319 #define SNAKE2_PLAYPAUSE_TEXT "Play"
321 #elif CONFIG_KEYPAD == MPIO_HD200_PAD
322 #define SNAKE2_LEFT BUTTON_VOL_DOWN
323 #define SNAKE2_RIGHT BUTTON_VOL_UP
324 #define SNAKE2_UP BUTTON_REW
325 #define SNAKE2_DOWN BUTTON_FF
326 #define SNAKE2_QUIT (BUTTON_REC | BUTTON_PLAY)
327 #define SNAKE2_PLAYPAUSE BUTTON_PLAY
328 #define SNAKE2_PLAYPAUSE_TEXT "Play"
330 #else
331 #error No keymap defined!
332 #endif
334 #ifdef HAVE_TOUCHSCREEN
335 #ifndef SNAKE2_LEFT
336 #define SNAKE2_LEFT BUTTON_MIDLEFT
337 #endif
338 #ifndef SNAKE2_RIGHT
339 #define SNAKE2_RIGHT BUTTON_MIDRIGHT
340 #endif
341 #ifndef SNAKE2_UP
342 #define SNAKE2_UP BUTTON_TOPMIDDLE
343 #endif
344 #ifndef SNAKE2_DOWN
345 #define SNAKE2_DOWN BUTTON_BOTTOMMIDDLE
346 #endif
347 #ifndef SNAKE2_QUIT
348 #define SNAKE2_QUIT BUTTON_TOPLEFT
349 #endif
350 #ifndef SNAKE2_PLAYPAUSE
351 #define SNAKE2_PLAYPAUSE BUTTON_CENTER
352 #endif
353 #ifndef SNAKE2_PLAYPAUSE_TEXT
354 #define SNAKE2_PLAYPAUSE_TEXT "CENTER"
355 #endif
356 #endif
358 static int max_levels = 0;
359 static char (*level_cache)[HEIGHT][WIDTH];
361 /*Board itself - 2D int array*/
362 static int board[WIDTH][HEIGHT];
364 Buffer for sorting movement (in case user presses two movements during a
365 single frame
367 static int ardirectionbuffer[2];
368 static int score;
369 static int applex;
370 static int appley;
371 static int dir;
372 static int frames;
373 static int apple;
374 static int level = 4, speed = 5,dead = 0, quit = 0;
375 static int sillydir = 0, num_levels = 0;
376 static int level_from_file = 0;
377 static int headx, heady, tailx, taily, applecountdown = 5;
378 static int game_type = 0;
379 static int num_apples_to_get=1;
380 static int num_apples_to_got=0;
381 static int game_b_level=0;
382 static int applecount=0;
383 /* used for string width, height for orientation purposes */
384 static int strwdt, strhgt;
385 static char strbuf[32];
387 #define NUM_SCORES 5
388 static struct highscore highscores[NUM_SCORES];
390 #define NORTH 1
391 #define EAST 2
392 #define SOUTH 4
393 #define WEST 8
394 #define HEAD 16
396 #define EAST_NORTH 32
397 #define EAST_SOUTH 64
398 #define WEST_NORTH 128
399 #define WEST_SOUTH 256
401 #define NORTH_EAST 512
402 #define NORTH_WEST 1024
403 #define SOUTH_EAST 2048
404 #define SOUTH_WEST 4096
406 #define LEVELS_FILE PLUGIN_GAMES_DIR "/snake2.levels"
407 #define SCORE_FILE PLUGIN_GAMES_DIR "/snake2.score"
409 int load_all_levels(void)
411 int linecnt = 0;
412 int fd;
413 size_t size;
414 char buf[64]; /* Larger than WIDTH, to allow for whitespace after the
415 lines */
417 /* Init the level_cache pointer and
418 calculate how many levels that will fit */
419 level_cache = rb->plugin_get_buffer(&size);
420 max_levels = size / (HEIGHT*WIDTH);
422 num_levels = 0;
424 /* open file */
425 if ((fd = rb->open(LEVELS_FILE, O_RDONLY)) < 0)
427 return -1;
430 while(rb->read_line(fd, buf, 64) > 0)
432 if(rb->strlen(buf) == 0) /* Separator? */
434 num_levels++;
435 if(num_levels > max_levels)
437 rb->splash(HZ, "Too many levels in file");
438 break;
440 continue;
443 rb->memcpy(level_cache[num_levels][linecnt], buf, WIDTH);
444 linecnt++;
445 if(linecnt == HEIGHT)
447 linecnt = 0;
451 rb->close(fd);
452 return 0;
456 ** Completely clear the board of walls and/or snake
459 void clear_board( void)
461 int x,y;
463 for (x = 0; x < WIDTH; x++)
465 for (y = 0; y < HEIGHT; y++)
467 board[x][y] = 0;
472 int load_level( int level_number )
474 int x,y;
475 clear_board();
476 for(y = 0;y < HEIGHT;y++)
478 for(x = 0;x < WIDTH;x++)
480 switch(level_cache[level_number][y][x])
482 case '|':
483 board[x][y] = NORTH;
484 break;
486 case '-':
487 board[x][y] = EAST;
488 break;
490 case '+':
491 board[x][y] = HEAD;
492 break;
496 return 1;
500 ** Gets the currently chosen direction from the first place
501 ** in the direction buffer. If there is something in the
502 ** next part of the buffer then that is moved to the first place
504 void get_direction( void )
506 /*if 1st place is empty*/
507 if(ardirectionbuffer[0] != -1)
509 /*return this direction*/
510 dir = ardirectionbuffer[0];
511 ardirectionbuffer[0]=-1;
512 /*now see if one needs moving:*/
513 if(ardirectionbuffer[1] != -1)
515 /*there's a move waiting to be done
516 so move it into the space:*/
517 ardirectionbuffer[0] = ardirectionbuffer[1];
518 ardirectionbuffer[1] = -1;
524 ** Sets the direction
526 void set_direction(int newdir)
528 if(ardirectionbuffer[0] != newdir)
530 /*if 1st place is empty*/
531 if(ardirectionbuffer[0] == -1)
533 /*use 1st space:*/
534 ardirectionbuffer[0] = newdir;
536 else
538 /*use 2nd space:*/
539 if(ardirectionbuffer[0] != newdir) ardirectionbuffer[1] = newdir;
542 if(frames < 0) ardirectionbuffer[0] = newdir;
546 void new_level(int level)
548 load_level(level);
550 ardirectionbuffer[0] = -1;
551 ardirectionbuffer[1] = -1;
552 dir = EAST;
553 headx = WIDTH/2;
554 heady = HEIGHT/2;
555 tailx = headx - 4;
556 taily = heady;
557 applecountdown = 0;
558 /*Create a small snake to start off with*/
559 board[headx][heady] = dir;
560 board[headx-1][heady] = dir;
561 board[headx-2][heady] = dir;
562 board[headx-3][heady] = dir;
563 board[headx-4][heady] = dir;
564 num_apples_to_got=0;
567 void init_snake(void)
569 num_apples_to_get=1;
570 if(game_type == 1)
571 level_from_file = 1;
572 game_b_level=1;
573 new_level(level_from_file);
576 #if (LCD_WIDTH >= 160) && (LCD_HEIGHT >= 128)
577 void draw_frame_bitmap(int header_type)
579 rb->lcd_bitmap(header_type==1? snake2_header1: snake2_header2, 0, 0,
580 BMPWIDTH_snake2_header, BMPHEIGHT_snake2_header);
581 rb->lcd_bitmap(snake2_left, 0, BMPHEIGHT_snake2_header,
582 BMPWIDTH_snake2_left, BMPHEIGHT_snake2_left);
583 rb->lcd_bitmap(snake2_right,
584 LCD_WIDTH - BMPWIDTH_snake2_right, BMPHEIGHT_snake2_header,
585 BMPWIDTH_snake2_right, BMPHEIGHT_snake2_right);
586 rb->lcd_bitmap(snake2_bottom,
587 0, BMPHEIGHT_snake2_header + BMPHEIGHT_snake2_left,
588 BMPWIDTH_snake2_bottom, BMPHEIGHT_snake2_bottom);
590 #endif
593 ** Draws the apple. If it doesn't exist then
594 ** a new one get's created.
596 void draw_apple_bit(int x, int y)
598 rb->lcd_fillrect((CENTER_X+x*MULTIPLIER)+1, CENTER_Y+y*MULTIPLIER,
599 MODIFIER_2, MODIFIER_1);
600 rb->lcd_fillrect(CENTER_X+x*MULTIPLIER, (CENTER_Y+y*MULTIPLIER)+1,
601 MODIFIER_1, MODIFIER_2);
604 void draw_apple( void )
606 int x,y;
608 #if LCD_WIDTH >= 160 && LCD_HEIGHT >= 128
609 draw_frame_bitmap(2);
611 rb->snprintf(strbuf, sizeof(strbuf), "%d", applecount);
612 rb->lcd_getstringsize(strbuf, &strwdt, &strhgt);
613 rb->lcd_putsxy(TOP_X3 - strwdt/2, TOP_Y2, strbuf);
615 rb->snprintf(strbuf, sizeof(strbuf), "%d", score);
616 rb->lcd_getstringsize(strbuf, &strwdt, &strhgt);
617 rb->lcd_putsxy(TOP_X4 - strwdt/2, TOP_Y2, strbuf);
618 #endif
620 if (!apple)
624 x = (rb->rand() % (WIDTH-1))+1;
625 y = (rb->rand() % (HEIGHT-1))+1;
626 } while (board[x][y]);
627 apple = 1;
628 board[x][y] = -1;
629 applex = x;appley = y;
631 draw_apple_bit(applex, appley);
635 * x x *
636 * x x *
637 * x x *
638 * x x *
640 void draw_vertical_bit(int x, int y)
642 rb->lcd_fillrect(CENTER_X+x*MULTIPLIER+1, CENTER_Y+y*MULTIPLIER,
643 MODIFIER_2, MODIFIER_1);
647 * * * *
648 X X X X
649 X X X X
650 * * * *
652 void draw_horizontal_bit(int x, int y)
654 rb->lcd_fillrect(CENTER_X+x*MULTIPLIER, CENTER_Y+y*MULTIPLIER+1,
655 MODIFIER_1, MODIFIER_2);
659 * * * *
660 * * X X
661 * X X X
662 * X X *
664 void draw_n_to_e_bit(int x, int y)
666 rb->lcd_fillrect(CENTER_X+x*MULTIPLIER+1, CENTER_Y+y*MULTIPLIER+2,
667 MODIFIER_2, MODIFIER_2);
668 rb->lcd_fillrect(CENTER_X+x*MULTIPLIER+2, CENTER_Y+y*MULTIPLIER+1,
669 MODIFIER_2, MODIFIER_2);
673 * * * *
674 * * X X
675 * X X X
676 * X X *
678 void draw_w_to_s_bit(int x, int y)
680 draw_n_to_e_bit(x,y);
684 * * * *
685 X X * *
686 X X X *
687 * X X *
689 void draw_n_to_w_bit(int x, int y)
691 rb->lcd_fillrect(CENTER_X+x*MULTIPLIER, CENTER_Y+y*MULTIPLIER+1,
692 MODIFIER_2, MODIFIER_2);
693 rb->lcd_fillrect(CENTER_X+x*MULTIPLIER+1, CENTER_Y+y*MULTIPLIER+2,
694 MODIFIER_2, MODIFIER_2);
698 * * * *
699 X X * *
700 X X X *
701 * X X *
703 void draw_e_to_s_bit(int x, int y)
705 draw_n_to_w_bit(x, y);
709 * X X *
710 * X X X
711 * * X X
712 * * * *
714 void draw_s_to_e_bit(int x, int y)
716 rb->lcd_fillrect(CENTER_X+x*MULTIPLIER+1, CENTER_Y+y*MULTIPLIER,
717 MODIFIER_2, MODIFIER_2);
718 rb->lcd_fillrect(CENTER_X+x*MULTIPLIER+2, CENTER_Y+y*MULTIPLIER+1,
719 MODIFIER_2, MODIFIER_2);
723 * X X *
724 * X X X
725 * * X X
726 * * * *
728 void draw_w_to_n_bit(int x, int y)
730 draw_s_to_e_bit(x,y);
734 * X X *
735 X X X *
736 X X * *
737 * * * *
739 void draw_e_to_n_bit(int x, int y)
741 rb->lcd_fillrect(CENTER_X+x*MULTIPLIER+1, CENTER_Y+y*MULTIPLIER,
742 MODIFIER_2, MODIFIER_2);
743 rb->lcd_fillrect(CENTER_X+x*MULTIPLIER, CENTER_Y+y*MULTIPLIER+1,
744 MODIFIER_2, MODIFIER_2);
748 * X X *
749 X X X *
750 X X * *
751 * * * *
753 void draw_s_to_w_bit(int x, int y)
755 draw_e_to_n_bit(x, y);
758 void draw_head_bit(int x, int y)
760 rb->lcd_fillrect(CENTER_X+x*MULTIPLIER, CENTER_Y+y*MULTIPLIER,
761 MODIFIER_1, MODIFIER_1);
765 ** Draws a wall/obsticals
767 void draw_boundary ( void )
769 int x, y;
771 /*TODO: Load levels from file!*/
773 /*top and bottom line*/
774 for(x=0; x < WIDTH; x++)
776 board[x][0] = EAST;
777 board[x][HEIGHT-1] = WEST;
780 /*left and right lines*/
781 for(y=0; y < HEIGHT; y++)
783 board[0][y] = NORTH;
784 board[WIDTH-1][y] = SOUTH;
787 /*corners:*/
788 board[0][0] = NORTH_EAST;
789 board[WIDTH-1][0] = EAST_SOUTH;
790 board[0][HEIGHT-1] = SOUTH_EAST;
791 board[WIDTH-1][HEIGHT-1] = EAST_NORTH;
795 ** Redraw the entire board
797 void redraw (void)
799 int x,y;
801 #ifdef HAVE_LCD_COLOR
802 rb->lcd_set_foreground(LCD_BLACK);
803 rb->lcd_set_background(LCD_WHITE);
804 #endif
806 rb->lcd_clear_display();
808 for (x = 0; x < WIDTH; x++)
810 for (y = 0; y < HEIGHT; y++)
812 switch (board[x][y])
814 case -1:
815 draw_apple_bit(x, y);
816 break;
817 case 0:
818 break;
820 case NORTH:
821 case SOUTH:
822 draw_vertical_bit(x,y);
823 break;
825 case EAST:
826 case WEST:
827 draw_horizontal_bit(x,y);
828 break;
830 default:
831 draw_head_bit(x, y);
832 break;
837 #if LCD_WIDTH >= 160 && LCD_HEIGHT >= 128
838 draw_frame_bitmap(2);
840 rb->snprintf(strbuf, sizeof(strbuf), "%d", applecount);
841 rb->lcd_getstringsize(strbuf, &strwdt, &strhgt);
842 rb->lcd_putsxy(TOP_X3 - strwdt/2, TOP_Y2, strbuf);
844 rb->snprintf(strbuf, sizeof(strbuf), "%d", score);
845 rb->lcd_getstringsize(strbuf, &strwdt, &strhgt);
846 rb->lcd_putsxy(TOP_X4 - strwdt/2, TOP_Y2, strbuf);
847 #endif
851 ** Draws the snake bit described by nCurrentBit at position x/y
852 ** deciding whether it's a corner bit by examing the nPrevious bit
854 void draw_snake_bit(int currentbit, int previousbit, int x, int y)
856 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
857 draw_head_bit(x, y);
858 rb->lcd_set_drawmode(DRMODE_SOLID);
860 switch(currentbit)
862 case(NORTH):
863 switch(previousbit)
865 case(SOUTH):
866 case(NORTH):
867 draw_vertical_bit(x,y);
868 break;
870 case(EAST):
871 draw_e_to_n_bit(x,y);
872 break;
874 case(WEST):
875 draw_w_to_n_bit(x,y);
876 break;
878 break;
880 case(EAST):
881 switch(previousbit)
883 case(WEST):
884 case(EAST):
885 draw_horizontal_bit(x,y);
886 break;
888 case(NORTH):
889 draw_n_to_e_bit(x,y);
890 break;
892 case(SOUTH):
893 draw_s_to_e_bit(x,y);
894 break;
896 break;
898 case(SOUTH):
899 switch(previousbit)
901 case(SOUTH):
902 case(NORTH):
903 draw_vertical_bit(x,y);
904 break;
906 case(EAST):
907 draw_e_to_s_bit(x,y);
908 break;
910 case(WEST):
911 draw_w_to_s_bit(x,y);
912 break;
914 break;
916 case(WEST):
917 switch(previousbit)
919 case(EAST):
920 case(WEST):
921 draw_horizontal_bit(x,y);
922 break;
924 case(SOUTH):
925 draw_s_to_w_bit(x,y);
926 break;
928 case(NORTH):
929 draw_n_to_w_bit(x,y);
930 break;
932 break;
936 void redraw_snake(void)
938 int x = tailx, y = taily;
939 int olddir, newdir = board[x][y];
941 while (x != headx || y != heady)
943 olddir = newdir;
945 switch (olddir)
947 case(NORTH):
948 y--;
949 break;
951 case(EAST):
952 x++;
953 break;
955 case(SOUTH):
956 y++;
957 break;
959 case(WEST):
960 x--;
961 break;
964 if(x == WIDTH)
965 x = 0;
966 else if(x < 0)
967 x = WIDTH-1;
969 if(y == HEIGHT)
970 y = 0;
971 else if(y < 0)
972 y = HEIGHT-1;
974 newdir = board[x][y];
975 if(olddir != newdir)
976 draw_snake_bit(newdir, olddir, x, y);
981 ** Death 'sequence' and end game stuff.
983 void die (void)
985 int button;
986 bool done=false;
988 rb->splash(HZ*2, "Oops!");
990 rb->lcd_clear_display();
992 applecount=0;
994 rb->lcd_getstringsize("You died!",&strwdt,&strhgt);
995 rb->lcd_putsxy((LCD_WIDTH - strwdt)/2,strhgt,"You died!");
997 rb->snprintf(strbuf, sizeof(strbuf), "Your score: %d", score);
998 rb->lcd_getstringsize(strbuf, &strwdt, &strhgt);
999 rb->lcd_putsxy((LCD_WIDTH - strwdt)/2, strhgt * 2 + 2, strbuf);
1001 if (highscore_update(score, level_from_file, game_type==0?"Type A":"Type B",
1002 highscores, NUM_SCORES) == 0)
1004 rb->lcd_getstringsize("New high score!",&strwdt,&strhgt);
1005 rb->lcd_putsxy((LCD_WIDTH - strwdt)/2,strhgt * 4 + 2,"New high score!");
1007 else
1009 rb->snprintf(strbuf, sizeof(strbuf), "High score: %d", highscores[0].score);
1010 rb->lcd_getstringsize(strbuf, &strwdt, &strhgt);
1011 rb->lcd_putsxy((LCD_WIDTH - strwdt)/2, strhgt * 5, strbuf);
1014 rb->snprintf(strbuf, sizeof(strbuf), "Press %s...", SNAKE2_PLAYPAUSE_TEXT);
1015 rb->lcd_getstringsize(strbuf, &strwdt, &strhgt);
1016 rb->lcd_putsxy((LCD_WIDTH - strwdt)/2, strhgt * 7, strbuf);
1018 rb->lcd_update();
1020 while(!done)
1022 button=rb->button_get(true);
1023 switch(button)
1025 case SNAKE2_PLAYPAUSE:
1026 done = true;
1027 break;
1031 dead=1;
1035 ** Check for collision. TODO: Currently this
1036 ** sets of the death sequence. What we want is it to only return a true/false
1037 ** depending on whether a collision occured.
1039 void collision ( int x, int y )
1041 int bdeath=0;
1044 switch (board[x][y])
1046 case 0:
1048 break;
1049 case -1:
1050 score = score + (1 * level);
1051 apple=0;
1052 applecountdown=2;
1053 applecount++;
1055 if(game_type==1)
1057 if(num_apples_to_get == num_apples_to_got)
1059 level_from_file++;
1060 if(level_from_file >= num_levels)
1062 level_from_file = 1;
1063 /*and increase the number of apples to pick up
1064 before level changes*/
1065 num_apples_to_get+=2;
1066 game_b_level++;
1068 rb->splash(HZ, "Level Completed!");
1069 new_level(level_from_file);
1070 redraw();
1071 rb->lcd_update();
1073 else
1074 num_apples_to_got++;
1076 break;
1077 default:
1078 bdeath=1;
1079 break;
1082 if(bdeath==1)
1084 die();
1085 sillydir = dir;
1086 frames = -110;
1090 void move( void )
1092 int taildir;
1093 /*this actually sets the dir variable.*/
1094 get_direction();
1095 /*draw head*/
1096 switch (dir)
1098 case (NORTH):
1099 board[headx][heady]=NORTH;
1100 heady--;
1101 break;
1102 case (EAST):
1103 board[headx][heady]=EAST;
1104 headx++;
1105 break;
1106 case (SOUTH):
1107 board[headx][heady]=SOUTH;
1108 heady++;
1109 break;
1110 case (WEST):
1111 board[headx][heady]=WEST;
1112 headx--;
1113 break;
1116 if(headx == WIDTH)
1117 headx = 0;
1118 else if(headx < 0)
1119 headx = WIDTH-1;
1121 if(heady == HEIGHT)
1122 heady = 0;
1123 else if(heady < 0)
1124 heady = HEIGHT-1;
1126 draw_head_bit(headx, heady);
1128 /*clear tail*/
1129 if(applecountdown <= 0)
1131 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
1132 draw_head_bit(tailx, taily);
1133 rb->lcd_set_drawmode(DRMODE_SOLID);
1135 taildir = board[tailx][taily];
1136 board[tailx][taily] = 0;
1138 switch (taildir)
1140 case(NORTH):
1141 taily--;
1142 break;
1144 case(EAST):
1145 tailx++;
1146 break;
1148 case(SOUTH):
1149 taily++;
1150 break;
1152 case(WEST):
1153 tailx--;
1154 break;
1157 if(tailx == WIDTH)
1158 tailx = 0;
1159 else if(tailx < 0)
1160 tailx = WIDTH-1;
1162 if(taily == HEIGHT)
1163 taily = 0;
1164 else if(taily < 0)
1165 taily = HEIGHT-1;
1167 else
1168 applecountdown--;
1171 void frame (void)
1173 int olddir, noldx, noldy, temp;
1174 noldx = headx;
1175 noldy = heady;
1176 olddir = 0;
1177 switch(dir)
1179 case(NORTH):
1180 if(heady == HEIGHT-1)
1181 temp = 0;
1182 else
1183 temp = heady + 1;
1185 olddir = board[headx][temp];
1186 break;
1188 case(EAST):
1189 if(headx == 0)
1190 temp = WIDTH-1;
1191 else
1192 temp = headx - 1;
1194 olddir = board[temp][heady];
1195 break;
1197 case(SOUTH):
1198 if(heady == 0)
1199 temp = HEIGHT-1;
1200 else
1201 temp = heady - 1;
1203 olddir = board[headx][temp];
1204 break;
1206 case(WEST):
1207 if(headx == WIDTH-1)
1208 temp = 0;
1209 else
1210 temp = headx + 1;
1212 olddir = board[temp][heady];
1213 break;
1216 move();
1219 now redraw the bit that was
1220 the tail, to something snake-like:
1222 draw_snake_bit(dir, olddir, noldx, noldy);
1224 collision(headx, heady);
1226 rb->lcd_update();
1229 void game_pause (void)
1231 int button;
1233 rb->lcd_clear_display();
1234 rb->lcd_getstringsize("Paused",&strwdt,&strhgt);
1235 rb->lcd_putsxy((LCD_WIDTH - strwdt)/2,LCD_HEIGHT/2,"Paused");
1237 rb->lcd_update();
1238 while (1)
1240 button = rb->button_get(true);
1241 switch (button)
1243 case SNAKE2_PLAYPAUSE:
1244 redraw();
1245 redraw_snake();
1246 draw_head_bit(headx, heady);
1247 rb->lcd_update();
1248 rb->sleep(HZ/2);
1249 return;
1251 #ifdef SNAKE2_RC_QUIT
1252 case SNAKE2_RC_QUIT:
1253 #endif
1254 case SNAKE2_QUIT:
1255 dead = 1;
1256 quit = 1;
1257 return;
1259 default:
1260 if (rb->default_event_handler(button)==SYS_USB_CONNECTED) {
1261 dead = 1;
1262 quit = 2;
1263 return;
1265 break;
1270 void game (void)
1272 int button;
1274 redraw();
1275 rb->lcd_update();
1276 /*main loop:*/
1277 while (1)
1279 if(frames==5)
1281 frame();
1282 if(frames > 0) frames = 0;
1284 frames++;
1286 if(frames == 0)
1288 die();
1290 else
1292 if(frames < 0)
1294 if(sillydir != dir)
1296 /*it has, great set frames to a positive value again:*/
1297 frames = 1;
1302 if (dead) return;
1304 draw_apple();
1306 rb->sleep(HZ/speed);
1308 button = rb->button_get(false);
1310 #ifdef HAS_BUTTON_HOLD
1311 if (rb->button_hold())
1312 button = SNAKE2_PLAYPAUSE;
1313 #endif
1315 switch (button)
1317 case SNAKE2_UP:
1318 case SNAKE2_UP | BUTTON_REPEAT:
1319 if (dir != SOUTH) set_direction(NORTH);
1320 break;
1322 case SNAKE2_RIGHT:
1323 case SNAKE2_RIGHT | BUTTON_REPEAT:
1324 if (dir != WEST) set_direction(EAST);
1325 break;
1327 case SNAKE2_DOWN:
1328 case SNAKE2_DOWN | BUTTON_REPEAT:
1329 if (dir != NORTH) set_direction(SOUTH);
1330 break;
1332 case SNAKE2_LEFT:
1333 case SNAKE2_LEFT | BUTTON_REPEAT:
1334 if (dir != EAST) set_direction(WEST);
1335 break;
1337 #ifdef SNAKE2_RC_QUIT
1338 case SNAKE2_RC_QUIT:
1339 #endif
1340 case SNAKE2_QUIT:
1341 quit = 1;
1342 return;
1344 case SNAKE2_PLAYPAUSE:
1345 game_pause();
1346 break;
1348 default:
1349 if (rb->default_event_handler(button)==SYS_USB_CONNECTED) {
1350 quit = 2;
1351 return;
1353 break;
1359 void select_maze(void)
1361 int button;
1363 clear_board();
1364 load_level( level_from_file );
1365 redraw();
1366 rb->lcd_update();
1368 while (1)
1370 #if LCD_WIDTH >= 160 && LCD_HEIGHT >= 128
1371 draw_frame_bitmap(1);
1373 rb->snprintf(strbuf, sizeof(strbuf), "%d", level);
1374 rb->lcd_getstringsize(strbuf, &strwdt, &strhgt);
1375 rb->lcd_putsxy(TOP_X3 - strwdt/2, TOP_Y2, strbuf);
1377 rb->snprintf(strbuf, sizeof(strbuf), "%d", level_from_file);
1378 rb->lcd_getstringsize(strbuf, &strwdt, &strhgt);
1379 rb->lcd_putsxy(TOP_X2 - strwdt/2, TOP_Y1, strbuf);
1381 rb->strcpy(strbuf, game_type==0? "A": "B");
1382 rb->lcd_getstringsize(strbuf, &strwdt, &strhgt);
1383 rb->lcd_putsxy(TOP_X1, TOP_Y1, strbuf);
1385 rb->snprintf(strbuf, sizeof(strbuf), "%d", highscores[0].score);
1386 rb->lcd_getstringsize(strbuf, &strwdt, &strhgt);
1387 rb->lcd_putsxy(TOP_X4 - strwdt/2, TOP_Y2, strbuf);
1389 #else
1390 rb->snprintf(strbuf, sizeof(strbuf), "Maze: %d", level_from_file);
1391 rb->lcd_getstringsize(strbuf, &strwdt, &strhgt);
1392 rb->lcd_putsxy((WIDTH*MULTIPLIER - strwdt)/2,
1393 (HEIGHT*MULTIPLIER - strhgt)/2, strbuf);
1394 #endif
1396 rb->lcd_update();
1398 button = rb->button_get(true);
1399 switch (button)
1401 case SNAKE2_QUIT:
1402 case SNAKE2_PLAYPAUSE:
1403 return;
1404 break;
1405 case SNAKE2_UP:
1406 case SNAKE2_RIGHT:
1407 if(level_from_file < num_levels)
1408 level_from_file++;
1409 else
1410 level_from_file = 0;
1411 load_level( level_from_file );
1412 redraw();
1413 break;
1414 case SNAKE2_DOWN:
1415 case SNAKE2_LEFT:
1416 if(level_from_file > 0)
1417 level_from_file--;
1418 else
1419 level_from_file = num_levels;
1420 load_level( level_from_file );
1421 redraw();
1422 break;
1423 default:
1424 if (rb->default_event_handler(button)==SYS_USB_CONNECTED) {
1425 quit = 2;
1426 return;
1428 break;
1434 void game_init(void)
1436 int selection = 0;
1438 static const struct opt_items type_options[] = {
1439 { "Type A", -1 },
1440 { "Type B", -1 },
1443 MENUITEM_STRINGLIST(menu, "Snake2 Menu", NULL,
1444 "Start New Game",
1445 "Game Type", "Select Maze", "Speed",
1446 "High Scores",
1447 "Playback Control", "Quit");
1449 rb->button_clear_queue();
1451 dead = 0;
1452 apple = 0;
1453 score = 0;
1454 applecount = 0;
1456 while (1) {
1457 switch (rb->do_menu(&menu, &selection, NULL, false)) {
1458 case 0:
1459 speed = level*20;
1460 return;
1461 case 1:
1462 rb->set_option("Game Type", &game_type, INT,
1463 type_options, 2, NULL);
1464 break;
1465 case 2:
1466 select_maze();
1467 if(quit) return;
1468 break;
1469 case 3:
1470 rb->set_int("Speed", "", UNIT_INT, &level,
1471 NULL, 1, 1, 10, NULL);
1472 break;
1473 case 4:
1474 highscore_show(-1, highscores, NUM_SCORES, true);
1475 break;
1476 case 5:
1477 playback_control(NULL);
1478 break;
1479 case 6:
1480 quit = 1;
1481 return;
1482 case MENU_ATTACHED_USB:
1483 quit = 2;
1484 return;
1485 default:
1486 break;
1491 enum plugin_status plugin_start(const void* parameter)
1493 (void)(parameter);
1495 /* Lets use the default font */
1496 rb->lcd_setfont(FONT_SYSFIXED);
1497 #if LCD_DEPTH > 1
1498 rb->lcd_set_backdrop(NULL);
1499 #endif
1501 load_all_levels();
1503 if (num_levels == 0) {
1504 rb->splash(HZ*2, "Failed loading levels!");
1505 return PLUGIN_OK;
1508 highscore_load(SCORE_FILE, highscores, NUM_SCORES);
1510 while(quit==0)
1512 game_init();
1513 if(quit)
1514 break;
1516 rb->lcd_clear_display();
1517 frames=1;
1519 init_snake();
1521 /*Start Game:*/
1522 game();
1525 highscore_save(SCORE_FILE, highscores, NUM_SCORES);
1527 return (quit==1) ? PLUGIN_OK : PLUGIN_USB_CONNECTED;