From bee024c3e10609bfcef670e1a7a267938be8a0f9 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Sat, 30 Apr 2022 09:48:00 -0700 Subject: [PATCH] Finished basic question box --- src/conversation/questionbox.c | 57 ++++++++++++++++++++++++++++++++-- src/conversation/questionbox.h | 2 ++ src/conversation/textbox.c | 6 ++-- src/conversation/textbox.h | 1 + src/input.c | 2 ++ src/input.h | 4 ++- src/main.c | 26 ++++++---------- 7 files changed, 74 insertions(+), 24 deletions(-) diff --git a/src/conversation/questionbox.c b/src/conversation/questionbox.c index ef22df8..34e802b 100644 --- a/src/conversation/questionbox.c +++ b/src/conversation/questionbox.c @@ -11,26 +11,77 @@ uint8_t QUESTION_BOX_OPTION_COUNT; uint8_t QUESTION_BOX_OPTION_CURRENT; void questionBoxSetOptions(char *title, char **options, uint8_t count) { - uint8_t i; + uint8_t i, j; char buffer[TEXTBOX_CHARS_MAX + 1]; + char spaces[QUESTION_BOX_QUESTION_SPACES]; sprintf(buffer, title); i = 0; QUESTION_BOX_OPTION_CURRENT = 0; QUESTION_BOX_OPTION_COUNT = count; + for(i = 0; i < QUESTION_BOX_QUESTION_SPACES; i++) { + spaces[i] = ' '; + } + // For each row... for(i = 0; i < count; i++) { if((i & 1) == 0) { + j = strlen(options[i]); sprintf(buffer, "%s\n %s", buffer, options[i]); } else { - sprintf(buffer, "%s %s", buffer, options[i]); + j = QUESTION_BOX_QUESTION_SPACES - j; + spaces[j + 1] = '\0'; + sprintf(buffer, "%s%s%s", buffer, spaces, options[i]); + spaces[j + 1] = ' '; } } conversationTextboxSetText(buffer); + + // Modify the textbox state + TEXTBOX_STATE |= TEXTBOX_STATE_IS_QUESTION; + + // Repurpose old array and draw arrow + spaces[0] = QUESTION_BOX_CURSOR - TEXTBOX_FONT_START + FONT_DATA_POSITION; + set_bkg_tiles(0x01, TEXTBOX_WIN_Y + 0x02, 1, 1, spaces); } inline void questionBoxUpdate() { - + uint8_t tiles[1]; + uint8_t i; + + if((TEXTBOX_STATE & (TEXTBOX_STATE_VISIBLE|TEXTBOX_STATE_IS_QUESTION)) == 0) return; + + // Detect input + if(INPUT_PRESSED & J_RIGHT) { + QUESTION_BOX_OPTION_CURRENT++; + } else if(INPUT_PRESSED & J_LEFT) { + QUESTION_BOX_OPTION_CURRENT--; + } else if(INPUT_PRESSED & J_UP) { + QUESTION_BOX_OPTION_CURRENT -= 2; + } else if(INPUT_PRESSED & J_DOWN) { + QUESTION_BOX_OPTION_CURRENT += 2; + } else { + return; + } + + // Bound. + QUESTION_BOX_OPTION_CURRENT = QUESTION_BOX_OPTION_CURRENT % QUESTION_BOX_OPTION_COUNT; + + // Decide where to render arrow + for(i = 0; i < QUESTION_BOX_OPTION_COUNT; i++) { + if(i == QUESTION_BOX_OPTION_CURRENT) { + tiles[0] = QUESTION_BOX_CURSOR - TEXTBOX_FONT_START + FONT_DATA_POSITION; + } else { + tiles[0] = ' ' - TEXTBOX_FONT_START + FONT_DATA_POSITION; + } + set_bkg_tiles( + 0x01 + ((i % 0x02) * QUESTION_BOX_QUESTION_SPACES), + TEXTBOX_WIN_Y + 0x02 + (i / 0x02), + 1, 1, + tiles + ); + } + } \ No newline at end of file diff --git a/src/conversation/questionbox.h b/src/conversation/questionbox.h index e0894ef..1293da6 100644 --- a/src/conversation/questionbox.h +++ b/src/conversation/questionbox.h @@ -15,6 +15,8 @@ #include "textbox.h" #define QUESTION_BOX_OPTIONS_MAX ((TEXTBOX_CHAR_ROWS-1)*2) +#define QUESTION_BOX_CURSOR '>' +#define QUESTION_BOX_QUESTION_SPACES 9 extern uint8_t QUESTION_BOX_OPTION_COUNT; extern uint8_t QUESTION_BOX_OPTION_CURRENT; diff --git a/src/conversation/textbox.c b/src/conversation/textbox.c index 2c38e38..e18f378 100644 --- a/src/conversation/textbox.c +++ b/src/conversation/textbox.c @@ -57,7 +57,7 @@ void conversationTextboxSetText(char *text) { // IF that number is less than the remaining chars on the current row, // then treat this space like a newline. - if(k >= (rowStart + TEXTBOX_CHARS_PER_ROW - 1)) { + if(k >= (rowStart + TEXTBOX_CHARS_PER_ROW + 1)) { stateFlags |= 1 << 0; } } else if(c == '\n') { @@ -70,7 +70,7 @@ void conversationTextboxSetText(char *text) { i++; rowStart = i;// Update the row start (Should this be i+1?) //TODO: can I optimize the next line by using rowStart somehow? - j = ((j / TEXTBOX_CHARS_PER_ROW) + 1) * TEXTBOX_CHARS_PER_ROW;// Update destination character position. + j = ((j / TEXTBOX_CHARS_PER_ROW)+1) * TEXTBOX_CHARS_PER_ROW;// Update destination character position. TEXTBOX_ROW_COUNT++; continue; } @@ -109,7 +109,7 @@ inline void conversationTextboxUpdate() { // Have we finished scrolling? if(TEXTBOX_STATE & TEXTBOX_STATE_SCROLLED) { // Is the user trying to go to the next line? - if(INPUT_STATE & J_A) { + if(INPUT_PRESSED & J_A) { // First, lets figure out if there's any more text to reveal or not. if((TEXTBOX_ROW_COUNT - TEXTBOX_ROW_CURRENT) < TEXTBOX_TILES_ROWS) { TEXTBOX_STATE &= ~TEXTBOX_STATE_VISIBLE; diff --git a/src/conversation/textbox.h b/src/conversation/textbox.h index d5cd949..f660f18 100644 --- a/src/conversation/textbox.h +++ b/src/conversation/textbox.h @@ -16,6 +16,7 @@ #define TEXTBOX_STATE_VISIBLE (1 << 0) #define TEXTBOX_STATE_SCROLLED (1 << 1) +#define TEXTBOX_STATE_IS_QUESTION (1 << 2) #define TEXTBOX_WIDTH_IN_TILES 20 #define TEXTBOX_HEIGHT_IN_TILES 5 diff --git a/src/input.c b/src/input.c index 6e2f10f..3f0274b 100644 --- a/src/input.c +++ b/src/input.c @@ -8,3 +8,5 @@ #include "input.h" uint8_t INPUT_STATE = 0x00; +uint8_t INPUT_PRESSED = 0x00; +uint8_t INPUT_LAST = 0x00; \ No newline at end of file diff --git a/src/input.h b/src/input.h index c0d87ff..7dd9e30 100644 --- a/src/input.h +++ b/src/input.h @@ -8,4 +8,6 @@ #pragma once #include "libs.h" -extern uint8_t INPUT_STATE; \ No newline at end of file +extern uint8_t INPUT_STATE; +extern uint8_t INPUT_PRESSED; +extern uint8_t INPUT_LAST; \ No newline at end of file diff --git a/src/main.c b/src/main.c index 043390c..c583511 100644 --- a/src/main.c +++ b/src/main.c @@ -159,7 +159,6 @@ inline void mainDebugDraw() { void main() { int16_t j; - uint8_t filled[GB_BACKGROUND_COLUMNS*GB_BACKGROUND_ROWS]; // Set up the GAMEBOY's registers. @@ -171,15 +170,12 @@ void main() { // Init the random seed initarand(DIV_REG); - - // Prepare time and input - INPUT_STATE = joypad(); // Init things commonTilesInit(); conversationTextboxInit(); - // conversationQueueInit(); - // pokerInit(); + conversationQueueInit(); + pokerInit(); // Fill screen white for(j = 0; j < GB_BACKGROUND_COLUMNS * GB_BACKGROUND_ROWS; j++) filled[j] = COMMON_TILE_3; @@ -187,24 +183,23 @@ void main() { SCX_REG = 0x00; SCY_REG = 0x00; - // Alright begin the game logic here. - char *options[2] = { - "Yes", "No" - }; - questionBoxSetOptions("Title", options, 0x02); - // Now turn the screen on DISPLAY_ON; enable_interrupts(); wait_vbl_done(); + // Begin game + conversationQueueNext(); + // Begin the loop while(1) { // Perform non-graphical code updates wait_vbl_done(); // Update the input state + INPUT_LAST = INPUT_STATE; INPUT_STATE = joypad(); + INPUT_PRESSED = (~INPUT_LAST) & INPUT_STATE; // Tick time. timeUpdate(); @@ -212,15 +207,12 @@ void main() { // Update conversation pause effect conversationPauseUpdate(); - // Update question box. + // Update question box and textbox questionBoxUpdate(); - - // Update conversation textbox conversationTextboxUpdate(); - // Update conversation fade effect conversationFadeUpdate(); - // mainDebugDraw(); + mainDebugDraw(); } } \ No newline at end of file