The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

SDL2::rwops - General Interface to Read and Write Data Streams

SYNOPSIS

    use SDL2 qw[:rwops];

DESCRIPTION

This package provides a general interface for SDL to read and write data streams. It can easily be extended to files, memory, etc.

Functions

These functions may imported by name or with the :rwops tag.

SDL_RWFromFile( ... )

Creates a SDL2::RWops structure from a file.

        my $file = SDL_RWFromFile('myimage.bmp', 'rb');

Expected parameters include:

file - a UTF-8 string representing the filename to open
mode - an ASCII string representing the mode to be used for opening the file

The mode string is treated roughly the same as in a call to the C library's fopen(), even if SDL doesn't happen to use fopen() behind the scenes.

Available 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.

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.

This function supports Unicode filenames, but they must be encoded in UTF-8 format, regardless of the underlying operating system.

Returns a new SDL2::RWops structure on success or undef on failure; call SDL_GetErro( ) for more information.

SDL_RWFromFP( ... )

Create a SDL2::RWops structure from a standard I/O file pointer.

    use FFI::C::File;
    my $file = FFI::C::File->fopen( "foo.txt", "w" );
    my $ops  = SDL_RWFromFP( $file, SDL_FALSE );

Expected parameters include:

fp - FFI::C::File object which wraps the C FILE pointer
autoclose - SDL_TRUE to close the file handle when closing the SDL2::RWops, SDL_FALSE to leave the file handle open when the RWops is closed

Returns a new SDL2::RWops structure on success or undef on failure; call SDL_GetErro( ) for more information.

SDL_RWFromMem( ... )

Use this function to prepare a read-write memory buffer for use with SDL2::RWops.

    my $bitmap = ' ' x 100; # Must preallocate it in perl
    my $ops    = SDL_RWFromMem( $bitmap, length $bitmap );
    # ...write to $ops...
    print $bitmap; # contains everything you wrote

Expected parameters include:

mem - pointer to a buffer to feed an SDL2::RWops stream
size - the buffer size, in bytes

Returns a new SDL2::RWops structure on success or undef on failure; call SDL_GetErro( ) for more information.

This memory buffer is not copied by the RWops; the pointer you provide must remain valid until you close the stream. Closing the stream will not free the original buffer.

SDL_RWFromConstMem( ... )

Use this function to prepare a read-only memory buffer for use with RWops.

        my $bitmap = ...; # raw data
        my $rw  = SDL_RWFromConstMem( $bitmap, length $bitmap );
        my $img = SDL_LoadBMP_RW( $rw, 1 );
        # Do something with img

Expected parameters include:

mem - pointer to a read-only buffer to fee to an SDL2::RWops stream
size - the buffer size, in bytes

Returns a new SDL2::RWops structure on success or undef on failure; call SDL_GetErro( ) for more information.

This function sets up an SDL2::RWops struct based on a memory area of a certain size. It assumes the memory area is not writable.

Attempting to write to this RWops stream will report an error without writing to the memory buffer.

This memory buffer is not copied by the RWops; the pointer you provide must remain valid until you close the stream. Closing the stream will not free the original buffer.

SDL_AllocRW( ... )

Creates an empty, unpopulated SDL2::RWops structure.

        my $rw = SDL_AllocRW( );

Returns a new SDL2::RWops structure on success or undef on failure; call SDL_GetErro( ) for more information.

Applications do not need to use this function unless they are providing their own SDL_RWops implementation. If you just need a SDL_RWops to read/write a common data source, you should use the built-in implementations in SDL, like SDL_RWFromFile( ) or SDL_RWFromMem( ... ), etc.

You must free the returned pointer with SDL_FreeRW( ... ). Depending on your operating system and compiler, there may be a difference between the malloc( ) and free( ) your program uses and the versions SDL calls internally. Trying to mix the two can cause crashing such as segmentation faults. Since all SDL_RWops must free themselves when their close method is called, all SDL_RWops must be allocated through this function, so they can all be freed correctly with SDL_FreeRW( ).

