Skip to content

Commit

Permalink
Implement partial text drawing
Browse files Browse the repository at this point in the history
Text can now be drawn while the PPU is turned on.  One tile is written
to the PPU per frame.  No new commands are added for this specifically.

Added the ClearText scene command to clear two whole rows of tiles.
This includes the ClearTileRow function in the text engine to implement
clearing the CHR RAM in an unrolled loop.
  • Loading branch information
zorchenhimer committed Jul 10, 2022
1 parent 3e66321 commit e9f7988
Show file tree
Hide file tree
Showing 9 changed files with 211 additions and 24 deletions.
Binary file modified images/font.aseprite
Binary file not shown.
Binary file modified images/font.bmp
Binary file not shown.
9 changes: 7 additions & 2 deletions main.asm
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ CHUNK_DONE = 0 ; no more chunks
; Arguments: [start PPU address] [text] $00
DrawTextFromTable

; Clears out two full rows of tiles. Takes two
; arguments, one for each tile ID to start from.
ClearText

; Turn off the PPU and enable commands to draw
; to the screen outside of NMI
TurnOffPPU
Expand All @@ -149,8 +153,8 @@ CHUNK_DONE = 0 ; no more chunks
SetPalette

; TODO: rewrite these functions
TvAttr
StaticAttr
;TvAttr
;StaticAttr

; Clear an attribute table to $00
ClearAttr0
Expand Down Expand Up @@ -219,6 +223,7 @@ sf_ExitRoutine: .res 1
sf_TextStartTiles: .res 2 ; Tile IDs where the two text areas start
sf_DialogueTable: .res 2
sf_DialogueSwitch: .res 1 ; used in sf_DrawText and sf_DrawTextFromTable
sf_LiveSwitch: .res 1 ; Only draw text in NMI

Sleeping: .res 1

Expand Down
8 changes: 4 additions & 4 deletions maps/main-boards.tmx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.2" tiledversion="1.3.1" orientation="orthogonal" renderorder="right-down" compressionlevel="0" width="24" height="12" tilewidth="8" tileheight="8" infinite="0" nextlayerid="17" nextobjectid="1">
<map version="1.9" tiledversion="1.9.0" orientation="orthogonal" renderorder="right-down" compressionlevel="0" width="24" height="12" tilewidth="8" tileheight="8" infinite="0" nextlayerid="17" nextobjectid="1">
<tileset firstgid="1" source="MetaTiles.tsx"/>
<layer id="3" name="board-00" width="24" height="12" visible="0">
<layer id="3" name="board-00" width="24" height="12">
<properties>
<property name="gravity" type="bool" value="false"/>
<property name="health" type="int" value="0"/>
Expand All @@ -20,7 +20,7 @@
23,0,23,0,23,0,23,0,23,0,0,0,23,0,23,0,23,0,23,0,23,0,23,0,
23,0,23,0,23,0,23,0,23,0,0,0,23,0,23,0,23,0,23,0,23,0,23,0,
23,0,23,0,23,0,23,0,23,0,0,0,23,0,23,0,23,0,23,0,23,0,23,0,
23,0,23,0,23,0,23,0,23,0,0,0,23,0,23,0,23,0,23,0,23,0,23,0
23,0,23,0,23,0,23,0,3,0,0,0,4,0,23,0,23,0,23,0,23,0,23,0
</data>
</layer>
<layer id="2" name="board-01" width="24" height="12" visible="0">
Expand Down Expand Up @@ -275,7 +275,7 @@
38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38
</data>
</layer>
<layer id="9" name="board-15" width="24" height="12">
<layer id="9" name="board-15" width="24" height="12" visible="0">
<properties>
<property name="gravity" type="bool" value="true"/>
</properties>
Expand Down
37 changes: 36 additions & 1 deletion scene-data.asm
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@ Scene_Intro:
.byte 2

.byte SceneCmd::TurnOffPPU

