Added InputStream

This commit is contained in:
Laurent Gomila committed 2011-07-21 20:38:54 +02:00
1 parent 56df80a7d3
commit 6e251c5415
15 files changed
+323 -33

No files matched your search

+11
View File
@@ -31,6 +31,7 @@
#include <SFML/Config.h>
#include <SFML/Audio/SoundStatus.h>
#include <SFML/Audio/Types.h>
#include <SFML/System/InputStream.h>
////////////////////////////////////////////////////////////
@@ -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
///
+18 -31
View File
@@ -105,45 +105,32 @@ typedef int sfBool;
////////////////////////////////////////////////////////////
// Define portable types
// Define portable fixed-size types
////////////////////////////////////////////////////////////
#include <stdlib.h>
#include <limits.h>
// 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
+11
View File
@@ -30,6 +30,7 @@
////////////////////////////////////////////////////////////
#include <SFML/Graphics/Glyph.h>
#include <SFML/Graphics/Types.h>
#include <SFML/System/InputStream.h>
////////////////////////////////////////////////////////////
@@ -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
///
+11
View File
@@ -32,6 +32,7 @@
#include <SFML/Graphics/Color.h>
#include <SFML/Graphics/Rect.h>
#include <SFML/Graphics/Types.h>
#include <SFML/System/InputStream.h>
////////////////////////////////////////////////////////////
@@ -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
///
+11
View File
@@ -30,6 +30,7 @@
////////////////////////////////////////////////////////////
#include <SFML/Config.h>
#include <SFML/Graphics/Types.h>
#include <SFML/System/InputStream.h>
////////////////////////////////////////////////////////////
@@ -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
///
+1
View File
@@ -31,6 +31,7 @@
#include <SFML/Config.h>
#include <SFML/System/Clock.h>
#include <SFML/System/InputStream.h>
#include <SFML/System/Mutex.h>
#include <SFML/System/Sleep.h>
#include <SFML/System/Thread.h>
+54
View File
@@ -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 <SFML/Config.h>
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
+19 -2
View File
@@ -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
////////////////////////////////////////////////////////////
+2
View File
@@ -29,6 +29,7 @@
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Audio/Music.hpp>
#include <SFML/CallbackStream.h>
////////////////////////////////////////////////////////////
@@ -37,6 +38,7 @@
struct sfMusic
{
sf::Music This;
CallbackStream Stream;
};
+120
View File
@@ -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 <SFML/System/InputStream.hpp>
#include <SFML/System/InputStream.h>
////////////////////////////////////////////////////////////
/// \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
+20
View File
@@ -28,6 +28,7 @@
#include <SFML/Graphics/Font.h>
#include <SFML/Graphics/FontStruct.h>
#include <SFML/Internal.h>
#include <SFML/CallbackStream.h>
////////////////////////////////////////////////////////////
@@ -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
////////////////////////////////////////////////////////////
+2
View File
@@ -30,6 +30,7 @@
////////////////////////////////////////////////////////////
#include <SFML/Graphics/Font.hpp>
#include <SFML/Graphics/ImageStruct.h>
#include <SFML/CallbackStream.h>
#include <map>
@@ -40,6 +41,7 @@ struct sfFont
{
sf::Font This;
std::map<unsigned int, sfImage> Images;
CallbackStream Stream;
};
+21
View File
@@ -29,6 +29,7 @@
#include <SFML/Graphics/ImageStruct.h>
#include <SFML/Graphics/RenderWindowStruct.h>
#include <SFML/Internal.h>
#include <SFML/CallbackStream.h>
////////////////////////////////////////////////////////////
@@ -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
////////////////////////////////////////////////////////////
+21
View File
@@ -29,6 +29,7 @@
#include <SFML/Graphics/ShaderStruct.h>
#include <SFML/Graphics/ImageStruct.h>
#include <SFML/Internal.h>
#include <SFML/CallbackStream.h>
////////////////////////////////////////////////////////////
@@ -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
////////////////////////////////////////////////////////////
+1
View File
@@ -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