From 6e251c54152af6666d0bbb2c90bf9b491f5140ac Mon Sep 17 00:00:00 2001 From: Laurent Gomila Date: Thu, 21 Jul 2011 20:38:54 +0200 Subject: [PATCH] Added InputStream --- include/SFML/Audio/Music.h | 11 +++ include/SFML/Config.h | 49 +++++------- include/SFML/Graphics/Font.h | 11 +++ include/SFML/Graphics/Image.h | 11 +++ include/SFML/Graphics/Shader.h | 11 +++ include/SFML/System.h | 1 + include/SFML/System/InputStream.h | 54 ++++++++++++++ src/SFML/Audio/Music.cpp | 21 +++++- src/SFML/Audio/MusicStruct.h | 2 + src/SFML/CallbackStream.h | 120 ++++++++++++++++++++++++++++++ src/SFML/Graphics/Font.cpp | 20 +++++ src/SFML/Graphics/FontStruct.h | 2 + src/SFML/Graphics/Image.cpp | 21 ++++++ src/SFML/Graphics/Shader.cpp | 21 ++++++ src/SFML/System/CMakeLists.txt | 1 + 15 files changed, 323 insertions(+), 33 deletions(-) create mode 100644 include/SFML/System/InputStream.h create mode 100644 src/SFML/CallbackStream.h diff --git a/include/SFML/Audio/Music.h b/include/SFML/Audio/Music.h index 2f75570..526fe55 100644 --- a/include/SFML/Audio/Music.h +++ b/include/SFML/Audio/Music.h @@ -31,6 +31,7 @@ #include #include #include +#include //////////////////////////////////////////////////////////// @@ -54,6 +55,16 @@ CSFML_API sfMusic* sfMusic_CreateFromFile(const char* filename); //////////////////////////////////////////////////////////// CSFML_API sfMusic* sfMusic_CreateFromMemory(const void* data, size_t sizeInBytes); +//////////////////////////////////////////////////////////// +/// Create a new music and load it from a custom stream +/// +/// \param stream : Source stream to read from +/// +/// \return A new sfMusic object (NULL if failed) +/// +//////////////////////////////////////////////////////////// +CSFML_API sfMusic* sfMusic_CreateFromStream(sfInputStream* stream); + //////////////////////////////////////////////////////////// /// Destroy an existing music /// diff --git a/include/SFML/Config.h b/include/SFML/Config.h index 8e8f945..8241037 100644 --- a/include/SFML/Config.h +++ b/include/SFML/Config.h @@ -105,45 +105,32 @@ typedef int sfBool; //////////////////////////////////////////////////////////// -// Define portable types +// Define portable fixed-size types //////////////////////////////////////////////////////////// -#include -#include + +// All "common" platforms use the same size for char, short and int +// (basically there are 3 types for 3 sizes, so no other match is possible), +// we can use them without doing any kind of check // 8 bits integer types -#if UCHAR_MAX == 0xFF - typedef signed char sfInt8; - typedef unsigned char sfUint8; -#else - #error No 8 bits integer type for this platform -#endif +typedef signed char sfInt8; +typedef unsigned char sfUint8; // 16 bits integer types -#if USHRT_MAX == 0xFFFF - typedef signed short sfInt16; - typedef unsigned short sfUint16; -#elif UINT_MAX == 0xFFFF - typedef signed int sfInt16; - typedef unsigned int sfUint16; -#elif ULONG_MAX == 0xFFFF - typedef signed long sfInt16; - typedef unsigned long sfUint16; -#else - #error No 16 bits integer type for this platform -#endif +typedef signed short sfInt16; +typedef unsigned short sfUint16; // 32 bits integer types -#if USHRT_MAX == 0xFFFFFFFF - typedef signed short sfInt32; - typedef unsigned short sfUint32; -#elif UINT_MAX == 0xFFFFFFFF - typedef signed int sfInt32; - typedef unsigned int sfUint32; -#elif ULONG_MAX == 0xFFFFFFFF - typedef signed long sfInt32; - typedef unsigned long sfUint32; +typedef signed int sfInt32; +typedef unsigned int sfUint32; + +// 64 bits integer types +#if defined(_MSC_VER) + typedef signed __int64 sfInt64; + typedef unsigned __int64 sfUint64; #else - #error No 32 bits integer type for this platform + typedef signed long long sfInt64; + typedef unsigned long long sfUint64; #endif diff --git a/include/SFML/Graphics/Font.h b/include/SFML/Graphics/Font.h index a946019..7667c6b 100644 --- a/include/SFML/Graphics/Font.h +++ b/include/SFML/Graphics/Font.h @@ -30,6 +30,7 @@ //////////////////////////////////////////////////////////// #include #include +#include //////////////////////////////////////////////////////////// @@ -53,6 +54,16 @@ CSFML_API sfFont* sfFont_CreateFromFile(const char* filename); //////////////////////////////////////////////////////////// CSFML_API sfFont* sfFont_CreateFromMemory(const void* data, size_t sizeInBytes); +//////////////////////////////////////////////////////////// +/// Create a new image font a custom stream +/// +/// \param stream : Source stream to read from +/// +/// \return A new sfFont object, or NULL if it failed +/// +//////////////////////////////////////////////////////////// +CSFML_API sfFont* sfFont_CreateFromStream(sfInputStream* stream); + //////////////////////////////////////////////////////////// /// Copy an existing font /// diff --git a/include/SFML/Graphics/Image.h b/include/SFML/Graphics/Image.h index a323474..101ea41 100644 --- a/include/SFML/Graphics/Image.h +++ b/include/SFML/Graphics/Image.h @@ -32,6 +32,7 @@ #include #include #include +#include //////////////////////////////////////////////////////////// @@ -79,6 +80,16 @@ CSFML_API sfImage* sfImage_CreateFromFile(const char* filename); //////////////////////////////////////////////////////////// CSFML_API sfImage* sfImage_CreateFromMemory(const void* data, size_t sizeInBytes); +//////////////////////////////////////////////////////////// +/// Create a new image from a custom stream +/// +/// \param stream : Source stream to read from +/// +/// \return A new sfImage object, or NULL if it failed +/// +//////////////////////////////////////////////////////////// +CSFML_API sfImage* sfImage_CreateFromStream(sfInputStream* stream); + //////////////////////////////////////////////////////////// /// Copy an existing image /// diff --git a/include/SFML/Graphics/Shader.h b/include/SFML/Graphics/Shader.h index ff5e0e3..87aff57 100644 --- a/include/SFML/Graphics/Shader.h +++ b/include/SFML/Graphics/Shader.h @@ -30,6 +30,7 @@ //////////////////////////////////////////////////////////// #include #include +#include //////////////////////////////////////////////////////////// @@ -52,6 +53,16 @@ CSFML_API sfShader* sfShader_CreateFromFile(const char* filename); //////////////////////////////////////////////////////////// CSFML_API sfShader* sfShader_CreateFromMemory(const char* effect); +//////////////////////////////////////////////////////////// +/// Create a new shader from a custom stream +/// +/// \param stream : Source stream to read from +/// +/// \return A new sfShader object, or NULL if it failed +/// +//////////////////////////////////////////////////////////// +CSFML_API sfShader* sfShader_CreateFromStream(sfInputStream* stream); + //////////////////////////////////////////////////////////// /// Copy an existing shader /// diff --git a/include/SFML/System.h b/include/SFML/System.h index a6769bd..f57718e 100644 --- a/include/SFML/System.h +++ b/include/SFML/System.h @@ -31,6 +31,7 @@ #include #include +#include #include #include #include diff --git a/include/SFML/System/InputStream.h b/include/SFML/System/InputStream.h new file mode 100644 index 0000000..4229d43 --- /dev/null +++ b/include/SFML/System/InputStream.h @@ -0,0 +1,54 @@ +//////////////////////////////////////////////////////////// +// +// 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_H +#define SFML_INPUTSTREAM_H + +//////////////////////////////////////////////////////////// +// Headers +//////////////////////////////////////////////////////////// +#include + + +typedef sfInt64 (*sfInputStreamReadFunc)(char* data, sfInt64 size, void* userData); +typedef sfInt64 (*sfInputStreamSeekFunc)(sfInt64 position, void* userData); +typedef sfInt64 (*sfInputStreamTellFunc)(void* userData); +typedef sfInt64 (*sfInputStreamGetSizeFunc)(void* userData); + + +//////////////////////////////////////////////////////////// +/// \brief Set of callbacks that allow users to define custom file streams +/// +//////////////////////////////////////////////////////////// +typedef struct sfInputStream +{ + sfInputStreamReadFunc Read; ///< Function to read data from the stream + sfInputStreamSeekFunc Seek; ///< Function to set the current read position + sfInputStreamTellFunc Tell; ///< Function to get the current read position + sfInputStreamGetSizeFunc GetSize; ///< Function to get the total number of bytes in the stream + void* UserData; ///< User data that will be passed to the callbacks +} sfInputStream; + + +#endif // SFML_INPUTSTREAM_H diff --git a/src/SFML/Audio/Music.cpp b/src/SFML/Audio/Music.cpp index 1dd293f..1b52e1b 100644 --- a/src/SFML/Audio/Music.cpp +++ b/src/SFML/Audio/Music.cpp @@ -36,7 +36,6 @@ sfMusic* sfMusic_CreateFromFile(const char* filename) { sfMusic* music = new sfMusic; - if (!music->This.OpenFromFile(filename)) { delete music; @@ -53,7 +52,6 @@ sfMusic* sfMusic_CreateFromFile(const char* filename) sfMusic* sfMusic_CreateFromMemory(const void* data, size_t sizeInBytes) { sfMusic* music = new sfMusic; - if (!music->This.OpenFromMemory(data, sizeInBytes)) { delete music; @@ -64,6 +62,25 @@ sfMusic* sfMusic_CreateFromMemory(const void* data, size_t sizeInBytes) } +//////////////////////////////////////////////////////////// +/// Create a new music and load it from a custom stream +//////////////////////////////////////////////////////////// +sfMusic* sfMusic_CreateFromStream(sfInputStream* stream) +{ + CSFML_CHECK_RETURN(stream, NULL); + + sfMusic* music = new sfMusic; + music->Stream = CallbackStream(stream); + if (!music->This.OpenFromStream(music->Stream)) + { + delete music; + music = NULL; + } + + return music; +} + + //////////////////////////////////////////////////////////// /// Destroy an existing music //////////////////////////////////////////////////////////// diff --git a/src/SFML/Audio/MusicStruct.h b/src/SFML/Audio/MusicStruct.h index 80b653c..6d10345 100644 --- a/src/SFML/Audio/MusicStruct.h +++ b/src/SFML/Audio/MusicStruct.h @@ -29,6 +29,7 @@ // Headers //////////////////////////////////////////////////////////// #include +#include //////////////////////////////////////////////////////////// @@ -37,6 +38,7 @@ struct sfMusic { sf::Music This; + CallbackStream Stream; }; diff --git a/src/SFML/CallbackStream.h b/src/SFML/CallbackStream.h new file mode 100644 index 0000000..115f731 --- /dev/null +++ b/src/SFML/CallbackStream.h @@ -0,0 +1,120 @@ +//////////////////////////////////////////////////////////// +// +// 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_CALLBACKSTREAM_H +#define SFML_CALLBACKSTREAM_H + +//////////////////////////////////////////////////////////// +// Headers +//////////////////////////////////////////////////////////// +#include +#include + + +//////////////////////////////////////////////////////////// +/// \brief Adapts a CSFML input stream to a SFML input stream +/// +//////////////////////////////////////////////////////////// +class CallbackStream : public sf::InputStream +{ +public: + + //////////////////////////////////////////////////////////// + /// \brief Default constructor + /// + //////////////////////////////////////////////////////////// + CallbackStream() + { + } + + //////////////////////////////////////////////////////////// + /// \brief Constructor + /// + /// \param stream CSFML input stream callbacks + /// + //////////////////////////////////////////////////////////// + CallbackStream(sfInputStream* stream) : + myStream(*stream) + { + } + + //////////////////////////////////////////////////////////// + /// \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 sf::Int64 Read(char* data, sf::Int64 size) + { + return myStream.Read ? myStream.Read(data, size, myStream.UserData) : -1; + } + + //////////////////////////////////////////////////////////// + /// \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 sf::Int64 Seek(sf::Int64 position) + { + return myStream.Seek ? myStream.Seek(position, myStream.UserData) : -1; + } + + //////////////////////////////////////////////////////////// + /// \brief Return the current reading position in the stream + /// + /// \return The current position, or -1 on error. + /// + //////////////////////////////////////////////////////////// + virtual sf::Int64 Tell() + { + return myStream.Tell ? myStream.Tell(myStream.UserData) : -1; + } + + //////////////////////////////////////////////////////////// + /// \brief Return the size of the stream + /// + /// \return The total number of bytes available in the stream, or -1 on error + /// + //////////////////////////////////////////////////////////// + virtual sf::Int64 GetSize() + { + return myStream.GetSize ? myStream.GetSize(myStream.UserData) : -1; + } + +private: + + //////////////////////////////////////////////////////////// + // Member data + //////////////////////////////////////////////////////////// + sfInputStream myStream; ///< The source CSFML input stream +}; + + +#endif // SFML_CALLBACKSTREAM_H diff --git a/src/SFML/Graphics/Font.cpp b/src/SFML/Graphics/Font.cpp index 2e4c510..ec8e07b 100644 --- a/src/SFML/Graphics/Font.cpp +++ b/src/SFML/Graphics/Font.cpp @@ -28,6 +28,7 @@ #include #include #include +#include //////////////////////////////////////////////////////////// @@ -62,6 +63,25 @@ sfFont* sfFont_CreateFromMemory(const void* data, size_t sizeInBytes) } +//////////////////////////////////////////////////////////// +/// Create a new font from a custom stream +//////////////////////////////////////////////////////////// +sfFont* sfFont_CreateFromStream(sfInputStream* stream) +{ + CSFML_CHECK_RETURN(stream, NULL); + + sfFont* font = new sfFont; + font->Stream = CallbackStream(stream); + if (!font->This.LoadFromStream(font->Stream)) + { + delete font; + font = NULL; + } + + return font; +} + + //////////////////////////////////////////////////////////// /// Copy an existing font //////////////////////////////////////////////////////////// diff --git a/src/SFML/Graphics/FontStruct.h b/src/SFML/Graphics/FontStruct.h index 8336194..44195ed 100644 --- a/src/SFML/Graphics/FontStruct.h +++ b/src/SFML/Graphics/FontStruct.h @@ -30,6 +30,7 @@ //////////////////////////////////////////////////////////// #include #include +#include #include @@ -40,6 +41,7 @@ struct sfFont { sf::Font This; std::map Images; + CallbackStream Stream; }; diff --git a/src/SFML/Graphics/Image.cpp b/src/SFML/Graphics/Image.cpp index 33761b2..1b4d879 100644 --- a/src/SFML/Graphics/Image.cpp +++ b/src/SFML/Graphics/Image.cpp @@ -29,6 +29,7 @@ #include #include #include +#include //////////////////////////////////////////////////////////// @@ -99,6 +100,26 @@ sfImage* sfImage_CreateFromMemory(const void* data, size_t sizeInBytes) } +//////////////////////////////////////////////////////////// +/// Create a new image from a custom stream +//////////////////////////////////////////////////////////// +sfImage* sfImage_CreateFromStream(sfInputStream* stream) +{ + CSFML_CHECK_RETURN(stream, NULL); + + sfImage* image = new sfImage; + + CallbackStream sfmlStream(stream); + if (!image->This->LoadFromStream(sfmlStream)) + { + delete image; + image = NULL; + } + + return image; +} + + //////////////////////////////////////////////////////////// /// Copy an existing image //////////////////////////////////////////////////////////// diff --git a/src/SFML/Graphics/Shader.cpp b/src/SFML/Graphics/Shader.cpp index ee542d4..1ed221c 100644 --- a/src/SFML/Graphics/Shader.cpp +++ b/src/SFML/Graphics/Shader.cpp @@ -29,6 +29,7 @@ #include #include #include +#include //////////////////////////////////////////////////////////// @@ -65,6 +66,26 @@ sfShader* sfShader_CreateFromMemory(const char* effect) } +//////////////////////////////////////////////////////////// +/// Create a new shader from a custom stream +//////////////////////////////////////////////////////////// +sfShader* sfShader_CreateFromStream(sfInputStream* stream) +{ + CSFML_CHECK_RETURN(stream, NULL); + + sfShader* shader = new sfShader; + + CallbackStream sfmlStream(stream); + if (!shader->This.LoadFromStream(sfmlStream)) + { + delete shader; + shader = NULL; + } + + return shader; +} + + //////////////////////////////////////////////////////////// /// Copy an existing clock //////////////////////////////////////////////////////////// diff --git a/src/SFML/System/CMakeLists.txt b/src/SFML/System/CMakeLists.txt index d8f295c..b1d0baf 100644 --- a/src/SFML/System/CMakeLists.txt +++ b/src/SFML/System/CMakeLists.txt @@ -7,6 +7,7 @@ set(SRC ${SRCROOT}/Clock.cpp ${SRCROOT}/ClockStruct.h ${INCROOT}/Clock.h + ${INCROOT}/InputStream.h ${SRCROOT}/Mutex.cpp ${SRCROOT}/MutexStruct.h ${INCROOT}/Mutex.h