Jump to content
  • Advertisement
Sign in to follow this  

SDL_Surface sprite clipping question

This topic is 3029 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

Hey all, newbie SDL question.

I have a spritesheet in my game and a bunch of objects that look like

class Object

Object ( int y, int x );

int y, x;
SDL_Surface *tile;

...so (hopefully) when drawing the screen all objects that derive from the base 'Object' class can just call a generic display function that blits *tile at the current x-y position.

My question is, how can I clip a specific part of the spritesheet out to be stored as the Object's 'tile' surface? I understand how to set an array of SDL_Rects to define the area of each clipped sprite, but it would be simpler for my purposes if I could set one SDL_Surface as a clipped part of another SDL_Surface.

edit: sorry, I've only just realised on checking back on this post that I posted it in the wrong forum, I intended for it to go in Alternative Game Libraries. Apologies for that, I don't know how it ended up here.

[Edited by - chiranjivi on August 24, 2010 4:12:20 PM]

Share this post

Link to post
Share on other sites
Let me get this straight. You merely want to clip a single sprite from the sprite sheet to use with your Object class? And you want to have, say, a previously defined SDL_Surface that contains the larger sprite sheet that you can create a new clipped SDL_Surface from? If that's the case, have a look-see at this function.

//Clip SDL_Surface from another SDL_Surface
void clip_surface(SDL_Surface *main_sprite_sheet, SDL_Surface *clipped_surface, int x, int y, int w, int h)
//Initialize temporary SDL_Rect
SDL_Rect *temp_rect;

//Setup clipped SDL_Surface & SDL_Rect
clipped_surface = main_sprite_sheet;
temp_rect->x = x;
temp_rect->y = y;
temp_rect->w = w;
temp_rect->h = h;

//Clip SDL_Surface with temporary SDL_Rect
SDL_SetClipRect(clipped_surface, temp_rect);

This method allows you to copy the main surface into the surface you want to clip and then use a temporary SDL_Rect for the clipping. This function constructs the temporary SDL_Rect with integers passed to the function, but if you're willing, you could just have your Object class contain an SDL_Rect as a class member variable, modify this function to accept SDL_Rects, and pass that instead of a bunch of numbers.

One thing to note, when you blit to the screen, don't make the parameter that takes in the SDL_Rect for the clip area equal to NULL, as that defeats the purpose. Every SDL_Surface has an SDL_Rect defining the clip area. I'm not sure, but I think setting the clip area to NULL when you blit to the screen overrides any clip area data the SDL_Surface has. You can simply call on the SDL_Surface's internal SDL_Rect to get the clip area. Say if you had an instance of your Object class called foo and you already used the function above to clip it, you'd blit it to the screen like this.

//Standard SDL blitting, clip_rect is predifined in SDL as read-only
SDL_BlitSurface(foo.tile, &foo.tile->clip_rect, some_destination_surface, some_offset_rect);

//Lazy Foo' way for Lazy Foo' tutorials
apply_surface(x, y, foo.tile, some_destination_surface, &foo.tile->clip_rect);

I'm a bit rusty working with addresses of variables from pointers to structures inside of classes. Plus it's late at night.

[Edited by - Shonumi on August 24, 2010 9:33:53 PM]

Share this post

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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!