#7448 - hnager - Wed Jun 18, 2003 3:24 am
Simple question - hopefully with a simple answer - how do you handle sprite clipping on the gba...
If I move a sprite off screen (to the top or left) I get unpredictable (but expected) results. I'm pretty familiar with techniques to handle this in a mode 13h style, but not sure how to do it when using hardware sprites.
Oh, this is MODE1 - if that helps.
#7449 - hnager - Wed Jun 18, 2003 3:39 am
would this do it? I seem to remember reading about this but didn't understand what it was used for...correct me if I'm wrong:
void MoveSprites(OAMEntry* sp, int x, int y)
{
if(x < 0)
x = 512 + x;
if(y < 0)
y = 256 + y;
sp->attribute1 = sp->attribute1 & 0xFE00;
sp->attribute1 = sp->attribute1 | x;
sp->attribute0 = sp->attribute0 & 0xFF00;
sp->attribute0 = sp->attribute0 | y;
}
#7452 - tepples - Wed Jun 18, 2003 5:28 am
That function will work, as long as you don't try to put a sprite too far offscreen. I'll comment it so that you can see what it does:
Code: |
void MoveSprites(OAMEntry* sp, int x, int y)
{
if(x < 0) /* if sprite is to left side of screen, wrap x coordinate */
x = 512 + x;
if(y < 0) /* if sprite is above screen, wrap x coordinate */
y = 256 + y;
sp->attribute1 = sp->attribute1 & 0xFE00; /* preserve old attributes */
sp->attribute1 = sp->attribute1 | x; /* add in x coordinate */
sp->attribute0 = sp->attribute0 & 0xFF00; /* preserve old attributes */
sp->attribute0 = sp->attribute0 | y; /* add in y coordinate */
} |
I'd probably have written it more robustly, with checks for completely offscreen sprites, like this:
Code: |
void move_sprite_to(OAMEntry *spr, int x, int y)
{
/* if it's out of bounds, then put it at (240, 160) where it won't affect anything */
/* but the out of bounds calculation may fail for the largest double-size sprites */
if(x < -64 || x > 240 || y < -64 || y > 160)
{
x = 240;
y = 160;
}
y &= 0x00ff; /* wrap y coordinate */
spr->attribute0 = (spr->attribute0 & 0xff00) | y; /* change only y bits */
x &= 0x01ff; /* wrap x coordinate */
spr->attribute1 = (spr->attribute1 & 0xfe00) | x; /* change only x bits */
} |
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.
#7644 - jcpredator - Sun Jun 22, 2003 5:54 am
I tried to resolve my own sprite clipping issue with the above code but some how it still does not work.
I'm not sure we are talking about the same clipping but I will try to describe my problem that I'm having. I'm a newbie at GBA programming so please bear with me, :)
Every time I move the sprite by pressing up, down, left, or right, the sprite (a picture of the USA flag) flips vertically and cut off midway horizontally with the other half displayed right beside it, hope that made sense. If I continue pressing the same button the sprite will eventually come back to its original state.
I used the above MoveSprite function and still recieved the same distortion on my sprite.
Just wondering if anyone could help a newbie out, I would greatly appreciate it.
If you would like source code please let me know.
_________________
There is no spoon...
#7648 - Ninja - Sun Jun 22, 2003 11:43 am
The above ways are the solution. You are essentially overwriting parts of your sprite control data by feeding it a negative number. (which overflows to a very high positive number if you are using a u16.)
Insert the code that the nice fellows above have posted, and it should work just fine.