Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
SavedGames/SavedGames.cpp
- Committer:
- evanso
- Date:
- 2020-05-19
- Revision:
- 54:d46459104dea
- Parent:
- 53:01be7898c23f
- Child:
- 55:c04568b25617
File content as of revision 54:d46459104dea:
#include "SavedGames.h"
const int arrow_up[3][5] = {
{ 0, 0, 1, 0, 0 },
{ 0, 1, 1, 1, 0 },
{ 1, 1, 1, 1, 1 },
};
const int arrow_down[3][5] = {
{ 1, 1, 1, 1, 1 },
{ 0, 1, 1, 1, 0 },
{ 0, 0, 1, 0, 0 },
};
SavedGames::SavedGames() {
}
SavedGames::~SavedGames() {
}
void SavedGames::init(SDFileSystem &sd,N5110 &lcd){
error_ = false;
check_sd_present(sd,lcd);
read_saved_data(sd,lcd);
display_data_number_ = 0;
}
void SavedGames::check_sd_present(SDFileSystem &sd,N5110 &lcd){
if(!sd.card_present()) {
error_ = true;
//print error screen
lcd.clear();
lcd.printString("SD card not ",9,3);
lcd.printString(" present ",9,4);
lcd.refresh();
wait(3);
}
}
void SavedGames::error_open_file(N5110 &lcd){
error_ = true;
//print error screen
lcd.clear();
lcd.printString(" Unable to ",9,3);
lcd.printString(" open file ",9,4);
lcd.refresh();
wait(3);
}
void SavedGames::add_saved_data(SDFileSystem &sd, SavedGamesData data,
N5110 &lcd){
//opens file
FILE *fp = fopen("/sd/SavedGames.txt", "a");
if (fp == NULL) {
error_open_file(lcd);
return;
}else{
//Adda data to file if no error
fprintf(fp, "%d\n", data.score);
fprintf(fp, "%d\n", data.lives);
fprintf(fp, "%d\n", data.smart_bombs);
fprintf(fp, "%d\n", data.alien_number);
fclose(fp);
}
}
void SavedGames::read_saved_data(SDFileSystem &sd, N5110 &lcd){
//opens file
FILE *fp = fopen("/sd/SavedGames.txt", "r");
if (fp == NULL) {
error_open_file(lcd);
return;
//Reads saved data into vector
}else{
int line_number = 0;
int *data_array;
//count number of lines in files
while (fscanf(fp, "%*d") != EOF) {
line_number++;
}
//creats data arrays
data_array = (int *)calloc(line_number, sizeof (int));
//Reads into arrays
int i = 0;
rewind(fp);
while (fscanf(fp, "%d",&data_array[i]) != EOF) {
i++;
}
// puts array data into structs and sotres structs in vector
for(int i = 0; i <= line_number; i+=4){
//creats objects
SavedGamesData data;
//sets data
data.score = data_array[i];
data.lives = data_array[i+1];
data.smart_bombs = data_array[i+2];
data.alien_number = data_array[i+3];
//store object in vector
saved_data_vector.push_back(data);
}
fclose(fp);
}
}
void SavedGames::display_saved_games(N5110 &lcd){
lcd.clear();
//prints saved game screen
lcd.printString("Saved Games",9,1);
lcd.drawSprite(39, 16, 3, 5, (int *)arrow_up);
lcd.drawSprite(39, 32, 3, 5, (int *)arrow_down);
//prints saved game that is displayed
char buffer[9];
sprintf(buffer,"Save = %2d",display_data_number_);
lcd.printString(buffer,15,3);
lcd.refresh();
}
bool SavedGames::get_error(){
return error_;
}
void SavedGames::saved_games_scroll(Gamepad &pad, Direction d_) {
// Changes displayed manu part depending on joystick input
if (d_ == N || d_ == NE || d_ == NW ) {
display_data_number_++;
//loops back round when at end of saved game vecotr
if(display_data_number_ == saved_data_vector.size()){
display_data_number_ = 0;
}
} else if (d_ == S || d_ == SW || d_ == SE) {
display_data_number_--;
if(display_data_number_ < 0 ){
display_data_number_ = saved_data_vector.size() - 1;
}
}
wait(0.15);
}