From 47bd7e092e8f1542bfc639907548cc7ba1890cc4 Mon Sep 17 00:00:00 2001 From: Scott Duensing Date: Fri, 7 Aug 2020 20:52:29 -0500 Subject: [PATCH] Joystick support. --- warehouse/main.c | 186 ++++++++++++++++++++++++++++++++++---- warehouse/updateImages.sh | 11 ++- 2 files changed, 176 insertions(+), 21 deletions(-) diff --git a/warehouse/main.c b/warehouse/main.c index 3b7c850..2d7db3c 100644 --- a/warehouse/main.c +++ b/warehouse/main.c @@ -76,11 +76,12 @@ static jlImgT *helpI = NULL; static jint16 puzzleCount; static jint16 puzzleCurrent; static jint16 puzzleLast; +static jint16 puzzleSolved; static jint32 *puzzleIndex = NULL; static PuzzleT puzzle; -static SaveT saveGame; +static SaveT saveGameData; static byte puzzleNow[MAX_WIDTH][MAX_HEIGHT]; static byte puzzleBefore[MAX_WIDTH][MAX_HEIGHT]; @@ -89,9 +90,13 @@ static byte avatarX; static byte avatarY; static byte avatarXLast; static byte avatarYLast; +static byte avatarXStart; +static byte avatarYStart; static byte crateCount; +static byte crateInitialCount; static byte cratesOnTarget; +static byte cratesInitiallyOnTarget; static char puzzleChars[] = { "_# .$@+*" }; @@ -107,10 +112,12 @@ static CoordT tileLookup[TILE_COUNT] = { }; +void countSolved(void); void drawAvatar(void); byte drawMenu(char *title, char *menuItems[], byte requestedWidth, byte *height, byte *offsetX, byte *offsetY); void drawPuzzle(void); void forceFullRedraw(void); +void levelComplete(void); void loadPuzzle(jint16 number); bool mainMenu(void); char menu(char *title, char *menuItems[], char selected, byte width, byte offsetX, byte offsetY); @@ -121,6 +128,7 @@ void printAt(jlImgT *font, jint16 cx, jint16 cy, const char *what, ...); bool readInput(byte *key); void redraw(void); void resetPuzzle(void); +void saveGame(void); void selectLevel(void); void showImage(jlImgT *image); void showPalette(void); @@ -128,6 +136,29 @@ void ticker(void); void title(void); +void countSolved(void) { + byte data; + char bit = 0; + jint16 index = -1; + jint16 p; + + puzzleSolved = 0; + + // Count number of solved puzzles. + for (p=0; p puzzleCount) { + puzzleCurrent = 1; + } + + saveGame(); + + // Recount solves. + countSolved(); + + // Is the game complete? + if (puzzleSolved == puzzleCount) { + // They did them all! + //***TODO*** + return; + } + + //***TODO*** +} + + void loadPuzzle(jint16 number) { FILE *in = NULL; byte x = 0; @@ -305,6 +368,11 @@ void loadPuzzle(jint16 number) { fclose(in); + crateInitialCount = crateCount; + cratesInitiallyOnTarget = cratesOnTarget; + avatarXStart = avatarX; + avatarYStart = avatarY; + // Center puzzle on display. puzzle.offsetX = (10 - puzzle.width / 2) * 2; puzzle.offsetY = (6 - puzzle.height / 2) * 2; @@ -321,8 +389,6 @@ bool mainMenu(void) { bool running = true; char choice = 0; - //***TODO*** Reset disk? - while ((choice >= 0) && running && !jlUtilMustExit()) { choice = menu("Main Menu", options, choice, 0, 0, 0); switch (choice) { @@ -441,6 +507,7 @@ char menu(char *title, char *menuItems[], char selected, byte width, byte offset } jlImgDisplay(screen); + jlDisplayPresent(); jlImgFree(screen); return result; @@ -462,6 +529,11 @@ void moveCrate(byte sx, byte sy, byte dx, byte dy) { puzzleNow[dx][dy] = TILE_CRATE_ON_GOAL; cratesOnTarget++; } + + // Did they win? + if (cratesOnTarget == crateCount) { + levelComplete(); + } } @@ -484,7 +556,9 @@ void play(void) { switch (key) { case 27: playing = mainMenu(); - if (playing) redraw(); + if (playing) { + redraw(); + } break; case 'I': @@ -616,13 +690,57 @@ void printAt(jlImgT *font, jint16 cx, jint16 cy, const char *what, ...) { bool readInput(byte *key) { + + static bool debounceController = false; + + // Keyboard if (jlKeyPressed()) { while (jlKeyPressed()) { *key = jlKeyRead(); } return true; } - //***TODO*** Add joystick here. + + // Joystick + if (jlGameGetAxis(0) < -50) { + if (debounceController) return false; + *key = 'J'; + debounceController = true; + return true; + } + if (jlGameGetAxis(0) > 50) { + if (debounceController) return false; + *key = 'K'; + debounceController = true; + return true; + } + if (jlGameGetAxis(1) < -50) { + if (debounceController) return false; + *key = 'I'; + debounceController = true; + return true; + } + if (jlGameGetAxis(1) > 50) { + if (debounceController) return false; + *key = 'M'; + debounceController = true; + return true; + } + if (jlGameGetButton(0)) { + if (debounceController) return false; + *key = 13; + debounceController = true; + return true; + } + if (jlGameGetButton(1)) { + if (debounceController) return false; + *key = 27; + debounceController = true; + return true; + } + + debounceController = false; + return false; } @@ -637,8 +755,33 @@ void redraw(void) { void resetPuzzle(void) { - // Make copy for our rendering array. + // Copy loaded puzzle data into playable data. memcpy(&puzzleNow, puzzle.puzzle, sizeof(byte) * MAX_WIDTH * MAX_HEIGHT); + // Reset crate count. + crateCount = crateInitialCount; + cratesOnTarget = cratesInitiallyOnTarget; + // Put avatar back. + avatarX = avatarXStart; + avatarY = avatarYStart; + avatarXLast = -1; + avatarYLast = -1; +} + + +void saveGame(void) { + FILE *out = NULL; + + saveGameData.lastPuzzle = puzzleCurrent; + + // Save game. + out = fopen("data/save.dat", "wb"); + if (out) { + fwrite(&puzzleCount, sizeof(jint16), 1, out); + fwrite(&saveGameData.lastPuzzle, sizeof(jint16), 1, out); + fwrite(&saveGameData.solvedSize, sizeof(jint16), 1, out); + fwrite(saveGameData.solved, saveGameData.solvedSize, 1, out); + fclose(out); + } } @@ -658,6 +801,7 @@ void selectLevel(void) { jint16 dangling = puzzleCount - (cols * (cols - 1)); char bit = 0; byte data; + byte oldColor = jlDrawColorGet(); bool inMenu = true; jlImgT *screen = NULL; @@ -685,7 +829,7 @@ void selectLevel(void) { if (bit < 0) { bit = 7; index++; - data = saveGame.solved[index]; + data = saveGameData.solved[index]; } if (data & (1 << bit)) { jlDrawColorSet(1); @@ -794,7 +938,9 @@ void selectLevel(void) { } } + jlDrawColorSet(oldColor); jlImgDisplay(screen); + jlDisplayPresent(); jlImgFree(screen); } @@ -872,7 +1018,7 @@ void title(void) { int main(void) { - FILE *in = NULL; + FILE *in = NULL; jlUtilStartup("Warehouse"); @@ -895,29 +1041,33 @@ int main(void) { fclose(in); // Create bit array of possible games for save file. - saveGame.solvedSize = sizeof(byte) * (size_t)((float)(puzzleCount + 0.5f) / 8.0f); - saveGame.solved = (byte *)jlMalloc(saveGame.solvedSize); + saveGameData.solvedSize = sizeof(byte) * (size_t)((float)(puzzleCount + 0.5f) / 8.0f); + saveGameData.solved = (byte *)jlMalloc(saveGameData.solvedSize); // Does this user have a save file? in = fopen("data/save.dat", "rb"); if (in) { // Load save file. fread(&puzzleCount, sizeof(jint16), 1, in); - fread(&saveGame.lastPuzzle, sizeof(jint16), 1, in); - fread(&saveGame.solvedSize, sizeof(jint16), 1, in); - fread(saveGame.solved, saveGame.solvedSize, 1, in); + fread(&saveGameData.lastPuzzle, sizeof(jint16), 1, in); + fread(&saveGameData.solvedSize, sizeof(jint16), 1, in); + fread(saveGameData.solved, saveGameData.solvedSize, 1, in); fclose(in); + countSolved(); } else { // No save. Prime the pump. - saveGame.lastPuzzle = 1; - memset(saveGame.solved, 0, saveGame.solvedSize); + saveGameData.lastPuzzle = 1; + memset(saveGameData.solved, 0, saveGameData.solvedSize); + puzzleSolved = 0; } - loadPuzzle(saveGame.lastPuzzle); - + // Do the fun stuff. + loadPuzzle(saveGameData.lastPuzzle); play(); + saveGame(); - jlFree(saveGame.solved); + // Clean up. + jlFree(saveGameData.solved); jlFree(puzzleIndex); jlImgFree(helpI); jlImgFree(aboutI); diff --git a/warehouse/updateImages.sh b/warehouse/updateImages.sh index 2105ac0..64a4f85 100755 --- a/warehouse/updateImages.sh +++ b/warehouse/updateImages.sh @@ -1,6 +1,11 @@ -#!/bin/bash +#!/bin/bash -x -for IMAGE in *.xcf; do +#for IMAGE in *.xcf; do +# echo Converting ${IMAGE}... +# $JOEY/joeylib/scripts/xcf2img.sh ${IMAGE} +#done + +for IMAGE in *.png; do echo Converting ${IMAGE}... - $JOEY/joeylib/scripts/xcf2img.sh ${IMAGE} + "${JOEY}/utils/imgconvert" ${IMAGE} NO_STENCIL $(basename ${IMAGE} .png) $1 done