summaryrefslogtreecommitdiff
path: root/apps
diff options
context:
space:
mode:
authorJohannes Schwarz <ubuntuxer@rockbox.org>2009-11-17 06:57:36 +0000
committerJohannes Schwarz <ubuntuxer@rockbox.org>2009-11-17 06:57:36 +0000
commit8826115a65c89dcd6cc03230ff5cd9573d1430cd (patch)
tree6fc22eb12957635c1d4a69aa35f15c5336a851df /apps
parenta052102cfb861aae6c8e683ae01bbace9620235c (diff)
downloadrockbox-8826115a65c89dcd6cc03230ff5cd9573d1430cd.tar.gz
rockbox-8826115a65c89dcd6cc03230ff5cd9573d1430cd.zip
FS#10193 - Add a standart menu to rockblox and clean up the code
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@23653 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps')
-rw-r--r--apps/plugins/rockblox.c173
1 files changed, 143 insertions, 30 deletions
diff --git a/apps/plugins/rockblox.c b/apps/plugins/rockblox.c
index 0314a7c516..e530a16fc1 100644
--- a/apps/plugins/rockblox.c
+++ b/apps/plugins/rockblox.c
@@ -21,9 +21,11 @@
21 * 21 *
22 ****************************************************************************/ 22 ****************************************************************************/
23#include "plugin.h" 23#include "plugin.h"
24#include "lib/display_text.h"
25#include "lib/helper.h"
24#include "lib/highscore.h" 26#include "lib/highscore.h"
27#include "lib/playback_control.h"
25#include "lib/playergfx.h" 28#include "lib/playergfx.h"
26#include "lib/helper.h"
27 29
28PLUGIN_HEADER 30PLUGIN_HEADER
29 31
@@ -163,7 +165,7 @@ PLUGIN_HEADER
163#define ROCKBLOX_DROP BUTTON_SELECT 165#define ROCKBLOX_DROP BUTTON_SELECT
164#define ROCKBLOX_RESTART BUTTON_HOME 166#define ROCKBLOX_RESTART BUTTON_HOME
165 167
166#elif CONFIG_KEYPAD == SANSA_M200_PAD 168#elif CONFIG_KEYPAD == SANSA_M200_PAD
167 169
168#define ROCKBLOX_OFF BUTTON_POWER 170#define ROCKBLOX_OFF BUTTON_POWER
169#define ROCKBLOX_ROTATE_RIGHT BUTTON_UP 171#define ROCKBLOX_ROTATE_RIGHT BUTTON_UP
@@ -723,6 +725,8 @@ figures[BLOCKS_NUM] = {
723 {0, 0, 0, 1} 725 {0, 0, 0, 1}
724 } 726 }
725}; 727};
728bool resume = false;
729bool resume_file = false;
726 730
727/* Rockbox File System only supports full filenames inc dir */ 731/* Rockbox File System only supports full filenames inc dir */
728#define HIGH_SCORE PLUGIN_GAMES_DIR "/rockblox.score" 732#define HIGH_SCORE PLUGIN_GAMES_DIR "/rockblox.score"
@@ -792,31 +796,30 @@ static void show_highscores (void)
792} 796}
793#endif 797#endif
794 798
795/* Returns >0 on successful read AND if the game wasn't over, else 0 */ 799static void load_game(void)
796static int load_resume(void)
797{ 800{
798 int fd; 801 int fd;
802
803 resume = false;
804
799 fd = rb->open(RESUME_FILE, O_RDONLY); 805 fd = rb->open(RESUME_FILE, O_RDONLY);
800 if (fd < 0) 806 if (fd < 0) return;
801 return 0;
802 807
803 if (rb->read(fd, &rockblox_status, sizeof(struct _rockblox_status)) 808 if (rb->read(fd, &rockblox_status, sizeof(struct _rockblox_status))
804 < (ssize_t)sizeof(struct _rockblox_status)) 809 < (ssize_t)sizeof(struct _rockblox_status))
805 { 810 {
806 rb->splash(HZ/2, "Loading Rockblox resume info failed"); 811 rb->splash(HZ/2, "Loading Rockblox resume info failed");
807 return 0; 812 return;
813 } else {
814 resume = true;
808 } 815 }
809 816
810 rb->close(fd); 817 rb->close(fd);
811 818
812 if (rockblox_status.gameover) 819 return;
813 show_game_over();
814
815 return !rockblox_status.gameover;
816} 820}
817 821
818/* Returns >0 on success, else 0 */ 822static void dump_resume(void)
819static int dump_resume(void)
820{ 823{
821 int fd; 824 int fd;
822 825
@@ -831,25 +834,23 @@ static int dump_resume(void)
831 goto fail; 834 goto fail;
832 } 835 }
833 rb->close(fd); 836 rb->close(fd);
834 return 1; 837 return;
835 838
836fail: 839fail:
837 rb->splash(HZ/2, "Writing Rockblox resume info failed"); 840 rb->splash(HZ/2, "Writing Rockblox resume info failed");
838 return 0; 841 return;
839} 842}
843
840static void init_rockblox (bool resume) 844static void init_rockblox (bool resume)
841{ 845{
842 char score_name[50]; 846 char score_name[50];
843 struct tm* tm; 847 struct tm* tm;
844 848
845 tm = rb->get_time(); 849 tm = rb->get_time();
846 rb->snprintf(score_name, sizeof(score_name), "%04d%02d%02d %02d%02d%02d", 850 rb->snprintf(score_name, sizeof(score_name), "%04d%02d%02d %02d%02d%02d",
847 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, 851 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
848 tm->tm_hour, tm->tm_min, tm->tm_sec); 852 tm->tm_hour, tm->tm_min, tm->tm_sec);
849 853
850 highscore_update(rockblox_status.score, rockblox_status.level,
851 score_name, highest, MAX_HIGH_SCORES);
852
853#ifdef HAVE_LCD_BITMAP 854#ifdef HAVE_LCD_BITMAP
854 rb->lcd_bitmap (rockblox_background, 0, 0, LCD_WIDTH, LCD_HEIGHT); 855 rb->lcd_bitmap (rockblox_background, 0, 0, LCD_WIDTH, LCD_HEIGHT);
855#else /* HAVE_LCD_CHARCELLS */ 856#else /* HAVE_LCD_CHARCELLS */
@@ -861,7 +862,7 @@ static void init_rockblox (bool resume)
861 pgfx_fillrect (15, 7, 2, 7); 862 pgfx_fillrect (15, 7, 2, 7);
862 pgfx_update(); 863 pgfx_update();
863#endif 864#endif
864 if (!resume || !load_resume()) 865 if (!resume)
865 { 866 {
866 rockblox_status.level = 1; 867 rockblox_status.level = 1;
867 rockblox_status.lines = 0; 868 rockblox_status.lines = 0;
@@ -1177,12 +1178,113 @@ static void move_down (void)
1177 move_block (0, 1, rockblox_status.co); 1178 move_block (0, 1, rockblox_status.co);
1178} 1179}
1179 1180
1181static bool rockblox_help(void)
1182{
1183 int button;
1184
1185#define WORDS (sizeof help_text / sizeof (char*))
1186 char *help_text[] = {
1187 "Rockblox", "", "Aim", "", "Make", "the", "falling", "blocks", "of", "different",
1188 "shapes", "form", "full", "rows.", "Whenever", "a", "row", "is", "completed,", "it",
1189 "will", "be", "cleared", "away", "and", "you", "gain", "points."
1190 };
1191
1192 static struct style_text formation[]={
1193 { 0, TEXT_CENTER|TEXT_UNDERLINE },
1194 { 2, C_RED },
1195 { -1, 0 }
1196 };
1197#ifdef HAVE_LCD_BITMAP
1198 rb->lcd_setfont(FONT_UI);
1199#endif
1200#ifdef HAVE_LCD_COLOR
1201 rb->lcd_set_background(LCD_BLACK);
1202 rb->lcd_set_foreground(LCD_WHITE);
1203#endif
1204 if (display_text(WORDS, help_text, formation, NULL))
1205 return true;
1206 do {
1207 button = rb->button_get(true);
1208 if ( rb->default_event_handler( button ) == SYS_USB_CONNECTED )
1209 return true;
1210 } while( ( button == BUTTON_NONE )
1211 || ( button & (BUTTON_REL|BUTTON_REPEAT) ) );
1212#ifdef HAVE_LCD_BITMAP
1213 rb->lcd_setfont(FONT_SYSFIXED);
1214#endif
1215 return false;
1216}
1217
1218static int rockblox_menu_cb(int action, const struct menu_item_ex *this_item)
1219{
1220 int i = ((intptr_t)this_item);
1221 if(action == ACTION_REQUEST_MENUITEM
1222 && !resume && (i==0 || i==5))
1223 return ACTION_EXIT_MENUITEM;
1224 return action;
1225}
1226
1227static int rockblox_menu(void)
1228{
1229 int selected = 0;
1230
1231 MENUITEM_STRINGLIST(main_menu, "Rockblox Menu", rockblox_menu_cb,
1232 "Resume Game", "Start New Game",
1233 "Help", "High Scores", "Playback Control",
1234 "Quit without Saving", "Quit");
1235
1236 rb->button_clear_queue();
1237 while (true) {
1238 switch (rb->do_menu(&main_menu, &selected, NULL, false)) {
1239 case 0:
1240 if(resume_file)
1241 rb->remove(RESUME_FILE);
1242 init_rockblox(true);
1243 return 0;
1244 case 1:
1245 init_rockblox(false);
1246 return 0;
1247 case 2:
1248 if (rockblox_help())
1249 return 1;
1250 break;
1251 case 3:
1252 highscore_show(MAX_HIGH_SCORES, highest, MAX_HIGH_SCORES, true);
1253 break;
1254 case 4:
1255 if (playback_control(NULL))
1256 return 1;
1257 break;
1258 case 5:
1259 return 1;
1260 case 6:
1261 if (resume) {
1262 rb->splash(HZ*1, "Saving game ...");
1263 dump_resume();
1264 }
1265 return 1;
1266 case MENU_ATTACHED_USB:
1267 return 1;
1268 default:
1269 return 1;
1270 break;
1271 }
1272 }
1273}
1274
1275
1180static int rockblox_loop (void) 1276static int rockblox_loop (void)
1181{ 1277{
1182 int button; 1278 int button;
1183 int lastbutton = BUTTON_NONE; 1279 int lastbutton = BUTTON_NONE;
1184 long next_down_tick = *rb->current_tick + level_speed(rockblox_status.level); 1280 long next_down_tick = *rb->current_tick + level_speed(rockblox_status.level);
1185 1281
1282 if (rockblox_menu()) {
1283 return 1;
1284 }
1285 resume = false;
1286 resume_file = false;
1287
1186 while (1) { 1288 while (1) {
1187#ifdef HAS_BUTTON_HOLD 1289#ifdef HAS_BUTTON_HOLD
1188 if (rb->button_hold ()) { 1290 if (rb->button_hold ()) {
@@ -1216,7 +1318,9 @@ static int rockblox_loop (void)
1216 if (lastbutton != ROCKBLOX_OFF_PRE) 1318 if (lastbutton != ROCKBLOX_OFF_PRE)
1217 break; 1319 break;
1218#endif 1320#endif
1219 return PLUGIN_OK; 1321 resume = true;
1322 return 0;
1323 break;
1220 1324
1221#if defined(ROCKBLOX_ROTATE) 1325#if defined(ROCKBLOX_ROTATE)
1222 case ROCKBLOX_ROTATE: 1326 case ROCKBLOX_ROTATE:
@@ -1331,18 +1435,18 @@ static int rockblox_loop (void)
1331 rb->lcd_set_foreground (LCD_BLACK); 1435 rb->lcd_set_foreground (LCD_BLACK);
1332#endif 1436#endif
1333 show_game_over(); 1437 show_game_over();
1334 init_rockblox (false); 1438 resume = false;
1439 return 0;
1335 } 1440 }
1336 1441
1337 refresh_board (); 1442 refresh_board ();
1338 } 1443 }
1339 1444
1340 return PLUGIN_OK; 1445 return 0;
1341} 1446}
1342 1447
1343enum plugin_status plugin_start (const void *parameter) 1448enum plugin_status plugin_start (const void *parameter)
1344{ 1449{
1345 int ret;
1346 1450
1347 (void) parameter; 1451 (void) parameter;
1348 1452
@@ -1366,9 +1470,20 @@ enum plugin_status plugin_start (const void *parameter)
1366#endif 1470#endif
1367 /* Turn off backlight timeout */ 1471 /* Turn off backlight timeout */
1368 backlight_force_on(); /* backlight control in lib/helper.c */ 1472 backlight_force_on(); /* backlight control in lib/helper.c */
1369 1473 load_game();
1370 init_rockblox (true); 1474 resume_file = resume;
1371 ret = rockblox_loop (); 1475 while(!rockblox_loop()) {
1476 if(!resume) {
1477 int position = highscore_update(rockblox_status.score, rockblox_status.level, "", highest,
1478 MAX_HIGH_SCORES);
1479 if (position == 0) {
1480 rb->splash(HZ*2, "New High Score");
1481 }
1482 if (position != -1) {
1483 highscore_show(position, highest, MAX_HIGH_SCORES, true);
1484 }
1485 }
1486 }
1372 1487
1373#ifndef HAVE_LCD_BITMAP 1488#ifndef HAVE_LCD_BITMAP
1374 pgfx_release(); 1489 pgfx_release();
@@ -1377,7 +1492,5 @@ enum plugin_status plugin_start (const void *parameter)
1377 highscore_save(HIGH_SCORE, highest, MAX_HIGH_SCORES); 1492 highscore_save(HIGH_SCORE, highest, MAX_HIGH_SCORES);
1378 backlight_use_settings(); /* backlight control in lib/helper.c */ 1493 backlight_use_settings(); /* backlight control in lib/helper.c */
1379 1494
1380 dump_resume(); 1495 return PLUGIN_OK;
1381
1382 return ret;
1383} 1496}