diff options
Diffstat (limited to 'apps')
-rw-r--r-- | apps/plugins/pegbox.c | 572 |
1 files changed, 162 insertions, 410 deletions
diff --git a/apps/plugins/pegbox.c b/apps/plugins/pegbox.c index eb53eca8b5..e9c233c6bd 100644 --- a/apps/plugins/pegbox.c +++ b/apps/plugins/pegbox.c | |||
@@ -19,29 +19,16 @@ | |||
19 | * | 19 | * |
20 | ****************************************************************************/ | 20 | ****************************************************************************/ |
21 | #include "plugin.h" | 21 | #include "plugin.h" |
22 | #include "lib/configfile.h" | ||
22 | #include "lib/display_text.h" | 23 | #include "lib/display_text.h" |
24 | #include "lib/playback_control.h" | ||
23 | 25 | ||
24 | #include "pluginbitmaps/pegbox_header.h" | 26 | #include "pluginbitmaps/pegbox_header.h" |
25 | #include "pluginbitmaps/pegbox_pieces.h" | 27 | #include "pluginbitmaps/pegbox_pieces.h" |
26 | 28 | ||
27 | #if LCD_HEIGHT >= 80 /* enough space for a graphical menu */ | ||
28 | #include "pluginbitmaps/pegbox_menu_top.h" | ||
29 | #include "pluginbitmaps/pegbox_menu_items.h" | ||
30 | #define MENU_X (LCD_WIDTH-BMPWIDTH_pegbox_menu_items)/2 | ||
31 | #define MENU_Y BMPHEIGHT_pegbox_menu_top | ||
32 | #define ITEM_WIDTH BMPWIDTH_pegbox_menu_items | ||
33 | #define ITEM_HEIGHT (BMPHEIGHT_pegbox_menu_items/9) | ||
34 | #endif | ||
35 | |||
36 | PLUGIN_HEADER | 29 | PLUGIN_HEADER |
37 | 30 | ||
38 | /* final game return status */ | 31 | #define CONFIG_FILE_NAME "pegbox.cfg" |
39 | #define PB_END 3 | ||
40 | #define PB_USB 2 | ||
41 | #define PB_QUIT 1 | ||
42 | |||
43 | #define DATA_FILE PLUGIN_GAMES_DIR "/pegbox.data" | ||
44 | #define SAVE_FILE PLUGIN_GAMES_DIR "/pegbox.save" | ||
45 | 32 | ||
46 | #define ROWS 8 /* Number of rows on each board */ | 33 | #define ROWS 8 /* Number of rows on each board */ |
47 | #define COLS 12 /* Number of columns on each board */ | 34 | #define COLS 12 /* Number of columns on each board */ |
@@ -530,26 +517,6 @@ PLUGIN_HEADER | |||
530 | 517 | ||
531 | #ifdef HAVE_TOUCHSCREEN | 518 | #ifdef HAVE_TOUCHSCREEN |
532 | #include "lib/touchscreen.h" | 519 | #include "lib/touchscreen.h" |
533 | |||
534 | static struct ts_mapping main_menu_items[5] = | ||
535 | { | ||
536 | {MENU_X, MENU_Y, ITEM_WIDTH, ITEM_HEIGHT}, | ||
537 | {MENU_X, MENU_Y+ITEM_HEIGHT, ITEM_WIDTH, ITEM_HEIGHT}, | ||
538 | {MENU_X, MENU_Y+ITEM_HEIGHT*2, ITEM_WIDTH, ITEM_HEIGHT}, | ||
539 | {MENU_X, MENU_Y+ITEM_HEIGHT*3, ITEM_WIDTH, ITEM_HEIGHT}, | ||
540 | { | ||
541 | #if (LCD_WIDTH >= 138) && (LCD_HEIGHT > 110) | ||
542 | 0, MENU_Y+4*ITEM_HEIGHT+8, SYSFONT_WIDTH*28, SYSFONT_HEIGHT | ||
543 | #elif LCD_WIDTH > 112 | ||
544 | 0, LCD_HEIGHT - 8, SYSFONT_WIDTH*28, SYSFONT_HEIGHT | ||
545 | #else | ||
546 | #error "Touchscreen isn't supported on non-bitmap screens!" | ||
547 | #endif | ||
548 | } | ||
549 | |||
550 | }; | ||
551 | static struct ts_mappings main_menu = {main_menu_items, 5}; | ||
552 | |||
553 | static struct ts_raster pegbox_raster = | 520 | static struct ts_raster pegbox_raster = |
554 | { BOARD_X, BOARD_Y, COLS*PIECE_WIDTH, ROWS*PIECE_HEIGHT, | 521 | { BOARD_X, BOARD_Y, COLS*PIECE_WIDTH, ROWS*PIECE_HEIGHT, |
555 | PIECE_WIDTH, PIECE_HEIGHT }; | 522 | PIECE_WIDTH, PIECE_HEIGHT }; |
@@ -557,13 +524,13 @@ static struct ts_raster_button_mapping pegbox_raster_btn = | |||
557 | { &pegbox_raster, false, false, true, false, true, {0, 0}, 0, 0, 0 }; | 524 | { &pegbox_raster, false, false, true, false, true, {0, 0}, 0, 0, 0 }; |
558 | #endif | 525 | #endif |
559 | 526 | ||
527 | |||
560 | struct game_context { | 528 | struct game_context { |
561 | unsigned int level; | 529 | unsigned int level; |
562 | unsigned int highlevel; | 530 | unsigned int highlevel; |
563 | signed int player_row; | 531 | signed int player_row; |
564 | signed int player_col; | 532 | signed int player_col; |
565 | unsigned int num_left; | 533 | unsigned int num_left; |
566 | bool save_exist; | ||
567 | unsigned int playboard[ROWS][COLS]; | 534 | unsigned int playboard[ROWS][COLS]; |
568 | }; | 535 | }; |
569 | 536 | ||
@@ -719,11 +686,11 @@ char levels[NUM_LEVELS][ROWS][COLS] = { | |||
719 | {0, 0, 4, 6, 0, 6, 0, 6, 0, 6, 0, 1,}} | 686 | {0, 0, 4, 6, 0, 6, 0, 6, 0, 6, 0, 1,}} |
720 | }; | 687 | }; |
721 | 688 | ||
722 | 689 | /*********************************************************************** | |
723 | /***************************************************************************** | 690 | * pegbox_draw_board() draws the game's current level. |
724 | * draw_board() draws the game's current level. | 691 | ************************************************************************/ |
725 | ******************************************************************************/ | 692 | static void pegbox_draw_board(struct game_context* pb) |
726 | static void draw_board(struct game_context* pb) { | 693 | { |
727 | unsigned int r, c, type; | 694 | unsigned int r, c, type; |
728 | pb->num_left = 0; | 695 | pb->num_left = 0; |
729 | char str[5]; | 696 | char str[5]; |
@@ -795,10 +762,11 @@ static void draw_board(struct game_context* pb) { | |||
795 | } | 762 | } |
796 | 763 | ||
797 | /***************************************************************************** | 764 | /***************************************************************************** |
798 | * load_level() loads the player's current level from the array and sets the | 765 | * pegbox_load_level() loads the player's current level from the array and sets the |
799 | * player's position. | 766 | * player's position. |
800 | ******************************************************************************/ | 767 | ******************************************************************************/ |
801 | static void load_level(struct game_context* pb) { | 768 | static void pegbox_load_level(struct game_context* pb) |
769 | { | ||
802 | int r, c; | 770 | int r, c; |
803 | 771 | ||
804 | for(r = 0; r < ROWS; r++) | 772 | for(r = 0; r < ROWS; r++) |
@@ -807,18 +775,19 @@ static void load_level(struct game_context* pb) { | |||
807 | } | 775 | } |
808 | 776 | ||
809 | /***************************************************************************** | 777 | /***************************************************************************** |
810 | * new_piece() creates a new piece at a specified location. The player | 778 | * pegbox_new_piece() creates a new piece at a specified location. The player |
811 | * navigates through the pieces and selects one. | 779 | * navigates through the pieces and selects one. |
812 | ******************************************************************************/ | 780 | ******************************************************************************/ |
813 | static void new_piece(struct game_context* pb, unsigned int x_loc, | 781 | static void pegbox_new_piece(struct game_context* pb, unsigned int x_loc, |
814 | unsigned int y_loc) { | 782 | unsigned int y_loc) |
783 | { | ||
815 | int button; | 784 | int button; |
816 | bool exit = false; | 785 | bool exit = false; |
817 | 786 | ||
818 | pb->playboard[x_loc][y_loc] = TRIANGLE; | 787 | pb->playboard[x_loc][y_loc] = TRIANGLE; |
819 | 788 | ||
820 | while (!exit) { | 789 | while (!exit) { |
821 | draw_board(pb); | 790 | pegbox_draw_board(pb); |
822 | button = rb->button_get(true); | 791 | button = rb->button_get(true); |
823 | #ifdef HAVE_TOUCHSCREEN | 792 | #ifdef HAVE_TOUCHSCREEN |
824 | if(button & BUTTON_TOUCHSCREEN) | 793 | if(button & BUTTON_TOUCHSCREEN) |
@@ -868,17 +837,17 @@ static void new_piece(struct game_context* pb, unsigned int x_loc, | |||
868 | pb->playboard[x_loc][y_loc] = CIRCLE; | 837 | pb->playboard[x_loc][y_loc] = CIRCLE; |
869 | break; | 838 | break; |
870 | case PEGBOX_SAVE: | 839 | case PEGBOX_SAVE: |
871 | exit = true; | 840 | exit=true; |
872 | break; | ||
873 | } | 841 | } |
874 | } | 842 | } |
875 | } | 843 | } |
876 | 844 | ||
877 | /***************************************************************************** | 845 | /***************************************************************************** |
878 | * move_player() moves the player and pieces and updates the board accordingly. | 846 | * pegbox_move_player() moves the player and pieces and updates the board accordingly. |
879 | ******************************************************************************/ | 847 | ******************************************************************************/ |
880 | static void move_player(struct game_context* pb, signed int x_dir, | 848 | static void pegbox_move_player(struct game_context* pb, signed int x_dir, |
881 | signed int y_dir) { | 849 | signed int y_dir) |
850 | { | ||
882 | unsigned int type1, type2; | 851 | unsigned int type1, type2; |
883 | signed int r,c; | 852 | signed int r,c; |
884 | 853 | ||
@@ -902,9 +871,9 @@ static void move_player(struct game_context* pb, signed int x_dir, | |||
902 | pb->player_col += x_dir; | 871 | pb->player_col += x_dir; |
903 | 872 | ||
904 | if (type1 == HOLE) { | 873 | if (type1 == HOLE) { |
905 | draw_board(pb); | 874 | pegbox_draw_board(pb); |
906 | rb->splash(HZ*2, "You fell down a hole!"); | 875 | rb->splash(HZ*2, "You fell down a hole!"); |
907 | load_level(pb); | 876 | pegbox_load_level(pb); |
908 | } | 877 | } |
909 | else if (type1 == SPACE) | 878 | else if (type1 == SPACE) |
910 | pb->playboard[r][c] = PLAYER; | 879 | pb->playboard[r][c] = PLAYER; |
@@ -915,7 +884,7 @@ static void move_player(struct game_context* pb, signed int x_dir, | |||
915 | pb->playboard[r+y_dir][c+x_dir] = WALL; | 884 | pb->playboard[r+y_dir][c+x_dir] = WALL; |
916 | else if (type1 == CROSS) { | 885 | else if (type1 == CROSS) { |
917 | pb->playboard[r][c] = SPACE; | 886 | pb->playboard[r][c] = SPACE; |
918 | new_piece(pb, r+y_dir, c+x_dir); | 887 | pegbox_new_piece(pb, r+y_dir, c+x_dir); |
919 | pb->playboard[r][c] = PLAYER; | 888 | pb->playboard[r][c] = PLAYER; |
920 | } | 889 | } |
921 | else | 890 | else |
@@ -929,98 +898,11 @@ static void move_player(struct game_context* pb, signed int x_dir, | |||
929 | } | 898 | } |
930 | else { | 899 | else { |
931 | rb->splash(HZ*2, "Illegal Move!"); | 900 | rb->splash(HZ*2, "Illegal Move!"); |
932 | load_level(pb); | 901 | pegbox_load_level(pb); |
933 | } | 902 | } |
934 | } | 903 | } |
935 | 904 | ||
936 | draw_board(pb); | 905 | pegbox_draw_board(pb); |
937 | } | ||
938 | |||
939 | /***************************************************************************** | ||
940 | * pegbox_loadgame() loads the saved game and returns load success. | ||
941 | ******************************************************************************/ | ||
942 | static bool pegbox_loadgame(struct game_context* pb) { | ||
943 | signed int fd; | ||
944 | bool loaded = false; | ||
945 | |||
946 | /* open game file */ | ||
947 | fd = rb->open(SAVE_FILE, O_RDONLY); | ||
948 | if(fd < 0) return loaded; | ||
949 | |||
950 | /* read in saved game */ | ||
951 | while(true) { | ||
952 | if(rb->read(fd, &pb->level, sizeof(pb->level)) <= 0) break; | ||
953 | if(rb->read(fd, &pb->playboard, sizeof(pb->playboard)) <= 0) | ||
954 | { | ||
955 | loaded = true; | ||
956 | break; | ||
957 | } | ||
958 | break; | ||
959 | } | ||
960 | |||
961 | rb->close(fd); | ||
962 | return loaded; | ||
963 | } | ||
964 | |||
965 | /***************************************************************************** | ||
966 | * pegbox_savegame() saves the current game state. | ||
967 | ******************************************************************************/ | ||
968 | static void pegbox_savegame(struct game_context* pb) { | ||
969 | unsigned int fd; | ||
970 | |||
971 | /* write out the game state to the save file */ | ||
972 | fd = rb->open(SAVE_FILE, O_WRONLY|O_CREAT); | ||
973 | rb->write(fd, &pb->level, sizeof(pb->level)); | ||
974 | rb->write(fd, &pb->playboard, sizeof(pb->playboard)); | ||
975 | rb->close(fd); | ||
976 | } | ||
977 | |||
978 | /***************************************************************************** | ||
979 | * pegbox_loaddata() loads the level and highlevel and returns load success. | ||
980 | ******************************************************************************/ | ||
981 | static void pegbox_loaddata(struct game_context* pb) { | ||
982 | signed int fd; | ||
983 | |||
984 | /* open game file */ | ||
985 | fd = rb->open(DATA_FILE, O_RDONLY); | ||
986 | if(fd < 0) { | ||
987 | pb->level = 1; | ||
988 | pb->highlevel = 1; | ||
989 | return; | ||
990 | } | ||
991 | |||
992 | /* read in saved game */ | ||
993 | while(true) { | ||
994 | if(rb->read(fd, &pb->level, sizeof(pb->level)) <= 0) break; | ||
995 | if(rb->read(fd, &pb->highlevel, sizeof(pb->highlevel)) <= 0) break; | ||
996 | break; | ||
997 | } | ||
998 | |||
999 | rb->close(fd); | ||
1000 | return; | ||
1001 | } | ||
1002 | |||
1003 | /***************************************************************************** | ||
1004 | * pegbox_savedata() saves the level and highlevel. | ||
1005 | ******************************************************************************/ | ||
1006 | static void pegbox_savedata(struct game_context* pb) { | ||
1007 | unsigned int fd; | ||
1008 | |||
1009 | /* write out the game state to the save file */ | ||
1010 | fd = rb->open(DATA_FILE, O_WRONLY|O_CREAT); | ||
1011 | rb->write(fd, &pb->level, sizeof(pb->level)); | ||
1012 | rb->write(fd, &pb->highlevel, sizeof(pb->highlevel)); | ||
1013 | rb->close(fd); | ||
1014 | } | ||
1015 | |||
1016 | /***************************************************************************** | ||
1017 | * pegbox_callback() is the default event handler callback which is called | ||
1018 | * on usb connect and shutdown. | ||
1019 | ******************************************************************************/ | ||
1020 | static void pegbox_callback(void* param) { | ||
1021 | struct game_context* pb = (struct game_context*) param; | ||
1022 | rb->splash(HZ, "Saving data..."); | ||
1023 | pegbox_savedata(pb); | ||
1024 | } | 906 | } |
1025 | 907 | ||
1026 | /*********************************************************************** | 908 | /*********************************************************************** |
@@ -1065,216 +947,104 @@ static bool pegbox_help(void) | |||
1065 | return false; | 947 | return false; |
1066 | } | 948 | } |
1067 | 949 | ||
1068 | /***************************************************************************** | 950 | /*********************************************************************** |
1069 | * pegbox_menu() is the initial menu at the start of the game. | 951 | * pegbox_menu() is the game menu |
1070 | ******************************************************************************/ | 952 | ************************************************************************/ |
1071 | static unsigned int pegbox_menu(struct game_context* pb) { | 953 | static unsigned int pegbox_menu(struct game_context* pb, bool ingame) |
1072 | int button; | 954 | { |
1073 | char str[30]; | 955 | rb->button_clear_queue(); |
1074 | unsigned int startlevel = 1, loc = 0; | 956 | int choice = 0; |
1075 | bool breakout = false, can_resume = false; | ||
1076 | 957 | ||
1077 | if (pb->num_left > 0 || pb->save_exist) | 958 | if (ingame) { |
1078 | can_resume = true; | 959 | MENUITEM_STRINGLIST (main_menu, "Pegbox Menu", NULL, |
1079 | 960 | "Resume Game", | |
1080 | while(!breakout){ | 961 | "Restart Level", |
1081 | #if LCD_HEIGHT >= 80 | 962 | "Select Level", |
1082 | rb->lcd_clear_display(); | 963 | "Help", |
1083 | rb->lcd_bitmap(pegbox_menu_top,0,0,LCD_WIDTH, BMPHEIGHT_pegbox_menu_top); | 964 | "Playback Control", |
1084 | 965 | "Quit"); | |
1085 | /* menu bitmaps */ | 966 | |
1086 | if (loc == 0) { | 967 | while (true) { |
1087 | rb->lcd_bitmap_part(pegbox_menu_items, 0, ITEM_HEIGHT, ITEM_WIDTH, | 968 | switch (rb->do_menu(&main_menu, &choice, NULL, false)) { |
1088 | MENU_X, MENU_Y, ITEM_WIDTH, ITEM_HEIGHT); | 969 | case 0: |
1089 | } | 970 | pegbox_draw_board(pb); |
1090 | else { | 971 | return 0; |
1091 | rb->lcd_bitmap_part(pegbox_menu_items, 0, 0, ITEM_WIDTH, | 972 | case 1: |
1092 | MENU_X, MENU_Y, ITEM_WIDTH, ITEM_HEIGHT); | 973 | pegbox_load_level(pb); |
1093 | } | 974 | pegbox_draw_board(pb); |
1094 | if (can_resume) { | 975 | return 0; |
1095 | if (loc == 1) { | 976 | case 2: |
1096 | rb->lcd_bitmap_part(pegbox_menu_items, 0, ITEM_HEIGHT*3, ITEM_WIDTH, | 977 | rb->set_int("Select Level", "", UNIT_INT, |
1097 | MENU_X, MENU_Y+ITEM_HEIGHT, ITEM_WIDTH, ITEM_HEIGHT); | 978 | &pb->level, NULL, 1, 1, |
1098 | } | 979 | pb->highlevel, NULL); |
1099 | else { | 980 | break; |
1100 | rb->lcd_bitmap_part(pegbox_menu_items, 0, ITEM_HEIGHT*2, ITEM_WIDTH, | 981 | case 3: |
1101 | MENU_X, MENU_Y+ITEM_HEIGHT, ITEM_WIDTH, ITEM_HEIGHT); | 982 | if (pegbox_help()==PLUGIN_USB_CONNECTED) |
983 | return 1; | ||
984 | break; | ||
985 | case 4: | ||
986 | playback_control(NULL); | ||
987 | break; | ||
988 | case 5: | ||
989 | return 1; | ||
990 | case MENU_ATTACHED_USB: | ||
991 | return 1; | ||
992 | default: | ||
993 | break; | ||
1102 | } | 994 | } |
1103 | } | 995 | } |
1104 | else { | 996 | } else { |
1105 | rb->lcd_bitmap_part(pegbox_menu_items, 0, ITEM_HEIGHT*4, ITEM_WIDTH, | 997 | MENUITEM_STRINGLIST (main_menu, "Pegbox Menu", NULL, |
1106 | MENU_X, MENU_Y+ITEM_HEIGHT, ITEM_WIDTH, ITEM_HEIGHT); | 998 | "Start Game", |
1107 | } | 999 | "Select Level", |
1108 | 1000 | "Help", | |
1109 | if (loc==2) { | 1001 | "Playback Control", |
1110 | rb->lcd_bitmap_part(pegbox_menu_items, 0, ITEM_HEIGHT*6, ITEM_WIDTH, | 1002 | "Quit"); |
1111 | MENU_X, MENU_Y+ITEM_HEIGHT*2, ITEM_WIDTH, ITEM_HEIGHT); | 1003 | |
1112 | } | 1004 | while (true) { |
1113 | else { | 1005 | switch (rb->do_menu(&main_menu, &choice, NULL, false)) { |
1114 | rb->lcd_bitmap_part(pegbox_menu_items, 0, ITEM_HEIGHT*5, ITEM_WIDTH, | 1006 | case 0: |
1115 | MENU_X, MENU_Y+ITEM_HEIGHT*2, ITEM_WIDTH, ITEM_HEIGHT); | 1007 | pegbox_load_level(pb); |
1116 | } | 1008 | pegbox_draw_board(pb); |
1117 | 1009 | return 0; | |
1118 | if (loc==3) { | 1010 | case 1: |
1119 | rb->lcd_bitmap_part(pegbox_menu_items, 0, ITEM_HEIGHT*8, ITEM_WIDTH, | 1011 | rb->set_int("Select Level", "", UNIT_INT, |
1120 | MENU_X, MENU_Y+ITEM_HEIGHT*3, ITEM_WIDTH, ITEM_HEIGHT); | 1012 | &pb->level, NULL, 1, 1, |
1121 | } | 1013 | pb->highlevel, NULL); |
1122 | else { | 1014 | break; |
1123 | rb->lcd_bitmap_part(pegbox_menu_items, 0, ITEM_HEIGHT*7, ITEM_WIDTH, | 1015 | case 2: |
1124 | MENU_X, MENU_Y+ITEM_HEIGHT*3, ITEM_WIDTH, ITEM_HEIGHT); | 1016 | if (pegbox_help()==PLUGIN_USB_CONNECTED) |
1125 | } | 1017 | return 1; |
1126 | #else | 1018 | break; |
1127 | unsigned int w,h; | 1019 | case 3: |
1128 | rb->lcd_clear_display(); | 1020 | playback_control(NULL); |
1129 | rb->lcd_getstringsize("PegBox", &w, &h); | 1021 | break; |
1130 | rb->lcd_putsxy((LCD_WIDTH-w)/2, 0, "PegBox"); | 1022 | case 4: |
1131 | rb->lcd_putsxy((LCD_WIDTH)/4, 16, "New Game"); | 1023 | return 1; |
1132 | rb->lcd_putsxy((LCD_WIDTH)/4, 24, "Resume"); | 1024 | case MENU_ATTACHED_USB: |
1133 | rb->lcd_putsxy((LCD_WIDTH)/4, 32, "Help"); | 1025 | return 1; |
1134 | rb->lcd_putsxy((LCD_WIDTH)/4, 40, "Quit"); | 1026 | default: |
1135 | 1027 | break; | |
1136 | if(!can_resume) | ||
1137 | rb->lcd_hline((LCD_WIDTH)/4, (LCD_WIDTH)/4+30, 28); | ||
1138 | |||
1139 | rb->lcd_putsxy((LCD_WIDTH)/4-8, loc*8+16, "*"); | ||
1140 | |||
1141 | |||
1142 | #endif | ||
1143 | rb->snprintf(str, 28, "Start on level %d of %d", startlevel, | ||
1144 | pb->highlevel); | ||
1145 | #if LCD_HEIGHT > 110 | ||
1146 | rb->lcd_putsxy(0, MENU_Y+4*ITEM_HEIGHT+8, str); | ||
1147 | #elif LCD_HEIGHT > 64 | ||
1148 | rb->lcd_putsxy(0, LCD_HEIGHT - 8, str); | ||
1149 | #else | ||
1150 | rb->lcd_puts_scroll(0, 7, str); | ||
1151 | #endif | ||
1152 | rb->lcd_update(); | ||
1153 | |||
1154 | /* handle menu button presses */ | ||
1155 | button = rb->button_get(true); | ||
1156 | |||
1157 | #ifdef HAVE_TOUCHSCREEN | ||
1158 | if(button & BUTTON_TOUCHSCREEN) | ||
1159 | { | ||
1160 | unsigned int result = touchscreen_map(&main_menu, | ||
1161 | rb->button_get_data() >> 16, | ||
1162 | rb->button_get_data() & 0xffff); | ||
1163 | if(result != (unsigned)-1 && button & BUTTON_REL) | ||
1164 | { | ||
1165 | if(result == 4) | ||
1166 | button = PEGBOX_LVL_UP; | ||
1167 | else | ||
1168 | { | ||
1169 | if(loc == result) | ||
1170 | button = PEGBOX_RIGHT; | ||
1171 | loc = result; | ||
1172 | } | ||
1173 | } | 1028 | } |
1174 | } | 1029 | } |
1175 | #endif | ||
1176 | |||
1177 | switch(button) { | ||
1178 | case PEGBOX_SAVE: /* start playing */ | ||
1179 | case PEGBOX_RIGHT: | ||
1180 | if (loc == 0) { | ||
1181 | breakout = true; | ||
1182 | pb->level = startlevel; | ||
1183 | load_level(pb); | ||
1184 | } | ||
1185 | else if (loc == 1 && can_resume) { | ||
1186 | if(pb->save_exist) | ||
1187 | { | ||
1188 | rb->remove(SAVE_FILE); | ||
1189 | pb->save_exist = false; | ||
1190 | } | ||
1191 | breakout = true; | ||
1192 | } | ||
1193 | else if (loc == 2) | ||
1194 | { | ||
1195 | if(pegbox_help()) | ||
1196 | return PB_USB; | ||
1197 | } | ||
1198 | else if (loc == 3) | ||
1199 | return PB_QUIT; | ||
1200 | break; | ||
1201 | |||
1202 | case PEGBOX_QUIT: /* quit program */ | ||
1203 | return PB_QUIT; | ||
1204 | |||
1205 | case (PEGBOX_UP|BUTTON_REPEAT): | ||
1206 | case PEGBOX_UP: | ||
1207 | if (loc <= 0) | ||
1208 | loc = 3; | ||
1209 | else | ||
1210 | loc--; | ||
1211 | if (!can_resume && loc == 1) { | ||
1212 | loc = 0; | ||
1213 | } | ||
1214 | break; | ||
1215 | |||
1216 | |||
1217 | case (PEGBOX_DOWN|BUTTON_REPEAT): | ||
1218 | case PEGBOX_DOWN: | ||
1219 | if (loc >= 3) | ||
1220 | loc = 0; | ||
1221 | else | ||
1222 | loc++; | ||
1223 | if (!can_resume && loc == 1) { | ||
1224 | loc = 2; | ||
1225 | } | ||
1226 | break; | ||
1227 | |||
1228 | case (PEGBOX_LVL_UP|BUTTON_REPEAT): | ||
1229 | case PEGBOX_LVL_UP: /* increase starting level */ | ||
1230 | if(startlevel >= pb->highlevel) { | ||
1231 | startlevel = 1; | ||
1232 | } else { | ||
1233 | startlevel++; | ||
1234 | } | ||
1235 | break; | ||
1236 | |||
1237 | /* only for targets with enough buttons */ | ||
1238 | #ifdef PEGBOX_LVL_DOWN | ||
1239 | case (PEGBOX_LVL_DOWN|BUTTON_REPEAT): | ||
1240 | case PEGBOX_LVL_DOWN: /* decrease starting level */ | ||
1241 | if(startlevel <= 1) { | ||
1242 | startlevel = pb->highlevel; | ||
1243 | } else { | ||
1244 | startlevel--; | ||
1245 | } | ||
1246 | break; | ||
1247 | #endif | ||
1248 | default: | ||
1249 | if(rb->default_event_handler_ex(button, pegbox_callback, | ||
1250 | (void*) pb) == SYS_USB_CONNECTED) | ||
1251 | return PB_USB; | ||
1252 | break; | ||
1253 | } | ||
1254 | |||
1255 | } | 1030 | } |
1256 | draw_board(pb); | ||
1257 | |||
1258 | return 0; | ||
1259 | } | 1031 | } |
1260 | 1032 | ||
1261 | /***************************************************************************** | 1033 | /*********************************************************************** |
1262 | * pegbox() is the main game subroutine, it returns the final game status. | 1034 | * pegbox_main() is the main game subroutine |
1263 | ******************************************************************************/ | 1035 | ************************************************************************/ |
1264 | static int pegbox(struct game_context* pb) { | 1036 | static int pegbox_main(struct game_context* pb) |
1265 | int temp_var; | 1037 | { |
1266 | 1038 | int button; | |
1267 | /******************** | 1039 | |
1268 | * menu * | 1040 | if (pegbox_menu(pb, false)==1) { |
1269 | ********************/ | 1041 | return 1; |
1270 | temp_var = pegbox_menu(pb); | 1042 | } |
1271 | if (temp_var == PB_QUIT || temp_var == PB_USB) | ||
1272 | return temp_var; | ||
1273 | 1043 | ||
1274 | while (true) { | 1044 | while (true) { |
1275 | temp_var = rb->button_get(true); | 1045 | button = rb->button_get(true); |
1276 | #ifdef HAVE_TOUCHSCREEN | 1046 | #ifdef HAVE_TOUCHSCREEN |
1277 | if(temp_var & BUTTON_TOUCHSCREEN) | 1047 | if(button & BUTTON_TOUCHSCREEN) |
1278 | { | 1048 | { |
1279 | pegbox_raster_btn.two_d_from.y = pb->player_row; | 1049 | pegbox_raster_btn.two_d_from.y = pb->player_row; |
1280 | pegbox_raster_btn.two_d_from.x = pb->player_col; | 1050 | pegbox_raster_btn.two_d_from.x = pb->player_col; |
@@ -1283,57 +1053,56 @@ static int pegbox(struct game_context* pb) { | |||
1283 | touchscreen_raster_map_button(&pegbox_raster_btn, | 1053 | touchscreen_raster_map_button(&pegbox_raster_btn, |
1284 | rb->button_get_data() >> 16, | 1054 | rb->button_get_data() >> 16, |
1285 | rb->button_get_data() & 0xffff, | 1055 | rb->button_get_data() & 0xffff, |
1286 | temp_var); | 1056 | button); |
1287 | if(ret.action == TS_ACTION_TWO_D_MOVEMENT) | 1057 | if(ret.action == TS_ACTION_TWO_D_MOVEMENT) |
1288 | move_player(pb, ret.to.x - ret.from.x, ret.to.y - ret.from.y); | 1058 | pegbox_move_player(pb, ret.to.x - ret.from.x, ret.to.y - ret.from.y); |
1289 | } | 1059 | } |
1290 | #endif | 1060 | #endif |
1291 | switch(temp_var){ | 1061 | switch(button){ |
1292 | case PEGBOX_LEFT: /* move cursor left */ | 1062 | case PEGBOX_LEFT: /* move cursor left */ |
1293 | case (PEGBOX_LEFT|BUTTON_REPEAT): | 1063 | case (PEGBOX_LEFT|BUTTON_REPEAT): |
1294 | move_player(pb, -1, 0); | 1064 | pegbox_move_player(pb, -1, 0); |
1295 | break; | 1065 | break; |
1296 | 1066 | ||
1297 | case PEGBOX_RIGHT: /* move cursor right */ | 1067 | case PEGBOX_RIGHT: /* move cursor right */ |
1298 | case (PEGBOX_RIGHT|BUTTON_REPEAT): | 1068 | case (PEGBOX_RIGHT|BUTTON_REPEAT): |
1299 | move_player(pb, 1, 0); | 1069 | pegbox_move_player(pb, 1, 0); |
1300 | break; | 1070 | break; |
1301 | 1071 | ||
1302 | case PEGBOX_DOWN: /* move cursor down */ | 1072 | case PEGBOX_DOWN: /* move cursor down */ |
1303 | case (PEGBOX_DOWN|BUTTON_REPEAT): | 1073 | case (PEGBOX_DOWN|BUTTON_REPEAT): |
1304 | move_player(pb, 0, 1); | 1074 | pegbox_move_player(pb, 0, 1); |
1305 | break; | 1075 | break; |
1306 | 1076 | ||
1307 | case PEGBOX_UP: /* move cursor up */ | 1077 | case PEGBOX_UP: /* move cursor up */ |
1308 | case (PEGBOX_UP|BUTTON_REPEAT): | 1078 | case (PEGBOX_UP|BUTTON_REPEAT): |
1309 | move_player(pb, 0, -1); | 1079 | pegbox_move_player(pb, 0, -1); |
1310 | break; | 1080 | break; |
1311 | 1081 | ||
1312 | case PEGBOX_SAVE: /* save and end game */ | ||
1313 | rb->splash(HZ, "Saving game..."); | ||
1314 | pegbox_savegame(pb); | ||
1315 | /* fall through to PEGBOX_QUIT */ | ||
1316 | |||
1317 | case PEGBOX_QUIT: | 1082 | case PEGBOX_QUIT: |
1318 | return PB_END; | 1083 | if (pegbox_menu(pb, true)==1) { |
1319 | 1084 | return 1; | |
1085 | } | ||
1086 | #ifdef PEGBOX_RESTART | ||
1320 | case PEGBOX_RESTART: | 1087 | case PEGBOX_RESTART: |
1321 | load_level(pb); | 1088 | pegbox_load_level(pb); |
1322 | draw_board(pb); | 1089 | pegbox_draw_board(pb); |
1323 | break; | 1090 | break; |
1091 | #endif | ||
1324 | 1092 | ||
1093 | #ifdef PEGBOX_LVL_UP | ||
1325 | case (PEGBOX_LVL_UP|BUTTON_REPEAT): | 1094 | case (PEGBOX_LVL_UP|BUTTON_REPEAT): |
1326 | case PEGBOX_LVL_UP: | 1095 | case PEGBOX_LVL_UP: |
1327 | if(pb->level >= pb->highlevel) { | 1096 | if (pb->level >= pb->highlevel) { |
1328 | pb->level = 1; | 1097 | pb->level = 1; |
1329 | } else { | 1098 | } else { |
1330 | pb->level++; | 1099 | pb->level++; |
1331 | } | 1100 | } |
1332 | load_level(pb); | 1101 | pegbox_load_level(pb); |
1333 | draw_board(pb); | 1102 | pegbox_draw_board(pb); |
1334 | break; | 1103 | break; |
1104 | #endif | ||
1335 | 1105 | ||
1336 | /* only for targets with enough buttons */ | ||
1337 | #ifdef PEGBOX_LVL_DOWN | 1106 | #ifdef PEGBOX_LVL_DOWN |
1338 | case (PEGBOX_LVL_DOWN|BUTTON_REPEAT): | 1107 | case (PEGBOX_LVL_DOWN|BUTTON_REPEAT): |
1339 | case PEGBOX_LVL_DOWN: | 1108 | case PEGBOX_LVL_DOWN: |
@@ -1342,82 +1111,65 @@ static int pegbox(struct game_context* pb) { | |||
1342 | } else { | 1111 | } else { |
1343 | pb->level--; | 1112 | pb->level--; |
1344 | } | 1113 | } |
1345 | load_level(pb); | 1114 | pegbox_load_level(pb); |
1346 | draw_board(pb); | 1115 | pegbox_draw_board(pb); |
1347 | break; | 1116 | break; |
1348 | #endif | 1117 | #endif |
1349 | |||
1350 | } | 1118 | } |
1351 | 1119 | ||
1352 | if(pb->num_left == 0) { | 1120 | if (pb->num_left == 0) { |
1353 | rb->splash(HZ*2, "Nice Pegging!"); | 1121 | rb->splash(HZ*2, "Nice Pegging!"); |
1354 | if(pb->level == NUM_LEVELS) { | 1122 | if (pb->level == NUM_LEVELS) { |
1355 | draw_board(pb); | 1123 | pegbox_draw_board(pb); |
1356 | rb->splash(HZ*2, "You Won!"); | 1124 | rb->splash(HZ*2, "Congratulations!"); |
1357 | break; | 1125 | rb->splash(HZ*2, "You have finished the game!"); |
1126 | if (pegbox_menu(pb,false)==1) { | ||
1127 | return 1; | ||
1128 | } | ||
1358 | } | 1129 | } |
1359 | else { | 1130 | else { |
1360 | pb->level++; | 1131 | pb->level++; |
1361 | load_level(pb); | 1132 | pegbox_load_level(pb); |
1362 | draw_board(pb); | 1133 | pegbox_draw_board(pb); |
1363 | } | 1134 | } |
1364 | 1135 | ||
1365 | if(pb->level > pb->highlevel) | 1136 | if(pb->level > pb->highlevel) |
1366 | pb->highlevel = pb->level; | 1137 | pb->highlevel = pb->level; |
1367 | 1138 | ||
1368 | } | 1139 | } |
1369 | } | 1140 | } |
1370 | |||
1371 | return PLUGIN_OK; | 1141 | return PLUGIN_OK; |
1372 | } | 1142 | } |
1373 | 1143 | ||
1374 | |||
1375 | /***************************************************************************** | 1144 | /***************************************************************************** |
1376 | * plugin entry point. | 1145 | * plugin entry point. |
1377 | ******************************************************************************/ | 1146 | ******************************************************************************/ |
1378 | enum plugin_status plugin_start(const void* parameter) { | 1147 | enum plugin_status plugin_start(const void* parameter) |
1379 | bool exit = false; | 1148 | { |
1380 | struct game_context pb; | ||
1381 | |||
1382 | (void)parameter; | 1149 | (void)parameter; |
1383 | 1150 | #ifdef HAVE_LCD_BITMAP | |
1384 | rb->lcd_setfont(FONT_SYSFIXED); | 1151 | rb->lcd_setfont(FONT_SYSFIXED); |
1385 | #if LCD_DEPTH > 1 | 1152 | #if LCD_DEPTH > 1 |
1386 | rb->lcd_set_backdrop(NULL); | 1153 | rb->lcd_set_backdrop(NULL); |
1387 | #endif | 1154 | #endif |
1388 | #ifdef HAVE_LCD_COLOR | 1155 | #ifdef HAVE_LCD_COLOR |
1389 | rb->lcd_set_foreground(LCD_WHITE); | 1156 | rb->lcd_set_foreground(LCD_WHITE); |
1390 | rb->lcd_set_background(BG_COLOR); | 1157 | rb->lcd_set_background(BG_COLOR); |
1391 | #endif | 1158 | #endif |
1392 | |||
1393 | rb->splash(0, "Loading..."); | ||
1394 | pegbox_loaddata(&pb); | ||
1395 | pb.save_exist = pegbox_loadgame(&pb); | ||
1396 | pb.num_left = 0; | ||
1397 | |||
1398 | rb->lcd_clear_display(); | 1159 | rb->lcd_clear_display(); |
1399 | 1160 | ||
1400 | 1161 | struct game_context pb; | |
1401 | while(!exit) { | 1162 | pb.level=1; |
1402 | switch(pegbox(&pb)){ | 1163 | pb.highlevel=1; |
1403 | case PB_END: | 1164 | struct configdata config[] = { |
1404 | break; | 1165 | {TYPE_INT, 1, NUM_LEVELS, { .int_p = &(pb.level) }, "level", NULL}, |
1405 | 1166 | {TYPE_INT, 1, NUM_LEVELS, { .int_p = &(pb.highlevel) }, "highlevel", NULL}, | |
1406 | case PB_USB: | 1167 | }; |
1407 | rb->lcd_setfont(FONT_UI); | 1168 | configfile_load(CONFIG_FILE_NAME,config,2,0); |
1408 | return PLUGIN_USB_CONNECTED; | 1169 | pegbox_main(&pb); |
1409 | 1170 | configfile_save(CONFIG_FILE_NAME,config,2,0); | |
1410 | case PB_QUIT: | ||
1411 | rb->splash(HZ, "Saving data..."); | ||
1412 | pegbox_savedata(&pb); | ||
1413 | exit = true; | ||
1414 | break; | ||
1415 | |||
1416 | default: | ||
1417 | break; | ||
1418 | } | ||
1419 | } | ||
1420 | |||
1421 | rb->lcd_setfont(FONT_UI); | 1171 | rb->lcd_setfont(FONT_UI); |
1172 | #endif /* HAVE_LCD_BITMAP */ | ||
1173 | |||
1422 | return PLUGIN_OK; | 1174 | return PLUGIN_OK; |
1423 | } | 1175 | } |