Sign in to follow this  
ishkabible

getting sub-surface of SDL surface

Recommended Posts

ishkabible    100
on of biggest problems with making games is art, i always want really nice sprites but lack the skills and desire to make them. i thought it would be fun to do a complete Super Mario Bros knock off for at least the first level. all the sounds and pictures for it can be found online leaving me to just program it. i found a comprehensive sprite sheet (seems to have everything i can think of) and want to make a function as a part of my texture class that takes an SDL_Rect as an argument and returns the sub SDL_Surface of this rectangle. this sounds like something that should already be in the SDL library but if not i think i can manage to use the raw data to get it.

Share this post


Link to post
Share on other sites
ishkabible    100
sorry i wasn't more clear, how can i retrieve a sub SDL_Surface defined by an SDL_Rect(or what ever data defines a rectangle) from another larger SDL_Surface.

Share this post


Link to post
Share on other sites
zerothrillz    152
I'm a little rusty on SDL but do you want to have a shared-memory sub bitmap or a copy?

---

Or maybe you're confused about how to access the individual tiles in the tile set surface?

Share this post


Link to post
Share on other sites
Dunge    405
You should load the full texture file into a surface, and then use SDL_BlitSurface() with only a portion of it onto your main surface (or anywhere else in-between).

Share this post


Link to post
Share on other sites
Crowseye    308
From the documentation:

SDL_BlitSurface(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect)

--

srcrect is the SDL_Rect that defines the rectangular area of your source image surface that you want blitted to the destination surface.

Rather than have your tiles hold their own surface generated from your original tileset surface, you can have the tiles just hold a rect (or SDL_Rect if you don't care about separating your tile data from their implementation using SDL) structure that defines the rectangular area of the tileset image representing that tile.

When you want to "draw" a tile, retrieve its rect and use the (address of the) appropriate SDL_Rect as the second argument into SDL_BlitSurface.

If for some reason you ever want a genuine "subsurface," the easiest way I know of is to create a surface of the appropriate size using, say, SDL_CreateRGBSurface, and then use the same principle above: SDL_BlitSurface the "clip" rectangle to the new surface.

Share this post


Link to post
Share on other sites
ishkabible    100
i don't just want to blit a part of it i want to get the sub surface as separate item. so take a 4x4 image and say i wan't the sub-surface 0x,0y 2w,2h so that i would have a 4x4 image and in a separate place in memory from the 2x2 image.

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