Added a new InputStream interface, and LoadFromStream functions to resource classes

This commit is contained in:
Laurent Gomila 2011-07-17 12:21:47 +02:00
parent 73665bd50a
commit c5276ff30a
16 changed files with 736 additions and 192 deletions

View file

@ -41,6 +41,8 @@ namespace priv
class SoundFile;
}
class InputStream;
////////////////////////////////////////////////////////////
/// \brief Streamed music played from an audio file
///
@ -74,7 +76,7 @@ public :
///
/// \return True if loading succeeded, false if it failed
///
/// \see OpenFromMemory
/// \see OpenFromMemory, OpenFromStream
///
////////////////////////////////////////////////////////////
bool OpenFromFile(const std::string& filename);
@ -93,11 +95,29 @@ public :
///
/// \return True if loading succeeded, false if it failed
///
/// \see OpenFromFile
/// \see OpenFromFile, OpenFromStream
///
////////////////////////////////////////////////////////////
bool OpenFromMemory(const void* data, std::size_t sizeInBytes);
////////////////////////////////////////////////////////////
/// \brief Open a music from an audio file in a custom stream
///
/// This function doesn't start playing the music (call Play()
/// to do so).
/// Here is a complete list of all the supported audio formats:
/// ogg, wav, flac, aiff, au, raw, paf, svx, nist, voc, ircam,
/// w64, mat4, mat5 pvf, htk, sds, avr, sd2, caf, wve, mpc2k, rf64.
///
/// \param stream Source stream to read from
///
/// \return True if loading succeeded, false if it failed
///
/// \see OpenFromFile, OpenFromMemory
///
////////////////////////////////////////////////////////////
bool OpenFromStream(InputStream& stream);
////////////////////////////////////////////////////////////
/// \brief Get the total duration of the music
///
@ -131,6 +151,12 @@ protected :
private :
////////////////////////////////////////////////////////////
/// \brief Initialize the internal state after loading a new music
///
////////////////////////////////////////////////////////////
void Initialize();
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////

View file

