summaryrefslogtreecommitdiff
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/games/sfcave/sfcave.cpp17
1 files changed, 13 insertions, 4 deletions
diff --git a/noncore/games/sfcave/sfcave.cpp b/noncore/games/sfcave/sfcave.cpp
index 93f5f82..d551afe 100644
--- a/noncore/games/sfcave/sfcave.cpp
+++ b/noncore/games/sfcave/sfcave.cpp
@@ -781,158 +781,160 @@ void SFCave :: keyPressEvent( QKeyEvent *e )
781 { 781 {
782 if ( currentMenuOption[currentMenuNr] == MENU_GAME_TYPE ) 782 if ( currentMenuOption[currentMenuNr] == MENU_GAME_TYPE )
783 { 783 {
784 currentGameType --; 784 currentGameType --;
785 if ( currentGameType < 0 ) 785 if ( currentGameType < 0 )
786 currentGameType = NR_GAME_TYPES - 1; 786 currentGameType = NR_GAME_TYPES - 1;
787 } 787 }
788 else if ( currentMenuOption[currentMenuNr] == MENU_GAME_DIFFICULTY ) 788 else if ( currentMenuOption[currentMenuNr] == MENU_GAME_DIFFICULTY )
789 { 789 {
790 currentGameDifficulty --; 790 currentGameDifficulty --;
791 if ( currentGameDifficulty < 0 ) 791 if ( currentGameDifficulty < 0 )
792 currentGameDifficulty = NR_GAME_DIFFICULTIES - 1; 792 currentGameDifficulty = NR_GAME_DIFFICULTIES - 1;
793 } 793 }
794 } 794 }
795 break; 795 break;
796 796
797 case Qt::Key_Right: 797 case Qt::Key_Right:
798 if ( currentMenuNr == MENU_OPTIONS_MENU ) 798 if ( currentMenuNr == MENU_OPTIONS_MENU )
799 { 799 {
800 if ( currentMenuOption[currentMenuNr] == MENU_GAME_TYPE ) 800 if ( currentMenuOption[currentMenuNr] == MENU_GAME_TYPE )
801 { 801 {
802 currentGameType ++; 802 currentGameType ++;
803 if ( currentGameType == NR_GAME_TYPES ) 803 if ( currentGameType == NR_GAME_TYPES )
804 currentGameType = 0; 804 currentGameType = 0;
805 } 805 }
806 else if ( currentMenuOption[currentMenuNr] == MENU_GAME_DIFFICULTY ) 806 else if ( currentMenuOption[currentMenuNr] == MENU_GAME_DIFFICULTY )
807 { 807 {
808 currentGameDifficulty ++; 808 currentGameDifficulty ++;
809 if ( currentGameDifficulty == NR_GAME_DIFFICULTIES ) 809 if ( currentGameDifficulty == NR_GAME_DIFFICULTIES )
810 currentGameDifficulty = 0; 810 currentGameDifficulty = 0;
811 } 811 }
812 } 812 }
813 break; 813 break;
814 814
815 case Qt::Key_Space: 815 case Qt::Key_Space:
816 case Qt::Key_Return: 816 case Qt::Key_Return:
817 case Qt::Key_Enter: 817 case Qt::Key_Enter:
818 dealWithMenuSelection(); 818 dealWithMenuSelection();
819 break; 819 break;
820 } 820 }
821 } 821 }
822 else 822 else
823 { 823 {
824 switch( e->key() ) 824 switch( e->key() )
825 { 825 {
826 case Qt::Key_Up: 826 case Qt::Key_Up:
827 case Qt::Key_F9: 827 case Qt::Key_F9:
828 case Qt::Key_Space: 828 case Qt::Key_Space:
829 if ( !press ) 829 if ( !replay && !press )
830 { 830 {
831 press = true; 831 press = true;
832 replayList.append( new int( nrFrames ) ); 832 replayList.append( new int( nrFrames ) );
833 } 833 }
834 break; 834 break;
835 case Qt::Key_M: 835 case Qt::Key_M:
836 case Qt::Key_Return: 836 case Qt::Key_Return:
837 case Qt::Key_Enter: 837 case Qt::Key_Enter:
838 if ( state == STATE_CRASHED ) 838 if ( state == STATE_CRASHED )
839 state = STATE_MENU; 839 state = STATE_MENU;
840 break; 840 break;
841 841
842 case Qt::Key_Z: 842 case Qt::Key_Z:
843 showScoreZones = !showScoreZones; 843 showScoreZones = !showScoreZones;
844 break; 844 break;
845 845
846 default: 846 default:
847 e->ignore(); 847 e->ignore();
848 break; 848 break;
849 } 849 }
850 } 850 }
851} 851}
852 852
853void SFCave :: keyReleaseEvent( QKeyEvent *e ) 853void SFCave :: keyReleaseEvent( QKeyEvent *e )
854{ 854{
855 if ( state == STATE_MENU ) 855 if ( state == STATE_MENU )
856 { 856 {
857 } 857 }
858 else 858 else
859 { 859 {
860 switch( e->key() ) 860 switch( e->key() )
861 { 861 {
862 case Qt::Key_F9: 862 case Qt::Key_F9:
863 case Qt::Key_Space: 863 case Qt::Key_Space:
864 case Qt::Key_Up: 864 case Qt::Key_Up:
865 if ( press ) 865 if ( !replay && press )
866 { 866 {
867 press = false; 867 press = false;
868 868
869 replayList.append( new int( nrFrames ) ); 869 replayList.append( new int( nrFrames ) );
870 } 870 }
871 break; 871 break;
872 872
873 case Qt::Key_R: 873 case Qt::Key_R:
874 if ( state == STATE_CRASHED ) 874 if ( state == STATE_CRASHED )
875 { 875 {
876 state = STATE_REPLAY; 876 state = STATE_REPLAY;
877 } 877 }
878 break; 878 break;
879 879
880 case Qt::Key_Down: 880 case Qt::Key_Down:
881 if ( state == STATE_CRASHED ) 881 if ( state == STATE_CRASHED )
882 state = STATE_NEWGAME; 882 state = STATE_NEWGAME;
883 break; 883 break;
884 884
885 case Qt::Key_S: 885 case Qt::Key_S:
886 saveReplay(); 886 if ( state == STATE_CRASHED )
887 saveReplay();
887 break; 888 break;
888 889
889 case Qt::Key_L: 890 case Qt::Key_L:
890 loadReplay(); 891 if ( state == STATE_CRASHED )
892 loadReplay();
891 break; 893 break;
892 default: 894 default:
893 e->ignore(); 895 e->ignore();
894 break; 896 break;
895 } 897 }
896 } 898 }
897 899
898} 900}
899 901
900void SFCave :: displayMenu() 902void SFCave :: displayMenu()
901{ 903{
902 offscreen->fill( Qt::black ); 904 offscreen->fill( Qt::black );
903 905
904 QPainter p( offscreen ); 906 QPainter p( offscreen );
905 p.setPen( Qt::white ); 907 p.setPen( Qt::white );
906 908
907 QFont f( "Helvetica", 16 ); 909 QFont f( "Helvetica", 16 );
908 p.setFont( f ); 910 p.setFont( f );
909 911
910 QFontMetrics fm = p.fontMetrics(); 912 QFontMetrics fm = p.fontMetrics();
911 913
912 QString text = "SFCave"; 914 QString text = "SFCave";
913 p.drawText( (sWidth/2) - (fm.width( text )/2), 60, text ); 915 p.drawText( (sWidth/2) - (fm.width( text )/2), 60, text );
914 916
915 text = "Written by Andy Qua"; 917 text = "Written by Andy Qua";
916 p.drawText( (sWidth/2) - (fm.width( text )/2), 85, text ); 918 p.drawText( (sWidth/2) - (fm.width( text )/2), 85, text );
917 919
918 // Draw options 920 // Draw options
919 int pos = 170; 921 int pos = 170;
920 for ( int i = 0 ; menuOptions[currentMenuNr][i] != "" ; ++i, pos += 25 ) 922 for ( int i = 0 ; menuOptions[currentMenuNr][i] != "" ; ++i, pos += 25 )
921 { 923 {
922 if ( currentMenuOption[currentMenuNr] == i ) 924 if ( currentMenuOption[currentMenuNr] == i )
923 p.setPen( Qt::yellow ); 925 p.setPen( Qt::yellow );
924 else 926 else
925 p.setPen( Qt::white ); 927 p.setPen( Qt::white );
926 928
927 QString text; 929 QString text;
928 if ( menuOptions[currentMenuNr][i].find( "%s" ) != -1 ) 930 if ( menuOptions[currentMenuNr][i].find( "%s" ) != -1 )
929 { 931 {
930 QString val; 932 QString val;
931 if ( i == MENU_GAME_TYPE ) 933 if ( i == MENU_GAME_TYPE )
932 val = gameTypes[currentGameType]; 934 val = gameTypes[currentGameType];
933 else 935 else
934 val = dificultyOption[currentGameDifficulty]; 936 val = dificultyOption[currentGameDifficulty];
935 937
936 text.sprintf( (const char *)menuOptions[currentMenuNr][i], (const char *)val ); 938 text.sprintf( (const char *)menuOptions[currentMenuNr][i], (const char *)val );
937 } 939 }
938 else 940 else
@@ -1020,77 +1022,84 @@ void SFCave :: saveScore()
1020 1022
1021 cfg.writeEntry( key + gameTypes[currentGameType] + "_" + dificultyOption[currentGameDifficulty], highestScore[currentGameType][currentGameDifficulty] ); 1023 cfg.writeEntry( key + gameTypes[currentGameType] + "_" + dificultyOption[currentGameDifficulty], highestScore[currentGameType][currentGameDifficulty] );
1022 key += CURRENT_GAME_TYPE; 1024 key += CURRENT_GAME_TYPE;
1023 cfg.writeEntry( key, highestScore[currentGameType] ); 1025 cfg.writeEntry( key, highestScore[currentGameType] );
1024#endif 1026#endif
1025} 1027}
1026 1028
1027void SFCave :: saveReplay() 1029void SFCave :: saveReplay()
1028{ 1030{
1029 FILE *out; 1031 FILE *out;
1030 out = fopen( (const char *)replayFile, "w" ); 1032 out = fopen( (const char *)replayFile, "w" );
1031 if ( !out ) 1033 if ( !out )
1032 { 1034 {
1033 printf( "Couldn't write to /home/root/sfcave.replay\n" ); 1035 printf( "Couldn't write to /home/root/sfcave.replay\n" );
1034 return; 1036 return;
1035 } 1037 }
1036 1038
1037 // Build up string of values 1039 // Build up string of values
1038 // Format is:: <landscape seed> <framenr> <framenr>....... 1040 // Format is:: <landscape seed> <framenr> <framenr>.......
1039 QString val; 1041 QString val;
1040 val.sprintf( "%d ", currentSeed ); 1042 val.sprintf( "%d ", currentSeed );
1041 1043
1042 QListIterator<int> it( replayList ); 1044 QListIterator<int> it( replayList );
1043 while( it.current() ) 1045 while( it.current() )
1044 { 1046 {
1045 QString tmp; 1047 QString tmp;
1046 tmp.sprintf( "%d ", (*it.current()) ); 1048 tmp.sprintf( "%d ", (*it.current()) );
1047 val += tmp; 1049 val += tmp;
1048 1050
1049 ++it; 1051 ++it;
1050 } 1052 }
1051 val += "\n"; 1053 val += "\n";
1052 1054
1053 QString line; 1055 QString line;
1054 line.sprintf( "%d\n", val.length() ); 1056 line.sprintf( "%d\n", val.length() );
1055 fwrite( (const char *)line, 1, line.length(), out ); 1057 fwrite( (const char *)line, 1, line.length(), out );
1056 fwrite( (const char *)val, 1, val.length(), out ); 1058 fwrite( (const char *)val, 1, val.length(), out );
1057 1059
1058 fclose( out ); 1060 fclose( out );
1059 1061
1060 printf( "Replay saved to %s\n", (const char *)replayFile ); 1062 printf( "Replay saved to %s\n", (const char *)replayFile );
1061 1063
1062} 1064}
1063 1065
1064void SFCave :: loadReplay() 1066void SFCave :: loadReplay()
1065{ 1067{
1066 FILE *in = fopen( (const char *)replayFile, "r" ); 1068 FILE *in = fopen( (const char *)replayFile, "r" );
1067 1069
1070 if ( in == 0 )
1071 {
1072 printf( "Couldn't load replay file!\n" );
1073 return;
1074 }
1068 // Read size of next line 1075 // Read size of next line
1069 char line[10+1]; 1076 char line[10+1];
1070 fgets( line, 10, in ); 1077 fgets( line, 10, in );
1071 1078
1072 int length = -1; 1079 int length = -1;
1073 sscanf( line, "%d", &length ); 1080 sscanf( line, "%d", &length );
1074 char *data = new char[length+1]; 1081 char *data = new char[length+1];
1075 1082
1076 fread( data, 1, length, in ); 1083 fread( data, 1, length, in );
1077 1084
1078 QString sep = " "; 1085 QString sep = " ";
1079 QStringList list = QStringList::split( sep, QString( data ) ); 1086 QStringList list = QStringList::split( sep, QString( data ) );
1080 1087
1081 // print it out 1088 // print it out
1082 QStringList::Iterator it = list.begin(); 1089 QStringList::Iterator it = list.begin();
1083 currentSeed = (*it).toInt(); 1090 currentSeed = (*it).toInt();
1084 ++it; 1091 ++it;
1085 1092
1086 replayList.clear(); 1093 replayList.clear();
1087 for ( ; it != list.end(); ++it ) 1094 for ( ; it != list.end(); ++it )
1088 { 1095 {
1089 int v = (*it).toInt(); 1096 int v = (*it).toInt();
1090 replayList.append( new int( v ) ); 1097 replayList.append( new int( v ) );
1091 } 1098 }
1092 1099
1093 delete data; 1100 delete data;
1094 1101
1095 fclose( in ); 1102 fclose( in );
1103
1104 printf( "Replay loaded from %s\n", (const char *)replayFile );
1096} \ No newline at end of file 1105} \ No newline at end of file