-rw-r--r-- | noncore/games/sfcave/sfcave.cpp | 367 | ||||
-rw-r--r-- | noncore/games/sfcave/sfcave.h | 10 |
2 files changed, 219 insertions, 158 deletions
diff --git a/noncore/games/sfcave/sfcave.cpp b/noncore/games/sfcave/sfcave.cpp index d551afe..0160d34 100644 --- a/noncore/games/sfcave/sfcave.cpp +++ b/noncore/games/sfcave/sfcave.cpp | |||
@@ -34,10 +34,12 @@ | |||
34 | #define MENU_MAIN_MENU 0 | 34 | #define MENU_MAIN_MENU 0 |
35 | #define MENU_OPTIONS_MENU 1 | 35 | #define MENU_OPTIONS_MENU 1 |
36 | #define MENU_REPLAY_MENU 2 | ||
36 | 37 | ||
37 | // Main Menu Options | 38 | // Main Menu Options |
38 | #define MENU_START_GAME 0 | 39 | #define MENU_START_GAME 0 |
39 | #define MENU_OPTIONS 1 | 40 | #define MENU_REPLAY 1 |
40 | #define MENU_HELP 2 | 41 | #define MENU_OPTIONS 2 |
41 | #define MENU_QUIT 3 | 42 | #define MENU_HELP 3 |
43 | #define MENU_QUIT 4 | ||
42 | 44 | ||
43 | // Option Menu Options | 45 | // Option Menu Options |
@@ -47,4 +49,10 @@ | |||
47 | #define MENU_BACK 3 | 49 | #define MENU_BACK 3 |
48 | 50 | ||
51 | // Replay Menu Options | ||
52 | #define MENU_REPLAY_START 0 | ||
53 | #define MENU_REPLAY_LOAD 1 | ||
54 | #define MENU_REPLAY_SAVE 2 | ||
55 | #define MENU_REPLAY_BACK 3 | ||
56 | |||
49 | 57 | ||
50 | #define NR_GAME_DIFFICULTIES 3 | 58 | #define NR_GAME_DIFFICULTIES 3 |
@@ -70,7 +78,11 @@ QString SFCave::dificultyOption[] = { EASY, NORMAL, HARD }; | |||
70 | QString SFCave::gameTypes[] = { SFCAVE_GAME, GATES_GAME, FLY_GAME }; | 78 | QString SFCave::gameTypes[] = { SFCAVE_GAME, GATES_GAME, FLY_GAME }; |
71 | 79 | ||
72 | QString SFCave::menuOptions[2][5] = { { "Start Game", "Options", "Help", "Quit", "" }, | 80 | QString SFCave::menuOptions[NR_MENUS][MAX_MENU_OPTIONS] = { { "Start Game", "Replays", "Options", "Help", "Quit", "", "", "" }, |
73 | { "Game Type - %s", "Game Difficulty - %s", "Clear High Scores for this game", "Back", "" } }; | 81 | { "Game Type - %s", "Game Difficulty - %s", "Clear High Scores for this game", "Back", "", "", "", "" }, |
82 | { "Play Reply", "Load Replay", "Save Replay", "Back", "", "", "", "" } }; | ||
74 | 83 | ||
84 | int SFCave::nrMenuOptions[NR_MENUS] = { 5, 4, 4 }; | ||
85 | int SFCave ::currentMenuOption[NR_MENUS] = { 0, 0, 0 }; | ||
86 | |||
75 | #define UP_THRUST 0.6 | 87 | #define UP_THRUST 0.6 |
76 | #define NO_THRUST 0.8 | 88 | #define NO_THRUST 0.8 |
@@ -95,6 +107,4 @@ double SFCave::MaxDownThrustVals[3][3] = {{ 4.0, 4.0, 4.0 }, // S | |||
95 | int SFCave::initialGateGaps[] = { 75, 50, 25 }; | 107 | int SFCave::initialGateGaps[] = { 75, 50, 25 }; |
96 | 108 | ||
97 | int SFCave::nrMenuOptions[2] = { 4, 4 }; | ||
98 | int SFCave ::currentMenuOption[2] = { 0, 0 }; | ||
99 | 109 | ||
100 | bool movel; | 110 | bool movel; |
@@ -693,5 +703,5 @@ void SFCave :: draw() | |||
693 | text = "Press OK for menu"; | 703 | text = "Press OK for menu"; |
694 | p.drawText( (sWidth/2) - (fm.width( text )/2), 135, text ); | 704 | p.drawText( (sWidth/2) - (fm.width( text )/2), 135, text ); |
695 | 705 | /* | |
696 | text = "Press r to replay"; | 706 | text = "Press r to replay"; |
697 | p.drawText( (sWidth/2) - (fm.width( text )/2), 150, text ); | 707 | p.drawText( (sWidth/2) - (fm.width( text )/2), 150, text ); |
@@ -702,4 +712,5 @@ void SFCave :: draw() | |||
702 | text = "Press r to load a saved replay"; | 712 | text = "Press r to load a saved replay"; |
703 | p.drawText( (sWidth/2) - (fm.width( text )/2), 180, text ); | 713 | p.drawText( (sWidth/2) - (fm.width( text )/2), 180, text ); |
714 | */ | ||
704 | } | 715 | } |
705 | else | 716 | else |
@@ -763,61 +774,5 @@ void SFCave :: keyPressEvent( QKeyEvent *e ) | |||
763 | { | 774 | { |
764 | if ( state == STATE_MENU ) | 775 | if ( state == STATE_MENU ) |
765 | { | 776 | handleMenuKeys( e ); |
766 | switch( e->key() ) | ||
767 | { | ||
768 | case Qt::Key_Down: | ||
769 | currentMenuOption[currentMenuNr] ++; | ||
770 | if ( menuOptions[currentMenuNr][currentMenuOption[currentMenuNr]] == "" ) | ||
771 | currentMenuOption[currentMenuNr] = 0; | ||
772 | break; | ||
773 | case Qt::Key_Up: | ||
774 | currentMenuOption[currentMenuNr] --; | ||
775 | if ( currentMenuOption[currentMenuNr] < 0 ) | ||
776 | currentMenuOption[currentMenuNr] = nrMenuOptions[currentMenuNr]-1; | ||
777 | break; | ||
778 | |||
779 | case Qt::Key_Left: | ||
780 | if ( currentMenuNr == MENU_OPTIONS_MENU ) | ||
781 | { | ||
782 | if ( currentMenuOption[currentMenuNr] == MENU_GAME_TYPE ) | ||
783 | { | ||
784 | currentGameType --; | ||
785 | if ( currentGameType < 0 ) | ||
786 | currentGameType = NR_GAME_TYPES - 1; | ||
787 | } | ||
788 | else if ( currentMenuOption[currentMenuNr] == MENU_GAME_DIFFICULTY ) | ||
789 | { | ||
790 | currentGameDifficulty --; | ||
791 | if ( currentGameDifficulty < 0 ) | ||
792 | currentGameDifficulty = NR_GAME_DIFFICULTIES - 1; | ||
793 | } | ||
794 | } | ||
795 | break; | ||
796 | |||
797 | case Qt::Key_Right: | ||
798 | if ( currentMenuNr == MENU_OPTIONS_MENU ) | ||
799 | { | ||
800 | if ( currentMenuOption[currentMenuNr] == MENU_GAME_TYPE ) | ||
801 | { | ||
802 | currentGameType ++; | ||
803 | if ( currentGameType == NR_GAME_TYPES ) | ||
804 | currentGameType = 0; | ||
805 | } | ||
806 | else if ( currentMenuOption[currentMenuNr] == MENU_GAME_DIFFICULTY ) | ||
807 | { | ||
808 | currentGameDifficulty ++; | ||
809 | if ( currentGameDifficulty == NR_GAME_DIFFICULTIES ) | ||
810 | currentGameDifficulty = 0; | ||
811 | } | ||
812 | } | ||
813 | break; | ||
814 | |||
815 | case Qt::Key_Space: | ||
816 | case Qt::Key_Return: | ||
817 | case Qt::Key_Enter: | ||
818 | dealWithMenuSelection(); | ||
819 | break; | ||
820 | } | ||
821 | } | ||
822 | else | 777 | else |
823 | { | 778 | { |
@@ -837,5 +792,9 @@ void SFCave :: keyPressEvent( QKeyEvent *e ) | |||
837 | case Qt::Key_Enter: | 792 | case Qt::Key_Enter: |
838 | if ( state == STATE_CRASHED ) | 793 | if ( state == STATE_CRASHED ) |
794 | { | ||
839 | state = STATE_MENU; | 795 | state = STATE_MENU; |
796 | currentMenuNr = 0; | ||
797 | currentMenuOption[currentMenuNr] = 0; | ||
798 | } | ||
840 | break; | 799 | break; |
841 | 800 | ||
@@ -900,4 +859,164 @@ void SFCave :: keyReleaseEvent( QKeyEvent *e ) | |||
900 | } | 859 | } |
901 | 860 | ||
861 | |||
862 | void SFCave :: saveScore() | ||
863 | { | ||
864 | #ifdef QWS | ||
865 | Config cfg( "sfcave" ); | ||
866 | cfg.setGroup( "settings" ); | ||
867 | QString key = "highScore_"; | ||
868 | |||
869 | cfg.writeEntry( key + gameTypes[currentGameType] + "_" + dificultyOption[currentGameDifficulty], highestScore[currentGameType][currentGameDifficulty] ); | ||
870 | key += CURRENT_GAME_TYPE; | ||
871 | cfg.writeEntry( key, highestScore[currentGameType] ); | ||
872 | #endif | ||
873 | } | ||
874 | |||
875 | void SFCave :: saveReplay() | ||
876 | { | ||
877 | FILE *out; | ||
878 | out = fopen( (const char *)replayFile, "w" ); | ||
879 | if ( !out ) | ||
880 | { | ||
881 | printf( "Couldn't write to /home/root/sfcave.replay\n" ); | ||
882 | return; | ||
883 | } | ||
884 | |||
885 | // Build up string of values | ||
886 | // Format is:: <landscape seed> <game type> <difficulty> <framenr> <framenr>....... | ||
887 | QString val; | ||
888 | val.sprintf( "%d %d %d ", currentSeed, currentGameType, currentGameDifficulty ); | ||
889 | |||
890 | QListIterator<int> it( replayList ); | ||
891 | while( it.current() ) | ||
892 | { | ||
893 | QString tmp; | ||
894 | tmp.sprintf( "%d ", (*it.current()) ); | ||
895 | val += tmp; | ||
896 | |||
897 | ++it; | ||
898 | } | ||
899 | val += "\n"; | ||
900 | |||
901 | QString line; | ||
902 | line.sprintf( "%d\n", val.length() ); | ||
903 | fwrite( (const char *)line, 1, line.length(), out ); | ||
904 | |||
905 | fwrite( (const char *)val, 1, val.length(), out ); | ||
906 | |||
907 | fclose( out ); | ||
908 | |||
909 | printf( "Replay saved to %s\n", (const char *)replayFile ); | ||
910 | |||
911 | } | ||
912 | |||
913 | void SFCave :: loadReplay() | ||
914 | { | ||
915 | FILE *in = fopen( (const char *)replayFile, "r" ); | ||
916 | |||
917 | if ( in == 0 ) | ||
918 | { | ||
919 | printf( "Couldn't load replay file!\n" ); | ||
920 | return; | ||
921 | } | ||
922 | |||
923 | // Read next line - contains the size of the options | ||
924 | char line[10+1]; | ||
925 | fgets( line, 10, in ); | ||
926 | |||
927 | int length = -1; | ||
928 | sscanf( line, "%d", &length ); | ||
929 | char *data = new char[length+1]; | ||
930 | |||
931 | fread( data, 1, length, in ); | ||
932 | // printf( "data - %s", data ); | ||
933 | |||
934 | QString sep = " "; | ||
935 | QStringList list = QStringList::split( sep, QString( data ) ); | ||
936 | |||
937 | // print it out | ||
938 | QStringList::Iterator it = list.begin(); | ||
939 | currentSeed = (*it).toInt(); | ||
940 | ++it; | ||
941 | currentGameType = (*it).toInt(); | ||
942 | ++it; | ||
943 | currentGameDifficulty = (*it).toInt(); | ||
944 | ++it; | ||
945 | |||
946 | replayList.clear(); | ||
947 | for ( ; it != list.end(); ++it ) | ||
948 | { | ||
949 | int v = (*it).toInt(); | ||
950 | replayList.append( new int( v ) ); | ||
951 | } | ||
952 | |||
953 | delete data; | ||
954 | |||
955 | fclose( in ); | ||
956 | |||
957 | printf( "Replay loaded from %s\n", (const char *)replayFile ); | ||
958 | } | ||
959 | |||
960 | |||
961 | //--------------- MENU CODE --------------------- | ||
962 | void SFCave :: handleMenuKeys( QKeyEvent *e ) | ||
963 | { | ||
964 | switch( e->key() ) | ||
965 | { | ||
966 | case Qt::Key_Down: | ||
967 | currentMenuOption[currentMenuNr] ++; | ||
968 | if ( menuOptions[currentMenuNr][currentMenuOption[currentMenuNr]] == "" ) | ||
969 | currentMenuOption[currentMenuNr] = 0; | ||
970 | break; | ||
971 | case Qt::Key_Up: | ||
972 | currentMenuOption[currentMenuNr] --; | ||
973 | if ( currentMenuOption[currentMenuNr] < 0 ) | ||
974 | currentMenuOption[currentMenuNr] = nrMenuOptions[currentMenuNr]-1; | ||
975 | break; | ||
976 | |||
977 | case Qt::Key_Left: | ||
978 | if ( currentMenuNr == MENU_OPTIONS_MENU ) | ||
979 | { | ||
980 | if ( currentMenuOption[currentMenuNr] == MENU_GAME_TYPE ) | ||
981 | { | ||
982 | currentGameType --; | ||
983 | if ( currentGameType < 0 ) | ||
984 | currentGameType = NR_GAME_TYPES - 1; | ||
985 | } | ||
986 | else if ( currentMenuOption[currentMenuNr] == MENU_GAME_DIFFICULTY ) | ||
987 | { | ||
988 | currentGameDifficulty --; | ||
989 | if ( currentGameDifficulty < 0 ) | ||
990 | currentGameDifficulty = NR_GAME_DIFFICULTIES - 1; | ||
991 | } | ||
992 | } | ||
993 | break; | ||
994 | |||
995 | case Qt::Key_Right: | ||
996 | if ( currentMenuNr == MENU_OPTIONS_MENU ) | ||
997 | { | ||
998 | if ( currentMenuOption[currentMenuNr] == MENU_GAME_TYPE ) | ||
999 | { | ||
1000 | currentGameType ++; | ||
1001 | if ( currentGameType == NR_GAME_TYPES ) | ||
1002 | currentGameType = 0; | ||
1003 | } | ||
1004 | else if ( currentMenuOption[currentMenuNr] == MENU_GAME_DIFFICULTY ) | ||
1005 | { | ||
1006 | currentGameDifficulty ++; | ||
1007 | if ( currentGameDifficulty == NR_GAME_DIFFICULTIES ) | ||
1008 | currentGameDifficulty = 0; | ||
1009 | } | ||
1010 | } | ||
1011 | break; | ||
1012 | |||
1013 | case Qt::Key_Space: | ||
1014 | case Qt::Key_Return: | ||
1015 | case Qt::Key_Enter: | ||
1016 | dealWithMenuSelection(); | ||
1017 | break; | ||
1018 | } | ||
1019 | } | ||
1020 | |||
902 | void SFCave :: displayMenu() | 1021 | void SFCave :: displayMenu() |
903 | { | 1022 | { |
@@ -919,5 +1038,5 @@ void SFCave :: displayMenu() | |||
919 | 1038 | ||
920 | // Draw options | 1039 | // Draw options |
921 | int pos = 170; | 1040 | int pos = 140; |
922 | for ( int i = 0 ; menuOptions[currentMenuNr][i] != "" ; ++i, pos += 25 ) | 1041 | for ( int i = 0 ; menuOptions[currentMenuNr][i] != "" ; ++i, pos += 25 ) |
923 | { | 1042 | { |
@@ -960,4 +1079,9 @@ void SFCave :: dealWithMenuSelection() | |||
960 | break; | 1079 | break; |
961 | 1080 | ||
1081 | case MENU_REPLAY: | ||
1082 | currentMenuNr = MENU_REPLAY_MENU; | ||
1083 | currentMenuOption[currentMenuNr] = 0; | ||
1084 | break; | ||
1085 | |||
962 | case MENU_OPTIONS: | 1086 | case MENU_OPTIONS: |
963 | currentMenuNr = MENU_OPTIONS_MENU; | 1087 | currentMenuNr = MENU_OPTIONS_MENU; |
@@ -973,5 +1097,5 @@ void SFCave :: dealWithMenuSelection() | |||
973 | break; | 1097 | break; |
974 | } | 1098 | } |
975 | 1099 | ||
976 | case MENU_QUIT: | 1100 | case MENU_QUIT: |
977 | QApplication::exit(); | 1101 | QApplication::exit(); |
@@ -988,5 +1112,5 @@ void SFCave :: dealWithMenuSelection() | |||
988 | case MENU_GAME_TYPE: | 1112 | case MENU_GAME_TYPE: |
989 | break; | 1113 | break; |
990 | 1114 | ||
991 | case MENU_GAME_DIFFICULTY: | 1115 | case MENU_GAME_DIFFICULTY: |
992 | break; | 1116 | break; |
@@ -996,5 +1120,5 @@ void SFCave :: dealWithMenuSelection() | |||
996 | highestScore[currentGameType][i] = 0; | 1120 | highestScore[currentGameType][i] = 0; |
997 | break; | 1121 | break; |
998 | 1122 | ||
999 | case MENU_BACK: | 1123 | case MENU_BACK: |
1000 | currentMenuNr = MENU_MAIN_MENU; | 1124 | currentMenuNr = MENU_MAIN_MENU; |
@@ -1011,95 +1135,30 @@ void SFCave :: dealWithMenuSelection() | |||
1011 | break; | 1135 | break; |
1012 | } | 1136 | } |
1013 | } | ||
1014 | } | ||
1015 | 1137 | ||
1016 | void SFCave :: saveScore() | 1138 | case MENU_REPLAY_MENU: |
1017 | { | 1139 | { |
1018 | #ifdef QWS | 1140 | switch( currentMenuOption[currentMenuNr] ) |
1019 | Config cfg( "sfcave" ); | 1141 | { |
1020 | cfg.setGroup( "settings" ); | 1142 | case MENU_REPLAY_START: |
1021 | QString key = "highScore_"; | 1143 | if ( currentSeed != 0 ) |
1144 | state = STATE_REPLAY; | ||
1145 | // Display No Replay | ||
1146 | break; | ||
1022 | 1147 | ||
1023 | cfg.writeEntry( key + gameTypes[currentGameType] + "_" + dificultyOption[currentGameDifficulty], highestScore[currentGameType][currentGameDifficulty] ); | 1148 | case MENU_REPLAY_LOAD: |
1024 | key += CURRENT_GAME_TYPE; | 1149 | loadReplay(); |
1025 | cfg.writeEntry( key, highestScore[currentGameType] ); | 1150 | break; |
1026 | #endif | ||
1027 | } | ||
1028 | 1151 | ||
1029 | void SFCave :: saveReplay() | 1152 | case MENU_REPLAY_SAVE: |
1030 | { | 1153 | saveReplay(); |
1031 | FILE *out; | 1154 | break; |
1032 | out = fopen( (const char *)replayFile, "w" ); | ||
1033 | if ( !out ) | ||
1034 | { | ||
1035 | printf( "Couldn't write to /home/root/sfcave.replay\n" ); | ||
1036 | return; | ||
1037 | } | ||
1038 | 1155 | ||
1039 | // Build up string of values | 1156 | case MENU_REPLAY_BACK: |
1040 | // Format is:: <landscape seed> <framenr> <framenr>....... | 1157 | currentMenuNr = MENU_MAIN_MENU; |
1041 | QString val; | 1158 | break; |
1042 | val.sprintf( "%d ", currentSeed ); | ||
1043 | |||
1044 | QListIterator<int> it( replayList ); | ||
1045 | while( it.current() ) | ||
1046 | { | ||
1047 | QString tmp; | ||
1048 | tmp.sprintf( "%d ", (*it.current()) ); | ||
1049 | val += tmp; | ||
1050 | 1159 | ||
1051 | ++it; | 1160 | } |
1161 | } | ||
1052 | } | 1162 | } |
1053 | val += "\n"; | ||
1054 | |||
1055 | QString line; | ||
1056 | line.sprintf( "%d\n", val.length() ); | ||
1057 | fwrite( (const char *)line, 1, line.length(), out ); | ||
1058 | fwrite( (const char *)val, 1, val.length(), out ); | ||
1059 | |||
1060 | fclose( out ); | ||
1061 | |||
1062 | printf( "Replay saved to %s\n", (const char *)replayFile ); | ||
1063 | |||
1064 | } | 1163 | } |
1065 | 1164 | ||
1066 | void SFCave :: loadReplay() | ||
1067 | { | ||
1068 | FILE *in = fopen( (const char *)replayFile, "r" ); | ||
1069 | |||
1070 | if ( in == 0 ) | ||
1071 | { | ||
1072 | printf( "Couldn't load replay file!\n" ); | ||
1073 | return; | ||
1074 | } | ||
1075 | // Read size of next line | ||
1076 | char line[10+1]; | ||
1077 | fgets( line, 10, in ); | ||
1078 | |||
1079 | int length = -1; | ||
1080 | sscanf( line, "%d", &length ); | ||
1081 | char *data = new char[length+1]; | ||
1082 | |||
1083 | fread( data, 1, length, in ); | ||
1084 | |||
1085 | QString sep = " "; | ||
1086 | QStringList list = QStringList::split( sep, QString( data ) ); | ||
1087 | |||
1088 | // print it out | ||
1089 | QStringList::Iterator it = list.begin(); | ||
1090 | currentSeed = (*it).toInt(); | ||
1091 | ++it; | ||
1092 | |||
1093 | replayList.clear(); | ||
1094 | for ( ; it != list.end(); ++it ) | ||
1095 | { | ||
1096 | int v = (*it).toInt(); | ||
1097 | replayList.append( new int( v ) ); | ||
1098 | } | ||
1099 | |||
1100 | delete data; | ||
1101 | |||
1102 | fclose( in ); | ||
1103 | |||
1104 | printf( "Replay loaded from %s\n", (const char *)replayFile ); | ||
1105 | } \ No newline at end of file | ||
diff --git a/noncore/games/sfcave/sfcave.h b/noncore/games/sfcave/sfcave.h index 238a615..65e5ae4 100644 --- a/noncore/games/sfcave/sfcave.h +++ b/noncore/games/sfcave/sfcave.h | |||
@@ -13,5 +13,6 @@ | |||
13 | #define TRAILSIZE 30 | 13 | #define TRAILSIZE 30 |
14 | 14 | ||
15 | 15 | #define NR_MENUS 3 | |
16 | #define MAX_MENU_OPTIONS 8 | ||
16 | 17 | ||
17 | class SFCave : public QMainWindow | 18 | class SFCave : public QMainWindow |
@@ -54,8 +55,8 @@ public: | |||
54 | int lastGateBottomY; | 55 | int lastGateBottomY; |
55 | 56 | ||
56 | static QString menuOptions[2][5]; | 57 | static QString menuOptions[NR_MENUS][MAX_MENU_OPTIONS]; |
57 | int currentMenuNr; | 58 | int currentMenuNr; |
58 | static int nrMenuOptions[2]; | 59 | static int nrMenuOptions[NR_MENUS]; |
59 | static int currentMenuOption[2]; | 60 | static int currentMenuOption[NR_MENUS]; |
60 | 61 | ||
61 | static QString dificultyOption[3]; | 62 | static QString dificultyOption[3]; |
@@ -108,4 +109,5 @@ public: | |||
108 | void handleKeys(); | 109 | void handleKeys(); |
109 | 110 | ||
111 | void handleMenuKeys( QKeyEvent * e ); | ||
110 | void displayMenu(); | 112 | void displayMenu(); |
111 | void dealWithMenuSelection(); | 113 | void dealWithMenuSelection(); |