@ -37,7 +37,13 @@
namespace sf
{
namespace priv
{
class SoundFile;
}
class Sound;
class InputStream;
////////////////////////////////////////////////////////////
/// \brief Storage for audio samples defining a sound
@ -78,7 +84,7 @@ public :
///
/// \return True if loading succeeded, false if it failed
///
/// \see LoadFromMemory, LoadFromSamples, SaveToFile
/// \see LoadFromMemory, LoadFromStream, LoadFromSamples, SaveToFile
///
////////////////////////////////////////////////////////////
bool LoadFromFile(const std::string& filename);
@ -95,11 +101,27 @@ public :
///
/// \return True if loading succeeded, false if it failed
///
/// \see LoadFromFile, LoadFromSamples, SaveToFile
/// \see LoadFromFile, LoadFromStream, LoadFromSamples
///
////////////////////////////////////////////////////////////
bool LoadFromMemory(const void* data, std::size_t sizeInBytes);
////////////////////////////////////////////////////////////
/// \brief Load the sound buffer from a custom stream
///
/// Here is a complete list of all the supported audio formats:
/// ogg, wav, flac, aiff, au, raw, paf, svx, nist, voc, ircam,
/// w64, mat4, mat5 pvf, htk, sds, avr, sd2, caf, wve, mpc2k, rf64.
///
/// \param stream Source stream to read from
///
/// \return True if loading succeeded, false if it failed
///
/// \see LoadFromFile, LoadFromMemory, LoadFromSamples
///
////////////////////////////////////////////////////////////
bool LoadFromStream(InputStream& stream);
////////////////////////////////////////////////////////////
/// \brief Load the sound buffer from an array of audio samples
///
@ -212,6 +234,16 @@ private :
friend class Sound;
////////////////////////////////////////////////////////////
/// \brief Initialize the internal state after loading a new sound
///
/// \param file Sound file providing access to the new loaded sound
///
/// \return True on succesful initialization, false on failure
///
////////////////////////////////////////////////////////////
bool Initialize(priv::SoundFile& file);
////////////////////////////////////////////////////////////
/// \brief Update the internal buffer with the cached audio samples
///
@ -273,9 +305,9 @@ private :
/// a sf::Image.
///
/// A sound buffer can be loaded from a file (see LoadFromFile()
/// for the complete list of supported formats), from memory
/// or directly from an array of samples. It can also be saved
/// back to a file.
/// for the complete list of supported formats), from memory, from
/// a custom stream (see sf::InputStream) or directly from an array
/// of samples. It can also be saved back to a file.
///
/// Sound buffers alone are not very useful: they hold the audio data
/// but cannot be played. To do so, you need to use the sf::Sound class,

View file

@ -41,6 +41,8 @@
namespace sf
{
class InputStream;
////////////////////////////////////////////////////////////
/// \brief Class for loading and manipulating character fonts
///
@ -86,7 +88,7 @@ public :
///
/// \return True if loading succeeded, false if it failed
///
/// \see LoadFromMemory
/// \see LoadFromMemory, LoadFromStream
///
////////////////////////////////////////////////////////////
bool LoadFromFile(const std::string& filename);
@ -96,9 +98,6 @@ public :
///
/// The supported font formats are: TrueType, Type 1, CFF,
/// OpenType, SFNT, X11 PCF, Windows FNT, BDF, PFR and Type 42.
/// Note that this function know nothing about the standard
/// fonts installed on the user's system, thus you can't
/// load them directly.
/// Warning: SFML cannot preload all the font data in this
/// function, so the buffer pointed by \a data has to remain
/// valid as long as the font is used.
@ -108,11 +107,29 @@ public :
///
/// \return True if loading succeeded, false if it failed
///
/// \see LoadFromFile
/// \see LoadFromFile, LoadFromStream
///
////////////////////////////////////////////////////////////
bool LoadFromMemory(const void* data, std::size_t sizeInBytes);
////////////////////////////////////////////////////////////
/// \brief Load the font from a custom stream
///
/// The supported font formats are: TrueType, Type 1, CFF,
/// OpenType, SFNT, X11 PCF, Windows FNT, BDF, PFR and Type 42.
/// Warning: SFML cannot preload all the font data in this
/// function, so the contents of \a stream have to remain
/// valid as long as the font is used.
///
/// \param stream Source stream to read from
///
/// \return True if loading succeeded, false if it failed
///
/// \see LoadFromFile, LoadFromMemory
///
////////////////////////////////////////////////////////////
bool LoadFromStream(InputStream& stream);
////////////////////////////////////////////////////////////
/// \brief Retrieve a glyph of the font
///
@ -278,6 +295,7 @@ private :
////////////////////////////////////////////////////////////
void* myLibrary; ///< Pointer to the internal library interface (it is typeless to avoid exposing implementation details)
void* myFace; ///< Pointer to the internal font face (it is typeless to avoid exposing implementation details)
void* myStreamRec; ///< Pointer to the stream rec instance (it is typeless to avoid exposing implementation details)
int* myRefCount; ///< Reference counter used by implicit sharing
mutable PageTable myPages; ///< Table containing the glyphs pages by character size
mutable std::vector<Uint8> myPixelBuffer; ///< Pixel buffer holding a glyph's pixels before being written to the texture
@ -293,9 +311,9 @@ private :
/// \class sf::Font
/// \ingroup graphics
///
/// Fonts can be loaded from a file or from memory, from
/// the most common types of fonts. See the LoadFromFile
/// function for the complete list of supported formats.
/// Fonts can be loaded from a file, from memory or from a custom
/// stream, and supports the most common types of fonts. See
/// the LoadFromFile function for the complete list of supported formats.
///
/// Once it is loaded, a sf::Font instance provides three
/// types of informations about the font:

View file

@ -41,6 +41,7 @@ namespace sf
class Renderer;
class RenderImage;
class RenderWindow;
class InputStream;
////////////////////////////////////////////////////////////
/// \brief Class for loading, manipulating and saving images
@ -86,7 +87,7 @@ public :
///
/// \return True if loading was successful
///
/// \see LoadFromMemory, LoadFromPixels, SaveToFile
/// \see LoadFromMemory, LoadFromStream, LoadFromPixels, SaveToFile
///
////////////////////////////////////////////////////////////
bool LoadFromFile(const std::string& filename);
@ -106,11 +107,30 @@ public :
///
/// \return True if loading was successful
///
/// \see LoadFromFile, LoadFromPixels, SaveToFile
/// \see LoadFromFile, LoadFromStream, LoadFromPixels
///
////////////////////////////////////////////////////////////
bool LoadFromMemory(const void* data, std::size_t size);
////////////////////////////////////////////////////////////
/// \brief Load the image from a custom stream
///
/// The supported image formats are bmp, png, tga, jpg, gif,
/// psd, hdr and pic. Some format options are not supported,
/// like progressive jpeg.
/// The maximum size for an image depends on the graphics
/// driver and can be retrieve with the GetMaximumSize function.
/// If this function fails, the image is left unchanged.
///
/// \param stream Source stream to read from
///
/// \return True if loading was successful
///
/// \see LoadFromFile, LoadFromMemory, LoadFromPixels
///
////////////////////////////////////////////////////////////
bool LoadFromStream(InputStream& stream);
////////////////////////////////////////////////////////////
/// \brief Load the image from an array of pixels
///

View file

@ -84,7 +84,7 @@ public :
///
/// \return True if loading succeeded, false if it failed
///
/// \see LoadFromMemory
/// \see LoadFromMemory, LoadFromStream
///
////////////////////////////////////////////////////////////
bool LoadFromFile(const std::string& filename);
@ -101,11 +101,28 @@ public :
///
/// \return True if loading succeeded, false if it failed
///
/// \see LoadFromFile
/// \see LoadFromFile, LoadFromStream
///
////////////////////////////////////////////////////////////
bool LoadFromMemory(const std::string& shader);
////////////////////////////////////////////////////////////
/// \brief Load the shader from a custom stream
///
/// The source code must be a valid fragment shader in
/// GLSL language. GLSL is a C-like language dedicated
/// to OpenGL shaders; you'll probably need to read a
/// good documentation for it before writing your own shaders.
///
/// \param stream Source stream to read from
///
/// \return True if loading succeeded, false if it failed
///
/// \see LoadFromFile, LoadFromMemory
///
////////////////////////////////////////////////////////////
bool LoadFromStream(InputStream& stream);
////////////////////////////////////////////////////////////
/// \brief Change a float parameter of the shader
///

View file

@ -32,6 +32,7 @@
#include <SFML/Config.hpp>
#include <SFML/System/Clock.hpp>
#include <SFML/System/Err.hpp>
#include <SFML/System/InputStream.hpp>
#include <SFML/System/Lock.hpp>
#include <SFML/System/Mutex.hpp>
#include <SFML/System/Sleep.hpp>

View file

@ -0,0 +1,148 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#ifndef SFML_INPUTSTREAM_HPP
#define SFML_INPUTSTREAM_HPP
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Config.hpp>
namespace sf
{
////////////////////////////////////////////////////////////
/// \brief Abstract class for custom file input streams
///
////////////////////////////////////////////////////////////
class SFML_API InputStream
{
public :
////////////////////////////////////////////////////////////
/// \brief Virtual destructor
///
////////////////////////////////////////////////////////////
virtual ~InputStream() {}
////////////////////////////////////////////////////////////
/// \brief Read data from the stream
///
/// \param data Buffer where to copy the read data
/// \param size Desired number of bytes to read
///
/// \return The number of bytes actually read
///
////////////////////////////////////////////////////////////
virtual Int64 Read(char* data, Int64 size) = 0;
////////////////////////////////////////////////////////////
/// \brief Change the current reading position
///
/// \param position The position to seek to, from the beginning
///
/// \return The position actually seeked to, or -1 on error
///
////////////////////////////////////////////////////////////
virtual Int64 Seek(Int64 position) = 0;
////////////////////////////////////////////////////////////
/// \brief Return the current reading position in the stream
///
/// \return The current position, or -1 on error.
///
////////////////////////////////////////////////////////////
virtual Int64 GetPosition() = 0;
////////////////////////////////////////////////////////////
/// \brief Return the size of the stream
///
/// \return The total number of bytes available in the stream, or -1 on error
///
////////////////////////////////////////////////////////////
virtual Int64 GetSize() = 0;
};
} // namespace sf
#endif // SFML_INPUTSTREAM_HPP
////////////////////////////////////////////////////////////
/// \class sf::InputStream
/// \ingroup system
///
/// This class allows users to define their own file input sources
/// from which SFML can load resources.
///
/// SFML resource classes like sf::Image 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:
/// \code
/// // custom stream class that reads from inside a zip file
/// class ZipStream : public sf::InputStream
/// {
/// public :
///
/// ZipStream(std::string archive);
///
/// bool Open(std::string filename);
///
/// Int64 Read(char* data, Int64 size);
///
/// Int64 Seek(Int64 position);
///
/// Int64 GetPosition();
///
/// Int64 GetSize();
///
/// private :
///
/// ...
/// };
///
/// // now you can load images...
/// sf::Image image;
/// ZipStream stream("resources.zip");
/// stream.Open("images/img.png");
/// image.LoadFromStream(stream);
///
/// // musics...
/// sf::Music music;
/// ZipStream stream("resources.zip");
/// stream.Open("musics/msc.ogg");
/// music.OpenFromStream(stream);
///
/// // etc.
/// \endcode
///
////////////////////////////////////////////////////////////