#include #include #include #include "HighScoresList.h" #include "HighScoreRecord.h" #include "InputText.h" #include "csv.h" #include "constant.h" using namespace std; extern PALETTE currentColorPalette; extern RGB white, yellow; extern FONT *joystix; extern FONT *pacmania; extern int layerCount; extern BITMAP *layer[maxLayers]; extern bool forgotScreen; extern int score; extern void unload(); extern void createGraphicsBackup(int layersWanted); extern void restoreGraphicsBackup(int layersWanted); extern const char *csvProblem; const char* HighScoresList::getProblem() { return problem; } bool HighScoresList::load() { vector > raw; if (!loadCsv("Hiscores.csv", &raw)) { problem = csvProblem; return false; } if (raw.size() != 10) { problem = "Expected ten rows of three."; destroyCsv(&raw); return false; } for (int i = 0; i < 10; i++) if (raw[i].size() != 3) { problem = "Expected ten rows of three."; destroyCsv(&raw); return false; } int goodScores[10]; goodScores[0] = atoi(raw[0][1]); for (int i = 0 + 1; i < 10; i++) { goodScores[i] = atoi(raw[i][1]); if (goodScores[i] > goodScores[i - 1]) { problem = "Not sorted by score!"; destroyCsv(&raw); return false; } } for (int i = 0; i < 10; i++) { content[i] = HighScoreRecord(raw[i][0], goodScores[i], raw[i][2], true); free(raw[i][1]); } problem = NULL; return true; } bool HighScoresList::save() { vector > raw(10, vector(3)); for (int i = 0; i < 10; i++) { char *buf = (char*)malloc(bufSizeForItoa * sizeof(char)); if (!buf) { problem = "Not enough memory."; for (int j = 0; j < i; j++) free(raw[j][1]); return false; } itoa(content[i].getRecordScore(), buf, 10); raw[i][0] = content[i].getRecordName(); raw[i][1] = buf; raw[i][2] = content[i].getRecordDate(); } if (!saveCsv("Hiscores.csv", &raw)) { problem = csvProblem; for (int i = 0; i < 10; i++) free(raw[i][1]); return false; } problem = NULL; for (int i = 0; i < 10; i++) free(raw[i][1]); return true; } void HighScoresList::drawHighScores(BITMAP *canvas) { clear_bitmap(canvas); currentColorPalette[14] = yellow; currentColorPalette[15] = white; textout_centre_ex(canvas, pacmania, "HI SCORES", SCREEN_W / 2, 0, 14, -1); for (int i = 0; i < 10; i++) { char buf[bufSizeForItoa]; itoa(content[i].getRecordScore(), buf, 10); textout_ex(canvas, joystix, content[i].getRecordName(), highScoresContentX, highScoresContentY + i * 2 * charHeight, 15, -1); textout_ex(canvas, joystix, buf, highScoresContentX, highScoresContentY + (i * 2 + 1) * charHeight, 15, -1); textout_right_ex(canvas, joystix, content[i].getRecordDate(), highScoresContentRight, highScoresContentY + (i * 2 + 1) * charHeight, 15, -1); } } void HighScoresList::judge(BITMAP *canvas, int layerDonation, bool blinking) { createGraphicsBackup(layerCount); int rank = 10; for (int i = 0; i < 10; i++) if (score > content[i].getRecordScore()) { rank = i; break; } if (rank < 10) { time_t rawNow; time(&rawNow); struct tm *localNow = localtime(&rawNow); char *recordDate = (char*)malloc(11 * sizeof(char)); if (!recordDate) { unload(); allegro_message("Out of memory."); exit(0); } strftime(recordDate, 11, "%m/%d/%Y", localNow); for (int i = 9; i > rank; i--) content[i] = content[i - 1]; content[rank] = HighScoreRecord("", score, recordDate, false); drawHighScores(canvas); drawFrame(); showFrame(); fade_in(currentColorPalette, fadeSpeed); if (forgotScreen) { drawSpritesForBlitting(); restoreGraphicsBackup(layerCount); drawHighScores(canvas); drawFrame(); showFrame(); forgotScreen = false; } char *recordName = InputText(canvas, layerDonation, highScoresContentX, highScoresContentY + rank * 2 * charHeight, SCREEN_W / charWidth, 4, 10, true, "Please enter your name.", 0, -23).block(blinking); if (!recordName) { recordName = (char*)malloc(10 * sizeof(char)); if (!recordName) { unload(); allegro_message("Out of memory."); exit(0); } strcpy(recordName, "Anonymous"); } content[rank] = HighScoreRecord(recordName, score, recordDate, true); drawHighScores(canvas); drawFrame(); showFrame(); } else { drawHighScores(canvas); drawFrame(); showFrame(); fade_in(currentColorPalette, fadeSpeed); if (forgotScreen) { drawSpritesForBlitting(); restoreGraphicsBackup(layerCount); drawHighScores(canvas); drawFrame(); showFrame(); forgotScreen = false; } } createGraphicsBackup(layerCount); clear_keybuf(); while (!keypressed()) { if (forgotScreen) { drawSpritesForBlitting(); restoreGraphicsBackup(layerCount); drawFrame(); showFrame(); forgotScreen = false; } rest(0); } fade_out(fadeSpeed); }