Send SpriteDefinition in function

SGDK only sub forum

Moderator: Stef

Post Reply
faeldaniel
Newbie
Posts: 5
Joined: Wed Oct 01, 2014 2:06 pm
Location: Brazil - Fortaleza
Contact:

Send SpriteDefinition in function

Post by faeldaniel » Wed Oct 08, 2014 7:37 pm

Hello brothers,

I create one function for start my routine, I pass my variables in the function:

Code: Select all

void initVarsAndGraphicsForLevel(fix32 initX, fix32 initY, Image imageBg0, Image imageBg1, SpriteDefinition spriteDef){

    ////////////////////////////////////////
    // initialize my vars here

   ...

    ////////////////////////////////////////

    u16 palette[64];
    u16 ind;

    // disable interrupt when accessing VDP
    SYS_disableInts();
    // initialization
    VDP_setScreenWidth320();
    // init sprites engine
    SPR_init(256);

    // load background
    ind = TILE_USERINDEX;
    VDP_drawImageEx(BPLAN, &imageBg1, TILE_ATTR_FULL(PAL0, TRUE, FALSE, FALSE, ind), 0, 0, FALSE, TRUE);
    ind += imageBg0.tileset->numTile;
    VDP_drawImageEx(APLAN, &imageBg0, TILE_ATTR_FULL(PAL1, FALSE, FALSE, FALSE, ind), 0, 0, FALSE, TRUE);
    ind += imageBg1.tileset->numTile;

    // VDP process done, we can re enable interrupts
    SYS_enableInts();

    // init sprite
    SPR_initSprite(&sprites[0], &spriteDef, fix32ToInt(engineSpr_hero_X), fix32ToInt(engineSpr_hero_Y), TILE_ATTR(PAL2, FALSE, FALSE, FALSE));
    SPR_setAnim(&sprites[0], 2);

    // prepare palettes
    memcpy(&palette[0], imageBg1.palette->data, 16 * 2);
    memcpy(&palette[16], imageBg0.palette->data, 16 * 2);
    memcpy(&palette[32], spriteDef.palette->data, 16 * 2);

    // fade in (disable interrupts because of the passive fade in)
    SYS_disableInts();
    VDP_fadeIn(0, (3 * 16) - 1, palette, 20, FALSE);
    SYS_enableInts();

}
The imageBg0 and imageBg1 (Image) used for BGs worked perfectly but spriteDef (SpriteDefinition) not, the sprites is not initialized, but if i charge directly in the function &spriteDef for &heroSprites everything works perfectly, but i liked pass directly in the function, because i not need to rewrite this code every time i can calling this function only when you want to start the graphics and the variables.

You can see below another codes.

In the gxf.h is:

Code: Select all

#ifndef _GFX_H_
#define _GFX_H_

extern const Image bgb_image;
extern const Image bga_image;
extern const SpriteDefinition heroSprites;

#endif // _GFX_H_
In the main.c is:

Code: Select all

int main(){

    initVarsAndGraphicsForLevel(0, 0, bgb_image, bga_image, heroSprites);

    while(1){

        movingInLevel(JOY_readJoypad(JOY_1));

    }
    return (0);
}

Anyone have any idea what I'm doing wrong?

Moon-Watcher
Very interested
Posts: 117
Joined: Sun Jan 02, 2011 9:14 pm
Contact:

Post by Moon-Watcher » Wed Oct 08, 2014 8:18 pm

You forgot SPR_update();

faeldaniel
Newbie
Posts: 5
Joined: Wed Oct 01, 2014 2:06 pm
Location: Brazil - Fortaleza
Contact:

Post by faeldaniel » Wed Oct 08, 2014 11:47 pm

Moon-Watcher wrote:You forgot SPR_update();
Yes, in the funcion movingInLevel(...); i set SPR_update(sprites, 1);

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Post by Stef » Thu Oct 09, 2014 3:49 pm

You should avoid function like this :

Code: Select all

void initVarsAndGraphicsForLevel(fix32 initX, fix32 initY, Image imageBg0, Image imageBg1, SpriteDefinition spriteDef);
as it will put a copy of the Image or SpriteDefinition structure on the stack (which is an heavy task), instead you should use pointer (as the SGDK methods) :

Code: Select all

void initVarsAndGraphicsForLevel(fix32 initX, fix32 initY, Image* imageBg0, Image* imageBg1, SpriteDefinition* spriteDef);
and so adapt your code for that...

faeldaniel
Newbie
Posts: 5
Joined: Wed Oct 01, 2014 2:06 pm
Location: Brazil - Fortaleza
Contact:

Post by faeldaniel » Fri Oct 10, 2014 2:58 pm

I go try, thank your help brother, this sdk is fantastic, I ported my engine for rpg writing to snes perfectly with this sgdk

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Post by Stef » Fri Oct 10, 2014 7:30 pm

Thanks for the kind words, glad you appreciate it =)
I'm looking forward your progresses =)

Post Reply