axmol/cocos/2d/CCFastTMXLayer.cpp

849 lines
28 KiB
C++
Raw Normal View History

2014-06-06 16:15:46 +08:00
/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2011 Zynga Inc.
Copyright (c) 2013-2014 Chukong Technologies Inc.
Copyright (c) 2011 HKASoftware
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.
****************************************************************************/
/*
Original rewrite of TMXLayer was based on HKTMXTiledMap by HKASoftware http://hkasoftware.com
Further info: http://www.cocos2d-iphone.org/forums/topic/hktmxtiledmap/
It was rewritten again, and only a small part of the original HK ideas/code remains in this implementation
*/
#include "CCFastTMXLayer.h"
2014-06-06 16:15:46 +08:00
#include "CCTMXXMLParser.h"
#include "CCFastTMXTiledMap.h"
#include "2d/CCSprite.h"
#include "renderer/CCTextureCache.h"
#include "renderer/CCGLProgramCache.h"
#include "renderer/CCGLProgram.h"
#include "base/CCDirector.h"
#include "base/CCConfiguration.h"
2014-06-06 16:15:46 +08:00
#include "renderer/CCRenderer.h"
#include "deprecated/CCString.h"
#include "renderer/CCGLProgramStateCache.h"
2014-06-06 16:15:46 +08:00
NS_CC_BEGIN
namespace
{
static const int MAX_QUADS_COUNT = 65536 / 6;
}
// FastTMXLayer - init & alloc & dealloc
FastTMXLayer * FastTMXLayer::create(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo)
2014-06-06 16:15:46 +08:00
{
FastTMXLayer *ret = new FastTMXLayer();
2014-06-06 16:15:46 +08:00
if (ret->initWithTilesetInfo(tilesetInfo, layerInfo, mapInfo))
{
ret->autorelease();
return ret;
}
return nullptr;
}
2014-06-25 10:06:37 +08:00
bool FastTMXLayer::initWithTilesetInfo(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo)
2014-06-06 16:15:46 +08:00
{
if( tilesetInfo )
{
_texture = Director::getInstance()->getTextureCache()->addImage(tilesetInfo->_sourceImage);
_texture->retain();
}
// layerInfo
_layerName = layerInfo->_name;
_layerSize = layerInfo->_layerSize;
_tiles = layerInfo->_tiles;
2014-06-26 20:45:08 +08:00
_quadsDirty = true;
2014-06-06 16:15:46 +08:00
setOpacity( layerInfo->_opacity );
setProperties(layerInfo->getProperties());
// tilesetInfo
_tileSet = tilesetInfo;
CC_SAFE_RETAIN(_tileSet);
// mapInfo
_mapTileSize = mapInfo->getTileSize();
_layerOrientation = mapInfo->getOrientation();
// offset (after layer orientation is set);
Vec2 offset = this->calculateLayerOffset(layerInfo->_offset);
2014-06-06 16:15:46 +08:00
this->setPosition(CC_POINT_PIXELS_TO_POINTS(offset));
this->setContentSize(CC_SIZE_PIXELS_TO_POINTS(Size(_layerSize.width * _mapTileSize.width, _layerSize.height * _mapTileSize.height)));
this->tileToNodeTransform();
// shader, and other stuff
setGLProgram(GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
_useAutomaticVertexZ = false;
_vertexZvalue = 0;
return true;
}
FastTMXLayer::FastTMXLayer()
: _layerName("")
, _layerSize(Size::ZERO)
, _mapTileSize(Size::ZERO)
, _tiles(nullptr)
, _tileSet(nullptr)
, _layerOrientation(FastTMXOrientationOrtho)
2014-06-06 16:15:46 +08:00
,_texture(nullptr)
, _vertexZvalue(0)
, _useAutomaticVertexZ(false)
, _dirty(true)
2014-06-26 20:45:08 +08:00
, _quadsDirty(true)
2014-06-25 11:08:47 +08:00
{
_buffersVBO[0] = _buffersVBO[1] = 0;
2014-06-25 11:08:47 +08:00
}
2014-06-06 16:15:46 +08:00
FastTMXLayer::~FastTMXLayer()
2014-06-06 16:15:46 +08:00
{
CC_SAFE_RELEASE(_tileSet);
CC_SAFE_RELEASE(_texture);
CC_SAFE_DELETE_ARRAY(_tiles);
if(glIsBuffer(_buffersVBO[0]))
{
glDeleteBuffers(1, &_buffersVBO[0]);
}
if(glIsBuffer(_buffersVBO[1]))
{
glDeleteBuffers(1, &_buffersVBO[1]);
}
2014-06-06 16:15:46 +08:00
}
void FastTMXLayer::draw(Renderer *renderer, const Mat4& transform, uint32_t flags)
2014-06-06 16:15:46 +08:00
{
2014-06-26 20:45:08 +08:00
updateTotalQuads();
2014-06-27 11:50:44 +08:00
if( flags != 0 || _dirty || _quadsDirty )
{
2014-06-06 16:15:46 +08:00
Size s = Director::getInstance()->getWinSize();
2014-06-19 18:05:21 +08:00
auto rect = Rect(0, 0, s.width, s.height);
2014-06-06 16:15:46 +08:00
Mat4 inv = transform;
inv.inverse();
rect = RectApplyTransform(rect, inv);
2014-06-19 18:05:21 +08:00
2014-06-27 14:42:53 +08:00
updateTiles(rect);
2014-06-27 11:50:44 +08:00
updateIndexBuffer();
2014-06-06 18:03:44 +08:00
_dirty = false;
2014-06-06 16:15:46 +08:00
}
2014-06-27 11:50:44 +08:00
if(_renderCommands.size() < _indicesVertexZNumber.size())
2014-06-26 20:45:08 +08:00
{
2014-06-27 11:50:44 +08:00
_renderCommands.resize(_indicesVertexZNumber.size());
2014-06-26 20:45:08 +08:00
}
2014-06-24 11:04:17 +08:00
int index = 0;
2014-06-27 11:50:44 +08:00
for(const auto& iter : _indicesVertexZNumber)
{
2014-06-26 20:45:08 +08:00
auto& cmd = _renderCommands[index++];
cmd.init(iter.first);
2014-06-27 11:50:44 +08:00
cmd.func = CC_CALLBACK_0(FastTMXLayer::onDraw, this, _indicesVertexZOffsets[iter.first], iter.second);
2014-06-26 20:45:08 +08:00
renderer->addCommand(&cmd);
}
2014-06-26 20:45:08 +08:00
}
2014-06-27 11:50:44 +08:00
void FastTMXLayer::onDraw(int offset, int count)
2014-06-26 20:45:08 +08:00
{
GL::bindTexture2D(_texture->getName());
getGLProgramState()->apply(_modelViewTransform);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]);
2014-06-27 11:50:44 +08:00
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]);
2014-06-26 20:45:08 +08:00
GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX);
2014-06-27 11:50:44 +08:00
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B_T2F), (GLvoid*)0);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V3F_C4B_T2F), (GLvoid*)offsetof(V3F_C4B_T2F, colors));
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B_T2F), (GLvoid*)offsetof(V3F_C4B_T2F, texCoords));
glDrawElements(GL_TRIANGLES, (GLsizei)count * 6, GL_UNSIGNED_INT, (GLvoid*)(offset * 6 * sizeof(int)));
2014-06-27 11:50:44 +08:00
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
2014-06-27 14:42:53 +08:00
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, count * 4);
2014-06-06 16:15:46 +08:00
}
2014-06-27 14:42:53 +08:00
void FastTMXLayer::updateTiles(const Rect& culledRect)
2014-06-06 16:15:46 +08:00
{
Rect visibleTiles = culledRect;
Size mapTileSize = CC_SIZE_PIXELS_TO_POINTS(_mapTileSize);
Size tileSize = CC_SIZE_PIXELS_TO_POINTS(_tileSet->_tileSize);
2014-06-19 18:05:21 +08:00
Mat4 nodeToTileTransform = _tileToNodeTransform.getInversed();
2014-06-06 16:15:46 +08:00
//transform to tile
visibleTiles = RectApplyTransform(visibleTiles, nodeToTileTransform);
// tile coordinate is upside-down, so we need to make the tile coordinate use top-left for the start point.
visibleTiles.origin.y += 1;
2014-06-06 16:15:46 +08:00
// if x=0.7, width=9.5, we need to draw number 0~10 of tiles, and so is height.
visibleTiles.size.width = ceil(visibleTiles.origin.x + visibleTiles.size.width) - floor(visibleTiles.origin.x);
visibleTiles.size.height = ceil(visibleTiles.origin.y + visibleTiles.size.height) - floor(visibleTiles.origin.y);
2014-06-06 16:15:46 +08:00
visibleTiles.origin.x = floor(visibleTiles.origin.x);
visibleTiles.origin.y = floor(visibleTiles.origin.y);
2014-06-19 18:05:21 +08:00
// for the bigger tiles.
int tilesOverX = 0;
int tilesOverY = 0;
// for diagonal oriention tiles
float tileSizeMax = std::max(tileSize.width, tileSize.height);
if (_layerOrientation == FastTMXOrientationOrtho)
{
tilesOverX = ceil(tileSizeMax / mapTileSize.width) - 1;
tilesOverY = ceil(tileSizeMax / mapTileSize.height) - 1;
if (tilesOverX < 0) tilesOverX = 0;
if (tilesOverY < 0) tilesOverY = 0;
}
else if(_layerOrientation == FastTMXOrientationIso)
{
Rect overTileRect(0, 0, tileSizeMax - mapTileSize.width, tileSizeMax - mapTileSize.height);
if (overTileRect.size.width < 0) overTileRect.size.width = 0;
if (overTileRect.size.height < 0) overTileRect.size.height = 0;
overTileRect = RectApplyTransform(overTileRect, nodeToTileTransform);
tilesOverX = ceil(overTileRect.origin.x + overTileRect.size.width) - floor(overTileRect.origin.x);
tilesOverY = ceil(overTileRect.origin.y + overTileRect.size.height) - floor(overTileRect.origin.y);
}
2014-06-27 11:50:44 +08:00
_indicesVertexZNumber.clear();
for(const auto& iter : _indicesVertexZOffsets)
{
_indicesVertexZNumber[iter.first] = iter.second;
}
2014-06-26 20:45:08 +08:00
for (int y = visibleTiles.origin.y - tilesOverY; y < visibleTiles.origin.y + visibleTiles.size.height + tilesOverY; ++y)
2014-06-06 16:15:46 +08:00
{
if(y<0 || y >= _layerSize.height)
continue;
for (int x = visibleTiles.origin.x - tilesOverX; x < visibleTiles.origin.x + visibleTiles.size.width + tilesOverX; ++x)
2014-06-06 16:15:46 +08:00
{
if(x<0 || x >= _layerSize.width)
continue;
2014-06-26 20:45:08 +08:00
int tileIndex = getTileIndexByPos(x, y);
if(_tiles[tileIndex] == 0) continue;
int vertexZ = getVertexZForPos(Vec2(x,y));
2014-06-27 11:50:44 +08:00
auto iter = _indicesVertexZNumber.find(vertexZ);
int offset = iter->second;
iter->second++;
2014-06-06 16:15:46 +08:00
2014-06-27 11:50:44 +08:00
//CC_ASSERT(_tileToQuadIndex.find(tileIndex) != _tileToQuadIndex.end() && _tileToQuadIndex[tileIndex] <= _totalQuads.size()-1);
2014-06-26 20:45:08 +08:00
int quadIndex = (int)_tileToQuadIndex[tileIndex];
2014-06-27 11:50:44 +08:00
_indices[6 * offset + 0] = quadIndex * 4 + 0;
_indices[6 * offset + 1] = quadIndex * 4 + 1;
_indices[6 * offset + 2] = quadIndex * 4 + 2;
_indices[6 * offset + 3] = quadIndex * 4 + 3;
_indices[6 * offset + 4] = quadIndex * 4 + 2;
_indices[6 * offset + 5] = quadIndex * 4 + 1;
2014-06-26 20:45:08 +08:00
2014-06-06 16:15:46 +08:00
} // for x
} // for y
2014-06-27 11:50:44 +08:00
for(const auto& iter : _indicesVertexZOffsets)
{
_indicesVertexZNumber[iter.first] -= iter.second;
if(_indicesVertexZNumber[iter.first] == 0)
{
_indicesVertexZNumber.erase(iter.first);
}
}
2014-06-06 16:15:46 +08:00
}
void FastTMXLayer::updateVertexBuffer()
2014-06-06 16:15:46 +08:00
{
GL::bindVAO(0);
if(!glIsBuffer(_buffersVBO[0]))
{
glGenBuffers(1, &_buffersVBO[0]);
}
2014-06-06 16:15:46 +08:00
glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]);
2014-06-27 11:50:44 +08:00
glBufferData(GL_ARRAY_BUFFER, sizeof(V3F_C4B_T2F_Quad) * _totalQuads.size(), (GLvoid*)&_totalQuads[0], GL_STATIC_DRAW);
2014-06-06 16:15:46 +08:00
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
2014-06-27 11:50:44 +08:00
void FastTMXLayer::updateIndexBuffer()
{
if(!glIsBuffer(_buffersVBO[1]))
{
glGenBuffers(1, &_buffersVBO[1]);
}
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int) * _indices.size(), &_indices[0], GL_DYNAMIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
// FastTMXLayer - setup Tiles
void FastTMXLayer::setupTiles()
2014-06-06 16:15:46 +08:00
{
// Optimization: quick hack that sets the image size on the tileset
_tileSet->_imageSize = _texture->getContentSizeInPixels();
// By default all the tiles are aliased
// pros: easier to render
// cons: difficult to scale / rotate / etc.
_texture->setAliasTexParameters();
//CFByteOrder o = CFByteOrderGetCurrent();
// Parse cocos2d properties
this->parseInternalProperties();
Size screenSize = Director::getInstance()->getWinSize();
switch (_layerOrientation)
{
case FastTMXOrientationOrtho:
2014-06-06 16:15:46 +08:00
_screenGridSize.width = ceil(screenSize.width / _mapTileSize.width) + 1;
_screenGridSize.height = ceil(screenSize.height / _mapTileSize.height) + 1;
// tiles could be bigger than the grid, add additional rows if needed
_screenGridSize.height += _tileSet->_tileSize.height / _mapTileSize.height;
break;
case FastTMXOrientationIso:
2014-06-06 16:15:46 +08:00
_screenGridSize.width = ceil(screenSize.width / _mapTileSize.width) + 2;
_screenGridSize.height = ceil(screenSize.height / (_mapTileSize.height/2)) + 4;
break;
case FastTMXOrientationHex:
2014-06-27 14:42:53 +08:00
default:
CCLOGERROR("FastTMX does not support type %d", _layerOrientation);
2014-06-06 16:15:46 +08:00
break;
}
_screenTileCount = _screenGridSize.width * _screenGridSize.height;
}
Mat4 FastTMXLayer::tileToNodeTransform()
2014-06-06 16:15:46 +08:00
{
2014-06-19 18:05:21 +08:00
float w = _mapTileSize.width / CC_CONTENT_SCALE_FACTOR();
float h = _mapTileSize.height / CC_CONTENT_SCALE_FACTOR();
float offY = (_layerSize.height - 1) * h;
2014-06-06 16:15:46 +08:00
2014-06-19 18:05:21 +08:00
switch(_layerOrientation)
2014-06-06 16:15:46 +08:00
{
2014-06-27 14:45:30 +08:00
case FastTMXOrientationOrtho:
2014-06-06 16:15:46 +08:00
{
_tileToNodeTransform = Mat4
(
w, 0.0f, 0.0f, 0.0f,
0.0f, -h, 0.0f, offY,
2014-06-06 16:15:46 +08:00
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0, 0.0f, 1.0f
2014-06-06 16:15:46 +08:00
);
return _tileToNodeTransform;
}
2014-06-27 14:45:30 +08:00
case FastTMXOrientationIso:
2014-06-06 16:15:46 +08:00
{
float offX = (_layerSize.width - 1) * w / 2;
_tileToNodeTransform = Mat4
(
w/2, -w/2, 0.0f, offX,
-h/2, -h/2, 0.0f, offY,
2014-06-06 16:15:46 +08:00
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
2014-06-06 16:15:46 +08:00
);
2014-06-27 14:45:30 +08:00
return _tileToNodeTransform;
}
case FastTMXOrientationHex:
2014-06-06 16:15:46 +08:00
{
_tileToNodeTransform = Mat4::IDENTITY;
return _tileToNodeTransform;
}
default:
{
_tileToNodeTransform = Mat4::IDENTITY;
return _tileToNodeTransform;
}
2014-06-19 18:05:21 +08:00
}
2014-06-06 16:15:46 +08:00
}
2014-06-26 20:45:08 +08:00
void FastTMXLayer::updateTotalQuads()
{
if(_quadsDirty)
{
Size tileSize = CC_SIZE_PIXELS_TO_POINTS(_tileSet->_tileSize);
Size texSize = _tileSet->_imageSize;
_tileToQuadIndex.clear();
_totalQuads.resize(int(_layerSize.width * _layerSize.height));
2014-06-27 11:50:44 +08:00
_indices.resize(6 * int(_layerSize.width * _layerSize.height));
_indicesVertexZOffsets.clear();
2014-06-26 20:45:08 +08:00
int quadIndex = 0;
for(int y = 0; y < _layerSize.height; ++y)
{
for(int x =0; x < _layerSize.width; ++x)
{
int tileIndex = getTileIndexByPos(x, y);
int tileGID = _tiles[tileIndex];
if(tileGID == 0) continue;
_tileToQuadIndex[tileIndex] = quadIndex;
auto& quad = _totalQuads[quadIndex];
Vec3 nodePos(float(x), float(y), 0);
_tileToNodeTransform.transformPoint(&nodePos);
float left, right, top, bottom, z;
z = getVertexZForPos(Vec2(x, y));
2014-06-27 11:50:44 +08:00
auto iter = _indicesVertexZOffsets.find(z);
if(iter == _indicesVertexZOffsets.end())
{
_indicesVertexZOffsets[z] = 1;
}
else
{
iter->second++;
}
2014-06-26 20:45:08 +08:00
// vertices
if (tileGID & kTMXTileDiagonalFlag)
{
left = nodePos.x;
right = nodePos.x + tileSize.height;
bottom = nodePos.y + tileSize.width;
top = nodePos.y;
}
else
{
left = nodePos.x;
right = nodePos.x + tileSize.width;
bottom = nodePos.y + tileSize.height;
top = nodePos.y;
}
if(tileGID & kTMXTileVerticalFlag)
std::swap(top, bottom);
if(tileGID & kTMXTileHorizontalFlag)
std::swap(left, right);
if(tileGID & kTMXTileDiagonalFlag)
{
// XXX: not working correcly
quad.bl.vertices.x = left;
quad.bl.vertices.y = bottom;
quad.bl.vertices.z = z;
quad.br.vertices.x = left;
quad.br.vertices.y = top;
quad.br.vertices.z = z;
quad.tl.vertices.x = right;
quad.tl.vertices.y = bottom;
quad.tl.vertices.z = z;
quad.tr.vertices.x = right;
quad.tr.vertices.y = top;
quad.tr.vertices.z = z;
}
else
{
quad.bl.vertices.x = left;
quad.bl.vertices.y = bottom;
quad.bl.vertices.z = z;
quad.br.vertices.x = right;
quad.br.vertices.y = bottom;
quad.br.vertices.z = z;
quad.tl.vertices.x = left;
quad.tl.vertices.y = top;
quad.tl.vertices.z = z;
quad.tr.vertices.x = right;
quad.tr.vertices.y = top;
quad.tr.vertices.z = z;
}
// texcoords
Rect tileTexture = _tileSet->getRectForGID(tileGID);
left = (tileTexture.origin.x / texSize.width);
right = left + (tileTexture.size.width / texSize.width);
bottom = (tileTexture.origin.y / texSize.height);
top = bottom + (tileTexture.size.height / texSize.height);
quad.bl.texCoords.u = left;
quad.bl.texCoords.v = bottom;
quad.br.texCoords.u = right;
quad.br.texCoords.v = bottom;
quad.tl.texCoords.u = left;
quad.tl.texCoords.v = top;
quad.tr.texCoords.u = right;
quad.tr.texCoords.v = top;
quad.bl.colors = Color4B::WHITE;
quad.br.colors = Color4B::WHITE;
quad.tl.colors = Color4B::WHITE;
quad.tr.colors = Color4B::WHITE;
++quadIndex;
}
}
2014-06-27 11:50:44 +08:00
int offset = 0;
for(auto iter = _indicesVertexZOffsets.begin(); iter != _indicesVertexZOffsets.end(); ++iter)
{
std::swap(offset, iter->second);
offset += iter->second;
}
updateVertexBuffer();
2014-06-26 20:45:08 +08:00
_quadsDirty = false;
}
}
2014-06-06 16:15:46 +08:00
// removing / getting tiles
Sprite* FastTMXLayer::getTileAt(const Vec2& tileCoordinate)
2014-06-06 16:15:46 +08:00
{
2014-06-19 18:05:21 +08:00
CCASSERT( tileCoordinate.x < _layerSize.width && tileCoordinate.y < _layerSize.height && tileCoordinate.x >=0 && tileCoordinate.y >=0, "TMXLayer: invalid position");
CCASSERT( _tiles, "TMXLayer: the tiles map has been released");
2014-06-06 16:15:46 +08:00
2014-06-19 18:05:21 +08:00
Sprite *tile = nullptr;
int gid = this->getTileGIDAt(tileCoordinate);
2014-06-06 16:15:46 +08:00
2014-06-19 18:05:21 +08:00
// if GID == 0, then no tile is present
if( gid ) {
int index = tileCoordinate.x + tileCoordinate.y * _layerSize.width;
2014-06-06 16:15:46 +08:00
auto it = _spriteContainer.find(index);
if (it != _spriteContainer.end())
{
tile = it->second.first;
}
else
{
// tile not created yet. create it
2014-06-19 18:05:21 +08:00
Rect rect = _tileSet->getRectForGID(gid);
2014-06-06 16:15:46 +08:00
rect = CC_RECT_PIXELS_TO_POINTS(rect);
tile = Sprite::createWithTexture(_texture, rect);
Vec2 p = this->getPositionAt(tileCoordinate);
tile->setAnchorPoint(Vec2::ZERO);
2014-06-06 16:15:46 +08:00
tile->setPosition(p);
tile->setPositionZ((float)getVertexZForPos(tileCoordinate));
tile->setOpacity(this->getOpacity());
tile->setTag(index);
this->addChild(tile, index);
2014-06-06 16:15:46 +08:00
_spriteContainer.insert(std::pair<int, std::pair<Sprite*, int> >(index, std::pair<Sprite*, int>(tile, gid)));
// tile is converted to sprite.
2014-06-26 20:45:08 +08:00
setFlaggedTileGIDByIndex(index, 0);
2014-06-06 16:15:46 +08:00
}
2014-06-19 18:05:21 +08:00
}
return tile;
2014-06-06 16:15:46 +08:00
}
int FastTMXLayer::getTileGIDAt(const Vec2& tileCoordinate, TMXTileFlags* flags/* = nullptr*/)
2014-06-06 16:15:46 +08:00
{
CCASSERT(tileCoordinate.x < _layerSize.width && tileCoordinate.y < _layerSize.height && tileCoordinate.x >=0 && tileCoordinate.y >=0, "TMXLayer: invalid position");
CCASSERT(_tiles, "TMXLayer: the tiles map has been released");
int idx = static_cast<int>((tileCoordinate.x + tileCoordinate.y * _layerSize.width));
// Bits on the far end of the 32-bit global tile ID are used for tile flags
int tile = _tiles[idx];
auto it = _spriteContainer.find(idx);
// converted to sprite.
if (tile == 0 && it != _spriteContainer.end())
{
tile = it->second.second;
}
// issue1264, flipped tiles can be changed dynamically
if (flags)
{
*flags = (TMXTileFlags)(tile & kTMXFlipedAll);
}
return (tile & kTMXFlippedMask);
}
Vec2 FastTMXLayer::getPositionAt(const Vec2& pos)
2014-06-06 16:15:46 +08:00
{
return PointApplyTransform(pos, _tileToNodeTransform);
}
int FastTMXLayer::getVertexZForPos(const Vec2& pos)
2014-06-06 16:15:46 +08:00
{
int ret = 0;
int maxVal = 0;
if (_useAutomaticVertexZ)
{
switch (_layerOrientation)
{
case FastTMXOrientationIso:
2014-06-06 16:15:46 +08:00
maxVal = static_cast<int>(_layerSize.width + _layerSize.height);
ret = static_cast<int>(-(maxVal - (pos.x + pos.y)));
break;
case FastTMXOrientationOrtho:
2014-06-06 16:15:46 +08:00
ret = static_cast<int>(-(_layerSize.height-pos.y));
break;
case FastTMXOrientationHex:
2014-06-06 16:15:46 +08:00
CCASSERT(0, "TMX Hexa zOrder not supported");
break;
default:
CCASSERT(0, "TMX invalid value");
break;
}
}
else
{
ret = _vertexZvalue;
}
return ret;
}
void FastTMXLayer::removeTileAt(const Vec2& tileCoordinate)
2014-06-06 16:15:46 +08:00
{
2014-06-19 18:05:21 +08:00
CCASSERT( tileCoordinate.x < _layerSize.width && tileCoordinate.y < _layerSize.height && tileCoordinate.x >=0 && tileCoordinate.y >=0, "TMXLayer: invalid position");
2014-06-06 16:15:46 +08:00
2014-06-19 18:05:21 +08:00
int gid = this->getTileGIDAt(tileCoordinate);
2014-06-06 16:15:46 +08:00
2014-06-19 18:05:21 +08:00
if( gid ) {
2014-06-06 16:15:46 +08:00
2014-06-19 18:05:21 +08:00
int z = tileCoordinate.x + tileCoordinate.y * _layerSize.width;
2014-06-06 16:15:46 +08:00
2014-06-19 18:05:21 +08:00
// remove tile from GID map
2014-06-26 20:45:08 +08:00
setFlaggedTileGIDByIndex(z, 0);
2014-06-06 16:15:46 +08:00
2014-06-19 18:05:21 +08:00
// remove it from sprites
2014-06-06 16:15:46 +08:00
auto it = _spriteContainer.find(z);
if (it != _spriteContainer.end())
{
this->removeChild(it->second.first);
}
2014-06-19 18:05:21 +08:00
}
2014-06-06 16:15:46 +08:00
}
2014-06-26 20:45:08 +08:00
void FastTMXLayer::setFlaggedTileGIDByIndex(int index, int gid)
2014-06-06 18:03:44 +08:00
{
2014-06-26 20:45:08 +08:00
if(gid == _tiles[index]) return;
2014-06-06 18:03:44 +08:00
_tiles[index] = gid;
2014-06-26 20:45:08 +08:00
_quadsDirty = true;
2014-06-06 18:03:44 +08:00
_dirty = true;
}
void FastTMXLayer::removeChild(Node* node, bool cleanup)
2014-06-06 16:15:46 +08:00
{
int tag = node->getTag();
auto it = _spriteContainer.find(tag);
if (it != _spriteContainer.end() && it->second.first == node)
{
_spriteContainer.erase(it);
}
Node::removeChild(node, cleanup);
}
// FastTMXLayer - Properties
Value FastTMXLayer::getProperty(const std::string& propertyName) const
2014-06-06 16:15:46 +08:00
{
if (_properties.find(propertyName) != _properties.end())
return _properties.at(propertyName);
return Value();
}
void FastTMXLayer::parseInternalProperties()
2014-06-06 16:15:46 +08:00
{
auto vertexz = getProperty("cc_vertexz");
2014-06-26 11:45:15 +08:00
if (vertexz.isNull()) return;
std::string vertexZStr = vertexz.asString();
// If "automatic" is on, then parse the "cc_alpha_func" too
if (vertexZStr == "automatic")
2014-06-06 16:15:46 +08:00
{
2014-06-26 11:45:15 +08:00
_useAutomaticVertexZ = true;
auto alphaFuncVal = getProperty("cc_alpha_func");
float alphaFuncValue = alphaFuncVal.asFloat();
setGLProgram(GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST));
GLint alphaValueLocation = glGetUniformLocation(getGLProgram()->getProgram(), GLProgram::UNIFORM_NAME_ALPHA_TEST_VALUE);
// NOTE: alpha test shader is hard-coded to use the equivalent of a glAlphaFunc(GL_GREATER) comparison
// use shader program to set uniform
getGLProgram()->use();
getGLProgram()->setUniformLocationWith1f(alphaValueLocation, alphaFuncValue);
CHECK_GL_ERROR_DEBUG();
}
else
{
_vertexZvalue = vertexz.asInt();
2014-06-06 16:15:46 +08:00
}
}
//CCTMXLayer2 - obtaining positions, offset
Vec2 FastTMXLayer::calculateLayerOffset(const Vec2& pos)
2014-06-06 16:15:46 +08:00
{
Vec2 ret = Vec2::ZERO;
2014-06-06 16:15:46 +08:00
switch (_layerOrientation)
{
case FastTMXOrientationOrtho:
ret = Vec2( pos.x * _mapTileSize.width, -pos.y *_mapTileSize.height);
2014-06-06 16:15:46 +08:00
break;
case FastTMXOrientationIso:
ret = Vec2((_mapTileSize.width /2) * (pos.x - pos.y),
2014-06-06 16:15:46 +08:00
(_mapTileSize.height /2 ) * (-pos.x - pos.y));
break;
case FastTMXOrientationHex:
CCASSERT(pos.equals(Vec2::ZERO), "offset for hexagonal map not implemented yet");
2014-06-06 16:15:46 +08:00
break;
}
return ret;
}
// TMXLayer - adding / remove tiles
void FastTMXLayer::setTileGID(int gid, const Vec2& tileCoordinate)
2014-06-06 16:15:46 +08:00
{
setTileGID(gid, tileCoordinate, (TMXTileFlags)0);
}
void FastTMXLayer::setTileGID(int gid, const Vec2& tileCoordinate, TMXTileFlags flags)
2014-06-06 16:15:46 +08:00
{
CCASSERT(tileCoordinate.x < _layerSize.width && tileCoordinate.y < _layerSize.height && tileCoordinate.x >=0 && tileCoordinate.y >=0, "TMXLayer: invalid position");
CCASSERT(_tiles, "TMXLayer: the tiles map has been released");
CCASSERT(gid == 0 || gid >= _tileSet->_firstGid, "TMXLayer: invalid gid" );
TMXTileFlags currentFlags;
int currentGID = getTileGIDAt(tileCoordinate, &currentFlags);
2014-06-26 11:45:15 +08:00
if (currentGID == gid && currentFlags == flags) return;
int gidAndFlags = gid | flags;
// setting gid=0 is equal to remove the tile
if (gid == 0)
2014-06-06 16:15:46 +08:00
{
2014-06-26 11:45:15 +08:00
removeTileAt(tileCoordinate);
}
// empty tile. create a new one
else if (currentGID == 0)
{
int z = tileCoordinate.x + tileCoordinate.y * _layerSize.width;
2014-06-26 20:45:08 +08:00
setFlaggedTileGIDByIndex(z, gidAndFlags);
2014-06-26 11:45:15 +08:00
}
// modifying an existing tile with a non-empty tile
else
{
int z = tileCoordinate.x + tileCoordinate.y * _layerSize.width;
auto it = _spriteContainer.find(z);
if (it != _spriteContainer.end())
2014-06-06 16:15:46 +08:00
{
2014-06-26 11:45:15 +08:00
Sprite *sprite = it->second.first;
Rect rect = _tileSet->getRectForGID(gid);
rect = CC_RECT_PIXELS_TO_POINTS(rect);
sprite->setTextureRect(rect, false, rect.size);
this->reorderChild(sprite, z);
if (flags)
{
setupTileSprite(sprite, sprite->getPosition(), gidAndFlags);
}
it->second.second = gidAndFlags;
2014-06-06 16:15:46 +08:00
}
else
{
2014-06-26 20:45:08 +08:00
setFlaggedTileGIDByIndex(z, gidAndFlags);
2014-06-06 16:15:46 +08:00
}
}
}
void FastTMXLayer::setupTileSprite(Sprite* sprite, Vec2 pos, int gid)
2014-06-06 16:15:46 +08:00
{
sprite->setPosition(getPositionAt(pos));
sprite->setPositionZ((float)getVertexZForPos(pos));
sprite->setAnchorPoint(Vec2::ZERO);
2014-06-06 16:15:46 +08:00
sprite->setOpacity(this->getOpacity());
//issue 1264, flip can be undone as well
sprite->setFlippedX(false);
sprite->setFlippedY(false);
sprite->setRotation(0.0f);
// Rotation in tiled is achieved using 3 flipped states, flipping across the horizontal, vertical, and diagonal axes of the tiles.
if (gid & kTMXTileDiagonalFlag)
{
// put the anchor in the middle for ease of rotation.
sprite->setAnchorPoint(Vec2(0.5f,0.5f));
sprite->setPosition(Vec2(getPositionAt(pos).x + sprite->getContentSize().height/2,
2014-06-06 16:15:46 +08:00
getPositionAt(pos).y + sprite->getContentSize().width/2 ) );
int flag = gid & (kTMXTileHorizontalFlag | kTMXTileVerticalFlag );
// handle the 4 diagonally flipped states.
if (flag == kTMXTileHorizontalFlag)
{
sprite->setRotation(90.0f);
}
else if (flag == kTMXTileVerticalFlag)
{
sprite->setRotation(270.0f);
}
else if (flag == (kTMXTileVerticalFlag | kTMXTileHorizontalFlag) )
{
sprite->setRotation(90.0f);
sprite->setFlippedX(true);
}
else
{
sprite->setRotation(270.0f);
sprite->setFlippedX(true);
}
}
else
{
if (gid & kTMXTileHorizontalFlag)
{
sprite->setFlippedX(true);
}
if (gid & kTMXTileVerticalFlag)
{
sprite->setFlippedY(true);
}
}
}
std::string FastTMXLayer::getDescription() const
2014-06-06 16:15:46 +08:00
{
return StringUtils::format("<FastTMXLayer | tag = %d, size = %d,%d>", _tag, (int)_mapTileSize.width, (int)_mapTileSize.height);
2014-06-06 16:15:46 +08:00
}
NS_CC_END