User Tools

Site Tools


base:moving_sprites

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

base:moving_sprites [2015-04-17 04:33] (current)
Line 1: Line 1:
 +====== Moving sprites / Sorting movement from VIC update ======
 +by Achim
 +
 +===== x+msb =====
 +
 +Moving sprites can be annoying in terms of msb handling. To avoid the msb issue you usually use tables for moving the sprites and let a small routine update the VIC registers every frame. ​
 +<​code>​
 +spritey:​ .byte $00, $00, $00, $00, $00, $00, $00, $00
 +spritex:​ .byte $00, $00, $00, $00, $00, $00, $00, $00
 +spritemsb:​ .byte $00, $00, $00, $00, $00, $00, $00, $00
 +spritecolor:​ ...
 +spritepointer:​ ...
 +</​code>​
 +Now simple 16bit additions/​subtractions apply to moving sprites. The main program can ignore the msb handling.
 +
 +Here's a small routine to update VIC registers. Should be called by an irq. Preferably before VIC starts to draw the next frame.
 + 
 +<​code>​
 + ldx #$07
 + ldy #$0e
 +
 +loop: lda srpitey,x
 + sta $d001,​y //​write y
 + lda spritex,x
 + sta $d000,​y //​write x
 + lda spritemsb,x
 + cmp #$01 //no msb=carry clear  / msb=carry set
 + rol $d010 //​carry -> $d010, repeat 8 times and all bits are set
 +
 + lda spritecolor,​x
 + sta $d027,x
 + lda spritepointer,​x
 + sta $07f8,​x //​screen at $0400
 +
 + dey
 + dey
 + dex
 + bpl loop
 +</​code>​
 +
 +===== Oldschool: x*2 =====
 +This allows to use 8bit calculations for moving the sprite on x axis. No need for a msb table anymore. On the downside sprites can only be moved with 2px/frame.
 +<​code>​
 +spritey:​ .byte $00, $00, $00, $00, $00, $00, $00, $00
 +spritex:​ .byte $00, $00, $00, $00, $00, $00, $00, $00
 +spritecolor:​ ...
 +spritepointer:​ ...
 +</​code>​
 +Update VIC registers:
 +<​code>​
 + ldx #$07
 + ldy #$0e
 +
 +loop: lda srpitey,x
 + sta $d001,​y //​write y
 + lda spritex,x
 + asl //​x*2>​$ff -> carry set=msb, x*2<=$ff -> carry clear=no msb
 + sta $d000,​y //​write x*2
 + rol $d010 //​carry -> $d010
 +
 + lda spritecolor,​x
 + sta $d027,x
 + lda spritepointer,​x
 + sta $07f8,​x //​screen at $0400
 +
 +        dey
 + dey
 + dex
 + bpl loop
 +</​code>​
  
base/moving_sprites.txt ยท Last modified: 2015-04-17 04:33 (external edit)