Thread: Sprite Movement

  1. #1
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709

    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.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #2
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    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.

  3. #3
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    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.
    Last edited by cboard_member; 08-07-2005 at 02:28 AM.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  4. #4
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    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?

  5. #5
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    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?
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    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.
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DirectX 9 sprite issue
    By maxthecat in forum Game Programming
    Replies: 9
    Last Post: 05-18-2006, 04:04 AM
  2. SDL question (long)
    By yahn in forum C++ Programming
    Replies: 0
    Last Post: 04-16-2006, 08:46 PM
  3. GDI sprite (Win32 API)
    By fiff in forum Game Programming
    Replies: 4
    Last Post: 06-24-2005, 09:09 AM
  4. I need movement...
    By Finchie_88 in forum C++ Programming
    Replies: 1
    Last Post: 10-04-2004, 03:10 PM
  5. Replies: 3
    Last Post: 05-12-2003, 05:49 PM