gbdk-2020 | GameBoy Development Kit |
| download: https://git.y1.nz/archives/gbdk.tar.gz | |
| README | Files | Log | Refs | LICENSE |
commit 5c8be3f338c1c5d40618f50b963da21ae6479822 parent c0f4fc09f264694f77894b7598cb04f959830e67 Author: Toxa <untoxa@mail.ru> Date: Thu, 26 Aug 2021 17:56:10 +0300 examples: cross-platform (GB/GG) large_map example Diffstat:
11 files changed, 228 insertions(+), 128 deletions(-)
diff --git a/gbdk-lib/examples/cross-platform/large_map/Makefile b/gbdk-lib/examples/cross-platform/large_map/Makefile @@ -0,0 +1,75 @@ +# Note: "override" ensures that appending to the variables still works if the variable was set from the command line +# +# 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 + +# Set platforms to build here (spaced separated). Possible are: gb gbc pocket sms gg +# These are found in the separate Makefile.targets +TARGETS=gb gg + +# MBC + Autobanking related flags +override LCCFLAGS += -Wl-j -Wm-yoA -Wm-ya4 -autobank -Wb-ext=.rel -Wb-v +# override LCCFLAGS += -debug # Uncomment to enable debug output +# override LCCFLAGS += -v # Uncomment for lcc verbose output + +# You can set the name of the ROM file here +PROJECTNAME = Example + +# EXT?=gb # Only sets extension to default (game boy .gb) if not populated +SRCDIR = src +OBJDIR = obj/$(EXT) +RESDIR = res +BINDIR = build/$(EXT) +MKDIRS = $(OBJDIR) $(BINDIR) # See bottom of Makefile for directory auto-creation + +BINS = $(OBJDIR)/$(PROJECTNAME).$(EXT) +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 +all: $(TARGETS) + +# Compile .c files in "src/" to .o object files +$(OBJDIR)/%.o: $(SRCDIR)/%.c + $(LCC) $(CFLAGS) -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) $(CFLAGS) -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) $(CFLAGS) -S -o $@ $< + +# Link the compiled object files into a .gb ROM file +# Then copy the ROM into a binary directory +$(BINS): $(OBJS) + @echo $(LCCFLAGS) + $(LCC) $(LCCFLAGS) $(CFLAGS) -o $(BINDIR)/$(PROJECTNAME).$(EXT) $(OBJS) + +clean: + @echo Cleaning + @for target in $(TARGETS); do \ + $(MAKE) $$target-clean; \ + done + +cleanbin: + rm -f $(BINDIR)/*.* + +# Include available build targets +include Makefile.targets + + +# create necessary directories after Makefile is parsed but before build +# info prevents the command from being pasted into the makefile +ifneq ($(strip $(EXT)),) # Only make the directories if EXT has been set by a target +$(info $(shell mkdir -p $(MKDIRS))) +endif diff --git a/gbdk-lib/examples/cross-platform/large_map/Makefile.targets b/gbdk-lib/examples/cross-platform/large_map/Makefile.targets @@ -0,0 +1,45 @@ + +# Platform specific flags for compiling (only populate if they're both present) +ifneq ($(strip $(PORT)),) +ifneq ($(strip $(PLAT)),) +override CFLAGS += -m$(PORT):$(PLAT) -D_PLAT_$(PLAT) +endif +endif + +# Called by the individual targets below to build a ROM +build-target: $(BINS) + +clean-target: + rm -rf $(OBJDIR) + rm -rf $(BINDIR) + +gb-clean: + ${MAKE} clean-target EXT=gb +gb: + ${MAKE} build-target LCCFLAGS=-Wl-yt0x1B PORT=gbz80 PLAT=gb EXT=gb + +# GB & GBC: -Wm-yc +# GBC Exclusive: -Wm-yC +gbc-clean: + ${MAKE} clean-target EXT=gbc +gbc: + ${MAKE} build-target LCCFLAGS="-Wl-yt0x1B -Wm-yC" PORT=gbz80 PLAT=gb EXT=gbc + + +pocket-clean: + ${MAKE} clean-target EXT=pocket +pocket: + ${MAKE} build-target LCCFLAGS=-Wl-yt0x1B PORT=gbz80 PLAT=ap EXT=pocket + + +sms-clean: + ${MAKE} clean-target EXT=sms +sms: + ${MAKE} build-target PORT=z80 PLAT=sms EXT=sms + + +gg-clean: + ${MAKE} clean-target EXT=gg +gg: + ${MAKE} build-target PORT=z80 PLAT=gg EXT=gg + diff --git a/gbdk-lib/examples/gg/large_map/Readme.md b/gbdk-lib/examples/cross-platform/large_map/Readme.md diff --git a/gbdk-lib/examples/gg/large_map/bigmap_map.c b/gbdk-lib/examples/cross-platform/large_map/src/bigmap_map.c diff --git a/gbdk-lib/examples/gg/large_map/bigmap_map.h b/gbdk-lib/examples/cross-platform/large_map/src/bigmap_map.h diff --git a/gbdk-lib/examples/gg/large_map/bigmap_tiles.c b/gbdk-lib/examples/cross-platform/large_map/src/bigmap_tiles.c diff --git a/gbdk-lib/examples/gg/large_map/bigmap_tiles.h b/gbdk-lib/examples/cross-platform/large_map/src/bigmap_tiles.h diff --git a/gbdk-lib/examples/cross-platform/large_map/src/large_map.c b/gbdk-lib/examples/cross-platform/large_map/src/large_map.c @@ -0,0 +1,104 @@ +#include <stdint.h> + +#include <gbdk/platform.h> + +#include "bigmap_map.h" +#include "bigmap_tiles.h" + +#define camera_max_y ((bigmap_mapHeight - 18) * 8) +#define camera_max_x ((bigmap_mapWidth - 20) * 8) + +#if defined(_PLAT_gg) || defined(_PLAT_sms) + #define WRAP_SCROLL_Y(y) ((y) % 224u) +#else + #define WRAP_SCROLL_Y(y) y +#endif + +#define MIN(A,B) ((A)<(B)?(A):(B)) + +uint8_t joy; + +// current and old positions of the camera in pixels +uint16_t camera_x, camera_y, old_camera_x, old_camera_y; +// current and old position of the map in tiles +uint8_t map_pos_x, map_pos_y, old_map_pos_x, old_map_pos_y; +// redraw flag, indicates that camera position was changed +uint8_t redraw; + +void set_camera() { + // update hardware scroll position + move_bkg(camera_x, WRAP_SCROLL_Y(camera_y)); + // up or down + map_pos_y = (uint8_t)(camera_y >> 3u); + if (map_pos_y != old_map_pos_y) { + if (camera_y < old_camera_y) { + set_bkg_submap(map_pos_x, map_pos_y, MIN(21u, bigmap_mapWidth-map_pos_x), 1, bigmap_map, bigmap_mapWidth); + } else { + if ((bigmap_mapHeight - 18u) > map_pos_y) set_bkg_submap(map_pos_x, map_pos_y + 18u, MIN(21u, bigmap_mapWidth-map_pos_x), 1, bigmap_map, bigmap_mapWidth); + } + old_map_pos_y = map_pos_y; + } + // left or right + map_pos_x = (uint8_t)(camera_x >> 3u); + if (map_pos_x != old_map_pos_x) { + if (camera_x < old_camera_x) { + set_bkg_submap(map_pos_x, map_pos_y, 1, MIN(19u, bigmap_mapHeight - map_pos_y), bigmap_map, bigmap_mapWidth); + } else { + if ((bigmap_mapWidth - 20u) > map_pos_x) set_bkg_submap(map_pos_x + 20u, map_pos_y, 1, MIN(19u, bigmap_mapHeight - map_pos_y), bigmap_map, bigmap_mapWidth); + } + old_map_pos_x = map_pos_x; + } + // set old camera position to current camera position + old_camera_x = camera_x, old_camera_y = camera_y; +} + +void main(){ + DISPLAY_OFF; + set_2bpp_palette(COMPAT_PALETTE(11,5,4,3)); + set_bkg_data(0, 241u, bigmap_tiles); + + map_pos_x = map_pos_y = 0; + old_map_pos_x = old_map_pos_y = 255; + set_bkg_submap(map_pos_x, map_pos_y, 20, 18, bigmap_map, bigmap_mapWidth); + + camera_x = camera_y = 0; + old_camera_x = camera_x; old_camera_y = camera_y; + + redraw = FALSE; + + move_bkg(camera_x, WRAP_SCROLL_Y(camera_y)); + SHOW_BKG; + DISPLAY_ON; + while (TRUE) { + joy = joypad(); + // up or down + if (joy & J_UP) { + if (camera_y) { + camera_y--; + redraw = TRUE; + } + } else if (joy & J_DOWN) { + if (camera_y < camera_max_y) { + camera_y++; + redraw = TRUE; + } + } + // left or right + if (joy & J_LEFT) { + if (camera_x) { + camera_x--; + redraw = TRUE; + } + } else if (joy & J_RIGHT) { + if (camera_x < camera_max_x) { + camera_x++; + redraw = TRUE; + } + } + if (redraw) { + wait_vbl_done(); + set_camera(); + redraw = FALSE; + } else wait_vbl_done(); + } +} + diff --git a/gbdk-lib/examples/gg/large_map/Makefile b/gbdk-lib/examples/gg/large_map/Makefile @@ -1,30 +0,0 @@ -CC = ../../../bin/lcc -mz80:gg -Wm-yoA -Wl-m -Wl-j - -BINS = large_map.gg - -all: $(BINS) - -ASRC = $(wildcard *.s) -CSRC = $(wildcard *.c) - -OBJS = $(CSRC:%.c=%.o) $(ASRC:%.s=%.o) - -compile.bat: Makefile - @echo "REM Automatically generated from Makefile" > compile.bat - @make -sn | sed y/\\//\\\\/ | grep -v make >> compile.bat - -# Compile and link single file in one pass - -%.o: %.c - $(CC) -c -o $@ $< - -%.o: %.s - $(CC) -c -o $@ $< - -$(BINS): $(OBJS) - $(CC) -Wm-yS -o $@ $^ - rm -f *.map *.noi *.ihx *.lst - -clean: - rm -f *.o *.lst *.map *.gg *~ *.rel *.cdb *.ihx *.lnk *.sym *.asm *.noi - diff --git a/gbdk-lib/examples/gg/large_map/large_map.c b/gbdk-lib/examples/gg/large_map/large_map.c @@ -1,97 +0,0 @@ -#include <stdint.h> - -#include <gbdk/platform.h> - -#include "bigmap_map.h" -#include "bigmap_tiles.h" - -#define camera_max_y ((bigmap_mapHeight - 18) * 8) -#define camera_max_x ((bigmap_mapWidth - 20) * 8) - -#define MIN(A,B) ((A)<(B)?(A):(B)) - -uint8_t joy; - -// current and old positions of the camera in pixels -uint16_t camera_x, camera_y, old_camera_x, old_camera_y; -// current and old position of the map in tiles -uint8_t map_pos_x, map_pos_y, old_map_pos_x, old_map_pos_y; -// redraw flag, indicates that camera position was changed -uint8_t redraw; - -void set_camera() { - // update hardware scroll position - move_bkg(camera_x, camera_y % 224); - // up or down - map_pos_y = (uint8_t)(camera_y >> 3u); - if (map_pos_y != old_map_pos_y) { - if (camera_y < old_camera_y) { - set_bkg_submap(map_pos_x, map_pos_y, MIN(21u, bigmap_mapWidth-map_pos_x), 1, bigmap_map, bigmap_mapWidth); - } else { - if ((bigmap_mapHeight - 18u) > map_pos_y) set_bkg_submap(map_pos_x, map_pos_y + 18u, MIN(21u, bigmap_mapWidth-map_pos_x), 1, bigmap_map, bigmap_mapWidth); - } - old_map_pos_y = map_pos_y; - } - // left or right - map_pos_x = (uint8_t)(camera_x >> 3u); - if (map_pos_x != old_map_pos_x) { - if (camera_x < old_camera_x) { - set_bkg_submap(map_pos_x, map_pos_y, 1, MIN(19u, bigmap_mapHeight - map_pos_y), bigmap_map, bigmap_mapWidth); - } else { - if ((bigmap_mapWidth - 20u) > map_pos_x) set_bkg_submap(map_pos_x + 20u, map_pos_y, 1, MIN(19u, bigmap_mapHeight - map_pos_y), bigmap_map, bigmap_mapWidth); - } - old_map_pos_x = map_pos_x; - } - // set old camera position to current camera position - old_camera_x = camera_x, old_camera_y = camera_y; -} - -void main(){ - DISPLAY_OFF; - set_2bpp_palette(COMPAT_PALETTE(11,5,4,3)); - set_bkg_data(0, 241u, bigmap_tiles); - - map_pos_x = map_pos_y = 0; - old_map_pos_x = old_map_pos_y = 255; - set_bkg_submap(map_pos_x, map_pos_y, 20, 18, bigmap_map, bigmap_mapWidth); - - camera_x = camera_y = 0; - old_camera_x = camera_x; old_camera_y = camera_y; - - redraw = FALSE; - - move_bkg(camera_x, camera_y % 224); - DISPLAY_ON; - while (TRUE) { - joy = joypad(); - // up or down - if (joy & J_UP) { - if (camera_y) { - camera_y--; - redraw = TRUE; - } - } else if (joy & J_DOWN) { - if (camera_y < camera_max_y) { - camera_y++; - redraw = TRUE; - } - } - // left or right - if (joy & J_LEFT) { - if (camera_x) { - camera_x--; - redraw = TRUE; - } - } else if (joy & J_RIGHT) { - if (camera_x < camera_max_x) { - camera_x++; - redraw = TRUE; - } - } - if (redraw) { - wait_vbl_done(); - set_camera(); - redraw = FALSE; - } else wait_vbl_done(); - } -} - diff --git a/gbdk-lib/include/gb/gb.h b/gbdk-lib/include/gb/gb.h @@ -722,6 +722,9 @@ uint8_t get_vram_byte(uint8_t * addr) __preserves_regs(b, c); */ uint8_t * get_bkg_xy_addr(uint8_t x, uint8_t y) __preserves_regs(b, c); + +#define COMPAT_PALETTE(C0,C1,C2,C3) (((C3) << 6) | ((C2) << 4) | ((C1) << 2) | (C0)) + /** Sets palette for 2bpp color translation for GG/SMS, does nothing on GB */ inline void set_2bpp_palette(uint16_t palette) {
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.