-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 | |||
@@ -1,40 +1,41 @@ | |||
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 |
29 | #define STATE_MENU 5 | 30 | #define STATE_MENU 5 |
30 | #define STATE_REPLAY 6 | 31 | #define STATE_REPLAY 6 |
31 | 32 | ||
32 | // Menus | 33 | // Menus |
33 | #define MENU_MAIN_MENU 0 | 34 | #define MENU_MAIN_MENU 0 |
34 | #define MENU_OPTIONS_MENU 1 | 35 | #define MENU_OPTIONS_MENU 1 |
35 | 36 | ||
36 | // Main Menu Options | 37 | // Main Menu Options |
37 | #define MENU_START_GAME 0 | 38 | #define MENU_START_GAME 0 |
38 | #define MENU_OPTIONS 1 | 39 | #define MENU_OPTIONS 1 |
39 | #define MENU_HELP 2 | 40 | #define MENU_HELP 2 |
40 | #define MENU_QUIT 3 | 41 | #define MENU_QUIT 3 |
@@ -115,48 +116,52 @@ int main( int argc, char *argv[] ) | |||
115 | { | 116 | { |
116 | if ( i+1 < argc ) | 117 | if ( i+1 < argc ) |
117 | speed = atoi( argv[i+1] ); | 118 | speed = atoi( argv[i+1] ); |
118 | } | 119 | } |
119 | } | 120 | } |
120 | 121 | ||
121 | SFCave app( speed ); | 122 | SFCave app( speed ); |
122 | a.setMainWidget( &app ); | 123 | a.setMainWidget( &app ); |
123 | app.show(); | 124 | app.show(); |
124 | app.start(); | 125 | app.start(); |
125 | a.exec(); | 126 | a.exec(); |
126 | } | 127 | } |
127 | 128 | ||
128 | SFCave :: SFCave( int spd, QWidget *w, char *name ) | 129 | SFCave :: 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 |
151 | Config cfg( "sfcave" ); | 156 | Config cfg( "sfcave" ); |
152 | cfg.setGroup( "settings" ); | 157 | cfg.setGroup( "settings" ); |
153 | QString key = "highScore_"; | 158 | QString key = "highScore_"; |
154 | 159 | ||
155 | for ( int i = 0 ; i < 3 ; ++i ) | 160 | for ( int i = 0 ; i < 3 ; ++i ) |
156 | { | 161 | { |
157 | for ( int j = 0 ; j < 3 ; ++j ) | 162 | for ( int j = 0 ; j < 3 ; ++j ) |
158 | highestScore[i][j] = cfg.readNumEntry( key + gameTypes[i] + "_" + dificultyOption[j], 0 ); | 163 | highestScore[i][j] = cfg.readNumEntry( key + gameTypes[i] + "_" + dificultyOption[j], 0 ); |
159 | } | 164 | } |
160 | 165 | ||
161 | currentGameType = cfg.readNumEntry( "gameType", 0 ); | 166 | currentGameType = cfg.readNumEntry( "gameType", 0 ); |
162 | currentGameDifficulty = cfg.readNumEntry( "difficulty", 0 ); | 167 | currentGameDifficulty = cfg.readNumEntry( "difficulty", 0 ); |
@@ -662,52 +667,61 @@ void SFCave :: draw() | |||
662 | QString s; | 667 | QString s; |
663 | s.sprintf( "score %06d high score %06d", score, highestScore[currentGameType][currentGameDifficulty] ); | 668 | s.sprintf( "score %06d high score %06d", score, highestScore[currentGameType][currentGameDifficulty] ); |
664 | p.drawText( 5, 10, s ); | 669 | p.drawText( 5, 10, s ); |
665 | 670 | ||
666 | 671 | ||
667 | if ( state == STATE_CRASHING || state == STATE_CRASHED ) | 672 | if ( state == STATE_CRASHING || state == STATE_CRASHED ) |
668 | { | 673 | { |
669 | // add next crash line | 674 | // add next crash line |
670 | 675 | ||
671 | if ( crashLineLength != -1 ) | 676 | if ( crashLineLength != -1 ) |
672 | { | 677 | { |
673 | for ( int i = 0 ; i < 36 ; ++i ) | 678 | for ( int i = 0 ; i < 36 ; ++i ) |
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 | ||
700 | void SFCave :: handleKeys() | 714 | void SFCave :: handleKeys() |
701 | { | 715 | { |
702 | // Find enpty trail and move others | 716 | // Find enpty trail and move others |
703 | bool done = false; | 717 | bool done = false; |
704 | for ( int i = 0 ; i < TRAILSIZE ; ++i ) | 718 | for ( int i = 0 ; i < TRAILSIZE ; ++i ) |
705 | { | 719 | { |
706 | if ( trail[i].x() < 0 ) | 720 | if ( trail[i].x() < 0 ) |
707 | { | 721 | { |
708 | if ( !done ) | 722 | if ( !done ) |
709 | { | 723 | { |
710 | trail[i].setX( user.x() - 5 ); | 724 | trail[i].setX( user.x() - 5 ); |
711 | trail[i].setY( user.y() ); | 725 | trail[i].setY( user.y() ); |
712 | done = true; | 726 | done = true; |
713 | } | 727 | } |
@@ -846,48 +860,56 @@ void SFCave :: keyReleaseEvent( QKeyEvent *e ) | |||
846 | switch( e->key() ) | 860 | switch( e->key() ) |
847 | { | 861 | { |
848 | case Qt::Key_F9: | 862 | case Qt::Key_F9: |
849 | case Qt::Key_Space: | 863 | case Qt::Key_Space: |
850 | case Qt::Key_Up: | 864 | case Qt::Key_Up: |
851 | if ( press ) | 865 | if ( press ) |
852 | { | 866 | { |
853 | press = false; | 867 | press = false; |
854 | 868 | ||
855 | replayList.append( new int( nrFrames ) ); | 869 | replayList.append( new int( nrFrames ) ); |
856 | } | 870 | } |
857 | break; | 871 | break; |
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 | ||
878 | void SFCave :: displayMenu() | 900 | void SFCave :: displayMenu() |
879 | { | 901 | { |
880 | offscreen->fill( Qt::black ); | 902 | offscreen->fill( Qt::black ); |
881 | 903 | ||
882 | QPainter p( offscreen ); | 904 | QPainter p( offscreen ); |
883 | p.setPen( Qt::white ); | 905 | p.setPen( Qt::white ); |
884 | 906 | ||
885 | QFont f( "Helvetica", 16 ); | 907 | QFont f( "Helvetica", 16 ); |
886 | p.setFont( f ); | 908 | p.setFont( f ); |
887 | 909 | ||
888 | QFontMetrics fm = p.fontMetrics(); | 910 | QFontMetrics fm = p.fontMetrics(); |
889 | 911 | ||
890 | QString text = "SFCave"; | 912 | QString text = "SFCave"; |
891 | p.drawText( (sWidth/2) - (fm.width( text )/2), 60, text ); | 913 | p.drawText( (sWidth/2) - (fm.width( text )/2), 60, text ); |
892 | 914 | ||
893 | text = "Written by Andy Qua"; | 915 | text = "Written by Andy Qua"; |
@@ -980,24 +1002,95 @@ void SFCave :: dealWithMenuSelection() | |||
980 | cfg.setGroup( "settings" ); | 1002 | cfg.setGroup( "settings" ); |
981 | cfg.writeEntry( "difficulty", currentGameDifficulty ); | 1003 | cfg.writeEntry( "difficulty", currentGameDifficulty ); |
982 | cfg.writeEntry( "gameType", currentGameType ); | 1004 | cfg.writeEntry( "gameType", currentGameType ); |
983 | #endif | 1005 | #endif |
984 | break; | 1006 | break; |
985 | } | 1007 | } |
986 | 1008 | ||
987 | break; | 1009 | break; |
988 | } | 1010 | } |
989 | } | 1011 | } |
990 | } | 1012 | } |
991 | 1013 | ||
992 | void SFCave :: saveScore() | 1014 | void 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 | |||
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 | ||