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"
|
2013-10-14 14:01:00 +08:00
|
|
|
#include "CCTextureAtlas.h"
|
|
|
|
#include "TGAlib.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
#include "ccConfig.h"
|
2013-10-14 14:01:00 +08:00
|
|
|
#include "CCDictionary.h"
|
|
|
|
#include "CCInteger.h"
|
2012-06-11 14:44:32 +08:00
|
|
|
#include "CCDirector.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
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// implementation TileMapAtlas
|
2010-08-30 15:23:07 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
TileMapAtlas * TileMapAtlas::create(const char *tile, const char *mapFile, int tileWidth, int tileHeight)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
TileMapAtlas *pRet = new TileMapAtlas();
|
2012-04-19 14:35:52 +08:00
|
|
|
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
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool TileMapAtlas::initWithTileFile(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
|
|
|
this->loadTGAfile(mapFile);
|
|
|
|
this->calculateItemsToRender();
|
2012-06-11 14:44:32 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
if( AtlasNode::initWithTileFile(tile, tileWidth, tileHeight, _itemsToRender) )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
_posToAtlasIndex = new Dictionary();
|
2013-08-22 11:12:09 +08:00
|
|
|
_posToAtlasIndex->init();
|
2012-04-19 14:35:52 +08:00
|
|
|
this->updateAtlasValues();
|
2013-07-12 14:30:26 +08:00
|
|
|
this->setContentSize(Size((float)(_TGAInfo->width*_itemWidth),
|
2013-06-15 14:03:30 +08:00
|
|
|
(float)(_TGAInfo->height*_itemHeight)));
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2012-06-11 14:44:32 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
TileMapAtlas::TileMapAtlas()
|
2013-07-29 14:07:57 +08:00
|
|
|
: _posToAtlasIndex(NULL)
|
|
|
|
, _itemsToRender(0)
|
|
|
|
, _TGAInfo(NULL)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
|
|
|
}
|
2012-06-11 14:44:32 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
TileMapAtlas::~TileMapAtlas()
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_TGAInfo)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
tgaDestroy(_TGAInfo);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_posToAtlasIndex);
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2012-06-11 14:44:32 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TileMapAtlas::releaseMap()
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_TGAInfo)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
tgaDestroy(_TGAInfo);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
_TGAInfo = NULL;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE_NULL(_posToAtlasIndex);
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2012-06-11 14:44:32 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TileMapAtlas::calculateItemsToRender()
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT( _TGAInfo != NULL, "tgaInfo must be non-nil");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_itemsToRender = 0;
|
|
|
|
for(int x=0;x < _TGAInfo->width; x++ )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
for( int y=0; y < _TGAInfo->height; y++ )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-05 16:49:22 +08:00
|
|
|
Color3B *ptr = (Color3B*) _TGAInfo->imageData;
|
|
|
|
Color3B value = ptr[x + y * _TGAInfo->width];
|
2012-04-19 14:35:52 +08:00
|
|
|
if( value.r )
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
++_itemsToRender;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2012-06-11 14:44:32 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TileMapAtlas::loadTGAfile(const char *file)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT( file != NULL, "file must be non-nil");
|
2010-08-30 15:23:07 +08:00
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(file);
|
2012-06-08 14:11:48 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// //Find the path of the file
|
2013-06-20 14:13:12 +08:00
|
|
|
// NSBundle *mainBndl = [Director sharedDirector].loadingBundle;
|
|
|
|
// String *resourcePath = [mainBndl resourcePath];
|
|
|
|
// String * path = [resourcePath stringByAppendingPathComponent:file];
|
2010-08-30 15:23:07 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_TGAInfo = tgaLoad( fullPath.c_str() );
|
2012-04-18 18:43:45 +08:00
|
|
|
#if 1
|
2013-06-15 14:03:30 +08:00
|
|
|
if( _TGAInfo->status != TGA_OK )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +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
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// TileMapAtlas - Atlas generation / updates
|
2013-07-05 16:49:22 +08:00
|
|
|
void TileMapAtlas::setTile(const Color3B& tile, const Point& position)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(_TGAInfo != NULL, "tgaInfo must not be nil");
|
|
|
|
CCASSERT(_posToAtlasIndex != NULL, "posToAtlasIndex must not be nil");
|
|
|
|
CCASSERT(position.x < _TGAInfo->width, "Invalid position.x");
|
|
|
|
CCASSERT(position.y < _TGAInfo->height, "Invalid position.x");
|
|
|
|
CCASSERT(tile.r != 0, "R component must be non 0");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
Color3B *ptr = (Color3B*)_TGAInfo->imageData;
|
|
|
|
Color3B value = ptr[(unsigned int)(position.x + position.y * _TGAInfo->width)];
|
2012-04-19 14:35:52 +08:00
|
|
|
if( value.r == 0 )
|
|
|
|
{
|
|
|
|
CCLOG("cocos2d: Value.r must be non 0.");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
ptr[(unsigned int)(position.x + position.y * _TGAInfo->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
|
2013-06-20 14:13:12 +08:00
|
|
|
Integer *num = (Integer*)_posToAtlasIndex->objectForKey(String::createWithFormat("%ld,%ld",
|
2012-08-22 15:51:25 +08:00
|
|
|
(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
|
|
|
|
2013-07-19 14:57:54 +08:00
|
|
|
Color3B TileMapAtlas::getTileAt(const Point& position) const
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT( _TGAInfo != NULL, "tgaInfo must not be nil");
|
|
|
|
CCASSERT( position.x < _TGAInfo->width, "Invalid position.x");
|
|
|
|
CCASSERT( position.y < _TGAInfo->height, "Invalid position.y");
|
2010-08-30 15:23:07 +08:00
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
Color3B *ptr = (Color3B*)_TGAInfo->imageData;
|
|
|
|
Color3B value = ptr[(unsigned int)(position.x + position.y * _TGAInfo->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
|
|
|
|
2013-07-22 14:28:19 +08:00
|
|
|
void TileMapAtlas::updateAtlasValueAt(const Point& pos, const Color3B& value, int index)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT( index >= 0 && index < _textureAtlas->getCapacity(), "updateAtlasValueAt: Invalid index");
|
2013-05-29 19:18:46 +08:00
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
V3F_C4B_T2F_Quad* quad = &((_textureAtlas->getQuads())[index]);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
int x = pos.x;
|
|
|
|
int y = pos.y;
|
2013-06-15 14:03:30 +08:00
|
|
|
float row = (float) (value.r % _itemsPerRow);
|
|
|
|
float col = (float) (value.r / _itemsPerRow);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
float textureWide = (float) (_textureAtlas->getTexture()->getPixelsWide());
|
|
|
|
float textureHigh = (float) (_textureAtlas->getTexture()->getPixelsHigh());
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
float itemWidthInPixels = _itemWidth * CC_CONTENT_SCALE_FACTOR();
|
|
|
|
float itemHeightInPixels = _itemHeight * CC_CONTENT_SCALE_FACTOR();
|
2012-06-11 14:44:32 +08:00
|
|
|
|
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;
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
quad->bl.vertices.x = (float) (x * _itemWidth);
|
|
|
|
quad->bl.vertices.y = (float) (y * _itemHeight);
|
2013-05-28 10:23:47 +08:00
|
|
|
quad->bl.vertices.z = 0.0f;
|
2013-06-15 14:03:30 +08:00
|
|
|
quad->br.vertices.x = (float)(x * _itemWidth + _itemWidth);
|
|
|
|
quad->br.vertices.y = (float)(y * _itemHeight);
|
2013-05-28 10:23:47 +08:00
|
|
|
quad->br.vertices.z = 0.0f;
|
2013-06-15 14:03:30 +08:00
|
|
|
quad->tl.vertices.x = (float)(x * _itemWidth);
|
|
|
|
quad->tl.vertices.y = (float)(y * _itemHeight + _itemHeight);
|
2013-05-28 10:23:47 +08:00
|
|
|
quad->tl.vertices.z = 0.0f;
|
2013-06-15 14:03:30 +08:00
|
|
|
quad->tr.vertices.x = (float)(x * _itemWidth + _itemWidth);
|
|
|
|
quad->tr.vertices.y = (float)(y * _itemHeight + _itemHeight);
|
2013-05-28 10:23:47 +08:00
|
|
|
quad->tr.vertices.z = 0.0f;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-07 21:08:14 +08:00
|
|
|
Color4B 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;
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_textureAtlas->setDirty(true);
|
2013-07-22 14:28:19 +08:00
|
|
|
int totalQuads = _textureAtlas->getTotalQuads();
|
2013-05-28 10:23:47 +08:00
|
|
|
if (index + 1 > totalQuads) {
|
2013-06-15 14:03:30 +08:00
|
|
|
_textureAtlas->increaseTotalQuadsWith(index + 1 - totalQuads);
|
2013-05-28 10:23:47 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2012-06-11 14:44:32 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TileMapAtlas::updateAtlasValues()
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT( _TGAInfo != NULL, "tgaInfo must be non-nil");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
int total = 0;
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
for(int x=0;x < _TGAInfo->width; x++ )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
for( int y=0; y < _TGAInfo->height; y++ )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if( total < _itemsToRender )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-05 16:49:22 +08:00
|
|
|
Color3B *ptr = (Color3B*) _TGAInfo->imageData;
|
|
|
|
Color3B value = ptr[x + y * _TGAInfo->width];
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
if( value.r != 0 )
|
|
|
|
{
|
2013-07-12 14:11:55 +08:00
|
|
|
this->updateAtlasValueAt(Point(x,y), value, total);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
String *key = String::createWithFormat("%d,%d", x,y);
|
|
|
|
Integer *num = Integer::create(total);
|
2013-06-15 14:03:30 +08:00
|
|
|
_posToAtlasIndex->setObject(num, key->getCString());
|
2012-06-19 16:20:46 +08:00
|
|
|
|
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
|
|
|
|
2010-08-30 15:23:07 +08:00
|
|
|
|
2013-06-03 21:17:03 +08:00
|
|
|
NS_CC_END
|