• Advertisement
Sign in to follow this  

[SDL] Cropping images

This topic is 2328 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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??

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement