Hello !
I have large graphic files with multiple images in (all the same size,
thankfully) as frames of animation. How do I extract them to single
surfaces? Do I load the image as normal then “grab” sections with x&y
coords (if this is possible, what functions do I call?).
Hope someone can help!
Look at :
bool Load_Tiles (char *Filename, Tiles **TILES_main,
int Tile_Size_X, int Tile_Size_Y)
{
bool status = false;
SDL_Surface *temp = NULL;
SDL_Rect tile_rect;
Uint32 index_tile = 0, index_x = 0, index_y = 0;
Uint32 max_nr_tiles = 0, max_nr_tiles_x = 0, max_nr_tiles_y = 0;
Uint32 r_mask = 0, g_mask = 0, b_mask = 0, a_mask = 0;
if ( (*TILES_main) != NULL ) return status;
if ( strlen (Filename) == 0 ) return status;
if ( (Tile_Size_X == 0) || (Tile_Size_Y == 0) ) return status;
#if SDL_BYTEORDER == SDL_BIGENDIAN
r_mask = 0xff000000;
g_mask = 0x00ff0000;
b_mask = 0x0000ff00;
a_mask = 0x000000ff;
#else
r_mask = 0x000000ff;
g_mask = 0x0000ff00;
b_mask = 0x00ff0000;
a_mask = 0xff000000;
#endif
temp = SDL_LoadBMP (Filename);
if (! temp) return status;
if ( (! temp -> w) || (! temp -> h) ) return status;
index_tile = 1;
max_nr_tiles_x = (Uint32) ((temp -> w) / Tile_Size_X);
max_nr_tiles_y = (Uint32) ((temp -> h) / Tile_Size_Y);
if ( ((temp -> w) % Tile_Size_X) > 0 )
{
max_nr_tiles_x ++;
}
if ( ((temp -> h) % Tile_Size_Y) > 0 )
{
max_nr_tiles_y ++;
}
max_nr_tiles = max_nr_tiles_x * max_nr_tiles_y;
max_nr_tiles ++;
(*TILES_main) = new Tiles;
(*TILES_main) -> Nr_Tiles = max_nr_tiles;
(*TILES_main) -> Tile_Size_X = Tile_Size_X;
(*TILES_main) -> Tile_Size_Y = Tile_Size_Y;
(*TILES_main) -> Nr_Tiles_X = max_nr_tiles_x;
(*TILES_main) -> Nr_Tiles_Y = max_nr_tiles_y;
(*TILES_main) -> Orig_Width = temp -> w;
(*TILES_main) -> Orig_Height = temp -> h;
(*TILES_main) -> Tiles = new SDL_Surface *[max_nr_tiles];
(*TILES_main) -> Tiles [0] = NULL;
for (index_y = 0;
index_y < (temp -> h);
index_y = index_y + ((*TILES_main) -> Tile_Size_Y))
{
for (index_x = 0;
index_x < (temp -> w);
index_x = index_x + ((*TILES_main) -> Tile_Size_X))
{
(*TILES_main) -> Tiles [index_tile] =
SDL_CreateRGBSurface ( SDL_SWSURFACE,
(*TILES_main) -> Tile_Size_X,
(*TILES_main) -> Tile_Size_Y,
32,
r_mask,
g_mask,
b_mask,
a_mask );
tile_rect.x = index_x;
tile_rect.y = index_y;
tile_rect.w = (*TILES_main) -> Tile_Size_X;
tile_rect.h = (*TILES_main) -> Tile_Size_Y;
SDL_BlitSurface ( temp, & tile_rect,
(*TILES_main) -> Tiles [index_tile], NULL);
index_tile ++;
}
}
SDL_FreeSurface (temp);
status = true;
return status;
}
bool Delete_Tiles (Tiles **TILES_main)
{
bool status = false;
Uint32 index_tile = 0;
if ( (*TILES_main) == NULL ) return status;
for (index_tile = 0;
index_tile < (*TILES_main) -> Nr_Tiles;
index_tile ++)
{
if ( (*TILES_main) -> Tiles [index_tile] != NULL )
{
SDL_FreeSurface ( (*TILES_main) -> Tiles [index_tile] );
(*TILES_main) -> Tiles [index_tile] = NULL;
}
}
delete[] (*TILES_main) -> Tiles;
delete (*TILES_main);
(*TILES_main) = NULL;
status = true;
return status;
}
CU