Joystick support.

This commit is contained in:
Scott Duensing 2020-08-07 20:52:29 -05:00
parent 68061ae7ae
commit 47bd7e092e
2 changed files with 176 additions and 21 deletions

View file

@ -76,11 +76,12 @@ static jlImgT *helpI = NULL;
static jint16 puzzleCount; static jint16 puzzleCount;
static jint16 puzzleCurrent; static jint16 puzzleCurrent;
static jint16 puzzleLast; static jint16 puzzleLast;
static jint16 puzzleSolved;
static jint32 *puzzleIndex = NULL; static jint32 *puzzleIndex = NULL;
static PuzzleT puzzle; static PuzzleT puzzle;
static SaveT saveGame; static SaveT saveGameData;
static byte puzzleNow[MAX_WIDTH][MAX_HEIGHT]; static byte puzzleNow[MAX_WIDTH][MAX_HEIGHT];
static byte puzzleBefore[MAX_WIDTH][MAX_HEIGHT]; static byte puzzleBefore[MAX_WIDTH][MAX_HEIGHT];
@ -89,9 +90,13 @@ static byte avatarX;
static byte avatarY; static byte avatarY;
static byte avatarXLast; static byte avatarXLast;
static byte avatarYLast; static byte avatarYLast;
static byte avatarXStart;
static byte avatarYStart;
static byte crateCount; static byte crateCount;
static byte crateInitialCount;
static byte cratesOnTarget; static byte cratesOnTarget;
static byte cratesInitiallyOnTarget;
static char puzzleChars[] = { "_# .$@+*" }; static char puzzleChars[] = { "_# .$@+*" };
@ -107,10 +112,12 @@ static CoordT tileLookup[TILE_COUNT] = {
}; };
void countSolved(void);
void drawAvatar(void); void drawAvatar(void);
byte drawMenu(char *title, char *menuItems[], byte requestedWidth, byte *height, byte *offsetX, byte *offsetY); byte drawMenu(char *title, char *menuItems[], byte requestedWidth, byte *height, byte *offsetX, byte *offsetY);
void drawPuzzle(void); void drawPuzzle(void);
void forceFullRedraw(void); void forceFullRedraw(void);
void levelComplete(void);
void loadPuzzle(jint16 number); void loadPuzzle(jint16 number);
bool mainMenu(void); bool mainMenu(void);
char menu(char *title, char *menuItems[], char selected, byte width, byte offsetX, byte offsetY); 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); bool readInput(byte *key);
void redraw(void); void redraw(void);
void resetPuzzle(void); void resetPuzzle(void);
void saveGame(void);
void selectLevel(void); void selectLevel(void);
void showImage(jlImgT *image); void showImage(jlImgT *image);
void showPalette(void); void showPalette(void);
@ -128,6 +136,29 @@ void ticker(void);
void title(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; p++) {
bit--;
if (bit < 0) {
bit = 7;
index++;
data = saveGameData.solved[index];
}
if (data & (1 << bit)) {
puzzleSolved++;
}
}
}
void drawAvatar(void) { void drawAvatar(void) {
jint16 x = 0; // Screen (tile) coordinates. jint16 x = 0; // Screen (tile) coordinates.
jint16 y = 0; jint16 y = 0;
@ -244,6 +275,38 @@ void forceFullRedraw(void) {
} }
void levelComplete(void) {
char bit;
jint16 index;
// Mark it solved.
index = puzzleCurrent - 1;
bit = 7 - (index % 8);
index /= 8;
saveGameData.solved[index] = saveGameData.solved[index] | (1 << bit);
// Move to next puzzle.
puzzleCurrent++;
if (puzzleCurrent > 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) { void loadPuzzle(jint16 number) {
FILE *in = NULL; FILE *in = NULL;
byte x = 0; byte x = 0;
@ -305,6 +368,11 @@ void loadPuzzle(jint16 number) {
fclose(in); fclose(in);
crateInitialCount = crateCount;
cratesInitiallyOnTarget = cratesOnTarget;
avatarXStart = avatarX;
avatarYStart = avatarY;
// Center puzzle on display. // Center puzzle on display.
puzzle.offsetX = (10 - puzzle.width / 2) * 2; puzzle.offsetX = (10 - puzzle.width / 2) * 2;
puzzle.offsetY = (6 - puzzle.height / 2) * 2; puzzle.offsetY = (6 - puzzle.height / 2) * 2;
@ -321,8 +389,6 @@ bool mainMenu(void) {
bool running = true; bool running = true;
char choice = 0; char choice = 0;
//***TODO*** Reset disk?
while ((choice >= 0) && running && !jlUtilMustExit()) { while ((choice >= 0) && running && !jlUtilMustExit()) {
choice = menu("Main Menu", options, choice, 0, 0, 0); choice = menu("Main Menu", options, choice, 0, 0, 0);
switch (choice) { switch (choice) {
@ -441,6 +507,7 @@ char menu(char *title, char *menuItems[], char selected, byte width, byte offset
} }
jlImgDisplay(screen); jlImgDisplay(screen);
jlDisplayPresent();
jlImgFree(screen); jlImgFree(screen);
return result; return result;
@ -462,6 +529,11 @@ void moveCrate(byte sx, byte sy, byte dx, byte dy) {
puzzleNow[dx][dy] = TILE_CRATE_ON_GOAL; puzzleNow[dx][dy] = TILE_CRATE_ON_GOAL;
cratesOnTarget++; cratesOnTarget++;
} }
// Did they win?
if (cratesOnTarget == crateCount) {
levelComplete();
}
} }
@ -484,7 +556,9 @@ void play(void) {
switch (key) { switch (key) {
case 27: case 27:
playing = mainMenu(); playing = mainMenu();
if (playing) redraw(); if (playing) {
redraw();
}
break; break;
case 'I': case 'I':
@ -616,13 +690,57 @@ void printAt(jlImgT *font, jint16 cx, jint16 cy, const char *what, ...) {
bool readInput(byte *key) { bool readInput(byte *key) {
static bool debounceController = false;
// Keyboard
if (jlKeyPressed()) { if (jlKeyPressed()) {
while (jlKeyPressed()) { while (jlKeyPressed()) {
*key = jlKeyRead(); *key = jlKeyRead();
} }
return true; 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; return false;
} }
@ -637,8 +755,33 @@ void redraw(void) {
void resetPuzzle(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); 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)); jint16 dangling = puzzleCount - (cols * (cols - 1));
char bit = 0; char bit = 0;
byte data; byte data;
byte oldColor = jlDrawColorGet();
bool inMenu = true; bool inMenu = true;
jlImgT *screen = NULL; jlImgT *screen = NULL;
@ -685,7 +829,7 @@ void selectLevel(void) {
if (bit < 0) { if (bit < 0) {
bit = 7; bit = 7;
index++; index++;
data = saveGame.solved[index]; data = saveGameData.solved[index];
} }
if (data & (1 << bit)) { if (data & (1 << bit)) {
jlDrawColorSet(1); jlDrawColorSet(1);
@ -794,7 +938,9 @@ void selectLevel(void) {
} }
} }
jlDrawColorSet(oldColor);
jlImgDisplay(screen); jlImgDisplay(screen);
jlDisplayPresent();
jlImgFree(screen); jlImgFree(screen);
} }
@ -872,7 +1018,7 @@ void title(void) {
int main(void) { int main(void) {
FILE *in = NULL; FILE *in = NULL;
jlUtilStartup("Warehouse"); jlUtilStartup("Warehouse");
@ -895,29 +1041,33 @@ int main(void) {
fclose(in); fclose(in);
// Create bit array of possible games for save file. // Create bit array of possible games for save file.
saveGame.solvedSize = sizeof(byte) * (size_t)((float)(puzzleCount + 0.5f) / 8.0f); saveGameData.solvedSize = sizeof(byte) * (size_t)((float)(puzzleCount + 0.5f) / 8.0f);
saveGame.solved = (byte *)jlMalloc(saveGame.solvedSize); saveGameData.solved = (byte *)jlMalloc(saveGameData.solvedSize);
// Does this user have a save file? // Does this user have a save file?
in = fopen("data/save.dat", "rb"); in = fopen("data/save.dat", "rb");
if (in) { if (in) {
// Load save file. // Load save file.
fread(&puzzleCount, sizeof(jint16), 1, in); fread(&puzzleCount, sizeof(jint16), 1, in);
fread(&saveGame.lastPuzzle, sizeof(jint16), 1, in); fread(&saveGameData.lastPuzzle, sizeof(jint16), 1, in);
fread(&saveGame.solvedSize, sizeof(jint16), 1, in); fread(&saveGameData.solvedSize, sizeof(jint16), 1, in);
fread(saveGame.solved, saveGame.solvedSize, 1, in); fread(saveGameData.solved, saveGameData.solvedSize, 1, in);
fclose(in); fclose(in);
countSolved();
} else { } else {
// No save. Prime the pump. // No save. Prime the pump.
saveGame.lastPuzzle = 1; saveGameData.lastPuzzle = 1;
memset(saveGame.solved, 0, saveGame.solvedSize); memset(saveGameData.solved, 0, saveGameData.solvedSize);
puzzleSolved = 0;
} }
loadPuzzle(saveGame.lastPuzzle); // Do the fun stuff.
loadPuzzle(saveGameData.lastPuzzle);
play(); play();
saveGame();
jlFree(saveGame.solved); // Clean up.
jlFree(saveGameData.solved);
jlFree(puzzleIndex); jlFree(puzzleIndex);
jlImgFree(helpI); jlImgFree(helpI);
jlImgFree(aboutI); jlImgFree(aboutI);

View file

@ -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}... echo Converting ${IMAGE}...
$JOEY/joeylib/scripts/xcf2img.sh ${IMAGE} "${JOEY}/utils/imgconvert" ${IMAGE} NO_STENCIL $(basename ${IMAGE} .png) $1
done done