Poke_Transporter_GB/source/text_engine.cpp
Philippe Symons 26fd1e2dd3 Add compression for the text data, output stack usage .su files and rework script_array
Add a binary table format and convert the text entries into this format in text_helper/main.py. It then gets compressed with zx0.

The new text_data_table and streamed_data_table classes exist to read the various entries from this binary table. streamed_data_table specifically
exists to use a decompression buffer that is smaller than the actual binary table. But it requires a decompression buffer that is
still larger than ZX0_DEFAULT_WINDOW_SIZE (default: 2048 bytes) and will only be able to decompress in
chunks of (<decompression_buffer_size> - <ZX0_DEFAULT_WINDOW_SIZE>) bytes

Try to keep the binary text tables sufficiently small though, because since zx0 doesn't actually support random access,
getting to the last entry is significantly more expensive than reading the first one. And unless you use streamed_data_table,
it also requires <uncompressed_size> bytes of stack space, therefore IWRAM to decompress them.

I also had to rework script_array because it can no longer reference the strings directly. Instead we now reference the DIA_* "enum" values.
We also no longer store an array of script_obj instances, because these were getting stored in IWRAM since they're non-const global variables
originally. Instead we now have const arrays of script_obj_params structs, which should end up in .rodata -> therefore EWRAM.

Right now, script_obj only supports the PTGB text table (originally the dialogue array). But if the need arises to support other tables as well,
I'd consider adding a separate enum to script_obj_params to indicate the specific table.

The compilation process will also output .su files in the build folder from now on. These files indicate the stack frame size for every function in
every compilation unit, so be sure to check them from time to time. Note that they will only show the stack consumption for that specific function.
So to get the worst case stack consumption, you need to manually add all the functions in a certain stack flow.
2025-05-21 12:21:06 +02:00

277 lines
7.6 KiB
C++

