Poke_Transporter_GB/source/box_menu.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

175 lines
5.4 KiB
C++

#include <tonc.h>
#include "button_menu.h"
#include "button_handler.h"
#include "save_data_manager.h"
#include "global_frame_controller.h"
#include "string"
#include "sprite_data.h"
#include "box_menu.h"
#include "pokemon_data.h"
#include "text_engine.h"
#include "translated_text.h"
#include "text_data_table.h"
#include "zx0_decompressor.h"
Box_Menu::Box_Menu() {};
int Box_Menu::box_main(Pokemon_Party party_data)
{
u8 names_decompression_buffer[3072];
text_data_table PKMN_NAMES(names_decompression_buffer);
tte_erase_screen();
load_flex_background(BG_BOX, 2);
REG_BG1VOFS = 0;
REG_BG1HOFS = 0;
REG_BG2CNT = (REG_BG2CNT & ~BG_PRIO_MASK) | BG_PRIO(3);
load_temp_box_sprites(&party_data);
Button cancel_button(button_cancel_left, button_cancel_right, 64);
Button confirm_button(button_confirm_left, button_confirm_right, 64);
cancel_button.set_location(88, 144);
confirm_button.set_location(160, 144);
cancel_button.show();
confirm_button.show();
curr_button = 0;
x = 0;
y = 0;
bool update_pos = true;
obj_unhide(box_select, 0);
int index = 0;
PKMN_NAMES.decompress(get_compressed_pkmn_names_table());
while (true)
{
if (get_frame_count() % 20 == 0)
{
for (int i = 0; i < 30; i++)
{
update_menu_sprite(&party_data, i, get_frame_count() % 40);
}
}
if (curr_button == 0)
{
if (key_hit(KEY_LEFT) && (x > 0))
{
x--;
update_pos = true;
}
else if (key_hit(KEY_RIGHT) && (x < 5))
{
x++;
update_pos = true;
}
else if (key_hit(KEY_UP) && y > 0)
{
y--;
update_pos = true;
}
else if (key_hit(KEY_DOWN) && (y < 4))
{
y++;
update_pos = true;
}
else if (key_hit(KEY_DOWN) && (y == 4))
{
obj_hide(box_select);
if (x < 3)
{
cancel_button.set_highlight(true);
curr_button = CANCEL_BUTTON;
}
else
{
confirm_button.set_highlight(true);
curr_button = CONFIRM_BUTTON;
}
}
}
else
{
if (key_hit(KEY_LEFT) && (curr_button == CONFIRM_BUTTON))
{
curr_button = CANCEL_BUTTON;
cancel_button.set_highlight(true);
confirm_button.set_highlight(false);
x -= 3;
}
else if (key_hit(KEY_RIGHT) && (curr_button == CANCEL_BUTTON))
{
curr_button = CONFIRM_BUTTON;
cancel_button.set_highlight(false);
confirm_button.set_highlight(true);
x += 3;
}
else if (key_hit(KEY_UP))
{
curr_button = 0;
cancel_button.set_highlight(false);
confirm_button.set_highlight(false);
obj_unhide(box_select, 0);
update_pos = true;
}
else if (key_hit(KEY_A))
{
cancel_button.hide();
confirm_button.hide();
for (int i = 0; i < 30; i++)
{
obj_hide(party_sprites[i]);
}
tte_erase_screen();
load_flex_background(BG_FENNEL, 2);
REG_BG2VOFS = BG2VOF_SMALL_TEXTBOX;
global_next_frame();
return curr_button;
}
}
if (update_pos)
{
index = x + (y * BOXMENU_HNUM);
obj_set_pos(box_select, BOXMENU_LEFT + (x * (BOXMENU_SPRITE_WIDTH + BOXMENU_HSPACE)), BOXMENU_TOP + (y * (BOXMENU_SPRITE_HEIGHT + BOXMENU_VSPACE)));
tte_erase_rect(6, 16, 80, 152);
Simplified_Pokemon curr_pkmn = party_data.get_simple_pkmn(index);
obj_hide(grabbed_front_sprite);
if (curr_pkmn.is_valid)
{
byte val[11];
tte_set_pos(6, 88);
ptgb_write(curr_pkmn.nickname, true);
if (curr_pkmn.is_shiny)
{
tte_set_pos(64, 16);
val[0] = 0xF7;
val[1] = 0xFF;
ptgb_write(val, true);
}
tte_set_pos(14, 98);
if (curr_pkmn.is_missingno)
{
ptgb_write(PKMN_NAMES.get_text_entry(0), true);
}
else
{
ptgb_write(PKMN_NAMES.get_text_entry(curr_pkmn.dex_number), true);
}
tte_set_pos(6, 108);
val[0] = 0xC6; // L
val[1] = 0xEA; // v
val[2] = 0xF0; // :
val[3] = 0x00; // " "
val[4] = 0xFF; // endline
ptgb_write(val, true);
convert_int_to_ptgb_str(curr_pkmn.met_level, val); // Val should never go out of bounds
ptgb_write(val, true);
update_front_box_sprite(&curr_pkmn);
obj_unhide(grabbed_front_sprite, 0);
update_pos = false;
}
}
global_next_frame();
}
}