getting sub-surface of SDL surface

Started by
5 comments, last by ishkabible 13 years, 6 months ago
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.
Advertisement
is there a question?
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.
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?
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).
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.
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.

This topic is closed to new replies.

Advertisement