diff options
Diffstat (limited to 'apps/plugins')
-rw-r--r-- | apps/plugins/rockblox.c | 85 |
1 files changed, 69 insertions, 16 deletions
diff --git a/apps/plugins/rockblox.c b/apps/plugins/rockblox.c index 696ba2f84d..368ad6bc7e 100644 --- a/apps/plugins/rockblox.c +++ b/apps/plugins/rockblox.c | |||
@@ -717,6 +717,7 @@ figures[BLOCKS_NUM] = { | |||
717 | 717 | ||
718 | /* Rockbox File System only supports full filenames inc dir */ | 718 | /* Rockbox File System only supports full filenames inc dir */ |
719 | #define HIGH_SCORE PLUGIN_GAMES_DIR "/rockblox.score" | 719 | #define HIGH_SCORE PLUGIN_GAMES_DIR "/rockblox.score" |
720 | #define RESUME_FILE PLUGIN_GAMES_DIR "/rockblox.resume" | ||
720 | #define MAX_HIGH_SCORES 5 | 721 | #define MAX_HIGH_SCORES 5 |
721 | 722 | ||
722 | /* Default High Scores... */ | 723 | /* Default High Scores... */ |
@@ -728,6 +729,11 @@ static int t_rand (int range) | |||
728 | return rb->rand () % range; | 729 | return rb->rand () % range; |
729 | } | 730 | } |
730 | 731 | ||
732 | static inline void show_game_over (void) | ||
733 | { | ||
734 | rb->splash(HZ,"Game over!"); | ||
735 | } | ||
736 | |||
731 | /* init the board array to have no blocks */ | 737 | /* init the board array to have no blocks */ |
732 | static void init_board (void) | 738 | static void init_board (void) |
733 | { | 739 | { |
@@ -776,18 +782,55 @@ static void show_highscores (void) | |||
776 | } | 782 | } |
777 | #endif | 783 | #endif |
778 | 784 | ||
779 | static void init_rockblox (void) | 785 | /* Returns >0 on successful read AND if the game wasn't over, else 0 */ |
786 | static int load_resume(void) | ||
780 | { | 787 | { |
781 | highscore_update(rockblox_status.score, rockblox_status.level, Highest, | 788 | int fd; |
782 | MAX_HIGH_SCORES); | 789 | fd = rb->open(RESUME_FILE, O_RDONLY); |
790 | if (fd < 0) | ||
791 | return 0; | ||
783 | 792 | ||
784 | rockblox_status.level = 1; | 793 | if (rb->read(fd, &rockblox_status, sizeof(struct _rockblox_status)) |
785 | rockblox_status.lines = 0; | 794 | < (ssize_t)sizeof(struct _rockblox_status)) |
786 | rockblox_status.score = 0; | 795 | { |
787 | rockblox_status.nf = t_rand (BLOCKS_NUM); | 796 | rb->splash(HZ/2, "Loading Rockblox resume info failed"); |
788 | rockblox_status.gameover = false; | 797 | return 0; |
798 | } | ||
799 | |||
800 | rb->close(fd); | ||
801 | |||
802 | if (rockblox_status.gameover) | ||
803 | show_game_over(); | ||
804 | |||
805 | return !rockblox_status.gameover; | ||
806 | } | ||
807 | |||
808 | /* Returns >0 on success, else 0 */ | ||
809 | static int dump_resume(void) | ||
810 | { | ||
811 | int fd; | ||
812 | |||
813 | fd = rb->open(RESUME_FILE, O_WRONLY|O_CREAT); | ||
814 | if (fd <= 0) | ||
815 | goto fail; | ||
789 | 816 | ||
790 | init_board (); | 817 | if (rb->write(fd, &rockblox_status, sizeof(struct _rockblox_status)) |
818 | <= 0) | ||
819 | { | ||
820 | rb->close(fd); | ||
821 | goto fail; | ||
822 | } | ||
823 | rb->close(fd); | ||
824 | return 1; | ||
825 | |||
826 | fail: | ||
827 | rb->splash(HZ/2, "Writing Rockblox resume info failed"); | ||
828 | return 0; | ||
829 | } | ||
830 | static void init_rockblox (bool resume) | ||
831 | { | ||
832 | highscore_update(rockblox_status.score, rockblox_status.level, Highest, | ||
833 | MAX_HIGH_SCORES); | ||
791 | #ifdef HAVE_LCD_BITMAP | 834 | #ifdef HAVE_LCD_BITMAP |
792 | rb->lcd_bitmap (rockblox_background, 0, 0, LCD_WIDTH, LCD_HEIGHT); | 835 | rb->lcd_bitmap (rockblox_background, 0, 0, LCD_WIDTH, LCD_HEIGHT); |
793 | #else /* HAVE_LCD_CHARCELLS */ | 836 | #else /* HAVE_LCD_CHARCELLS */ |
@@ -799,6 +842,17 @@ static void init_rockblox (void) | |||
799 | pgfx_fillrect (15, 7, 2, 7); | 842 | pgfx_fillrect (15, 7, 2, 7); |
800 | pgfx_update(); | 843 | pgfx_update(); |
801 | #endif | 844 | #endif |
845 | if (!resume || !load_resume()) | ||
846 | { | ||
847 | rockblox_status.level = 1; | ||
848 | rockblox_status.lines = 0; | ||
849 | rockblox_status.score = 0; | ||
850 | rockblox_status.nf = t_rand(BLOCKS_NUM); | ||
851 | init_board (); | ||
852 | new_block (); | ||
853 | } | ||
854 | draw_next_block(); | ||
855 | |||
802 | show_details (); | 856 | show_details (); |
803 | #ifdef HIGH_SCORE_Y | 857 | #ifdef HIGH_SCORE_Y |
804 | show_highscores (); | 858 | show_highscores (); |
@@ -1110,8 +1164,6 @@ static int rockblox_loop (void) | |||
1110 | int lastbutton = BUTTON_NONE; | 1164 | int lastbutton = BUTTON_NONE; |
1111 | long next_down_tick = *rb->current_tick + level_speed(rockblox_status.level); | 1165 | long next_down_tick = *rb->current_tick + level_speed(rockblox_status.level); |
1112 | 1166 | ||
1113 | new_block (); | ||
1114 | |||
1115 | while (1) { | 1167 | while (1) { |
1116 | #ifdef HAS_BUTTON_HOLD | 1168 | #ifdef HAS_BUTTON_HOLD |
1117 | if (rb->button_hold ()) { | 1169 | if (rb->button_hold ()) { |
@@ -1204,8 +1256,7 @@ static int rockblox_loop (void) | |||
1204 | #ifdef ROCKBLOX_RESTART | 1256 | #ifdef ROCKBLOX_RESTART |
1205 | case ROCKBLOX_RESTART: | 1257 | case ROCKBLOX_RESTART: |
1206 | rb->splash (HZ * 1, "Restarting..."); | 1258 | rb->splash (HZ * 1, "Restarting..."); |
1207 | init_rockblox (); | 1259 | init_rockblox (false); |
1208 | new_block (); | ||
1209 | break; | 1260 | break; |
1210 | #endif | 1261 | #endif |
1211 | 1262 | ||
@@ -1256,8 +1307,8 @@ static int rockblox_loop (void) | |||
1256 | #if LCD_DEPTH >= 2 | 1307 | #if LCD_DEPTH >= 2 |
1257 | rb->lcd_set_foreground (LCD_BLACK); | 1308 | rb->lcd_set_foreground (LCD_BLACK); |
1258 | #endif | 1309 | #endif |
1259 | rb->splash (HZ * 2, "Game Over"); | 1310 | show_game_over(); |
1260 | init_rockblox (); | 1311 | init_rockblox (false); |
1261 | } | 1312 | } |
1262 | 1313 | ||
1263 | refresh_board (); | 1314 | refresh_board (); |
@@ -1293,7 +1344,7 @@ enum plugin_status plugin_start (const void *parameter) | |||
1293 | /* Turn off backlight timeout */ | 1344 | /* Turn off backlight timeout */ |
1294 | backlight_force_on(); /* backlight control in lib/helper.c */ | 1345 | backlight_force_on(); /* backlight control in lib/helper.c */ |
1295 | 1346 | ||
1296 | init_rockblox (); | 1347 | init_rockblox (true); |
1297 | ret = rockblox_loop (); | 1348 | ret = rockblox_loop (); |
1298 | 1349 | ||
1299 | #ifndef HAVE_LCD_BITMAP | 1350 | #ifndef HAVE_LCD_BITMAP |
@@ -1303,5 +1354,7 @@ enum plugin_status plugin_start (const void *parameter) | |||
1303 | highscore_save(HIGH_SCORE,Highest,MAX_HIGH_SCORES); | 1354 | highscore_save(HIGH_SCORE,Highest,MAX_HIGH_SCORES); |
1304 | backlight_use_settings(); /* backlight control in lib/helper.c */ | 1355 | backlight_use_settings(); /* backlight control in lib/helper.c */ |
1305 | 1356 | ||
1357 | dump_resume(); | ||
1358 | |||
1306 | return ret; | 1359 | return ret; |
1307 | } | 1360 | } |