From ce2f9f008c577c39adf7f5b43322639e3d6ed8f6 Mon Sep 17 00:00:00 2001 From: "spartanj@gmail.com" Date: Sun, 2 Dec 2012 00:40:05 -0300 Subject: [PATCH] Added loading for textures from iostream. Changed GetPosition to Tell in cIOStream. --- include/eepp/graphics/ctexturefactory.hpp | 10 ++ include/eepp/graphics/ctextureloader.hpp | 9 +- include/eepp/system/ciostream.hpp | 2 +- include/eepp/system/ciostreamfile.hpp | 5 +- include/eepp/system/ciostreammemory.hpp | 2 +- projects/linux/ee.creator.user | 6 +- src/eepp/graphics/ctexturefactory.cpp | 6 ++ src/eepp/graphics/ctextureloader.cpp | 100 +++++++++++++++++- src/eepp/graphics/cttffont.cpp | 18 +--- src/eepp/system/ciostreamfile.cpp | 18 ++-- src/eepp/system/ciostreammemory.cpp | 2 +- src/eepp/system/cpak.cpp | 2 +- .../external_shader/external_shader.cpp | 2 +- 13 files changed, 141 insertions(+), 41 deletions(-) diff --git a/include/eepp/graphics/ctexturefactory.hpp b/include/eepp/graphics/ctexturefactory.hpp index c6224f2b8..1a86baa48 100755 --- a/include/eepp/graphics/ctexturefactory.hpp +++ b/include/eepp/graphics/ctexturefactory.hpp @@ -62,6 +62,16 @@ class EE_API cTextureFactory : protected cMutex { */ Uint32 LoadFromMemory( const unsigned char* ImagePtr, const eeUint& Size, const bool& Mipmap = false, const EE_CLAMP_MODE& ClampMode = EE_CLAMP_TO_EDGE, const bool& CompressTexture = false, const bool& KeepLocalCopy = false ); + /** Load a Texture from a file path + * @param Stream The cIOStream instance + * @param mipmap Use mipmaps? + * @param ClampMode Defines the CLAMP MODE + * @param CompressTexture If use the DXT compression on the texture loading ( if the card can display them, will convert RGB to DXT1, RGBA to DXT5 ) + * @param KeepLocalCopy Keep the array data copy. ( usefull if want to reload the texture ) + * @return The internal Texture Id + */ + Uint32 LoadFromStream( cIOStream& Stream, const bool& mipmap = false, const EE_CLAMP_MODE& ClampMode = EE_CLAMP_TO_EDGE, const bool& CompressTexture = false, const bool& KeepLocalCopy = false ); + /** Load a Texture from a file path * @param filepath The path for the texture * @param mipmap Use mipmaps? diff --git a/include/eepp/graphics/ctextureloader.hpp b/include/eepp/graphics/ctextureloader.hpp index ec510df68..6a5771490 100644 --- a/include/eepp/graphics/ctextureloader.hpp +++ b/include/eepp/graphics/ctextureloader.hpp @@ -6,15 +6,12 @@ namespace EE { namespace Graphics { -#define TEX_LT_PATH (1) -#define TEX_LT_MEM (2) -#define TEX_LT_PACK (3) -#define TEX_LT_PIXELS (4) - class cTexture; class EE_API cTextureLoader : public cObjectLoader { public: + cTextureLoader( cIOStream& Stream, const bool& Mipmap = false, const EE_CLAMP_MODE& ClampMode = EE_CLAMP_TO_EDGE, const bool& CompressTexture = false, const bool& KeepLocalCopy = false ); + cTextureLoader( const std::string& Filepath, const bool& Mipmap = false, const EE_CLAMP_MODE& ClampMode = EE_CLAMP_TO_EDGE, const bool& CompressTexture = false, const bool& KeepLocalCopy = false ); cTextureLoader( const unsigned char * ImagePtr, const eeUint& Size, const bool& Mipmap = false, const EE_CLAMP_MODE& ClampMode = EE_CLAMP_TO_EDGE, const bool& CompressTexture = false, const bool& KeepLocalCopy = false ); @@ -52,6 +49,7 @@ class EE_API cTextureLoader : public cObjectLoader { bool mCompressTexture; bool mLocalCopy; cPack * mPack; + cIOStream * mStream; const Uint8 * mImagePtr; Uint32 mSize; @@ -70,6 +68,7 @@ class EE_API cTextureLoader : public cObjectLoader { void LoadFromMemory(); void LoadFromPack(); void LoadFromPixels(); + void LoadFromStream(); }; }} diff --git a/include/eepp/system/ciostream.hpp b/include/eepp/system/ciostream.hpp index 0a6af021a..3314eb4b4 100644 --- a/include/eepp/system/ciostream.hpp +++ b/include/eepp/system/ciostream.hpp @@ -19,7 +19,7 @@ class EE_API cIOStream { virtual ios_size Seek( ios_size position ) = 0; - virtual ios_size GetPosition() = 0; + virtual ios_size Tell() = 0; virtual ios_size GetSize() = 0; diff --git a/include/eepp/system/ciostreamfile.hpp b/include/eepp/system/ciostreamfile.hpp index 5c318937d..4ca8f4b14 100644 --- a/include/eepp/system/ciostreamfile.hpp +++ b/include/eepp/system/ciostreamfile.hpp @@ -9,7 +9,7 @@ namespace EE { namespace System { class EE_API cIOStreamFile : public cIOStream { public: - cIOStreamFile( const std::string& path, std::ios_base::openmode mode ); + cIOStreamFile( const std::string& path, std::ios_base::openmode mode = std::ios::in | std::ios::binary ); virtual ~cIOStreamFile(); @@ -19,13 +19,14 @@ class EE_API cIOStreamFile : public cIOStream { ios_size Seek( ios_size position ); - ios_size GetPosition(); + ios_size Tell(); ios_size GetSize(); bool IsOpen(); protected: std::fstream mFS; + ios_size mSize; }; }} diff --git a/include/eepp/system/ciostreammemory.hpp b/include/eepp/system/ciostreammemory.hpp index f07ee31fa..0d6a5c2a1 100644 --- a/include/eepp/system/ciostreammemory.hpp +++ b/include/eepp/system/ciostreammemory.hpp @@ -19,7 +19,7 @@ class EE_API cIOStreamMemory : public cIOStream { ios_size Seek( ios_size position ); - ios_size GetPosition(); + ios_size Tell(); ios_size GetSize(); diff --git a/projects/linux/ee.creator.user b/projects/linux/ee.creator.user index ddf3b6936..018289f37 100644 --- a/projects/linux/ee.creator.user +++ b/projects/linux/ee.creator.user @@ -1,6 +1,6 @@ - + ProjectExplorer.Project.ActiveTarget @@ -48,9 +48,9 @@ Desktop Desktop {388e5431-b31b-42b3-b9ad-9002d279d75d} - 8 + 0 0 - 2 + 0 /home/programming/eepp diff --git a/src/eepp/graphics/ctexturefactory.cpp b/src/eepp/graphics/ctexturefactory.cpp index c525d71a1..545ab4ad0 100755 --- a/src/eepp/graphics/ctexturefactory.cpp +++ b/src/eepp/graphics/ctexturefactory.cpp @@ -47,6 +47,12 @@ Uint32 cTextureFactory::LoadFromMemory( const unsigned char * ImagePtr, const ee return myTex.Id(); } +Uint32 cTextureFactory::LoadFromStream( cIOStream& Stream, const bool& Mipmap, const EE_CLAMP_MODE& ClampMode, const bool& CompressTexture, const bool& KeepLocalCopy ) { + cTextureLoader myTex( Stream, Mipmap, ClampMode, CompressTexture, KeepLocalCopy ); + myTex.Load(); + return myTex.Id(); +} + Uint32 cTextureFactory::Load( const std::string& Filepath, const bool& Mipmap, const EE_CLAMP_MODE& ClampMode, const bool& CompressTexture, const bool& KeepLocalCopy ) { cTextureLoader myTex( Filepath, Mipmap, ClampMode, CompressTexture, KeepLocalCopy ); myTex.Load(); diff --git a/src/eepp/graphics/ctextureloader.cpp b/src/eepp/graphics/ctextureloader.cpp index a48095e5a..12ed1bc06 100644 --- a/src/eepp/graphics/ctextureloader.cpp +++ b/src/eepp/graphics/ctextureloader.cpp @@ -6,8 +6,65 @@ #include #include +#define TEX_LT_PATH (1) +#define TEX_LT_MEM (2) +#define TEX_LT_PACK (3) +#define TEX_LT_PIXELS (4) +#define TEX_LT_STREAM (5) + namespace EE { namespace Graphics { +namespace IOCb +{ + // stb_image callbacks that operate on a sf::InputStream + int read(void* user, char* data, int size) + { + cIOStream * stream = static_cast(user); + return static_cast(stream->Read(data, size)); + } + + void skip(void* user, unsigned int size) + { + cIOStream * stream = static_cast(user); + stream->Seek(stream->Tell() + size); + } + + int eof(void* user) + { + cIOStream* stream = static_cast(user); + return stream->Tell() >= stream->GetSize(); + } +} + +cTextureLoader::cTextureLoader( cIOStream& Stream, + const bool& Mipmap, + const EE_CLAMP_MODE& ClampMode, + const bool& CompressTexture, + const bool& KeepLocalCopy +) : cObjectLoader( cObjectLoader::TextureLoader ), + mLoadType(TEX_LT_STREAM), + mPixels(NULL), + mTexId(0), + mImgWidth(0), + mImgHeight(0), + mFilepath(""), + mWidth(0), + mHeight(0), + mMipmap(Mipmap), + mChannels(0), + mClampMode(ClampMode), + mCompressTexture(CompressTexture), + mLocalCopy(KeepLocalCopy), + mPack(NULL), + mStream(&Stream), + mImagePtr(NULL), + mSize(0), + mColorKey(NULL), + mTexLoaded(false), + mIsDDS(false), + mIsDDSCompressed(0) +{ +} cTextureLoader::cTextureLoader( const std::string& Filepath, const bool& Mipmap, const EE_CLAMP_MODE& ClampMode, @@ -28,6 +85,7 @@ cTextureLoader::cTextureLoader( const std::string& Filepath, mCompressTexture(CompressTexture), mLocalCopy(KeepLocalCopy), mPack(NULL), + mStream(NULL), mImagePtr(NULL), mSize(0), mColorKey(NULL), @@ -58,6 +116,7 @@ cTextureLoader::cTextureLoader( const unsigned char * ImagePtr, mCompressTexture(CompressTexture), mLocalCopy(KeepLocalCopy), mPack(NULL), + mStream(NULL), mImagePtr(ImagePtr), mSize(Size), mColorKey(NULL), @@ -88,6 +147,7 @@ cTextureLoader::cTextureLoader( cPack * Pack, mCompressTexture(CompressTexture), mLocalCopy(KeepLocalCopy), mPack(Pack), + mStream(NULL), mImagePtr(NULL), mSize(0), mColorKey(NULL), @@ -121,6 +181,7 @@ cTextureLoader::cTextureLoader( const unsigned char * Pixels, mCompressTexture(CompressTexture), mLocalCopy(KeepLocalCopy), mPack(NULL), + mStream(NULL), mImagePtr(NULL), mSize(0), mColorKey(NULL), @@ -146,6 +207,8 @@ void cTextureLoader::Start() { LoadFromMemory(); else if ( TEX_LT_PACK == mLoadType ) LoadFromPack(); + else if ( TEX_LT_STREAM == mLoadType ) + LoadFromStream(); mTexLoaded = true; @@ -169,7 +232,7 @@ void cTextureLoader::LoadFromPath() { stbi_dds_info_from_memory( mPixels, mSize, &mImgWidth, &mImgHeight, &mChannels, &mIsDDSCompressed ); } else { - mPixels = stbi_load( mFilepath.c_str(), &mImgWidth, &mImgHeight, &mChannels, 0 ); + mPixels = stbi_load( mFilepath.c_str(), &mImgWidth, &mImgHeight, &mChannels, STBI_default ); } if ( NULL == mPixels ) @@ -205,13 +268,46 @@ void cTextureLoader::LoadFromMemory() { memcpy( mPixels, mImagePtr, mSize ); stbi_dds_info_from_memory( mPixels, mSize, &mImgWidth, &mImgHeight, &mChannels, &mIsDDSCompressed ); } else { - mPixels = stbi_load_from_memory( mImagePtr, mSize, &mImgWidth, &mImgHeight, &mChannels, 0 ); + mPixels = stbi_load_from_memory( mImagePtr, mSize, &mImgWidth, &mImgHeight, &mChannels, STBI_default ); } if ( NULL == mPixels ) cLog::instance()->Write( stbi_failure_reason() ); } +void cTextureLoader::LoadFromStream() { + if ( mStream->IsOpen() ) { + mSize = mStream->GetSize(); + + stbi_io_callbacks callbacks; + callbacks.read = &IOCb::read; + callbacks.skip = &IOCb::skip; + callbacks.eof = &IOCb::eof; + + if ( GLi->IsExtension( EEGL_EXT_texture_compression_s3tc ) ) + mIsDDS = 0 != stbi_dds_test_callbacks( &callbacks, mStream ); + + if ( mIsDDS ) { + mSize = mStream->GetSize(); + mPixels = (Uint8*) eeMalloc( mSize ); + + mStream->Seek( 0 ); + mStream->Read( reinterpret_cast ( mPixels ), mSize ); + + mStream->Seek( 0 ); + stbi_dds_info_from_callbacks( &callbacks, mStream, &mImgWidth, &mImgHeight, &mChannels, &mIsDDSCompressed ); + mStream->Seek( 0 ); + } else { + mStream->Seek( 0 ); + mPixels = stbi_load_from_callbacks( &callbacks, mStream, &mImgWidth, &mImgHeight, &mChannels, STBI_default ); + mStream->Seek( 0 ); + } + + if ( NULL == mPixels ) + cLog::instance()->Write( stbi_failure_reason() ); + } +} + void cTextureLoader::LoadFromPixels() { if ( !mLoaded && mTexLoaded ) { Uint32 tTexId = 0; diff --git a/src/eepp/graphics/cttffont.cpp b/src/eepp/graphics/cttffont.cpp index 9147f4946..830a25215 100755 --- a/src/eepp/graphics/cttffont.cpp +++ b/src/eepp/graphics/cttffont.cpp @@ -106,22 +106,6 @@ bool cTTFFont::iLoad( const eeUint& Size, EE_TTF_FONTSTYLE Style, const bool& Ve bool lastWasWidth = false; Uint32 ReqSize; - // Find the larger glyph - /* - int maxWidth = 0, maxHeight = 0; - - for ( eeUint i = 0; i < mNumChars; i++) { - TempGlyphSurface = mFont->GlyphRender( i, 0x00000000 ); - - maxWidth = eemax( mFont->Current()->Pixmap()->width, maxWidth ); - maxHeight = eemax( mFont->Current()->Pixmap()->rows, maxHeight ); - - hkSAFE_DELETE_ARRAY( TempGlyphSurface ); - } - - //ReqSize = mNumChars * mHeight * mHeight; - */ - // Find the best size for the texture ( aprox ) // Totally wild guessing, but it's working Int32 tWildGuessW = ( mAscent + PixelSep + OutlineTotal ); @@ -150,7 +134,7 @@ bool cTTFFont::iLoad( const eeUint& Size, EE_TTF_FONTSTYLE Style, const bool& Ve //Loop through all chars for ( eeUint i = 0; i < mNumChars; i++) { - TempGlyphSurface = mFont->GlyphRender( i, 0x00000000 ); + TempGlyphSurface = mFont->GlyphRender( i ); //New temp glyph eeGlyph TempGlyph; diff --git a/src/eepp/system/ciostreamfile.cpp b/src/eepp/system/ciostreamfile.cpp index 3483af746..087f40559 100644 --- a/src/eepp/system/ciostreamfile.cpp +++ b/src/eepp/system/ciostreamfile.cpp @@ -2,7 +2,9 @@ namespace EE { namespace System { -cIOStreamFile::cIOStreamFile( const std::string& path, std::ios_base::openmode mode ) { +cIOStreamFile::cIOStreamFile( const std::string& path, std::ios_base::openmode mode ) : + mSize(0) +{ mFS.open( path.c_str(), mode ); } @@ -36,21 +38,23 @@ ios_size cIOStreamFile::Seek( ios_size position ) { return position; } -ios_size cIOStreamFile::GetPosition() { +ios_size cIOStreamFile::Tell() { return mFS.tellg(); } ios_size cIOStreamFile::GetSize() { if ( IsOpen() ) { - ios_size Pos = GetPosition(); + if ( 0 == mSize ) { + ios_size Pos = Tell(); - mFS.seekg ( 0, std::ios::end ); + mFS.seekg ( 0, std::ios::end ); - ios_size Length = mFS.tellg(); + mSize = mFS.tellg(); - mFS.seekg ( Pos, std::ios::beg ); + mFS.seekg ( Pos, std::ios::beg ); + } - return Length; + return mSize; } return 0; diff --git a/src/eepp/system/ciostreammemory.cpp b/src/eepp/system/ciostreammemory.cpp index f1f2fd96c..398f6c412 100644 --- a/src/eepp/system/ciostreammemory.cpp +++ b/src/eepp/system/ciostreammemory.cpp @@ -48,7 +48,7 @@ ios_size cIOStreamMemory::Seek( ios_size position ) { return mPos; } -ios_size cIOStreamMemory::GetPosition() { +ios_size cIOStreamMemory::Tell() { return mPos; } diff --git a/src/eepp/system/cpak.cpp b/src/eepp/system/cpak.cpp index bf79ee14c..2c4adc3c0 100755 --- a/src/eepp/system/cpak.cpp +++ b/src/eepp/system/cpak.cpp @@ -335,7 +335,7 @@ bool cPak::EraseFiles( const std::vector& paths ) { std::vector data; for ( i = 0; i < uEntry.size(); i++ ) if ( ExtractFileToMemory( std::string( uEntry[i].filename ), data ) ) { - uEntry[i].file_position = nPf.fs->GetPosition(); + uEntry[i].file_position = nPf.fs->Tell(); uEntry[i].file_length = (Uint32)data.size(); nPf.fs->Write( reinterpret_cast(&data[0]), (std::streamsize)data.size() ); } diff --git a/src/examples/external_shader/external_shader.cpp b/src/examples/external_shader/external_shader.cpp index c204f4610..0c692072b 100644 --- a/src/examples/external_shader/external_shader.cpp +++ b/src/examples/external_shader/external_shader.cpp @@ -231,7 +231,7 @@ EE_MAIN_FUNC int main (int argc, char * argv []) /// VertexPointer assigns values by default to the attribute "dgl_Vertex" /// TextureCoordPointer to "dgl_MultiTexCoord0" - GLi->VertexPointer( 3, GL_FP, sizeof(eeVector3ff), reinterpret_cast ( &vertices[0] ), 0 ); + GLi->VertexPointer( 3, GL_FLOAT, sizeof(eeVector3ff), reinterpret_cast ( &vertices[0] ), 0 ); /// ColorPointer to "dgl_FrontColor" GLi->ColorPointer( 4, GL_FP, sizeof(eeColorAf), reinterpret_cast ( &colors[0] ), 0 );