From 75a7576e1752c43ed6963de48d0cc022f125ef59 Mon Sep 17 00:00:00 2001 From: Forest Belton <65484+forestbelton@users.noreply.github.com> Date: Mon, 28 Jun 2021 23:45:37 -0400 Subject: [PATCH] Draw background --- src/bg.s | 120 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.s | 30 ++++--------- src/player.s | 7 ++- src/util.s | 2 +- 4 files changed, 134 insertions(+), 25 deletions(-) create mode 100644 src/bg.s diff --git a/src/bg.s b/src/bg.s new file mode 100644 index 0000000..c27de18 --- /dev/null +++ b/src/bg.s @@ -0,0 +1,120 @@ +INCLUDE "hardware.inc" + +; SCENE.Z80 +; +; Map Source File. +; +; Info: +; Section : ROM0 +; Bank : 0 +; Map size : 20 x 18 +; Tile set : C:\Users\case\Downloads\woods.gbr +; Plane count : 1 plane (8 bits) +; Plane order : Tiles are continues +; Tile offset : 0 +; Split data : No +; +; This file was generated by GBMB v1.8 + +sceneWidth EQU 20 +sceneHeight EQU 18 + +SECTION "BG0", ROM0 + +bg: +DB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00 +DB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00 +DB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00 +DB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00 +DB $01,$02,$02,$03,$00,$00,$00,$00,$01,$02 +DB $02,$02,$02,$02,$02,$0A,$03,$00,$00,$00 +DB $07,$09,$09,$08,$00,$00,$00,$00,$07,$09 +DB $09,$09,$09,$09,$09,$0B,$08,$00,$00,$00 +DB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00 +DB $00,$00,$00,$00,$00,$0C,$00,$00,$00,$00 +DB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00 +DB $00,$00,$00,$00,$00,$0A,$00,$00,$00,$00 +DB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00 +DB $00,$00,$00,$00,$00,$0B,$00,$00,$00,$00 +DB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00 +DB $00,$00,$00,$00,$00,$0C,$00,$00,$00,$00 +DB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00 +DB $00,$00,$00,$00,$00,$0A,$00,$00,$00,$00 +DB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00 +DB $00,$00,$00,$00,$01,$02,$02,$02,$02,$02 +DB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00 +DB $00,$00,$00,$00,$07,$09,$09,$09,$09,$09 +DB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00 +DB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00 +DB $00,$00,$00,$00,$00,$00,$01,$02,$02,$02 +DB $02,$02,$03,$00,$00,$00,$00,$00,$00,$00 +DB $00,$00,$00,$00,$00,$00,$07,$09,$09,$09 +DB $09,$09,$08,$00,$00,$00,$00,$00,$00,$00 +DB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00 +DB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00 +DB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00 +DB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00 +DB $01,$02,$02,$02,$02,$02,$02,$02,$02,$02 +DB $02,$02,$02,$02,$02,$02,$02,$02,$02,$02 +DB $04,$06,$06,$06,$06,$06,$06,$06,$06,$06 +DB $06,$06,$06,$06,$06,$06,$06,$06,$06,$06 +bg_end: + +bg_tiles: + DB $00,$00,$00,$00,$00,$00,$00,$00 + DB $00,$00,$00,$00,$00,$00,$00,$00 + DB $FF,$FF,$DF,$80,$FD,$80,$77,$40 + DB $3F,$3F,$22,$3D,$20,$3F,$28,$37 + DB $FF,$FF,$DF,$00,$FD,$00,$F7,$00 + DB $FF,$FF,$40,$BF,$01,$FE,$10,$EF + DB $FF,$FF,$FB,$01,$BF,$01,$EE,$02 + DB $FC,$FC,$44,$BC,$04,$FC,$14,$EC + DB $22,$3D,$30,$2F,$24,$3B,$20,$3F + DB $20,$3F,$32,$2D,$20,$3F,$28,$37 + DB $44,$BC,$0C,$F4,$24,$DC,$04,$FC + DB $04,$FC,$4C,$B4,$04,$FC,$14,$EC + DB $40,$BF,$02,$FD,$20,$DF,$00,$FF + DB $08,$F7,$40,$BF,$02,$FD,$10,$EF + DB $20,$3F,$22,$3D,$10,$1F,$0F,$0F + DB $00,$00,$00,$00,$00,$00,$00,$00 + DB $04,$FC,$44,$BC,$08,$F8,$F0,$F0 + DB $00,$00,$00,$00,$00,$00,$00,$00 + DB $40,$BF,$02,$FD,$20,$DF,$FF,$FF + DB $00,$00,$00,$00,$00,$00,$00,$00 + DB $FF,$FF,$FF,$81,$FF,$81,$FF,$81 + DB $FF,$FF,$FF,$FF,$FF,$FF,$FF,$81 + DB $FF,$81,$FF,$81,$FF,$FF,$FF,$FF + DB $FF,$FF,$FF,$81,$FF,$81,$FF,$81 + DB $FF,$FF,$FF,$FF,$FF,$FF,$FF,$81 + DB $FF,$81,$FF,$81,$FF,$FF,$FF,$FF +bg_tiles_end: + +BG_Init:: + ; copy map + ld e, 18 + ld bc, _SCRN0 + ld hl, bg +.copy_map_row: + ld d, 20 + call memcpy + dec e + jr z, .done + ld d, 32 - 20 +.skip: + ; skip over trailing part in vram + inc c + jr nz, .nocarry + inc b +.nocarry: + dec d + jr nz, .skip + jr .copy_map_row +.done: + + ; copy tiles + ld hl, bg_tiles + ld bc, _VRAM + ld d, bg_tiles_end - bg_tiles + call memcpy + + ret diff --git a/src/main.s b/src/main.s index a4cc98b..a13045e 100644 --- a/src/main.s +++ b/src/main.s @@ -21,38 +21,24 @@ start: res 7, [hl] res 2, [hl] - ; set tilemap to tile index 1 - ld hl, _SCRN0 - ld a, 1 - ld c, 32 -.loop0: - ld b, 32 -.loop1 - ld [hli], a - dec b - jr nz, .loop1 - dec c - jr nz, .loop0 - - ; clear 2nd sprite - ld hl, _VRAM + 16 - ld a, 0 - ld c, 16 - call memset - + call BG_Init call OAM_Init call Keys_Init call Player_Init + ; set palette + ld a, %11100100 + ld hl, rOBP0 + ld [hl], a + ld hl, rBGP + ld [hl], a + ; enable lcd, sprites and interrupts ld hl, rLCDC set 7, [hl] set 1, [hl] ei - ld hl, rOBP0 - ld [hl], %11100100 - .loop: ld hl, frame inc [hl] diff --git a/src/player.s b/src/player.s index c8ec48a..56a3bb6 100644 --- a/src/player.s +++ b/src/player.s @@ -11,12 +11,14 @@ Section "Player Code", ROM0 spriteData: INCBIN "png/player.2bpp" +DEF SPRITE_IDX EQU 32 + Player_Init:: ld a, 8 ld hl, _OAM + 1 ld [hl], a - ld bc, _VRAM8000 + ld bc, _VRAM8000 + SPRITE_IDX * 16 ld hl, spriteData ld d, 16 call memcpy @@ -26,8 +28,9 @@ Player_Init:: ld [hli], a ld a, 8 ld [hli], a - xor a + ld a, SPRITE_IDX ld [hli], a + xor a ld [hli], a ld bc, _OAMRAM diff --git a/src/util.s b/src/util.s index 24614c0..07705a2 100644 --- a/src/util.s +++ b/src/util.s @@ -9,9 +9,9 @@ waitForVblank:: ret ; Copies data between two regions -; @param d Size (in bytes) to copy. Must be >0 ; @param bc Pointer to the destination region ; @param hl Pointer to the source region +; @param d Size (in bytes) to copy. Must be >0 memcpy:: ld a, [hli] ld [bc], a