git.y1.nz

gbdk-2020

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

commit 08db7c125d65d45181f4cba13ec48ea9bd7ae3cd
parent e3b863a0f16a665e5b66e418e139696a7c2ae140
Author: bbbbbr <bbbbbr@users.noreply.github.com>
Date:   Tue,  1 Feb 2022 22:24:26 -0800

Merge pull request #314 from bbbbbr/examples_rle_decompress_map_new_gfx

Example: Map RLE decompress - new assets + fixes
Diffstat:
Mgbdk-lib/examples/cross-platform/rle_map/Readme.md14++++++++++++--
Agbdk-lib/examples/cross-platform/rle_map/res/asset_sources/map.bin0
Agbdk-lib/examples/cross-platform/rle_map/res/asset_sources/map.gbm0
Agbdk-lib/examples/cross-platform/rle_map/res/asset_sources/map.png0
Agbdk-lib/examples/cross-platform/rle_map/res/asset_sources/map_tiles.gbr0
Dgbdk-lib/examples/cross-platform/rle_map/res/level1_map.bin0
Dgbdk-lib/examples/cross-platform/rle_map/res/level1_map.rle0
Dgbdk-lib/examples/cross-platform/rle_map/res/level1_tiles.bin0
Agbdk-lib/examples/cross-platform/rle_map/res/map.bin.rle0
Agbdk-lib/examples/cross-platform/rle_map/res/map_tiles.bin0
Mgbdk-lib/examples/cross-platform/rle_map/src/main.c88++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------------
11 files changed, 68 insertions(+), 34 deletions(-)

