No Description

pokedex_region.c 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #include <agb_debug.h>
  2. #include <constants/pkmns.h>
  3. #include <pokeagb/pokeagb.h>
  4. #include <pokeagb/core/m4a.h>
  5. #include <pokedex/pdexSelectBg.h>
  6. #include <pokedex/pdexSelectRegion.h>
  7. #include "pokedex_common.h"
  8. void region_select_load(void);
  9. extern const pchar *pdex_str_regions[];
  10. extern const pchar *pdex_str_empty;
  11. const u16 region_to_dex[] = {1, 152, 252, 393, 501, 656, 728};
  12. struct TextboxTemplate region_select_boxes[] = {
  13. {.bg_id = 0, .x = 4, .y = 7, .width = 6, .height = 3, .pal_id = 15, .charbase = 1},
  14. {.bg_id = 0, .x = 13, .y = 7, .width = 6, .height = 3, .pal_id = 15, .charbase = 19},
  15. {.bg_id = 0, .x = 22, .y = 7, .width = 6, .height = 3, .pal_id = 15, .charbase = 38},
  16. {.bg_id = 0, .x = 2, .y = 17, .width = 6, .height = 2, .pal_id = 15, .charbase = 57},
  17. {.bg_id = 0, .x = 9, .y = 17, .width = 6, .height = 2, .pal_id = 15, .charbase = 69},
  18. {.bg_id = 0, .x = 17, .y = 17, .width = 6, .height = 2, .pal_id = 15, .charbase = 81},
  19. {.bg_id = 0, .x = 24, .y = 17, .width = 6, .height = 2, .pal_id = 15, .charbase = 93},
  20. {.bg_id = 0xFF},
  21. };
  22. u8 region_text_x_offset[] = {2, 4, 2, 1, 5, 1, 4};
  23. u8 region_text_y_offset[] = {7, 7, 7, 2, 2, 2, 2};
  24. u8 region_select_x_offset[] = {54, 128, 198, 38, 97, 156, 215};
  25. u8 region_select_y_offset[] = {40, 40, 40, 115, 115, 115, 115};
  26. u16 region_icons[] = {PKMN_BISASAM, PKMN_SCHIGGY, PKMN_GLUMANDA, PKMN_ENDIVIE, PKMN_KARNIMANI, PKMN_FEURIGEL,
  27. PKMN_GECKARBOR, PKMN_HYDROPI, PKMN_FLEMMLI, PKMN_CHELAST, PKMN_PLINFA, PKMN_PANFLAM,
  28. PKMN_SERPIFEU, PKMN_OTTARO, PKMN_FLOINK, PKMN_IGAMARO, PKMN_FROXY, PKMN_FYNX,
  29. PKMN_BAUZ, PKMN_ROBBALL, PKMN_FLAMIAU};
  30. u16 region_icons_x_offset[] = {37, 49, 58, 110, 122, 133, 182, 193, 206, 22, 33,
  31. 43, 80, 93, 103, 139, 150, 161, 196, 207, 219};
  32. u16 region_icons_y_offset[] = {39, 17, 39, 39, 19, 39, 39, 19, 39, 117, 94,
  33. 116, 116, 95, 115, 116, 95, 116, 120, 99, 120};
  34. bool sm_pdex_init(void) {
  35. if (pal_fade_control.active)
  36. return false;
  37. audioDampenMaybe();
  38. sav1_secure_increment(0x29); // this is something the original dex routine does, probably for statistics
  39. /* maybe clean up safari stuff here if necessary */
  40. overworld_free_bgmaps();
  41. malloc_init((void *)0x2000000, 0x1C000);
  42. set_callback2(region_select_load);
  43. return true;
  44. }
  45. void region_select_load_gfx() {
  46. rbox_init_from_templates(&region_select_boxes[0]);
  47. lz77UnCompVram(pdexSelectBgTiles, (void *)0x0600C000);
  48. LZ77UnCompWram(pdexSelectBgMap, bgid_get_tilemap(2));
  49. gpu_pal_apply_compressed(pdexSelectBgPal, 0, 32);
  50. gpu_pal_apply(pdex_text_pal, 15 * 16, 32);
  51. bgid_mark_for_sync(0);
  52. }
  53. void region_oac_select(struct Object *obj) {
  54. obj->pos1.x = region_select_x_offset[pokedex_context->region_selected];
  55. obj->pos1.y = region_select_y_offset[pokedex_context->region_selected];
  56. }
  57. struct SpriteTiles region_select_tiles = {pdexSelectRegionTiles, 2048, DEX_REGION_SELECT};
  58. struct SpritePalette region_select_pal = {pdexSelectRegionPal, DEX_REGION_SELECT};
  59. const struct OamData region_select_oam = {
  60. .affine_mode = 0,
  61. .obj_mode = 0,
  62. .mosaic = false,
  63. .shape = 0,
  64. .size = 3,
  65. };
  66. struct Template region_select_template = {
  67. .tiles_tag = DEX_REGION_SELECT,
  68. .pal_tag = DEX_REGION_SELECT,
  69. .oam = &region_select_oam,
  70. .animation = &anim_image_empty,
  71. .graphics = &region_select_tiles,
  72. .rotscale = &rotscale_empty,
  73. .callback = region_oac_select,
  74. };
  75. void region_load_icon_palettes(void) {
  76. for (u8 i = 0; i < 3; ++i) {
  77. struct SpritePalette current = {pokeicon_pals[i], DEX_REGION_PAL(i)};
  78. gpu_pal_obj_alloc_tag_and_apply(&current);
  79. }
  80. }
  81. const struct OamData region_icon_oam = {
  82. .affine_mode = 0,
  83. .obj_mode = 0,
  84. .mosaic = false,
  85. .shape = 0,
  86. .size = 2,
  87. };
  88. void region_load_icon(u8 i) {
  89. struct SpriteTiles current = {pokeicon_table[region_icons[i]], 512, DEX_REGION_ICON(i)};
  90. gpu_tile_obj_alloc_tag_and_upload(&current);
  91. struct Template region_icon_template = {
  92. .tiles_tag = DEX_REGION_ICON(i),
  93. .pal_tag = DEX_REGION_PAL(pokeicon_pal_indices[region_icons[i]]),
  94. .oam = &region_icon_oam,
  95. .animation = &anim_image_empty,
  96. .graphics = &current,
  97. .rotscale = &rotscale_empty,
  98. .callback = oac_nullsub,
  99. };
  100. template_instanciate_forward_search(&region_icon_template, region_icons_x_offset[i], region_icons_y_offset[i], 0);
  101. }
  102. void region_load_border() {
  103. gpu_tile_obj_decompress_alloc_tag_and_upload(&region_select_tiles);
  104. gpu_pal_obj_alloc_tag_and_apply(&region_select_pal);
  105. (void)template_instanciate_forward_search(&region_select_template, 100, 100, 0);
  106. }
  107. void region_loop(u8 tid) {
  108. (void)tid;
  109. switch (pokedex_context->state) {
  110. case 0:
  111. bgid_send_tilemap(2);
  112. for (u8 i = 0; i < 7; ++i) {
  113. rboxid_clear_pixels(i, 0);
  114. rboxid_print(i, FONT_DEX_STD, region_text_x_offset[i], region_text_y_offset[i], &pdex_text_color, 0,
  115. pdex_str_regions[i + 1]);
  116. rboxid_update_tilemap_and_tileset(i);
  117. }
  118. region_load_border();
  119. region_load_icon_palettes();
  120. for (u8 i = 0; i < 21; ++i)
  121. region_load_icon(i);
  122. palette_bg_faded_fill_black();
  123. pokedex_context->state++;
  124. break;
  125. case 1:
  126. gpu_sync_bg_show(0);
  127. gpu_sync_bg_hide(1);
  128. gpu_sync_bg_hide(3);
  129. gpu_sync_bg_show(2);
  130. fade_screen(0xFFFFFFFF, PDEX_FADEIN_SPD, 16, 0, 0x0000);
  131. pokedex_context->state++;
  132. break;
  133. case 2:
  134. if (!pal_fade_control.active)
  135. pokedex_context->state++;
  136. break;
  137. case 3:
  138. switch (super.buttons_new) {
  139. case KEY_RIGHT:
  140. if (pokedex_context->region_selected < 6) {
  141. pokedex_context->region_selected++;
  142. m4aSongNumStart(101);
  143. }
  144. break;
  145. case KEY_DOWN:
  146. if (pokedex_context->region_selected < 3) {
  147. pokedex_context->region_selected += 3;
  148. m4aSongNumStart(101);
  149. }
  150. break;
  151. case KEY_LEFT:
  152. if (pokedex_context->region_selected > 0) {
  153. pokedex_context->region_selected--;
  154. m4aSongNumStart(101);
  155. }
  156. break;
  157. case KEY_UP:
  158. if ((pokedex_context->region_selected > 2) && (pokedex_context->region_selected < 6)) {
  159. pokedex_context->region_selected -= 3;
  160. m4aSongNumStart(101);
  161. }
  162. break;
  163. case KEY_A:
  164. fade_screen(0xFFFFFFFF, PDEX_FADEIN_SPD, 0, 16, 0x0000);
  165. pokedex_context->state = 10;
  166. m4aSongNumStart(5);
  167. break;
  168. case KEY_B:
  169. fade_screen(0xFFFFFFFF, PDEX_FADEIN_SPD, 0, 16, 0x0000);
  170. pokedex_context->state = 11;
  171. m4aSongNumStart(601);
  172. default:
  173. break;
  174. }
  175. break;
  176. case 10:
  177. if (!pal_fade_control.active) {
  178. task_del(tid);
  179. pdex_vram_free_bgmaps();
  180. pokedex_context->cursor_position_top = region_to_dex[pokedex_context->region_selected];
  181. set_callback2(pdex_load);
  182. }
  183. break;
  184. case 11:
  185. if (!pal_fade_control.active) {
  186. task_del(tid);
  187. pdex_free_memory();
  188. set_callback2(c2_overworld_switch_start_menu);
  189. set_callback1(c1_overworld);
  190. }
  191. break;
  192. default:
  193. break;
  194. }
  195. }
  196. void region_select_load(void) {
  197. pdex_vram_setup();
  198. pdex_alloc_memory();
  199. region_select_load_gfx();
  200. task_add(region_loop, 0);
  201. set_callback2(pdex_cb_handler);
  202. }