-
Sprite Movement
Damn, it was going so well...
I've got the functions to move a sprite up, left and right working fine. They're very similar to the function below.
This function is supposed to move the sprite down the screen, but it doesn't:
Code:
void MoveSprDown (int x, int y, int dist, unsigned char spr[], unsigned char bg_colour)
{
int sprite_width, sprite_height;
sprite_width = spr[0] + (spr[1] << 8);
sprite_height = spr[1] + (spr[3] << 8);
Wait_VRT ();
for (; y <= dist; y++)
{
PutSprite (x, y, spr);
Wait_VRT ();
PutBar (x, y, x + sprite_width, y + sprite_height, bg_colour);
}
PutSprite (x, y, spr);
}
The sprite doesn't move at all.
The Y coord is incremented downwards (i.e. incrementing moves down the screen) BTW.
Hmm that's about it.
-
I'm not quite sure what that spr[] is supposed to be, but I do know that if you bitshift any of those characters by 8, you come up with 0.
-
spr[] is the sprite array to move.
0? Well that's weird. That same function (with the for loop condition changed) works fine for up / left / right.
Hmm.
*edit*
I'm using the 'linear sprite format' (apparently - following a tutorial) where the sprite array looks like this:
Code:
/* width height
/ \/ \ */
unsigned char man_sprite[] = { 5, 0, 8, 0,
// rest of sprite here
};
Not sure if that's usefull info? :)
*edit*
Bollocks that formatting's bad. Well I'll just explain it: the first two numbers are the width of the sprite, while the second two are the height, then the rest of the array denote the actual sprite itself, with 0 being taken as trasparent.
-
It should definitely be 0. A char is 1 byte, which is 8 bits. so if you bitshift 8 times, it's all gone. What you might want is this:
Code:
( * ((unsigned short*)&spr[1]) ) << 8;
//or
*(static_cast<unsigned short *>(&spr[1]) << 8;
Either would work, I just showed you both because I'm not quite sure what your thoughts on static_cast are (some people are finnicky about it). This still may not give you what you want depending on the endian-ness.
That's my explanaition of that, however I'm not sure you're going about things the easiest way. If you're moving a sprite, why are you messing with the width and height?
-
Well there's a little thing about moving sprites - they leave a coloured trail behind them. The call to PutBar() erases that trail, and I need to know the width and height of the sprite to draw a corresponding Bar.
Ah I'm ok with static_cast
Could you show me an easier way to move a given sprite?
-
One quite easy way to move a sprite is to have this:
Code:
class sprite
{
private:
// posX and posY together shows where the upper left corner of the sprite is
float posX;
float posY;
float height;
float width;
// Bla bla more data that you need.
public:
// bla bla bla
};
Now when you draw it you always calculate the sides by using posX, posY, height and width. This way you only have to change posX and posY to change the position of the whole sprite.