Download Tutorial C Files H Files

dg_spritesheet_to_sprite

This function is used to update a sprite from a spritesheet.

Usage

void dg_spritesheet_to_sprite(dg_spritesheet_t *sheet, sfSprite *sprite, int id);

Example

dg_spritesheet_to_sprite(sheet, sprite, 3);

Source code

void dg_spritesheet_to_sprite(
    dg_spritesheet_t *sheet,
    sfSprite *sprite,
    int id
    )
{
    sfVector2u image_size = sfTexture_getSize(sheet->sheet);
    sfIntRect pos = {0, 0, sheet->x, sheet->y};

    for (int i = 0; i < id; i++) {
        pos.left += sheet->x;
        if (pos.left >= image_size.x) {
            pos.top += sheet->y;
            pos.left = 0;
        }
        if (pos.top >= image_size.y) {
            pos.top = 0;
            pos.left = 0;
        }
    }
    sfSprite_setTexture(sprite, sheet->sheet, id);
    sfSprite_setTextureRect(sprite, pos);
}