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 | |||
@@ -6,17 +6,18 @@ | |||
6 | #ifdef QWS | 6 | #ifdef QWS |
7 | #include <qpe/qpeapplication.h> | 7 | #include <qpe/qpeapplication.h> |
8 | #include <qpe/config.h> | 8 | #include <qpe/config.h> |
9 | #else | 9 | #else |
10 | #include <qapplication.h> | 10 | #include <qapplication.h> |
11 | #endif | 11 | #endif |
12 | #include <qdir.h> | ||
12 | 13 | ||
13 | #include "helpwindow.h" | 14 | #include "helpwindow.h" |
14 | #include "sfcave.h" | 15 | #include "sfcave.h" |
15 | 16 | ||
16 | #define CAPTION "SFCave 1.8 by AndyQ" | 17 | #define CAPTION "SFCave 1.10 by AndyQ" |
17 | 18 | ||
18 | #define UP_THRUST 0.6 | 19 | #define UP_THRUST 0.6 |
19 | #define NO_THRUST 0.8 | 20 | #define NO_THRUST 0.8 |
20 | #define MAX_DOWN_THRUST 4.0 | 21 | #define MAX_DOWN_THRUST 4.0 |
21 | #define MAX_UP_THRUST -3.5 | 22 | #define MAX_UP_THRUST -3.5 |
22 | 23 | ||
@@ -133,12 +134,16 @@ SFCave :: SFCave( int spd, QWidget *w, char *name ) | |||
133 | #ifdef QWS | 134 | #ifdef QWS |
134 | showMaximized(); | 135 | showMaximized(); |
135 | #else | 136 | #else |
136 | resize( 240, 284 ); | 137 | resize( 240, 284 ); |
137 | #endif | 138 | #endif |
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(); |
140 | sHeight = height(); | 145 | sHeight = height(); |
141 | segSize = sWidth/(MAPSIZE-1)+1; | 146 | segSize = sWidth/(MAPSIZE-1)+1; |
142 | 147 | ||
143 | currentMenuNr = 0; | 148 | currentMenuNr = 0; |
144 | currentGameType = 0; | 149 | currentGameType = 0; |
@@ -680,16 +685,25 @@ void SFCave :: draw() | |||
680 | if ( state == STATE_CRASHING && crashLineLength >= 15 ) //|| crashLineLength == -1) ) | 685 | if ( state == STATE_CRASHING && crashLineLength >= 15 ) //|| crashLineLength == -1) ) |
681 | state = STATE_CRASHED; | 686 | state = STATE_CRASHED; |
682 | 687 | ||
683 | if ( state == STATE_CRASHED ) | 688 | if ( state == STATE_CRASHED ) |
684 | { | 689 | { |
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 | } |
691 | else | 705 | else |
692 | crashLineLength ++; | 706 | crashLineLength ++; |
693 | } | 707 | } |
694 | 708 | ||
695 | p.end(); | 709 | p.end(); |
@@ -864,12 +878,20 @@ void SFCave :: keyReleaseEvent( QKeyEvent *e ) | |||
864 | break; | 878 | break; |
865 | 879 | ||
866 | case Qt::Key_Down: | 880 | case Qt::Key_Down: |
867 | if ( state == STATE_CRASHED ) | 881 | if ( state == STATE_CRASHED ) |
868 | state = STATE_NEWGAME; | 882 | state = STATE_NEWGAME; |
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: |
871 | e->ignore(); | 893 | e->ignore(); |
872 | break; | 894 | break; |
873 | } | 895 | } |
874 | } | 896 | } |
875 | 897 | ||
@@ -998,6 +1020,77 @@ void SFCave :: saveScore() | |||
998 | 1020 | ||
999 | cfg.writeEntry( key + gameTypes[currentGameType] + "_" + dificultyOption[currentGameDifficulty], highestScore[currentGameType][currentGameDifficulty] ); | 1021 | cfg.writeEntry( key + gameTypes[currentGameType] + "_" + dificultyOption[currentGameDifficulty], highestScore[currentGameType][currentGameDifficulty] ); |
1000 | key += CURRENT_GAME_TYPE; | 1022 | key += CURRENT_GAME_TYPE; |
1001 | cfg.writeEntry( key, highestScore[currentGameType] ); | 1023 | cfg.writeEntry( key, highestScore[currentGameType] ); |
1002 | #endif | 1024 | #endif |
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 | ||