From 3bb3475238ea8be0b5735a0a1a12a6a1695c4c32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Lucas=20Golini?= Date: Fri, 26 Jan 2018 02:25:53 -0300 Subject: [PATCH] Set TextureFilter to Texture Atlas. --HG-- branch : dev-2.1 --- include/eepp/graphics/packerhelper.hpp | 7 +- include/eepp/graphics/textureatlasloader.hpp | 6 +- include/eepp/graphics/texturepacker.hpp | 12 ++- include/eepp/ui/tools/textureatlaseditor.hpp | 4 + src/eepp/graphics/textureatlasloader.cpp | 32 +++++-- src/eepp/graphics/texturepacker.cpp | 28 ++++-- src/eepp/ui/tools/textureatlaseditor.cpp | 21 ++++- src/eepp/ui/tools/textureatlasnew.cpp | 89 +++++++++++--------- src/eepp/ui/tools/textureatlasnew.hpp | 1 + 9 files changed, 137 insertions(+), 63 deletions(-) diff --git a/include/eepp/graphics/packerhelper.hpp b/include/eepp/graphics/packerhelper.hpp index 80e8d1f0e..f4eb5b8f2 100644 --- a/include/eepp/graphics/packerhelper.hpp +++ b/include/eepp/graphics/packerhelper.hpp @@ -5,6 +5,8 @@ namespace EE { namespace Graphics { namespace Private { +#pragma pack(push, 1) + #define HDR_NAME_SIZE 64 typedef struct sTextureRegionHdrSN { @@ -43,7 +45,8 @@ typedef struct sTextureAtlasHdrSN { Int32 Height; Uint32 PixelBorder; Uint32 Flags; - char Reserved[16]; + char TextureFilter; + char Reserved[15]; } sTextureAtlasHdr; #define HDR_TEXTURE_ATLAS_ALLOW_FLIPPING ( 1 << 0 ) @@ -53,6 +56,8 @@ typedef struct sTextureAtlasHdrSN { #define EE_TEXTURE_ATLAS_MAGIC ( ( 'E' << 0 ) | ( 'E' << 8 ) | ( 'T' << 16 ) | ( 'A' << 24 ) ) #define EE_TEXTURE_ATLAS_EXTENSION ".eta" +#pragma pack(pop) + }}} #endif diff --git a/include/eepp/graphics/textureatlasloader.hpp b/include/eepp/graphics/textureatlasloader.hpp index 2b8cea953..3353b2a4e 100644 --- a/include/eepp/graphics/textureatlasloader.hpp +++ b/include/eepp/graphics/textureatlasloader.hpp @@ -122,6 +122,10 @@ class EE_API TextureAtlasLoader { /** Sets a load notification callback. */ void setLoadCallback( GLLoadCallback LoadCallback ); + + sTextureAtlasHdr getTextureAtlasHeader(); + + void setTextureFilter( const Texture::TextureFilter& textureFilter ); protected: ResourceLoader mRL; std::string mTextureAtlasPath; @@ -132,7 +136,7 @@ class EE_API TextureAtlasLoader { bool mIsLoading; TextureAtlas * mTextureAtlas; GLLoadCallback mLoadCallback; - std::vector mTexuresLoaded; + std::vector mTexturesLoaded; typedef struct sTempTexAtlasS { sTextureHdr Texture; diff --git a/include/eepp/graphics/texturepacker.hpp b/include/eepp/graphics/texturepacker.hpp index c7a8c3abc..59d240f17 100644 --- a/include/eepp/graphics/texturepacker.hpp +++ b/include/eepp/graphics/texturepacker.hpp @@ -35,6 +35,7 @@ #include #include #include +#include #include namespace EE { namespace Graphics { @@ -55,11 +56,12 @@ class EE_API TexturePacker { * @param MaxWidth The maximum width that the texture atlas will use. * @param MaxHeight The maximum height that the texture atlas will use. * @param PixelDensity Indicates the device pixel density that represents the resources that will the packer pack. + * @param textureFilter Indicates with texture filter should be used when the texture of the atlas is loaded * @param ForcePowOfTwo Indicates that if the max with and height must be adjusted to fit a power of two texture. * @param PixelBorder Indicates how many pixels will be added to separate one image to another in the texture atlas. Usefull to avoid artifacts when rendered scaled TextureRegions. Use at least 1 pixel to separate images if you will scale any TextureRegion. * @param AllowFlipping Indicates if the images can be flipped inside the texture atlas. This is not compatible with eepp ( since it can't flip the textures back to the original orientation ). So avoid it for eepp. */ - TexturePacker( const Uint32& MaxWidth, const Uint32& MaxHeight, const EE_PIXEL_DENSITY& PixelDensity = PD_MDPI, const bool& ForcePowOfTwo = true, const Uint32& PixelBorder = 0, const bool& AllowFlipping = false ); + TexturePacker( const Uint32& MaxWidth, const Uint32& MaxHeight, const EE_PIXEL_DENSITY& PixelDensity = PD_MDPI, const bool& ForcePowOfTwo = true, const Uint32& PixelBorder = 0, const Texture::TextureFilter& textureFilter = Texture::TextureFilter::Linear, const bool& AllowFlipping = false ); ~TexturePacker(); @@ -92,11 +94,12 @@ class EE_API TexturePacker { * @param MaxWidth The maximum width that the texture atlas will use. * @param MaxHeight The maximum height that the texture atlas will use. * @param PixelDensity Indicates the device pixel density that represents the resources that will the packer pack. - * @param ForcePowOfTwo Indicates that if the max with and height must be adjusted to fit a power of two texture. + * @param textureFilter Indicates with texture filter should be used when the texture of the atlas is loaded * @param PixelBorder Indicates how many pixels will be added to separate one image to another in the texture atlas. Usefull to avoid artifacts when rendered scaled TextureRegions. Use at least 1 pixel to separate images if you will scale any TextureRegion. + * @param ForcePowOfTwo Indicates that if the max with and height must be adjusted to fit a power of two texture. * @param AllowFlipping Indicates if the images can be flipped inside the texture atlas. This is not compatible with eepp ( since it can't flip the textures back to the original orientation ). So avoid it for eepp. */ - void setOptions( const Uint32& MaxWidth, const Uint32& MaxHeight, const EE_PIXEL_DENSITY& PixelDensity = PD_MDPI, const bool& ForcePowOfTwo = true, const Uint32& PixelBorder = 0, const bool& AllowFlipping = false ); + void setOptions( const Uint32& MaxWidth, const Uint32& MaxHeight, const EE_PIXEL_DENSITY& PixelDensity = PD_MDPI, const bool& ForcePowOfTwo = true, const Uint32& PixelBorder = 0, const Texture::TextureFilter& textureFilter = Texture::TextureFilter::Linear, const bool& AllowFlipping = false ); /** @return The texture atlas to generate width. */ const Int32& getWidth() const; @@ -131,8 +134,9 @@ class EE_API TexturePacker { bool mForcePowOfTwo; Int32 mPixelBorder; EE_PIXEL_DENSITY mPixelDensity; + Texture::TextureFilter mTextureFilter; bool mSaveExtensions; - Image::SaveType mFormat; + Image::SaveType mFormat; TexturePacker * getChild() const; diff --git a/include/eepp/ui/tools/textureatlaseditor.hpp b/include/eepp/ui/tools/textureatlaseditor.hpp index b4d0853f6..be947b6e6 100644 --- a/include/eepp/ui/tools/textureatlaseditor.hpp +++ b/include/eepp/ui/tools/textureatlaseditor.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -52,6 +53,7 @@ class EE_API TextureAtlasEditor { UISpinBox * mSpinDestH; UIListBox * mTextureRegionList; UIWinMenu * mWinMenu; + UIDropDownList * mTextureFilterList; TextureAtlasTextureRegionEditor * mTextureRegionEditor; UITGEUpdater * mTGEU; @@ -89,6 +91,8 @@ class EE_API TextureAtlasEditor { void onHBOffset( const UIEvent * Event ); + void onTextureFilterChange( const UIEvent * Event ); + void onTextureAtlasLoaded( TextureAtlasLoader * TGLoader ); void update(); diff --git a/src/eepp/graphics/textureatlasloader.cpp b/src/eepp/graphics/textureatlasloader.cpp index 6b5c7ad8f..46fad8bcd 100644 --- a/src/eepp/graphics/textureatlasloader.cpp +++ b/src/eepp/graphics/textureatlasloader.cpp @@ -83,6 +83,21 @@ void TextureAtlasLoader::setLoadCallback( GLLoadCallback LoadCallback ) { mLoadCallback = LoadCallback; } +sTextureAtlasHdr TextureAtlasLoader::getTextureAtlasHeader() { + return mTexGrHdr; +} + +void TextureAtlasLoader::setTextureFilter(const Texture::TextureFilter & textureFilter) { + mTexGrHdr.TextureFilter = (char)textureFilter; + + size_t count = getTextureAtlas()->getTexturesCount() == 0; + + if ( count > 0 ) { + for ( size_t i = 0; i < count; i++ ) + getTextureAtlas()->getTexture( i )->setFilter( textureFilter ); + } +} + void TextureAtlasLoader::update() { mRL.update(); @@ -196,7 +211,7 @@ void TextureAtlasLoader::createTextureRegions() { Texture * tTex = TextureFactory::instance()->getByName( path ); if ( NULL != tTex ) - mTexuresLoaded.push_back( tTex ); + mTexturesLoaded.push_back( tTex ); // Create the Texture Atlas with the name of the real texture, not the Childs ( example load 1.png and not 1_ch1.png ) if ( 0 == z ) { @@ -250,8 +265,11 @@ void TextureAtlasLoader::createTextureRegions() { } } - if ( NULL != mTextureAtlas && mTexuresLoaded.size() ) { - mTextureAtlas->setTextures( mTexuresLoaded ); + if ( NULL != mTextureAtlas && mTexturesLoaded.size() ) { + for ( size_t i = 0; i < mTexturesLoaded.size(); i++ ) + mTexturesLoaded[i]->setFilter( (Texture::TextureFilter)mTexGrHdr.TextureFilter ); + + mTextureAtlas->setTextures( mTexturesLoaded ); } mLoaded = true; @@ -278,12 +296,12 @@ const bool& TextureAtlasLoader::isLoading() const { } Texture * TextureAtlasLoader::getTexture( const Uint32& texnum ) const { - eeASSERT( texnum < mTexuresLoaded.size() ); - return mTexuresLoaded[ texnum ]; + eeASSERT( texnum < mTexturesLoaded.size() ); + return mTexturesLoaded[ texnum ]; } Uint32 TextureAtlasLoader::getTexturesLoadedCount() { - return mTexuresLoaded.size(); + return mTexturesLoaded.size(); } bool TextureAtlasLoader::updateTextureAtlas() { @@ -414,7 +432,7 @@ bool TextureAtlasLoader::updateTextureAtlas( std::string TextureAtlasPath, std:: std::string tapath( FileSystem::fileRemoveExtension( TextureAtlasPath ) + "." + Image::saveTypeToExtension( mTexGrHdr.Format ) ); if ( 2 == NeedUpdate ) { - TexturePacker tp( mTexGrHdr.Width, mTexGrHdr.Height, pixelDensity, 0 != ( mTexGrHdr.Flags & HDR_TEXTURE_ATLAS_POW_OF_TWO ), mTexGrHdr.PixelBorder, mTexGrHdr.Flags & HDR_TEXTURE_ATLAS_ALLOW_FLIPPING ); + TexturePacker tp( mTexGrHdr.Width, mTexGrHdr.Height, pixelDensity, 0 != ( mTexGrHdr.Flags & HDR_TEXTURE_ATLAS_POW_OF_TWO ), mTexGrHdr.PixelBorder, (Texture::TextureFilter)mTexGrHdr.TextureFilter, mTexGrHdr.Flags & HDR_TEXTURE_ATLAS_ALLOW_FLIPPING ); tp.addTexturesPath( ImagesPath ); diff --git a/src/eepp/graphics/texturepacker.cpp b/src/eepp/graphics/texturepacker.cpp index bc2ad19d2..785e773eb 100644 --- a/src/eepp/graphics/texturepacker.cpp +++ b/src/eepp/graphics/texturepacker.cpp @@ -9,7 +9,7 @@ namespace EE { namespace Graphics { -TexturePacker::TexturePacker( const Uint32& MaxWidth, const Uint32& MaxHeight, const EE_PIXEL_DENSITY& PixelDensity, const bool& ForcePowOfTwo, const Uint32& PixelBorder, const bool& AllowFlipping ) : +TexturePacker::TexturePacker(const Uint32& MaxWidth, const Uint32& MaxHeight, const EE_PIXEL_DENSITY& PixelDensity, const bool& ForcePowOfTwo, const Uint32& PixelBorder, const Texture::TextureFilter& textureFilter, const bool& AllowFlipping ) : mTotalArea(0), mFreeList(NULL), mWidth(128), @@ -22,9 +22,13 @@ TexturePacker::TexturePacker( const Uint32& MaxWidth, const Uint32& MaxHeight, c mParent(NULL), mPlacedCount(0), mForcePowOfTwo(true), - mPixelBorder(0) + mPixelBorder(0), + mPixelDensity(PixelDensity), + mTextureFilter(textureFilter), + mSaveExtensions(false), + mFormat(Image::SaveType::SAVE_TYPE_PNG) { - setOptions( MaxWidth, MaxHeight, PixelDensity, ForcePowOfTwo, PixelBorder, AllowFlipping ); + setOptions( MaxWidth, MaxHeight, PixelDensity, ForcePowOfTwo, PixelBorder, textureFilter, AllowFlipping ); } TexturePacker::TexturePacker() : @@ -41,9 +45,12 @@ TexturePacker::TexturePacker() : mParent(NULL), mPlacedCount(0), mForcePowOfTwo(true), - mPixelBorder(0) -{ -} + mPixelBorder(0), + mPixelDensity(PD_MDPI), + mTextureFilter(Texture::TextureFilter::Linear), + mSaveExtensions(false), + mFormat(Image::SaveType::SAVE_TYPE_PNG) +{} TexturePacker::~TexturePacker() { @@ -108,7 +115,7 @@ Uint32 TexturePacker::getAtlasNumChannels() { return maxChannels; } -void TexturePacker::setOptions( const Uint32& MaxWidth, const Uint32& MaxHeight, const EE_PIXEL_DENSITY& PixelDensity, const bool& ForcePowOfTwo, const Uint32& PixelBorder, const bool& AllowFlipping ) { +void TexturePacker::setOptions( const Uint32& MaxWidth, const Uint32& MaxHeight, const EE_PIXEL_DENSITY& PixelDensity, const bool& ForcePowOfTwo, const Uint32& PixelBorder, const Texture::TextureFilter& textureFilter, const bool& AllowFlipping ) { if ( !mTextures.size() ) { // only can change the dimensions before adding any texture mMaxSize.x = MaxWidth; mMaxSize.y = MaxHeight; @@ -123,6 +130,7 @@ void TexturePacker::setOptions( const Uint32& MaxWidth, const Uint32& MaxHeight, mAllowFlipping = AllowFlipping; mPixelBorder = PixelBorder; mPixelDensity = PixelDensity; + mTextureFilter = textureFilter; } } @@ -370,7 +378,7 @@ void TexturePacker::insertTexture( TexturePackerTex * t, TexturePackerNode * bes } void TexturePacker::createChild() { - mChild = eeNew( TexturePacker, ( mWidth, mHeight, mPixelDensity, mForcePowOfTwo, mPixelBorder, mAllowFlipping ) ); + mChild = eeNew( TexturePacker, ( mWidth, mHeight, mPixelDensity, mForcePowOfTwo, mPixelBorder, mTextureFilter, mAllowFlipping ) ); std::list::iterator it; std::list< std::list::iterator > remove; @@ -652,8 +660,10 @@ void TexturePacker::saveTextureRegions() { TexGrHdr.Height = mHeight; TexGrHdr.PixelBorder = mPixelBorder; TexGrHdr.Flags = 0; + TexGrHdr.TextureFilter = mTextureFilter; - memset( TexGrHdr.Reserved, 0, 16 ); + int reservedSize = eeARRAY_SIZE(TexGrHdr.Reserved); + memset( TexGrHdr.Reserved, 0, reservedSize ); if ( mAllowFlipping ) TexGrHdr.Flags |= HDR_TEXTURE_ATLAS_ALLOW_FLIPPING; diff --git a/src/eepp/ui/tools/textureatlaseditor.cpp b/src/eepp/ui/tools/textureatlaseditor.cpp index de97134df..4e369840c 100644 --- a/src/eepp/ui/tools/textureatlaseditor.cpp +++ b/src/eepp/ui/tools/textureatlaseditor.cpp @@ -64,7 +64,12 @@ TextureAtlasEditor::TextureAtlasEditor( UIWindow * AttatchTo, const TGEditorClos " " " " - " " + " " + " " + " Linear" + " Nearest" + " " + " " " " " " " " @@ -116,6 +121,9 @@ TextureAtlasEditor::TextureAtlasEditor( UIWindow * AttatchTo, const TGEditorClos mUIContainer->bind( "destH", mSpinDestH ); mSpinDestH->addEventListener( UIEvent::OnValueChange, cb::Make1( this, &TextureAtlasEditor::onDestHChange ) ); + mUIContainer->bind( "textureFilter", mTextureFilterList ); + mTextureFilterList->addEventListener( UIEvent::OnItemSelected, cb::Make1( this, &TextureAtlasEditor::onTextureFilterChange ) ); + mUIContainer->find( "resetDest" )->addEventListener( UIEvent::MouseClick, cb::Make1( this, &TextureAtlasEditor::onResetDestSize ) ); mUIContainer->find( "resetOff" )->addEventListener( UIEvent::MouseClick, cb::Make1( this, &TextureAtlasEditor::onResetOffset ) ); @@ -256,6 +264,15 @@ void TextureAtlasEditor::fileMenuClick( const UIEvent * Event ) { } } +void TextureAtlasEditor::onTextureFilterChange( const UIEvent * Event ) { + if ( NULL == mTextureAtlasLoader || NULL == mTextureAtlasLoader->getTextureAtlas() || !mTextureAtlasLoader->isLoaded() ) + return; + + Texture::TextureFilter textureFilter = mTextureFilterList->getText() == "Nearest" ? Texture::TextureFilter::Nearest : Texture::TextureFilter::Linear; + + mTextureAtlasLoader->setTextureFilter( textureFilter ); +} + void TextureAtlasEditor::onTextureAtlasCreate( TexturePacker * TexPacker ) { eeSAFE_DELETE( mTexturePacker ); mTexturePacker = TexPacker; @@ -269,6 +286,8 @@ void TextureAtlasEditor::onTextureAtlasCreate( TexturePacker * TexPacker ) { void TextureAtlasEditor::updateControls() { if ( NULL != mTextureAtlasLoader && mTextureAtlasLoader->isLoaded() ) { + mTextureFilterList->getListBox()->setSelected( mTextureAtlasLoader->getTextureAtlasHeader().TextureFilter ); + fillTextureRegionList(); } } diff --git a/src/eepp/ui/tools/textureatlasnew.cpp b/src/eepp/ui/tools/textureatlasnew.cpp index 46046dcb0..2e02581c2 100644 --- a/src/eepp/ui/tools/textureatlasnew.cpp +++ b/src/eepp/ui/tools/textureatlasnew.cpp @@ -18,8 +18,8 @@ TextureAtlasNew::TextureAtlasNew( TGCreateCb NewTGCb ) : return; mUIWindow = UIWindow::New(); - mUIWindow->setSizeWithDecoration( 378, 283 ) - ->setMinWindowSize( 378, 283 ) + mUIWindow->setSizeWithDecoration( 378, 303 ) + ->setMinWindowSize( 378, 303 ) ->setWinFlags( UI_WIN_CLOSE_BUTTON | UI_WIN_USE_DEFAULT_BUTTONS_ACTIONS | UI_WIN_SHARE_ALPHA_WITH_CHILDS | UI_WIN_MODAL ); mUIWindow->addEventListener( UIEvent::OnWindowClose, cb::Make1( this, &TextureAtlasNew::windowClose ) ); @@ -27,39 +27,46 @@ TextureAtlasNew::TextureAtlasNew( TGCreateCb NewTGCb ) : std::string layout = "" - " " - " " - " " - " TGA" - " BMP" - " PNG" - " DDS" - " JPG" - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " MDPI" - " HDPI" - " XHDPI" - " XXHDPI" - " XXXHDPI" - " " - " " - " " + " " + " " + " " + " TGA" + " BMP" + " PNG" + " DDS" + " JPG" + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " MDPI" + " HDPI" + " XHDPI" + " XXHDPI" + " XXXHDPI" + " " + " " + " " + " " + " " + " Linear" + " Nearest" + " " + " " + " " " " " " " " @@ -79,6 +86,7 @@ TextureAtlasNew::TextureAtlasNew( TGCreateCb NewTGCb ) : mUIWindow->bind( "pixelDensity", mPixelDensity ); mUIWindow->bind( "pathInput", mTGPath ); mUIWindow->bind( "openPath", mSetPathButton ) ; + mUIWindow->bind( "textureFilter", mTextureFilter ); std::vector Sizes; @@ -144,13 +152,14 @@ void TextureAtlasNew::textureAtlasSave( const UIEvent * Event ) { bool Res1 = String::fromString( w, mComboWidth->getText() ); bool Res2 = String::fromString( h, mComboHeight->getText() ); b = static_cast( mPixelSpace->getValue() ); + Texture::TextureFilter textureFilter = mTextureFilter->getText() == "Nearest" ? Texture::TextureFilter::Nearest : Texture::TextureFilter::Linear; if ( Res1 && Res2 ) { - Graphics::TexturePacker * TexturePacker = eeNew( Graphics::TexturePacker, ( w, h, PixelDensity::fromString( mPixelDensity->getText() ), false, b ) ); + TexturePacker * texturePacker = eeNew( TexturePacker, ( w, h, PixelDensity::fromString( mPixelDensity->getText() ), false, b, textureFilter ) ); - TexturePacker->addTexturesPath( mTGPath->getText() ); + texturePacker->addTexturesPath( mTGPath->getText() ); - TexturePacker->packTextures(); + texturePacker->packTextures(); std::string ext = FileSystem::fileExtension( FPath, true ); @@ -164,10 +173,10 @@ void TextureAtlasNew::textureAtlasSave( const UIEvent * Event ) { FPath += "." + ext; } - TexturePacker->save( FPath, static_cast ( mSaveFileType->getListBox()->getItemSelectedIndex() ) ); + texturePacker->save( FPath, static_cast ( mSaveFileType->getListBox()->getItemSelectedIndex() ) ); if ( mNewTGCb.IsSet() ) - mNewTGCb( TexturePacker ); + mNewTGCb( texturePacker ); mUIWindow->closeWindow(); } diff --git a/src/eepp/ui/tools/textureatlasnew.hpp b/src/eepp/ui/tools/textureatlasnew.hpp index 90f37f7d0..10837b44e 100644 --- a/src/eepp/ui/tools/textureatlasnew.hpp +++ b/src/eepp/ui/tools/textureatlasnew.hpp @@ -28,6 +28,7 @@ class EE_API TextureAtlasNew { UIPushButton * mSetPathButton; UIDropDownList * mSaveFileType; UIDropDownList * mPixelDensity; + UIDropDownList * mTextureFilter; void windowClose( const UIEvent * Event );