git.y1.nz

gbdk-2020

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

commit 4316aebb656dc683a8367bcca1313a6a7001610e
parent babfc15aa1ac6cf06ef9773e83683d14378b3710
Author: bbbbbr <bbbbbr@users.noreply.github.com>
Date:   Sat, 11 Jun 2022 20:02:09 -0700

Merge pull request #376 from bbbbbr/examples_apa_image

Examples apa image
Diffstat:
Agbdk-lib/examples/gb/apa_image/Makefile97+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Agbdk-lib/examples/gb/apa_image/Readme.md21+++++++++++++++++++++
Agbdk-lib/examples/gb/apa_image/res/scenery.png0
Agbdk-lib/examples/gb/apa_image/src/main.c42++++++++++++++++++++++++++++++++++++++++++
Mgbdk-lib/include/gb/gb.h2+-
5 files changed, 161 insertions(+), 1 deletion(-)

diff --git a/gbdk-lib/examples/gb/apa_image/Makefile b/gbdk-lib/examples/gb/apa_image/Makefile @@ -0,0 +1,97 @@ +# +# A Makefile that compiles all .c and .s files in "src" and "res" +# subdirectories and places the output in a "obj" subdirectory +# + +# If you move this project you can change the directory +# to match your GBDK root directory (ex: GBDK_HOME = "C:/GBDK/" +GBDK_HOME = ../../../ + +LCC = $(GBDK_HOME)bin/lcc +PNG2ASSET = $(GBDK_HOME)bin/png2asset + +# You can set flags for LCC here +# For example, you can uncomment the line below to turn on debug output +# LCCFLAGS += -debug + +# Add directory where image gets converted into (obj/) +# So they can be included with "#include <res/somefile.h>" +LCCFLAGS += -I$(OBJDIR) + +# Make the ROM CGB compatible (but not exclusive) +LCCFLAGS += -Wm-yc + +# You can set the name of the .gb ROM file here +PROJECTNAME = apa_image + +SRCDIR = src +OBJDIR = obj +RESOBJSRC = obj/res +RESDIR = res +MKDIRS = $(OBJDIR) $(BINDIR) $(RESOBJSRC) # See bottom of Makefile for directory auto-creation + +BINS = $(OBJDIR)/$(PROJECTNAME).gb + +# For png2asset: converting source pngs -> .c -> .o +IMGPNGS = $(foreach dir,$(RESDIR),$(notdir $(wildcard $(dir)/*.png))) +IMGSOURCES = $(IMGPNGS:%.png=$(RESOBJSRC)/%.c) +IMGOBJS = $(IMGSOURCES:$(RESOBJSRC)/%.c=$(OBJDIR)/%.o) + +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) + +all: $(BINS) + +compile.bat: Makefile + @echo "REM Automatically generated from Makefile" > compile.bat + @make -sn | sed y/\\//\\\\/ | grep -v make >> compile.bat + +# Use png2asset to convert the png into C formatted metasprite data +# -keep_duplicate_tiles : Don't remove duplicate tiles +# -map : Use "map style" output, not metasprite +# -tiles_only : Only keep tiles, no map (since APA mode expects tiles in linear order) +# -bpp 2 : Use 2bpp output +# -c ... : Set C output file +# Convert metasprite .pngs in res/ -> .c files in obj/<platform ext>/src/ +$(RESOBJSRC)/%.c: $(RESDIR)/%.png + $(PNG2ASSET) $< -keep_duplicate_tiles -map -bpp2 -tiles_only -c $@ + +# Compile the pngs that were converted to .c files +# .c files in obj/res/ -> .o files in obj/ +$(OBJDIR)/%.o: $(RESOBJSRC)/%.c + $(LCC) $(CFLAGS) -c -o $@ $< + +# Compile .c files in "src/" to .o object files +$(OBJDIR)/%.o: $(SRCDIR)/%.c + $(LCC) $(LCCFLAGS) -c -o $@ $< + +# Compile .c files in "res/" to .o object files +$(OBJDIR)/%.o: $(RESDIR)/%.c + $(LCC) $(CFLAGS) -c -o $@ $< + +# Compile .s assembly files in "src/" to .o object files +$(OBJDIR)/%.o: $(SRCDIR)/%.s + $(LCC) $(LCCFLAGS) -c -o $@ $< + +# If needed, compile .c files in "src/" to .s assembly files +# (not required if .c is compiled directly to .o) +$(OBJDIR)/%.s: $(SRCDIR)/%.c + $(LCC) $(LCCFLAGS) -S -o $@ $< + +# Convert images first so they're available when compiling the main sources +$(OBJS): $(IMGOBJS) + +# Link the compiled object files into a .gb ROM file +$(BINS): $(OBJS) + $(LCC) $(LCCFLAGS) -o $(BINS) $(IMGOBJS) $(OBJS) + +clean: + rm -f $(OBJDIR)/*.* + rm -f $(RESOBJSRC)/*.* + + +# create necessary directories after Makefile is parsed but before build +# info prevents the command from being pasted into the makefile +$(info $(shell mkdir -p $(MKDIRS))) + diff --git a/gbdk-lib/examples/gb/apa_image/Readme.md b/gbdk-lib/examples/gb/apa_image/Readme.md @@ -0,0 +1,21 @@ + +### Displaying an image with more than 256 tiles in APA mode. + +This example shows how to use APA graphics mode and png2asset to convert and display an image with more than 256 tiles. The image should be in PNG format with 4 colors at 160 x 144. + +This sample project can be used to make a minimal program which displays a logo + +The image is displayed with: +```draw_image();``` + +This call will automatically switch to APA graphics mode and install it's start and mid-frame ISRs which switch the tile source, enabling it to display more than 256 tiles. The screen hardware map is configured such that each tile used is unique, and indexed sequentially from left-to-right, top-to-bottom (20 x 18 = 360 tiles total). + +png2asset is called with these values in order to produce a compatible image: + -keep_duplicate_tiles : Don't remove duplicate tiles (required for APA bitmap image display) + -map : Use "map style" output, not metasprite + -tiles_only : Only keep tiles, no map (since APA mode expects tiles in linear order) + -bpp 2 : Use 2bpp output + + +Pixel art originally by RodrixAP under Creative Commons Attribution 2.0 Generic (CC BY 2.0) +https://www.flickr.com/photos/rodrixap/10591266994/in/album-72157637154901153/ diff --git a/gbdk-lib/examples/gb/apa_image/res/scenery.png b/gbdk-lib/examples/gb/apa_image/res/scenery.png Binary files differ. diff --git a/gbdk-lib/examples/gb/apa_image/src/main.c b/gbdk-lib/examples/gb/apa_image/src/main.c @@ -0,0 +1,42 @@ +#include <gbdk/platform.h> +#include <gb/drawing.h> +#include <stdint.h> + +#include <res/scenery.h> + + +#define CGB_ONE_PAL 1u +const palette_color_t cgb_pal_black[] = {RGB_BLACK, RGB_BLACK, RGB_BLACK, RGB_BLACK}; + + +void main(void) +{ + // Set the screen to black via the palettes to hide the image draw + if (_cpu == CGB_TYPE) { + set_bkg_palette(OAMF_CGB_PAL0, CGB_ONE_PAL, cgb_pal_black); + } else { + BGP_REG = DMG_PALETTE(DMG_BLACK, DMG_BLACK, DMG_BLACK, DMG_BLACK); + } + + // Display the image + // This will automatically switch to APA graphics mode + // and install it's start and mid-frame ISRs. + draw_image(scenery_tiles); + SHOW_BKG; + + // Then load the palettes at the start of a new frame + wait_vbl_done(); + if (_cpu == CGB_TYPE) { + set_bkg_palette(OAMF_CGB_PAL0, CGB_ONE_PAL, scenery_palettes); + } else { + BGP_REG = DMG_PALETTE(DMG_WHITE, DMG_LITE_GRAY, DMG_DARK_GRAY, DMG_BLACK); + } + + + // Loop forever + while(1) { + // Main processing goes here + // Done processing, yield CPU and wait for start of next frame + wait_vbl_done(); + } +} diff --git a/gbdk-lib/include/gb/gb.h b/gbdk-lib/include/gb/gb.h @@ -126,7 +126,7 @@ Example: \code{.c} - REG_BGP = DMG_PALETTE(DMG_BLACK, DMG_DARK_GRAY, DMG_LITE_GRAY, DMG_WHITE); + BGP_REG = DMG_PALETTE(DMG_BLACK, DMG_DARK_GRAY, DMG_LITE_GRAY, DMG_WHITE); \endcode @see OBP0_REG, OBP1_REG, BGP_REG

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