author | andyq <andyq> | 2002-12-11 17:48:18 (UTC) |
---|---|---|
committer | andyq <andyq> | 2002-12-11 17:48:18 (UTC) |
commit | d3d49c3022d763157a3c8309ddc5b9ce4ecde61a (patch) (unidiff) | |
tree | 67a8aa13cb3c41560c09e7cc7ca9f8ed169e05fc /noncore | |
parent | 719acab56c57f283168b955582ab5b30b9809b02 (diff) | |
download | opie-d3d49c3022d763157a3c8309ddc5b9ce4ecde61a.zip opie-d3d49c3022d763157a3c8309ddc5b9ce4ecde61a.tar.gz opie-d3d49c3022d763157a3c8309ddc5b9ce4ecde61a.tar.bz2 |
Added save and load replays
-rw-r--r-- | noncore/games/sfcave/sfcave.cpp | 101 |
1 files changed, 97 insertions, 4 deletions
diff --git a/noncore/games/sfcave/sfcave.cpp b/noncore/games/sfcave/sfcave.cpp index b5bc736..93f5f82 100644 --- a/noncore/games/sfcave/sfcave.cpp +++ b/noncore/games/sfcave/sfcave.cpp | |||
@@ -11,2 +11,3 @@ | |||
11 | #endif | 11 | #endif |
12 | #include <qdir.h> | ||
12 | 13 | ||
@@ -15,3 +16,3 @@ | |||
15 | 16 | ||
16 | #define CAPTION "SFCave 1.8 by AndyQ" | 17 | #define CAPTION "SFCave 1.10 by AndyQ" |
17 | 18 | ||
@@ -138,2 +139,6 @@ SFCave :: SFCave( int spd, QWidget *w, char *name ) | |||
138 | 139 | ||
140 | replayFile = QDir::home().path(); | ||
141 | replayFile += "/sfcave.replay"; | ||
142 | printf( "%s\n", (const char *)replayFile ); | ||
143 | |||
139 | sWidth = width(); | 144 | sWidth = width(); |
@@ -685,6 +690,15 @@ void SFCave :: draw() | |||
685 | QString text = "Press up or down to start"; | 690 | QString text = "Press up or down to start"; |
686 | p.drawText( (sWidth/2) - (fm.width( text )/2), 140, text ); | 691 | p.drawText( (sWidth/2) - (fm.width( text )/2), 120, text ); |
692 | |||
693 | text = "Press OK for menu"; | ||
694 | p.drawText( (sWidth/2) - (fm.width( text )/2), 135, text ); | ||
695 | |||
696 | text = "Press r to replay"; | ||
697 | p.drawText( (sWidth/2) - (fm.width( text )/2), 150, text ); | ||
687 | 698 | ||
688 | text = "or press OK for menu"; | 699 | text = "Press s to save the replay"; |
689 | p.drawText( (sWidth/2) - (fm.width( text )/2), 155, text ); | 700 | p.drawText( (sWidth/2) - (fm.width( text )/2), 165, text ); |
701 | |||
702 | text = "Press r to load a saved replay"; | ||
703 | p.drawText( (sWidth/2) - (fm.width( text )/2), 180, text ); | ||
690 | } | 704 | } |
@@ -869,2 +883,10 @@ void SFCave :: keyReleaseEvent( QKeyEvent *e ) | |||
869 | break; | 883 | break; |
884 | |||
885 | case Qt::Key_S: | ||
886 | saveReplay(); | ||
887 | break; | ||
888 | |||
889 | case Qt::Key_L: | ||
890 | loadReplay(); | ||
891 | break; | ||
870 | default: | 892 | default: |
@@ -1003 +1025,72 @@ void SFCave :: saveScore() | |||
1003 | } | 1025 | } |
1026 | |||
1027 | void SFCave :: saveReplay() | ||
1028 | { | ||
1029 | FILE *out; | ||
1030 | out = fopen( (const char *)replayFile, "w" ); | ||
1031 | if ( !out ) | ||
1032 | { | ||
1033 | printf( "Couldn't write to /home/root/sfcave.replay\n" ); | ||
1034 | return; | ||
1035 | } | ||
1036 | |||
1037 | // Build up string of values | ||
1038 | // Format is:: <landscape seed> <framenr> <framenr>....... | ||
1039 | QString val; | ||
1040 | val.sprintf( "%d ", currentSeed ); | ||
1041 | |||
1042 | QListIterator<int> it( replayList ); | ||
1043 | while( it.current() ) | ||
1044 | { | ||
1045 | QString tmp; | ||
1046 | tmp.sprintf( "%d ", (*it.current()) ); | ||
1047 | val += tmp; | ||
1048 | |||
1049 | ++it; | ||
1050 | } | ||
1051 | val += "\n"; | ||
1052 | |||
1053 | QString line; | ||
1054 | line.sprintf( "%d\n", val.length() ); | ||
1055 | fwrite( (const char *)line, 1, line.length(), out ); | ||
1056 | fwrite( (const char *)val, 1, val.length(), out ); | ||
1057 | |||
1058 | fclose( out ); | ||
1059 | |||
1060 | printf( "Replay saved to %s\n", (const char *)replayFile ); | ||
1061 | |||
1062 | } | ||
1063 | |||
1064 | void SFCave :: loadReplay() | ||
1065 | { | ||
1066 | FILE *in = fopen( (const char *)replayFile, "r" ); | ||
1067 | |||
1068 | // Read size of next line | ||
1069 | char line[10+1]; | ||
1070 | fgets( line, 10, in ); | ||
1071 | |||
1072 | int length = -1; | ||
1073 | sscanf( line, "%d", &length ); | ||
1074 | char *data = new char[length+1]; | ||
1075 | |||
1076 | fread( data, 1, length, in ); | ||
1077 | |||
1078 | QString sep = " "; | ||
1079 | QStringList list = QStringList::split( sep, QString( data ) ); | ||
1080 | |||
1081 | // print it out | ||
1082 | QStringList::Iterator it = list.begin(); | ||
1083 | currentSeed = (*it).toInt(); | ||
1084 | ++it; | ||
1085 | |||
1086 | replayList.clear(); | ||
1087 | for ( ; it != list.end(); ++it ) | ||
1088 | { | ||
1089 | int v = (*it).toInt(); | ||
1090 | replayList.append( new int( v ) ); | ||
1091 | } | ||
1092 | |||
1093 | delete data; | ||
1094 | |||
1095 | fclose( in ); | ||
1096 | } \ No newline at end of file | ||