Advertisement
colonelsalt

Documented bigmap.c

Jun 9th, 2021 (edited)
1,005
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.27 KB | None | 0 0
  1. #include <tonc.h>
  2. #include "map.h"
  3. #include "maps/bigmap.h"
  4. #include "assets/pokemonMap.h"
  5. #include "assets/Pal0.h"
  6. #include "assets/Pal1.h"
  7. #include "assets/Pal2.h"
  8. #include "assets/Pal3.h"
  9. #include "assets/Pal4.h"
  10. #include "assets/Pal5.h"
  11. #include "assets/Pal6.h"
  12.  
  13. static void append_map_column(int src_tile_x, int src_tile_y);
  14. static void append_map_row(int src_tile_x, int src_tile_y);
  15.  
  16. Viewport g_viewport;
  17. TMap g_map;
  18.  
  19. void init_map(POINT16 start_pos) {
  20.     g_map.bgNum = 0;
  21.     g_map.srcMap = (SCR_ENTRY*) BG_0;
  22.     g_map.dstMap = se_mem[31]; // One 256x256p map takes up a single screenblock
  23.     g_map.srcMapWidth = BIGMAP_WIDTH;
  24.     g_map.srcMapHeight = BIGMAP_HEIGHT;
  25.    
  26.     // Copy base tiles to charblock 0
  27.     GRIT_CPY(tile_mem[0], pokemonMapTiles);
  28.  
  29.     centre_viewport(start_pos);
  30.     g_viewport.prevOffset = g_viewport.offset;
  31.  
  32.     // Tile coordinates (in 128x128t srcMap space) of the top left corner of the viewport
  33.     int src_y = g_viewport.offset.y / 8;
  34.     int src_x = g_viewport.offset.x / 8;
  35.  
  36.     // Copy first 32x32 tiles around the start pos. from srcMap into dstMap (se_mem)
  37.     for (int y = 0; y < 32; y++) {
  38.         append_map_row(src_x, src_y);
  39.         src_y++;
  40.     }
  41.  
  42.     // Copy palettes to correct banks in BG pallete memory
  43.     GRIT_CPY(pal_bg_bank[0], Pal0Pal);
  44.     GRIT_CPY(pal_bg_bank[1], Pal1Pal);
  45.     GRIT_CPY(pal_bg_bank[2], Pal2Pal);
  46.     GRIT_CPY(pal_bg_bank[3], Pal3Pal);
  47.     GRIT_CPY(pal_bg_bank[4], Pal4Pal);
  48.     GRIT_CPY(pal_bg_bank[5], Pal5Pal);
  49.     GRIT_CPY(pal_bg_bank[6], Pal6Pal);
  50.  
  51.     REG_BGCNT[g_map.bgNum] = BG_CBB(0) | BG_SBB(31) | BG_4BPP | BG_REG_32x32;
  52. }
  53.  
  54. void centre_viewport(POINT16 pt) { 
  55.     g_viewport.offset.x = clamp(pt.x - (SCREEN_WIDTH / 2), 0, 1024 - SCREEN_WIDTH);
  56.     g_viewport.offset.y = clamp(pt.y - (SCREEN_HEIGHT / 2), 0, 1024 - SCREEN_HEIGHT);
  57. }
  58.  
  59. /*
  60.     Check whether the viewport has shifted to another tile (up/down/left/right) this frame.
  61.     If it has, copy one 32t row/column of srcMap over to dstMap to give the appearance of a
  62.     larger continuously scrolling map.
  63.  
  64.     Since regular backgrounds wrap around by default, REG_BG_OFS (the viewport offset) is modulo
  65.     256x256p (dimensions of dstMap). We can thus write the newly exposed row/column from srcMap
  66.     to the row/column in 32x32t dstMap space that is just outside the dimensions of the
  67.     viewport (in the direction of the shift).
  68.  
  69.     The result is that the viewport will always show a 240x160p slice of dstMap that corresponds
  70.     to the "camera's position" in srcMap (centred around the player's position).
  71. */
  72. void update_map() {
  73.     // Tile coordinates of the viewport in srcMap
  74.     int prev_tile_x = g_viewport.prevOffset.x / 8;
  75.     int prev_tile_y = g_viewport.prevOffset.y / 8;
  76.     int curr_tile_x = g_viewport.offset.x / 8;
  77.     int curr_tile_y = g_viewport.offset.y / 8;
  78.  
  79.     if (curr_tile_x < prev_tile_x) {
  80.         append_map_column(curr_tile_x, curr_tile_y); // Append from left side of srcMap
  81.     } else if (curr_tile_x > prev_tile_x) {
  82.         append_map_column(curr_tile_x + 31, curr_tile_y); // Append from right side of srcMap
  83.     }
  84.  
  85.     if (curr_tile_y < prev_tile_y) {
  86.         append_map_row(curr_tile_x, curr_tile_y); // Append from top row of srcMap
  87.     } else if (curr_tile_y > prev_tile_y) {
  88.         append_map_row(curr_tile_x, curr_tile_y + 31); // Append from bottom row of srcMap
  89.     }
  90.  
  91.     g_viewport.prevOffset = g_viewport.offset;
  92.     REG_BG_OFS[g_map.bgNum] = g_viewport.offset;
  93. }
  94.  
  95. /*
  96.     Write one 32t column of SCR_ENTRIES from srcMap to dstMap.
  97.     Called when the viewport has moved to a new tile coordinate
  98.     towards the left or right this frame.
  99.  
  100.     Parameters: The tile coordinates (in range [srcWidth, srcHeight]) of either
  101.         a) the top left corner, or
  102.         b) the top right corner
  103.     of the viewport window.
  104.  
  105.     The column is retrieved from srcMap tile coords
  106.     (x = src_tile_x, y in [src_tile_y, src_tile_y + 31]).
  107.  
  108.     It's copied over to
  109.     (x = dst_tile_x, y in [0, 31]).
  110. */
  111. static void append_map_column(int src_tile_x, int src_tile_y) {
  112.     // Find a corresponding column in the 32x32t space of dstMap
  113.     int dst_tile_x = src_tile_x % 32;
  114.     int dst_tile_y = src_tile_y % 32;
  115.  
  116.     // SCR_ENTRY of the newly exposed tile (top left/right of viewport) in srcMap
  117.     SCR_ENTRY* src_entry = &g_map.srcMap[src_tile_y * g_map.srcMapWidth + src_tile_x];
  118.  
  119.     // SCR_ENTRY in dstMap (left/rightmost edge of the viewport offset) to write the new tile to
  120.     SCR_ENTRY* dst_entry = &g_map.dstMap[dst_tile_y * 32 + dst_tile_x];
  121.  
  122.     // Copy over every tile to dstMap for y range [dst_tile_y, dst_tile_y + 31]
  123.     for(int y = dst_tile_y; y < 32; y++) {
  124.         // Write a single tile
  125.         *dst_entry= *src_entry;
  126.  
  127.         // Move dst_entry pointer down one tile (dst_tile_y++)
  128.         dst_entry += 32;
  129.         // Move src_entry pointer down one tile (src_tile_y++)
  130.         src_entry += g_map.srcMapWidth;
  131.     }
  132.  
  133.     // Move dst_entry pointer up to y = 0
  134.     dst_entry -= 1024;
  135.  
  136.     // Copy over every tile to dstMap for y range [0, dst_tile_y - 1]
  137.     for(int y = 0; y < dst_tile_y; y++) {
  138.         *dst_entry= *src_entry;
  139.  
  140.         dst_entry += 32;
  141.         src_entry += g_map.srcMapWidth;
  142.     }
  143. }
  144.  
  145. /*
  146.     Write one 32t row of SCR_ENTRIES from srcMap to dstMap.
  147.     Called when the viewport has moved up or down one tile coordinate
  148.     this frame.
  149.  
  150.     Parameters: The tile coordinates (in range [srcWidth, srcHeight]) of either
  151.         a) the top left corner, or
  152.         b) the bottom left corner
  153.     of the viewport window.
  154.  
  155.     The row is retrieved from srcMap tile coords
  156.     (x in [src_tile_x, src_tile_x + 31], y = src_tile_y).
  157.  
  158.     It's copied over to
  159.     (x in [0, 31], y = dst_tile_y).
  160. */
  161. static void append_map_row(int src_tile_x, int src_tile_y) {
  162.     // Find a corresponding row in the 32x32t space of dstMap
  163.     int dst_tile_x = src_tile_x % 32;
  164.     int dst_tile_y = src_tile_y % 32;
  165.  
  166.     // SCR_ENTRY of the newly exposed tile (top/bottom left of viewport) in srcMap
  167.     SCR_ENTRY* src_entry = &g_map.srcMap[src_tile_y * g_map.srcMapWidth + src_tile_x];
  168.  
  169.     // SCR_ENTRY in dstMap (top/bottom edge of the viewport offset) to write the new tile to
  170.     SCR_ENTRY* dst_entry = &g_map.dstMap[dst_tile_y * 32 + dst_tile_x];
  171.  
  172.     // Write every tile to dstMap from x in [dst_tile_x, 31]
  173.     for(int x = dst_tile_x; x < 32; x++) {
  174.         *dst_entry = *src_entry;
  175.  
  176.         dst_entry++;
  177.         src_entry++;
  178.     }
  179.  
  180.     // Move dst_entry back to x = 0
  181.     dst_entry -= 32;
  182.  
  183.     // Write every tile to dstMap from x in [0, dst_tile_x - 1]
  184.     for(int x = 0; x < dst_tile_x; x++) {
  185.         *dst_entry = *src_entry;
  186.  
  187.         dst_entry++;
  188.         src_entry++;
  189.     }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement