
;******************************
;*
;* drawHUD
;* 
;* r0 = player data
;* r1 = map data
;*	
	
drawHUD

	;prolog

	push	r0
	push	r1
	push	r2
	push	r3
	push	r4
	push	r5
	push	r6
	push	r7
	
	push	r0	;store player struct
	
	;draw hud
	
	dexti	>hudBobData
	movei	r6,hudBobData
	
	dexti	>drawFrame
	movei	r7,drawFrame
	nop
	rqldi	r7,0
	rqldi	r7,1
	nop
	ld	r4	;high
	ld	r5	;low
	nop
	
	ssto	csDestAdrLow
	stwo	r6,r5
	ssto	csDestAdrHigh
	stwo	r6,r4
	
	gpci	r7,2		;next 4 + X
	br	drawBob		;br	copySolid16xN
	or	r0,r6,r6	;delay slot
	nop	;delay slot
	nop			;delay slot
	nop			;delay slot	
	
	rqpop
	nop
	nop
	pop	r0	;player struct
	nop
	
	rqldi	r0,plEnergy
	rqldi   r0,plLives
	nop
	ld	r3	;energy
	ld	r4	;lives
	nop
	
	dexti	>drawEnergy
	movei	r2,drawEnergy
	gpci	r7,2
	jmpi	r2,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	
	push	r1	;store map struct
	
	;draw lives ... text
	
	dexti	>drawChar8x8
	movei	r2,drawChar8x8
	
	dexti	>setCharDrawer
	movei	r3,setCharDrawer
	gpci	r7,2
	jmpi	r3,0
	movei	r0,globalFont8x8	;delay slot
	movei	r1,>>globalFont8x8	;delay slot
	moveih	r0,>globalFont8x8	;delay slot
	moveih	r1,>>>globalFont8x8	;delay slot
	
	push	r4	;push lives
	
	dexti	>drawTextFormated
	movei	r3,drawTextFormated
	gpci	r7,2		;next 4 + X
	jmpi	r3,0
	movei	r0,hudText1	;delay slot
	movei	r1,7		;delay slot
	moveih	r0,>hudText1	;delay slot
	movei	r2,1		;delay slot
	
	rqpop
	rqpop
	nop
	pop	r4
	pop	r1		;restore map struct
	
	;get living enemy count
	
	dexti	>getLivingEnemyCount
	movei	r6,getLivingEnemyCount	;(need r1 as map struct)
	gpci	r7,2
	jmpi	r6,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;r0 = living enemys
	
	;draw enemys ... text
	
	push	r0	;store enmy count
	
	dexti	>drawTextFormated
	movei	r3,drawTextFormated
	gpci	r7,2		;next 4 + X
	jmpi	r3,0
	movei	r0,hudText2	;delay slot
	movei	r1,7		;delay slot
	moveih	r0,>hudText2	;delay slot
	movei	r2,2		;delay slot
	
	rqpop
	nop
	nop
	nop
	
	
	;epilog
	
	rqpop
	rqpop
	rqpop
	poparqp	r7
	poparqp	r6
	poparqp	r5
	pop	r4
	pop	r3
	pop	r2
	
	rqpop
	rqpop
	jmpi	r7,0
	pop	r1	;delay slot
	pop	r0	;delay slot
	nop		;delay slot
	nop		;delay slot

;******************************
;*
;* drawEnergy
;* 
;* r3 = energy (0..255)
;*	
	
	
drawEnergy

	;prolog

	push	r0
	push	r1
	push	r2
	push	r3
	push	r4
	push	r5
	push	r6
	push	r7
	
	;get destination frame
	
	dexti	>drawFrame
	movei	r7,drawFrame
	nop
	rqldi	r7,0
	rqldi	r7,1
	nop
	ld	r4	;high
	ld	r5	;low
	
	movei	r1,6	;y offset
	movei	r0,139	;x offset
	
	dexti	1
	muli	r1,$88	;y * screen width (392)
	nop
	add	r0,r0,r1
	nop
	add	r5,r5,r0
	addtqi	r4,r4,0	;to dest
	
	;
	
	
	or	r1,r3,r3		;r1 (width) = energy	
	movei	r0,$ff
	
	;draw bar r1 = width (0..23) , r4,r5 = dest adr
	
	.def	energyBarL1	0
	.def	energyBarL2	$188
	.def	energyBarL3	$310
	.def	energyBarL4	$498
	.def	energyBarL5	$620
	
	cmplosi	r1,1	
	brts	drawEnergyBarZero
	dexti	>energyColors	;delay slot
	movei	r3,energyColors	;delay slot
	subi	r1,1		;delay slot
	movei	r2,0		;delay slot
	nop
	
drawEnergyBarLoop	
	
	rqld	r3,r2
	addi	r2,1
	nop
	ld	r0		;get color
	
	esadr	r4,r5
	
	est	r0,energyBarL1
	dexti	3>energyBarL2
	est	r0,energyBarL2
	dexti	3>energyBarL3
	est	r0,energyBarL3
	dexti	3>energyBarL4
	est	r0,energyBarL4
	dexti	3>energyBarL5
	est	r0,energyBarL5
	
	subi	r1,1
	brts	drawEnergyBarLoop
	addi	r5,1	;delay slot
	addtqi	r4,r4,0	;delay slot
	nop		;delay slot
	nop		;delay slot
	
drawEnergyBarZero

	;epilog
	
	rqpop
	rqpop
	rqpop
	poparqp	r7
	poparqp	r6
	poparqp	r5
	pop	r4
	pop	r3
	pop	r2
	
	rqpop
	rqpop
	jmpi	r7,0
	pop	r1	;delay slot
	pop	r0	;delay slot
	nop		;delay slot
	nop		;delay slot

hudText1

	.stringz	"LIVES  %u"
hudText2
	.stringz	"ENEMYS %u"	
	
hudBobData	.data	20,4,9,23		;bob x,y,width16,height
		.data	$0000,$0000,392,225	;dest adr, width, height
		.data	0,0			;sx,sy
		worduc	>>globalHud		;source, 
		worduc	globalHud
		word	144			;width
		word	copySolidTransparent16xN
		word	0			;flip
		.data	0,0,0,0,0		;tmps
		
		
energyColors

	    word    $0E10  ;at 00000000
	    word    $0E20  ;at 00000001
	    word    $0E30  ;at 00000002
	    word    $0E40  ;at 00000003
	    word    $0E50  ;at 00000004
	    word    $0E60  ;at 00000005
	    word    $0D70  ;at 00000006
	    word    $0D70  ;at 00000007
	    word    $0D80  ;at 00000008
	    word    $0D80  ;at 00000009
	    word    $0C80  ;at 00000010
	    word    $0C90  ;at 00000011
	    word    $0C90  ;at 00000012
	    word    $0CA0  ;at 00000013
	    word    $0CA0  ;at 00000014
	    word    $0CB0  ;at 00000015
	    word    $0BC0  ;at 00000016
	    word    $0BC0  ;at 00000017
	    word    $0BD0  ;at 00000018
	    word    $0BD0  ;at 00000019
	    word    $0BE0  ;at 00000020
	    word    $0AE0  ;at 00000021
	    word    $0AF0  ;at 00000022
	    word    $0BF0  ;at 00000023
	