summaryrefslogtreecommitdiff
authorandyq <andyq>2002-12-11 17:58:53 (UTC)
committer andyq <andyq>2002-12-11 17:58:53 (UTC)
commitf4865ac8e114baedd13c95160631e4574da9dc84 (patch) (unidiff)
treeecc74f212ffde8b9733ad6a7dd9e2360717917ef
parent676f39d78772b19d11471db55ece82262278f942 (diff)
downloadopie-f4865ac8e114baedd13c95160631e4574da9dc84.zip
opie-f4865ac8e114baedd13c95160631e4574da9dc84.tar.gz
opie-f4865ac8e114baedd13c95160631e4574da9dc84.tar.bz2
Added check for no replay file on loading and fixed bugs where you could screw up the replay by pressing stuff
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
@@ -733,364 +733,373 @@ void SFCave :: handleKeys()
733 } 733 }
734 734
735 if ( speed <= 3 ) 735 if ( speed <= 3 )
736 { 736 {
737 if ( press ) 737 if ( press )
738 thrust -= thrustUp; 738 thrust -= thrustUp;
739 else 739 else
740 thrust += noThrust; 740 thrust += noThrust;
741 741
742 if ( thrust > maxDownThrust ) 742 if ( thrust > maxDownThrust )
743 thrust = maxDownThrust; 743 thrust = maxDownThrust;
744 else if ( thrust < maxUpThrust ) 744 else if ( thrust < maxUpThrust )
745 thrust = maxUpThrust; 745 thrust = maxUpThrust;
746 } 746 }
747 else 747 else
748 { 748 {
749 if ( press ) 749 if ( press )
750 thrust -= 0.5; 750 thrust -= 0.5;
751 else 751 else
752 thrust += 0.8; 752 thrust += 0.8;
753 753
754 if ( thrust > 5.0 ) 754 if ( thrust > 5.0 )
755 thrust = 5.0; 755 thrust = 5.0;
756 else if ( thrust < -3.5 ) 756 else if ( thrust < -3.5 )
757 thrust = -3.5; 757 thrust = -3.5;
758 } 758 }
759 user.moveBy( 0, (int)thrust ); 759 user.moveBy( 0, (int)thrust );
760} 760}
761 761
762void SFCave :: keyPressEvent( QKeyEvent *e ) 762void SFCave :: keyPressEvent( QKeyEvent *e )
763{ 763{
764 if ( state == STATE_MENU ) 764 if ( state == STATE_MENU )
765 { 765 {
766 switch( e->key() ) 766 switch( e->key() )
767 { 767 {
768 case Qt::Key_Down: 768 case Qt::Key_Down:
769 currentMenuOption[currentMenuNr] ++; 769 currentMenuOption[currentMenuNr] ++;
770 if ( menuOptions[currentMenuNr][currentMenuOption[currentMenuNr]] == "" ) 770 if ( menuOptions[currentMenuNr][currentMenuOption[currentMenuNr]] == "" )
771 currentMenuOption[currentMenuNr] = 0; 771 currentMenuOption[currentMenuNr] = 0;
772 break; 772 break;
773 case Qt::Key_Up: 773 case Qt::Key_Up:
774 currentMenuOption[currentMenuNr] --; 774 currentMenuOption[currentMenuNr] --;
775 if ( currentMenuOption[currentMenuNr] < 0 ) 775 if ( currentMenuOption[currentMenuNr] < 0 )
776 currentMenuOption[currentMenuNr] = nrMenuOptions[currentMenuNr]-1; 776 currentMenuOption[currentMenuNr] = nrMenuOptions[currentMenuNr]-1;
777 break; 777 break;
778 778
779 case Qt::Key_Left: 779 case Qt::Key_Left:
780 if ( currentMenuNr == MENU_OPTIONS_MENU ) 780 if ( currentMenuNr == MENU_OPTIONS_MENU )
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
939 text = menuOptions[currentMenuNr][i]; 941 text = menuOptions[currentMenuNr][i];
940 942
941 p.drawText( (sWidth/2) - (fm.width( text )/2), pos, text ); 943 p.drawText( (sWidth/2) - (fm.width( text )/2), pos, text );
942 } 944 }
943 945
944 p.end(); 946 p.end();
945 bitBlt( this, 0, 0, offscreen, 0, 0, sWidth, sHeight, Qt::CopyROP, true ); 947 bitBlt( this, 0, 0, offscreen, 0, 0, sWidth, sHeight, Qt::CopyROP, true );
946} 948}
947 949
948void SFCave :: dealWithMenuSelection() 950void SFCave :: dealWithMenuSelection()
949{ 951{
950 switch( currentMenuNr ) 952 switch( currentMenuNr )
951 { 953 {
952 case MENU_MAIN_MENU: 954 case MENU_MAIN_MENU:
953 { 955 {
954 switch( currentMenuOption[currentMenuNr] ) 956 switch( currentMenuOption[currentMenuNr] )
955 { 957 {
956 case MENU_START_GAME: 958 case MENU_START_GAME:
957 state = STATE_NEWGAME; 959 state = STATE_NEWGAME;
958 break; 960 break;
959 961
960 case MENU_OPTIONS: 962 case MENU_OPTIONS:
961 currentMenuNr = MENU_OPTIONS_MENU; 963 currentMenuNr = MENU_OPTIONS_MENU;
962 currentMenuOption[currentMenuNr] = 0; 964 currentMenuOption[currentMenuNr] = 0;
963 break; 965 break;
964 966
965 case MENU_HELP: 967 case MENU_HELP:
966 { 968 {
967 // Display Help Menu 969 // Display Help Menu
968 HelpWindow *dlg = new HelpWindow( this ); 970 HelpWindow *dlg = new HelpWindow( this );
969 dlg->exec(); 971 dlg->exec();
970 delete dlg; 972 delete dlg;
971 break; 973 break;
972 } 974 }
973 975
974 case MENU_QUIT: 976 case MENU_QUIT:
975 QApplication::exit(); 977 QApplication::exit();
976 break; 978 break;
977 } 979 }
978 980
979 break; 981 break;
980 } 982 }
981 983
982 case MENU_OPTIONS_MENU: 984 case MENU_OPTIONS_MENU:
983 { 985 {
984 switch( currentMenuOption[currentMenuNr] ) 986 switch( currentMenuOption[currentMenuNr] )
985 { 987 {
986 case MENU_GAME_TYPE: 988 case MENU_GAME_TYPE:
987 break; 989 break;
988 990
989 case MENU_GAME_DIFFICULTY: 991 case MENU_GAME_DIFFICULTY:
990 break; 992 break;
991 993
992 case MENU_CLEAR_HIGHSCORES: 994 case MENU_CLEAR_HIGHSCORES:
993 for ( int i = 0 ; i < 3 ; ++i ) 995 for ( int i = 0 ; i < 3 ; ++i )
994 highestScore[currentGameType][i] = 0; 996 highestScore[currentGameType][i] = 0;
995 break; 997 break;
996 998
997 case MENU_BACK: 999 case MENU_BACK:
998 currentMenuNr = MENU_MAIN_MENU; 1000 currentMenuNr = MENU_MAIN_MENU;
999 1001
1000#ifdef QWS 1002#ifdef QWS
1001 Config cfg( "sfcave" ); 1003 Config cfg( "sfcave" );
1002 cfg.setGroup( "settings" ); 1004 cfg.setGroup( "settings" );
1003 cfg.writeEntry( "difficulty", currentGameDifficulty ); 1005 cfg.writeEntry( "difficulty", currentGameDifficulty );
1004 cfg.writeEntry( "gameType", currentGameType ); 1006 cfg.writeEntry( "gameType", currentGameType );
1005#endif 1007#endif
1006 break; 1008 break;
1007 } 1009 }
1008 1010
1009 break; 1011 break;
1010 } 1012 }
1011 } 1013 }
1012} 1014}
1013 1015
1014void SFCave :: saveScore() 1016void SFCave :: saveScore()
1015{ 1017{
1016#ifdef QWS 1018#ifdef QWS
1017 Config cfg( "sfcave" ); 1019 Config cfg( "sfcave" );
1018 cfg.setGroup( "settings" ); 1020 cfg.setGroup( "settings" );
1019 QString key = "highScore_"; 1021 QString key = "highScore_";
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