SDL_FreeRW( ... )

Use this function to free an SDL2::RWops structure allocated by SDL_AllocRW( ... ).

        SDL_FreeRW( $rw );

Expected parameters include:

area - the SDL2::RWops structure to be freed

Applications do not need to use this function unless they are providing their own SDL2::RWops implementation. If you just need a SDL2::RWops to read/write a common data source, you should use the built-in implementations in SDL, like SDL_RWFromFile( ... ) or SDL_RWFromMem( ... ), etc, and call the close method on those SDL2::RWops pointers when you are done with them.

Only use SDL_FreeRW( ... ) on pointers returned by SDL_AllocRW( ). The pointer is invalid as soon as this function returns. Any extra memory allocated during creation of the SDL2::RWops is not freed by SDL_FreeRW( ... ); the programmer must be responsible for managing that memory in their close method.

SDL_RWsize( ... )

Get the size of the data stream in an SDL2::RWops.

Expected parameters include:

context - the SDL2::RWops structure to query

Returns the size of the data stream on success, -1 if unknown or a negative error code on failure; call SDL_GetError( ) for more information.

SDL_RWseek( ... )

Seek within an SDL2::RWops data stream.

This function seeks to byte offset, relative to whence.

Expected parameters include:

context - the SDL2::RWops structure to seek through
offset - an offset in bytes, relative to whence location; can be negative
whence - any of the RWops week (whence) values

Returns the final offset in the data stream after the seek or -1 on error.

SDL_RWtell( ... )

Determine the current read/write offset in an SDL2::RWops data stream.

Expected parameters include:

context - the SDL2::RWops structure to query

Returns the current offset in the stream, or -1 if the information can not be determined.

SDL_RWread( ... )

Read from a data source.

This function reads up to maxnum objects each of size size from the data source to the area pointed at by ptr. This function may read less objects than requested. It will return zero when there has been an error or the data stream is completely read.

Expected parameters include:

context - the SDL2::RWops structure to read
ptr - a pointer to a buffer to read data into
size - the size of each object to read, in bytes
maxnum - the maximum number of objects to be read

Returns the number of objects read, or 0 at error or end of file; call SDL_GetError( ) for more information.

SDL_RWwrite( ... )

Write to an SDL2::RWops data stream.

This function writes exactly num objects each of size size from the area pointed at by ptr to the stream. If this fails for any reason, it'll return less than num to demonstrate how far the write progressed. On success, it returns num.

Expected parameters include:

context - a pointer to an SDL2::RWops structure
ptr - a pointer to a buffer containing data to write
size - the size of an object to write, in bytes
num - the number of objects to write

Returns the number of objects written, which will be less than num on error; call SDL_GetError( ) for more information.

SDL_RWclose( ... )

Close and free an allocated SDL2::RWops structure.

This function releases any resources used by the stream and frees the structure itself with SDL_FreeRW( ... ).

Expected parameters include:

context - a pointer to an SDL2::RWops structure to close

Returns 0 on success, or -1 if the stream failed to flush to its output (e.g. to disk); call SDL_GetError( ) for more information.

SDL_LoadFile_RW( ... )

Load all the data from an SDL data stream.

The data is allocated with a zero byte at the end (null terminated) for convenience. This extra byte is not included in the value reported via datasize.

The data should be freed with SDL_free( ... ).

Expected parameters include:

src - the SDL2::RWops to read all available data from
datasize - if not undef, will store the number of bytes read
freesrc - if non-zero, calls SDL_RWclose( ... ) on src before returning

Returns the data, or undef if there was an error.

SDL_LoadFile( ... )

Load all the data from a file path.

The data is allocated with a zero byte at the end (null terminated) for convenience. This extra byte is not included in the value reported via datasize.

The data should be freed with SDL_free( ... ).

