2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
2012-09-24 21:22:20 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2012-04-19 14:35:52 +08:00
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
|
|
|
Copyright (c) 2011 Zynga Inc.
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
#include "CCTileMapAtlas.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
#include "textures/CCTextureAtlas.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
#include "support/image_support/TGAlib.h"
|
|
|
|
#include "ccConfig.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "cocoa/CCDictionary.h"
|
|
|
|
#include "cocoa/CCInteger.h"
|
2012-06-11 14:44:32 +08:00
|
|
|
#include "CCDirector.h"
|
2012-12-26 18:59:31 +08:00
|
|
|
#include "support/CCPointExtension.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
NS_CC_BEGIN
|
2010-08-30 15:23:07 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
// implementation CCTileMapAtlas
|
2010-08-30 15:23:07 +08:00
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
CCTileMapAtlas * CCTileMapAtlas::create(const char *tile, const char *mapFile, int tileWidth, int tileHeight)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCTileMapAtlas *pRet = new CCTileMapAtlas();
|
|
|
|
if (pRet->initWithTileFile(tile, mapFile, tileWidth, tileHeight))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
return NULL;
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2012-06-11 14:44:32 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
bool CCTileMapAtlas::initWithTileFile(const char *tile, const char *mapFile, int tileWidth, int tileHeight)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
this->loadTGAfile(mapFile);
|
|
|
|
this->calculateItemsToRender();
|
2012-06-11 14:44:32 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if( CCAtlasNode::initWithTileFile(tile, tileWidth, tileHeight, m_nItemsToRender) )
|
|
|
|
{
|
2012-06-11 14:44:32 +08:00
|
|
|
m_pPosToAtlasIndex = new CCDictionary();
|
2012-04-19 14:35:52 +08:00
|
|
|
this->updateAtlasValues();
|
|
|
|
this->setContentSize(CCSizeMake((float)(m_pTGAInfo->width*m_uItemWidth),
|
|
|
|
(float)(m_pTGAInfo->height*m_uItemHeight)));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2012-06-11 14:44:32 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
CCTileMapAtlas::CCTileMapAtlas()
|
|
|
|
:m_pTGAInfo(NULL)
|
2012-04-19 14:35:52 +08:00
|
|
|
,m_pPosToAtlasIndex(NULL)
|
|
|
|
,m_nItemsToRender(0)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
|
|
|
}
|
2012-06-11 14:44:32 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
CCTileMapAtlas::~CCTileMapAtlas()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if (m_pTGAInfo)
|
|
|
|
{
|
|
|
|
tgaDestroy(m_pTGAInfo);
|
|
|
|
}
|
2012-06-11 14:44:32 +08:00
|
|
|
CC_SAFE_RELEASE(m_pPosToAtlasIndex);
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2012-06-11 14:44:32 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
void CCTileMapAtlas::releaseMap()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if (m_pTGAInfo)
|
|
|
|
{
|
|
|
|
tgaDestroy(m_pTGAInfo);
|
|
|
|
}
|
|
|
|
m_pTGAInfo = NULL;
|
|
|
|
|
2012-06-11 14:44:32 +08:00
|
|
|
CC_SAFE_RELEASE_NULL(m_pPosToAtlasIndex);
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2012-06-11 14:44:32 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
void CCTileMapAtlas::calculateItemsToRender()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert( m_pTGAInfo != NULL, "tgaInfo must be non-nil");
|
|
|
|
|
|
|
|
m_nItemsToRender = 0;
|
|
|
|
for(int x=0;x < m_pTGAInfo->width; x++ )
|
|
|
|
{
|
|
|
|
for( int y=0; y < m_pTGAInfo->height; y++ )
|
|
|
|
{
|
|
|
|
ccColor3B *ptr = (ccColor3B*) m_pTGAInfo->imageData;
|
|
|
|
ccColor3B value = ptr[x + y * m_pTGAInfo->width];
|
|
|
|
if( value.r )
|
|
|
|
{
|
|
|
|
++m_nItemsToRender;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2012-06-11 14:44:32 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
void CCTileMapAtlas::loadTGAfile(const char *file)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert( file != NULL, "file must be non-nil");
|
2010-08-30 15:23:07 +08:00
|
|
|
|
2013-01-25 20:51:52 +08:00
|
|
|
std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(file);
|
2012-06-08 14:11:48 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// //Find the path of the file
|
|
|
|
// NSBundle *mainBndl = [CCDirector sharedDirector].loadingBundle;
|
|
|
|
// CCString *resourcePath = [mainBndl resourcePath];
|
|
|
|
// CCString * path = [resourcePath stringByAppendingPathComponent:file];
|
2010-08-30 15:23:07 +08:00
|
|
|
|
2013-01-25 20:51:52 +08:00
|
|
|
m_pTGAInfo = tgaLoad( fullPath.c_str() );
|
2012-04-18 18:43:45 +08:00
|
|
|
#if 1
|
2012-04-19 14:35:52 +08:00
|
|
|
if( m_pTGAInfo->status != TGA_OK )
|
|
|
|
{
|
2012-09-17 15:02:24 +08:00
|
|
|
CCAssert(0, "TileMapAtlasLoadTGA : TileMapAtlas cannot load TGA file");
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
#endif
|
|
|
|
}
|
2010-08-30 15:23:07 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
// CCTileMapAtlas - Atlas generation / updates
|
2012-12-26 18:59:31 +08:00
|
|
|
void CCTileMapAtlas::setTile(const ccColor3B& tile, const CCPoint& position)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-02-26 18:08:51 +08:00
|
|
|
CCAssert(m_pTGAInfo != NULL, "tgaInfo must not be nil");
|
|
|
|
CCAssert(m_pPosToAtlasIndex != NULL, "posToAtlasIndex must not be nil");
|
|
|
|
CCAssert(position.x < m_pTGAInfo->width, "Invalid position.x");
|
|
|
|
CCAssert(position.y < m_pTGAInfo->height, "Invalid position.x");
|
|
|
|
CCAssert(tile.r != 0, "R component must be non 0");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
ccColor3B *ptr = (ccColor3B*)m_pTGAInfo->imageData;
|
2012-12-26 18:59:31 +08:00
|
|
|
ccColor3B value = ptr[(unsigned int)(position.x + position.y * m_pTGAInfo->width)];
|
2012-04-19 14:35:52 +08:00
|
|
|
if( value.r == 0 )
|
|
|
|
{
|
|
|
|
CCLOG("cocos2d: Value.r must be non 0.");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-12-26 18:59:31 +08:00
|
|
|
ptr[(unsigned int)(position.x + position.y * m_pTGAInfo->width)] = tile;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-06-19 16:20:46 +08:00
|
|
|
// XXX: this method consumes a lot of memory
|
2012-09-17 15:02:24 +08:00
|
|
|
// XXX: a tree of something like that shall be implemented
|
2012-08-22 15:51:25 +08:00
|
|
|
CCInteger *num = (CCInteger*)m_pPosToAtlasIndex->objectForKey(CCString::createWithFormat("%ld,%ld",
|
|
|
|
(long)position.x,
|
|
|
|
(long)position.y)->getCString());
|
2012-06-11 14:44:32 +08:00
|
|
|
this->updateAtlasValueAt(position, tile, num->getValue());
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2012-06-11 14:44:32 +08:00
|
|
|
|
2012-12-26 18:59:31 +08:00
|
|
|
ccColor3B CCTileMapAtlas::tileAt(const CCPoint& position)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert( m_pTGAInfo != NULL, "tgaInfo must not be nil");
|
|
|
|
CCAssert( position.x < m_pTGAInfo->width, "Invalid position.x");
|
|
|
|
CCAssert( position.y < m_pTGAInfo->height, "Invalid position.y");
|
2010-08-30 15:23:07 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
ccColor3B *ptr = (ccColor3B*)m_pTGAInfo->imageData;
|
2012-12-26 18:59:31 +08:00
|
|
|
ccColor3B value = ptr[(unsigned int)(position.x + position.y * m_pTGAInfo->width)];
|
2010-08-30 15:23:07 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return value;
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2012-06-11 14:44:32 +08:00
|
|
|
|
2012-12-26 18:59:31 +08:00
|
|
|
void CCTileMapAtlas::updateAtlasValueAt(const CCPoint& pos, const ccColor3B& value, unsigned int index)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-05-29 19:18:46 +08:00
|
|
|
unsigned int capacity = m_pTextureAtlas->getCapacity();
|
|
|
|
CCAssert( index >= 0 && index < capacity, "updateAtlasValueAt: Invalid index");
|
|
|
|
|
2013-05-28 10:23:47 +08:00
|
|
|
ccV3F_C4B_T2F_Quad* quad = &((m_pTextureAtlas->getQuads())[index]);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
int x = pos.x;
|
|
|
|
int y = pos.y;
|
|
|
|
float row = (float) (value.r % m_uItemsPerRow);
|
|
|
|
float col = (float) (value.r / m_uItemsPerRow);
|
|
|
|
|
|
|
|
float textureWide = (float) (m_pTextureAtlas->getTexture()->getPixelsWide());
|
|
|
|
float textureHigh = (float) (m_pTextureAtlas->getTexture()->getPixelsHigh());
|
|
|
|
|
2012-06-19 16:20:46 +08:00
|
|
|
float itemWidthInPixels = m_uItemWidth * CC_CONTENT_SCALE_FACTOR();
|
2012-06-11 14:44:32 +08:00
|
|
|
float itemHeightInPixels = m_uItemHeight * CC_CONTENT_SCALE_FACTOR();
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
#if CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
|
2012-06-11 14:44:32 +08:00
|
|
|
float left = (2 * row * itemWidthInPixels + 1) / (2 * textureWide);
|
|
|
|
float right = left + (itemWidthInPixels * 2 - 2) / (2 * textureWide);
|
|
|
|
float top = (2 * col * itemHeightInPixels + 1) / (2 * textureHigh);
|
|
|
|
float bottom = top + (itemHeightInPixels * 2 - 2) / (2 * textureHigh);
|
2012-04-19 14:35:52 +08:00
|
|
|
#else
|
2012-06-11 14:44:32 +08:00
|
|
|
float left = (row * itemWidthInPixels) / textureWide;
|
|
|
|
float right = left + itemWidthInPixels / textureWide;
|
|
|
|
float top = (col * itemHeightInPixels) / textureHigh;
|
|
|
|
float bottom = top + itemHeightInPixels / textureHigh;
|
2012-04-19 14:35:52 +08:00
|
|
|
#endif
|
|
|
|
|
2013-05-28 10:23:47 +08:00
|
|
|
quad->tl.texCoords.u = left;
|
|
|
|
quad->tl.texCoords.v = top;
|
|
|
|
quad->tr.texCoords.u = right;
|
|
|
|
quad->tr.texCoords.v = top;
|
|
|
|
quad->bl.texCoords.u = left;
|
|
|
|
quad->bl.texCoords.v = bottom;
|
|
|
|
quad->br.texCoords.u = right;
|
|
|
|
quad->br.texCoords.v = bottom;
|
|
|
|
|
|
|
|
quad->bl.vertices.x = (float) (x * m_uItemWidth);
|
|
|
|
quad->bl.vertices.y = (float) (y * m_uItemHeight);
|
|
|
|
quad->bl.vertices.z = 0.0f;
|
|
|
|
quad->br.vertices.x = (float)(x * m_uItemWidth + m_uItemWidth);
|
|
|
|
quad->br.vertices.y = (float)(y * m_uItemHeight);
|
|
|
|
quad->br.vertices.z = 0.0f;
|
|
|
|
quad->tl.vertices.x = (float)(x * m_uItemWidth);
|
|
|
|
quad->tl.vertices.y = (float)(y * m_uItemHeight + m_uItemHeight);
|
|
|
|
quad->tl.vertices.z = 0.0f;
|
|
|
|
quad->tr.vertices.x = (float)(x * m_uItemWidth + m_uItemWidth);
|
|
|
|
quad->tr.vertices.y = (float)(y * m_uItemHeight + m_uItemHeight);
|
|
|
|
quad->tr.vertices.z = 0.0f;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-02-28 11:55:36 +08:00
|
|
|
ccColor4B color = { _displayedColor.r, _displayedColor.g, _displayedColor.b, _displayedOpacity };
|
2013-05-28 10:23:47 +08:00
|
|
|
quad->tr.colors = color;
|
|
|
|
quad->tl.colors = color;
|
|
|
|
quad->br.colors = color;
|
|
|
|
quad->bl.colors = color;
|
|
|
|
|
|
|
|
m_pTextureAtlas->setDirty(true);
|
|
|
|
unsigned int totalQuads = m_pTextureAtlas->getTotalQuads();
|
|
|
|
if (index + 1 > totalQuads) {
|
|
|
|
m_pTextureAtlas->increaseTotalQuadsWith(index + 1 - totalQuads);
|
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2012-06-11 14:44:32 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
void CCTileMapAtlas::updateAtlasValues()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert( m_pTGAInfo != NULL, "tgaInfo must be non-nil");
|
|
|
|
|
|
|
|
int total = 0;
|
|
|
|
|
|
|
|
for(int x=0;x < m_pTGAInfo->width; x++ )
|
|
|
|
{
|
|
|
|
for( int y=0; y < m_pTGAInfo->height; y++ )
|
|
|
|
{
|
|
|
|
if( total < m_nItemsToRender )
|
|
|
|
{
|
|
|
|
ccColor3B *ptr = (ccColor3B*) m_pTGAInfo->imageData;
|
|
|
|
ccColor3B value = ptr[x + y * m_pTGAInfo->width];
|
|
|
|
|
|
|
|
if( value.r != 0 )
|
|
|
|
{
|
2012-12-26 18:59:31 +08:00
|
|
|
this->updateAtlasValueAt(ccp(x,y), value, total);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-06-19 16:20:46 +08:00
|
|
|
CCString *key = CCString::createWithFormat("%d,%d", x,y);
|
|
|
|
CCInteger *num = CCInteger::create(total);
|
|
|
|
m_pPosToAtlasIndex->setObject(num, key->getCString());
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
total++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2012-06-11 14:44:32 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
void CCTileMapAtlas::setTGAInfo(struct sImageTGA* var)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_pTGAInfo = var;
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2012-06-11 14:44:32 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
struct sImageTGA * CCTileMapAtlas::getTGAInfo()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_pTGAInfo;
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-08-30 15:23:07 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
NS_CC_END
|