I’ve developed a module that extract FLIC files and convert it to an usable format for SDL. There’s two implementations for the converter.
First implementation
typedef struct sdl_flic_animation {
Uint16 w;
Uint16 h;
Uint32 delay;
Uint16 depth;
Uint16 count;
SDL_Texture *frames[];
} SDL_FlicAnimation;
SDL_FlicAnimation*
SDL_ConvertAnimation(SDL_Renderer *render,
FLIC_Animation *animation)
{
SDL_FlicAnimation *retval = NULL;
retval = malloc(sizeof(SDL_FlicAnimation) +
sizeof(SDL_Texture*) * animation->framesno);
// Copy meta data
memcpy(retval, animation, sizeof(SDL_FlicAnimation));
surface = SDL_CreateRGBSurfaceWithFormat(0,
retval->w,
retval->h,
retval->depth,
SDL_PIXELFORMAT_INDEX8);
SDL_SetPaletteColors(surface->format->palette,
animation->colors,
0,
animation->colorno);
int size = retval->w * retval->h;
for (size_t i = 0; i < retval->count; ++i) {
SDL_LockSurface(surface);
// Skip 'i' frames.
surface->pixels = animation->pixels + (i * size);
SDL_UnlockSurface(surface);
retval->frames[i] = SDL_CreateTextureFromSurface(render,
surface);
}
/*
* DO NOT FREE THE PIXELS.
*/
surface->flags |= SDL_PREALLOC;
SDL_FreeSurface(surface);
return retval;
}
The second implementation:
It’s almost identical, but instead of having an array of SDL_Texture, the whole animation is in one texture:
typedef struct sdl_flic_animation {
Uint16 w;
Uint16 h;
Uint32 delay;
Uint16 depth;
Uint16 count;
SDL_Texture *frames;
} SDL_FlicAnimation;
SDL_FlicAnimation*
SDL_ConvertAnimation(SDL_Renderer *render,
FLIC_Animation *animation)
{
SDL_FlicAnimation *retval = NULL;
retval = malloc(sizeof(SDL_FlicAnimation);
// Copy meta data
memcpy(retval, animation, sizeof(SDL_FlicAnimation) -
sizeof(SDL_Texture*));
surface = SDL_CreateRGBSurfaceWithFormatFrom(animation->pixels,
retval->w,
retval->h * retval->count,
retval->depth,
retval->width * sizeof(BYTE),
SDL_PIXELFORMAT_INDEX8);
SDL_SetPaletteColors(surface->format->palette,
animation->colors,
0,
colorno);
retval->frames = SDL_CreateTextureFromSurface(render, surface);
/*
* DO NOT FREE THE PIXELS.
*/
surface->flags |= SDL_PREALLOC;
SDL_FreeSurface(surface);
return retval;
}
Note: FLIC_Animation is an internal structure for the extracter.
Both implementations work, but the first one is a little bit faster, and I have no idea why. My question is, which implementation should I keep for performance. Will SDL be able do render the animation better with an array of SDL_Texture, or a single SDL_Texture and a SDL_Rect to move the source? I would also be interested in why the first implementation is faster.