diff --git a/src/rl_image.c b/src/rl_image.c index 1adb14f..a010dec 100644 --- a/src/rl_image.c +++ b/src/rl_image.c @@ -58,6 +58,62 @@ rl_image_t* rl_image_create( const void* data, size_t size ) return NULL; } +rl_imageset_t* rl_imageset_create( const void* data, size_t size ) +{ + union + { + const void* restrict v; + const uint8_t* restrict u8; + const uint16_t* restrict u16; + const uint32_t* restrict u32; + } + ptr; + + ptr.v = data; + + int num_images = ne16( *ptr.u16++ ); + + rl_imageset_t* imageset = (rl_imageset_t*)rl_malloc( sizeof( rl_imageset_t ) + num_images * sizeof( rl_image_t* ) ); + + if ( imageset ) + { + imageset->num_images = num_images; + + for ( int i = 0; i < num_images; i++ ) + { + size_t image_size = ne32( *ptr.u32++ ); + imageset->images[ i ] = rl_image_create( ptr.v, image_size ); + + if ( !imageset->images[ i ] ) + { + for ( int j = i - 1; j >= 0; --j ) + { + rl_image_destroy( (void*)imageset->images[ j ] ); + } + + rl_free( imageset ); + return NULL; + } + + ptr.u8 += image_size; + } + + return imageset; + } + + return NULL; +} + +void rl_imageset_destroy( const rl_imageset_t* imageset ) +{ + for ( int i = imageset->num_images - 1; i >= 0; --i ) + { + rl_image_destroy( imageset->images[ i ] ); + } + + rl_free( (void*)imageset ); +} + void rl_image_blit_nobg( const rl_image_t* image, int x, int y ) { int x0 = 0; diff --git a/src/rl_image.h b/src/rl_image.h index 004c77f..33e0e31 100644 --- a/src/rl_image.h +++ b/src/rl_image.h @@ -26,10 +26,23 @@ typedef struct } rl_image_t; +typedef struct +{ + int num_images; + + const rl_image_t* images[ 0 ]; +} +rl_imageset_t; + /* Creates an image given the RLE-encoded data produced by rlrle. */ rl_image_t* rl_image_create( const void* data, size_t size ); /* Destroys an image. */ -#define rl_image_destroy( image ) do { rl_free( image ); } while ( 0 ) +#define rl_image_destroy( image ) do { rl_free( (void*)image ); } while ( 0 ) + +/* Creates an image set. */ +rl_imageset_t* rl_imageset_create( const void* data, size_t size ); +/* Destroyes an image set. */ +void rl_imageset_destroy( const rl_imageset_t* imageset ); /* Blits an image to the given background. */ void rl_image_blit_nobg( const rl_image_t* image, int x, int y );