2010-08-02 10:58:00 +08:00
|
|
|
/****************************************************************************
|
2011-03-19 14:45:51 +08:00
|
|
|
Copyright (c) 2010-2011 cocos2d-x.org
|
|
|
|
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-16 10:34:42 +08:00
|
|
|
|
2011-01-17 21:16:25 +08:00
|
|
|
#if 0 // PVR TEXTURE CANNOT CROSS PLATFORM
|
|
|
|
|
2010-07-16 10:34:42 +08:00
|
|
|
#include "CCPVRTexture.h"
|
|
|
|
#include "ccMacros.h"
|
2011-03-07 17:11:57 +08:00
|
|
|
#include "CCData.h"
|
2010-07-16 10:34:42 +08:00
|
|
|
|
2011-01-17 21:16:25 +08:00
|
|
|
namespace cocos2d {
|
2010-07-20 13:49:13 +08:00
|
|
|
|
2010-07-16 10:34:42 +08:00
|
|
|
#define PVR_TEXTURE_FLAG_TYPE_MASK 0xff
|
|
|
|
|
|
|
|
static char* gPVRTexIdentifier[] = {"PVR!"};
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
kPVRTextureFlagTypePVRTC_2 = 24,
|
|
|
|
kPVRTextureFlagTypePVRTC_4
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct _PVRTexHeader
|
|
|
|
{
|
2010-08-25 11:38:08 +08:00
|
|
|
unsigned int headerLength;
|
|
|
|
unsigned int height;
|
|
|
|
unsigned int width;
|
|
|
|
unsigned int numMipmaps;
|
|
|
|
unsigned int flags;
|
|
|
|
unsigned int dataLength;
|
|
|
|
unsigned int bpp;
|
|
|
|
unsigned int bitmaskRed;
|
|
|
|
unsigned int bitmaskGreen;
|
|
|
|
unsigned int bitmaskBlue;
|
|
|
|
unsigned int bitmaskAlpha;
|
|
|
|
unsigned int pvrTag;
|
|
|
|
unsigned int numSurfs;
|
2010-07-16 10:34:42 +08:00
|
|
|
} PVRTexHeader;
|
|
|
|
|
|
|
|
CCPVRTexture::CCPVRTexture()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CCPVRTexture::~CCPVRTexture()
|
|
|
|
{
|
|
|
|
CCLOGINFO( "cocos2d: deallocing CCPVRTexture" );
|
|
|
|
|
|
|
|
m_pImageData->removeAllObjects();
|
2011-03-07 17:11:57 +08:00
|
|
|
CC_SAFE_DELETE(m_pImageData)
|
2010-07-16 10:34:42 +08:00
|
|
|
|
|
|
|
if (m_uName != 0 && ! m_bRetainName )
|
|
|
|
glDeleteTextures(1, &m_uName);
|
|
|
|
}
|
|
|
|
|
2010-07-16 14:15:06 +08:00
|
|
|
GLuint CCPVRTexture::getName()
|
|
|
|
{
|
|
|
|
return m_uName;
|
|
|
|
}
|
|
|
|
|
2010-08-25 11:38:08 +08:00
|
|
|
unsigned int CCPVRTexture::getWidth()
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
|
|
|
return m_uWidth;
|
|
|
|
}
|
|
|
|
|
2010-08-25 11:38:08 +08:00
|
|
|
unsigned int CCPVRTexture::getHeight()
|
2010-07-16 14:15:06 +08:00
|
|
|
{
|
|
|
|
return m_uHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLenum CCPVRTexture::getInternalFormat()
|
|
|
|
{
|
|
|
|
return m_uInternalFormat;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCPVRTexture::getHasAlpha()
|
|
|
|
{
|
|
|
|
return m_bHasAlpha;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCPVRTexture::getRetainName()
|
|
|
|
{
|
|
|
|
return m_bRetainName;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCPVRTexture::setRetainName(bool var)
|
|
|
|
{
|
|
|
|
m_bRetainName = var;
|
|
|
|
}
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
/** @todo CCData uint8_t
|
|
|
|
- (BOOL)unpackPVRData:(CCData *)data
|
2010-07-16 10:34:42 +08:00
|
|
|
{
|
|
|
|
BOOL success = FALSE;
|
|
|
|
PVRTexHeader *header = NULL;
|
|
|
|
uint32_t flags, pvrTag;
|
|
|
|
uint32_t dataLength = 0, dataOffset = 0, dataSize = 0;
|
|
|
|
uint32_t blockSize = 0, widthBlocks = 0, heightBlocks = 0;
|
|
|
|
uint32_t width = 0, height = 0, bpp = 4;
|
|
|
|
uint8_t *bytes = NULL;
|
|
|
|
uint32_t formatFlags;
|
|
|
|
|
|
|
|
header = (PVRTexHeader *)[data bytes];
|
|
|
|
|
|
|
|
pvrTag = CFSwapInt32LittleToHost(header->pvrTag);
|
|
|
|
|
|
|
|
if ((uint32_t)gPVRTexIdentifier[0] != ((pvrTag >> 0) & 0xff) ||
|
|
|
|
(uint32_t)gPVRTexIdentifier[1] != ((pvrTag >> 8) & 0xff) ||
|
|
|
|
(uint32_t)gPVRTexIdentifier[2] != ((pvrTag >> 16) & 0xff) ||
|
|
|
|
(uint32_t)gPVRTexIdentifier[3] != ((pvrTag >> 24) & 0xff))
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
flags = CFSwapInt32LittleToHost(header->flags);
|
|
|
|
formatFlags = flags & PVR_TEXTURE_FLAG_TYPE_MASK;
|
|
|
|
|
|
|
|
if (formatFlags == kPVRTextureFlagTypePVRTC_4 || formatFlags == kPVRTextureFlagTypePVRTC_2)
|
|
|
|
{
|
|
|
|
[_imageData removeAllObjects];
|
|
|
|
|
|
|
|
if (formatFlags == kPVRTextureFlagTypePVRTC_4)
|
|
|
|
_internalFormat = GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG;
|
|
|
|
else if (formatFlags == kPVRTextureFlagTypePVRTC_2)
|
|
|
|
_internalFormat = GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG;
|
|
|
|
|
|
|
|
_width = width = CFSwapInt32LittleToHost(header->width);
|
|
|
|
_height = height = CFSwapInt32LittleToHost(header->height);
|
|
|
|
|
|
|
|
if (CFSwapInt32LittleToHost(header->bitmaskAlpha))
|
|
|
|
_hasAlpha = TRUE;
|
|
|
|
else
|
|
|
|
_hasAlpha = FALSE;
|
|
|
|
|
|
|
|
dataLength = CFSwapInt32LittleToHost(header->dataLength);
|
|
|
|
|
|
|
|
bytes = ((uint8_t *)[data bytes]) + sizeof(PVRTexHeader);
|
|
|
|
|
|
|
|
// Calculate the data size for each texture level and respect the minimum number of blocks
|
|
|
|
while (dataOffset < dataLength)
|
|
|
|
{
|
|
|
|
if (formatFlags == kPVRTextureFlagTypePVRTC_4)
|
|
|
|
{
|
|
|
|
blockSize = 4 * 4; // Pixel by pixel block size for 4bpp
|
|
|
|
widthBlocks = width / 4;
|
|
|
|
heightBlocks = height / 4;
|
|
|
|
bpp = 4;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
blockSize = 8 * 4; // Pixel by pixel block size for 2bpp
|
|
|
|
widthBlocks = width / 8;
|
|
|
|
heightBlocks = height / 4;
|
|
|
|
bpp = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clamp to minimum number of blocks
|
|
|
|
if (widthBlocks < 2)
|
|
|
|
widthBlocks = 2;
|
|
|
|
if (heightBlocks < 2)
|
|
|
|
heightBlocks = 2;
|
|
|
|
|
|
|
|
dataSize = widthBlocks * heightBlocks * ((blockSize * bpp) / 8);
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
[_imageData addObject:[CCData dataWithBytes:bytes+dataOffset length:dataSize]];
|
2010-07-16 10:34:42 +08:00
|
|
|
|
|
|
|
dataOffset += dataSize;
|
|
|
|
|
|
|
|
width = MAX(width >> 1, 1);
|
|
|
|
height = MAX(height >> 1, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
success = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
|
|
bool CCPVRTexture::createGLTexture()
|
|
|
|
{
|
|
|
|
int width = m_uWidth;
|
|
|
|
int height = m_uHeight;
|
2011-03-07 17:11:57 +08:00
|
|
|
/// @todo CCData *data;
|
2010-07-16 10:34:42 +08:00
|
|
|
GLenum err;
|
|
|
|
|
|
|
|
if (m_pImageData->count() > 0)
|
|
|
|
{
|
|
|
|
if (m_uName != 0)
|
|
|
|
glDeleteTextures(1, &m_uName);
|
|
|
|
|
|
|
|
glGenTextures(1, &m_uName);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, m_uName);
|
|
|
|
}
|
|
|
|
|
2010-08-02 10:58:00 +08:00
|
|
|
for (unsigned int i=0; i < m_pImageData->count(); i++)
|
2010-07-16 10:34:42 +08:00
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
/// @todo CCData data = m_pImageData->getObjectAtIndex(i);
|
|
|
|
/// @todo CCData glCompressedTexImage2D(GL_TEXTURE_2D, i, m_uInternalFormat, width, height, 0, [data length], [data bytes]);
|
2010-07-16 10:34:42 +08:00
|
|
|
|
|
|
|
err = glGetError();
|
|
|
|
if (err != GL_NO_ERROR)
|
|
|
|
{
|
|
|
|
CCLOG("Error uploading compressed texture level: %u. glError: %u", i, err);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
width = MAX(width >> 1, 1);
|
|
|
|
height = MAX(height >> 1, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_pImageData->removeAllObjects();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-22 11:15:25 +08:00
|
|
|
CCPVRTexture * CCPVRTexture::initWithContentsOfFile(const char* path)
|
2010-07-16 10:34:42 +08:00
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
/** @todo CCData*/
|
|
|
|
CCData *data = CCData::dataWithContentsOfFile(path);
|
2010-07-28 13:48:13 +08:00
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
m_pImageData = new CCMutableArray<CCData*>(10);
|
2010-07-28 13:48:13 +08:00
|
|
|
|
|
|
|
m_uName = 0;
|
|
|
|
m_uWidth = m_uHeight = 0;
|
|
|
|
m_uInternalFormat = GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG;
|
|
|
|
m_bHasAlpha = false;
|
|
|
|
|
|
|
|
m_bRetainName = false; // cocos2d integration
|
|
|
|
/// @todo
|
2010-08-02 10:58:00 +08:00
|
|
|
// if (!data || ![self unpackPVRData:data] || ![self createGLTexture])
|
|
|
|
// {
|
|
|
|
// [self release];
|
|
|
|
// self = nil;
|
|
|
|
// }
|
2010-07-16 10:34:42 +08:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @todo NSURL
|
|
|
|
- (id)initWithContentsOfURL:(NSURL *)url
|
|
|
|
{
|
|
|
|
if (![url isFileURL])
|
|
|
|
{
|
|
|
|
CCLOG(@"cocos2d: CCPVRTexture: Only files are supported");
|
|
|
|
[self release];
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [self initWithContentsOfFile:[url path]];
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
2010-07-22 11:15:25 +08:00
|
|
|
CCPVRTexture * CCPVRTexture::pvrTextureWithContentsOfFile(const char* path)
|
2010-07-16 10:34:42 +08:00
|
|
|
{
|
|
|
|
CCPVRTexture * pTexture = new CCPVRTexture();
|
|
|
|
pTexture->initWithContentsOfFile(path);
|
|
|
|
pTexture->autorelease();
|
|
|
|
return pTexture;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @todo NSURL
|
|
|
|
+ (id)pvrTextureWithContentsOfURL:(NSURL *)url
|
|
|
|
{
|
|
|
|
if (![url isFileURL])
|
|
|
|
return nil;
|
|
|
|
|
|
|
|
return [CCPVRTexture pvrTextureWithContentsOfFile:[url path]];
|
|
|
|
}*/
|
|
|
|
|
2011-01-17 21:16:25 +08:00
|
|
|
}//namespace cocos2d
|
2010-07-16 10:34:42 +08:00
|
|
|
|
2011-01-17 21:16:25 +08:00
|
|
|
#endif
|