From 4e92542f5c716d11143f6ea4bdbfc382df6b8586 Mon Sep 17 00:00:00 2001 From: GearsProgress Date: Thu, 16 Apr 2026 13:56:50 -0400 Subject: [PATCH] Updating global_next_frame to be interrupt based --- source/box_menu.cpp | 4 ++-- source/button_menu.cpp | 4 ++-- source/dbg/debug_menu_functions.cpp | 2 +- source/gameboy_colour.cpp | 16 ++++++------- source/global_frame_controller.cpp | 1 - source/main.cpp | 35 +++++++++++++++-------------- source/multiboot_upload.cpp | 6 ++--- source/pokedex.cpp | 4 ++-- source/script_array.cpp | 4 ++-- source/select_menu.cpp | 2 +- source/sprite_data.cpp | 14 ++++++------ source/text_engine.cpp | 14 ++++++------ source/vertical_menu.cpp | 4 ++-- 13 files changed, 55 insertions(+), 55 deletions(-) diff --git a/source/box_menu.cpp b/source/box_menu.cpp index 0d354f9..0b7192e 100644 --- a/source/box_menu.cpp +++ b/source/box_menu.cpp @@ -119,7 +119,7 @@ int Box_Menu::box_main(PokeBox* box) tte_erase_screen(); load_flex_background(FLEXBG_FENNEL, 2); REG_BG2VOFS = BG2VOF_SMALL_TEXTBOX; - global_next_frame(); + VBlankIntrWait(); return curr_button; } } @@ -168,6 +168,6 @@ int Box_Menu::box_main(PokeBox* box) update_pos = false; } } - global_next_frame(); + VBlankIntrWait(); } } \ No newline at end of file diff --git a/source/button_menu.cpp b/source/button_menu.cpp index fffde84..2b4177a 100644 --- a/source/button_menu.cpp +++ b/source/button_menu.cpp @@ -36,7 +36,7 @@ int Button_Menu::button_main() int curr_x = 0; int curr_y = 0; - key_poll(); // Reset the buttons + //key_poll(); // Reset the buttons while (true) { @@ -96,7 +96,7 @@ int Button_Menu::button_main() curr_position = get_pos_from_xy(curr_x, curr_y); button_vector.at(curr_position).set_highlight(true); } - global_next_frame(); + VBlankIntrWait(); } return 0; } diff --git a/source/dbg/debug_menu_functions.cpp b/source/dbg/debug_menu_functions.cpp index 46a649e..279b21b 100644 --- a/source/dbg/debug_menu_functions.cpp +++ b/source/dbg/debug_menu_functions.cpp @@ -188,7 +188,7 @@ void show_debug_info_screen(void *context, unsigned user_param) reload_textbox_background(); return; } - global_next_frame(); + VBlankIntrWait(); } } diff --git a/source/gameboy_colour.cpp b/source/gameboy_colour.cpp index b73931e..1d6644d 100644 --- a/source/gameboy_colour.cpp +++ b/source/gameboy_colour.cpp @@ -136,9 +136,9 @@ void print(const char *format, ...) void setup(const u16 *debug_charset) { - interrupt_init(); - interrupt_set_handler(INTR_SERIAL, LINK_SPI_ISR_SERIAL); - interrupt_enable(INTR_SERIAL); + //interrupt_init(); + //interrupt_set_handler(INTR_SERIAL, LINK_SPI_ISR_SERIAL); + //interrupt_enable(INTR_SERIAL); linkSPI->activate(LinkSPI::Mode::MASTER_256KBPS); linkSPI->setWaitModeActive(false); @@ -374,9 +374,9 @@ int loop(byte *box_data_storage, byte *curr_payload, GB_ROM *curr_gb_rom, PokeBo { while (!key_hit(KEY_R)) { - global_next_frame(); + VBlankIntrWait(); } - global_next_frame(); + VBlankIntrWait(); } // TODO: Restore Errors in_data = linkSPI->transfer(out_data); @@ -467,7 +467,7 @@ int loop(byte *box_data_storage, byte *curr_payload, GB_ROM *curr_gb_rom, PokeBo counter++; for (int i = 0; i < mosi_delay; i++) { - global_next_frame(); + VBlankIntrWait(); } } }; @@ -617,9 +617,9 @@ byte exchange_boxes(byte curr_in, byte *box_data_storage, GB_ROM *curr_gb_rom, c while (!key_held(KEY_A)) { - global_next_frame(); + VBlankIntrWait(); } - global_next_frame(); + VBlankIntrWait(); } packet_index = 0; init_packet = false; diff --git a/source/global_frame_controller.cpp b/source/global_frame_controller.cpp index 098fd9e..cd60ab3 100644 --- a/source/global_frame_controller.cpp +++ b/source/global_frame_controller.cpp @@ -76,7 +76,6 @@ void global_next_frame() } } global_frame_count++; - VBlankIntrWait(); }; int get_frame_count() diff --git a/source/main.cpp b/source/main.cpp index ee0c9fe..947a3aa 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -81,12 +81,15 @@ void initialization_script(void) REG_IE = 0; // Sound bank init - irq_init(NULL); - irq_enable(II_VBLANK); + //irq_init(NULL); + //irq_enable(II_VBLANK); // This currently crashes when you try to transfer a Pokemon: // sound_init(); // Graphics init + irq_init(NULL); + irq_add(II_VBLANK, global_next_frame); + irq_enable(II_VBLANK); oam_init(obj_buffer, 128); load_graphics(); @@ -118,10 +121,10 @@ void game_load_error(void) GENERAL_INDEX, GENERAL_cart_load_error, false); } - key_poll(); + //key_poll(); do { - global_next_frame(); + //global_next_frame(); } while (!key_hit(KEY_A) && !key_hit(KEY_SELECT)); tte_erase_rect(0, 0, H_MAX, V_MAX); @@ -143,7 +146,7 @@ void game_load_error(void) while (delay_counter < 60) { delay_counter++; - global_next_frame(); + VBlankIntrWait(); } } @@ -161,7 +164,7 @@ void first_load_message(void) while (!key_hit(KEY_A)) { - global_next_frame(); + VBlankIntrWait(); } } @@ -174,7 +177,6 @@ int credits() credits_text_table.decompress(get_compressed_text_table(CREDITS_INDEX)); bool update = true; - global_next_frame(); while (true) { if (update) @@ -202,7 +204,7 @@ int credits() update = true; } - global_next_frame(); + VBlankIntrWait(); } }; @@ -265,8 +267,7 @@ int main_menu_loop() { update = false; } - - global_next_frame(); + VBlankIntrWait(); } } @@ -277,7 +278,7 @@ static void show_gears_of_progress() delay_counter = 0; while (delay_counter < (15 * 60)) { - global_next_frame(); + VBlankIntrWait(); delay_counter++; if (key_hit(KEY_A)) { @@ -309,7 +310,7 @@ static void __attribute__((noinline)) show_intro() BG_FLEX = BG_FLEX | BG_PRIO(3); - key_poll(); // Reset the keys + //key_poll(); // Reset the keys curr_GBA_rom.load_rom(false); obj_set_pos(ptgb_logo_l, 56, 12); @@ -329,7 +330,7 @@ static void __attribute__((noinline)) show_intro() while (!start_pressed) { fade = abs(((get_frame_count() / 6) % 24) - 12); - global_next_frame(); + VBlankIntrWait(); start_pressed = key_hit(KEY_START) | key_hit(KEY_A); REG_BLDALPHA = BLDA_BUILD(0b10000, fade); } @@ -350,7 +351,7 @@ int main(void) }*/ show_intro(); - key_poll(); + //key_poll(); tte_erase_rect(0, 0, H_MAX, V_MAX); REG_BLDALPHA = BLDA_BUILD(0b10000, 0); // Reset fade @@ -365,7 +366,7 @@ int main(void) else { obj_hide_multi(ptgb_logo_l, 2); - global_next_frame(); + VBlankIntrWait(); game_load_error(); // initialization_script(); } @@ -415,7 +416,7 @@ int main(void) if (get_tutorial_flag()) { obj_hide_multi(ptgb_logo_l, 2); - global_next_frame(); + //global_next_frame(); load_flex_background(FLEXBG_DEX, 2); set_background_pal(curr_GBA_rom.gamecode, true, false); pokedex_loop(); @@ -441,7 +442,7 @@ int main(void) break; #endif default: - global_next_frame(); + VBlankIntrWait(); } } } \ No newline at end of file diff --git a/source/multiboot_upload.cpp b/source/multiboot_upload.cpp index ef68e4c..360c0cc 100644 --- a/source/multiboot_upload.cpp +++ b/source/multiboot_upload.cpp @@ -22,7 +22,7 @@ void multiboot_upload_screen() // wait for key press do { - global_next_frame(); + VBlankIntrWait(); } while (!key_hit(KEY_A) && !key_hit(KEY_B)); if (key_hit(KEY_B)) @@ -35,7 +35,7 @@ void multiboot_upload_screen() // multiboot_show_textbox(); ptgb_write_textbox(general_text.get_text_entry(GENERAL_send_multiboot_wait), true, false, GENERAL_INDEX, GENERAL_send_multiboot_wait, false); - global_next_frame(); + VBlankIntrWait(); const u32 romSize = 256 * 1024; // EWRAM = 256 KB LinkCableMultiboot::Result multibootResult = linkCableMultiboot.sendRom( @@ -63,6 +63,6 @@ void multiboot_upload_screen() // wait for keypress again. do { - global_next_frame(); + VBlankIntrWait(); } while (!key_hit(KEY_A)); } \ No newline at end of file diff --git a/source/pokedex.cpp b/source/pokedex.cpp index 51abeee..b047a90 100644 --- a/source/pokedex.cpp +++ b/source/pokedex.cpp @@ -232,7 +232,7 @@ int pokedex_loop() ptgb_write_simple(is_caught(dex_shift + i + 1 + mythic_skip) ? PKMN_NAMES.get_text_entry(dex_shift + i + 1 + mythic_skip) : undiscovered_text, true); } - global_next_frame(); // This is a bit silly, but it works. Makes the types one frame off from the text, but that's 'fine' + //global_next_frame(); // This is a bit silly, but it works. Makes the types one frame off from the text, but that's 'fine' // Eventually it could be optimized to move the labels around, but this honestly makes the most sense. Less code but one frame different for (int i = 0; i < DEX_MAX; i++) { @@ -240,7 +240,7 @@ int pokedex_loop() } update = false; } - global_next_frame(); + VBlankIntrWait(); } } diff --git a/source/script_array.cpp b/source/script_array.cpp index 2776b76..86707a9 100644 --- a/source/script_array.cpp +++ b/source/script_array.cpp @@ -881,7 +881,7 @@ bool run_conditional(int index) REG_BG1HOFS = i + FENNEL_SHIFT; if (!g_debug_options.instant_text_speed) { - global_next_frame(); + VBlankIntrWait(); } } return true; @@ -892,7 +892,7 @@ bool run_conditional(int index) REG_BG1HOFS = i + FENNEL_SHIFT; if (!g_debug_options.instant_text_speed) { - global_next_frame(); + VBlankIntrWait(); } } return true; diff --git a/source/select_menu.cpp b/source/select_menu.cpp index a09a00f..5e9202c 100644 --- a/source/select_menu.cpp +++ b/source/select_menu.cpp @@ -76,7 +76,7 @@ int Select_Menu::select_menu_main() menu_widget_.hide(); - global_next_frame(); + //global_next_frame(); return item_value; } diff --git a/source/sprite_data.cpp b/source/sprite_data.cpp index d02965d..71ae024 100644 --- a/source/sprite_data.cpp +++ b/source/sprite_data.cpp @@ -103,7 +103,7 @@ void set_background_pal(int curr_rom_id, bool dark, bool fade) ((((NUM_CYCLES - n) * INV_NUM_CYCLES) * old_pal[1]) + ((n * INV_NUM_CYCLES) * new_pal[1])) >> 16, ((((NUM_CYCLES - n) * INV_NUM_CYCLES) * old_pal[2]) + ((n * INV_NUM_CYCLES) * new_pal[2])) >> 16); } - global_next_frame(); + //global_next_frame(); } } else @@ -132,7 +132,7 @@ void load_flex_background(int background_id, int layer) if (curr_flex_background != background_id) // Only load the background if it isn't already loaded { // This prevents screen tearing on this frame - global_next_frame(); + //global_next_frame(); BG_FLEX = (BG_FLEX && !BG_PRIO_MASK) | BG_PRIO(3); switch (background_id) @@ -143,7 +143,7 @@ void load_flex_background(int background_id, int layer) // Load tiles into CBB 0 LZ77UnCompVram(openingBGTiles, &tile_mem[CBB][0]); // Give it a frame to uncompress the data - global_next_frame(); + //global_next_frame(); // Load map into SBB 0 LZ77UnCompVram(openingBGMap, &se_mem[SBB][0]); REG_BG1VOFS = 96; @@ -154,7 +154,7 @@ void load_flex_background(int background_id, int layer) // Load tiles into CBB 0 LZ77UnCompVram(fennelBGTiles, &tile_mem[CBB][0]); // Give it a frame to uncompress the data - global_next_frame(); + //global_next_frame(); // Load map into SBB 0 LZ77UnCompVram(fennelBGMap, &se_mem[SBB][0]); REG_BG1VOFS = FENNEL_SHIFT; @@ -165,7 +165,7 @@ void load_flex_background(int background_id, int layer) // Load tiles into CBB 0 LZ77UnCompVram(dexBGTiles, &tile_mem[CBB][0]); // Give it a frame to uncompress the data - global_next_frame(); + //global_next_frame(); // Load map into SBB 0 LZ77UnCompVram(dexBGMap, &se_mem[SBB][0]); REG_BG1VOFS = 0; @@ -176,7 +176,7 @@ void load_flex_background(int background_id, int layer) // Load tiles into CBB 0 LZ77UnCompVram(menu_barsTiles, &tile_mem[CBB][0]); // Give it a frame to uncompress the data - global_next_frame(); + //global_next_frame(); // Load map into SBB 0 LZ77UnCompVram(menu_barsMap, &se_mem[SBB][0]); REG_BG1VOFS = 0; @@ -187,7 +187,7 @@ void load_flex_background(int background_id, int layer) // Load tiles into CBB 0 LZ77UnCompVram(boxBGTiles, &tile_mem[CBB][0]); // Give it a frame to uncompress the data - global_next_frame(); + //global_next_frame(); // Load map into SBB 0 LZ77UnCompVram(boxBGMap, &se_mem[SBB][0]); REG_BG1VOFS = 0; diff --git a/source/text_engine.cpp b/source/text_engine.cpp index 8334c80..213c659 100644 --- a/source/text_engine.cpp +++ b/source/text_engine.cpp @@ -168,7 +168,7 @@ int text_loop(int script) bool exit = false; bool update_text = true; bool instant_text = false; - key_poll(); + VBlankIntrWait(); while (!exit) { if (key_hit(KEY_LEFT)) @@ -216,7 +216,7 @@ int text_loop(int script) ptgb_write_debug(debug_charset, ")", true); update_text = false; } - global_next_frame(); + VBlankIntrWait(); } line_char_index = 0; @@ -263,7 +263,7 @@ int text_next_obj_id(script_obj current_line) void set_text_exit() { text_exit = true; - key_poll(); // This removes the "A Hit" when exiting the text + VBlankIntrWait(); // This removes the "A Hit" when exiting the text } // Implement a version that creates the textbox as well @@ -399,7 +399,7 @@ int ptgb_write(const byte *text, bool instant, int length, int box_type) } if (!instant) { - global_next_frame(); + VBlankIntrWait(); } } @@ -451,10 +451,10 @@ void wait_for_user_to_continue() fennel_speak(0); } } - key_poll(); + VBlankIntrWait(); while (!(key_hit(KEY_A) || key_hit(KEY_B))) { - global_next_frame(); + VBlankIntrWait(); } } @@ -466,7 +466,7 @@ void scroll_text(bool instant, TTC *tc, int left, int top, int right, int bottom tte_erase_rect(left, top - tc->font->charH, right, top + i); if (!instant) { - global_next_frame(); + VBlankIntrWait(); } } REG_BG3VOFS = 0; diff --git a/source/vertical_menu.cpp b/source/vertical_menu.cpp index 4903c3d..ce8e143 100644 --- a/source/vertical_menu.cpp +++ b/source/vertical_menu.cpp @@ -247,7 +247,7 @@ unsigned vertical_menu::run() while(true) { - key_poll(); // Reset the buttons + //key_poll(); // Reset the buttons input_result = handle_input(); @@ -269,7 +269,7 @@ unsigned vertical_menu::run() run_cycle_handler_->on_run_cycle(); } - global_next_frame(); + VBlankIntrWait(); } // should never happen return UINT32_MAX;