Sprite Animation Problem

SGDK only sub forum

Moderator: Stef

Post Reply
Tatchou
Interested
Posts: 12
Joined: Sun Feb 23, 2014 10:49 pm

Sprite Animation Problem

Post by Tatchou »

Hey everyone, I continue to learn the tutorial and I'm stuck at a point.
I fail to see or to animate a sprite with genres method.
I have this code which is almost the same as in the tutorial :

Code: Select all

#include <genesis.h>

typedef struct
{
    s16 posx;
    s16 posy;
    u16 tile_attr;
    u8 size;
    u8 link;
    u32 startaddr;
    u8 steps;

} _spritedef;

struct genresSprites
{
    u16 *pal;               //pointer to pal data
    u32 **sprites;          //pointer to sprites data
    u16 count;              //nb sprites
    u16 width;              //width of each sprite in pixels (not tiles!)
    u16 height;             //height of each sprite in pixels (not tiles!)
    u16 size;               //since we use width/height in pixel, useful info on   sprite size
                            //TODO : size is not SGDK compliant, you need to use size>>8
                            //will be fixed in coming release
};


int main()
{

        struct genresSprites sonicTEST;
        _spritedef SonicSprite;

        u16 nbTiles = (sonicTEST.height>>3) * (sonicTEST.width>>3);

        VDP_loadTileData( sonicTEST.sprites[1], 1, nbTiles, 0);

        // load in PAL2
        VDP_setPalette(PAL2, sonicTEST.pal);

        VDP_resetSprites();
        VDP_setSprite(0, 0, 0, sonicTEST.size>>8, TILE_ATTR_FULL(PAL2,1,0,0,1), 0);
        VDP_updateSprites();

        u8 frame = 1;

        // define the sprite (using a _spritedef to easily make Sonic move later)
        SonicSprite.posx = 40;
        SonicSprite.posy = 40;
        SonicSprite.size = sonicTEST.size>>8;
        SonicSprite.tile_attr = TILE_ATTR_FULL(PAL2,1,0,0,1);
        SonicSprite.link  = 0;
        VDP_setSpriteP(0, &SonicSprite);


         //GAME LOOP
         while(1)
        {

             // we still use nbTiles since ALL sprite are of the size in a sprite sheet
            VDP_loadTileData( sonicTEST.sprites[frame + 1], 1, nbTiles, 0);
            frame++; // next frame
            frame%=3; // we only need 3 frames, so roll back


            //make it move!
            SonicSprite.posx+=10;
            VDP_setSpriteP(0, &SonicSprite);

            //flush
            VDP_updateSprites();

            // .... code

            VDP_waitVSync();

        }
        return (0);
}

I don't understand what's the problem. I see on my screen two red squares moving
instead of sonic sprite.

I have in my folder src the file resource.c of tutoriel :

Code: Select all

   ; to put the data in a "res" folder, else it will compiled by GenRes AND GDK's bitmap compiler
   SPRITE sonic "data/sonic.bmp" 24 32 0 7
   ; it's VERY important to finish by a new line ! else the last resource won't be compiled
And I have my sonic.bmp file in src /data.


In short, if someone can help me because it's been a few days that I'm stuck.
Thank you in advance.
Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Post by Stef »

You might use the "sprite" example given in the SGDK sample folder.
It uses the new resource compiler and the new SGDK structures and methods to handle sprites and i believe it is far easier to work with !
I know there is no tutorial yet but you can read the rescomp.txt file which describe how you have to define you sprite also the sprite tutorial itself is pretty straight forward. Don't forget to also read the "sprite_eng" methods documentation (in the header file or directly in the SGDK doc folder).
Tatchou
Interested
Posts: 12
Joined: Sun Feb 23, 2014 10:49 pm

Post by Tatchou »

It worck perfectly I can draw a sprite on screen.

Now I try to do a little pong project, and I have a new problem.
I can't draw more than one sprite :

There is my code :

Code: Select all

#include <genesis.h>

#include "gfx.h"


// sprite structure
Sprite Raquette1;
Sprite Raquette2;
Sprite Balle;

s16 Raquette1_posx;
s16 Raquette1_posy;

s16 Raquette2_posx;
s16 Raquette2_posy;

s16 Balle_posx;
s16 Balle_posy;

int main()
{
    u16 palette[64];


    // initialization
    VDP_setScreenWidth320();

    JOY_setEventHandler(joyEvent);

    // init sprites engine
    SPR_init(256);

    // set all palettes to black
    VDP_setPaletteColors(0, palette_black, 64);


    //INIT VARIABLES
    Raquette1_posx = 5;
    Raquette1_posy = 0;

    Raquette2_posx = 100;
    Raquette2_posy = 5;

    Balle_posx = 50;
    Balle_posy = 50;



    //INIT RAQUETTE SPRITES

    //Raquette
    SPR_initSprite(&Raquette1, &Raquette_sprite, Raquette1_posx, Raquette1_posy, TILE_ATTR(PAL0, TRUE, FALSE, FALSE));
    SPR_setAnim(&Raquette1, 0);


    SPR_initSprite(&Raquette2, &Raquette_sprite, Raquette2_posx, Raquette2_posy, TILE_ATTR(PAL0, TRUE, FALSE, FALSE));
    SPR_setAnim(&Raquette2, 0);

    //Balle
    SPR_initSprite(&Balle, &Balle_sprite, Balle_posx, Balle_posy, TILE_ATTR(PAL1, TRUE, FALSE, FALSE));
    SPR_setAnim(&Balle, 0);


    memcpy(&palette[0], Raquette_sprite.palette->data, 16 * 2);
    memcpy(&palette[16], Balle_sprite.palette->data, 16 * 2);

    // fade in
    VDP_fadeIn(0, (3 * 16) - 1, palette, 20, FALSE);


    while(TRUE)
    {
        //MAJ SPRITE:
        SPR_update(&Raquette1, 1);
        SPR_update(&Raquette2, 1);
       SPR_update(&Balle, 1);


        VDP_waitVSync();
    }

    return 0;
}
Only the last SPR_update is realised, I don't understand how to draw the all the sprites...
Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Post by Stef »

Oh i guess the documentation is definitely not enough explicit about this one.
Actually SPR_update(..) should be called only once per frame, this is the method that prepare all your sprites for the next frame.
In your case you may do that :

Code: Select all

#define MAX_SPRITE    16;

Sprite sprites[MAX_SPRITE];

Sprite Raquette1 = &sprites[0];
Sprite Raquette2 = &sprites[1];
Sprite Balle = &sprites[2];
then

Code: Select all

SPR_update(sprites, 3);
If you want to hide a specific sprite you can just put it outside screen area.
Tatchou
Interested
Posts: 12
Joined: Sun Feb 23, 2014 10:49 pm

Post by Tatchou »

Sorry for not responding sooner but
thank you very much it works!
Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Post by Stef »

Glad to hear that, well done =)
Post Reply