Page 1 of 1

Sprite Animation Problem

Posted: Sat Mar 01, 2014 11:49 am
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.

Posted: Sat Mar 01, 2014 1:52 pm
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).

Posted: Sat Mar 01, 2014 5:53 pm
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...

Posted: Sat Mar 01, 2014 7:06 pm
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.

Posted: Tue Mar 04, 2014 12:51 pm
by Tatchou
Sorry for not responding sooner but
thank you very much it works!

Posted: Wed Mar 05, 2014 10:14 pm
by Stef
Glad to hear that, well done =)