; Clear out the text from before
.byte SceneCmd::ClearText
.byte $90 ; start tile ID
.byte $A0 ; start tile ID

.byte SceneCmd::SetNametable0

.byte SceneCmd::DrawFullScene
Expand Down Expand Up @@ -233,8 +239,37 @@ Scene_Intro:
.byte SceneCmd::SetFramePointer
.word $0000

.byte SceneCmd::PrepareText
.word $22A8 ; NT address
.byte 0 ; ID
.byte $90 ; start tile ID

.byte SceneCmd::PrepareText
.word $22C8 ; NT address
.byte 1 ; ID
.byte $A0 ; start tile ID

.byte SceneCmd::DrawTextFromTable
.byte 0 | $80
.byte Dialogue::CyberPiss_1

.byte SceneCmd::DrawTextFromTable
.byte 1 | $80
.byte Dialogue::CyberPiss_2

.byte SceneCmd::WaitSeconds
.byte 3

.byte SceneCmd::DrawTextFromTable
.byte 0 | $80
.byte Dialogue::CyberPiss_3

.byte SceneCmd::DrawTextFromTable
.byte 1 | $80
.byte Dialogue::CyberPiss_4

.byte SceneCmd::WaitSeconds
.byte 10
.byte 5

.byte SceneCmd::GotoInit

Expand Down
102 changes: 86 additions & 16 deletions scene-engine.asm
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,15 @@ scene_Functions:
.word sf_SetUnskippable-1
.word sf_DrawText-1
.word sf_DrawTextFromTable-1
.word sf_ClearText-1
.word sf_TurnOffPPU-1
.word sf_TurnOnPPU-1
.word sf_FillNametable-1
.word sf_LoadChr-1
.word sf_PadSprites-1
.word sf_SetPalette-1
.word WriteNewsAttr-1
.word WriteStaticAttributes-1
;.word WriteNewsAttr-1
;.word WriteStaticAttributes-1
.word ClearAttrTable0-1
.word ClearAttrTable1-1
.word ClearAttrTable2-1
Expand Down Expand Up @@ -243,7 +244,10 @@ sf_SetUnskippable:
rts

sf_TurnOffPPU:
.Disable_Drawing
;.Disable_Drawing
lda #0
sta sf_PpuOn
jsr WaitForNMI
rts

sf_TurnOnPPU:
Expand Down Expand Up @@ -409,6 +413,48 @@ sf_SetExitRoutine:
sta sf_ExitRoutine
rts

sf_ClearText:
; Get the CHR start tile ID
lda (AddressPointer3), y
iny
tax

; TileID -> CHR Address
lda data_Mult16_A, x
sta AddressPointer0+0
lda data_Mult16_B, x
sta AddressPointer0+1

; Clear all the CHR tiles in PPU RAM
;jsr WriteBufferUnrolled
ldx #$FF
lda #$00
jsr ClearTileRow

; Do it again, lmao

; Get the CHR start tile ID
lda (AddressPointer3), y
iny
tax

; TileID -> CHR Address
lda data_Mult16_A, x
sta AddressPointer0+0
lda data_Mult16_B, x
sta AddressPointer0+1

; Clear all the CHR tiles in PPU RAM
;jsr WriteBufferUnrolled
ldx #$FF
lda #$00
jsr ClearTileRow

jsr TextClearStringBuffer
jsr TextClearBuffer

rts

sf_DrawText:
;bit $2002
lda #$00
Expand Down Expand Up @@ -440,25 +486,26 @@ sf_DrawText:
lda sf_DialogueSwitch
beq :+

; Backup pointer
lda AddressPointer3+0
pha
lda AddressPointer3+1
pha

; Lookup pointer to text
iny
lda (AddressPointer3), y
asl a
tax
lda DialogueIndex+0, x
sta AddressPointer4+0
lda DialogueIndex+1, x
sta AddressPointer4+1

