summaryrefslogtreecommitdiff
authorandyq <andyq>2002-12-11 17:48:18 (UTC)
committer andyq <andyq>2002-12-11 17:48:18 (UTC)
commitd3d49c3022d763157a3c8309ddc5b9ce4ecde61a (patch) (unidiff)
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,28 +1,29 @@
1#include <stdio.h> 1#include <stdio.h>
2#include <stdlib.h> 2#include <stdlib.h>
3#include <math.h> 3#include <math.h>
4#include <time.h> 4#include <time.h>
5 5
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
23// States 24// States
24#define STATE_BOSS 0 25#define STATE_BOSS 0
25#define STATE_RUNNING 1 26#define STATE_RUNNING 1
26#define STATE_CRASHING 2 27#define STATE_CRASHING 2
27#define STATE_CRASHED 3 28#define STATE_CRASHED 3
28#define STATE_NEWGAME 4 29#define STATE_NEWGAME 4
@@ -127,24 +128,28 @@ int main( int argc, char *argv[] )
127 128
128SFCave :: SFCave( int spd, QWidget *w, char *name ) 129SFCave :: SFCave( int spd, QWidget *w, char *name )
129 : QMainWindow( w, name ) 130 : QMainWindow( w, name )
130 131
131{ 132{
132 replayIt = 0; 133 replayIt = 0;
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;
145 currentGameDifficulty = 0; 150 currentGameDifficulty = 0;
146 151
147 setCaption( CAPTION ); 152 setCaption( CAPTION );
148 showScoreZones = false; 153 showScoreZones = false;
149 154
150#ifdef QWS 155#ifdef QWS
@@ -674,28 +679,37 @@ void SFCave :: draw()
674 { 679 {
675 int x = (int)(user.x() + (crashLineLength+nextInt(10)) * cos( (M_PI/180) * (10.0 * i) ) ); 680 int x = (int)(user.x() + (crashLineLength+nextInt(10)) * cos( (M_PI/180) * (10.0 * i) ) );
676 int y = (int)(user.y() + (crashLineLength+nextInt(10)) * sin( (M_PI/180) * (10.0 * i) ) ); p.drawLine( user.x(), user.y(), x, y ); 681 int y = (int)(user.y() + (crashLineLength+nextInt(10)) * sin( (M_PI/180) * (10.0 * i) ) ); p.drawLine( user.x(), user.y(), x, y );
677 } 682 }
678 } 683 }
679 684
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 );
687 695
688 text = "or press OK for menu"; 696 text = "Press r to replay";
689 p.drawText( (sWidth/2) - (fm.width( text )/2), 155, text ); 697 p.drawText( (sWidth/2) - (fm.width( text )/2), 150, text );
698
699 text = "Press s to save the replay";
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();
696 bitBlt( this, 0, 0, offscreen, 0, 0, sWidth, sHeight, Qt::CopyROP, true ); 710 bitBlt( this, 0, 0, offscreen, 0, 0, sWidth, sHeight, Qt::CopyROP, true );
697 //printf( "endpaint\n" ); 711 //printf( "endpaint\n" );
698} 712}
699 713
700void SFCave :: handleKeys() 714void SFCave :: handleKeys()
701{ 715{
@@ -858,24 +872,32 @@ void SFCave :: keyReleaseEvent( QKeyEvent *e )
858 872
859 case Qt::Key_R: 873 case Qt::Key_R:
860 if ( state == STATE_CRASHED ) 874 if ( state == STATE_CRASHED )
861 { 875 {
862 state = STATE_REPLAY; 876 state = STATE_REPLAY;
863 } 877 }
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
876} 898}
877 899
878void SFCave :: displayMenu() 900void SFCave :: displayMenu()
879{ 901{
880 offscreen->fill( Qt::black ); 902 offscreen->fill( Qt::black );
881 903
@@ -992,12 +1014,83 @@ void SFCave :: dealWithMenuSelection()
992void SFCave :: saveScore() 1014void SFCave :: saveScore()
993{ 1015{
994#ifdef QWS 1016#ifdef QWS
995 Config cfg( "sfcave" ); 1017 Config cfg( "sfcave" );
996 cfg.setGroup( "settings" ); 1018 cfg.setGroup( "settings" );
997 QString key = "highScore_"; 1019 QString key = "highScore_";
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
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