From 31a2817afc0daedc7021c0de21f8267b096c8a53 Mon Sep 17 00:00:00 2001 From: Johannes Le Roux Date: Thu, 11 Apr 2024 15:19:04 +0200 Subject: [PATCH] file io --- catskillgame.c | 41 +++++++++++++++++++++++++++-------------- catskillgfx.c | 30 +++++++++++++++++++----------- catskillgfx.h | 3 ++- main.c | 5 ----- saves/slot_1.sav | Bin 0 -> 12 bytes 5 files changed, 48 insertions(+), 31 deletions(-) create mode 100644 saves/slot_1.sav diff --git a/catskillgame.c b/catskillgame.c index d92081c..441950c 100644 --- a/catskillgame.c +++ b/catskillgame.c @@ -4,6 +4,7 @@ #include #include #include +#include // Game object here typedef struct @@ -461,6 +462,18 @@ void loop() gameLoopLogic(); // Check this every loop frame serviceDebounce(); serviceAudio(); + if (displayPauseState == true) + { // If paused for USB xfer, push START to unpause + if (button(start_but)) { //Button pressed? + displayPause(false); + if (gameState == pauseMode) { //USB menu pause? Go back to main menu (else game will resume I think?) + switchGameTo(titleScreen); + } + else { + //music.PauseTrack(currentFloor); //Probably needs to be current MUSIC? + } + } + } } void loop1() @@ -502,7 +515,7 @@ void gameLoopLogic() case 1: // gpio_put(15, 1); - //serviceAudio(); // Service PCM audio file streaming while Core1 draws the screens (gives Core0 something to do while we wait for vid mem access) + // serviceAudio(); // Service PCM audio file streaming while Core1 draws the screens (gives Core0 something to do while we wait for vid mem access) gameLoopState = 2; // gpio_put(15, 0); break; @@ -635,7 +648,6 @@ void gameFrame() currentFloor = 1; } } - tileDirect(12, 9, (((currentFloor - 1) * 16) + 128) + 15); } @@ -685,11 +697,15 @@ void gameFrame() break; case 13: - switchGameTo(pauseMode); + //switchGameTo(pauseMode); + gtk_main_quit(); break; } } - + if (button(start_but)) + { + gtk_main_quit(); + } // if (button(B_but) && cursorY == 11) { //DEV MODE - DISABLE // menuTimer = 0; // editType = false; //Press B to edit hallway @@ -937,7 +953,7 @@ void drawTitleScreen() drawText("start", 10, 10, false); drawText("load", 10, 11, false); drawText("edit", 10, 12, false); - drawText("usb", 10, 13, false); + drawText("exit", 10, 13, false); setWindow(0, 0); @@ -1221,7 +1237,7 @@ void saveLogic() void checkSaveSlots() { - if (loadFile("/saves/slot_1.sav")) + if (checkFile("saves/slot_1.sav")) { saveSlots[0] = 1; } @@ -1229,8 +1245,7 @@ void checkSaveSlots() { saveSlots[0] = 0; } - closeFile(); - if (loadFile("/saves/slot_2.sav")) + if (checkFile("saves/slot_2.sav")) { saveSlots[1] = 1; } @@ -1238,8 +1253,7 @@ void checkSaveSlots() { saveSlots[1] = 0; } - closeFile(); - if (loadFile("/saves/slot_3.sav")) + if (checkFile("saves/slot_3.sav")) { saveSlots[2] = 1; } @@ -1247,7 +1261,6 @@ void checkSaveSlots() { saveSlots[2] = 0; } - closeFile(); } void eraseSaveSlots(int which) @@ -1257,13 +1270,13 @@ void eraseSaveSlots(int which) { case 0: - eraseFile("/saves/slot_1.sav"); + eraseFile("saves/slot_1.sav"); break; case 1: - eraseFile("/saves/slot_2.sav"); + eraseFile("saves/slot_2.sav"); break; case 2: - eraseFile("/saves/slot_3.sav"); + eraseFile("saves/slot_3.sav"); break; } } diff --git a/catskillgfx.c b/catskillgfx.c index bdf6560..1609ff7 100644 --- a/catskillgfx.c +++ b/catskillgfx.c @@ -68,11 +68,11 @@ const int notes[] = { int indexToGPIO[9] = {7, 11, 9, 13, 21, 5, 4, 3, 2}; // Button index is UDLR SEL STR A B C this maps that to the matching GPIO -volatile uint8_t buttonValue = 0; -bool buttonDown[10] = {false}; -bool debounce[10] = {false, false, false, false, true, true, true, true, true, true}; // Index of which buttons have debounce (button must open before it can re-trigger) -uint8_t debounceStart[10] = {0, 0, 0, 0, 5, 5, 1, 1, 1, 0}; // If debounce, how many frames must button be open before it can re-trigger. -uint8_t debounceTimer[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // The debounceStart time is copied here, and debounceTimer is what's decrimented +volatile uint8_t buttonValue = no_but; +bool buttonDown[11] = {false}; +bool debounce[11] = {false, false, false, false, true, true, true, true, true, true, true}; // Index of which buttons have debounce (button must open before it can re-trigger) +uint8_t debounceStart[11] = {0, 0, 0, 0, 5, 5, 1, 1, 1, 1, 1}; // If debounce, how many frames must button be open before it can re-trigger. +uint8_t debounceTimer[11] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // The debounceStart time is copied here, and debounceTimer is what's decrimented // Used to pre-store GPIO-to-channel numbers for PWM (5th channel reserved for PCM audio wave files) uint8_t slice_numbers[4]; @@ -110,6 +110,9 @@ void setButton(uint8_t b, bool down) case 228: buttonValue = B_but; break; + case 27: + buttonValue = start_but; + break; default: buttonValue = no_but; } @@ -180,7 +183,7 @@ void setButtonDebounce(int which, bool useDebounce, uint8_t frames) // Must be called once per frame to service the button debounce void serviceDebounce() { // Must be called every frame, even if paused (because the buttons need to work to unpause!) - for (int x = 0; x < 9; x++) + for (int x = 0; x < 11; x++) { // Scan all buttons if (debounce[x] == 1) { // Button uses debounce? @@ -1285,26 +1288,25 @@ bool checkFile(const char *path) void saveFile(const char *path) { // Opens a file for saving. Deletes the file if it already exists (write-over) - + file = fopen(path, "wb"); fileActive = true; } bool loadFile(const char *path) { // Opens a file for loading - - fileActive = true; file = fopen(path, "rb"); if (!file) { - printf("Unable to open palette file!\n"); + printf("Unable to open %s!\n", path); return false; } - + fileActive = true; return true; } void writeByte(uint8_t theByte) { // Writes a byte to current file + fwrite(&theByte, sizeof(uint8_t), 1, file); } void writeBool(bool state) @@ -1345,6 +1347,12 @@ void closeFile() void eraseFile(const char *path) { + if (fileActive) { + closeFile(); + } + if (remove(path) != 0) { + printf("Unable to delete %s!", path); + } } int rnd(int min, int max) diff --git a/catskillgfx.h b/catskillgfx.h index cbebf13..48836bf 100644 --- a/catskillgfx.h +++ b/catskillgfx.h @@ -106,6 +106,7 @@ int rnd(int min, int max); #define A_but 6 #define B_but 7 #define C_but 8 -#define no_but 9 +#define ESC_but 9 +#define no_but 10 #endif \ No newline at end of file diff --git a/main.c b/main.c index c73c3d3..d9d92ec 100644 --- a/main.c +++ b/main.c @@ -9,11 +9,6 @@ GdkPixbuf *pbs; gboolean keypress_function(GtkWidget *widget, GdkEventKey *event, gpointer data) { uint8_t k = event->keyval; - if (k == 27) - { - gtk_main_quit(); - return TRUE; - } setButton(k, true); return TRUE; } diff --git a/saves/slot_1.sav b/saves/slot_1.sav new file mode 100644 index 0000000000000000000000000000000000000000..ca12c8bd0f0d91d712ec0906fc7f9a12b8f3b790 GIT binary patch literal 12 LcmZQ%Vt@kx04@Lo literal 0 HcmV?d00001