diff --git a/src/graphics/cshape.cpp b/src/graphics/cshape.cpp index 4bec164ab..a3871f359 100644 --- a/src/graphics/cshape.cpp +++ b/src/graphics/cshape.cpp @@ -216,16 +216,20 @@ void cShape::CacheAlphaMask() { } void cShape::CacheColors() { - Uint32 size = ( mSrcRect.Right - mSrcRect.Left ) * ( mSrcRect.Bottom - mSrcRect.Top ) ; + mTexture->Lock(); + + Uint32 size = ( mSrcRect.Right - mSrcRect.Left ) * ( mSrcRect.Bottom - mSrcRect.Top ) * mTexture->Channels(); eeSAFE_DELETE_ARRAY( mPixels ); - mPixels = new eeColorA[ size ]; - mTexture->Lock(); + mPixels = new Uint8[ size ]; eeInt rY = 0; eeInt rX = 0; eeInt rW = mSrcRect.Right - mSrcRect.Left; + eeColorA tColor; + Uint32 Channels = mTexture->Channels(); + eeInt Pos; for ( eeInt y = mSrcRect.Top; y < mSrcRect.Bottom; y++ ) { rY = y - mSrcRect.Top; @@ -233,7 +237,14 @@ void cShape::CacheColors() { for ( eeInt x = mSrcRect.Left; x < mSrcRect.Right; x++ ) { rX = x - mSrcRect.Left; - mPixels[ rX + rY * rW ] = mTexture->GetPixel( x, y ); + tColor = mTexture->GetPixel( x, y ); + + Pos = ( rX + rY * rW ) * Channels; + + if ( Channels >= 1 ) mPixels[ Pos ] = tColor.R(); + if ( Channels >= 2 ) mPixels[ Pos + 1 ] = tColor.G(); + if ( Channels >= 3 ) mPixels[ Pos + 2 ] = tColor.B(); + if ( Channels >= 4 ) mPixels[ Pos + 3 ] = tColor.A(); } } @@ -248,7 +259,7 @@ Uint8 cShape::GetAlphaAt( const Int32& X, const Int32& Y ) { return mAlpha[ X + Y * ( mSrcRect.Right - mSrcRect.Left ) ]; if ( NULL != mPixels ) - return mPixels[ X + Y * ( mSrcRect.Right - mSrcRect.Left ) ].A(); + return mPixels[ ( X + Y * ( mSrcRect.Right - mSrcRect.Left ) ) * mTexture->Channels() + 3 ]; CacheAlphaMask(); @@ -259,8 +270,19 @@ eeColorA cShape::GetColorAt( const Int32& X, const Int32& Y ) { if ( mTexture->LocalCopy() ) return mTexture->GetPixel( mSrcRect.Left + X, mSrcRect.Right + Y ); - if ( NULL != mPixels ) - return mPixels[ X + Y * ( mSrcRect.Right - mSrcRect.Left ) ]; + if ( NULL != mPixels ) { + Uint32 Channels = mTexture->Channels(); + eeUint Pos = ( X + Y * ( mSrcRect.Right - mSrcRect.Left ) ) * Channels; + + if ( 4 == Channels ) + return eeColorA( mPixels[ Pos ], mPixels[ Pos + 1 ], mPixels[ Pos + 2 ], mPixels[ Pos + 3 ] ); + else if ( 3 == Channels ) + return eeColorA( mPixels[ Pos ], mPixels[ Pos + 1 ], mPixels[ Pos + 2 ], 255 ); + else if ( 2 == Channels ) + return eeColorA( mPixels[ Pos ], mPixels[ Pos + 1 ], 255, 255 ); + else + return eeColorA( mPixels[ Pos ], 255, 255, 255 ); + } CacheColors(); @@ -268,11 +290,17 @@ eeColorA cShape::GetColorAt( const Int32& X, const Int32& Y ) { } void cShape::SetColorAt( const Int32& X, const Int32& Y, const eeColorA& Color ) { - if ( NULL != mPixels ) - mPixels[ X + Y * ( mSrcRect.Right - mSrcRect.Left ) ] = Color; - else { + if ( NULL != mPixels ) { + Uint32 Channels = mTexture->Channels(); + eeUint Pos = ( X + Y * ( mSrcRect.Right - mSrcRect.Left ) ) * Channels; + + if ( Channels >= 1 ) mPixels[ Pos ] = Color.R(); + if ( Channels >= 2 ) mPixels[ Pos + 1 ] = Color.G(); + if ( Channels >= 3 ) mPixels[ Pos + 2 ] = Color.B(); + if ( Channels >= 4 ) mPixels[ Pos + 3 ] = Color.A(); + } else { CacheColors(); - mPixels[ X + Y * ( mSrcRect.Right - mSrcRect.Left ) ] = Color; + SetColorAt( X, Y, Color ); } } @@ -281,7 +309,7 @@ void cShape::ClearCache() { eeSAFE_DELETE_ARRAY( mAlpha ); } -eeColorA * cShape::Lock() { +Uint8 * cShape::Lock() { CacheColors(); return &mPixels[0]; @@ -296,7 +324,17 @@ bool cShape::Unlock( const bool& KeepData, const bool& Modified ) { if ( PreviousTexture != (Int32)mTexture->Texture() ) glBindTexture(GL_TEXTURE_2D, mTexture->Texture() ); - glTexSubImage2D( GL_TEXTURE_2D, 0, mSrcRect.Left, mSrcRect.Top, mSrcRect.Size().Width(), mSrcRect.Size().Height(), GL_RGBA, GL_UNSIGNED_BYTE, reinterpret_cast ( &mPixels[0] ) ); + Uint32 Channels = mTexture->Channels(); + Uint32 Channel = GL_RGBA; + + if ( 3 == Channels ) + Channel = GL_RGB; + else if ( 2 == Channels ) + Channel = GL_LUMINANCE_ALPHA; + else if ( 1 == Channels ) + Channel = GL_ALPHA; + + glTexSubImage2D( GL_TEXTURE_2D, 0, mSrcRect.Left, mSrcRect.Top, mSrcRect.Size().Width(), mSrcRect.Size().Height(), Channel, GL_UNSIGNED_BYTE, reinterpret_cast ( &mPixels[0] ) ); if ( PreviousTexture != (Int32)mTexture->Texture() ) glBindTexture(GL_TEXTURE_2D, PreviousTexture); diff --git a/src/graphics/cshape.hpp b/src/graphics/cshape.hpp index 79fc16f66..8c909380c 100644 --- a/src/graphics/cshape.hpp +++ b/src/graphics/cshape.hpp @@ -75,7 +75,7 @@ class EE_API cShape { void ClearCache(); - eeColorA * Lock(); + Uint8 * Lock(); bool Unlock( const bool& KeepData = false, const bool& Modified = false ); @@ -87,7 +87,7 @@ class EE_API cShape { bool SaveToFile(const std::string& filepath, const EE_SAVETYPE& Format); protected: - eeColorA * mPixels; + Uint8 * mPixels; Uint8 * mAlpha; std::string mName; Uint32 mId; diff --git a/src/graphics/cshapemanager.cpp b/src/graphics/cshapemanager.cpp index 37b49d335..3a366d38d 100644 --- a/src/graphics/cshapemanager.cpp +++ b/src/graphics/cshapemanager.cpp @@ -38,7 +38,7 @@ cShape * cShapeManager::GetAt( const Uint32& Index ) { cShape * cShapeManager::Get( const Uint32& Hash ) { if ( 0 != Hash ) { - for ( Uint32 i = 0; i < mShapes.size(); i++ ) { + for ( Uint32 i = mShapes.size() - 1; i >= 0; i-- ) { if ( NULL != mShapes[i] && mShapes[i]->Id() == Hash ) return mShapes[i]; } diff --git a/src/graphics/ctexture.cpp b/src/graphics/ctexture.cpp index 0adb5b05c..9f6c25a3d 100755 --- a/src/graphics/ctexture.cpp +++ b/src/graphics/ctexture.cpp @@ -129,7 +129,7 @@ Uint8 * cTexture::Lock( const bool& ForceRGBA ) { if ( !mLocked ) { if ( ForceRGBA ) mChannels = 4; - + GLint PreviousTexture; glGetIntegerv(GL_TEXTURE_BINDING_2D, &PreviousTexture); @@ -223,7 +223,7 @@ eeColorA cTexture::GetPixel( const eeUint& x, const eeUint& y ) { } eeUint Pos = ( x + y * mWidth ) * mChannels; - + if ( 4 == mChannels ) return eeColorA( mPixels[ Pos ], mPixels[ Pos + 1 ], mPixels[ Pos + 2 ], mPixels[ Pos + 3 ] ); else if ( 3 == mChannels ) @@ -241,16 +241,10 @@ void cTexture::SetPixel(const eeUint& x, const eeUint& y, const eeColorA& Color) eeUint Pos = ( x + y * mWidth ) * mChannels; - for ( Uint32 i = 0; i < mChannels; i++ ) { - if ( 0 == i ) - mPixels[ Pos + i ] = Color.R(); - else if ( 1 == i ) - mPixels[ Pos + i ] = Color.G(); - else if ( 2 == i ) - mPixels[ Pos + i ] = Color.B(); - else if ( 3 == i ) - mPixels[ Pos + i ] = Color.A(); - } + if ( mChannels >= 1 ) mPixels[ Pos ] = Color.R(); + if ( mChannels >= 2 ) mPixels[ Pos + 1 ] = Color.G(); + if ( mChannels >= 3 ) mPixels[ Pos + 2 ] = Color.B(); + if ( mChannels >= 4 ) mPixels[ Pos + 3 ] = Color.A(); mModified = true; } @@ -368,7 +362,7 @@ const Uint32& cTexture::TexId() const { void cTexture::Allocate( const Uint32& size ) { if ( eeARRAY_SIZE( mPixels ) != size ) { ClearCache(); - + mPixels = new unsigned char[ size * mChannels ]; } } diff --git a/src/graphics/ctexturefactory.cpp b/src/graphics/ctexturefactory.cpp index 003bf64a6..ab36c901e 100755 --- a/src/graphics/ctexturefactory.cpp +++ b/src/graphics/ctexturefactory.cpp @@ -13,6 +13,8 @@ cTextureFactory::cTextureFactory() : { mTextures.clear(); mTextures.push_back( NULL ); + + mAppPath = AppPath(); } cTextureFactory::~cTextureFactory() { @@ -56,10 +58,17 @@ Uint32 cTextureFactory::PushTexture( const std::string& Filepath, const Uint32& eeInt MyWidth = ImgWidth; eeInt MyHeight = ImgHeight; + std::string FPath = Filepath; + + Int32 pos = StrStartsWith( mAppPath, FPath ); + + if ( -1 != pos && (Uint32)(pos + 1) < FPath.size() ) + FPath = FPath.substr( pos + 1 ); + Pos = FindFreeSlot(); Tex = mTextures[ Pos ] = new cTexture(); - Tex->Create( TexId, Width, Height, MyWidth, MyHeight, Mipmap, Channels, Filepath, ColorKey, ClampMode, CompressTexture ); + Tex->Create( TexId, Width, Height, MyWidth, MyHeight, Mipmap, Channels, FPath, ColorKey, ClampMode, CompressTexture ); Tex->TexId( Pos ); if ( !ColorKey.voidRGB ) @@ -201,7 +210,7 @@ void cTextureFactory::SetBlendFunc( const EE_RENDERALPHAS& blend, const bool& fo glBlendFunc(GL_DST_COLOR,GL_ZERO); break; case ALPHA_NONE: - // COMPILER WARNING MY BALLS + // AVOID COMPILER WARNING break; } diff --git a/src/graphics/ctexturefactory.hpp b/src/graphics/ctexturefactory.hpp index 14fcc7fd0..ce599f908 100755 --- a/src/graphics/ctexturefactory.hpp +++ b/src/graphics/ctexturefactory.hpp @@ -166,8 +166,16 @@ class EE_API cTextureFactory: public cSingleton, protected cMut */ Uint32 PushTexture( const std::string& Filepath, const Uint32& TexId, const eeUint& Width, const eeUint& Height, const eeUint& ImgWidth, const eeUint& ImgHeight, const bool& Mipmap, const eeUint& Channels, const eeRGB& ColorKey, const EE_CLAMP_MODE& ClampMode, const bool& CompressTexture, const bool& LocalCopy = false ); + /** Return a texture by it file path name + * @param Name File path name + * @return The texture, NULL if not exists. + */ cTexture * GetByName( const std::string& Name ); - + + /** Return a texture by it hash path name + * @param Hash The file path hash + * @return The texture, NULL if not exists + */ cTexture * GetByHash( const Uint32& Hash ); protected: cTextureFactory(); @@ -182,6 +190,8 @@ class EE_API cTextureFactory: public cSingleton, protected cMut std::vector mTextures; eeUint mMemSize; + + std::string mAppPath; std::queue mVectorFreeSlots; diff --git a/src/test/ee.cpp b/src/test/ee.cpp index 9d4869b46..372e40f13 100644 --- a/src/test/ee.cpp +++ b/src/test/ee.cpp @@ -348,10 +348,6 @@ void cEETest::Init() { mBuda = L"El mono ve el pez en el agua y sufre. Piensa que su mundo es el único que existe, el mejor, el real. Sufre porque es bueno y tiene compasión, lo ve y piensa: \"Pobre se está ahogando no puede respirar\". Y lo saca, lo saca y se queda tranquilo, por fin lo salvé. Pero el pez se retuerce de dolor y muere. Por eso te mostré el sueño, es imposible meter el mar en tu cabeza, que es un balde.\nPowered by Text Shrinker =)"; TTF.ShrinkText( mBuda, 400 ); - mTexLoader = new cTextureLoader( MyPath + "data/test.jpg" ); - mTexLoader->Threaded(true); - mTexLoader->Load(); - Launch(); } else { cout << "Failed to start EE++" << endl; @@ -381,6 +377,10 @@ void cEETest::LoadTextures() { TF->Allocate(40); + mTexLoader = new cTextureLoader( MyPath + "data/test.jpg" ); + mTexLoader->Threaded(true); + mTexLoader->Load(); + TN.resize(12); TNP.resize(12); @@ -701,8 +701,9 @@ void cEETest::Render() { if ( mTexLoader->IsLoaded() ) { cTexture * TexLoaded = TF->GetTexture( mTexLoader->TexId() ); - - TexLoaded->Draw( 0, 0 ); + + if ( NULL != TexLoaded ) + TexLoaded->Draw( 0, 0 ); } HWidth = EE->GetWidth() * 0.5f; diff --git a/src/utils/string.cpp b/src/utils/string.cpp index 5db1a0e61..3710644be 100644 --- a/src/utils/string.cpp +++ b/src/utils/string.cpp @@ -252,4 +252,21 @@ void StrCopy( char * Dst, const char * Src, eeUint DstSize ) { *Dst = 0; } +Int32 StrStartsWith( const std::string& Start, const std::string Str ) { + Int32 Pos = 0; + + if ( Str.size() >= Start.size() ) { + for ( Uint32 i = 0; i < Start.size(); i++ ) { + if ( Start[i] == Str[i] ) { + Pos = (Int32)i; + } else { + Pos = -1; + break; + } + } + } + + return Pos; +} + }} diff --git a/src/utils/string.hpp b/src/utils/string.hpp index 472306111..1c76b0da5 100755 --- a/src/utils/string.hpp +++ b/src/utils/string.hpp @@ -107,8 +107,20 @@ void EE_API InsertChar( std::wstring& str, const eeUint& pos, const Uint32& tcha /** @return A storage path for config files for every platform */ std::string EE_API StoragePath( std::string appname ); +/** Copy a string to another +* @param Dst Destination String +* @param Src Source String +* @param DstSize Destination Size +*/ void EE_API StrCopy( char * Dst, const char * Src, eeUint DstSize ); +/** Compare two strings from its beginning. +* @param Start String start +* @param Str String to compare +* @return The position of the last char compared ( -1 if fails ) +*/ +Int32 StrStartsWith( const std::string& Start, const std::string Str ); + }} #endif