diff --git a/gbdk-lib/examples/cross-platform/rle_map/Readme.md b/gbdk-lib/examples/cross-platform/rle_map/Readme.md @@ -4,6 +4,16 @@ RLE Decompress Demonstrates using rle_decompress to load a compressed tile map into vram. -The data was compressed using the `gbcompress` utility using the `--alg=rle` argument. +## Map Data encoding +The Tile Map is exported in binary format (one map entry per byte) and encoded in sequential **columns** 20 tiles high. + +The data is compressed using the `gbcompress` utility using the `--alg=rle` argument. + `../../../../bin/gbcompress --alg=rle level1_map.bin level1_map.rle` + +## Display +As the program scrolls, a new column is drawn for every 8 pixels scrolled. The Tile Map data is decoded 20 tiles at a time and rendered to the next (just barely off-screen) column on the right. + +## Graphics +The tileset graphics are by GrafxKid under CC-0 license: +https://opengameart.org/content/cave-tileset-4 -`../../../../bin/gbcompress --alg=rle level1_map.bin level1_map.rle` diff --git a/gbdk-lib/examples/cross-platform/rle_map/res/asset_sources/map.bin b/gbdk-lib/examples/cross-platform/rle_map/res/asset_sources/map.bin Binary files differ. diff --git a/gbdk-lib/examples/cross-platform/rle_map/res/asset_sources/map.gbm b/gbdk-lib/examples/cross-platform/rle_map/res/asset_sources/map.gbm Binary files differ. diff --git a/gbdk-lib/examples/cross-platform/rle_map/res/asset_sources/map.png b/gbdk-lib/examples/cross-platform/rle_map/res/asset_sources/map.png Binary files differ. diff --git a/gbdk-lib/examples/cross-platform/rle_map/res/asset_sources/map_tiles.gbr b/gbdk-lib/examples/cross-platform/rle_map/res/asset_sources/map_tiles.gbr Binary files differ. diff --git a/gbdk-lib/examples/cross-platform/rle_map/res/level1_map.bin b/gbdk-lib/examples/cross-platform/rle_map/res/level1_map.bin Binary files differ. diff --git a/gbdk-lib/examples/cross-platform/rle_map/res/level1_map.rle b/gbdk-lib/examples/cross-platform/rle_map/res/level1_map.rle Binary files differ. diff --git a/gbdk-lib/examples/cross-platform/rle_map/res/level1_tiles.bin b/gbdk-lib/examples/cross-platform/rle_map/res/level1_tiles.bin Binary files differ. diff --git a/gbdk-lib/examples/cross-platform/rle_map/res/map.bin.rle b/gbdk-lib/examples/cross-platform/rle_map/res/map.bin.rle Binary files differ. diff --git a/gbdk-lib/examples/cross-platform/rle_map/res/map_tiles.bin b/gbdk-lib/examples/cross-platform/rle_map/res/map_tiles.bin Binary files differ. diff --git a/gbdk-lib/examples/cross-platform/rle_map/src/main.c b/gbdk-lib/examples/cross-platform/rle_map/src/main.c @@ -1,60 +1,84 @@ #include <gbdk/platform.h> #include <gbdk/incbin.h> +#include <stdbool.h> #include <gbdk/rledecompress.h> -INCBIN(level1_tiles, "res/level1_tiles.bin") -INCBIN_EXTERN(level1_tiles) +// The Tile data is not compressed +INCBIN(map_tiles, "res/map_tiles.bin") +INCBIN_EXTERN(map_tiles) -INCBIN(level1_map, "res/level1_map.rle") -INCBIN_EXTERN(level1_map) +// The Map data is compressed using: gbcompress --alg=rle +// The map is ordered in sequential columns 18 tiles high +// so it's easy to load one entire screen column worth at a time. +#define MAP_DATA_HEIGHT 18 +INCBIN(map_compressed, "res/map.bin.rle") +INCBIN_EXTERN(map_compressed) extern uint8_t sample_rle_data[]; -#define DATA_HEIGHT 16 -uint8_t data[32][DATA_HEIGHT]; // collision map buffer -uint8_t scrollpos = 0; // scroll position in pixels +uint8_t data[32][MAP_DATA_HEIGHT]; // Collision map buffer +uint8_t scrollpos = 0; // Scroll position in pixels uint8_t datapos = 0; // x position in tiles inside the collision map -void main() { +void main() { + SHOW_BKG; - set_bkg_data(0, INCBIN_SIZE(level1_tiles) >> 4, level1_tiles); + // Load Tile data + set_bkg_data(0, INCBIN_SIZE(map_tiles) >> 4, map_tiles); - rle_init(level1_map); // initialize decompressor + // Initialize decompressor with Map data + rle_init(map_compressed); - // decompress and display whole screen + // To get started, decompress and display a whole screen worth, + // + 1 additional column in the direction being scrolled (to + // avoid screen tearing from drawing a column right as it scrolls into view) uint8_t * data_ptr = &data[0][0]; - for (uint8_t i = 0; (rle_decompress(data_ptr, DATA_HEIGHT)) && (i != DEVICE_SCREEN_WIDTH); i++) { + for (uint8_t i = 0; (rle_decompress(data_ptr, MAP_DATA_HEIGHT)) && (i != DEVICE_SCREEN_WIDTH + 1); i++) { // pre-process column here // ... - // display column - set_bkg_tiles(i, 2, 1, DATA_HEIGHT, data_ptr); + // Draw current column starting at row 0 + set_bkg_tiles(i, 0, 1, MAP_DATA_HEIGHT, data_ptr); } // main game loop datapos = scrollpos = 0; while(TRUE) { - if ((scrollpos & 7) == 0) { - datapos = (scrollpos >> 3); - uint8_t x_pos = (datapos + DEVICE_SCREEN_WIDTH) & 31; - // decompress and display column - data_ptr = &data[x_pos][0]; - if (rle_decompress(data_ptr, DATA_HEIGHT)) { - // pre-process column here - // ... - - // display column - set_bkg_tiles(x_pos, 2, 1, DATA_HEIGHT, data_ptr); - scrollpos++; - move_bkg(scrollpos, 0); - } - } else { - scrollpos++; - move_bkg(scrollpos, 0); - } + wait_vbl_done(); + + // Scroll one pixel to the right + // (This should be done as close to wait_vbl_done and before + // the map drawing below to avoid tearing at the top of the screen) + scrollpos++; + move_bkg(scrollpos, 0); + + // Once for every 8 pixels scrolled: draw a + // new column of tiles on the right-most edge + // ( == 1 is used here so it triggers on the very first pass) + if ((scrollpos & 0x07u) == 1) { + + // Get hardware map tile X column from scroll position / 8 + // Then + 1 since there's a 1 tile column buffer on the right edge + datapos = (scrollpos >> 3) + 1; + uint8_t map_x_column = (datapos + DEVICE_SCREEN_WIDTH) & 31; + + // Decompress a column worth of data + data_ptr = &data[map_x_column][0]; + // If the end of compressed data is reached, reset decompression + // and resume at the start (to make infinite scrolling) + if (!rle_decompress(data_ptr, MAP_DATA_HEIGHT)) { + rle_init(map_compressed); + rle_decompress(data_ptr, MAP_DATA_HEIGHT); + } + // pre-process column here + // ... + + // Display column + set_bkg_tiles(map_x_column, 0, 1, MAP_DATA_HEIGHT, data_ptr); + } } }

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