No Description

dns.c 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include <pokeagb/pokeagb.h>
  2. #include <agb_debug.h>
  3. #include <dns.h>
  4. #include <rtc.h>
  5. #define DNS_BUF_SIZE 256
  6. #define DNS_SEC_PAL_START 224
  7. #define DNS_SEC_PAL_START_C (DNS_SEC_PAL_START / 2)
  8. #define crgb5(r,g,b) (union Color)(((u16)(((b) << 10) | ((g)<<5) | ((r)))))
  9. #define rgb82rgb5(r,g,b) (crgb5(r/8, g/8, b/8))
  10. const union Color dns_color_times[5] = {
  11. rgb82rgb5(70, 70, 196),
  12. rgb82rgb5(70, 70, 196),
  13. rgb82rgb5(70, 70, 196),
  14. rgb82rgb5(70, 70, 196),
  15. rgb82rgb5(70, 70, 196)
  16. };
  17. union Color dns_color_multiply(union Color a, union Color b);
  18. void dns_blockset_load_palette(struct MapBlockset* blockset, u16 offset, u16 size){
  19. struct RtcTimestamp stamp;
  20. rtc_get_time(&stamp);
  21. dprintf("rtc data was read: %x.%x.%x\n", stamp.day, stamp.month, stamp.year);
  22. if(blockset != NULL){
  23. union Color *dns_buffer = malloc(DNS_BUF_SIZE*2);
  24. if(blockset->secondary > 0)
  25. {
  26. if(blockset->secondary == 1)
  27. {
  28. //load secondary palette uncompressed
  29. memcpy(dns_buffer, blockset->palettes+DNS_SEC_PAL_START, size);
  30. dns_modify_palette(dns_buffer, size/2);
  31. gpu_pal_apply(dns_buffer, offset,size);
  32. load_palette3(offset, size/2);
  33. }
  34. else
  35. {
  36. //load secondary palette compressed
  37. LZ77UnCompWram(blockset->palettes, dns_buffer);
  38. dns_modify_palette(&dns_buffer[DNS_SEC_PAL_START_C], size/2);
  39. gpu_pal_apply(&dns_buffer[DNS_SEC_PAL_START_C], offset, size);
  40. load_palette3(offset,size/2);
  41. }
  42. }
  43. else
  44. {
  45. union Color black;
  46. black.packed = 0x0000;
  47. gpu_pal_apply(&black, 0, 2);
  48. memcpy(dns_buffer, blockset->palettes, size);
  49. dns_modify_palette(dns_buffer, size/2);
  50. gpu_pal_apply(&dns_buffer[1], offset+1, size-2);
  51. load_palette3(offset+1, (size-2)/2);
  52. //load primary palette
  53. }
  54. free(dns_buffer);
  55. }
  56. }
  57. void dns_oec01_load_pal_impl(u32 *oe_script) {
  58. struct SpritePalette *pal = (struct SpritePalette *)oe_read_word(oe_script);
  59. struct SpritePalette palToApply = {
  60. .data = NULL,
  61. .tag = pal->tag
  62. };
  63. u8 idx = gpu_pal_tags_index_of(palToApply.tag);
  64. union Color *buffer = malloc(32);
  65. memcpy(buffer, pal->data, 32);
  66. dns_modify_palette(buffer, 16);
  67. palToApply.data = buffer;
  68. gpu_pal_obj_alloc_tag_and_apply(&palToApply);
  69. if(idx == 0xFF) {
  70. u8 idx = gpu_pal_tags_index_of(palToApply.tag);
  71. tint_palette_switch(idx);
  72. }
  73. idx = gpu_pal_tags_index_of(palToApply.tag);
  74. free(buffer);
  75. palette_obj_807AA8C(idx);
  76. *oe_script += 4;
  77. }
  78. void dns_oec02_load_pal_impl(u32 *oe_script) {
  79. struct SpritePalette *pal = (struct SpritePalette *)oe_read_word(oe_script);
  80. struct SpritePalette palToApply = {
  81. .data = NULL,
  82. .tag = pal->tag
  83. };
  84. u8 idx = gpu_pal_tags_index_of(palToApply.tag);
  85. union Color *buffer = malloc(32);
  86. memcpy(buffer, pal->data, 32);
  87. dns_modify_palette(buffer, 16);
  88. palToApply.data = buffer;
  89. gpu_pal_obj_alloc_tag_and_apply(&palToApply);
  90. if(idx == 0xFF) {
  91. u8 idx = gpu_pal_tags_index_of(palToApply.tag);
  92. tint_palette_switch(idx);
  93. }
  94. free(buffer);
  95. *oe_script += 4;
  96. }
  97. void dns_pal_patch_for_npc(u16 tag, u8 idx)
  98. {
  99. extern struct SpritePalette ow_pal_table[1100];
  100. u16 npcPalIdx = hacked_npc_pal_idx_for_given_tag(tag);
  101. union Color *buffer = malloc(32);
  102. memcpy(buffer, ow_pal_table[npcPalIdx].data,32);
  103. dns_modify_palette(buffer,16);
  104. gpu_pal_apply(buffer, 256 + 16*idx, 32);
  105. free(buffer);
  106. tint_palette_switch(idx);
  107. }
  108. void dns_modify_palette(union Color* buffer, u16 size){
  109. if(current_map_header.light == 8)
  110. return;
  111. for(u16 i = 0; i < size; ++i) {
  112. buffer[i] = dns_color_multiply(dns_color_times[0], buffer[i]);
  113. }
  114. }
  115. union Color dns_color_multiply(union Color a, union Color b) {
  116. u32 red = (a.components.r * b.components.r) / 31;
  117. u32 green = (a.components.g * b.components.g) / 31;
  118. u32 blue = (a.components.b * b.components.b) / 31;
  119. return crgb5(red,green,blue);
  120. }