SFML 3.0.0

- Main Page
- Topics
- Namespaces
- Classes
- Files

- Class List
- Class Index
- Class Hierarchy
- Class Members

Loading...
Searching...
No Matches
Public Member Functions | List of all members
sf::InputStream Class Referenceabstract
System module

Abstract class for custom file input streams. More...

`#include <SFML/System/InputStream.hpp>`

Inheritance diagram for sf::InputStream:

## Public Member Functions
virtual | ~InputStream ()=default
| Virtual destructor.
virtual std::optional< std::size_t > | read (void *data, std::size_t size)=0
| Read data from the stream.
virtual std::optional< std::size_t > | seek (std::size_t position)=0
| Change the current reading position.
virtual std::optional< std::size_t > | tell ()=0
| Get the current reading position in the stream.
virtual std::optional< std::size_t > | getSize ()=0
| Return the size of the stream.

## Detailed Description

Abstract class for custom file input streams.

This class allows users to define their own file input sources from which SFML can load resources.

SFML resource classes like `sf::Texture` and `sf::SoundBuffer` provide `loadFromFile` and `loadFromMemory` functions, which read data from conventional sources. However, if you have data coming from a different source (over a network, embedded, encrypted, compressed, etc) you can derive your own class from `sf::InputStream` and load SFML resources with their `loadFromStream` function.

Usage example:

// custom stream class that reads from inside a zip file
class ZipStream : public sf::InputStream
{
public:
ZipStream(const std::string& archive);
[[nodiscard]] bool open(const std::filesystem::path& filename);
[[nodiscard]] std::optional<std::size_t> read(void* data, std::size_t size);
[[nodiscard]] std::optional<std::size_t> seek(std::size_t position);
[[nodiscard]] std::optional<std::size_t> tell();
std::optional<std::size_t> getSize();
private:
...
};
// now you can load textures...
ZipStream stream("resources.zip");
if (!stream.open("images/img.png"))
{
// Handle error...
}
const sf::Texture texture(stream);
// musics...
sf::Music music;
ZipStream stream("resources.zip");
if (!stream.open("musics/msc.ogg"))
{
// Handle error...
}
if (!music.openFromStream(stream))
{
// Handle error...
}
// etc.
sf::InputStream
Abstract class for custom file input streams.
Definition InputStream.hpp:46
sf::InputStream::tell
virtual std::optional< std::size_t > tell()=0
Get the current reading position in the stream.
sf::InputStream::getSize
virtual std::optional< std::size_t > getSize()=0
Return the size of the stream.
sf::InputStream::read
virtual std::optional< std::size_t > read(void *data, std::size_t size)=0
Read data from the stream.
sf::InputStream::seek
virtual std::optional< std::size_t > seek(std::size_t position)=0
Change the current reading position.
sf::Music
Streamed music played from an audio file.
Definition Music.hpp:53
sf::Music::openFromStream
bool openFromStream(InputStream &stream)
Open a music from an audio file in a custom stream.
sf::Texture
Image living on the graphics card that can be used for drawing.
Definition Texture.hpp:56

See also
  `FileInputStream`, `MemoryInputStream`

Definition at line 45 of file InputStream.hpp.

## Constructor & Destructor Documentation

## ◆ ~InputStream()

virtual sf::InputStream::~InputStream | ( |  | ) |
| virtual default

Virtual destructor.

## Member Function Documentation

## ◆ getSize()

virtual std::optional< std::size_t > sf::InputStream::getSize | ( |  | ) |
| pure virtual

Return the size of the stream.

Returns
  The total number of bytes available in the stream, or `std::nullopt` on error

Implemented in sf::FileInputStream, and sf::MemoryInputStream.

## ◆ read()

virtual std::optional< std::size_t > sf::InputStream::read | ( | void * | data,
|  | std::size_t | size )
| nodiscard pure virtual

Read data from the stream.

After reading, the stream's reading position must be advanced by the amount of bytes read.

Parameters

  data | Buffer where to copy the read data
  size | Desired number of bytes to read

Returns
  The number of bytes actually read, or `std::nullopt` on error

Implemented in sf::FileInputStream, and sf::MemoryInputStream.

## ◆ seek()

virtual std::optional< std::size_t > sf::InputStream::seek | ( | std::size_t | position | ) |
| nodiscard pure virtual

Change the current reading position.

Parameters

  position | The position to seek to, from the beginning

Returns
  The position actually sought to, or `std::nullopt` on error

Implemented in sf::FileInputStream, and sf::MemoryInputStream.

## ◆ tell()

virtual std::optional< std::size_t > sf::InputStream::tell | ( |  | ) |
| nodiscard pure virtual

Get the current reading position in the stream.

Returns
  The current position, or `std::nullopt` on error.

Implemented in sf::FileInputStream, and sf::MemoryInputStream.

The documentation for this class was generated from the following file:

- InputStream.hpp