[SDL] Cropping images

Started by
3 comments, last by bbqroast 12 years, 6 months ago
Hello!
In my latest project I have a sprite sheet, but rather than the method of passing a 'clip' to my applySurface() function I want to be able to just pass a cropped texture.

Basically I have a class and when it is intilized it takes the pre loaded sprite sheet and then cuts a 16X16 part of it using the values passed to it, but I'm stuck on this.
So is there like a function that accepts a SDL surface, x and y values and height and width values and then returns a cropped SDL surface??
Advertisement
The whole point of packing sprites to a sheet is to be able to reuse a single texture between multiple drawn sprites, which removes extra state changes and enables batching opportunities. It is better for performance to just compute the UV coordinates for the individual sprites in the sheet, and use those UV coordinates to draw the sprite.

I am not familiar with SDL internals, but perhaps there is a function that allows you to specify a pair of UVs for the sprite when rendering it?

Ok yeah I thought so, for me sprite-sheets just reduce the amount of crap I have to put in my includes folder :).
Oh well time to rethink my strategy.
SDL doesn't have a cropping function but you can easily build one
SDL_Surface* crop_surface(SDL_Surface* sprite_sheet, int x, int y, int width, int height)
{
SDL_Surface* surface = SDL_CreateRGBSurface(sprite_sheet->flags, width, height, sprite_sheet->format->BitsPerPixel, sprite_sheet->format->Rmask, sprite_sheet->format->Gmask, sprite_sheet->format->Bmask, sprite_sheet->format->Amask);
SDL_Rect rect = {x, y, width, height};
SDL_BlitSurface(sprite_sheet, &rect, surface, 0);
return surface;
}
Thanks, also your signature is awesome.

This topic is closed to new replies.

Advertisement