git.y1.nz

gbdk-2020

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

commit 5e8e11118c269c9edee78dc482a419b482464cfa
parent 3895fe9a799e06d78fe359a4b3961e33deaf7a69
Author: Toxa <untoxa@mail.ru>
Date:   Wed, 23 Jun 2021 15:16:00 +0300

new gb/incbin.h header, allows including binary files directly

Diffstat:
Agbdk-lib/examples/gb/incbin/Makefile60++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Agbdk-lib/examples/gb/incbin/Readme.md8++++++++
Agbdk-lib/examples/gb/incbin/res/blanktile.bin0
Agbdk-lib/examples/gb/incbin/res/blanktile.png0
Agbdk-lib/examples/gb/incbin/res/gbdk2020.bin0
Agbdk-lib/examples/gb/incbin/res/gbdk2020.png0
Agbdk-lib/examples/gb/incbin/res/gbdk2020_map.bin0
Agbdk-lib/examples/gb/incbin/src/main.c58++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Agbdk-lib/include/gb/incbin.h82+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
9 files changed, 208 insertions(+), 0 deletions(-)

diff --git a/gbdk-lib/examples/gb/incbin/Makefile b/gbdk-lib/examples/gb/incbin/Makefile @@ -0,0 +1,60 @@ +# +# 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 + +# You can set flags for LCC here +# For example, you can uncomment the line below to turn on debug output +LCCFLAGS = -Wl-j -Wm-yS + +# You can set the name of the .gb ROM file here +PROJECTNAME = Example + +SRCDIR = src +OBJDIR = obj +RESDIR = res +BINS = $(PROJECTNAME).gb +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: prepare $(BINS) + +make.bat: Makefile + @echo "REM Automatically generated from Makefile" > make.bat + @make -sn | sed y/\\//\\\\/ | grep -v make >> make.bat + +# 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) $(LCCFLAGS) -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 i n"src/" to .s assembly files +# (not required if .c is compiled directly to .o) +$(OBJDIR)/%.s: $(SRCDIR)/%.c + $(LCC) $(LCCFLAGS) -S -o $@ $< + +# Link the compiled object files into a .gb ROM file +$(BINS): $(OBJS) + $(LCC) $(LCCFLAGS) -o $(BINS) $(OBJS) + +prepare: + mkdir -p $(OBJDIR) + +clean: + rm -f *.gb *.ihx *.cdb *.adb *.noi *.map *.sym + rm -rf $(OBJDIR) + diff --git a/gbdk-lib/examples/gb/incbin/Readme.md b/gbdk-lib/examples/gb/incbin/Readme.md @@ -0,0 +1,8 @@ + +An example project showing how to use 'incbin.h' to include binary files into C source files. + + +The 4 color .png files were exported to .bin format (2bpp gb) using the gimp plugin listed below. +There are many other tools which can also do the conversion. + +https://github.com/bbbbbr/gimp-rom-bin diff --git a/gbdk-lib/examples/gb/incbin/res/blanktile.bin b/gbdk-lib/examples/gb/incbin/res/blanktile.bin Binary files differ. diff --git a/gbdk-lib/examples/gb/incbin/res/blanktile.png b/gbdk-lib/examples/gb/incbin/res/blanktile.png Binary files differ. diff --git a/gbdk-lib/examples/gb/incbin/res/gbdk2020.bin b/gbdk-lib/examples/gb/incbin/res/gbdk2020.bin Binary files differ. diff --git a/gbdk-lib/examples/gb/incbin/res/gbdk2020.png b/gbdk-lib/examples/gb/incbin/res/gbdk2020.png Binary files differ. diff --git a/gbdk-lib/examples/gb/incbin/res/gbdk2020_map.bin b/gbdk-lib/examples/gb/incbin/res/gbdk2020_map.bin Binary files differ. diff --git a/gbdk-lib/examples/gb/incbin/src/main.c b/gbdk-lib/examples/gb/incbin/src/main.c @@ -0,0 +1,58 @@ +#include <gb/gb.h> +#include <stdint.h> + +#include <gb/incbin.h> + +INCBIN(logo_tiles_data, res/gbdk2020.bin) // Variable name to use, Path to file +INCBIN_EXTERN(logo_tiles_data) // Extern declarations for binary data + +INCBIN(logo_map, res/gbdk2020_map.bin) +INCBIN_EXTERN(logo_map) + +INCBIN(blank_tile_data, res/blanktile.bin) +INCBIN_EXTERN(blank_tile_data) + + +#define TILE_BYTES 16 // 16 bytes per background tile + + +// Since incbin just includes a binary file (the logo tiles) +// a map needs to be created for them. For this example +// the tiles are non-deduplciated, so a array of incrementing +// values can be used. There are 84 tiles. +#define LOGO_MAP_WIDTH 7u +#define LOGO_MAP_HEIGHT 12u +#define LOGO_MAP_X (20u - LOGO_MAP_WIDTH) / 2u // Center on X axis +#define LOGO_MAP_Y (18u - LOGO_MAP_HEIGHT) / 2u // Center on Y axis + +void init_gfx() { + // Load a single clear background tile at location 0x80 and clear/fill the map with it + set_bkg_data(0x80u, 1u, blank_tile_data); // The first 0x80u here is the tile ID + fill_bkg_rect(0u, 0u, 20u, 18u, 0x80u); // The last 0x80u here is the tile ID + + // Load logo background tiles and map + // They start at 0u + set_bkg_data(0u, SIZE(logo_tiles_data) / TILE_BYTES, logo_tiles_data); + set_bkg_tiles(LOGO_MAP_X, LOGO_MAP_Y, + LOGO_MAP_WIDTH, LOGO_MAP_HEIGHT, + logo_map); + + // Turn the background map on to make it visible + SHOW_BKG; +} + + + +void main(void) +{ + init_gfx(); + + // Loop forever + while(1) { + + // Game main loop processing goes here + + // Done processing, yield CPU and wait for start of next frame + wait_vbl_done(); + } +} diff --git a/gbdk-lib/include/gb/incbin.h b/gbdk-lib/include/gb/incbin.h @@ -0,0 +1,82 @@ +/** @file gb/incbin.h + + Allows binary data from other files to be included + into a C source file. + + It is implemented using asm .incbin and macros. + + See the `incbin` example project for a demo of how to use it. +*/ +#ifndef _INCBIN_H +#define _INCBIN_H + +#include <stdint.h> + + +/** Creates extern entries for accessing a INCBIN() generated + variable and it's size in another source file. + + @param VARNAME Name of the variable used with INCBIN + + An entry is created for the variable and it's size variable. + + @ref INCBIN(), SIZE() +*/ +#define INCBIN_EXTERN(VARNAME) extern const uint8_t VARNAME[]; \ +extern const void __size_ ## VARNAME; \ +extern const void __bank_ ## VARNAME; + +/** Obtains the __size in bytes__ of the INCBIN() generated data + + @param VARNAME Name of the variable used with INCBIN + + Requires @ref INCBIN_EXTERN() to have been called earlier in the source file + + @ref INCBIN(), INCBIN_EXTERN() +*/ +#define SIZE(VARNAME) ( (uint16_t) & __size_ ## VARNAME ) + +/** Obtains the __bank number__ of the INCBIN() generated data + + @param VARNAME Name of the variable used with INCBIN + + Requires @ref INCBIN_EXTERN() to have been called earlier in the source file + + @ref INCBIN(), INCBIN_EXTERN() +*/ +#define BANK(VARNAME) ( (uint8_t) & __bank_ ## VARNAME ) + +/** Includes binary data into a C source file + + @param VARNAME Variable name to use + @param FILEPATH Path to the file which will be binary included into the C source file + + __filepath__ is relative to the working directory of the tool + that is calling it (often a makefile's working directory), __NOT__ + to the file it's being included into. + + The variable name is not modified and can be used as-is. + + @see SIZE() for obtaining the size of the included data. + @see BANK() for obtaining the bank number of the included data. + + Use @ref INCBIN_EXTERN() within another source file + to make the variable and it's data accesible there. +*/ +#define INCBIN(VARNAME, FILEPATH) void __func_ ## VARNAME() __banked __naked { \ +__asm \ +_ ## VARNAME:: \ +1$: \ + .incbin #FILEPATH \ +2$: \ + ___size_ ## VARNAME = (2$-1$) \ + .globl ___size_ ## VARNAME \ + .local b___func_ ## VARNAME \ + ___bank_ ## VARNAME = b___func_ ## VARNAME \ + .globl ___bank_ ## VARNAME \ +__endasm; \ +} + +#endif // _INCBIN_H + +

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