; 'NES-sprite example'
;---------------------------------------------------------------------
; Implemented in ATALAN Programming language
; by Marcel Cevani
; 14.01.2011
; 
;---------------------------------------------------------------------
; This is example some sprites are displayed by sending the OAM data to the PPU chip.
; Please run the buildSprite.bat file, it append the gfx data to the rom
; Tnx to Patater for his nice Nes dev tutorials (http://www.patater.com/nes-asm-tutorials) ;)
; gfx test data is from playpower.org game hanuman quiz
; NROM:
; Max codesize   : 16Kib
; Max gfxsize    : 4Kib
; Emulator tested: fceux (2.1.4a) , needs new PPU enabled!
;---------------------------------------------------------------------
; NROM rom layout:
; Max codesize : 16Kib
; Max gfxsize  : 4Kib
;---------------------------------------------------------------------
; changed by Marcel Cevani 01.02.2011
;  - clean up souce code
;  - created some simple lib's for controlling sprites (no dma)
;  - changed gfx to Hanuman quiz
;  Tnx to Atalan the snake & rudla :)
;---------------------------------------------------------------------


use nes
use nes_sprite
use nes_mmc1
use nes_bg

xcoord:0..319
ycoord:0..239

point: 
	x:xcoord		; x screen coordinate
	y:ycoord		; y screen coordinate

; Hanuman sprite pal (taken from Hanuman Quiz game)
const hanuspritePal:sprPal = ($21,$16,$0f,$38,$21,$16,$38,$18,$21,$30,$2e,$06,$21,$17,$27,$28)
const hanusBGPal:bgPal = ($21,$16,$0f,$38,$21,$16,$38,$18,$21,$30,$2e,$06,$21,$17,$27,$28)


const hanuSpriteMulti1:array = ($04,$04,
				$00,$03,$00,$04,$00,$00,
				$05,$00,$06,$00,$09,$00,$0a,$00,  
				$0b,$00,$0c,$00,$0f,$00,$10,$00,
				$00,$13 $00,$14,$00,$00)


const hanuSpriteMulti2:array = ($04,$04,
				$00,$01,$00,$02,$00,$00, 
				$05,$00,$06,$00,$07,$00,$08,$00,  
				$0b,$00,$0c,$00,$0d,$00,$0e,$00,
				$00,$011,$00,$12,$00,$00)


const hanuSpriteMulti3:array = ($04,$04,
				$00,$00,$00,$00, 
				$05,$00,$15,$00,$16,$00,$17,$00 ,  
				$18,$00,$19,$00,$1a,$00,$1b,$00,
				$00,$00,$00,$00)

const  sprite1:array = (hanuSpriteMulti1,hanuSpriteMulti2,hanuSpriteMulti3)
const  sprite2:array = (hanuSpriteMulti3,hanuSpriteMulti2,hanuSpriteMulti1)

const  spriteArray:array = (sprite1 , sprite2 , sprite2 , sprite1)


out sprXPos:array(4) of byte
out sprYPos:array(4) of byte



; start of example
start_example@


waitvbl
waitvbl
waitvbl


; Init MMC1 Mapper

setMMC1Ctrl 24

setMMC1Prog 0

setMMC1Chr0 0

setMMC1Chr1 1
; set Display Mode
setDisplayMode %00001000 %00010000

; loading pal
waitvbl
sprLoadPal hanuspritePal
waitvbl
bgLoadPal hanusBGPal
; Scrool pos of sprite
frame:0..255
anim:0..255
xPos:0..255

anim  = 0
frame = 0

test1:adr
test2:adr
test3:adr

test1 = sprite1
test1 = sprite2
test1 = hanuSpriteMulti1
test1 = hanuSpriteMulti2
test1 = hanuSpriteMulti3



sprXPos(0) = 32
sprYPos(0) = 128

sprXPos(1) = 64
sprYPos(1) = 128

sprXPos(2) = 128
sprYPos(2) = 128

sprXPos(3) = 200
sprYPos(3) = 128

_LAST_FRAME_OAM_INDEX = 0

_OAM_INDEX = 0
main@
; wait until we are in vblank
waitvbl
PPU_DMA_ADR= 3
_LAST_FRAME_OAM_INDEX =_OAM_INDEX
_OAM_INDEX = 0

CPU_PROF_START = 1

frame = frame + 1

windowStartIndex = frame bitand 3


i:0..3
for i
    sprIndex = (windowStartIndex + i) bitand 3 
    setMultiSprite spriteArray sprXPos(sprIndex) sprYPos(sprIndex) sprIndex




;setMultiSprite spriteArray sprXPos(0) sprYPos(0) 0
;setMultiSprite spriteArray sprXPos(1) sprYPos(1) 0
;setMultiSprite spriteArray sprXPos(2) sprYPos(2) 0
;setMultiSprite spriteArray sprXPos(3) sprYPos(3) 0






;if anim = 0
;   setMultiSprite testAdrArray(0) 128+xPos 128 4 4
;if anim = 1
;   setMultiSprite test2 128+xPos 128 4 4
;if anim = 2
;   setMultiSprite test3 128+xPos 128 4 4
	

;frame= frame + 1
;if frame = 60
;	anim = anim + 1
;	frame = 0




if _LAST_FRAME_OAM_INDEX >_OAM_INDEX
	resetUnusedSprites


CPU_PROF_END = 1
xPos = xPos + 1
goto main


