No Description

pokedex_region.c 7.6KB

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