;
; Title:        Vector Demo
; Author:       Dean Belfield
; Started:	30/06/2012
; Last Updated:	05/07/2012
;
; Requires:	vector.asm, output.asm, keyboard.asm, screen_buffer.asm, sound.asm, sprite.asm, math.asm
;
; Modinfo:
;
; 05/07/2012:	Pressing Space now toggles cursor between points; added circle draw
;

Code_Start:		EQU &8000

ORG			Code_Start

include "../lib/vector.asm"
include "../lib/output.asm"
include "../lib/keyboard.asm"
include "../lib/screen_buffer.asm"
include "../lib/sound.asm"
include "../lib/sprite.asm"
include "../lib/math.asm"

MAIN:			DI
			LD A,38h
			CALL Clear_Screen
			LD HL,Interrupt
			LD IX,&FFF0
			LD (IX+04h),&C3            ; Opcode for JP
			LD (IX+05h),L
			LD (IX+06h),H
			LD (IX+0Fh),&18            ; Opcode for JR; this will do JR to FFF4h
			LD A,&39
			LD I,A
			LD SP,&FFF0
			IM 2
			EI
	
LOOP:			HALT
			JR LOOP

; Draw cross, offset 7 pixels so centre is at pixel coord
;
Draw_Cross:		LD A,C
			SUB 7
			LD C,A
			LD A,B
			SUB 7
			LD B,A
			LD DE,Sprite_Cross
			JP Render_Sprite

; Clear cross, offset 7 pixels so centre is at pixel coord
;
Clear_Cross:		LD A,C
			SUB 7
			LD C,A
			LD A,B
			SUB 7
			LD B,A
			JP Clear_Sprite

Debounce_Key:		DB &FF
Position:		DW &5070,&6080,&7060

; Handle user input
;
User_Interface:		CALL Read_Keyboard		; Read the keyboard
			PUSH AF				; Stack the keypress
			JR Z,User_Interface_End		; No keys pressed; jump to end

			LD DE,(Position + 0)		; Get the cross position)
			CP "Q"
			JR NZ, $+3			; Saves lots of labels; skips 3 bytes (the JR and the DEC)
			DEC D
			CP "A"
			JR NZ, $+3
			INC D
			CP "O"
			JR NZ, $+3
			DEC E
			CP "P"
			JR NZ, $+3
			INC E
			LD (Position + 0),DE		; Store the new cross position

			LD HL,Debounce_Key		; Check whether we've pressed this key before
			CP (HL)				
			JR Z,User_Interface_End		; Yes we have, so skip the debounced keys

			CP " "				; If space pressed, then rotate the cross positions
			JR NZ,User_Interface_End
			LD HL,(Position + 0)
			LD DE,(Position + 2)
			LD BC,(Position + 4)
			LD (Position + 2),HL
			LD (Position + 4),DE
			LD (Position + 0),BC

User_Interface_End:	POP AF				; Unstack the keypress
			LD (Debounce_Key),A		; Store for debounce 
			RET

;
; Interrupt routine
;
Interrupt:		DI
			PUSH AF
			PUSH BC
			PUSH DE
			PUSH HL
			PUSH IX
			EXX
			EX AF,AF'
			PUSH AF
			PUSH BC
			PUSH DE
			PUSH HL
			PUSH IY

			LD A,(Debounce_Key)
			AND A
			JR Z,Interrupt_End

			LD A,2
			OUT (254),A
			CALL Clear_Screen_Fast
			LD A,3
			OUT (254),A
			LD BC,(Position + 0)
			CALL Draw_Cross
			LD A,4
			OUT (254),A
			LD IY,Position
			CALL Draw_Triangle
			LD A,5
			OUT (254),A

			LD A,(Position + 2)
			LD H,A
			LD A,(Position + 0)
			SUB H
			JR NC,$+3
			NEG
			LD H,A
			LD L,A
			CALL Multiply
			PUSH HL
			LD A,(Position + 3)
			LD H,A
			LD A,(Position + 1)
			SUB H
			JR NC,$+3
			NEG
			LD H,A
			LD L,A
			CALL Multiply
			POP DE
			ADD HL,DE
			CALL SQRT
			LD BC,(Position + 2)
			CALL Draw_Circle
			LD A,6
			OUT (254),A

Interrupt_End:		Call User_Interface
			LD A,7
			OUT (254),A

			POP IY
			POP HL
			POP DE
			POP BC
			POP AF
			EXX
			EX AF,AF'
			POP IX
			POP HL
			POP DE
			POP BC
			POP AF
			EI
			RET

Sprite_Cross:		DW Sprite_Cross_0,Sprite_Cross_1,Sprite_Cross_2,Sprite_Cross_3,Sprite_Cross_4,Sprite_Cross_5,Sprite_Cross_6,Sprite_Cross_7

Sprite_Cross_0:		DB %00000001,%00000000,%00000000
			DB %00000001,%00000000,%00000000
			DB %00000001,%00000000,%00000000
			DB %00000001,%00000000,%00000000
			DB %00000001,%00000000,%00000000
			DB %00000001,%00000000,%00000000
			DB %00000000,%00000000,%00000000
			DB %11111100,%01111110,%00000000
			DB %00000000,%00000000,%00000000
			DB %00000001,%00000000,%00000000
			DB %00000001,%00000000,%00000000
			DB %00000001,%00000000,%00000000
			DB %00000001,%00000000,%00000000
			DB %00000001,%00000000,%00000000
			DB %00000001,%00000000,%00000000
			DB %00000000,%00000000,%00000000

Sprite_Cross_1:		DB %00000000,%10000000,%00000000
			DB %00000000,%10000000,%00000000
			DB %00000000,%10000000,%00000000
			DB %00000000,%10000000,%00000000
			DB %00000000,%10000000,%00000000
			DB %00000000,%10000000,%00000000
			DB %00000000,%00000000,%00000000
			DB %01111110,%00111111,%00000000
			DB %00000000,%00000000,%00000000
			DB %00000000,%10000000,%00000000
			DB %00000000,%10000000,%00000000
			DB %00000000,%10000000,%00000000
			DB %00000000,%10000000,%00000000
			DB %00000000,%10000000,%00000000
			DB %00000000,%10000000,%00000000
			DB %00000000,%00000000,%00000000

Sprite_Cross_2:		DB %00000000,%01000000,%00000000
			DB %00000000,%01000000,%00000000
			DB %00000000,%01000000,%00000000
			DB %00000000,%01000000,%00000000
			DB %00000000,%01000000,%00000000
			DB %00000000,%01000000,%00000000
			DB %00000000,%00000000,%00000000
			DB %00111111,%00011111,%10000000
			DB %00000000,%00000000,%00000000
			DB %00000000,%01000000,%00000000
			DB %00000000,%01000000,%00000000
			DB %00000000,%01000000,%00000000
			DB %00000000,%01000000,%00000000
			DB %00000000,%01000000,%00000000
			DB %00000000,%01000000,%00000000
			DB %00000000,%00000000,%00000000

Sprite_Cross_3:		DB %00000000,%00100000,%00000000
			DB %00000000,%00100000,%00000000
			DB %00000000,%00100000,%00000000
			DB %00000000,%00100000,%00000000
			DB %00000000,%00100000,%00000000
			DB %00000000,%00100000,%00000000
			DB %00000000,%00000000,%00000000
			DB %00011111,%10001111,%11000000
			DB %00000000,%00000000,%00000000
			DB %00000000,%00100000,%00000000
			DB %00000000,%00100000,%00000000
			DB %00000000,%00100000,%00000000
			DB %00000000,%00100000,%00000000
			DB %00000000,%00100000,%00000000
			DB %00000000,%00100000,%00000000
			DB %00000000,%00000000,%00000000

Sprite_Cross_4:		DB %00000000,%00010000,%00000000
			DB %00000000,%00010000,%00000000
			DB %00000000,%00010000,%00000000
			DB %00000000,%00010000,%00000000
			DB %00000000,%00010000,%00000000
			DB %00000000,%00010000,%00000000
			DB %00000000,%00000000,%00000000
			DB %00001111,%11000111,%11100000
			DB %00000000,%00000000,%00000000
			DB %00000000,%00010000,%00000000
			DB %00000000,%00010000,%00000000
			DB %00000000,%00010000,%00000000
			DB %00000000,%00010000,%00000000
			DB %00000000,%00010000,%00000000
			DB %00000000,%00010000,%00000000
			DB %00000000,%00000000,%00000000

Sprite_Cross_5:		DB %00000000,%00001000,%00000000
			DB %00000000,%00001000,%00000000
			DB %00000000,%00001000,%00000000
			DB %00000000,%00001000,%00000000
			DB %00000000,%00001000,%00000000
			DB %00000000,%00001000,%00000000
			DB %00000000,%00000000,%00000000
			DB %00000111,%11100011,%11110000
			DB %00000000,%00000000,%00000000
			DB %00000000,%00001000,%00000000
			DB %00000000,%00001000,%00000000
			DB %00000000,%00001000,%00000000
			DB %00000000,%00001000,%00000000
			DB %00000000,%00001000,%00000000
			DB %00000000,%00001000,%00000000
			DB %00000000,%00000000,%00000000

Sprite_Cross_6:		DB %00000000,%00000100,%00000000
			DB %00000000,%00000100,%00000000
			DB %00000000,%00000100,%00000000
			DB %00000000,%00000100,%00000000
			DB %00000000,%00000100,%00000000
			DB %00000000,%00000100,%00000000
			DB %00000000,%00000000,%00000000
			DB %00000011,%11110001,%11111000
			DB %00000000,%00000000,%00000000
			DB %00000000,%00000100,%00000000
			DB %00000000,%00000100,%00000000
			DB %00000000,%00000100,%00000000
			DB %00000000,%00000100,%00000000
			DB %00000000,%00000100,%00000000
			DB %00000000,%00000100,%00000000
			DB %00000000,%00000000,%00000000

Sprite_Cross_7:		DB %00000000,%00000010,%00000000
			DB %00000000,%00000010,%00000000
			DB %00000000,%00000010,%00000000
			DB %00000000,%00000010,%00000000
			DB %00000000,%00000010,%00000000
			DB %00000000,%00000010,%00000000
			DB %00000000,%00000000,%00000000
			DB %00000001,%11111000,%11111100
			DB %00000000,%00000000,%00000000
			DB %00000000,%00000010,%00000000
			DB %00000000,%00000010,%00000000
			DB %00000000,%00000010,%00000000
			DB %00000000,%00000010,%00000000
			DB %00000000,%00000010,%00000000
			DB %00000000,%00000010,%00000000
			DB %00000000,%00000000,%00000000

Code_Length:		EQU $-Code_Start+1