Sign in to follow this  

[SDL] Cropping images

Recommended Posts

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
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
SDL doesn't have a cropping function but you can easily build one
[code]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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this