Some Questions About Sdl_setcliprect


PSyMastR

\m/O__O\m/
Joined
Sep 14, 2005
Messages
2,968
Website
Visit site
So, I loaded a surface in, that contains a whole bunch of images, for example, a tilesheet. Now, I want to split every tile into its own surface. What is the correct way to chop it up?

CODE
SDL_surface *temp = SDL_SetClipRect(old_surface, some_rect);


Now, does that work at all, or does SDL_SetClipRect just change the clipping of the rectangle to old_surface? And, if it changes the rect of the old surface, then does it also delete image data out of the bounds as well, or does it just keep a bunch of massive surfaces with smaller rects?

Also, what would be the correct way of doing this?

Currently, I am stuck at this point:

CODE
SDL_Surface *tiles_temp = load_image("some_image_png_or_bmp.png");

//A nice place to store all the tile images
std::vector<SDL_Surface*> tiles;
SDL_Rect load_tiles_rect;
load_tiles_rect.w = TILE_SIZE;
load_tiles_rect.h = TILE_SIZE;

for(int tile_load_x = 0; tile_load_x <= (tiles_temp->w - TILE_SIZE); tile_load_x += TILE_SIZE)
{
for(int tile_load_y = 0; tile_load_y <= (tiles_temp->h - TILE_SIZE); tile_load_y += TILE_SIZE)
{
load_tiles_rect.x = tile_load_x;
load_tiles_rect.y = tile_load_y;
tiles.push_back(/* WHAT GOES IN HERE */);
}
}
 
setClipRect only changes the current clipping rectangle, it doesn't produce a new surface.

What I would do is just store a rectangle and blit individual tiles straight from the large surface, without ever physically splitting it up.
BUT, I'm not an SDL expert, this might be too slow. I don't know.

If you really need to split the large surface into smaller ones, do something like this:

SDL_Rect tile_location;
tile_location.x = tile_width * x;
tile_location.y = tile_height * y;

SDL_Surface * tile = create_Surface (tile_width, tile_height, other_parameters);
SDL_BlitSurface (giant_surface, tile_location, tile, NULL);

This will copy the data from the huge tileset into the smaller tile surface. Repeat it for each tile you have, and you can make an array of tile-sized surfaces pretty easily.

Maybe this is more cache-friendly than blitting straight from the tile set, but my first thought still would be to not even bother splitting it up, and just blitting from the huge tile set to the output surface.


addition:
I have the SDL documentation which is available at:
http://www.libsdl.org/archives/sdldoc-html.tar.gz

I don't know how up-to-date it is, but it's always seemed to work for me, and it explains every function and structure pretty well. You should download it and use it as a reference.
 
No difference in performance in using one surface and image or a bunch.
CODE

http://www.gp32x.de/board/index.php?act=ST&f=64&t=47023&hl=&view=findpost&p=708382


I prefer one or two and an array of sprite structs containg pointers to arrays of rect coordinates..
 
Last edited by a moderator:
Alright, so I am just using a vector of SDL_Rect's. Now I have another question, how do I convert an SDL_Rect to a SDL_Rect* to use with blitting functions, as with a vector of SDL_Rect*'s it wont allow me to change the w/h/x/y of the rect.
 
Last edited by a moderator:
dynamic memory.
SDL_Rect* s are just pointers to potential SDL_Rect s
you would have to new and delete them.

BUT the easiest for you to use the normal SDL_Rect s would just be to precede them with an "&"

like SDL_BlitSurface(images, &src, dstimg, &dst);
 
lol, pointers.

SDL_Rect example;
SDL_Rect * ptr = &example;

The & converts any datatype to its pointer.
Since SDL_Rect is just a set of 4 integers, you should definitely just use SDL_Rects internally and then pass the pointers to SDL with &rect.
 
By the way, go learn pointers right before you regret it :)
 
Last edited by a moderator:
Back
Top