diff --git a/src/conversation/textbox.c b/src/conversation/textbox.c index 1464ba6..6731eab 100644 --- a/src/conversation/textbox.c +++ b/src/conversation/textbox.c @@ -7,24 +7,18 @@ #include "textbox.h" -char TEXTBOX_TEXT[TEXTBOX_BUFFER_MAX]; +char TEXTBOX_TEXTS[TEXTBOX_SCROLL_ROWS_MAX * TEXTBOX_CHARS_PER_ROW]; uint8_t TEXTBOX_ROW_COUNT; uint8_t TEXTBOX_ROW_CURRENT; - -uint8_t TEXTBOX_TEXT_LENGTH; uint8_t TEXTBOX_STATE; uint8_t TEXTBOX_SCROLL; -uint8_t TEXTBOX_CHAR_POSITION; inline void conversationTextboxInit() { uint8_t i; uint8_t TEXTBOX_TILES[TEXTBOX_TILES_MAX]; // Reset textbox state - TEXTBOX_TEXT[0] = '\0'; TEXTBOX_STATE = 0; - TEXTBOX_TEXT_LENGTH = 0; - TEXTBOX_SCROLL = 0; // Setup window data move_win(7, SCREENHEIGHT - (TEXTBOX_HEIGHT_IN_TILES * 8)); @@ -55,26 +49,56 @@ inline void conversationTextboxInit() { ); } -void conversationTextboxSetText(char *text, uint8_t length) { - uint8_t i, j; - char c; +void conversationTextboxSetText(char *text) { + uint8_t i, j, k, rowStart, stateFlags; + char c, c2; uint8_t TEXTBOX_TILES[TEXTBOX_CHAR_ROWS * TEXTBOX_CHARS_PER_ROW]; - TEXTBOX_TEXT_LENGTH = 0; - i = 0; - - while((c = text[i]) != '\0') { - TEXTBOX_TEXT[i] = c; - TEXTBOX_TEXT_LENGTH++; - i++; + for(i = 0; i < TEXTBOX_SCROLL_ROWS_MAX * TEXTBOX_CHARS_PER_ROW; i++) { + TEXTBOX_TEXTS[i] = '4'; } // Reset textbox state TEXTBOX_STATE = TEXTBOX_STATE_VISIBLE; TEXTBOX_SCROLL = 0; - TEXTBOX_CHAR_POSITION = 0; + TEXTBOX_ROW_CURRENT = 0; - // Fill blank characters + // Copy source text to buffer, also determine wordwrapping here. + i = 0, j = 0, rowStart = 0, stateFlags = 0; + while((c = text[i]) != '\0') { + if(c == ' ') { + // Scan ahead and look at how many chars remain to the next space + k = i; + while((c2 = text[++k]) != '\0') { + if(c2 == '\n' || c2 == ' ') break; + if((k - rowStart) >= TEXTBOX_CHARS_PER_ROW) break; + } + + // 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)) { + stateFlags |= 1 << 0; + } + } else if(c == '\n') { + stateFlags |= 1 << 0; + } + + // Do we need to newline? + if((stateFlags & (1 << 0)) != 0) { + stateFlags &= ~(1 << 0); + rowStart += TEXTBOX_CHARS_PER_ROW; + j = rowStart; + i++; + continue; + } + + TEXTBOX_TEXTS[j] = c; + i++; + j++; + } + + // Now we have organized the string nicely we can prep for rendering. Fill the + // tiles with blank chars. for(j = 0; j < TEXTBOX_CHAR_ROWS; j++) { for(i = 0; i < TEXTBOX_CHARS_PER_ROW ; i++) { TEXTBOX_TILES[i + (j * TEXTBOX_CHARS_PER_ROW)] = TEXTBOX_TILE_BLANK; @@ -92,45 +116,50 @@ void conversationTextboxSetText(char *text, uint8_t length) { inline void conversationTextboxUpdate() { uint8_t i; + char c; // Is the textbox visible? if(!(TEXTBOX_STATE & TEXTBOX_STATE_VISIBLE)) return; // Has the textbox finished scrolling? - if(TEXTBOX_STATE & TEXTBOX_STATE_SCROLLED) { - // Is the player attempting to close the textbox? - if(INPUT_STATE & J_A) { - TEXTBOX_STATE &= ~TEXTBOX_STATE_VISIBLE; - HIDE_WIN; - conversationQueueNext(); - } - return; - } + // if(TEXTBOX_STATE & TEXTBOX_STATE_SCROLLED) { + // // Is the player attempting to close the textbox? + // if(INPUT_STATE & J_A) { + // TEXTBOX_STATE &= ~TEXTBOX_STATE_VISIBLE; + // HIDE_WIN; + // conversationQueueNext(); + // } + // return; + // } // Move to the next character. - if(TEXTBOX_TEXT[TEXTBOX_SCROLL] == ' ') { - // Whitespace, do nothing. - TEXTBOX_CHAR_POSITION++; - } else if(TEXTBOX_TEXT[TEXTBOX_SCROLL] == '\n') { - // Newline character, move the next tile to the next row. - TEXTBOX_CHAR_POSITION = ( - (TEXTBOX_CHAR_POSITION / TEXTBOX_CHARS_PER_ROW) + 1 - ) * TEXTBOX_CHARS_PER_ROW; - } else { - // Reveal the next character. TODO: I'll optimize this. - set_win_tiles( - 1 + (TEXTBOX_CHAR_POSITION % TEXTBOX_CHARS_PER_ROW), - 1 + (TEXTBOX_CHAR_POSITION / TEXTBOX_CHARS_PER_ROW), - 1, 1, - TEXTBOX_TEXT + TEXTBOX_SCROLL - ); - TEXTBOX_CHAR_POSITION++; - } - TEXTBOX_SCROLL++; + i = TEXTBOX_ROW_CURRENT * TEXTBOX_CHARS_PER_ROW; + c = TEXTBOX_TEXTS[i+TEXTBOX_SCROLL]; + + // while((c = TEXTBOX_TEXTS[i+TEXTBOX_SCROLL]) == ' ') { + // TEXTBOX_SCROLL++; - // Update state. TODO: I actually don't really need this state, it's just here - // incase I want to check if the state has scrolled later on? Doubt it though. - if(TEXTBOX_SCROLL == TEXTBOX_TEXT_LENGTH) { - TEXTBOX_STATE |= TEXTBOX_STATE_SCROLLED; + // if(TEXTBOX_SCROLL == TEXTBOX_CHARS_MAX) { + // c = '\0'; + // break; + // } + // } + + if(TEXTBOX_SCROLL == TEXTBOX_CHARS_MAX) { + // TEXTBOX_ + } else if(c != '\0') { + set_win_tiles( + 1 + (TEXTBOX_SCROLL % TEXTBOX_CHARS_PER_ROW), + 1 + (TEXTBOX_SCROLL / TEXTBOX_CHARS_PER_ROW), + 1, 1, + 0x04 + ); + TEXTBOX_SCROLL++; } + + // // Update state. TODO: I actually don't really need this state, it's just here + // // incase I want to check if the state has scrolled later on? Doubt it though. + // if(TEXTBOX_SCROLL == TEXTBOX_TEXT_LENGTH) { + // TEXTBOX_STATE |= TEXTBOX_STATE_SCROLLED; + // } } \ No newline at end of file diff --git a/src/conversation/textbox.h b/src/conversation/textbox.h index a3f9947..ca97ea8 100644 --- a/src/conversation/textbox.h +++ b/src/conversation/textbox.h @@ -39,19 +39,19 @@ #define TEXTBOX_TILES_ROWS 3 #define TEXTBOX_TILE_BLANK COMMON_TILE_3 +#define TEXTBOX_SCROLL_ROWS_MAX 9 + +// STR_ ## name ## _DATA, STR_ ## name ## _LENGTH #define conversationTextboxString(name) conversationTextboxSetText(\ - STR_ ## name ## _DATA, STR_ ## name ## _LENGTH\ + STR_ ## name ## _DATA\ ) -extern char TEXTBOX_TEXT[TEXTBOX_BUFFER_MAX]; +extern char TEXTBOX_TEXTS[TEXTBOX_SCROLL_ROWS_MAX * TEXTBOX_CHARS_PER_ROW]; extern uint8_t TEXTBOX_ROW_COUNT; extern uint8_t TEXTBOX_ROW_CURRENT; - -extern uint8_t TEXTBOX_TEXT_LENGTH; extern uint8_t TEXTBOX_STATE; extern uint8_t TEXTBOX_SCROLL; -extern uint8_t TEXTBOX_CHAR_POSITION; inline void conversationTextboxInit(); -void conversationTextboxSetText(char *text, uint8_t length); +void conversationTextboxSetText(char *text); inline void conversationTextboxUpdate(); \ No newline at end of file diff --git a/src/main.c b/src/main.c index e3cf4b8..80bf412 100644 --- a/src/main.c +++ b/src/main.c @@ -166,10 +166,10 @@ void main() { // Alright begin the game logic here. // conversationQueueNext(); - char DEBUG_TEXT[] = "HEY"; - uint8_t DEBUG_TEXT_LENGTH = 3; - for(j = 0; j < DEBUG_TEXT_LENGTH; j++) DEBUG_TEXT[j] = DEBUG_TEXT[j] - 33 + 4; - conversationTextboxSetText(DEBUG_TEXT, DEBUG_TEXT_LENGTH); + char DEBUG_TEXT[] = "Hello World\nThe quick brown fox jumps over the lazy dog. How now brown cow? The fitness gram pacer test is a"; + // uint8_t DEBUG_TEXT_LENGTH = strlen(DEBUG_TEXT); + // for(j = 0; j < DEBUG_TEXT_LENGTH; j++) DEBUG_TEXT[j] = DEBUG_TEXT[j] - 33 + 4; + conversationTextboxSetText(DEBUG_TEXT); // Begin the loop while(1) {