#include <tonc.h>
#include <string>
#include <cstring>
#include "text_engine.h"
#include "global_frame_controller.h"
#include "pkmn_font.h"
#include "script_array.h"
#include "debug_mode.h"
#include "button_menu.h"
#include "sprite_data.h"
#include "latin_short.h"
#include "japanese_small.h"
#include "text_data_table.h"
#define TEXT_CBB 0
#define TEXT_SBB 10
script_obj curr_line;
uint char_index;
uint line_char_index;
const byte *curr_text;
bool text_exit;
void init_text_engine()
{
// Load the TTE
// tte_init_se(3, BG_CBB(TEXT_CBB) | BG_SBB(TEXT_SBB) | BG_PRIO(0), 0, CLR_WHITE, 14, &japanese_smallFont, NULL);
tte_init_chr4c(3, // BG 0
BG_CBB(TEXT_CBB) | BG_SBB(TEXT_SBB), // Charblock 0; screenblock 31
0xF000, // Screen-entry offset
bytes2word( // Color attributes:
15, // Text color
0, // Shadow color
0, // Paper
0), // Special
CLR_WHITE, // White text
BUILD_FONT, // Custom font
NULL // Use default chr4 renderer
);
tte_init_con();
// tte_set_margins(LEFT, TOP, RIGHT, BOTTOM);
// tte_set_pos(LEFT, TOP);
pal_bg_bank[15][INK_WHITE] = CLR_WHITE; // White
pal_bg_bank[15][INK_DARK_GREY] = 0b0000110001100010; // Dark Grey
// 14 will be changed to game color
// Set default variables
char_index = 0;
line_char_index = 0;
text_exit = false;
}
int text_loop(int script)
{
u8 text_decompression_buffer[3072];
u8 index_buffer[100];
streamed_text_data_table dialogue_table(text_decompression_buffer, sizeof(text_decompression_buffer), index_buffer);
dialogue_table.decompress(get_compressed_PTGB_table());
switch (script)
{
case BTN_TRANSFER:
curr_line = transfer_script_params[T_SCRIPT_START];
break;
case BTN_EVENTS:
curr_line = event_script_params[E_SCRIPT_START];
break;
}
curr_text = (curr_line.has_text()) ? dialogue_table.get_text_entry(curr_line.get_text_entry_index()) : NULL;
REG_BG1CNT = (REG_BG1CNT && !BG_PRIO_MASK) | BG_PRIO(2); // Show Fennel
show_text_box();
// tte_set_margins(LEFT, TOP, RIGHT, BOTTOM);
while (true) // This loops through all the connected script objects
{
if (curr_text != NULL && curr_text[char_index] != 0xFF && curr_text[char_index] != 0xFB)
{
tte_set_pos(LEFT, TOP);
tte_erase_rect(LEFT, TOP, RIGHT, BOTTOM);
ptgb_write(curr_text, char_index);
}
wait_for_user_to_continue(false);
line_char_index = 0;
switch (script)
{
case BTN_TRANSFER:
curr_line = transfer_script_params[text_next_obj_id(curr_line)];
break;
case BTN_EVENTS:
curr_line = event_script_params[text_next_obj_id(curr_line)];
break;
}
curr_text = (curr_line.has_text()) ? dialogue_table.get_text_entry(curr_line.get_text_entry_index()) : NULL;
char_index = 0;
if (text_exit)
{
hide_text_box();
tte_erase_rect(LEFT, TOP, RIGHT, BOTTOM);
text_exit = false;
return 0;
}
}
}
int text_next_obj_id(script_obj current_line)
{
if (current_line.get_cond_id() == 0)
{
return current_line.get_true_index();
}
else
{
if (run_conditional(current_line.get_cond_id()))
{
return current_line.get_true_index();
}
return current_line.get_false_index();
}
}
void show_text_box()
{
REG_BG2CNT = (REG_BG2CNT & ~BG_PRIO_MASK) | BG_PRIO(1);
}
void hide_text_box()
{
REG_BG2CNT = (REG_BG2CNT & ~BG_PRIO_MASK) | BG_PRIO(3);
}
void set_text_exit()
{
text_exit = true;
key_poll(); // This removes the "A Hit" when exiting the text
}
// Implement a version that just writes the whole string
int ptgb_write(const byte *text, bool instant)
{
return ptgb_write(text, instant, 9999); // This is kinda silly but it'll work.
}
// Re-implementing TTE's "tte_write" to use the gen 3 character encoding chart
int ptgb_write(const byte *text, bool instant, int length)
{
if (text == NULL)
return 0;
uint ch, gid;
char *str = (char *)text;
TTC *tc = tte_get_context();
TFont *font;
int num = 0;
while ((ch = *str) != 0xFF && num < length)
{
if (get_frame_count() % 2 == 0 || key_held(KEY_B) || key_held(KEY_A) || instant)
{
str++;
switch (ch)
{
case 0xFB:
if (DISPLAY_CONTROL_CHAR)
{
tc->drawgProc(0xB9);
}
wait_for_user_to_continue(true);
break;
case 0xFE:
if (DISPLAY_CONTROL_CHAR)
{
tc->drawgProc(0xEF);
}
tc->cursorY += tc->font->charH;
tc->cursorX = tc->marginLeft;
break;
default:
// Get glyph index and call renderer
font = tc->font;
gid = ch - font->charOffset;
if (tc->charLut)
gid = tc->charLut[gid];
// Character wrap
int charW = font->widths ? font->widths[gid] : font->charW;
// We don't want this tbh- all of the newlines should deal with moving to the next line
/* if (tc->cursorX + charW > tc->marginRight)
{
tc->cursorY += 10; // font->charH;
tc->cursorX = tc->marginLeft;
} */
// Draw and update position
tc->drawgProc(gid);
tc->cursorX += charW;
}
num += 1;
}
if (get_curr_flex_background() == BG_FENNEL && !instant)
{
fennel_speak(((num / 4) % 4) + 1);
}
if (!instant)
{
global_next_frame();
}
}
// Return characters used (PONDER: is this really the right thing?)
return 0; // str - text;
}
// This is mostly used for debug stuff, I shouldn't rely it on it much.
int ptgb_write_debug(const u16* charset, const char *text, bool instant)
{
byte temp_holding[256];
int i;
for (i = 0; i < 256; i++)
{
if (text[i] == '\0')
{
temp_holding[i] = 0xFF;
i = 256;
}
else if (text[i] == '\n')
{
temp_holding[i] = 0xFE;
}
else
{
temp_holding[i] = get_char_from_charset(charset, text[i]);
}
}
return ptgb_write(temp_holding, instant);
}
// Adding this to avoid compiler issues temporarilly
int ptgb_write(const char *text)
{
return 0;
}
void wait_for_user_to_continue(bool clear_text)
{
if (get_curr_flex_background() == BG_FENNEL)
{
if (get_missingno_enabled())
{
fennel_speak(0);
fennel_blink(4);
}
else
{
fennel_speak(0);
}
}
while (!(key_hit(KEY_A) || key_hit(KEY_B) || curr_text == NULL))
{
global_next_frame();
}
if (clear_text)
{
tte_erase_rect(LEFT, TOP, RIGHT, BOTTOM);
tte_set_pos(LEFT, TOP);
}
}