;license:MIT
;(c) 2023 by 4am
;
; drawing routines for Million Perfect Tiles Condensed
;
; Public functions:
; - DrawCondensedString
;

; VMW: commented, reformatted, minor changes, ca65 assembly
;	hacked up some more

;------------------------------------------------------------------------------
; DrawCondensedString
;
; in:    A/Y points to zero terminated string, with x-pos and y-pos at start
; out:   clobbers all registers & flags
;------------------------------------------------------------------------------
DrawCondensedString:

	; store the string location

	sta	OUTL
	sty	OUTH

DrawCondensedStringAgain:

	ldy	#0
	lda	(OUTL), Y		; get xpos
	sta	CH			; save the X column offset
	iny

	lda	(OUTL),Y		; get ypos
	tay

	; add two to string pointer
	clc
	lda	OUTL
	adc	#2
	sta	dcb_loop+1
	lda	#0
	adc	OUTH
	sta	dcb_loop+2

	; row0

	lda	hposn_low, Y			; get low memory offset
	clc
	adc	CH				; add in x-coord
	sta	dcb_row0+4
	lda	hposn_high, Y			; get high memory offset
	sta	dcb_row0+5			; save it out
	iny					; go to next row

	; row1

	lda	hposn_low, Y
	adc	CH
	sta	dcb_row1+4
	lda	hposn_high, Y
	sta	dcb_row1+5
	iny

	; row2

	lda	hposn_low, Y
	adc	CH
	sta	dcb_row2+4
	lda	hposn_high, Y
	sta	dcb_row2+5
	iny

	; row3

	lda	hposn_low, Y
	adc	CH
	sta	dcb_row3+4
	lda	hposn_high, Y
	sta	dcb_row3+5
	iny

	; row4

	lda	hposn_low, Y
	adc	CH
	sta	dcb_row4+4
	lda	hposn_high, Y
	sta	dcb_row4+5
	iny

	; row5

	lda	hposn_low, Y
	adc	CH
	sta	dcb_row5+4
	lda	hposn_high, Y
	sta	dcb_row5+5
	iny

	; row6

	lda	hposn_low, Y
	adc	CH
	sta	dcb_row6+4
	lda	hposn_high, Y
	sta	dcb_row6+5
	iny

	; row7

	lda	hposn_low, Y
	adc	CH
	sta	dcb_row7+4
	lda	hposn_high, Y
	sta	dcb_row7+5

	ldx	#0
dcb_loop:
dcb_loop_smc:
	ldy	$FDFD, X		; load next char into Y
	beq	dcb_done

	; unrolled loop to write out each line

dcb_row0:
	lda	CondensedRow0-$19, Y	; get 1-byte font row
	sta	$FDFD, X		; write out to graphics mem
dcb_row1:
	lda	CondensedRow1-$19, Y
	sta	$FDFD, X
dcb_row2:
	lda	CondensedRow2-$19, Y
	sta	$FDFD, X
dcb_row3:
	lda	CondensedRow3-$19, Y
	sta	$FDFD, X
dcb_row4:
	lda	CondensedRow4-$19, Y
	sta	$FDFD, X
dcb_row5:
	lda	CondensedRow5-$19, Y
	sta	$FDFD, X
dcb_row6:
	lda	CondensedRow6-$19, Y
	sta	$FDFD, X
dcb_row7:
	lda	CondensedRow7-$19, Y
	sta	$FDFD, X

	inx				; move to next

	bne	dcb_loop		; bra (well, as long as string
					;	is less than 255 chars)

dcb_done:

	; point to location after
	; need to be +3 currently as OUTL not updated earlier
	inx
	inx
	sec		; always add 1
	txa
	adc	OUTL
	sta	OUTL
	lda	#0
	adc	OUTH
	sta	OUTH

	rts

