Skip to content

Commit

Permalink
file io
Browse files Browse the repository at this point in the history
  • Loading branch information
dadecoza committed Apr 11, 2024
1 parent 824651f commit 31a2817
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 31 deletions.
41 changes: 27 additions & 14 deletions catskillgame.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
#include <gtk/gtk.h>

// Game object here
typedef struct
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -635,7 +648,6 @@ void gameFrame()
currentFloor = 1;
}
}

tileDirect(12, 9, (((currentFloor - 1) * 16) + 128) + 15);
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -1221,33 +1237,30 @@ void saveLogic()
void checkSaveSlots()
{

if (loadFile("/saves/slot_1.sav"))
if (checkFile("saves/slot_1.sav"))
{
saveSlots[0] = 1;
}
else
{
saveSlots[0] = 0;
}
closeFile();
if (loadFile("/saves/slot_2.sav"))
if (checkFile("saves/slot_2.sav"))
{
saveSlots[1] = 1;
}
else
{
saveSlots[1] = 0;
}
closeFile();
if (loadFile("/saves/slot_3.sav"))
if (checkFile("saves/slot_3.sav"))
{
saveSlots[2] = 1;
}
else
{
saveSlots[2] = 0;
}
closeFile();
}

void eraseSaveSlots(int which)
Expand All @@ -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;
}
}
Expand Down
30 changes: 19 additions & 11 deletions catskillgfx.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion catskillgfx.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 0 additions & 5 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Binary file added saves/slot_1.sav
Binary file not shown.

0 comments on commit 31a2817

Please sign in to comment.