Page 1 of 1

Rotating a sprite 90 degrees

Posted: Wed Oct 10, 2018 2:22 pm
by battagline
Is it possible to rotate a sprite by 90 degrees? Or do I need to do it in photoshop and include it in the .chr file that way?

Thanks

Re: Rotating a sprite 90 degrees

Posted: Wed Oct 10, 2018 2:26 pm
by lidnariq
The hardware does not support rotation by 90 degrees, no. Only horizontal and vertical flips, which together are equivalent to a 180 degree rotation.

Re: Rotating a sprite 90 degrees

Posted: Wed Oct 10, 2018 2:54 pm
by rainwarrior
If you're using CHR-RAM you could write some code to rotate the sprite tile before uploading it to the PPU. This would only really be helpful if you have a lot of stuff that needs a rotated form, though. Doesn't really solve any problems of e.g. pattern table management.

Re: Rotating a sprite 90 degrees

Posted: Wed Oct 10, 2018 3:27 pm
by dougeff
It'd be terribly slow.

Rotate 1 tile left 90°

LDY #0
LDX #0
Loop:
LDA Source, Y
Loop2:
LSR a
ROL Dest, X
INX
CPX #8
BNE Loop2
LDX #0
INY
CPY #8
BNE Loop
LDX #8
;x and y should be 8 now
Loop3:
LDA Source, Y
Loop4:
LSR a
ROL Dest, X
INX
CPX #16
BNE Loop4
LDX #8
INY
CPY #16
BNE Loop3

Edit, wrote the 2nd half.

Hasn't been tested. Only in my head.


Edit, and I think, rotate right 90° would be...replace LSR with ASL and ROL with ROR. (edited about 6 times)

Re: Rotating a sprite 90 degrees

Posted: Wed Oct 10, 2018 4:12 pm
by battagline
It sounds like the best thing to do is just rotate it 90 degrees photoshop and have 2 versions in the .chr

Thanks for your help, was just hoping there was some better way

Re: Rotating a sprite 90 degrees

Posted: Thu Oct 11, 2018 6:03 am
by dougeff
It works. I made a demo, with source. Also included flip tile H and flip tile V, which were easier.

It is very slow. About 2250 cycles per tile. (and only 300 cycles for flip V).

Re: Rotating a sprite 90 degrees

Posted: Thu Oct 11, 2018 6:36 am
by nesrocks
Would it make it faster to use a pre-calculated table of conversion? (I couldn't understand your code, where are you writing to?) edit: oh okay, it's the LSR and ROL. I'm rusty :)

Re: Rotating a sprite 90 degrees

Posted: Thu Oct 11, 2018 6:39 am
by Bregalad
I'm fairely sure at least one of the Dragon Quest/Warrior games does this (in software), could probably be 3 I don't remember. As this is a complex/slow operation they probably do it when loading the graphics on the screen, not on the fly.

Re: Rotating a sprite 90 degrees

Posted: Thu Oct 11, 2018 10:38 am
by lidnariq
dougeff wrote:It is very slow. About 2250 cycles per tile.
A partial unroll (replacing the inner X: lsr A / rol $0300,x / inx / cpx #8 / bne X) speeds up each row from 127 cycles to 64 cycles, speeding it up in total to just 1242 cycles.

Re: Rotating a sprite 90 degrees

Posted: Thu Oct 11, 2018 11:15 am
by rainwarrior
I would only think it should be useful if you need to mass rotate a bunch of tiles, e.g. if you wanted to rotate your world map or something like that. Could save a bunch of ROM space, maybe. Wasn't really suggesting it as a realtime effect, though that's not entirely out of the question.

Re: Rotating a sprite 90 degrees

Posted: Thu Oct 11, 2018 1:00 pm
by pubby
It could be useful for CHR compression algorithms I suppose.

Re: Rotating a sprite 90 degrees

Posted: Thu Oct 11, 2018 1:32 pm
by tepples
JRoatch's Donut codec, developed for a forthcoming version of Action 53, has a rotation mode. But it's not for reusing the same tile in unrotated and 90 degree rotated modes; it's for handling non-tile data in CHR ROM.