Link wrote:
I put off doing this for awhile, but now I've implemented it. Thanks, the code helped a lot. The next thing though is doing this in correspondence with my scrolling routine. That means I have to update the whole 240 bytes every time the game scrolls 8-pixels eithre way right? Is there a better way to do this?
Fortunately, there's no need to do so.
Instead you can treat things like a circular buffer, just like you would do tilemap scrolling. Admittedly it's somewhat favorable to wrap directly to a new line instead of the beggining of the current one, like the video hardware would.
Lets say you've got a small 3x3 (meta-)tile screen in it's initial (unscrolled state):
0 1 2
3 4 5
6 7 8
Instead of the whole 16-byte buffer when scrolling to the buffer left by one tile we simply replace to column that's been scrolled out:
1 2 3
4 5 6
7 8 a
And to scroll down we repeat the procedure but offset the buffer by a whole line instead of a single tile.
4 5 6
7 8 9
a b c
To avoid growing this buffer to infinity you can simply clip the index to a single byte (which is the fastest way to do things anyway). On a normal screen with a status bar and an extra tile row for each axis (for scrolling) this should still fit in a single 256-byte page.
In your collision lookup code you only have to keep track of the current scrolling offset, calculated through scroll_x+scroll_y*16, and add it to the lookup address.
Here's an updated version of my previous code:
Code:
;; the 16x15 byte collision array, occupies a full 256-byte page for natural wrapping
coltab = $300
;; the collision scroll offset, increase this by 1 to scroll left and by 16 to scroll up. this is also used as a starting point when updating the table during
colscroll = $400
;; input: x = sprite x-coord in pixels, y = sprite y-coord in pixels
;; output: a = collision byte
lookup:
;; divide x-coord by 16 to get the horizontal tile index
txa
lsr
lsr
lsr
lsr
sta tmp
;; divide y-coord by 16 to get it's tile index and multiply it by 16 again for the collision table lookup
;; (x >> 4) + (y >> 4) << 4 = (x >> 4) + (y & 0xf0)
tya
and #$f0
;; combine x and y coords into a byte offset
clc
adc tmp
;; compensate for scrolling by adding the current offset
adc colscroll
tax
;; lookup the byte and return it
lda coltab,x
rts