# The following example will load several png's from a single file to an array of SDL::Surface's. # Useful for e.g. levelfiles. use SDL; use SDL::Image; use SDL::RWOps; use SDL::Surface; # the file contains a 32-byte header with lengths of data blocks, followed by the data blocks itself my $file = '/path/to/file/containing_image_data.dat'; my $header = ''; # up to eight 32-bit integers specifying the length of the data blocks (images) my @images = ''; # we push the surfaces to that array later open(FH, "<$file") or die "Can't open file $file"; binmode(FH); read(FH, $header, 32); # read 32 bytes of data my @blocks = unpack( 'V*', $header ); # unpack the block sizes foreach my $block_size (@blocks) { if($block_size) { my $image = ''; read(FH, $image, $block_size); my $rw = SDL::RWOps->new_const_mem( $image ); push(@images, SDL::Image::load_PNG_rw( $rw ); } } close(FH); # ... now do something with the surfaces
SDL::RWOps is an ``undocumented'' feature of SDL, allowing you to use pointers to memory instead of files (though it can handle files too) for things such as images or samples. The primary advantage of this feature is that many libraries load files from the filesystem themselves, leaving you a bit stuck if you want to implement your own special file access, such as an archive format. Fortunately many libraries, such as SDL_image, provide additional methods designed to read from an SDL_RWops, so that you can provide the data in whatever way you like.
An example usage would be to put a bunch of resources in a zip file and use Zziplib to access them easily.
Mode Strings: "r" Open a file for reading. The file must exist. "w" Create an empty file for writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file. "a" Append to a file. Writing operations append data at the end of the file. The file is created if it does not exist. "r+" Open a file for update both reading and writing. The file must exist. "w+" Create an empty file for both reading and writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file. "a+" Open a file for reading and appending. All writing operations are performed at the end of the file, protecting the previous content to be overwritten. You can reposition (fseek, rewind) the internal pointer to anywhere in the file for reading, but writing operations will move it back to the end of file. The file is created if it does not exist.
NOTE: In order to open a file as a binary file, a ``b'' character has to be included in the mode string. This additional ``b'' character can either be appended at the end of the string (thus making the following compound modes: ``rb'', ``wb'', ``ab'', ``r+b'', ``w+b'', ``a+b'') or be inserted between the letter and the ``+'' sign for the mixed modes (``rb+'', ``wb+'', ``ab+''). Additional characters may follow the sequence, although they should have no effect. For example, ``t'' is sometimes appended to make explicit the file is a text file.
Note: This is not available under Win32, since files opened in an application on that platform cannot be used by a dynamically linked library.
Note: If the memory is not writable, use SDL::rw_from_const_mem instead.
my $rw = SDL::RWOps->from_const_mem( $image_data ); my $rw = SDL::RWOps->from_const_mem( $image_data, $size );
"from_const_mem" sets up a SDL::RWOps object based on a memory area of a certain size. The $size parameter is optional. It assumes the memory area is not writable. It returns a SDL::RWOps on success or undef on error.
Note: You must free any memory allocated with SDL::alloc_rw with SDL::free_rw.
1. A pointer to an SDL::RWOps structure 2. An offset in bytes. This can be a negative value. 3.SEEK_SET, SEEK_CUR, or SEEK_END. SEEK_SET seeks from the beginning of the file, SEEK_CUR from the current position, and SEEK_END from the end of the file.
SDL::rw_seek returns the final offset in the data source.
1. A pointer to an SDL::RWOps structure 2. A pointer to an area of memory to read data into 3. The size of each block of memory to read 4. The maximum number of memory blocks to read(it may read less)
It returns the number of memory blocks read, or -1 if the read failed.
1. A pointer to an SDL::RWOps structure 2. A pointer to an area in memory to read data from 3. The size of the memory blocks to write 4. The exact number of memory blocks to write
0n success, it returns the number of memory blocks you told it to write. If it couldn't write that exact number of blocks, or the write didn't work at all, it returns -1.