summaryrefslogtreecommitdiff
path: root/noncore
authorandyq <andyq>2002-12-11 17:48:18 (UTC)
committer andyq <andyq>2002-12-11 17:48:18 (UTC)
commitd3d49c3022d763157a3c8309ddc5b9ce4ecde61a (patch) (unidiff)
tree67a8aa13cb3c41560c09e7cc7ca9f8ed169e05fc /noncore
parent719acab56c57f283168b955582ab5b30b9809b02 (diff)
downloadopie-d3d49c3022d763157a3c8309ddc5b9ce4ecde61a.zip
opie-d3d49c3022d763157a3c8309ddc5b9ce4ecde61a.tar.gz
opie-d3d49c3022d763157a3c8309ddc5b9ce4ecde61a.tar.bz2
Added save and load replays
Diffstat (limited to 'noncore') (more/less context) (show whitespace changes)
-rw-r--r--noncore/games/sfcave/sfcave.cpp101
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
@@ -9,11 +9,12 @@
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
@@ -136,6 +137,10 @@ SFCave :: SFCave( int spd, QWidget *w, char *name )
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;
@@ -683,10 +688,19 @@ void SFCave :: draw()
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 ++;
@@ -867,6 +881,14 @@ void SFCave :: keyReleaseEvent( QKeyEvent *e )
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;
@@ -1001,3 +1023,74 @@ void SFCave :: saveScore()
1001 cfg.writeEntry( key, highestScore[currentGameType] ); 1023 cfg.writeEntry( key, highestScore[currentGameType] );
1002#endif 1024#endif
1003} 1025}
1026
1027void 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
1064void 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