Encountered this post (Screen Wrap Detection)

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems.

Moderator: Moderators

Post Reply
User avatar
rainwarrior
Posts: 8062
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Encountered this post (Screen Wrap Detection)

Post by rainwarrior »

I do something similar to cut metasprites off at the edges of the screen. (This example assumes metasprites are never wider than 128 pixels.) Not using overflow, though.

Code: Select all

sprite_draw:
	; ... various sprite setup ...
	; test for edge
	lda sprite_x
	rol
	eor sprite_x
	bpl draw_at_edge
	; if (x & $80) ^ ((x<<1) & $40) then x >= 64 and x < 196, not at edge
draw_normal:
	; ... draw sprite tiles normally ...
	rts
draw_at_edge:
	; ... setup for tile drawing loop ...
	:
		; ... generate Y, tile, attribute ...
		lda (metasprite), Y ; this byte of metasprite is X-offset
		iny
		clc
		adc sprite_x
		sta tile_x ; tile_x = sprite_x + metasprite_x
		; wrap test
		; compute: (tile_x ^ sprite_x) & $80
		; if the high bit of tile and sprite X do not match, it has wrapped
		eor sprite_x
		bpl @not_wrap
		@wrap:
			; ... skip tile, it has wrapped ...
		@not_wrap:
			; ... add tile, it is fine ....
		; ... continue loop ...
	rts
Last edited by rainwarrior on Thu May 21, 2015 10:19 am, edited 1 time in total.
User avatar
rainwarrior
Posts: 8062
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Encountered this post (Screen Wrap Detection)

Post by rainwarrior »

Actually, it's kinda funny how the overflow bit is used for the example you posted, since it's normal for a signed comparison to involve an eor #$80. Kind of what you expect to be doing for signed operations anyway, just applied here in a bit of clever code that was thinking about a different problem (that wasn't really as different as it looked).
User avatar
Movax12
Posts: 529
Joined: Sun Jan 02, 2011 11:50 am

Re: Encountered this post (Screen Wrap Detection)

Post by Movax12 »

I do something very similar for metasprites at screen edge too.
User avatar
tokumaru
Posts: 12106
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Encountered this post (Screen Wrap Detection)

Post by tokumaru »

Nice trick! I too did something similar for clipping metasprites, but this method is simpler. This is inside a playfield that's smaller than 256x256 pixels though, meaning that all coordinates are inside the screen. In a game that scrolls, the initial coordinate might very well be outside the screen, and enter the screen after the delta is applied. This might need some extra logic, and the high byte of the coordinate may have to be considered at some point.
tepples
Posts: 22345
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Encountered this post (Screen Wrap Detection)

Post by tepples »

It'd still be useful for, say, destroying things that leave the screen in a scrolling game that uses screen-space coordinates for everything like Contra does.
User avatar
tokumaru
Posts: 12106
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Encountered this post (Screen Wrap Detection)

Post by tokumaru »

tepples wrote:a scrolling game that uses screen-space coordinates for everything
I can't get over how insane that is.
User avatar
rainwarrior
Posts: 8062
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Encountered this post (Screen Wrap Detection)

Post by rainwarrior »

Yeah, once you've got 16-bit positions, detecting wrap is usually a really straightforward test of the high byte.
tepples
Posts: 22345
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Encountered this post (Screen Wrap Detection)

Post by tepples »

Once you've got 16-bit positions but 8-bit velocities, or 24-bit positions but 16-bit velocities, you still need a check on the high byte to determine whether the position has wrapped from the beginning of the level to the end of the level.
Post Reply