; Backup pointer
lda AddressPointer3+0
pha
lda AddressPointer3+1
pha

; Backup Y
iny
tya
pha
ldy #0
ldy #$FF

; Grab pointer to text
lda AddressPointer4+0
Expand Down Expand Up @@ -501,16 +548,33 @@ sf_DrawText:
;jsr TextPrepare_v2
jsr TextPrepare_v3

;lda #16 ; number of chars
ldx #0
;ldx #$FF

lda sf_PpuOn
beq @fulldraw
lda #16 ; CHR count

jsr PreparePartialTextWrite

@partial:
jsr WaitForNMI
jsr WritePartialTextBuffer
dec ChrCount
bne @partial
jmp @drawdone

@fulldraw:
lda AddressPointer6+0
sta AddressPointer0+0
lda AddressPointer6+1
sta AddressPointer0+1

lda #16 ; number of chars
ldx #0
;ldx #$FF
lda #16 ; CHR count
jsr WriteTextBuffer

@drawdone:
jsr TextClearBuffer

pla
Expand Down Expand Up @@ -582,7 +646,7 @@ sf_GotoInit:
NMI_Scene:
pha
lda sf_PpuOn
beq :+
beq @ppuoff
;lda #0
;sta sf_PpuOn

Expand All @@ -602,7 +666,13 @@ NMI_Scene:
lda #0
sta $2005
sta $2005
:
dec Sleeping
pla
rti

@ppuoff:
lda #0
sta $2001
dec Sleeping
pla
rti
1 change: 1 addition & 0 deletions screen-decode.asm
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ WriteTvAttr:
sta $2007
sta $2007
rts

WriteNewsAttr:
; 23CA
lda #$23
Expand Down
2 changes: 2 additions & 0 deletions text-engine-ram.asm
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ WorkingRuneB: .res 8

.segment "MAINRAM"

BgTextPal: .res 1

;ChrAddr: .res 2
ChrCount: .res 1 ; Tile count, not byte count

Expand Down
76 changes: 75 additions & 1 deletion text-engine.asm
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,36 @@

SPACE_WIDTH = 5

; Clear a set of 16 tiles, starting at the address in AddressPointer0
; X holds values for first bit plane
; A holds values for second bit plane
ClearTileRow:
pha
bit $2002
lda AddressPointer0+1
sta $2006
lda AddressPointer0+0
sta $2006
pla

.repeat 16
.repeat 8
stx $2007
.endrepeat
.repeat 8
sta $2007
.endrepeat
.endrepeat
rts

WriteBufferUnrolled:
bit $2002
lda AddressPointer0+1
sta $2006
lda AddressPointer0+0
sta $2006

.repeat 21, B
.repeat 16, B
.repeat 8, C
lda ChrBuffer + ((B*8) + C)
sta $2007
Expand Down Expand Up @@ -250,6 +272,58 @@ WriteTextBuffer_LevelSelect:
sta TextEor
rts

; A has count
; X has the BG palette value thing in it
PreparePartialTextWrite:
sta ChrCount
txa
sta BgTextPal
lda #0
sta TextIdx
lda #$FF
sta TextEor
rts

; Similar to WriteTextBuffer, but only
; writes a few tiles. (one for now)
WritePartialTextBuffer:
bit $2002
lda AddressPointer6+1
sta $2006
lda AddressPointer6+0
sta $2006

ldx TextIdx

.repeat 8,i
lda ChrBuffer+i, x
eor TextEor
sta $2007
.endrepeat

txa
clc
adc #8
sta TextIdx

; Second plane
lda BgTextPal
.repeat 8
sta $2007
.endrepeat

clc
lda #16
adc AddressPointer6+0
sta AddressPointer6+0
bcc :+
inc AddressPointer6+1
:
lda #0
sta $2005
sta $2005
rts

; A has count
; X has the BG palette value thing in it
WriteTextBuffer:
Expand Down

0 comments on commit e9f7988

Please sign in to comment.