Няма описание

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. #include <agb_debug.h>
  2. #include <pokeagb/pokeagb.h>
  3. #include <pokedex/pdexArrowRotated.h>
  4. #include <pokedex/pdexDetailBg.h>
  5. #include "pokedex_common.h"
  6. #define TB_PKNAME 0
  7. #define TB_PKSIZE 1
  8. #define TB_DESC 2
  9. #define TB_PKNAME_W 30
  10. #define TB_SW_W 12
  11. extern pchar pdex_entry_debug[];
  12. extern pchar pdex_str_size[];
  13. extern pchar pdex_str_weight[];
  14. extern pchar pdex_str_size_unit[];
  15. extern pchar pdex_str_weight_unit[];
  16. extern pchar pdex_str_comma[];
  17. struct TextboxTemplate dexdetail_boxes[] = {
  18. {.bg_id = 0, .x = 0, .y = 0, .width = TB_PKNAME_W, .height = 4, .pal_id = 15, .charbase = 1},
  19. {.bg_id = 0, .x = 17, .y = 5, .width = TB_SW_W, .height = 4, .pal_id = 15, .charbase = 121},
  20. {.bg_id = 1, .x = 1, .y = 0, .width = 28, .height = 20, .pal_id = 15, .charbase = 161},
  21. {.bg_id = 0xFF},
  22. };
  23. u8 dexdetail_type_to_oam_type[18] = {0, 9, 8, 12, 6, 5, 11, 10, 14, 17, 1, 2, 3, 4, 13, 7, 16, 15};
  24. #define O_TYPE(t) (dexdetail_type_to_oam_type[t])
  25. struct OamData dexdetail_type_oam = {
  26. .affine_mode = 0,
  27. .obj_mode = 0,
  28. .mosaic = false,
  29. .shape = 1,
  30. .size = 2,
  31. };
  32. void dexdetail_update_type_oam(u8 type, struct Object *obj) {
  33. memcpy((void *)((0x06010000) + (obj->final_oam.tile_num * 0x20)),
  34. pokemon_type_chart_gfx + (128 * O_TYPE(type)) + (512 * (O_TYPE(type) >> 2)), 128);
  35. memcpy((void *)((0x06010000) + 128 + (obj->final_oam.tile_num * 0x20)),
  36. pokemon_type_chart_gfx + 512 + (128 * O_TYPE(type)) + (512 * (O_TYPE(type) >> 2)), 128);
  37. }
  38. s8 dexdetail_load_type_oam(u8 type, u16 tag) {
  39. struct SpriteTiles typeTiles = {(&pokemon_type_chart_gfx[0]) + 256 * O_TYPE(type), 256, tag};
  40. gpu_tile_obj_alloc_tag_and_upload(&typeTiles);
  41. const struct Template typeTemplate = {
  42. .tiles_tag = tag,
  43. .pal_tag = DEX_DETAIL_TYPEPAL,
  44. .oam = &dexdetail_type_oam,
  45. .animation = &anim_image_empty,
  46. .graphics = &typeTiles,
  47. .rotscale = &rotscale_empty,
  48. .callback = oac_nullsub,
  49. };
  50. s8 id = (s8)template_instanciate_forward_search(&typeTemplate, 100, 100, 0);
  51. dexdetail_update_type_oam(type, &objects[id]);
  52. return id;
  53. }
  54. extern pchar pdex_str_form_alola[];
  55. void dexdetail_load_pokemon(u16 dexindex, enum DexFormType formType) {
  56. u16 species = pdex_lazy_lookup_entry(dexindex)->species;
  57. rboxid_clear_pixels(TB_PKNAME, 0);
  58. rboxid_clear_pixels(TB_PKSIZE, 0);
  59. rboxid_clear_pixels(TB_DESC, 0);
  60. pstrcpy(string_buffer, &pokemon_names[species][0]);
  61. switch (formType) {
  62. case ALOLA:
  63. pstrcat(string_buffer, pdex_str_form_alola);
  64. break;
  65. default:
  66. break;
  67. }
  68. u16 twidth = font_get_width_of_string(FONT_DEX_STD, string_buffer, 0x0000);
  69. rboxid_print(TB_PKNAME, FONT_DEX_STD, TB_STD_CENTER(twidth, 8 * TB_PKNAME_W), 0, &pdex_text_color, 0,
  70. string_buffer);
  71. pchar *strType = &pokedex_data[dexindex].category_name[0];
  72. u16 typeTwidth = font_get_width_of_string(FONT_DEX_STD, strType, 0x0000);
  73. rboxid_print(TB_PKNAME, FONT_DEX_STD, TB_STD_CENTER(typeTwidth, 88) + 110, 14, &pdex_text_color, 0, strType);
  74. rboxid_print(TB_PKSIZE, FONT_DEX_STD, 4, 1, &pdex_text_color, 0, pdex_str_size);
  75. rboxid_print(TB_PKSIZE, FONT_DEX_STD, 4, 14, &pdex_text_color, 0, pdex_str_weight);
  76. u16 weightNumber = pokedex_data[dexindex].weight / 10;
  77. u16 weightDecimal = pokedex_data[dexindex].weight % 10;
  78. pchar buffer[20];
  79. fmt_int_10(string_buffer, weightNumber, 0, 3);
  80. fmt_int_10(&buffer[0], weightDecimal, 0, 1);
  81. pstrcat(string_buffer, pdex_str_comma);
  82. pstrcat(string_buffer, buffer);
  83. pstrcat(string_buffer, pdex_str_weight_unit);
  84. u16 weightTwidth = font_get_width_of_string(FONT_DEX_STD, string_buffer, 0x0000);
  85. rboxid_print(TB_PKSIZE, FONT_DEX_STD, TB_STD_RIGHT(weightTwidth, TB_SW_W * 8) - 6, 1, &pdex_text_color, 0,
  86. string_buffer);
  87. u16 sizeNumber = pokedex_data[dexindex].height / 10;
  88. u16 sizeDecimal = pokedex_data[dexindex].height % 10;
  89. fmt_int_10(string_buffer, sizeNumber, 0, 3);
  90. fmt_int_10(&buffer[0], sizeDecimal, 0, 1);
  91. pstrcat(string_buffer, pdex_str_comma);
  92. pstrcat(string_buffer, buffer);
  93. pstrcat(string_buffer, pdex_str_size_unit);
  94. rboxid_print(TB_PKSIZE, FONT_DEX_STD, TB_STD_RIGHT(weightTwidth, TB_SW_W * 8) - 6, 14, &pdex_text_color, 0,
  95. string_buffer);
  96. rboxid_print(TB_DESC, FONT_DEX_STD, 3, 2, &pdex_text_color, 0, pokedex_data[dexindex].description1);
  97. /* load the species sprite */
  98. if (pokedex_context->detail_pokemon_oam != -1) {
  99. lz77UnCompVram(
  100. pokemon_graphics_front[species].data,
  101. ((void *)(objects[pokedex_context->detail_pokemon_oam].final_oam.tile_num * 32) + ADDR_VRAM + 0x10000));
  102. gpu_pal_apply_compressed(pokemon_palette_normal[species].data,
  103. 16 * (objects[pokedex_context->detail_pokemon_oam].final_oam.palette_num + 16), 32);
  104. } else {
  105. struct SpriteTiles pkmnTiles = {pokemon_graphics_front[species].data, 2048, DEX_PKMN_TAG};
  106. struct SpritePalette pkmnPal = {pokemon_palette_normal[species].data, DEX_PKMN_TAG};
  107. const struct Template pkmnTemplate = {
  108. .tiles_tag = DEX_PKMN_TAG,
  109. .pal_tag = DEX_PKMN_TAG,
  110. .oam = &pdex_oam_pkmn,
  111. .animation = &anim_image_empty,
  112. .graphics = &pkmnTiles,
  113. .rotscale = &rotscale_empty,
  114. .callback = oac_nullsub,
  115. };
  116. gpu_tile_obj_decompress_alloc_tag_and_upload(&pkmnTiles);
  117. gpu_pal_decompress_alloc_tag_and_upload(&pkmnPal);
  118. pokedex_context->detail_pokemon_oam = (s8)template_instanciate_forward_search(&pkmnTemplate, 10, 10, 0);
  119. objects[pokedex_context->detail_pokemon_oam].pos1.x = 55;
  120. objects[pokedex_context->detail_pokemon_oam].pos1.y = 55;
  121. }
  122. // draw the first type icon
  123. if (pokedex_context->detail_type_oam[0] != -1) {
  124. dexdetail_update_type_oam(pokemon_base_stats[species].type[0], &objects[pokedex_context->detail_type_oam[0]]);
  125. } else {
  126. pokedex_context->detail_type_oam[0] =
  127. dexdetail_load_type_oam(pokemon_base_stats[species].type[0], DEX_DETAIL_TYPE1);
  128. objects[pokedex_context->detail_type_oam[0]].pos1.x = 156;
  129. objects[pokedex_context->detail_type_oam[0]].pos1.y = 80;
  130. }
  131. // draw the second type icon eventually
  132. if (pokemon_base_stats[species].type[0] != pokemon_base_stats[species].type[1]) {
  133. if (pokedex_context->detail_type_oam[1] != -1) {
  134. dexdetail_update_type_oam(pokemon_base_stats[species].type[1],
  135. &objects[pokedex_context->detail_type_oam[1]]);
  136. } else {
  137. pokedex_context->detail_type_oam[1] =
  138. dexdetail_load_type_oam(pokemon_base_stats[species].type[1], DEX_DETAIL_TYPE2);
  139. objects[pokedex_context->detail_type_oam[1]].pos1.x = 210;
  140. objects[pokedex_context->detail_type_oam[1]].pos1.y = 80;
  141. }
  142. OBJID_SHOW(pokedex_context->detail_type_oam[1]);
  143. } else {
  144. OBJID_HIDE(pokedex_context->detail_type_oam[1]);
  145. }
  146. rboxid_update_tilemap_and_tileset(TB_DESC);
  147. rboxid_update_tilemap_and_tileset(TB_PKNAME);
  148. rboxid_update_tilemap_and_tileset(TB_PKSIZE);
  149. }
  150. void dexdetail_update_form(u16 form) {
  151. REG_DISPCNT &= ~((1 << 8) | (1 << 9));
  152. if (form == 0) {
  153. dexdetail_load_pokemon(pokedex_context->cursor_position_top + pokedex_context->cursor_position_internal,
  154. NORMAL);
  155. } else {
  156. form--;
  157. if (pokedex_context->detail_forms[form].index > PDEX_LAST_SHOWN) {
  158. dexdetail_load_pokemon(pokedex_context->detail_forms[form].index, pokedex_context->detail_forms[form].type);
  159. } else {
  160. // update the oam only
  161. u16 species = pokedex_context->detail_forms[form].species;
  162. lz77UnCompVram(
  163. pokemon_graphics_front[species].data,
  164. ((void *)(objects[pokedex_context->detail_pokemon_oam].final_oam.tile_num * 32) + ADDR_VRAM + 0x10000));
  165. gpu_pal_apply_compressed(pokemon_palette_normal[species].data,
  166. 16 * (objects[pokedex_context->detail_pokemon_oam].final_oam.palette_num + 16),
  167. 32);
  168. }
  169. }
  170. REG_DISPCNT |= ((1 << 8) | (1 << 9));
  171. }
  172. struct DexFormEntry *dexdetail_get_forms(u16 index) {
  173. u16 current = 0;
  174. while (pdex_forms[current].index != 0xFFFF) {
  175. if (pdex_forms[current].index == index)
  176. return &pdex_forms[current].forms[0];
  177. current++;
  178. }
  179. return NULL;
  180. }
  181. void dexdetail_oac_arrow(struct Object *obj) {
  182. if (pokedex_context->detail_forms != NULL) {
  183. if (obj->priv[0]) {
  184. // this is the left arrow
  185. if (pokedex_context->detail_form_position != 0) {
  186. obj->final_oam.palette_num = obj->priv[1];
  187. } else {
  188. obj->final_oam.palette_num = obj->priv[2];
  189. }
  190. } else {
  191. // this is the right arrow
  192. if (pokedex_context->detail_forms[pokedex_context->detail_form_position].type != FORM_END) {
  193. obj->final_oam.palette_num = obj->priv[1];
  194. } else {
  195. obj->final_oam.palette_num = obj->priv[2];
  196. }
  197. }
  198. }
  199. }
  200. struct SpriteTiles pdex_arrow_rot_tiles = {pdexArrowRotatedTiles, 256, DEX_ARROW_TAG};
  201. struct SpritePalette pdex_arrow_rot_pal = {pdexArrowRotatedPal, DEX_ARROW_TAG};
  202. struct SpritePalette pdex_arrow_rot_pal_gray = {pdexArrowRotatedPal, DEX_ARROW_TAG_EPAL};
  203. const struct OamData pdex_arrow_rot_oam = {
  204. .affine_mode = 0,
  205. .obj_mode = 0,
  206. .mosaic = false,
  207. .shape = 0,
  208. .size = 1,
  209. };
  210. const struct Template pdex_arrow_rot_template = {
  211. .tiles_tag = DEX_ARROW_TAG,
  212. .pal_tag = DEX_ARROW_TAG,
  213. .oam = &pdex_arrow_rot_oam,
  214. .animation = &anim_image_empty,
  215. .graphics = &pdex_arrow_rot_tiles,
  216. .rotscale = &rotscale_empty,
  217. .callback = dexdetail_oac_arrow,
  218. };
  219. void dexdetail_load_form_arrows(void) {
  220. gpu_tile_obj_decompress_alloc_tag_and_upload(&pdex_arrow_rot_tiles);
  221. u8 normalPal = gpu_pal_obj_alloc_tag_and_apply(&pdex_arrow_rot_pal);
  222. u8 grayPal = gpu_pal_obj_alloc_tag_and_apply(&pdex_arrow_rot_pal_gray);
  223. tint_palette_gray(&palette_bg_unfaded[16 * (16 + grayPal)], 16);
  224. u8 leftArrow = template_instanciate_forward_search(&pdex_arrow_rot_template, 10, 55, 0);
  225. u8 rightArrow = template_instanciate_forward_search(&pdex_arrow_rot_template, 100, 55, 0);
  226. objects[leftArrow].priv[0] = true;
  227. objects[leftArrow].final_oam.h_flip = true;
  228. objects[leftArrow].priv[1] = normalPal;
  229. objects[leftArrow].priv[2] = grayPal;
  230. objects[rightArrow].priv[0] = false;
  231. objects[rightArrow].priv[1] = normalPal;
  232. objects[rightArrow].priv[2] = grayPal;
  233. pokedex_context->detail_arrows_oam[0] = leftArrow;
  234. pokedex_context->detail_arrows_oam[1] = rightArrow;
  235. }
  236. void dexdetail_loop(u8 tid) {
  237. u16 currentIndex = pokedex_context->cursor_position_top + pokedex_context->cursor_position_internal;
  238. switch (pokedex_context->state) {
  239. case 0:
  240. bgid_send_tilemap(2);
  241. dexdetail_load_form_arrows();
  242. dexdetail_load_pokemon(currentIndex, NORMAL);
  243. pokedex_context->detail_form_position = 0;
  244. struct DexFormEntry *forms = dexdetail_get_forms(currentIndex);
  245. if (forms != NULL) {
  246. OBJID_SHOW(pokedex_context->detail_arrows_oam[0]);
  247. OBJID_SHOW(pokedex_context->detail_arrows_oam[1]);
  248. } else {
  249. OBJID_HIDE(pokedex_context->detail_arrows_oam[0]);
  250. OBJID_HIDE(pokedex_context->detail_arrows_oam[1]);
  251. }
  252. dprintf("test: %d", pokedex_index_to_species(1000));
  253. pokedex_context->detail_forms = forms;
  254. palette_bg_faded_fill_black();
  255. pokedex_context->state++;
  256. break;
  257. case 1:
  258. gpu_sync_bg_show(0);
  259. gpu_sync_bg_show(1);
  260. gpu_sync_bg_hide(3);
  261. gpu_sync_bg_show(2);
  262. fade_screen(0xFFFFFFFF, PDEX_FADEIN_SPD, 16, 0, 0x0000);
  263. pokedex_context->state++;
  264. break;
  265. case 2:
  266. if (!pal_fade_control.active) {
  267. pokecry_play(pdex_lazy_lookup_entry(currentIndex)->species, 0);
  268. pokedex_context->state++;
  269. }
  270. break;
  271. case 3:
  272. switch (super.buttons_new) {
  273. case KEY_B:
  274. pokedex_context->state = 12;
  275. u16 dexindex = pokedex_context->cursor_position_top + pokedex_context->cursor_position_internal;
  276. if (dexindex <= PDEX_LAST_SHOWN - 7) {
  277. pokedex_context->cursor_position_top = dexindex;
  278. pokedex_context->cursor_position_internal = 0;
  279. } else {
  280. pokedex_context->cursor_position_top = PDEX_LAST_SHOWN - 7;
  281. pokedex_context->cursor_position_internal = (dexindex - pokedex_context->cursor_position_top);
  282. }
  283. m4aSongNumStart(601);
  284. fade_screen(0xFFFFFFFF, PDEX_FADEIN_SPD, 0, 16, 0x0000);
  285. break;
  286. case KEY_LEFT:
  287. if (pokedex_context->detail_form_position != 0) {
  288. pokedex_context->detail_form_position--;
  289. dexdetail_update_form(pokedex_context->detail_form_position);
  290. }
  291. break;
  292. case KEY_RIGHT:
  293. if (pokedex_context->detail_forms[pokedex_context->detail_form_position].type != FORM_END) {
  294. pokedex_context->detail_form_position++;
  295. dexdetail_update_form(pokedex_context->detail_form_position);
  296. }
  297. break;
  298. case KEY_START:
  299. pokecry_play(pdex_lazy_lookup_entry(currentIndex)->species, 0);
  300. }
  301. break;
  302. case 12:
  303. if (!pal_fade_control.active) {
  304. task_del(tid);
  305. pdex_vram_free_bgmaps();
  306. set_callback2(pdex_load);
  307. }
  308. break;
  309. default:
  310. break;
  311. }
  312. }
  313. struct SpritePalette dexdetail_type_pal = {&pokemon_type_chart_pal[0], DEX_DETAIL_TYPEPAL};
  314. void dexdetail_load_gfx(void) {
  315. rbox_init_from_templates(&dexdetail_boxes[0]);
  316. lz77UnCompVram(pdexDetailBgTiles, (void *)0x0600C000);
  317. LZ77UnCompWram(pdexDetailBgMap, bgid_get_tilemap(2));
  318. gpu_pal_apply_compressed(pdexDetailBgPal, 0, 32);
  319. gpu_pal_apply(pdex_text_pal, 15 * 16, 32);
  320. gpu_pal_obj_alloc_tag_and_apply(&dexdetail_type_pal);
  321. lcd_io_set(REG_ID_WIN0H, (8 << 8) | (232));
  322. lcd_io_set(REG_ID_WIN0V, ((100 << 8) | (160)));
  323. lcd_io_set(REG_ID_WININ, WIN_BG0 | WIN_BG1 | WIN_BG2 | WIN_BG3 | WIN_OBJ);
  324. lcd_io_set(REG_ID_WINOUT, WIN_BG0 | WIN_BG2 | WIN_BG3 | WIN_OBJ);
  325. bgid_mod_y_offset(1, -24576, 1);
  326. bgid_mark_for_sync(0);
  327. }
  328. void dexdetail_load(void) {
  329. pdex_vram_setup();
  330. dexdetail_load_gfx();
  331. pokedex_context->state = 0;
  332. pokedex_context->detail_pokemon_oam = -1;
  333. pokedex_context->detail_type_oam[0] = -1;
  334. pokedex_context->detail_type_oam[1] = -1;
  335. task_add(dexdetail_loop, 0);
  336. set_callback2(pdex_cb_handler);
  337. }