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) (ignore 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
@@ -1,40 +1,41 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#ifdef QWS
#include <qpe/qpeapplication.h>
#include <qpe/config.h>
#else
#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
#define NO_THRUST 0.8
#define MAX_DOWN_THRUST 4.0
#define MAX_UP_THRUST -3.5
// States
#define STATE_BOSS 0
#define STATE_RUNNING 1
#define STATE_CRASHING 2
#define STATE_CRASHED 3
#define STATE_NEWGAME 4
#define STATE_MENU 5
#define STATE_REPLAY 6
// Menus
#define MENU_MAIN_MENU 0
#define MENU_OPTIONS_MENU 1
// Main Menu Options
#define MENU_START_GAME 0
#define MENU_OPTIONS 1
#define MENU_HELP 2
#define MENU_QUIT 3
@@ -115,48 +116,52 @@ int main( int argc, char *argv[] )
{
if ( i+1 < argc )
speed = atoi( argv[i+1] );
}
}
SFCave app( speed );
a.setMainWidget( &app );
app.show();
app.start();
a.exec();
}
SFCave :: SFCave( int spd, QWidget *w, char *name )
: QMainWindow( w, name )
{
replayIt = 0;
#ifdef QWS
showMaximized();
#else
resize( 240, 284 );
#endif
+ replayFile = QDir::home().path();
+ replayFile += "/sfcave.replay";
+ printf( "%s\n", (const char *)replayFile );
+
sWidth = width();
sHeight = height();
segSize = sWidth/(MAPSIZE-1)+1;
currentMenuNr = 0;
currentGameType = 0;
currentGameDifficulty = 0;
setCaption( CAPTION );
showScoreZones = false;
#ifdef QWS
Config cfg( "sfcave" );
cfg.setGroup( "settings" );
QString key = "highScore_";
for ( int i = 0 ; i < 3 ; ++i )
{
for ( int j = 0 ; j < 3 ; ++j )
highestScore[i][j] = cfg.readNumEntry( key + gameTypes[i] + "_" + dificultyOption[j], 0 );
}
currentGameType = cfg.readNumEntry( "gameType", 0 );
currentGameDifficulty = cfg.readNumEntry( "difficulty", 0 );
@@ -662,52 +667,61 @@ void SFCave :: draw()
QString s;
s.sprintf( "score %06d high score %06d", score, highestScore[currentGameType][currentGameDifficulty] );
p.drawText( 5, 10, s );
if ( state == STATE_CRASHING || state == STATE_CRASHED )
{
// add next crash line
if ( crashLineLength != -1 )
{
for ( int i = 0 ; i < 36 ; ++i )
{
int x = (int)(user.x() + (crashLineLength+nextInt(10)) * cos( (M_PI/180) * (10.0 * i) ) );
int y = (int)(user.y() + (crashLineLength+nextInt(10)) * sin( (M_PI/180) * (10.0 * i) ) ); p.drawLine( user.x(), user.y(), x, y );
}
}
if ( state == STATE_CRASHING && crashLineLength >= 15 ) //|| crashLineLength == -1) )
state = STATE_CRASHED;
if ( state == STATE_CRASHED )
{
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 = "or press OK for menu";
- p.drawText( (sWidth/2) - (fm.width( text )/2), 155, text );
+ text = "Press r to replay";
+ p.drawText( (sWidth/2) - (fm.width( text )/2), 150, 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
crashLineLength ++;
}
p.end();
bitBlt( this, 0, 0, offscreen, 0, 0, sWidth, sHeight, Qt::CopyROP, true );
//printf( "endpaint\n" );
}
void SFCave :: handleKeys()
{
// Find enpty trail and move others
bool done = false;
for ( int i = 0 ; i < TRAILSIZE ; ++i )
{
if ( trail[i].x() < 0 )
{
if ( !done )
{
trail[i].setX( user.x() - 5 );
trail[i].setY( user.y() );
done = true;
}
@@ -846,48 +860,56 @@ void SFCave :: keyReleaseEvent( QKeyEvent *e )
switch( e->key() )
{
case Qt::Key_F9:
case Qt::Key_Space:
case Qt::Key_Up:
if ( press )
{
press = false;
replayList.append( new int( nrFrames ) );
}
break;
case Qt::Key_R:
if ( state == STATE_CRASHED )
{
state = STATE_REPLAY;
}
break;
case Qt::Key_Down:
if ( state == STATE_CRASHED )
state = STATE_NEWGAME;
break;
+
+ case Qt::Key_S:
+ saveReplay();
+ break;
+
+ case Qt::Key_L:
+ loadReplay();
+ break;
default:
e->ignore();
break;
}
}
}
void SFCave :: displayMenu()
{
offscreen->fill( Qt::black );
QPainter p( offscreen );
p.setPen( Qt::white );
QFont f( "Helvetica", 16 );
p.setFont( f );
QFontMetrics fm = p.fontMetrics();
QString text = "SFCave";
p.drawText( (sWidth/2) - (fm.width( text )/2), 60, text );
text = "Written by Andy Qua";
@@ -980,24 +1002,95 @@ void SFCave :: dealWithMenuSelection()
cfg.setGroup( "settings" );
cfg.writeEntry( "difficulty", currentGameDifficulty );
cfg.writeEntry( "gameType", currentGameType );
#endif
break;
}
break;
}
}
}
void SFCave :: saveScore()
{
#ifdef QWS
Config cfg( "sfcave" );
cfg.setGroup( "settings" );
QString key = "highScore_";
cfg.writeEntry( key + gameTypes[currentGameType] + "_" + dificultyOption[currentGameDifficulty], highestScore[currentGameType][currentGameDifficulty] );
key += CURRENT_GAME_TYPE;
cfg.writeEntry( key, highestScore[currentGameType] );
#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