summaryrefslogtreecommitdiff
authorandyq <andyq>2002-12-11 17:48:18 (UTC)
committer andyq <andyq>2002-12-11 17:48:18 (UTC)
commitd3d49c3022d763157a3c8309ddc5b9ce4ecde61a (patch) (side-by-side diff)
tree67a8aa13cb3c41560c09e7cc7ca9f8ed169e05fc
parent719acab56c57f283168b955582ab5b30b9809b02 (diff)
downloadopie-d3d49c3022d763157a3c8309ddc5b9ce4ecde61a.zip
opie-d3d49c3022d763157a3c8309ddc5b9ce4ecde61a.tar.gz
opie-d3d49c3022d763157a3c8309ddc5b9ce4ecde61a.tar.bz2
Added save and load replays
Diffstat (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
@@ -10,9 +10,10 @@
#include <qapplication.h>
#endif
+#include <qdir.h>
#include "helpwindow.h"
#include "sfcave.h"
-#define CAPTION "SFCave 1.8 by AndyQ"
+#define CAPTION "SFCave 1.10 by AndyQ"
#define UP_THRUST 0.6
@@ -137,4 +138,8 @@ SFCave :: SFCave( int spd, QWidget *w, char *name )
#endif
+ replayFile = QDir::home().path();
+ replayFile += "/sfcave.replay";
+ printf( "%s\n", (const char *)replayFile );
+
sWidth = width();
sHeight = height();
@@ -684,8 +689,17 @@ void SFCave :: draw()
{
QString text = "Press up or down to start";
- p.drawText( (sWidth/2) - (fm.width( text )/2), 140, text );
+ p.drawText( (sWidth/2) - (fm.width( text )/2), 120, text );
+
+ text = "Press OK for menu";
+ p.drawText( (sWidth/2) - (fm.width( text )/2), 135, text );
+
+ text = "Press r to replay";
+ p.drawText( (sWidth/2) - (fm.width( text )/2), 150, text );
- text = "or press OK for menu";
- p.drawText( (sWidth/2) - (fm.width( text )/2), 155, text );
+ text = "Press s to save the replay";
+ p.drawText( (sWidth/2) - (fm.width( text )/2), 165, text );
+
+ text = "Press r to load a saved replay";
+ p.drawText( (sWidth/2) - (fm.width( text )/2), 180, text );
}
else
@@ -868,4 +882,12 @@ void SFCave :: keyReleaseEvent( QKeyEvent *e )
state = STATE_NEWGAME;
break;
+
+ case Qt::Key_S:
+ saveReplay();
+ break;
+
+ case Qt::Key_L:
+ loadReplay();
+ break;
default:
e->ignore();
@@ -1002,2 +1024,73 @@ void SFCave :: saveScore()
#endif
}
+
+void SFCave :: saveReplay()
+{
+ FILE *out;
+ out = fopen( (const char *)replayFile, "w" );
+ if ( !out )
+ {
+ printf( "Couldn't write to /home/root/sfcave.replay\n" );
+ return;
+ }
+
+ // Build up string of values
+ // Format is:: <landscape seed> <framenr> <framenr>.......
+ QString val;
+ val.sprintf( "%d ", currentSeed );
+
+ QListIterator<int> it( replayList );
+ while( it.current() )
+ {
+ QString tmp;
+ tmp.sprintf( "%d ", (*it.current()) );
+ val += tmp;
+
+ ++it;
+ }
+ val += "\n";
+
+ QString line;
+ line.sprintf( "%d\n", val.length() );
+ fwrite( (const char *)line, 1, line.length(), out );
+ fwrite( (const char *)val, 1, val.length(), out );
+
+ fclose( out );
+
+ printf( "Replay saved to %s\n", (const char *)replayFile );
+
+}
+
+void SFCave :: loadReplay()
+{
+ FILE *in = fopen( (const char *)replayFile, "r" );
+
+ // Read size of next line
+ char line[10+1];
+ fgets( line, 10, in );
+
+ int length = -1;
+ sscanf( line, "%d", &length );
+ char *data = new char[length+1];
+
+ fread( data, 1, length, in );
+
+ QString sep = " ";
+ QStringList list = QStringList::split( sep, QString( data ) );
+
+ // print it out
+ QStringList::Iterator it = list.begin();
+ currentSeed = (*it).toInt();
+ ++it;
+
+ replayList.clear();
+ for ( ; it != list.end(); ++it )
+ {
+ int v = (*it).toInt();
+ replayList.append( new int( v ) );
+ }
+
+ delete data;
+
+ fclose( in );
+} \ No newline at end of file