git.y1.nz

gbdk-2020

GameBoy Development Kit
download: https://git.y1.nz/archives/gbdk.tar.gz
README | Files | Log | Refs | LICENSE

gbdk-lib/examples/cross-platform/rle_map/src/main.c

      1 #include <gbdk/platform.h>
      2 #include <gbdk/incbin.h>
      3 #include <stdbool.h>
      4 
      5 #include <gbdk/rledecompress.h>
      6 
      7 #include <map.h>
      8 
      9 
     10 // The Tile data is not compressed
     11 INCBIN(map_tiles, "obj/all/map_tiles.bin")
     12 // INCBIN(map_tiles, INCPATH_PLAT)
     13 INCBIN_EXTERN(map_tiles)
     14 
     15 // The Map data is compressed using: gbcompress --alg=rle
     16 // The map is ordered in sequential columns 18 tiles high
     17 // so it's easy to load one entire screen column worth at a time.
     18 #define MAP_DATA_HEIGHT (map_HEIGHT / map_TILE_H)
     19 INCBIN(map_compressed, "obj/all/map_map.bin.rle")
     20 INCBIN_EXTERN(map_compressed)
     21 
     22 uint8_t data[MAP_DATA_HEIGHT];  // Collision map buffer
     23 uint16_t scrollpos = 0;             // Scroll position in pixels
     24 uint8_t datapos = 0;                // x position in tiles inside the collision map
     25 
     26 void main(void) {
     27     if(DEVICE_SCREEN_BUFFER_WIDTH == DEVICE_SCREEN_WIDTH)
     28     {
     29         // On platforms where screen buffer has no more space than physical screen,
     30         // the next map column will be written to the leftmost screen column.
     31         // So we blank the leftmost column to hide visual artifacts where possible.
     32         HIDE_LEFT_COLUMN;
     33     }
     34     SHOW_BKG;
     35 
     36     // Load Tile data
     37     set_bkg_data(0, INCBIN_SIZE(map_tiles) >> 4, map_tiles);
     38 
     39     // Initialize decompressor with Map data
     40     rle_init(map_compressed);
     41 
     42     // To get started, decompress and display a whole screen worth,
     43     // + 1 additional column in the direction being scrolled (to
     44     // avoid screen tearing from drawing a column right as it scrolls into view)
     45     for (uint8_t i = 0; (i != DEVICE_SCREEN_WIDTH + 1); i++) {
     46         rle_decompress(data, MAP_DATA_HEIGHT);
     47         // pre-process column here
     48         // ...
     49 
     50         // Draw current column starting at row 0
     51         set_bkg_tiles(i & (DEVICE_SCREEN_BUFFER_WIDTH-1), 0, 1, MAP_DATA_HEIGHT, data);
     52     }
     53 
     54     // main game loop
     55     datapos = 0;
     56     scrollpos = 1;
     57     while(TRUE) {
     58 
     59         vsync();
     60 
     61         // Scroll one pixel to the right
     62         // (This should be done as close to vsync and before
     63         // the map drawing below to avoid tearing at the top of the screen)
     64         scrollpos++;
     65         move_bkg(scrollpos, 0);
     66 
     67         // Once for every 8 pixels scrolled: draw a
     68         // new column of tiles on the right-most edge
     69         if ((scrollpos & 0x07u) == 0) {
     70 
     71             // Get hardware map tile X column from scroll position / 8
     72             // Then + 1 since there's a 1 tile column buffer on the right edge
     73             datapos = (scrollpos >> 3);
     74             uint8_t map_x_column = (datapos + DEVICE_SCREEN_WIDTH) & (DEVICE_SCREEN_BUFFER_WIDTH-1);
     75 
     76             // Decompress a column worth of data
     77             // If the end of compressed data is reached, reset decompression
     78             // and resume at the start (to make infinite scrolling)
     79             if (!rle_decompress(data, MAP_DATA_HEIGHT)) {
     80                 rle_init(map_compressed);
     81                 rle_decompress(data, MAP_DATA_HEIGHT);
     82             }
     83             // pre-process column here
     84             // ...
     85 
     86             // Display column
     87             set_bkg_tiles(map_x_column, 0, 1, MAP_DATA_HEIGHT, data);
     88         }
     89     }
     90 }

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.