Ben Evans University Second Year Project. Game Called Defender.
Embed:
(wiki syntax)
Show/hide line numbers
SavedGames.cpp
00001 #include "SavedGames.h" 00002 00003 SavedGames::SavedGames() { 00004 00005 } 00006 00007 SavedGames::~SavedGames() { 00008 00009 } 00010 00011 bool SavedGames::save_test(Direction d_,SDFileSystem &sd, N5110 &lcd) { 00012 00013 // Objects required for test 00014 00015 printf("Data save = %d : ",d_); 00016 00017 // Define input data object and set values 00018 SavedGamesData input_data; 00019 input_data.score = 0; 00020 input_data.lives = 3; 00021 input_data.smart_bombs = 4; 00022 input_data.alien_number = 5; 00023 00024 // Change the save file 00025 saved_games_scroll(d_); 00026 00027 add_saved_data(sd, input_data, lcd); 00028 00029 // Define output data object 00030 SavedGamesData output_data; 00031 00032 output_data = read_saved_data(sd, lcd); 00033 00034 // Checks if the input and output are equal 00035 if (output_data.score == input_data.score && 00036 output_data.lives == input_data.lives && 00037 output_data.smart_bombs == input_data.smart_bombs && 00038 output_data.alien_number == input_data.alien_number ) { 00039 printf ( "Passed!\n"); 00040 return true; 00041 } else { 00042 printf ( "Failed!\n"); 00043 return false; 00044 } 00045 } 00046 00047 void SavedGames::run_save_test(SDFileSystem &sd, N5110 &lcd) { 00048 printf ("\nTesting save_test() \n\n"); 00049 int passed_counter = 0; 00050 00051 // Runs test with all different possible inputs 00052 if (save_test(CENTRE, sd, lcd)) passed_counter++; 00053 if (save_test(N, sd, lcd)) passed_counter++; 00054 if (save_test(NE, sd, lcd)) passed_counter++; 00055 if (save_test(E, sd, lcd)) passed_counter++; 00056 if (save_test(SE, sd, lcd)) passed_counter++; 00057 if (save_test(S, sd, lcd)) passed_counter++; 00058 if (save_test(SW, sd, lcd)) passed_counter++; 00059 if (save_test(W, sd, lcd)) passed_counter++; 00060 if (save_test(NW, sd, lcd)) passed_counter++; 00061 00062 // Prints results 00063 printf ("\n save_test passed %d tests out of 9\n\n\n", 00064 passed_counter); 00065 } 00066 00067 void SavedGames::init() { 00068 error_ = false; 00069 display_data_number_ = 1; 00070 } 00071 00072 void SavedGames::check_sd_present(SDFileSystem &sd, N5110 &lcd) { 00073 if(!sd.card_present()) { 00074 // Print error screen 00075 lcd.clear(); 00076 lcd.printString("SD card not ",9,3); 00077 lcd.printString(" present ",9,4); 00078 lcd.refresh(); 00079 wait(3); 00080 } 00081 } 00082 00083 void SavedGames::error_open_file(N5110 &lcd) { 00084 error_ = true; 00085 00086 // Print error screen 00087 lcd.clear(); 00088 lcd.printString(" Unable to ",9,3); 00089 lcd.printString(" open file ",9,4); 00090 lcd.refresh(); 00091 wait(3); 00092 } 00093 00094 void SavedGames::no_saved_files(N5110 &lcd) { 00095 error_ = true; 00096 00097 // Print error screen 00098 lcd.clear(); 00099 lcd.printString("No saved Files",0,3); 00100 lcd.refresh(); 00101 wait(3); 00102 } 00103 00104 void SavedGames::add_saved_data(SDFileSystem &sd,SavedGamesData data, 00105 N5110 &lcd) { 00106 check_sd_present(sd,lcd); 00107 FILE *fp; 00108 00109 // Overwrites selected save file 00110 if (display_data_number_ == 1) { 00111 fp = fopen("/sd/SavedGamesOne.csv", "w"); 00112 // printf ("save1 \n"); 00113 }else if (display_data_number_ == 2) { 00114 fp = fopen("/sd/SavedGamesTwo.csv", "w"); 00115 // printf ("save2\n" ); 00116 }else{ 00117 fp = fopen("/sd/SavedGamesThree.csv", "w"); 00118 // printf ("save2 \n"); 00119 } 00120 00121 if (fp == NULL) { 00122 error_open_file(lcd); 00123 00124 }else{ 00125 // printf ("add data.score = %d \n", data.score); 00126 // printf ("add data.lives = %d \n", data.lives); 00127 // printf ("adddata.smart_bombs = %d \n", data.smart_bombs); 00128 // printf ("add data.alien_number = %d \n", data.lives); 00129 00130 // Adds data to file if no error 00131 fprintf(fp, "%d,%d,%d,%d\n", data.score, data.lives, data.smart_bombs, 00132 data.alien_number); 00133 fclose(fp); 00134 } 00135 } 00136 00137 SavedGamesData SavedGames::read_saved_data(SDFileSystem &sd, N5110 &lcd) { 00138 check_sd_present(sd,lcd); 00139 00140 FILE *fp; 00141 SavedGamesData data; 00142 00143 // Opens displayed file 00144 if (display_data_number_ == 1) { 00145 fp = fopen("/sd/SavedGamesOne.csv", "r"); 00146 //printf ("open save1\n"); 00147 }else if (display_data_number_ == 2) { 00148 fp = fopen("/sd/SavedGamesTwo.csv", "r"); 00149 //printf ("open save2\n"); 00150 }else{ 00151 fp = fopen("/sd/SavedGamesThree.csv", "r"); 00152 // printf ("open save2\n"); 00153 } 00154 00155 // Check if any saved game files 00156 if (fp == NULL) { 00157 no_saved_files(lcd); 00158 00159 // Reads saved data into object 00160 }else{ 00161 00162 // Read save file and add values to data object 00163 fscanf(fp, "%d,%d,%d,%d",&data.score, &data.lives,&data.smart_bombs, 00164 &data.alien_number); 00165 00166 // printf ("saved_data_vector_ 1%d \n", saved_data_vector.size()); 00167 fclose(fp); 00168 } 00169 00170 return data; 00171 } 00172 00173 void SavedGames::display_saved_games(N5110 &lcd) { 00174 lcd.clear(); 00175 00176 // Prints saved game screen 00177 lcd.printString("Saved Games",9,0); 00178 lcd.drawSprite(39, 18, 3, 5, (int *)arrow_up); 00179 lcd.drawSprite(39, 34, 3, 5, (int *)arrow_down); 00180 00181 // Prints saved game that is displayed 00182 char buffer[9]; 00183 sprintf(buffer,"Save %2d",display_data_number_); 00184 lcd.printString(buffer,21,3); 00185 lcd.printString("Back(B)Play(A)",0,5); 00186 00187 lcd.refresh(); 00188 } 00189 00190 void SavedGames::save_game_screen(N5110 &lcd) { 00191 lcd.clear(); 00192 00193 // Prints saved game screen 00194 lcd.printString("Select a save",3,0); 00195 lcd.drawSprite(39, 18, 3, 5, (int *)arrow_up); 00196 lcd.drawSprite(39, 34, 3, 5, (int *)arrow_down); 00197 00198 // Prints saved game that is displayed 00199 char buffer[9]; 00200 sprintf(buffer,"Save %2d",display_data_number_); 00201 lcd.printString(buffer,21,3); 00202 lcd.printString("Save(A)",21,5); 00203 00204 lcd.refresh(); 00205 } 00206 00207 void SavedGames::saved_games_scroll(Direction d_) { 00208 00209 // Changes displayed menu part depending on joystick input 00210 if (d_ == N || d_ == NE || d_ == NW ) { 00211 display_data_number_++; 00212 00213 // Loops back round when at end of saved game vector 00214 if(display_data_number_ > 3) { 00215 display_data_number_ = 1; 00216 } 00217 } else if (d_ == S || d_ == SW || d_ == SE) { 00218 display_data_number_--; 00219 if(display_data_number_ < 1 ) { 00220 display_data_number_ = 3; 00221 } 00222 } 00223 wait(0.15); 00224 } 00225 00226 00227 int SavedGames::get_display_data_number() { 00228 return display_data_number_; 00229 }
Generated on Fri Aug 5 2022 06:55:07 by
1.7.2