git.y1.nz

gbdk-2020

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

commit 428b3225a5a2f4e717e473f33ae480266000c444
parent 48d8b5207b5784169a80c21cfef0cceda321e8c8
Author: bbbbbr <bbbbbr@users.noreply.github.com>
Date:   Fri,  5 Dec 2025 22:46:14 -0800

Merge pull request #845 from bbbbbr/example/rlecompress_convert_in_makefile

examples: rle compress: convert and compress map at build time
Diffstat:
Mgbdk-lib/examples/cross-platform/rle_map/Makefile76+++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------
Mgbdk-lib/examples/cross-platform/rle_map/Makefile.targets1+
Mgbdk-lib/examples/cross-platform/rle_map/Readme.md3++-
Dgbdk-lib/examples/cross-platform/rle_map/res/asset_sources/map.bin0
Dgbdk-lib/examples/cross-platform/rle_map/res/asset_sources/map.gbm0
Dgbdk-lib/examples/cross-platform/rle_map/res/asset_sources/map_tiles.gbr0
Dgbdk-lib/examples/cross-platform/rle_map/res/map.bin.rle0
Rgbdk-lib/examples/cross-platform/rle_map/res/asset_sources/map.png -> gbdk-lib/examples/cross-platform/rle_map/res/map.png0
Agbdk-lib/examples/cross-platform/rle_map/res/map.png.meta3+++
Dgbdk-lib/examples/cross-platform/rle_map/res/map_tiles.bin0
Mgbdk-lib/examples/cross-platform/rle_map/src/main.c10+++++++---
11 files changed, 68 insertions(+), 25 deletions(-)

