2010-08-02 10:58:00 +08:00
/****************************************************************************
2012-06-08 14:11:48 +08:00
Copyright ( c ) 2010 - 2012 cocos2d - x . org
2011-03-19 14:45:51 +08:00
Copyright ( c ) 2008 Apple Inc . All Rights Reserved .
2010-08-02 10:58:00 +08:00
http : //www.cocos2d-x.org
Permission is hereby granted , free of charge , to any person obtaining a copy
of this software and associated documentation files ( the " Software " ) , to deal
in the Software without restriction , including without limitation the rights
to use , copy , modify , merge , publish , distribute , sublicense , and / or sell
copies of the Software , and to permit persons to whom the Software is
furnished to do so , subject to the following conditions :
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software .
THE SOFTWARE IS PROVIDED " AS IS " , WITHOUT WARRANTY OF ANY KIND , EXPRESS OR
IMPLIED , INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY ,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT . IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM , DAMAGES OR OTHER
LIABILITY , WHETHER IN AN ACTION OF CONTRACT , TORT OR OTHERWISE , ARISING FROM ,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2010-07-15 18:15:00 +08:00
/*
* Support for RGBA_4_4_4_4 and RGBA_5_5_5_1 was copied from :
* https : //devforums.apple.com/message/37855#37855 by a1studmuffin
*/
2010-08-02 10:58:00 +08:00
# include "CCTexture2D.h"
2010-07-15 18:15:00 +08:00
# include "ccConfig.h"
# include "ccMacros.h"
2010-07-20 14:29:18 +08:00
# include "CCConfiguration.h"
2010-07-27 13:47:29 +08:00
# include "platform/platform.h"
2012-06-19 16:20:46 +08:00
# include "platform/CCImage.h"
2010-12-30 17:30:11 +08:00
# include "CCGL.h"
# include "support/ccUtils.h"
2011-01-10 17:54:44 +08:00
# include "platform/CCPlatformMacros.h"
2011-11-28 17:28:43 +08:00
# include "CCDirector.h"
2012-06-19 16:20:46 +08:00
# include "shaders/CCGLProgram.h"
# include "shaders/ccGLStateCache.h"
# include "shaders/CCShaderCache.h"
2010-08-31 16:53:25 +08:00
2012-06-06 10:06:51 +08:00
# if CC_ENABLE_CACHE_TEXTURE_DATA
2011-04-06 16:29:58 +08:00
# include "CCTextureCache.h"
2011-01-10 17:54:44 +08:00
# endif
2012-04-18 18:43:45 +08:00
NS_CC_BEGIN
2010-07-15 18:15:00 +08:00
//CLASS IMPLEMENTATIONS:
// If the image has alpha, you can create RGBA8 (32-bit) or RGBA4 (16-bit) or RGB5A1 (16-bit)
// Default is: RGBA8888 (32-bit textures)
2013-06-20 14:13:12 +08:00
static Texture2DPixelFormat g_defaultAlphaPixelFormat = kTexture2DPixelFormat_Default ;
2010-07-15 18:15:00 +08:00
2011-12-20 14:59:15 +08:00
// By default PVR images are treated as if they don't have the alpha channel premultiplied
static bool PVRHaveAlphaPremultiplied_ = false ;
2013-07-17 17:12:04 +08:00
//////////////////////////////////////////////////////////////////////////
//conventer function
// IIIIIIII -> RRRRRRRRGGGGGGGGGBBBBBBBB
2013-07-25 21:35:00 +08:00
void Texture2D : : convertI8ToRGB888 ( const unsigned char * in , int len , unsigned char * out )
2013-07-17 17:12:04 +08:00
{
2013-07-19 15:37:54 +08:00
for ( int i = 0 ; i < len ; + + i )
{
* out + + = in [ i ] ; //R
* out + + = in [ i ] ; //G
* out + + = in [ i ] ; //B
}
2013-07-17 17:12:04 +08:00
}
// IIIIIIIIAAAAAAAA -> RRRRRRRRGGGGGGGGBBBBBBBB
2013-07-25 21:35:00 +08:00
void Texture2D : : convertAI88ToRGB888 ( const unsigned char * in , int len , unsigned char * out )
2013-07-17 17:12:04 +08:00
{
2013-07-19 15:37:54 +08:00
for ( int i = 0 , l = len - 1 ; i < l ; i + = 2 )
{
* out + + = in [ i ] ; //R
* out + + = in [ i ] ; //G
* out + + = in [ i ] ; //B
}
2013-07-17 17:12:04 +08:00
}
// IIIIIIII -> RRRRRRRRGGGGGGGGGBBBBBBBBAAAAAAAA
2013-07-25 21:35:00 +08:00
void Texture2D : : convertI8ToRGBA8888 ( const unsigned char * in , int len , unsigned char * out )
2013-07-17 17:12:04 +08:00
{
2013-07-19 15:37:54 +08:00
for ( int i = 0 ; i < len ; + + i )
{
* out + + = in [ i ] ; //R
* out + + = in [ i ] ; //G
* out + + = in [ i ] ; //B
* out + + = 0xFF ; //A
}
2013-07-17 17:12:04 +08:00
}
// IIIIIIIIAAAAAAAA -> RRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA
2013-07-25 21:35:00 +08:00
void Texture2D : : convertAI88ToRGBA8888 ( const unsigned char * in , int len , unsigned char * out )
2013-07-17 17:12:04 +08:00
{
2013-07-19 15:37:54 +08:00
for ( int i = 0 , l = len - 1 ; i < l ; i + = 2 )
{
* out + + = in [ i ] ; //R
* out + + = in [ i ] ; //G
* out + + = in [ i ] ; //B
* out + + = in [ i + 1 ] ; //A
}
2013-07-17 17:12:04 +08:00
}
// IIIIIIII -> RRRRRGGGGGGBBBBB
2013-07-25 21:35:00 +08:00
void Texture2D : : convertI8ToRGB565 ( const unsigned char * in , int len , unsigned char * out )
2013-07-17 17:12:04 +08:00
{
2013-07-19 15:37:54 +08:00
unsigned short * out16 = ( unsigned short * ) out ;
for ( int i = 0 ; i < len ; + + i )
{
2013-07-25 21:35:00 +08:00
* out16 + + = ( in [ i ] & 0x00F8 ) < < 8 //R
| ( in [ i ] & 0x00FC ) < < 3 //G
| ( in [ i ] & 0x00F8 ) > > 3 ; //B
2013-07-19 15:37:54 +08:00
}
2013-07-17 17:12:04 +08:00
}
// IIIIIIIIAAAAAAAA -> RRRRRGGGGGGBBBBB
2013-07-25 21:35:00 +08:00
void Texture2D : : convertAI88ToRGB565 ( const unsigned char * in , int len , unsigned char * out )
2013-07-17 17:12:04 +08:00
{
2013-07-19 15:37:54 +08:00
unsigned short * out16 = ( unsigned short * ) out ;
for ( int i = 0 , l = len - 1 ; i < l ; i + = 2 )
{
2013-07-25 21:35:00 +08:00
* out16 + + = ( in [ i ] & 0x00F8 ) < < 8 //R
| ( in [ i ] & 0x00FC ) < < 3 //G
| ( in [ i ] & 0x00F8 ) > > 3 ; //B
2013-07-19 15:37:54 +08:00
}
2013-07-17 17:12:04 +08:00
}
// IIIIIIII -> RRRRGGGGBBBBAAAA
2013-07-25 21:35:00 +08:00
void Texture2D : : convertI8ToRGBA4444 ( const unsigned char * in , int len , unsigned char * out )
2013-07-17 17:12:04 +08:00
{
2013-07-25 21:35:00 +08:00
unsigned short * out16 = ( unsigned short * ) out ;
2013-07-19 15:37:54 +08:00
for ( int i = 0 ; i < len ; + + i )
{
2013-07-25 21:35:00 +08:00
* out16 + + = ( in [ i ] & 0x00F0 ) < < 8 | ( in [ i ] & 0x00F0 ) < < 4 | ( in [ i ] & 0x00F0 ) | 0x000F ; //RGBA
2013-07-19 15:37:54 +08:00
}
2013-07-17 17:12:04 +08:00
}
// IIIIIIIIAAAAAAAA -> RRRRGGGGBBBBAAAA
2013-07-25 21:35:00 +08:00
void Texture2D : : convertAI88ToRGBA4444 ( const unsigned char * in , int len , unsigned char * out )
2013-07-17 17:12:04 +08:00
{
2013-07-25 21:35:00 +08:00
unsigned short * out16 = ( unsigned short * ) out ;
2013-07-19 15:37:54 +08:00
for ( int i = 0 , l = len - 1 ; i < l ; i + = 2 )
{
2013-07-25 21:35:00 +08:00
* out16 + + = ( in [ i ] & 0x00F0 ) < < 8 | ( in [ i ] & 0x00F0 ) < < 4 | ( in [ i ] & 0x00F0 ) | ( in [ i + 1 ] & 0x00F0 ) > > 4 ; //RGBA
2013-07-19 15:37:54 +08:00
}
2013-07-17 17:12:04 +08:00
}
// IIIIIIII -> RRRRRGGGGGBBBBBA
2013-07-25 21:35:00 +08:00
void Texture2D : : convertI8ToRGB5A1 ( const unsigned char * in , int len , unsigned char * out )
2013-07-17 17:12:04 +08:00
{
2013-07-19 15:37:54 +08:00
unsigned short * out16 = ( unsigned short * ) out ;
for ( int i = 0 ; i < len ; + + i )
{
2013-07-25 21:35:00 +08:00
* out16 + + = ( in [ i ] & 0x00F8 ) < < 8 //R
| ( in [ i ] & 0x00F8 ) < < 3 //G
| ( in [ i ] & 0x00F8 ) > > 2 //B
| 0x0001 ; //A
2013-07-19 15:37:54 +08:00
}
2013-07-17 17:12:04 +08:00
}
// IIIIIIIIAAAAAAAA -> RRRRRGGGGGBBBBBA
2013-07-25 21:35:00 +08:00
void Texture2D : : convertAI88ToRGB5A1 ( const unsigned char * in , int len , unsigned char * out )
2013-07-17 17:12:04 +08:00
{
2013-07-19 15:37:54 +08:00
unsigned short * out16 = ( unsigned short * ) out ;
for ( int i = 0 , l = len - 1 ; i < l ; i + = 2 )
{
2013-07-25 21:35:00 +08:00
* out16 + + = ( in [ i ] & 0x00F8 ) < < 8 //R
| ( in [ i ] & 0x00F8 ) < < 3 //G
| ( in [ i ] & 0x00F8 ) > > 2 //B
| ( in [ i + 1 ] & 0x0080 ) > > 7 ; //A
2013-07-19 15:37:54 +08:00
}
2013-07-17 17:12:04 +08:00
}
// IIIIIIII -> IIIIIIIIAAAAAAAA
2013-07-25 21:35:00 +08:00
void Texture2D : : convertI8ToAI88 ( const unsigned char * in , int len , unsigned char * out )
2013-07-17 17:12:04 +08:00
{
2013-07-19 15:37:54 +08:00
unsigned short * out16 = ( unsigned short * ) out ;
for ( int i = 0 ; i < len ; + + i )
{
* out16 + + = ( unsigned short ) in [ i ] < < 8 //R
| 0xFF ; //A
}
2013-07-17 17:12:04 +08:00
}
// IIIIIIIIAAAAAAAA -> AAAAAAAA
2013-07-25 21:35:00 +08:00
void Texture2D : : convertAI88ToA8 ( const unsigned char * in , int len , unsigned char * out )
2013-07-17 17:12:04 +08:00
{
2013-07-19 15:37:54 +08:00
for ( int i = 1 ; i < len ; i + = 2 )
{
* out + + = in [ i ] ; //A
}
2013-07-17 17:12:04 +08:00
}
// IIIIIIIIAAAAAAAA -> IIIIIIII
2013-07-25 21:35:00 +08:00
void Texture2D : : convertAI88ToI8 ( const unsigned char * in , int len , unsigned char * out )
2013-07-17 17:12:04 +08:00
{
2013-07-19 15:37:54 +08:00
for ( int i = 0 , l = len - 1 ; i < l ; i + = 2 )
{
* out + + = in [ i ] ; //R
}
2013-07-17 17:12:04 +08:00
}
// RRRRRRRRGGGGGGGGBBBBBBBB -> RRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA
2013-07-25 21:35:00 +08:00
void Texture2D : : convertRGB888ToRGBA8888 ( const unsigned char * in , int len , unsigned char * out )
2013-07-17 17:12:04 +08:00
{
2013-07-19 15:37:54 +08:00
for ( int i = 0 , l = len - 2 ; i < l ; i + = 3 )
{
* out + + = in [ i ] ;
* out + + = in [ i + 1 ] ;
* out + + = in [ i + 2 ] ;
* out + + = 0xFF ;
}
2013-07-17 17:12:04 +08:00
}
// RRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA -> RRRRRRRRGGGGGGGGBBBBBBBB
2013-07-25 21:35:00 +08:00
void Texture2D : : convertRGBA8888ToRGB888 ( const unsigned char * in , int len , unsigned char * out )
2013-07-17 17:12:04 +08:00
{
2013-07-19 15:37:54 +08:00
for ( int i = 0 , l = len - 3 ; i < l ; i + = 4 )
{
* out + + = in [ i ] ;
* out + + = in [ i + 1 ] ;
* out + + = in [ i + 2 ] ;
}
2013-07-17 17:12:04 +08:00
}
// RRRRRRRRGGGGGGGGBBBBBBBB -> RRRRRGGGGGGBBBBB
2013-07-25 21:35:00 +08:00
void Texture2D : : convertRGB888ToRGB565 ( const unsigned char * in , int len , unsigned char * out )
2013-07-17 17:12:04 +08:00
{
2013-07-19 15:37:54 +08:00
unsigned short * out16 = ( unsigned short * ) out ;
for ( int i = 0 , l = len - 2 ; i < l ; i + = 3 )
{
2013-07-25 21:35:00 +08:00
* out16 + + = ( in [ i ] & 0x00F8 ) < < 8 //R
| ( in [ i + 1 ] & 0x00FC ) < < 3 //G
| ( in [ i + 2 ] & 0x00F8 ) > > 3 ; //B
2013-07-19 15:37:54 +08:00
}
2013-07-17 17:12:04 +08:00
}
// RRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA -> RRRRRGGGGGGBBBBB
2013-07-25 21:35:00 +08:00
void Texture2D : : convertRGBA8888ToRGB565 ( const unsigned char * in , int len , unsigned char * out )
2013-07-17 17:12:04 +08:00
{
2013-07-19 15:37:54 +08:00
unsigned short * out16 = ( unsigned short * ) out ;
for ( int i = 0 , l = len - 3 ; i < l ; i + = 4 )
{
2013-07-25 21:35:00 +08:00
* out16 + + = ( in [ i ] & 0x00F8 ) < < 8 //R
| ( in [ i + 1 ] & 0x00FC ) < < 3 //G
| ( in [ i + 2 ] & 0x00F8 ) > > 3 ; //B
2013-07-19 15:37:54 +08:00
}
2013-07-17 17:12:04 +08:00
}
// RRRRRRRRGGGGGGGGBBBBBBBB -> IIIIIIII
2013-07-25 21:35:00 +08:00
void Texture2D : : convertRGB888ToI8 ( const unsigned char * in , int len , unsigned char * out )
2013-07-17 17:12:04 +08:00
{
2013-07-19 15:37:54 +08:00
for ( int i = 0 , l = len - 2 ; i < l ; i + = 3 )
{
* out + + = ( in [ i ] * 299 + in [ i + 1 ] * 587 + in [ i + 2 ] * 114 + 500 ) / 1000 ; //I = (R*299 + G*587 + B*114 + 500) / 1000
}
2013-07-17 17:12:04 +08:00
}
// RRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA -> IIIIIIII
2013-07-25 21:35:00 +08:00
void Texture2D : : convertRGBA8888ToI8 ( const unsigned char * in , int len , unsigned char * out )
2013-07-17 17:12:04 +08:00
{
2013-07-19 15:37:54 +08:00
for ( int i = 0 , l = len - 3 ; i < l ; i + = 4 )
{
* out + + = ( in [ i ] * 299 + in [ i + 1 ] * 587 + in [ i + 2 ] * 114 + 500 ) / 1000 ; //I = (R*299 + G*587 + B*114 + 500) / 1000
}
2013-07-17 17:12:04 +08:00
}
// RRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA -> AAAAAAAA
2013-07-25 21:35:00 +08:00
void Texture2D : : convertRGBA8888ToA8 ( const unsigned char * in , int len , unsigned char * out )
2013-07-17 17:12:04 +08:00
{
2013-07-19 15:37:54 +08:00
for ( int i = 0 , l = len - 3 ; i < l ; i + = 4 )
{
* out + + = in [ i + 3 ] ;
}
2013-07-17 17:12:04 +08:00
}
// RRRRRRRRGGGGGGGGBBBBBBBB -> IIIIIIIIAAAAAAAA
2013-07-25 21:35:00 +08:00
void Texture2D : : convertRGB888ToAI88 ( const unsigned char * in , int len , unsigned char * out )
2013-07-17 17:12:04 +08:00
{
2013-07-19 15:37:54 +08:00
for ( int i = 0 , l = len - 2 ; i < l ; i + = 3 )
{
* out + + = ( in [ i ] * 299 + in [ i + 1 ] * 587 + in [ i + 2 ] * 114 + 500 ) / 1000 ; //I = (R*299 + G*587 + B*114 + 500) / 1000
* out + + = 0xFF ;
}
2013-07-17 17:12:04 +08:00
}
// RRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA -> IIIIIIIIAAAAAAAA
2013-07-25 21:35:00 +08:00
void Texture2D : : convertRGBA8888ToAI88 ( const unsigned char * in , int len , unsigned char * out )
2013-07-17 17:12:04 +08:00
{
2013-07-19 15:37:54 +08:00
for ( int i = 0 , l = len - 3 ; i < l ; i + = 4 )
{
* out + + = ( in [ i ] * 299 + in [ i + 1 ] * 587 + in [ i + 2 ] * 114 + 500 ) / 1000 ; //I = (R*299 + G*587 + B*114 + 500) / 1000
* out + + = in [ i + 3 ] ;
}
2013-07-17 17:12:04 +08:00
}
// RRRRRRRRGGGGGGGGBBBBBBBB -> RRRRGGGGBBBBAAAA
2013-07-25 21:35:00 +08:00
void Texture2D : : convertRGB888ToRGBA4444 ( const unsigned char * in , int len , unsigned char * out )
2013-07-17 17:12:04 +08:00
{
2013-07-25 21:35:00 +08:00
unsigned short * out16 = ( unsigned short * ) out ;
2013-07-19 15:37:54 +08:00
for ( int i = 0 , l = len - 2 ; i < l ; i + = 3 )
{
2013-07-25 21:35:00 +08:00
* out16 + + = ( ( in [ i ] & 0x00F0 ) < < 8 | ( in [ i + 1 ] & 0x00F0 ) < < 4 | ( in [ i + 2 ] & 0xF0 ) | 0x0F ) ; //RGBA
2013-07-19 15:37:54 +08:00
}
2013-07-17 17:12:04 +08:00
}
// RRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA -> RRRRGGGGBBBBAAAA
2013-07-25 21:35:00 +08:00
void Texture2D : : convertRGBA8888ToRGBA4444 ( const unsigned char * in , int len , unsigned char * out )
2013-07-17 17:12:04 +08:00
{
2013-07-25 21:35:00 +08:00
unsigned short * out16 = ( unsigned short * ) out ;
2013-07-19 15:37:54 +08:00
for ( int i = 0 , l = len - 3 ; i < l ; i + = 4 )
{
2013-07-25 21:35:00 +08:00
* out16 + + = ( in [ i ] & 0x00F0 ) < < 8 | ( in [ i + 1 ] & 0x00F0 ) < < 4 | ( in [ i + 2 ] & 0xF0 ) | ( in [ i + 3 ] & 0xF0 ) > > 4 ; //RGBA
2013-07-19 15:37:54 +08:00
}
2013-07-17 17:12:04 +08:00
}
// RRRRRRRRGGGGGGGGBBBBBBBB -> RRRRRGGGGGBBBBBA
2013-07-25 21:35:00 +08:00
void Texture2D : : convertRGB888ToRGB5A1 ( const unsigned char * in , int len , unsigned char * out )
2013-07-17 17:12:04 +08:00
{
2013-07-19 15:37:54 +08:00
unsigned short * out16 = ( unsigned short * ) out ;
for ( int i = 0 , l = len - 2 ; i < l ; i + = 3 )
{
2013-07-25 21:35:00 +08:00
* out16 + + = ( in [ i ] & 0x00F8 ) < < 8 //R
| ( in [ i + 1 ] & 0x00F8 ) < < 3 //G
| ( in [ i + 2 ] & 0x00F8 ) > > 2 //B
2013-07-19 15:37:54 +08:00
| 0x01 ; //A
}
2013-07-17 17:12:04 +08:00
}
// RRRRRRRRGGGGGGGGBBBBBBBB -> RRRRRGGGGGBBBBBA
2013-07-25 21:35:00 +08:00
void Texture2D : : convertRGBA8888ToRGB5A1 ( const unsigned char * in , int len , unsigned char * out )
2013-07-17 17:12:04 +08:00
{
2013-07-19 15:37:54 +08:00
unsigned short * out16 = ( unsigned short * ) out ;
for ( int i = 0 , l = len - 2 ; i < l ; i + = 4 )
{
2013-07-25 21:35:00 +08:00
* out16 + + = ( in [ i ] & 0x00F8 ) < < 8 //R
| ( in [ i + 1 ] & 0x00F8 ) < < 3 //G
| ( in [ i + 2 ] & 0x00F8 ) > > 2 //B
| ( in [ i + 3 ] & 0x0080 ) > > 7 ; //A
2013-07-19 15:37:54 +08:00
}
2013-07-17 17:12:04 +08:00
}
// conventer function end
//////////////////////////////////////////////////////////////////////////
2013-06-20 14:13:12 +08:00
Texture2D : : Texture2D ( )
2013-06-15 14:03:30 +08:00
: _PVRHaveAlphaPremultiplied ( true )
, _pixelsWide ( 0 )
, _pixelsHigh ( 0 )
2013-07-25 21:35:00 +08:00
, _pixelFormat ( kTexture2DPixelFormat_None )
2013-06-15 14:03:30 +08:00
, _name ( 0 )
, _maxS ( 0.0 )
, _maxT ( 0.0 )
, _hasPremultipliedAlpha ( false )
, _hasMipmaps ( false )
, _shaderProgram ( NULL )
2010-07-15 18:15:00 +08:00
{
}
2013-06-20 14:13:12 +08:00
Texture2D : : ~ Texture2D ( )
2010-07-15 18:15:00 +08:00
{
2012-06-06 10:06:51 +08:00
# if CC_ENABLE_CACHE_TEXTURE_DATA
2011-01-10 17:54:44 +08:00
VolatileTexture : : removeTexture ( this ) ;
# endif
2013-06-20 14:13:12 +08:00
CCLOGINFO ( " cocos2d: deallocing Texture2D %u. " , _name ) ;
2013-06-15 14:03:30 +08:00
CC_SAFE_RELEASE ( _shaderProgram ) ;
2012-03-14 14:55:17 +08:00
2013-06-15 14:03:30 +08:00
if ( _name )
2012-04-19 14:35:52 +08:00
{
2013-06-15 14:03:30 +08:00
ccGLDeleteTexture ( _name ) ;
2012-04-19 14:35:52 +08:00
}
2010-07-15 18:15:00 +08:00
}
2013-06-20 14:13:12 +08:00
Texture2DPixelFormat Texture2D : : getPixelFormat ( ) const
2010-07-15 18:15:00 +08:00
{
2013-06-15 14:03:30 +08:00
return _pixelFormat ;
2010-07-15 18:15:00 +08:00
}
2013-06-20 14:13:12 +08:00
unsigned int Texture2D : : getPixelsWide ( ) const
2010-07-15 18:15:00 +08:00
{
2013-06-15 14:03:30 +08:00
return _pixelsWide ;
2010-07-15 18:15:00 +08:00
}
2013-06-20 14:13:12 +08:00
unsigned int Texture2D : : getPixelsHigh ( ) const
2010-07-15 18:15:00 +08:00
{
2013-06-15 14:03:30 +08:00
return _pixelsHigh ;
2010-07-15 18:15:00 +08:00
}
2013-06-20 14:13:12 +08:00
GLuint Texture2D : : getName ( ) const
2010-07-15 18:15:00 +08:00
{
2013-06-15 14:03:30 +08:00
return _name ;
2010-07-15 18:15:00 +08:00
}
2013-06-20 14:13:12 +08:00
Size Texture2D : : getContentSize ( ) const
2010-07-15 18:15:00 +08:00
{
2012-04-15 15:46:01 +08:00
2013-06-20 14:13:12 +08:00
Size ret ;
2013-06-15 14:03:30 +08:00
ret . width = _contentSize . width / CC_CONTENT_SCALE_FACTOR ( ) ;
ret . height = _contentSize . height / CC_CONTENT_SCALE_FACTOR ( ) ;
2012-04-08 22:37:58 +08:00
return ret ;
2010-07-15 18:15:00 +08:00
}
2013-06-20 14:13:12 +08:00
const Size & Texture2D : : getContentSizeInPixels ( )
2010-12-30 17:30:11 +08:00
{
2013-06-15 14:03:30 +08:00
return _contentSize ;
2010-12-30 17:30:11 +08:00
}
2013-06-20 14:13:12 +08:00
GLfloat Texture2D : : getMaxS ( )
2010-07-15 18:15:00 +08:00
{
2013-06-15 14:03:30 +08:00
return _maxS ;
2010-07-15 18:15:00 +08:00
}
2013-06-20 14:13:12 +08:00
void Texture2D : : setMaxS ( GLfloat maxS )
2010-07-15 18:15:00 +08:00
{
2013-06-15 14:03:30 +08:00
_maxS = maxS ;
2010-07-15 18:15:00 +08:00
}
2013-06-20 14:13:12 +08:00
GLfloat Texture2D : : getMaxT ( )
2010-07-15 18:15:00 +08:00
{
2013-06-15 14:03:30 +08:00
return _maxT ;
2010-07-15 18:15:00 +08:00
}
2013-06-20 14:13:12 +08:00
void Texture2D : : setMaxT ( GLfloat maxT )
2010-07-15 18:15:00 +08:00
{
2013-06-15 14:03:30 +08:00
_maxT = maxT ;
2010-07-15 18:15:00 +08:00
}
2013-06-20 14:13:12 +08:00
GLProgram * Texture2D : : getShaderProgram ( void )
2012-03-14 14:55:17 +08:00
{
2013-06-15 14:03:30 +08:00
return _shaderProgram ;
2012-03-14 14:55:17 +08:00
}
2013-06-20 14:13:12 +08:00
void Texture2D : : setShaderProgram ( GLProgram * pShaderProgram )
2012-03-14 14:55:17 +08:00
{
2012-04-19 14:35:52 +08:00
CC_SAFE_RETAIN ( pShaderProgram ) ;
2013-06-15 14:03:30 +08:00
CC_SAFE_RELEASE ( _shaderProgram ) ;
_shaderProgram = pShaderProgram ;
2012-03-14 14:55:17 +08:00
}
2013-06-20 14:13:12 +08:00
void Texture2D : : releaseData ( void * data )
2010-12-30 17:30:11 +08:00
{
free ( data ) ;
}
2013-06-20 14:13:12 +08:00
void * Texture2D : : keepData ( void * data , unsigned int length )
2010-12-30 17:30:11 +08:00
{
2011-06-10 17:51:37 +08:00
CC_UNUSED_PARAM ( length ) ;
2012-09-17 15:02:24 +08:00
//The texture data mustn't be saved because it isn't a mutable texture.
2012-04-19 14:35:52 +08:00
return data ;
2010-12-30 17:30:11 +08:00
}
2013-07-04 08:44:41 +08:00
bool Texture2D : : hasPremultipliedAlpha ( ) const
2010-07-15 18:15:00 +08:00
{
2013-06-15 14:03:30 +08:00
return _hasPremultipliedAlpha ;
2010-07-15 18:15:00 +08:00
}
2013-07-25 21:35:00 +08:00
bool Texture2D : : initWithData ( const void * data , int dataLen , Texture2DPixelFormat pixelFormat , unsigned int pixelsWide , unsigned int pixelsHigh , const Size & contentSize )
{
//if data has no mipmaps, we will consider it has only one mipmap
MipmapInfo mipmap ;
mipmap . address = ( unsigned char * ) data ;
mipmap . len = dataLen ;
return initWithMipmaps ( & mipmap , 1 , pixelFormat , pixelsWide , pixelsHigh ) ;
//update information
_contentSize = contentSize ;
_maxS = contentSize . width / ( float ) ( pixelsWide ) ;
_maxT = contentSize . height / ( float ) ( pixelsHigh ) ;
}
bool Texture2D : : initWithMipmaps ( MipmapInfo * mipmaps , int mipmapsNum , Texture2DPixelFormat pixelFormat , unsigned int pixelsWide , unsigned int pixelsHigh )
2010-07-15 18:15:00 +08:00
{
2013-07-19 15:37:54 +08:00
//the pixelFormat must be a certain value
2013-07-25 21:35:00 +08:00
CCAssert ( pixelFormat ! = kTexture2DPixelFormat_None & & pixelFormat ! = kTexture2DPixelFormat_Automatic , " the \" pixelFormat \" param must be a certain value! " ) ;
2013-07-19 15:37:54 +08:00
2013-07-25 21:35:00 +08:00
if ( mipmapsNum < = 0 )
2013-05-19 23:53:59 +08:00
{
2013-07-25 21:35:00 +08:00
CCLOG ( " cocos2d: WARNING: mipmap number is less than 1 " ) ;
return false ;
2013-05-19 23:53:59 +08:00
}
2013-07-25 21:35:00 +08:00
if ( g_texturePixelFormatInfoTables . find ( pixelFormat ) = = g_texturePixelFormatInfoTables . end ( ) )
2013-05-19 23:53:59 +08:00
{
2013-07-25 21:35:00 +08:00
CCLOG ( " cocos2d: WARNING: unsupported pixelformat: %lx " , ( unsigned long ) pixelFormat ) ;
return false ;
2013-05-19 23:53:59 +08:00
}
2013-07-25 21:35:00 +08:00
const TexturePixelFormatInfo & info = g_texturePixelFormatInfoTables . at ( pixelFormat ) ;
2013-05-12 23:08:33 +08:00
2013-07-26 17:34:44 +08:00
if ( info . compressed & & ! Configuration : : getInstance ( ) - > supportsPVRTC ( ) & & ! Configuration : : getInstance ( ) - > supportsETC ( ) )
2013-05-19 20:12:39 +08:00
{
2013-07-26 17:34:44 +08:00
CCLOG ( " cocos2d: WARNING: PVRTC/ETC images are not supported " ) ;
2013-07-25 21:35:00 +08:00
return false ;
2013-05-12 23:08:33 +08:00
}
2013-07-25 21:35:00 +08:00
2013-07-26 17:34:44 +08:00
//Set the row align only when mipmapsNum == 1 and the data is uncompressed
if ( mipmapsNum = = 1 & & ! info . compressed )
2013-05-19 20:12:39 +08:00
{
2013-07-25 21:35:00 +08:00
unsigned int bytesPerRow = pixelsWide * info . bpp / 8 ;
if ( bytesPerRow % 8 = = 0 )
{
glPixelStorei ( GL_UNPACK_ALIGNMENT , 8 ) ;
}
else if ( bytesPerRow % 4 = = 0 )
{
glPixelStorei ( GL_UNPACK_ALIGNMENT , 4 ) ;
}
else if ( bytesPerRow % 2 = = 0 )
{
glPixelStorei ( GL_UNPACK_ALIGNMENT , 2 ) ;
}
else
{
glPixelStorei ( GL_UNPACK_ALIGNMENT , 1 ) ;
}
} else
2012-06-08 14:11:48 +08:00
{
2013-05-12 23:08:33 +08:00
glPixelStorei ( GL_UNPACK_ALIGNMENT , 1 ) ;
2012-06-08 14:11:48 +08:00
}
2013-07-25 21:35:00 +08:00
2012-06-08 14:11:48 +08:00
2013-05-19 20:17:48 +08:00
2013-06-15 14:03:30 +08:00
glGenTextures ( 1 , & _name ) ;
ccGLBindTexture2D ( _name ) ;
2012-04-19 14:35:52 +08:00
2013-07-25 21:35:00 +08:00
if ( mipmapsNum = = 1 )
{
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_LINEAR ) ;
} else
{
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_LINEAR_MIPMAP_NEAREST ) ;
}
2012-06-13 16:20:58 +08:00
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , GL_LINEAR ) ;
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_WRAP_S , GL_CLAMP_TO_EDGE ) ;
2012-06-08 14:11:48 +08:00
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_WRAP_T , GL_CLAMP_TO_EDGE ) ;
2012-04-19 14:35:52 +08:00
2013-07-25 21:35:00 +08:00
CHECK_GL_ERROR_DEBUG ( ) ; // clean possible GL error
2012-04-19 14:35:52 +08:00
// Specify OpenGL texture image
2013-07-25 21:35:00 +08:00
int width = pixelsWide ;
int height = pixelsHigh ;
for ( int i = 0 ; i < mipmapsNum ; + + i )
2012-04-19 14:35:52 +08:00
{
2013-07-25 21:35:00 +08:00
unsigned char * data = mipmaps [ i ] . address ;
GLsizei datalen = mipmaps [ i ] . len ;
if ( info . compressed )
{
glCompressedTexImage2D ( GL_TEXTURE_2D , i , info . internalFormat , ( GLsizei ) width , ( GLsizei ) height , 0 , datalen , data ) ;
}
else
{
glTexImage2D ( GL_TEXTURE_2D , i , info . internalFormat , ( GLsizei ) width , ( GLsizei ) height , 0 , info . format , info . type , data ) ;
}
if ( i > 0 & & ( width ! = height | | ccNextPOT ( width ) ! = width ) )
{
CCLog ( " cocos2d: Texture2D. WARNING. Mipmap level %u is not squared. Texture won't render correctly. width=%u != height=%u " , i , width , height ) ;
}
GLenum err = glGetError ( ) ;
if ( err ! = GL_NO_ERROR )
{
CCLog ( " cocos2d: Texture2D: Error uploading compressed texture level: %u . glError: 0x%04X " , i , err ) ;
return false ;
}
width = MAX ( width > > 1 , 1 ) ;
height = MAX ( height > > 1 , 1 ) ;
2012-04-19 14:35:52 +08:00
}
2013-07-25 21:35:00 +08:00
_contentSize = Size ( ( float ) pixelsWide , ( float ) pixelsHigh ) ;
2013-06-15 14:03:30 +08:00
_pixelsWide = pixelsWide ;
_pixelsHigh = pixelsHigh ;
_pixelFormat = pixelFormat ;
2013-07-25 21:35:00 +08:00
_maxS = 1 ;
_maxT = 1 ;
2012-04-19 14:35:52 +08:00
2013-06-15 14:03:30 +08:00
_hasPremultipliedAlpha = false ;
2013-07-25 21:35:00 +08:00
_hasMipmaps = mipmapsNum > 1 ;
2012-04-19 14:35:52 +08:00
2013-07-12 06:24:23 +08:00
setShaderProgram ( ShaderCache : : getInstance ( ) - > programForKey ( kShader_PositionTexture ) ) ;
2012-04-19 14:35:52 +08:00
return true ;
2010-07-15 18:15:00 +08:00
}
2013-07-04 08:44:41 +08:00
const char * Texture2D : : description ( void ) const
2010-07-15 18:15:00 +08:00
{
2013-06-20 14:13:12 +08:00
return String : : createWithFormat ( " <Texture2D | Name = %u | Dimensions = %u x %u | Coordinates = (%.2f, %.2f)> " , _name , _pixelsWide , _pixelsHigh , _maxS , _maxT ) - > getCString ( ) ;
2010-07-15 18:15:00 +08:00
}
2013-06-20 14:13:12 +08:00
// implementation Texture2D (Image)
2013-07-25 21:35:00 +08:00
bool Texture2D : : initWithImage ( Image * uiImage )
{
return initWithImage ( uiImage , kTexture2DPixelFormat_None ) ;
}
2012-04-08 14:16:29 +08:00
2013-07-25 21:35:00 +08:00
bool Texture2D : : initWithImage ( Image * uiImage , Texture2DPixelFormat format )
2010-07-15 18:15:00 +08:00
{
2012-04-24 15:02:18 +08:00
if ( uiImage = = NULL )
2012-04-19 14:35:52 +08:00
{
2013-06-20 14:13:12 +08:00
CCLOG ( " cocos2d: Texture2D. Can't create Texture. UIImage is nil " ) ;
2012-04-19 14:35:52 +08:00
return false ;
}
2013-07-19 15:37:54 +08:00
2013-07-25 21:35:00 +08:00
int imageWidth = uiImage - > getWidth ( ) ;
int imageHeight = uiImage - > getHeight ( ) ;
2013-07-19 15:37:54 +08:00
2013-07-12 06:24:23 +08:00
Configuration * conf = Configuration : : getInstance ( ) ;
2013-07-19 15:37:54 +08:00
2013-07-25 21:35:00 +08:00
int maxTextureSize = conf - > getMaxTextureSize ( ) ;
2012-04-24 15:02:18 +08:00
if ( imageWidth > maxTextureSize | | imageHeight > maxTextureSize )
2012-04-19 14:35:52 +08:00
{
2012-04-24 15:02:18 +08:00
CCLOG ( " cocos2d: WARNING: Image (%u x %u) is bigger than the supported %u x %u " , imageWidth , imageHeight , maxTextureSize , maxTextureSize ) ;
2013-02-04 17:38:22 +08:00
return false ;
2012-04-19 14:35:52 +08:00
}
2013-07-19 15:37:54 +08:00
unsigned char * tempData = uiImage - > getData ( ) ;
Size imageSize = Size ( ( float ) imageWidth , ( float ) imageHeight ) ;
2013-07-26 17:34:44 +08:00
Texture2DPixelFormat pixelFormat = kTexture2DPixelFormat_None ;
2013-07-25 21:35:00 +08:00
Texture2DPixelFormat renderFormat = uiImage - > getRenderFormat ( ) ;
2013-07-19 15:37:54 +08:00
size_t tempDataLen = uiImage - > getDataLen ( ) ;
2013-07-25 21:35:00 +08:00
if ( uiImage - > getNumberOfMipmaps ( ) > 1 )
2013-07-19 15:37:54 +08:00
{
2013-07-25 21:35:00 +08:00
if ( format ! = kTexture2DPixelFormat_None )
{
CCLog ( " cocos2d: WARNING: This image has more than 1 mipmaps and we will not convert the data format " ) ;
}
initWithMipmaps ( uiImage - > getMipmaps ( ) , uiImage - > getNumberOfMipmaps ( ) , uiImage - > getRenderFormat ( ) , imageWidth , imageHeight ) ;
return true ;
}
else if ( uiImage - > isCompressed ( ) )
2013-07-19 15:37:54 +08:00
{
2013-07-25 21:35:00 +08:00
if ( format ! = kTexture2DPixelFormat_None )
{
CCLog ( " cocos2d: WARNING: This image is compressed and we cann't convert it for now " ) ;
}
initWithData ( tempData , tempDataLen , uiImage - > getRenderFormat ( ) , imageWidth , imageHeight , imageSize ) ;
return true ;
2013-07-19 15:37:54 +08:00
}
2013-07-25 21:35:00 +08:00
else
{
// compute pixel format
if ( format ! = kTexture2DPixelFormat_None )
{
pixelFormat = format ;
} else
{
pixelFormat = g_defaultAlphaPixelFormat ;
}
2013-07-19 15:37:54 +08:00
2013-07-25 21:35:00 +08:00
unsigned char * outTempData = NULL ;
int outTempDataLen = 0 ;
2013-07-19 15:37:54 +08:00
2013-07-25 21:35:00 +08:00
pixelFormat = convertDataToFormat ( tempData , tempDataLen , renderFormat , pixelFormat , & outTempData , & outTempDataLen ) ;
2013-07-19 15:37:54 +08:00
2013-07-25 21:35:00 +08:00
initWithData ( outTempData , outTempDataLen , pixelFormat , imageWidth , imageHeight , imageSize ) ;
2013-07-19 15:37:54 +08:00
2013-07-25 21:35:00 +08:00
if ( outTempData ! = NULL & & outTempData ! = tempData )
{
2013-07-19 15:37:54 +08:00
2013-07-25 21:35:00 +08:00
delete [ ] outTempData ;
}
2013-07-19 15:37:54 +08:00
2013-07-25 21:35:00 +08:00
_hasPremultipliedAlpha = uiImage - > isPremultipliedAlpha ( ) ;
return true ;
}
2010-07-15 18:15:00 +08:00
}
2012-08-08 18:39:33 +08:00
2013-07-25 21:35:00 +08:00
Texture2DPixelFormat Texture2D : : convertI8ToFormat ( const unsigned char * data , int dataLen , Texture2DPixelFormat format , unsigned char * * outData , int * outDataLen )
2013-07-19 15:37:54 +08:00
{
switch ( format )
{
case kTexture2DPixelFormat_RGBA8888 :
* outDataLen = dataLen * 4 ;
* outData = new unsigned char [ * outDataLen ] ;
2013-07-25 21:35:00 +08:00
convertI8ToRGBA8888 ( data , dataLen , * outData ) ;
2013-07-19 15:37:54 +08:00
break ;
case kTexture2DPixelFormat_RGB888 :
* outDataLen = dataLen * 3 ;
* outData = new unsigned char [ * outDataLen ] ;
2013-07-25 21:35:00 +08:00
convertI8ToRGB888 ( data , dataLen , * outData ) ;
2013-07-19 15:37:54 +08:00
break ;
case kTexture2DPixelFormat_RGB565 :
* outDataLen = dataLen * 2 ;
* outData = new unsigned char [ * outDataLen ] ;
2013-07-25 21:35:00 +08:00
convertI8ToRGB565 ( data , dataLen , * outData ) ;
2013-07-19 15:37:54 +08:00
break ;
case kTexture2DPixelFormat_AI88 :
* outDataLen = dataLen * 2 ;
* outData = new unsigned char [ * outDataLen ] ;
2013-07-25 21:35:00 +08:00
convertI8ToAI88 ( data , dataLen , * outData ) ;
2013-07-19 15:37:54 +08:00
break ;
case kTexture2DPixelFormat_RGBA4444 :
* outDataLen = dataLen * 2 ;
* outData = new unsigned char [ * outDataLen ] ;
2013-07-25 21:35:00 +08:00
convertI8ToRGBA4444 ( data , dataLen , * outData ) ;
2013-07-19 15:37:54 +08:00
break ;
case kTexture2DPixelFormat_RGB5A1 :
* outDataLen = dataLen * 2 ;
* outData = new unsigned char [ * outDataLen ] ;
2013-07-25 21:35:00 +08:00
convertAI88ToRGB5A1 ( data , dataLen , * outData ) ;
2013-07-19 15:37:54 +08:00
break ;
default :
// unsupport convertion or don't need to convert
if ( format ! = kTexture2DPixelFormat_Automatic & & format ! = kTexture2DPixelFormat_I8 )
{
2013-07-25 21:35:00 +08:00
CCLOG ( " Can not convert image format kTexture2DPixelFormat_I8 to format ID:%d, we will use it's origin format kTexture2DPixelFormat_I8 " , format ) ;
2013-07-19 15:37:54 +08:00
}
2013-07-25 21:35:00 +08:00
2013-07-19 15:37:54 +08:00
* outData = ( unsigned char * ) data ;
* outDataLen = dataLen ;
return kTexture2DPixelFormat_I8 ;
}
return format ;
}
2013-07-25 21:35:00 +08:00
Texture2DPixelFormat Texture2D : : convertAI88ToFormat ( const unsigned char * data , int dataLen , Texture2DPixelFormat format , unsigned char * * outData , int * outDataLen )
2013-07-19 15:37:54 +08:00
{
switch ( format )
{
case kTexture2DPixelFormat_RGBA8888 :
* outDataLen = dataLen * 2 ;
* outData = new unsigned char [ * outDataLen ] ;
2013-07-25 21:35:00 +08:00
convertAI88ToRGBA8888 ( data , dataLen , * outData ) ;
2013-07-19 15:37:54 +08:00
break ;
case kTexture2DPixelFormat_RGB888 :
* outDataLen = dataLen / 2 * 3 ;
* outData = new unsigned char [ * outDataLen ] ;
2013-07-25 21:35:00 +08:00
convertAI88ToRGB888 ( data , dataLen , * outData ) ;
2013-07-19 15:37:54 +08:00
break ;
case kTexture2DPixelFormat_RGB565 :
* outDataLen = dataLen ;
* outData = new unsigned char [ * outDataLen ] ;
2013-07-25 21:35:00 +08:00
convertAI88ToRGB565 ( data , dataLen , * outData ) ;
2013-07-19 15:37:54 +08:00
break ;
case kTexture2DPixelFormat_A8 :
* outDataLen = dataLen / 2 ;
* outData = new unsigned char [ * outDataLen ] ;
2013-07-25 21:35:00 +08:00
convertAI88ToA8 ( data , dataLen , * outData ) ;
2013-07-19 15:37:54 +08:00
break ;
case kTexture2DPixelFormat_I8 :
* outDataLen = dataLen / 2 ;
* outData = new unsigned char [ * outDataLen ] ;
2013-07-25 21:35:00 +08:00
convertAI88ToI8 ( data , dataLen , * outData ) ;
2013-07-19 15:37:54 +08:00
break ;
case kTexture2DPixelFormat_RGBA4444 :
* outDataLen = dataLen ;
* outData = new unsigned char [ * outDataLen ] ;
2013-07-25 21:35:00 +08:00
convertAI88ToRGBA4444 ( data , dataLen , * outData ) ;
2013-07-19 15:37:54 +08:00
break ;
case kTexture2DPixelFormat_RGB5A1 :
* outDataLen = dataLen ;
* outData = new unsigned char [ * outDataLen ] ;
2013-07-25 21:35:00 +08:00
convertAI88ToRGB5A1 ( data , dataLen , * outData ) ;
2013-07-19 15:37:54 +08:00
break ;
default :
// unsupport convertion or don't need to convert
if ( format ! = kTexture2DPixelFormat_Automatic & & format ! = kTexture2DPixelFormat_AI88 )
{
2013-07-25 21:35:00 +08:00
CCLOG ( " Can not convert image format kTexture2DPixelFormat_AI88 to format ID:%d, we will use it's origin format kTexture2DPixelFormat_AI88 " , format ) ;
2013-07-19 15:37:54 +08:00
}
* outData = ( unsigned char * ) data ;
* outDataLen = dataLen ;
return kTexture2DPixelFormat_AI88 ;
break ;
}
return format ;
}
2013-07-25 21:35:00 +08:00
Texture2DPixelFormat Texture2D : : convertRGB888ToFormat ( const unsigned char * data , int dataLen , Texture2DPixelFormat format , unsigned char * * outData , int * outDataLen )
2013-07-19 15:37:54 +08:00
{
switch ( format )
{
case kTexture2DPixelFormat_RGBA8888 :
* outDataLen = dataLen / 3 * 4 ;
* outData = new unsigned char [ * outDataLen ] ;
2013-07-25 21:35:00 +08:00
convertRGB888ToRGBA8888 ( data , dataLen , * outData ) ;
2013-07-19 15:37:54 +08:00
break ;
case kTexture2DPixelFormat_RGB565 :
* outDataLen = dataLen / 3 * 2 ;
* outData = new unsigned char [ * outDataLen ] ;
2013-07-25 21:35:00 +08:00
convertRGB888ToRGB565 ( data , dataLen , * outData ) ;
2013-07-19 15:37:54 +08:00
break ;
case kTexture2DPixelFormat_I8 :
* outDataLen = dataLen / 3 ;
* outData = new unsigned char [ * outDataLen ] ;
2013-07-25 21:35:00 +08:00
convertRGB888ToI8 ( data , dataLen , * outData ) ;
2013-07-19 15:37:54 +08:00
break ;
case kTexture2DPixelFormat_AI88 :
* outDataLen = dataLen / 3 * 2 ;
* outData = new unsigned char [ * outDataLen ] ;
2013-07-25 21:35:00 +08:00
convertRGB888ToAI88 ( data , dataLen , * outData ) ;
2013-07-19 15:37:54 +08:00
break ;
case kTexture2DPixelFormat_RGBA4444 :
* outDataLen = dataLen / 3 * 2 ;
* outData = new unsigned char [ * outDataLen ] ;
2013-07-25 21:35:00 +08:00
convertRGB888ToRGBA4444 ( data , dataLen , * outData ) ;
2013-07-19 15:37:54 +08:00
break ;
case kTexture2DPixelFormat_RGB5A1 :
* outDataLen = dataLen ;
* outData = new unsigned char [ * outDataLen ] ;
2013-07-25 21:35:00 +08:00
convertRGB888ToRGB5A1 ( data , dataLen , * outData ) ;
2013-07-19 15:37:54 +08:00
break ;
default :
// unsupport convertion or don't need to convert
if ( format ! = kTexture2DPixelFormat_Automatic & & format ! = kTexture2DPixelFormat_RGB888 )
{
2013-07-25 21:35:00 +08:00
CCLOG ( " Can not convert image format kTexture2DPixelFormat_RGB888 to format ID:%d, we will use it's origin format kTexture2DPixelFormat_RGB888 " , format ) ;
2013-07-19 15:37:54 +08:00
}
* outData = ( unsigned char * ) data ;
* outDataLen = dataLen ;
return kTexture2DPixelFormat_RGB888 ;
}
return format ;
}
2013-07-25 21:35:00 +08:00
Texture2DPixelFormat Texture2D : : convertRGBA8888ToFormat ( const unsigned char * data , int dataLen , Texture2DPixelFormat format , unsigned char * * outData , int * outDataLen )
2013-07-19 15:37:54 +08:00
{
switch ( format )
{
case kTexture2DPixelFormat_RGB888 :
* outDataLen = dataLen / 4 * 3 ;
* outData = new unsigned char [ * outDataLen ] ;
2013-07-25 21:35:00 +08:00
convertRGBA8888ToRGB888 ( data , dataLen , * outData ) ;
2013-07-19 15:37:54 +08:00
break ;
case kTexture2DPixelFormat_RGB565 :
* outDataLen = dataLen / 2 ;
* outData = new unsigned char [ * outDataLen ] ;
2013-07-25 21:35:00 +08:00
convertRGBA8888ToRGB565 ( data , dataLen , * outData ) ;
2013-07-19 15:37:54 +08:00
break ;
case kTexture2DPixelFormat_A8 :
* outDataLen = dataLen / 4 ;
* outData = new unsigned char [ * outDataLen ] ;
2013-07-25 21:35:00 +08:00
convertRGBA8888ToA8 ( data , dataLen , * outData ) ;
2013-07-19 15:37:54 +08:00
break ;
case kTexture2DPixelFormat_I8 :
* outDataLen = dataLen / 4 ;
* outData = new unsigned char [ * outDataLen ] ;
2013-07-25 21:35:00 +08:00
convertRGBA8888ToI8 ( data , dataLen , * outData ) ;
2013-07-19 15:37:54 +08:00
break ;
case kTexture2DPixelFormat_AI88 :
* outDataLen = dataLen / 2 ;
* outData = new unsigned char [ * outDataLen ] ;
2013-07-25 21:35:00 +08:00
convertRGBA8888ToAI88 ( data , dataLen , * outData ) ;
2013-07-19 15:37:54 +08:00
break ;
case kTexture2DPixelFormat_RGBA4444 :
* outDataLen = dataLen / 2 ;
* outData = new unsigned char [ * outDataLen ] ;
2013-07-25 21:35:00 +08:00
convertRGBA8888ToRGBA4444 ( data , dataLen , * outData ) ;
2013-07-19 15:37:54 +08:00
break ;
case kTexture2DPixelFormat_RGB5A1 :
* outDataLen = dataLen / 2 ;
* outData = new unsigned char [ * outDataLen ] ;
2013-07-25 21:35:00 +08:00
convertRGBA8888ToRGB5A1 ( data , dataLen , * outData ) ;
2013-07-19 15:37:54 +08:00
break ;
default :
// unsupport convertion or don't need to convert
if ( format ! = kTexture2DPixelFormat_Automatic & & format ! = kTexture2DPixelFormat_RGBA8888 )
{
2013-07-25 21:35:00 +08:00
CCLOG ( " Can not convert image format kTexture2DPixelFormat_RGBA8888 to format ID:%d, we will use it's origin format kTexture2DPixelFormat_RGBA8888 " , format ) ;
2013-07-19 15:37:54 +08:00
}
* outData = ( unsigned char * ) data ;
* outDataLen = dataLen ;
return kTexture2DPixelFormat_RGBA8888 ;
}
return format ;
}
2013-07-17 17:12:04 +08:00
/*
convert map :
1. kTexture2DPixelFormat_RGBA8888
2. kTexture2DPixelFormat_RGB888
3. kTexture2DPixelFormat_RGB565
4. kTexture2DPixelFormat_A8
5. kTexture2DPixelFormat_I8
6. kTexture2DPixelFormat_AI88
7. kTexture2DPixelFormat_RGBA4444
8. kTexture2DPixelFormat_RGB5A1
gray ( 5 ) - > 1235678
gray alpha ( 6 ) - > 12345678
rgb ( 2 ) - > 1235678
rgba ( 1 ) - > 12345678
*/
2013-07-25 21:35:00 +08:00
Texture2DPixelFormat Texture2D : : convertDataToFormat ( const unsigned char * data , int dataLen , Texture2DPixelFormat originFormat , Texture2DPixelFormat format , unsigned char * * outData , int * outDataLen )
2013-07-19 15:37:54 +08:00
{
2013-07-25 21:35:00 +08:00
switch ( originFormat )
2013-07-19 15:37:54 +08:00
{
2013-07-25 21:35:00 +08:00
case kTexture2DPixelFormat_I8 :
return convertI8ToFormat ( data , dataLen , format , outData , outDataLen ) ;
case kTexture2DPixelFormat_AI88 :
return convertAI88ToFormat ( data , dataLen , format , outData , outDataLen ) ;
case kTexture2DPixelFormat_RGB888 :
return convertRGB888ToFormat ( data , dataLen , format , outData , outDataLen ) ;
case kTexture2DPixelFormat_RGBA8888 :
return convertRGBA8888ToFormat ( data , dataLen , format , outData , outDataLen ) ;
default :
CCLOG ( " unsupport convert for format %d to format %d " , originFormat , format ) ;
* outData = ( unsigned char * ) data ;
* outDataLen = dataLen ;
return originFormat ;
2012-04-19 14:35:52 +08:00
}
2010-07-26 17:32:23 +08:00
}
2010-07-15 18:15:00 +08:00
2013-06-20 14:13:12 +08:00
// implementation Texture2D (Text)
bool Texture2D : : initWithString ( const char * text , const char * fontName , float fontSize )
2010-07-15 18:15:00 +08:00
{
2013-07-12 14:30:26 +08:00
return initWithString ( text , fontName , fontSize , Size ( 0 , 0 ) , kTextAlignmentCenter , kVerticalTextAlignmentTop ) ;
2010-07-26 17:32:23 +08:00
}
2012-06-08 14:11:48 +08:00
2013-06-20 14:13:12 +08:00
bool Texture2D : : initWithString ( const char * text , const char * fontName , float fontSize , const Size & dimensions , TextAlignment hAlignment , VerticalTextAlignment vAlignment )
2013-04-26 09:22:26 +08:00
{
2013-07-10 04:21:43 +08:00
FontDefinition tempDef ;
2013-05-01 07:36:14 +08:00
2013-07-10 04:21:43 +08:00
tempDef . _shadow . _shadowEnabled = false ;
tempDef . _stroke . _strokeEnabled = false ;
2013-05-01 07:36:14 +08:00
2013-07-10 04:21:43 +08:00
tempDef . _fontName = std : : string ( fontName ) ;
tempDef . _fontSize = fontSize ;
tempDef . _dimensions = dimensions ;
tempDef . _alignment = hAlignment ;
tempDef . _vertAlignment = vAlignment ;
tempDef . _fontFillColor = Color3B : : WHITE ;
return initWithString ( text , tempDef ) ;
2013-05-02 08:11:53 +08:00
}
2013-07-08 15:18:16 +08:00
bool Texture2D : : initWithString ( const char * text , const FontDefinition & textDefinition )
2013-05-02 08:11:53 +08:00
{
2013-07-16 16:47:35 +08:00
# if CC_ENABLE_CACHE_TEXTURE_DATA
// cache the texture data
VolatileTexture : : addStringTexture ( this , text , textDefinition ) ;
# endif
2013-07-10 04:21:43 +08:00
bool bRet = false ;
2013-07-19 15:37:54 +08:00
Image : : TextAlign eAlign ;
2013-04-26 09:22:26 +08:00
2013-07-10 04:21:43 +08:00
if ( kVerticalTextAlignmentTop = = textDefinition . _vertAlignment )
{
eAlign = ( kTextAlignmentCenter = = textDefinition . _alignment ) ? Image : : kAlignTop
: ( kTextAlignmentLeft = = textDefinition . _alignment ) ? Image : : kAlignTopLeft : Image : : kAlignTopRight ;
}
else if ( kVerticalTextAlignmentCenter = = textDefinition . _vertAlignment )
{
eAlign = ( kTextAlignmentCenter = = textDefinition . _alignment ) ? Image : : kAlignCenter
: ( kTextAlignmentLeft = = textDefinition . _alignment ) ? Image : : kAlignLeft : Image : : kAlignRight ;
}
else if ( kVerticalTextAlignmentBottom = = textDefinition . _vertAlignment )
{
eAlign = ( kTextAlignmentCenter = = textDefinition . _alignment ) ? Image : : kAlignBottom
: ( kTextAlignmentLeft = = textDefinition . _alignment ) ? Image : : kAlignBottomLeft : Image : : kAlignBottomRight ;
}
else
{
CCAssert ( false , " Not supported alignment format! " ) ;
return false ;
}
2013-04-27 07:34:10 +08:00
2013-07-16 16:47:35 +08:00
# if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
// handle shadow parameters
bool shadowEnabled = false ;
float shadowDX = 0.0f ;
float shadowDY = 0.0f ;
float shadowBlur = 0.0f ;
float shadowOpacity = 0.0f ;
2013-04-27 07:34:10 +08:00
2013-07-16 16:47:35 +08:00
if ( textDefinition . _shadow . _shadowEnabled )
{
shadowEnabled = true ;
shadowDX = textDefinition . _shadow . _shadowOffset . width ;
shadowDY = textDefinition . _shadow . _shadowOffset . height ;
shadowBlur = textDefinition . _shadow . _shadowBlur ;
shadowOpacity = textDefinition . _shadow . _shadowOpacity ;
}
2013-04-27 07:34:10 +08:00
2013-07-16 16:47:35 +08:00
// handle stroke parameters
bool strokeEnabled = false ;
float strokeColorR = 0.0f ;
float strokeColorG = 0.0f ;
float strokeColorB = 0.0f ;
float strokeSize = 0.0f ;
2013-04-27 07:34:10 +08:00
2013-07-16 16:47:35 +08:00
if ( textDefinition . _stroke . _strokeEnabled )
{
strokeEnabled = true ;
strokeColorR = textDefinition . _stroke . _strokeColor . r / 255.0f ;
strokeColorG = textDefinition . _stroke . _strokeColor . g / 255.0f ;
strokeColorB = textDefinition . _stroke . _strokeColor . b / 255.0f ;
strokeSize = textDefinition . _stroke . _strokeSize ;
}
2011-01-19 14:23:26 +08:00
2013-07-16 16:47:35 +08:00
Image * pImage = new Image ( ) ;
do
{
CC_BREAK_IF ( NULL = = pImage ) ;
2013-05-02 08:11:53 +08:00
2013-07-16 16:47:35 +08:00
bRet = pImage - > initWithStringShadowStroke ( text ,
( int ) textDefinition . _dimensions . width ,
( int ) textDefinition . _dimensions . height ,
eAlign ,
textDefinition . _fontName . c_str ( ) ,
textDefinition . _fontSize ,
textDefinition . _fontFillColor . r / 255.0f ,
textDefinition . _fontFillColor . g / 255.0f ,
textDefinition . _fontFillColor . b / 255.0f ,
shadowEnabled ,
shadowDX ,
shadowDY ,
shadowOpacity ,
shadowBlur ,
strokeEnabled ,
strokeColorR ,
strokeColorG ,
strokeColorB ,
strokeSize ) ;
2013-05-02 08:11:53 +08:00
2013-07-16 16:47:35 +08:00
CC_BREAK_IF ( ! bRet ) ;
bRet = initWithImage ( pImage ) ;
2013-05-02 08:11:53 +08:00
2013-07-16 16:47:35 +08:00
} while ( 0 ) ;
2013-05-02 08:11:53 +08:00
2013-07-16 16:47:35 +08:00
CC_SAFE_RELEASE ( pImage ) ;
2013-04-26 09:22:26 +08:00
2013-07-16 16:47:35 +08:00
return bRet ;
# else
bool requestUnsupported = textDefinition . _shadow . _shadowEnabled | | textDefinition . _stroke . _strokeEnabled ;
CCAssert ( requestUnsupported = = false , " Currently shadow and stroke only supported on iOS and Android! " ) ;
Image * pImage = new Image ( ) ;
do
{
CC_BREAK_IF ( NULL = = pImage ) ;
bRet = pImage - > initWithString ( text , ( int ) textDefinition . _dimensions . width , ( int ) textDefinition . _dimensions . height , eAlign , textDefinition . _fontName . c_str ( ) , ( int ) textDefinition . _fontSize ) ;
CC_BREAK_IF ( ! bRet ) ;
bRet = initWithImage ( pImage ) ;
} while ( 0 ) ;
2013-04-26 09:22:26 +08:00
2013-07-16 16:47:35 +08:00
CC_SAFE_RELEASE ( pImage ) ;
return bRet ;
# endif
2010-08-11 18:09:10 +08:00
}
2010-07-15 18:15:00 +08:00
2013-06-20 14:13:12 +08:00
// implementation Texture2D (Drawing)
2010-07-15 18:15:00 +08:00
2013-06-20 14:13:12 +08:00
void Texture2D : : drawAtPoint ( const Point & point )
2010-07-15 18:15:00 +08:00
{
2013-07-10 04:21:43 +08:00
GLfloat coordinates [ ] = {
2013-06-15 14:03:30 +08:00
0.0f , _maxT ,
_maxS , _maxT ,
2012-04-19 14:35:52 +08:00
0.0f , 0.0f ,
2013-06-15 14:03:30 +08:00
_maxS , 0.0f } ;
2010-07-15 18:15:00 +08:00
2013-06-15 14:03:30 +08:00
GLfloat width = ( GLfloat ) _pixelsWide * _maxS ,
height = ( GLfloat ) _pixelsHigh * _maxT ;
2010-07-15 18:15:00 +08:00
2012-04-19 14:35:52 +08:00
GLfloat vertices [ ] = {
point . x , point . y ,
width + point . x , point . y ,
point . x , height + point . y ,
width + point . x , height + point . y } ;
2012-03-14 14:55:17 +08:00
2013-06-20 14:13:12 +08:00
ccGLEnableVertexAttribs ( kVertexAttribFlag_Position | kVertexAttribFlag_TexCoords ) ;
2013-06-15 14:03:30 +08:00
_shaderProgram - > use ( ) ;
_shaderProgram - > setUniformsForBuiltins ( ) ;
2012-03-21 11:07:31 +08:00
2013-06-15 14:03:30 +08:00
ccGLBindTexture2D ( _name ) ;
2012-03-21 11:07:31 +08:00
2013-04-09 12:08:34 +08:00
# ifdef EMSCRIPTEN
setGLBufferData ( vertices , 8 * sizeof ( GLfloat ) , 0 ) ;
2013-06-20 14:13:12 +08:00
glVertexAttribPointer ( kVertexAttrib_Position , 2 , GL_FLOAT , GL_FALSE , 0 , 0 ) ;
2013-04-09 12:08:34 +08:00
setGLBufferData ( coordinates , 8 * sizeof ( GLfloat ) , 1 ) ;
2013-06-20 14:13:12 +08:00
glVertexAttribPointer ( kVertexAttrib_TexCoords , 2 , GL_FLOAT , GL_FALSE , 0 , 0 ) ;
2013-04-09 12:08:34 +08:00
# else
2013-06-20 14:13:12 +08:00
glVertexAttribPointer ( kVertexAttrib_Position , 2 , GL_FLOAT , GL_FALSE , 0 , vertices ) ;
glVertexAttribPointer ( kVertexAttrib_TexCoords , 2 , GL_FLOAT , GL_FALSE , 0 , coordinates ) ;
2013-04-09 12:08:34 +08:00
# endif // EMSCRIPTEN
2010-07-15 18:15:00 +08:00
2012-04-19 14:35:52 +08:00
glDrawArrays ( GL_TRIANGLE_STRIP , 0 , 4 ) ;
2010-07-15 18:15:00 +08:00
}
2013-06-20 14:13:12 +08:00
void Texture2D : : drawInRect ( const Rect & rect )
2010-07-15 18:15:00 +08:00
{
2012-04-19 14:35:52 +08:00
GLfloat coordinates [ ] = {
2013-06-15 14:03:30 +08:00
0.0f , _maxT ,
_maxS , _maxT ,
2012-04-19 14:35:52 +08:00
0.0f , 0.0f ,
2013-06-15 14:03:30 +08:00
_maxS , 0.0f } ;
2010-07-15 18:15:00 +08:00
2012-04-19 14:35:52 +08:00
GLfloat vertices [ ] = { rect . origin . x , rect . origin . y , /*0.0f,*/
rect . origin . x + rect . size . width , rect . origin . y , /*0.0f,*/
rect . origin . x , rect . origin . y + rect . size . height , /*0.0f,*/
rect . origin . x + rect . size . width , rect . origin . y + rect . size . height , /*0.0f*/ } ;
2010-07-15 18:15:00 +08:00
2013-06-20 14:13:12 +08:00
ccGLEnableVertexAttribs ( kVertexAttribFlag_Position | kVertexAttribFlag_TexCoords ) ;
2013-06-15 14:03:30 +08:00
_shaderProgram - > use ( ) ;
_shaderProgram - > setUniformsForBuiltins ( ) ;
2012-03-21 11:07:31 +08:00
2013-06-15 14:03:30 +08:00
ccGLBindTexture2D ( _name ) ;
2012-03-21 11:07:31 +08:00
2013-04-09 12:08:34 +08:00
# ifdef EMSCRIPTEN
setGLBufferData ( vertices , 8 * sizeof ( GLfloat ) , 0 ) ;
2013-06-20 14:13:12 +08:00
glVertexAttribPointer ( kVertexAttrib_Position , 2 , GL_FLOAT , GL_FALSE , 0 , 0 ) ;
2013-04-09 12:08:34 +08:00
setGLBufferData ( coordinates , 8 * sizeof ( GLfloat ) , 1 ) ;
2013-06-20 14:13:12 +08:00
glVertexAttribPointer ( kVertexAttrib_TexCoords , 2 , GL_FLOAT , GL_FALSE , 0 , 0 ) ;
2013-04-09 12:08:34 +08:00
# else
2013-06-20 14:13:12 +08:00
glVertexAttribPointer ( kVertexAttrib_Position , 2 , GL_FLOAT , GL_FALSE , 0 , vertices ) ;
glVertexAttribPointer ( kVertexAttrib_TexCoords , 2 , GL_FLOAT , GL_FALSE , 0 , coordinates ) ;
2013-04-09 12:08:34 +08:00
# endif // EMSCRIPTEN
2012-04-19 14:35:52 +08:00
glDrawArrays ( GL_TRIANGLE_STRIP , 0 , 4 ) ;
2010-07-15 18:15:00 +08:00
}
2013-06-20 14:13:12 +08:00
void Texture2D : : PVRImagesHavePremultipliedAlpha ( bool haveAlphaPremultiplied )
2011-07-19 15:14:59 +08:00
{
2011-12-20 14:59:15 +08:00
PVRHaveAlphaPremultiplied_ = haveAlphaPremultiplied ;
2010-07-15 18:15:00 +08:00
}
2011-07-19 15:14:59 +08:00
2010-07-15 18:15:00 +08:00
//
// Use to apply MIN/MAG filter
//
2013-06-20 14:13:12 +08:00
// implementation Texture2D (GLFilter)
2010-07-15 18:15:00 +08:00
2013-06-20 14:13:12 +08:00
void Texture2D : : generateMipmap ( )
2010-07-15 18:15:00 +08:00
{
2013-06-15 14:03:30 +08:00
CCAssert ( _pixelsWide = = ccNextPOT ( _pixelsWide ) & & _pixelsHigh = = ccNextPOT ( _pixelsHigh ) , " Mipmap texture only works in POT textures " ) ;
ccGLBindTexture2D ( _name ) ;
2012-04-19 14:35:52 +08:00
glGenerateMipmap ( GL_TEXTURE_2D ) ;
2013-06-15 14:03:30 +08:00
_hasMipmaps = true ;
2010-07-15 18:15:00 +08:00
}
2013-07-04 08:44:41 +08:00
bool Texture2D : : hasMipmaps ( ) const
2012-06-15 15:10:40 +08:00
{
2013-06-15 14:03:30 +08:00
return _hasMipmaps ;
2012-06-15 15:10:40 +08:00
}
2013-07-04 08:59:22 +08:00
void Texture2D : : setTexParameters ( const ccTexParams & texParams )
2010-07-15 18:15:00 +08:00
{
2013-07-04 08:59:22 +08:00
CCAssert ( ( _pixelsWide = = ccNextPOT ( _pixelsWide ) | | texParams . wrapS = = GL_CLAMP_TO_EDGE ) & &
( _pixelsHigh = = ccNextPOT ( _pixelsHigh ) | | texParams . wrapT = = GL_CLAMP_TO_EDGE ) ,
2012-06-08 14:11:48 +08:00
" GL_CLAMP_TO_EDGE should be used in NPOT dimensions " ) ;
2013-06-15 14:03:30 +08:00
ccGLBindTexture2D ( _name ) ;
2013-07-04 08:59:22 +08:00
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , texParams . minFilter ) ;
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , texParams . magFilter ) ;
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_WRAP_S , texParams . wrapS ) ;
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_WRAP_T , texParams . wrapT ) ;
2012-08-31 03:02:05 +08:00
# if CC_ENABLE_CACHE_TEXTURE_DATA
VolatileTexture : : setTexParameters ( this , texParams ) ;
# endif
2010-07-15 18:15:00 +08:00
}
2013-06-20 14:13:12 +08:00
void Texture2D : : setAliasTexParameters ( )
2010-07-15 18:15:00 +08:00
{
2013-06-15 14:03:30 +08:00
ccGLBindTexture2D ( _name ) ;
2012-06-13 16:20:58 +08:00
2013-06-15 14:03:30 +08:00
if ( ! _hasMipmaps )
2012-06-13 16:20:58 +08:00
{
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_NEAREST ) ;
}
else
{
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_NEAREST_MIPMAP_NEAREST ) ;
}
2012-06-08 14:11:48 +08:00
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , GL_NEAREST ) ;
2012-08-31 03:02:05 +08:00
# if CC_ENABLE_CACHE_TEXTURE_DATA
2013-06-15 14:03:30 +08:00
ccTexParams texParams = { ( GLuint ) ( _hasMipmaps ? GL_NEAREST_MIPMAP_NEAREST : GL_NEAREST ) , GL_NEAREST , GL_NONE , GL_NONE } ;
2013-07-04 08:59:22 +08:00
VolatileTexture : : setTexParameters ( this , texParams ) ;
2012-08-31 03:02:05 +08:00
# endif
2010-07-15 18:15:00 +08:00
}
2013-06-20 14:13:12 +08:00
void Texture2D : : setAntiAliasTexParameters ( )
2010-07-15 18:15:00 +08:00
{
2013-06-15 14:03:30 +08:00
ccGLBindTexture2D ( _name ) ;
2012-06-13 16:20:58 +08:00
2013-06-15 14:03:30 +08:00
if ( ! _hasMipmaps )
2012-06-13 16:20:58 +08:00
{
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_LINEAR ) ;
}
else
{
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_LINEAR_MIPMAP_NEAREST ) ;
}
2012-06-08 14:11:48 +08:00
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , GL_LINEAR ) ;
2012-08-31 03:02:05 +08:00
# if CC_ENABLE_CACHE_TEXTURE_DATA
2013-06-15 14:03:30 +08:00
ccTexParams texParams = { ( GLuint ) ( _hasMipmaps ? GL_LINEAR_MIPMAP_NEAREST : GL_LINEAR ) , GL_LINEAR , GL_NONE , GL_NONE } ;
2013-07-04 08:59:22 +08:00
VolatileTexture : : setTexParameters ( this , texParams ) ;
2012-08-31 03:02:05 +08:00
# endif
2010-07-15 18:15:00 +08:00
}
2013-07-04 08:44:41 +08:00
const char * Texture2D : : stringForFormat ( ) const
2012-06-13 16:20:58 +08:00
{
2013-06-15 14:03:30 +08:00
switch ( _pixelFormat )
2012-06-13 16:20:58 +08:00
{
2013-06-20 14:13:12 +08:00
case kTexture2DPixelFormat_RGBA8888 :
2012-06-13 16:20:58 +08:00
return " RGBA8888 " ;
2013-06-20 14:13:12 +08:00
case kTexture2DPixelFormat_RGB888 :
2012-06-13 16:20:58 +08:00
return " RGB888 " ;
2013-06-20 14:13:12 +08:00
case kTexture2DPixelFormat_RGB565 :
2012-06-13 16:20:58 +08:00
return " RGB565 " ;
2013-06-20 14:13:12 +08:00
case kTexture2DPixelFormat_RGBA4444 :
2012-06-13 16:20:58 +08:00
return " RGBA4444 " ;
2013-06-20 14:13:12 +08:00
case kTexture2DPixelFormat_RGB5A1 :
2012-06-13 16:20:58 +08:00
return " RGB5A1 " ;
2013-06-20 14:13:12 +08:00
case kTexture2DPixelFormat_AI88 :
2012-06-13 16:20:58 +08:00
return " AI88 " ;
2013-06-20 14:13:12 +08:00
case kTexture2DPixelFormat_A8 :
2012-06-13 16:20:58 +08:00
return " A8 " ;
2013-06-20 14:13:12 +08:00
case kTexture2DPixelFormat_I8 :
2012-06-13 16:20:58 +08:00
return " I8 " ;
2013-06-20 14:13:12 +08:00
case kTexture2DPixelFormat_PVRTC4 :
2012-06-13 16:20:58 +08:00
return " PVRTC4 " ;
2013-06-20 14:13:12 +08:00
case kTexture2DPixelFormat_PVRTC2 :
2012-06-13 16:20:58 +08:00
return " PVRTC2 " ;
default :
2012-09-17 15:02:24 +08:00
CCAssert ( false , " unrecognized pixel format " ) ;
2013-06-15 14:03:30 +08:00
CCLOG ( " stringForFormat: %ld, cannot give useful result " , ( long ) _pixelFormat ) ;
2012-06-13 16:20:58 +08:00
break ;
}
return NULL ;
}
2012-06-11 21:58:04 +08:00
2010-07-15 18:15:00 +08:00
//
// Texture options for images that contains alpha
//
2013-06-20 14:13:12 +08:00
// implementation Texture2D (PixelFormat)
2010-07-15 18:15:00 +08:00
2013-06-20 14:13:12 +08:00
void Texture2D : : setDefaultAlphaPixelFormat ( Texture2DPixelFormat format )
2010-07-15 18:15:00 +08:00
{
2012-04-19 14:35:52 +08:00
g_defaultAlphaPixelFormat = format ;
2010-07-15 18:15:00 +08:00
}
2013-06-20 14:13:12 +08:00
Texture2DPixelFormat Texture2D : : defaultAlphaPixelFormat ( )
2010-07-15 18:15:00 +08:00
{
2012-04-19 14:35:52 +08:00
return g_defaultAlphaPixelFormat ;
2010-07-15 18:15:00 +08:00
}
2010-08-02 10:58:00 +08:00
2013-07-04 08:44:41 +08:00
unsigned int Texture2D : : bitsPerPixelForFormat ( Texture2DPixelFormat format ) const
2012-06-13 16:20:58 +08:00
{
2013-07-25 21:35:00 +08:00
if ( format = = kTexture2DPixelFormat_None )
{
return 0 ;
}
return g_texturePixelFormatInfoTables . at ( format ) . bpp ;
2012-06-13 16:20:58 +08:00
}
2013-07-04 08:44:41 +08:00
unsigned int Texture2D : : bitsPerPixelForFormat ( ) const
2012-06-13 16:20:58 +08:00
{
2013-06-15 14:03:30 +08:00
return this - > bitsPerPixelForFormat ( _pixelFormat ) ;
2012-06-13 16:20:58 +08:00
}
2012-04-19 14:35:52 +08:00
2011-07-05 10:47:25 +08:00
2012-04-18 18:43:45 +08:00
NS_CC_END