axmol/cocos/2d/CCTileMapAtlas.cpp

255 lines
8.4 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2011 Zynga Inc.
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
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 "2d/CCTileMapAtlas.h"
#include "platform/CCFileUtils.h"
#include "renderer/CCTextureAtlas.h"
#include "base/TGAlib.h"
#include "base/CCDirector.h"
#include "base/ccUTF8.h"
NS_CC_BEGIN
// implementation TileMapAtlas
2021-12-26 23:26:34 +08:00
TileMapAtlas* TileMapAtlas::create(std::string_view tile, std::string_view mapFile, int tileWidth, int tileHeight)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
TileMapAtlas* ret = new TileMapAtlas();
2019-11-23 20:27:39 +08:00
if (ret->initWithTileFile(tile, mapFile, tileWidth, tileHeight))
{
ret->autorelease();
return ret;
}
CC_SAFE_DELETE(ret);
return nullptr;
}
2021-12-26 23:26:34 +08:00
bool TileMapAtlas::initWithTileFile(std::string_view tile, std::string_view mapFile, int tileWidth, int tileHeight)
2019-11-23 20:27:39 +08:00
{
this->loadTGAfile(mapFile);
this->calculateItemsToRender();
2021-12-25 10:04:45 +08:00
if (AtlasNode::initWithTileFile(tile, tileWidth, tileHeight, _itemsToRender))
2019-11-23 20:27:39 +08:00
{
this->updateAtlasValues();
2021-12-25 10:04:45 +08:00
this->setContentSize(Vec2((float)(_TGAInfo->width * _itemWidth), (float)(_TGAInfo->height * _itemHeight)));
2019-11-23 20:27:39 +08:00
return true;
}
return false;
}
2021-12-25 10:04:45 +08:00
TileMapAtlas::TileMapAtlas() : _itemsToRender(0), _TGAInfo(nullptr) {}
2019-11-23 20:27:39 +08:00
TileMapAtlas::~TileMapAtlas()
{
if (_TGAInfo)
{
tgaDestroy(_TGAInfo);
}
}
void TileMapAtlas::releaseMap()
{
if (_TGAInfo)
{
tgaDestroy(_TGAInfo);
}
_TGAInfo = nullptr;
}
void TileMapAtlas::calculateItemsToRender()
{
2021-12-25 10:04:45 +08:00
CCASSERT(_TGAInfo != nullptr, "tgaInfo must be non-nil");
2019-11-23 20:27:39 +08:00
_itemsToRender = 0;
2021-12-25 10:04:45 +08:00
for (int x = 0; x < _TGAInfo->width; x++)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
for (int y = 0; y < _TGAInfo->height; y++)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
Color3B* ptr = (Color3B*)_TGAInfo->imageData;
2019-11-23 20:27:39 +08:00
Color3B value = ptr[x + y * _TGAInfo->width];
2021-12-25 10:04:45 +08:00
if (value.r)
2019-11-23 20:27:39 +08:00
{
++_itemsToRender;
}
}
}
}
2021-12-26 23:26:34 +08:00
void TileMapAtlas::loadTGAfile(std::string_view file)
2019-11-23 20:27:39 +08:00
{
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(file);
// //Find the path of the file
// NSBundle *mainBndl = [Director sharedDirector].loadingBundle;
// String *resourcePath = [mainBndl resourcePath];
// String * path = [resourcePath stringByAppendingPathComponent:file];
2021-12-25 10:04:45 +08:00
_TGAInfo = tgaLoad(fullPath.c_str());
2019-11-23 20:27:39 +08:00
#if 1
2021-12-25 10:04:45 +08:00
if (_TGAInfo->status != TGA_OK)
2019-11-23 20:27:39 +08:00
{
CCASSERT(0, "TileMapAtlasLoadTGA : TileMapAtlas cannot load TGA file");
}
#endif
}
// TileMapAtlas - Atlas generation / updates
void TileMapAtlas::setTile(const Color3B& tile, const Vec2& position)
{
CCASSERT(_TGAInfo != nullptr, "tgaInfo 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");
2021-12-25 10:04:45 +08:00
Color3B* ptr = (Color3B*)_TGAInfo->imageData;
2019-11-23 20:27:39 +08:00
Color3B value = ptr[(unsigned int)(position.x + position.y * _TGAInfo->width)];
2021-12-25 10:04:45 +08:00
if (value.r == 0)
2019-11-23 20:27:39 +08:00
{
CCLOG("cocos2d: Value.r must be non 0.");
2021-12-25 10:04:45 +08:00
}
2019-11-23 20:27:39 +08:00
else
{
ptr[(unsigned int)(position.x + position.y * _TGAInfo->width)] = tile;
// FIXME:: this method consumes a lot of memory
// FIXME:: a tree of something like that shall be implemented
std::string key = StringUtils::toString(position.x) + "," + StringUtils::toString(position.y);
2021-12-25 10:04:45 +08:00
int num = _posToAtlasIndex[key].asInt();
2019-11-23 20:27:39 +08:00
this->updateAtlasValueAt(position, tile, num);
2021-12-25 10:04:45 +08:00
}
2019-11-23 20:27:39 +08:00
}
Color3B TileMapAtlas::getTileAt(const Vec2& position) const
{
2021-12-25 10:04:45 +08:00
CCASSERT(_TGAInfo != nullptr, "tgaInfo must not be nil");
CCASSERT(position.x < _TGAInfo->width, "Invalid position.x");
CCASSERT(position.y < _TGAInfo->height, "Invalid position.y");
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
Color3B* ptr = (Color3B*)_TGAInfo->imageData;
2019-11-23 20:27:39 +08:00
Color3B value = ptr[(unsigned int)(position.x + position.y * _TGAInfo->width)];
2021-12-25 10:04:45 +08:00
return value;
2019-11-23 20:27:39 +08:00
}
void TileMapAtlas::updateAtlasValueAt(const Vec2& pos, const Color3B& value, int index)
{
2021-12-25 10:04:45 +08:00
CCASSERT(index >= 0 && index < _textureAtlas->getCapacity(), "updateAtlasValueAt: Invalid index");
2019-11-23 20:27:39 +08:00
V3F_C4B_T2F_Quad* quad = &((_textureAtlas->getQuads())[index]);
2021-12-25 10:04:45 +08:00
int x = pos.x;
int y = pos.y;
float row = (float)(value.r % _itemsPerRow);
float col = (float)(value.r / _itemsPerRow);
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
float textureWide = (float)(_textureAtlas->getTexture()->getPixelsWide());
float textureHigh = (float)(_textureAtlas->getTexture()->getPixelsHigh());
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
float itemWidthInPixels = _itemWidth * CC_CONTENT_SCALE_FACTOR();
2019-11-23 20:27:39 +08:00
float itemHeightInPixels = _itemHeight * CC_CONTENT_SCALE_FACTOR();
#if CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
2021-12-25 10:04:45 +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);
2019-11-23 20:27:39 +08:00
#else
2021-12-25 10:04:45 +08:00
float left = (row * itemWidthInPixels) / textureWide;
float right = left + itemWidthInPixels / textureWide;
float top = (col * itemHeightInPixels) / textureHigh;
float bottom = top + itemHeightInPixels / textureHigh;
2019-11-23 20:27:39 +08:00
#endif
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;
2021-12-25 10:04:45 +08:00
quad->bl.vertices.x = (float)(x * _itemWidth);
quad->bl.vertices.y = (float)(y * _itemHeight);
2019-11-23 20:27:39 +08:00
quad->bl.vertices.z = 0.0f;
quad->br.vertices.x = (float)(x * _itemWidth + _itemWidth);
quad->br.vertices.y = (float)(y * _itemHeight);
quad->br.vertices.z = 0.0f;
quad->tl.vertices.x = (float)(x * _itemWidth);
quad->tl.vertices.y = (float)(y * _itemHeight + _itemHeight);
quad->tl.vertices.z = 0.0f;
quad->tr.vertices.x = (float)(x * _itemWidth + _itemWidth);
quad->tr.vertices.y = (float)(y * _itemHeight + _itemHeight);
quad->tr.vertices.z = 0.0f;
Color4B color(_displayedColor.r, _displayedColor.g, _displayedColor.b, _displayedOpacity);
quad->tr.colors = color;
quad->tl.colors = color;
quad->br.colors = color;
quad->bl.colors = color;
_textureAtlas->setDirty(true);
ssize_t totalQuads = _textureAtlas->getTotalQuads();
2021-12-25 10:04:45 +08:00
if (index + 1 > totalQuads)
{
2019-11-23 20:27:39 +08:00
_textureAtlas->increaseTotalQuadsWith(index + 1 - totalQuads);
}
}
void TileMapAtlas::updateAtlasValues()
{
2021-12-25 10:04:45 +08:00
CCASSERT(_TGAInfo != nullptr, "tgaInfo must be non-nil");
2019-11-23 20:27:39 +08:00
int total = 0;
2021-12-25 10:04:45 +08:00
for (int x = 0; x < _TGAInfo->width; x++)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
for (int y = 0; y < _TGAInfo->height; y++)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (total < _itemsToRender)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
Color3B* ptr = (Color3B*)_TGAInfo->imageData;
2019-11-23 20:27:39 +08:00
Color3B value = ptr[x + y * _TGAInfo->width];
2021-12-25 10:04:45 +08:00
if (value.r != 0)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
this->updateAtlasValueAt(Vec2(x, y), value, total);
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
std::string key = StringUtils::toString(x) + "," + StringUtils::toString(y);
2019-11-23 20:27:39 +08:00
_posToAtlasIndex[key] = total;
total++;
}
}
}
}
}
NS_CC_END