Expected parameters include:

file - the path to read all available data from
datasize - if not undef, will store the number of bytes read

Returns the data, or undef if there was an error.

SDL_ReadU8( ... )

Read an item of little endianness and return in native format.

Expected parameters include:

src - a SDL2::RWops structure to read from

Returns a 8-bit integer.

SDL_ReadLE16( ... )

Read an item of little endianness and return in native format.

Expected parameters include:

src - a SDL2::RWops structure to read from

Returns a 16-bit integer.

SDL_ReadBE16( ... )

Read an item of big endianness and return in native format.

Expected parameters include:

src - a SDL2::RWops structure to read from

Returns a 16-bit integer.

SDL_ReadLE32( ... )

Read an item of little endianness and return in native format.

Expected parameters include:

src - a SDL2::RWops structure to read from

Returns a 32-bit integer.

SDL_ReadBE32( ... )

Read an item of big endianness and return in native format.

Expected parameters include:

src - a SDL2::RWops structure to read from

Returns a 32-bit integer.

SDL_ReadLE64( ... )

Read an item of little endianness and return in native format.

Expected parameters include:

src - a SDL2::RWops structure to read from

Returns a 64-bit integer.

SDL_ReadBE64( ... )

Read an item of big endianness and return in native format.

Expected parameters include:

src - a SDL2::RWops structure to read from

Returns a 64-bit integer.

SDL_WriteU8( ... )

Write an item of native format to little endianness.

Expected parameters include:

dst - a SDL2::RWops structure to write to
value - 8-bit unsigned integer

Returns the amount of data written, in bytes.

SDL_WriteLE16( ... )

Write an item of native format to little endianness.

Expected parameters include:

dst - a SDL2::RWops structure to write to
value - 16-bit unsigned integer

Returns the amount of data written, in bytes.

SDL_WriteBE16( ... )

Write an item of native format to big endianness.

Expected parameters include:

dst - a SDL2::RWops structure to write to
value - 16-bit unsigned integer

Returns the amount of data written, in bytes.

SDL_WriteLE32( ... )

Write an item of native format to little endianness.

Expected parameters include:

dst - a SDL2::RWops structure to write to
value - 32-bit unsigned integer

Returns the amount of data written, in bytes.

SDL_WriteBE32( ... )

Write an item of native format to big endianness.

Expected parameters include:

dst - a SDL2::RWops structure to write to
value - 32-bit unsigned integer

Returns the amount of data written, in bytes.

SDL_WriteLE64( ... )

Write an item of native format to little endianness.

Expected parameters include:

dst - a SDL2::RWops structure to write to
value - 64-bit unsigned integer

Returns the amount of data written, in bytes.

SDL_WriteBE64( ... )

Write an item of native format to big endianness.

Expected parameters include:

dst - a SDL2::RWops structure to write to
value - 64-bit unsigned integer

Returns the amount of data written, in bytes.

Defined Values and Enumerations

These may be imported by name of with the :rwops tag.

RWops Types

SDL_RWOPS_UNKNOWN - Unknown stream type
SDL_RWOPS_WINFILE - Win32 file
SDL_RWOPS_STDFILE - Stdio file
SDL_RWOPS_JNIFILE - Android asset
SDL_RWOPS_MEMORY - Memory stream
SDL_RWOPS_MEMORY_RO - Read-Only memory stream
SDL_RWOPS_VITAFILE - Vita file (if applicable)

RWops Seek (whence) Values

RW_SEEK_SET - Seek from the beginning of data
RW_SEEK_CUR - Seek relative to current read point
RW_SEEK_END - Seek relative to the end of data

LICENSE

Copyright (C) Sanko Robinson.

This library is free software; you can redistribute it and/or modify it under the terms found in the Artistic License 2. Other copyrights, terms, and conditions may apply to data transmitted through this module.

AUTHOR

Sanko Robinson <sanko@cpan.org>