diff --git a/gbdk-lib/examples/cross-platform/rle_map/Makefile b/gbdk-lib/examples/cross-platform/rle_map/Makefile @@ -1,5 +1,3 @@ -SHELL := /bin/bash - # If you move this project you can change the directory # to match your GBDK root directory (ex: GBDK_HOME = "C:/GBDK/" ifndef GBDK_HOME @@ -7,6 +5,8 @@ ifndef GBDK_HOME endif LCC = $(GBDK_HOME)/bin/lcc +PNG2ASSET = $(GBDK_HOME)/bin/png2asset +GBCOMPRESS = $(GBDK_HOME)/bin/gbcompress # Set platforms to build here, spaced separated. (These are in the separate Makefile.targets) # They can also be built/cleaned individually: "make gg" and "make gg-clean" @@ -24,33 +24,48 @@ LCCFLAGS_nes = LCCFLAGS += $(LCCFLAGS_$(EXT)) # This adds the current platform specific LCC Flags -# .noi symbol file output -LCCFLAGS += -Wl-j -Wm-yS - -# LCCFLAGS += -Wl-j -Wm-yoA -Wm-ya4 -autobank -Wb-ext=.rel -Wb-v # MBC + Autobanking related flags +LCCFLAGS += -Wl-j -Wm-yoA -autobank -Wb-ext=.rel -Wb-v # MBC + Autobanking related flags # GBDK_DEBUG = ON ifdef GBDK_DEBUG LCCFLAGS += -debug -v endif - CFLAGS = -Wf-Iinclude +# Add directory where platform specific meta tile sprites get converted into (obj/<platform ext>/src) +# So they can be included with "#include <res/somefile.h>" +CFLAGS += -I$(OBJDIR) -I$(RESALLDIR) + # You can set the name of the ROM file here PROJECTNAME = rledecompress -# EXT?=gb # Only sets extension to default (game boy .gb) if not populated SRCDIR = src -SRCPLAT = src/$(PORT) OBJDIR = obj/$(EXT) +# In this example the converted output is the same on all platforms, and so shared for INCBIN() path simplicity +RESALLDIR = obj/all RESDIR = res BINDIR = build/$(EXT) -MKDIRS = $(OBJDIR) $(BINDIR) # See bottom of Makefile for directory auto-creation +MKDIRS = $(OBJDIR) $(BINDIR) $(RESALLDIR) # See bottom of Makefile for directory auto-creation BINS = $(OBJDIR)/$(PROJECTNAME).$(EXT) -CSOURCES = $(foreach dir,$(SRCDIR),$(notdir $(wildcard $(dir)/*.c))) $(foreach dir,$(SRCPLAT),$(notdir $(wildcard $(dir)/*.c))) $(foreach dir,$(RESDIR),$(notdir $(wildcard $(dir)/*.c))) -ASMSOURCES = $(foreach dir,$(SRCDIR),$(notdir $(wildcard $(dir)/*.s))) $(foreach dir,$(SRCPLAT),$(notdir $(wildcard $(dir)/*.s))) + +# For .png -> png2asset -> .bin -> gbcompress -> .c -> compile -> .o +# When exporting to .bin the single png input file gets split into _map.bin and _tiles.bin, etc output files along with a header file +PNGS = $(foreach dir,$(RESDIR),$(notdir $(wildcard $(dir)/*.png))) + +# In this example, only the Map is compressed and not the Tiles +# +# So there is one list of uncompressed Tiles from pngs +PNG_RAW_TILEBINS = $(PNGS:%.png=$(RESALLDIR)/%_tiles.bin) # tiles don't get compressed +# +# And another list of compressed Maps from the same pngs +PNG_RAW_MAPBINS = $(PNGS:%.png=$(RESALLDIR)/%_map.bin) +PNG_COMP_MAPBINS = $(PNG_RAW_MAPBINS:$(RESALLDIR)%_map.bin=$(RESALLDIR)%_map.bin.rle) + + +CSOURCES = $(foreach dir,$(SRCDIR),$(notdir $(wildcard $(dir)/*.c))) $(foreach dir,$(RESDIR),$(notdir $(wildcard $(dir)/*.c))) +ASMSOURCES = $(foreach dir,$(SRCDIR),$(notdir $(wildcard $(dir)/*.s))) OBJS = $(CSOURCES:%.c=$(OBJDIR)/%.o) $(ASMSOURCES:%.s=$(OBJDIR)/%.o) # Builds all targets sequentially @@ -61,22 +76,33 @@ compile.bat: Makefile @make -sn | sed y/\\//\\\\/ | grep -v make >> compile.bat +# ===== Start png conversion and compression ===== + +# For .png -> png2asset -> .bin -> gbcompress -> .bin.rle + +# Use png2asset to convert the png into binary blob files for compression +# All of the multiple targets (map, tiles) will get generated +# by the single prerequisite of the matching %.png +$(RESALLDIR)/%_tiles.bin $(RESALLDIR)/%_map.bin: $(RESDIR)/%.png + $(PNG2ASSET) $< -o $(subst _map,,$(subst _tiles,,$@)) -bin -use_metafile + +# Then compress the binary files and output as .c files +# Generate a variable name for the compressed C output based on stripping the path and extension from the output filename +$(RESALLDIR)/%.bin.rle: $(RESALLDIR)/%.bin + $(GBCOMPRESS) --alg=rle -v $< $@ + +# ===== End png conversion and compression ===== + + + # Compile .c files in "src/" to .o object files $(OBJDIR)/%.o: $(SRCDIR)/%.c $(LCC) $(LCCFLAGS) $(CFLAGS) -c -o $@ $< -# Compile .c files in "src/<target>/" to .o object files -$(OBJDIR)/%.o: $(SRCPLAT)/%.c - $(LCC) $(LCCFLAGS) $(CFLAGS) -c -o $@ $< - # Compile .c files in "res/" to .o object files $(OBJDIR)/%.o: $(RESDIR)/%.c $(LCC) $(LCCFLAGS) $(CFLAGS) -c -o $@ $< -# Compile .s assembly files in "src/<target>/" to .o object files -$(OBJDIR)/%.o: $(SRCPLAT)/%.s - $(LCC) $(LCCFLAGS) $(CFLAGS) -c -o $@ $< - # Compile .s assembly files in "src/" to .o object files $(OBJDIR)/%.o: $(SRCDIR)/%.s $(LCC) $(LCCFLAGS) $(CFLAGS) -c -o $@ $< @@ -86,8 +112,12 @@ $(OBJDIR)/%.o: $(SRCDIR)/%.s $(OBJDIR)/%.s: $(SRCDIR)/%.c $(LCC) $(LCCFLAGS) $(CFLAGS) -S -o $@ $< + +# Convert png images first so they're available when compiling the main sources +$(OBJS): $(PNG_RAW_TILEBINS) $(PNG_COMP_MAPBINS) + # Link the compiled object files into a .gb ROM file -$(BINS): $(OBJS) +$(BINS): $(OBJS) $(LCC) $(LCCFLAGS) $(CFLAGS) -o $(BINDIR)/$(PROJECTNAME).$(EXT) $(OBJS) clean: @@ -99,6 +129,10 @@ clean: # Include available build targets include Makefile.targets +# make -d to debug +# With builtin rules turned off for less noise: +# MAKEFLAGS += --no-builtin-rules + # create necessary directories after Makefile is parsed but before build # info prevents the command from being pasted into the makefile diff --git a/gbdk-lib/examples/cross-platform/rle_map/Makefile.targets b/gbdk-lib/examples/cross-platform/rle_map/Makefile.targets @@ -11,6 +11,7 @@ build-target: $(BINS) clean-target: rm -rf $(OBJDIR) + rm -rf $(RESALLDIR) rm -rf $(BINDIR) gb-clean: diff --git a/gbdk-lib/examples/cross-platform/rle_map/Readme.md b/gbdk-lib/examples/cross-platform/rle_map/Readme.md @@ -5,7 +5,8 @@ RLE Decompress Demonstrates using rle_decompress to load a compressed tile map into vram. ## 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 Tile Map is converted to binary format (one map entry per byte) and encoded in sequential **columns** 20 tiles high. +- The column wise encoding is the `-transpose` option for `png2asset` The data is compressed using the `gbcompress` utility using the `--alg=rle` argument. 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_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/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/asset_sources/map.png b/gbdk-lib/examples/cross-platform/rle_map/res/map.png Binary files differ. diff --git a/gbdk-lib/examples/cross-platform/rle_map/res/map.png.meta b/gbdk-lib/examples/cross-platform/rle_map/res/map.png.meta @@ -0,0 +1,3 @@ +-map +-transposed + 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 @@ -4,15 +4,19 @@ #include <gbdk/rledecompress.h> +#include <map.h> + + // The Tile data is not compressed -INCBIN(map_tiles, "res/map_tiles.bin") +INCBIN(map_tiles, "obj/all/map_tiles.bin") +// INCBIN(map_tiles, INCPATH_PLAT) INCBIN_EXTERN(map_tiles) // 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") +#define MAP_DATA_HEIGHT (map_HEIGHT / map_TILE_H) +INCBIN(map_compressed, "obj/all/map_map.bin.rle") INCBIN_EXTERN(map_compressed) uint8_t data[MAP_DATA_HEIGHT]; // Collision map buffer

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