No Description

pokedex_region.c 8.0KB

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