summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--apps/plugin.c3
-rw-r--r--apps/plugin.h5
-rw-r--r--apps/plugins/snake2.c169
3 files changed, 91 insertions, 86 deletions
diff --git a/apps/plugin.c b/apps/plugin.c
index 6c487f97fe..567b1796d4 100644
--- a/apps/plugin.c
+++ b/apps/plugin.c
@@ -244,6 +244,9 @@ static struct plugin_api rockbox_api = {
244#ifdef HAVE_LCD_CHARCELLS 244#ifdef HAVE_LCD_CHARCELLS
245 lcd_icon, 245 lcd_icon,
246#endif 246#endif
247#ifdef HAVE_LCD_BITMAP
248 lcd_puts_style,
249#endif
247}; 250};
248 251
249int plugin_load(char* plugin, void* parameter) 252int plugin_load(char* plugin, void* parameter)
diff --git a/apps/plugin.h b/apps/plugin.h
index e4ec910006..37067310ad 100644
--- a/apps/plugin.h
+++ b/apps/plugin.h
@@ -60,7 +60,7 @@
60#endif 60#endif
61 61
62/* increase this every time the api struct changes */ 62/* increase this every time the api struct changes */
63#define PLUGIN_API_VERSION 19 63#define PLUGIN_API_VERSION 20
64 64
65/* update this to latest version if a change to the api struct breaks 65/* update this to latest version if a change to the api struct breaks
66 backwards compatibility (and please take the opportunity to sort in any 66 backwards compatibility (and please take the opportunity to sort in any
@@ -280,6 +280,9 @@ struct plugin_api {
280#ifdef HAVE_LCD_CHARCELLS 280#ifdef HAVE_LCD_CHARCELLS
281 void (*lcd_icon)(int icon, bool enable); 281 void (*lcd_icon)(int icon, bool enable);
282#endif 282#endif
283#ifdef HAVE_LCD_BITMAP
284 void (*lcd_puts_style)(int x, int y, unsigned char *str, int style);
285#endif
283}; 286};
284 287
285/* defined by the plugin loader (plugin.c) */ 288/* defined by the plugin loader (plugin.c) */
diff --git a/apps/plugins/snake2.c b/apps/plugins/snake2.c
index faa89cddd1..4b51a55f34 100644
--- a/apps/plugins/snake2.c
+++ b/apps/plugins/snake2.c
@@ -33,6 +33,9 @@ Head and Tail are stored
33#define WIDTH 28 33#define WIDTH 28
34#define HEIGHT 16 34#define HEIGHT 16
35 35
36static int max_levels = 0;
37static char (*level_cache)[HEIGHT][WIDTH];
38
36/*Board itself - 2D int array*/ 39/*Board itself - 2D int array*/
37static int board[WIDTH][HEIGHT]; 40static int board[WIDTH][HEIGHT];
38/* 41/*
@@ -41,13 +44,14 @@ static int board[WIDTH][HEIGHT];
41*/ 44*/
42static int ardirectionbuffer[2]; 45static int ardirectionbuffer[2];
43static unsigned int score, hiscore = 0; 46static unsigned int score, hiscore = 0;
44static short applex; 47static int applex;
45static short appley; 48static int appley;
46static short dir; 49static int dir;
47static short frames; 50static int frames;
48static short apple; 51static int apple;
49static short level = 4, speed = 5,dead = 0, quit = 0, sillydir = 0, num_levels=0; 52static int level = 4, speed = 5,dead = 0, quit = 0;
50static short level_from_file = 1; 53static int sillydir = 0, num_levels = 0;
54static int level_from_file = 1;
51static struct plugin_api* rb; 55static struct plugin_api* rb;
52static int headx, heady, tailx, taily, applecountdown = 5; 56static int headx, heady, tailx, taily, applecountdown = 5;
53static int game_type = 0; 57static int game_type = 0;
@@ -73,37 +77,50 @@ static int game_b_level=1;
73 77
74#define LEVELS_FILE "/.rockbox/snake2.levels" 78#define LEVELS_FILE "/.rockbox/snake2.levels"
75 79
76static void set_level_count(void) 80int load_all_levels(void)
77{ 81{
78 int line_count=0; 82 int linecnt = 0;
79 int fd = 0; 83 int fd;
80 char buffer[WIDTH+2]; /* WIDTH plus CR/LF and \0 */ 84 int size;
85 char buf[64]; /* Larger than WIDTH, to allow for whitespace after the
86 lines */
87
88 /* Init the level_cache pointer and
89 calculate how many levels that will fit */
90 level_cache = rb->plugin_get_buffer(&size);
91 max_levels = size / (HEIGHT*WIDTH);
92
93 num_levels = 0;
81 94
82 if ((fd = rb->open(LEVELS_FILE, O_RDONLY)) < 0) 95 /* open file */
96 if ((fd = rb->open(LEVELS_FILE, O_RDONLY)) < 0)
83 { 97 {
84 rb->splash(0, true, "Unable to open %s", LEVELS_FILE); 98 return -1;
85 } 99 }
86 100
87 if(!(fd < 0)) 101 while(rb->read_line(fd, buf, 64))
88 { 102 {
89 while(1) 103 if(buf[0] == '-') /* Separator? */
90 { 104 {
91 int len = rb->read_line(fd, buffer, sizeof(buffer)); 105 num_levels++;
92 if(len <= 0) 106 if(num_levels > max_levels)
93 break;
94 else
95 line_count++;
96
97 if(line_count==HEIGHT)
98 { 107 {
99 num_levels++; 108 rb->splash(HZ, true, "Too many levels in file");
100 line_count=0; 109 break;
101 } 110 }
111 continue;
102 } 112 }
103 113
104 rb->close(fd); 114 rb->memcpy(level_cache[num_levels][linecnt], buf, WIDTH);
115 linecnt++;
116 if(linecnt == HEIGHT)
117 {
118 linecnt = 0;
119 }
105 } 120 }
106 121
122 rb->close(fd);
123 return 0;
107} 124}
108 125
109/* 126/*
@@ -124,51 +141,29 @@ void clear_board( void)
124 141
125int load_level( int level_number ) 142int load_level( int level_number )
126{ 143{
127 int fd = 0; 144 int x,y;
128 int x,y, len;
129 unsigned char buffer[WIDTH+2];
130
131 /* open file */
132 if ((fd = rb->open(LEVELS_FILE, O_RDONLY)) < 0)
133 {
134 return -1;
135 }
136
137 rb->lseek(fd, level_number*(WIDTH+2)*(HEIGHT+1), SEEK_SET);
138
139 clear_board(); 145 clear_board();
140 146 for(y = 0;y < HEIGHT;y++)
141 for(y=0; y < HEIGHT; y++)
142 { 147 {
143 len = rb->read_line(fd, buffer, WIDTH+2); 148 for(x = 0;x < WIDTH;x++)
144 if(len <= 0)
145 {
146 rb->close(fd);
147 return -1;
148 }
149 else
150 { 149 {
151 /*Read a line in, now add to the array:*/ 150 switch(level_cache[level_number][y][x])
152 for(x=0;x < WIDTH;x++) 151 {
153 { 152 case '1':
154 switch(buffer[x]) 153 board[x][y] = NORTH;
155 { 154 break;
156 case '1': 155
157 board[x][y] = NORTH; 156 case '2':
158 break; 157 board[x][y] = EAST;
159 158 break;
160 case '2': 159
161 board[x][y] = EAST; 160 case 'H':
162 break; 161 board[x][y] = HEAD;
163 162 break;
164 case 'H': 163 }
165 board[x][y] = HEAD; 164
166 break;
167 }
168 }
169 } 165 }
170 } 166 }
171 rb->close(fd);
172 return 1; 167 return 1;
173} 168}
174 169
@@ -219,8 +214,10 @@ void set_direction(int newdir)
219 } 214 }
220} 215}
221 216
222void init_snake( void ) 217void new_level(int level)
223{ 218{
219 load_level(level);
220
224 ardirectionbuffer[0] = -1; 221 ardirectionbuffer[0] = -1;
225 ardirectionbuffer[1] = -1; 222 ardirectionbuffer[1] = -1;
226 dir = EAST; 223 dir = EAST;
@@ -236,9 +233,14 @@ void init_snake( void )
236 board[headx-3][heady] = dir; 233 board[headx-3][heady] = dir;
237 board[headx-4][heady] = dir; 234 board[headx-4][heady] = dir;
238 num_apples_to_got=0; 235 num_apples_to_got=0;
236}
237
238void init_snake(void)
239{
239 num_apples_to_get=1; 240 num_apples_to_get=1;
240 level_from_file = 1; 241 level_from_file = 1;
241 game_b_level=1; 242 game_b_level=1;
243 new_level(level_from_file);
242} 244}
243 245
244/* 246/*
@@ -247,7 +249,7 @@ void init_snake( void )
247*/ 249*/
248void draw_apple( void ) 250void draw_apple( void )
249{ 251{
250 short x,y; 252 int x,y;
251 if (!apple) 253 if (!apple)
252 { 254 {
253 do 255 do
@@ -412,7 +414,7 @@ void draw_boundary ( void )
412*/ 414*/
413void redraw (void) 415void redraw (void)
414{ 416{
415 short x,y; 417 int x,y;
416 rb->lcd_clear_display(); 418 rb->lcd_clear_display();
417 419
418 for (x = 0; x < WIDTH; x++) 420 for (x = 0; x < WIDTH; x++)
@@ -607,12 +609,9 @@ void collision ( int x, int y )
607 num_apples_to_get+=2; 609 num_apples_to_get+=2;
608 game_b_level++; 610 game_b_level++;
609 } 611 }
610 rb->splash(0, true, "Level Completed!"); 612 rb->splash(HZ, true, "Level Completed!");
611 rb->sleep(HZ);
612 rb->lcd_clear_display(); 613 rb->lcd_clear_display();
613 num_apples_to_got=0; 614 new_level(level_from_file);
614 load_level(level_from_file);
615 init_snake();
616 redraw(); 615 redraw();
617 } 616 }
618 else 617 else
@@ -925,18 +924,19 @@ enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
925 TEST_PLUGIN_API(api); 924 TEST_PLUGIN_API(api);
926 (void)(parameter); 925 (void)(parameter);
927 rb = api; 926 rb = api;
928 set_level_count(); 927
928 /* Lets use the default font */
929 rb->lcd_setfont(FONT_SYSFIXED);
930 load_all_levels();
929 931
930 if (num_levels == 0) { 932 if (num_levels == 0) {
931 rb->splash(HZ*2, true, "Failed loading levels!"); 933 rb->splash(HZ*2, true, "Failed loading levels!");
932 return PLUGIN_OK; 934 return PLUGIN_OK;
933 } 935 }
934 936
935 /* Lets use the default font */
936 rb->lcd_setfont(FONT_SYSFIXED);
937 /*load the 1st level in*/ 937 /*load the 1st level in*/
938 load_level( level_from_file ); 938 load_level( level_from_file );
939 939
940 while(quit==0) 940 while(quit==0)
941 { 941 {
942 game_init(); 942 game_init();
@@ -945,13 +945,12 @@ enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
945 945
946 if(quit==0) 946 if(quit==0)
947 { 947 {
948 load_level( level_from_file ); 948 init_snake();
949 init_snake(); 949
950 /*Start Game:*/ 950 /*Start Game:*/
951 game(); 951 game();
952 /* Game over, reload level*/ 952
953 init_snake(); 953 clear_board();
954 load_level( level_from_file );
955 } 954 }
956 } 955 }
957 956