gbdk-2020 | GameBoy Development Kit |
| download: https://git.y1.nz/archives/gbdk.tar.gz | |
| README | Files | Log | Refs | LICENSE |
commit acb3df0c4bc752e71a266c712026f8a0b0e8097b parent b5af10fc660c5cc55e86c659fce96de2ed00f98c Author: Toxa <untoxa@mail.ru> Date: Sat, 7 Jan 2023 12:16:10 +0300 EXAMPLES: update "simple physics" example Diffstat:
| A | gbdk-lib/examples/cross-platform/simple_physics/Makefile | 106 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | gbdk-lib/examples/cross-platform/simple_physics/Makefile.targets | 55 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | gbdk-lib/examples/cross-platform/simple_physics/src/phys.c | 98 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| M | gbdk-lib/examples/gb/simple_physics/phys.c | 114 | +++++++++++++++++++++++++++++++++++++++++-------------------------------------- |
4 files changed, 318 insertions(+), 55 deletions(-)
diff --git a/gbdk-lib/examples/cross-platform/simple_physics/Makefile b/gbdk-lib/examples/cross-platform/simple_physics/Makefile @@ -0,0 +1,106 @@ +# 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 + +# 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" +# Possible are: gb gbc pocket megaduck sms gg +TARGETS=gb gbc pocket megaduck sms gg nes + +# Configure platform specific LCC flags here: +LCCFLAGS_gb = -Wl-yt0x1B -autobank # Set an MBC for banking (1B-ROM+MBC5+RAM+BATT) +LCCFLAGS_pocket = -Wl-yt0x1B -autobank # Usually the same as required for .gb +LCCFLAGS_duck = -Wl-yt0x1B -autobank # Usually the same as required for .gb +LCCFLAGS_gbc = -Wl-yt0x1B -Wm-yc -autobank # Same as .gb with: -Wm-yc (gb & gbc) or Wm-yC (gbc exclusive) +LCCFLAGS_sms = -autobank +LCCFLAGS_gg = -autobank +LCCFLAGS_nes = + +LCCFLAGS += $(LCCFLAGS_$(EXT)) # This adds the current platform specific LCC Flags + +LCCFLAGS += -Wl-j -Wm-yoA -Wm-ya4 -Wb-ext=.rel -Wb-v # MBC + Autobanking related flags +# LCCFLAGS += -debug # Uncomment to enable debug output +# LCCFLAGS += -v # Uncomment for lcc verbose output + +# 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) + +# You can set the name of the ROM file here +PROJECTNAME = physics + +SRCDIR = src +OBJDIR = obj/$(EXT) +RESOBJSRC = obj/$(EXT)/res +RESDIR = res +BINDIR = build/$(EXT) +MKDIRS = $(OBJDIR) $(BINDIR) $(RESOBJSRC) # See bottom of Makefile for directory auto-creation + +BINS = $(OBJDIR)/$(PROJECTNAME).$(EXT) +# For png2asset: converting metasprite source pngs -> .c -> .o +METAPNGS = $(foreach dir,$(RESDIR),$(notdir $(wildcard $(dir)/*.png))) +METASRCS = $(METAPNGS:%.png=$(RESOBJSRC)/%.c) +METAOBJS = $(METASRCS:$(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) + + +# Builds all targets sequentially +all: $(TARGETS) + +# Use png2asset to convert the png into C formatted metasprite data +# -sh 48 : Sets sprite height to 48 (width remains automatic) +# -spr8x16 : Use 8x16 hardware sprites +# -c ... : Set C output file +# Convert metasprite .pngs in res/ -> .c files in obj/<platform ext>/src/ +$(RESOBJSRC)/%.c: $(RESDIR)/%.png + $(PNG2ASSET) $< -sh 48 -spr8x8 -noflip -c $@ + +# Compile the Metasprite pngs that were converted to .c files +# .c files in obj/<platform ext>/src/ -> .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) $(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 in "src/" to .s assembly files +# (not required if .c is compiled directly to .o) +$(OBJDIR)/%.s: $(SRCDIR)/%.c + $(LCC) $(CFLAGS) -S -o $@ $< + +# Convert and build MetaSprites first so they're available when compiling the main sources +$(OBJS): $(METAOBJS) + +# Link the compiled object files into a .gb ROM file +$(BINS): $(OBJS) + $(LCC) $(LCCFLAGS) $(CFLAGS) -o $(BINDIR)/$(PROJECTNAME).$(EXT) $(METAOBJS) $(OBJS) + +clean: + @echo Cleaning + @for target in $(TARGETS); do \ + $(MAKE) $$target-clean; \ + done + +# 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/simple_physics/Makefile.targets b/gbdk-lib/examples/cross-platform/simple_physics/Makefile.targets @@ -0,0 +1,54 @@ + +# Platform specific flags for compiling (only populate if they're both present) +ifneq ($(strip $(PORT)),) +ifneq ($(strip $(PLAT)),) +CFLAGS += -m$(PORT):$(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 PORT=sm83 PLAT=gb EXT=gb + + +gbc-clean: + ${MAKE} clean-target EXT=gbc +gbc: + ${MAKE} build-target PORT=sm83 PLAT=gb EXT=gbc + + +pocket-clean: + ${MAKE} clean-target EXT=pocket +pocket: + ${MAKE} build-target PORT=sm83 PLAT=ap EXT=pocket + + +megaduck-clean: + ${MAKE} clean-target EXT=duck +megaduck: + ${MAKE} build-target PORT=sm83 PLAT=duck EXT=duck + + +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 + +nes-clean: + ${MAKE} clean-target EXT=nes +nes: + ${MAKE} build-target PORT=mos6502 PLAT=nes EXT=nes + diff --git a/gbdk-lib/examples/cross-platform/simple_physics/src/phys.c b/gbdk-lib/examples/cross-platform/simple_physics/src/phys.c @@ -0,0 +1,98 @@ +// A simple sub-pixel / fixed point example +// Postion values are calculated as 16 bit numbers and their +// lower 4 bits are dropped when applying them to the sprite + +#include <gbdk/platform.h> + +#include <stdint.h> + +const uint8_t sprite_data[] = { + 0x3C,0x3C,0x42,0x7E,0x99,0xFF,0xA9,0xFF,0x89,0xFF,0x89,0xFF,0x42,0x7E,0x3C,0x3C, + 0x3C,0x3C,0x42,0x7E,0xB9,0xFF,0x89,0xFF,0x91,0xFF,0xB9,0xFF,0x42,0x7E,0x3C,0x3C, + 0x3C,0x3C,0x42,0x7E,0x99,0xFF,0x89,0xFF,0x99,0xFF,0x89,0xFF,0x5A,0x7E,0x3C,0x3C, + 0x3C,0x3C,0x42,0x7E,0xA9,0xFF,0xA9,0xFF,0xB9,0xFF,0x89,0xFF,0x42,0x7E,0x3C,0x3C + +}; + +// update user input macro +#define INPUT_PROCESS (joy=old_joy,joy=joypad()) +// check putton down +#define INPUT_KEY(key) (joy&(key)) +// check button press +#define INPUT_KEYPRESS(key) ((joy & ~old_joy) & (key)) + +// coordinate translation macros +#define SUBPIXELS_TO_PIXELS(v) (v >> 4) +#define PIXELS_TO_SUBPIXELS(v) (v << 4) + +// physics constants +#define MAX_X_SPEED_IN_SUBPIXELS 64 +#define X_ACCELERATION_IN_SUBPIXELS 2 +#define MAX_Y_SPEED_IN_SUBPIXELS 64 +#define Y_ACCELERATION_IN_SUBPIXELS 2 +#define JUMP_ACCELERATION_IN_SUBPIXELS 32 + +// object coords +int16_t PosX, PosY; +// object speeds +int16_t SpdX, SpdY; + +// new and previous values of the joypad input +uint8_t joy = 0, old_joy; + +// main function +void main(void) { +#ifdef NINTENDO + // init palettes + BGP_REG = OBP0_REG = OBP1_REG = DMG_PALETTE(DMG_WHITE, DMG_LITE_GRAY, DMG_DARK_GRAY, DMG_BLACK); +#endif + // load tile data into VRAM + set_sprite_data(0, 4, sprite_data); + + // set sprite tile + set_sprite_tile(0, 0); + + // show bkg and sprites + SHOW_BKG; SHOW_SPRITES; + + PosX = PosY = PIXELS_TO_SUBPIXELS(64); + SpdX = SpdY = PIXELS_TO_SUBPIXELS(0); + + while(TRUE) { + // poll joypads + INPUT_PROCESS; + + // check d-pad and change the object speed + if (INPUT_KEY(J_UP)) { + SpdY -= Y_ACCELERATION_IN_SUBPIXELS; + if (SpdY < -MAX_Y_SPEED_IN_SUBPIXELS) SpdY = -MAX_Y_SPEED_IN_SUBPIXELS; + } else if (INPUT_KEY(J_DOWN)) { + SpdY += Y_ACCELERATION_IN_SUBPIXELS; + if (SpdY > MAX_Y_SPEED_IN_SUBPIXELS) SpdY = MAX_Y_SPEED_IN_SUBPIXELS; + } + if (INPUT_KEY(J_LEFT)) { + SpdX -= X_ACCELERATION_IN_SUBPIXELS; + if (SpdX < -MAX_X_SPEED_IN_SUBPIXELS) SpdX = -MAX_X_SPEED_IN_SUBPIXELS; + } else if (INPUT_KEY(J_RIGHT)) { + SpdX += X_ACCELERATION_IN_SUBPIXELS; + if (SpdX > MAX_X_SPEED_IN_SUBPIXELS) SpdX = MAX_X_SPEED_IN_SUBPIXELS; + } + // check button keypress + if (INPUT_KEYPRESS(J_A)) { + SpdY = -JUMP_ACCELERATION_IN_SUBPIXELS; + } + + // change coordinates of the object + PosX += SpdX, PosY += SpdY; + + // Translate to pixels and move sprite + move_sprite(0, SUBPIXELS_TO_PIXELS(PosX), SUBPIXELS_TO_PIXELS(PosY)); + + // decelerate Y and X + if (SpdY < 0) SpdY++; else if (SpdY) SpdY--; + if (SpdX < 0) SpdX++; else if (SpdX) SpdX--; + + // Done processing, yield CPU and wait for start of next frame (VBlank) + vsync(); + } +} diff --git a/gbdk-lib/examples/gb/simple_physics/phys.c b/gbdk-lib/examples/gb/simple_physics/phys.c @@ -1,14 +1,12 @@ -#include <gb/gb.h> -#include <gb/sgb.h> - -#include <stdio.h> -#include <gbdk/console.h> - // A simple sub-pixel / fixed point example // Postion values are calculated as 16 bit numbers and their // lower 4 bits are dropped when applying them to the sprite -UINT8 sprite_data[] = { +#include <gbdk/platform.h> + +#include <stdint.h> + +const uint8_t sprite_data[] = { 0x3C,0x3C,0x42,0x7E,0x99,0xFF,0xA9,0xFF,0x89,0xFF,0x89,0xFF,0x42,0x7E,0x3C,0x3C, 0x3C,0x3C,0x42,0x7E,0xB9,0xFF,0x89,0xFF,0x91,0xFF,0xB9,0xFF,0x42,0x7E,0x3C,0x3C, 0x3C,0x3C,0x42,0x7E,0x99,0xFF,0x89,0xFF,0x99,0xFF,0x89,0xFF,0x5A,0x7E,0x3C,0x3C, @@ -16,17 +14,36 @@ UINT8 sprite_data[] = { }; -joypads_t joypads; - -// sprite coords -UINT16 PosX, PosY; -INT16 SpdX, SpdY; -UINT8 Jump; - -// main funxction +// update user input macro +#define INPUT_PROCESS (joy=old_joy,joy=joypad()) +// check putton down +#define INPUT_KEY(key) (joy&(key)) +// check button press +#define INPUT_KEYPRESS(key) ((joy & ~old_joy) & (key)) + +// coordinate translation macros +#define SUBPIXELS_TO_PIXELS(v) (v >> 4) +#define PIXELS_TO_SUBPIXELS(v) (v << 4) + +// physics constants +#define MAX_X_SPEED_IN_SUBPIXELS 64 +#define X_ACCELERATION_IN_SUBPIXELS 2 +#define MAX_Y_SPEED_IN_SUBPIXELS 64 +#define Y_ACCELERATION_IN_SUBPIXELS 2 +#define JUMP_ACCELERATION_IN_SUBPIXELS 32 + +// object coords +int16_t PosX, PosY; +// object speeds +int16_t SpdX, SpdY; + +// new and previous values of the joypad input +uint8_t joy = 0, old_joy; + +// main function void main(void) { // init palettes - BGP_REG = OBP0_REG = OBP1_REG = 0xE4; + BGP_REG = OBP0_REG = OBP1_REG = DMG_PALETTE(DMG_WHITE, DMG_LITE_GRAY, DMG_DARK_GRAY, DMG_BLACK); // load tile data into VRAM set_sprite_data(0, 4, sprite_data); @@ -36,56 +53,43 @@ void main(void) { // show bkg and sprites SHOW_BKG; SHOW_SPRITES; - - // init 2 joypads - joypad_init(1, &joypads); - PosX = PosY = 64 << 4; - Jump = SpdX = SpdY = 0; + PosX = PosY = PIXELS_TO_SUBPIXELS(64); + SpdX = SpdY = PIXELS_TO_SUBPIXELS(0); - while(1) { + while(TRUE) { // poll joypads - joypad_ex(&joypads); + INPUT_PROCESS; - // game object - if (joypads.joy0 & J_UP) { - SpdY -= 2; - if (SpdY < -64) SpdY = -64; - } else if (joypads.joy0 & J_DOWN) { - SpdY += 2; - if (SpdY > 64) SpdY = 64; + // check d-pad and change the object speed + if (INPUT_KEY(J_UP)) { + SpdY -= Y_ACCELERATION_IN_SUBPIXELS; + if (SpdY < -MAX_Y_SPEED_IN_SUBPIXELS) SpdY = -MAX_Y_SPEED_IN_SUBPIXELS; + } else if (INPUT_KEY(J_DOWN)) { + SpdY += Y_ACCELERATION_IN_SUBPIXELS; + if (SpdY > MAX_Y_SPEED_IN_SUBPIXELS) SpdY = MAX_Y_SPEED_IN_SUBPIXELS; } - if (joypads.joy0 & J_LEFT) { - SpdX -= 2; - if (SpdX < -64) SpdX = -64; - } else if (joypads.joy0 & J_RIGHT) { - SpdX += 2; - if (SpdX > 64) SpdX = 64; + if (INPUT_KEY(J_LEFT)) { + SpdX -= X_ACCELERATION_IN_SUBPIXELS; + if (SpdX < -MAX_X_SPEED_IN_SUBPIXELS) SpdX = -MAX_X_SPEED_IN_SUBPIXELS; + } else if (INPUT_KEY(J_RIGHT)) { + SpdX += X_ACCELERATION_IN_SUBPIXELS; + if (SpdX > MAX_X_SPEED_IN_SUBPIXELS) SpdX = MAX_X_SPEED_IN_SUBPIXELS; } - if ((joypads.joy0 & J_A) && (!Jump)) { - Jump = 3; - } - - // jump - if (Jump) { - SpdY -= 8; - if (SpdY < -32) SpdY = -32; - Jump--; + // check button keypress + if (INPUT_KEYPRESS(J_A)) { + SpdY = -JUMP_ACCELERATION_IN_SUBPIXELS; } + // change coordinates of the object PosX += SpdX, PosY += SpdY; // Translate to pixels and move sprite - // Downshift by 4 bits to use the whole number values - move_sprite(0, PosX >> 4, PosY >> 4); - - // decelerate - if (SpdY >= 0) { - if (SpdY) SpdY--; - } else SpdY ++; - if (SpdX >= 0) { - if (SpdX) SpdX--; - } else SpdX ++; + move_sprite(0, SUBPIXELS_TO_PIXELS(PosX), SUBPIXELS_TO_PIXELS(PosY)); + + // decelerate Y and X + if (SpdY < 0) SpdY++; else if (SpdY) SpdY--; + if (SpdX < 0) SpdX++; else if (SpdX) SpdX--; // Done processing, yield CPU and wait for start of next frame (VBlank) vsync();
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.