From a8a5f6b230f2cd47ed1e3831f1fbab4e9fe04a1d Mon Sep 17 00:00:00 2001 From: boyu0 Date: Fri, 6 Jun 2014 16:15:46 +0800 Subject: [PATCH 01/40] issue #4437: add new tmx. --- build/cocos2d_libs.xcodeproj/project.pbxproj | 24 + cocos/2d/CCTMXLayer2.cpp | 805 +++++++++++++++++++ cocos/2d/CCTMXLayer2.h | 261 ++++++ cocos/2d/CCTMXTiledMap2.cpp | 259 ++++++ cocos/2d/CCTMXTiledMap2.h | 222 +++++ cocos/base/ccTypes.h | 21 + 6 files changed, 1592 insertions(+) create mode 100644 cocos/2d/CCTMXLayer2.cpp create mode 100644 cocos/2d/CCTMXLayer2.h create mode 100644 cocos/2d/CCTMXTiledMap2.cpp create mode 100644 cocos/2d/CCTMXTiledMap2.h diff --git a/build/cocos2d_libs.xcodeproj/project.pbxproj b/build/cocos2d_libs.xcodeproj/project.pbxproj index 2f28263747..abbf67adac 100644 --- a/build/cocos2d_libs.xcodeproj/project.pbxproj +++ b/build/cocos2d_libs.xcodeproj/project.pbxproj @@ -1775,6 +1775,14 @@ B3AF01A31842FBA400A98B85 /* b2MotorJoint.h in Headers */ = {isa = PBXBuildFile; fileRef = B3AF019F1842FBA400A98B85 /* b2MotorJoint.h */; }; B3B12A5A17E7F44000026B4A /* libchipmunk Mac.a in Frameworks */ = {isa = PBXBuildFile; fileRef = A03F2CB81780BD04006731B9 /* libchipmunk Mac.a */; }; B3B12A5B17E7F45C00026B4A /* libchipmunk iOS.a in Frameworks */ = {isa = PBXBuildFile; fileRef = A07A4F3B178387670073F6A7 /* libchipmunk iOS.a */; }; + C0940CAC19419BDA00B8BB48 /* CCTMXLayer2.h in Headers */ = {isa = PBXBuildFile; fileRef = C0940CA819419BDA00B8BB48 /* CCTMXLayer2.h */; }; + C0940CAD19419BDA00B8BB48 /* CCTMXLayer2.h in Headers */ = {isa = PBXBuildFile; fileRef = C0940CA819419BDA00B8BB48 /* CCTMXLayer2.h */; }; + C0940CAE19419BDA00B8BB48 /* CCTMXTiledMap2.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C0940CA919419BDA00B8BB48 /* CCTMXTiledMap2.cpp */; }; + C0940CAF19419BDA00B8BB48 /* CCTMXTiledMap2.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C0940CA919419BDA00B8BB48 /* CCTMXTiledMap2.cpp */; }; + C0940CB019419BDA00B8BB48 /* CCTMXTiledMap2.h in Headers */ = {isa = PBXBuildFile; fileRef = C0940CAA19419BDA00B8BB48 /* CCTMXTiledMap2.h */; }; + C0940CB119419BDA00B8BB48 /* CCTMXTiledMap2.h in Headers */ = {isa = PBXBuildFile; fileRef = C0940CAA19419BDA00B8BB48 /* CCTMXTiledMap2.h */; }; + C0940CB219419BDA00B8BB48 /* CCTMXLayer2.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C0940CAB19419BDA00B8BB48 /* CCTMXLayer2.cpp */; }; + C0940CB319419BDA00B8BB48 /* CCTMXLayer2.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C0940CAB19419BDA00B8BB48 /* CCTMXLayer2.cpp */; }; ED9C6A9418599AD8000A5232 /* CCNodeGrid.cpp in Sources */ = {isa = PBXBuildFile; fileRef = ED9C6A9218599AD8000A5232 /* CCNodeGrid.cpp */; }; ED9C6A9518599AD8000A5232 /* CCNodeGrid.cpp in Sources */ = {isa = PBXBuildFile; fileRef = ED9C6A9218599AD8000A5232 /* CCNodeGrid.cpp */; }; ED9C6A9618599AD8000A5232 /* CCNodeGrid.h in Headers */ = {isa = PBXBuildFile; fileRef = ED9C6A9318599AD8000A5232 /* CCNodeGrid.h */; }; @@ -2810,6 +2818,10 @@ B375104F1823AC7B00B3BA6A /* CCPhysicsWorldInfo_chipmunk.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCPhysicsWorldInfo_chipmunk.h; sourceTree = ""; }; B3AF019E1842FBA400A98B85 /* b2MotorJoint.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = b2MotorJoint.cpp; sourceTree = ""; }; B3AF019F1842FBA400A98B85 /* b2MotorJoint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = b2MotorJoint.h; sourceTree = ""; }; + C0940CA819419BDA00B8BB48 /* CCTMXLayer2.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCTMXLayer2.h; sourceTree = ""; }; + C0940CA919419BDA00B8BB48 /* CCTMXTiledMap2.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCTMXTiledMap2.cpp; sourceTree = ""; }; + C0940CAA19419BDA00B8BB48 /* CCTMXTiledMap2.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCTMXTiledMap2.h; sourceTree = ""; }; + C0940CAB19419BDA00B8BB48 /* CCTMXLayer2.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCTMXLayer2.cpp; sourceTree = ""; }; ED9C6A9218599AD8000A5232 /* CCNodeGrid.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; lineEnding = 0; path = CCNodeGrid.cpp; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.cpp; }; ED9C6A9318599AD8000A5232 /* CCNodeGrid.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCNodeGrid.h; sourceTree = ""; }; /* End PBXFileReference section */ @@ -3307,6 +3319,10 @@ 1A5702DF180BCE610088DEC7 /* tilemap-parallax-nodes */ = { isa = PBXGroup; children = ( + C0940CA819419BDA00B8BB48 /* CCTMXLayer2.h */, + C0940CAB19419BDA00B8BB48 /* CCTMXLayer2.cpp */, + C0940CA919419BDA00B8BB48 /* CCTMXTiledMap2.cpp */, + C0940CAA19419BDA00B8BB48 /* CCTMXTiledMap2.h */, 1A5702FE180BCE890088DEC7 /* CCParallaxNode.cpp */, 1A5702FF180BCE890088DEC7 /* CCParallaxNode.h */, 1A5702E0180BCE750088DEC7 /* CCTileMapAtlas.cpp */, @@ -4998,6 +5014,7 @@ 50ABBED51925AB6F00A911A9 /* utlist.h in Headers */, 50FCEBAD18C72017004AD434 /* PageViewReader.h in Headers */, 1A5702F4180BCE750088DEC7 /* CCTMXObjectGroup.h in Headers */, + C0940CB019419BDA00B8BB48 /* CCTMXTiledMap2.h in Headers */, 50ABBDAF1925AB4100A911A9 /* CCRenderer.h in Headers */, 1A5702F8180BCE750088DEC7 /* CCTMXTiledMap.h in Headers */, 5034CA21191D591100CE6051 /* ccShader_PositionTextureColorAlphaTest.frag in Headers */, @@ -5114,6 +5131,7 @@ 1AAF536C180E3374000584C8 /* HttpClient.h in Headers */, 50ABBD9D1925AB4100A911A9 /* ccGLStateCache.h in Headers */, 50ABBEB91925AB6F00A911A9 /* ccUTF8.h in Headers */, + C0940CAC19419BDA00B8BB48 /* CCTMXLayer2.h in Headers */, 1AAF536E180E3374000584C8 /* HttpRequest.h in Headers */, 29CB8F551929D7A900C841D6 /* UIScrollInterface.h in Headers */, 1AAF5370180E3374000584C8 /* HttpResponse.h in Headers */, @@ -5467,6 +5485,7 @@ 1A570230180BCC1A0088DEC7 /* CCParticleSystemQuad.h in Headers */, 5034CA2C191D591100CE6051 /* ccShader_PositionTextureA8Color.vert in Headers */, 50ABBE981925AB6F00A911A9 /* CCProtocols.h in Headers */, + C0940CB119419BDA00B8BB48 /* CCTMXTiledMap2.h in Headers */, 2905FA8B18CF08D100240AA3 /* UITextField.h in Headers */, 50FCEBA618C72017004AD434 /* ListViewReader.h in Headers */, 50ABBD431925AB0000A911A9 /* CCMathBase.h in Headers */, @@ -5478,6 +5497,7 @@ 50ABBE701925AB6F00A911A9 /* CCEventListenerKeyboard.h in Headers */, 1A57028D180BCC900088DEC7 /* CCSpriteFrameCache.h in Headers */, 1A570295180BCCAB0088DEC7 /* CCAnimation.h in Headers */, + C0940CAD19419BDA00B8BB48 /* CCTMXLayer2.h in Headers */, 50ABBDB81925AB4100A911A9 /* CCTexture2D.h in Headers */, 50ABBE341925AB6F00A911A9 /* CCConfiguration.h in Headers */, 1A570299180BCCAB0088DEC7 /* CCAnimationCache.h in Headers */, @@ -6121,6 +6141,7 @@ 50ABBD831925AB4100A911A9 /* CCBatchCommand.cpp in Sources */, 1A5701C7180BCB5A0088DEC7 /* CCLabelTextFormatter.cpp in Sources */, 1A5701CB180BCB5A0088DEC7 /* CCLabelTTF.cpp in Sources */, + C0940CAE19419BDA00B8BB48 /* CCTMXTiledMap2.cpp in Sources */, 50ABBE711925AB6F00A911A9 /* CCEventListenerMouse.cpp in Sources */, 1A5701DE180BCB8C0088DEC7 /* CCLayer.cpp in Sources */, 1A5701E2180BCB8C0088DEC7 /* CCScene.cpp in Sources */, @@ -6208,6 +6229,7 @@ 1AD71DE5180E26E600808F54 /* CCMenuItemImageLoader.cpp in Sources */, 50ABBD8B1925AB4100A911A9 /* CCGLProgram.cpp in Sources */, 1AD71DE9180E26E600808F54 /* CCMenuItemLoader.cpp in Sources */, + C0940CB219419BDA00B8BB48 /* CCTMXLayer2.cpp in Sources */, 50ABBDA31925AB4100A911A9 /* CCQuadCommand.cpp in Sources */, 2905FA6A18CF08D100240AA3 /* UIPageView.cpp in Sources */, B29594C61926D61F003EEF37 /* CCObjLoader.cpp in Sources */, @@ -6550,6 +6572,7 @@ 503DD8E21926736A00CD74DD /* CCCommon.mm in Sources */, 1A5701A2180BCB590088DEC7 /* CCFontAtlas.cpp in Sources */, 50ABC00E1926664800A911A9 /* CCFileUtils.cpp in Sources */, + C0940CB319419BDA00B8BB48 /* CCTMXLayer2.cpp in Sources */, 50ABBE241925AB6F00A911A9 /* base64.cpp in Sources */, 1A5701A6180BCB590088DEC7 /* CCFontAtlasCache.cpp in Sources */, 1A5701B2180BCB590088DEC7 /* CCFontFNT.cpp in Sources */, @@ -6733,6 +6756,7 @@ 50ABC0001926664800A911A9 /* CCFileUtilsApple.mm in Sources */, 2905FA7918CF08D100240AA3 /* UISlider.cpp in Sources */, 1A8C59A4180E930E00EF57C3 /* CCArmatureAnimation.cpp in Sources */, + C0940CAF19419BDA00B8BB48 /* CCTMXTiledMap2.cpp in Sources */, 1A8C59A8180E930E00EF57C3 /* CCArmatureDataManager.cpp in Sources */, 2905FA8918CF08D100240AA3 /* UITextField.cpp in Sources */, 1A8C59AC180E930E00EF57C3 /* CCArmatureDefine.cpp in Sources */, diff --git a/cocos/2d/CCTMXLayer2.cpp b/cocos/2d/CCTMXLayer2.cpp new file mode 100644 index 0000000000..cb9c798755 --- /dev/null +++ b/cocos/2d/CCTMXLayer2.cpp @@ -0,0 +1,805 @@ +/**************************************************************************** +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 "CCTMXLayer2.h" +#include "CCTMXXMLParser.h" +#include "CCTMXTiledMap.h" +#include "CCSprite.h" +#include "CCTextureCache.h" +#include "CCGLProgramCache.h" +#include "CCGLProgram.h" +#include "ccCArray.h" +#include "CCDirector.h" +#include "CCConfiguration.h" +#include "renderer/CCRenderer.h" +#include "deprecated/CCString.h" + +NS_CC_BEGIN + +namespace +{ + static const int MAX_QUADS_COUNT = 65536 / 6; +} + +// TMXLayer2 - init & alloc & dealloc + +TMXLayer2 * TMXLayer2::create(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo) +{ + TMXLayer2 *ret = new TMXLayer2(); + if (ret->initWithTilesetInfo(tilesetInfo, layerInfo, mapInfo)) + { + ret->autorelease(); + return ret; + } + return nullptr; +} +bool TMXLayer2::initWithTilesetInfo(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo) +{ + + if( tilesetInfo ) + { + _texture = Director::getInstance()->getTextureCache()->addImage(tilesetInfo->_sourceImage); + _texture->retain(); + } + + // layerInfo + _layerName = layerInfo->_name; + _layerSize = layerInfo->_layerSize; + _tiles = layerInfo->_tiles; + 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); + Point offset = this->calculateLayerOffset(layerInfo->_offset); + 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; +} + +TMXLayer2::TMXLayer2() +:_layerName("") +,_layerSize(Size::ZERO) +,_mapTileSize(Size::ZERO) +,_tiles(nullptr) +,_tileSet(nullptr) +,_layerOrientation(TMXOrientationOrtho) +,_texture(nullptr) +,_reusedTile(nullptr) +,_previousRect(0,0,0,0) +,_verticesToDraw(0) +,_vertexZvalue(0) +,_useAutomaticVertexZ(false) +,_quads(nullptr) +,_indices(nullptr) +,_numQuads(0) +{} + +TMXLayer2::~TMXLayer2() +{ + CC_SAFE_RELEASE(_tileSet); + CC_SAFE_RELEASE(_texture); + CC_SAFE_DELETE_ARRAY(_tiles); + CC_SAFE_FREE(_quads); + CC_SAFE_FREE(_indices); +} + +void TMXLayer2::draw(Renderer *renderer, const Mat4& transform, uint32_t flags) +{ + _customCommand.init(_globalZOrder); + _customCommand.func = CC_CALLBACK_0(TMXLayer2::onDraw, this, transform, flags); + renderer->addCommand(&_customCommand); +} + +void TMXLayer2::onDraw(const Mat4 &transform, bool transformUpdated) +{ + GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_TEX_COORD); + GL::bindTexture2D( _texture->getName() ); + + + // tex coords + indices + glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]); + + + if( transformUpdated ) { + + Size s = Director::getInstance()->getWinSize(); + auto rect = Rect(0, 0, s.width, s.height); + + Mat4 inv = transform; + inv.inverse(); + rect = RectApplyTransform(rect, inv); + + if( !rect.equals(_previousRect) ) + { + if (Configuration::getInstance()->supportsShareableVAO()) + { + V2F_T2F_Quad* quads = (V2F_T2F_Quad*)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY); + GLushort* indices = (GLushort *)glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY); + _verticesToDraw = updateTiles(rect, quads, indices); + glUnmapBuffer(GL_ARRAY_BUFFER); + glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER); + } + else + { + _verticesToDraw = updateTiles(rect, nullptr, nullptr); + + if (_quads != nullptr && _indices != nullptr && _verticesToDraw > 0) + { + glBufferData(GL_ARRAY_BUFFER, sizeof(_quads[0]) * _numQuads , _quads, GL_DYNAMIC_DRAW); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(_indices[0]) * _numQuads * 6 , _indices, GL_STATIC_DRAW); + } + } + + _previousRect = rect; + } + + // don't draw more than 65535 vertices since we are using GL_UNSIGNED_SHORT for indices + _verticesToDraw = std::min(_verticesToDraw, 65535); + } + + if(_verticesToDraw > 0) { + + getGLProgram()->use(); + getGLProgram()->setUniformsForBuiltins(_modelViewTransform); + + // vertices + glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_T2F), (GLvoid*) offsetof(V2F_T2F, vertices)); + + // tex coords + glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_T2F), (GLvoid*) offsetof(V2F_T2F, texCoords)); + + // color + glVertexAttrib4f(GLProgram::VERTEX_ATTRIB_COLOR, _displayedColor.r/255.0f, _displayedColor.g/255.0f, _displayedColor.b/255.0f, _displayedOpacity/255.0f); + + glDrawElements(GL_TRIANGLES, _verticesToDraw, GL_UNSIGNED_SHORT, nullptr); + CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,_verticesToDraw); + } + + // cleanup + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); +} + +int TMXLayer2::updateTiles(const Rect& culledRect, V2F_T2F_Quad *quads, GLushort *indices) +{ + int tilesUsed = 0; + + Rect visibleTiles = culledRect; + Size mapTileSize = CC_SIZE_PIXELS_TO_POINTS(_mapTileSize); + Size tileSize = CC_SIZE_PIXELS_TO_POINTS(_tileSet->_tileSize); + Mat4 nodeToTile = _tileToNodeTransform; + nodeToTile.inverse(); + //transform to tile + visibleTiles = RectApplyTransform(visibleTiles, nodeToTile); + + visibleTiles.origin.x = floor(visibleTiles.origin.x); + visibleTiles.origin.y = floor(visibleTiles.origin.y); + visibleTiles.size.width = ceil(visibleTiles.size.width); + visibleTiles.size.height = ceil(visibleTiles.size.height); + + V2F_T2F_Quad* quadsTmp = quads; + GLushort* indicesTmp = indices; + // doesn't support VBO + if (quadsTmp == nullptr) + { + int quadsNeed = std::min(static_cast((visibleTiles.size.width + 1) * (visibleTiles.size.height + 1)), MAX_QUADS_COUNT); + if (_numQuads < quadsNeed) + { + _numQuads = quadsNeed; + _quads = (V2F_T2F_Quad*)realloc(_quads, _numQuads * sizeof(V2F_T2F_Quad)); + _indices = (GLushort*)realloc(_indices, _numQuads * 6 * sizeof(GLushort)); + } + + quadsTmp = _quads; + indicesTmp = _indices; + } + + int rows_per_tile = ceil(tileSize.height / mapTileSize.height) - 1; + + Size texSize = _tileSet->_imageSize; + for (int y = visibleTiles.origin.y - rows_per_tile; y <= visibleTiles.origin.y + visibleTiles.size.height; ++y) + { + if(y<0 || y >= _layerSize.height) + continue; + for (int x = visibleTiles.origin.x; x <= visibleTiles.origin.x + visibleTiles.size.width; x++) + { + if(x<0 || x >= _layerSize.width) + continue; + + int tileGID = _tiles[x + y * (int)_layerSize.width]; + + // GID==0 empty tile + if(tileGID!=0) { + + V2F_T2F_Quad *quad = &quadsTmp[tilesUsed]; + + Vec3 nodePos(static_cast(x), static_cast(y), 0); + _tileToNodeTransform.transformPoint(&nodePos); + + float left, right, top, bottom; + + // vertices + 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->br.vertices.x = left; + quad->br.vertices.y = top; + quad->tl.vertices.x = right; + quad->tl.vertices.y = bottom; + quad->tr.vertices.x = right; + quad->tr.vertices.y = top; + } + else + { + quad->bl.vertices.x = left; + quad->bl.vertices.y = bottom; + quad->br.vertices.x = right; + quad->br.vertices.y = bottom; + quad->tl.vertices.x = left; + quad->tl.vertices.y = top; + quad->tr.vertices.x = right; + quad->tr.vertices.y = top; + } + + // 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; + + + GLushort *idxbase = indicesTmp + tilesUsed * 6; + int vertexbase = tilesUsed * 4; + + idxbase[0] = vertexbase; + idxbase[1] = vertexbase + 1; + idxbase[2] = vertexbase + 2; + idxbase[3] = vertexbase + 3; + idxbase[4] = vertexbase + 2; + idxbase[5] = vertexbase + 1; + + tilesUsed++; + } + + if (tilesUsed >= MAX_QUADS_COUNT) + { + break; + } + } // for x + + if (tilesUsed >= MAX_QUADS_COUNT) + { + break; + } + } // for y + + return tilesUsed * 6; +} + +void TMXLayer2::setupVBO() +{ + glGenBuffers(2, &_buffersVBO[0]); + + // 10922 = 65536/6 + int total = std::min(static_cast(_layerSize.width * _layerSize.height), MAX_QUADS_COUNT); + + // Vertex + Tex Coords + glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]); + glBufferData(GL_ARRAY_BUFFER, total * sizeof(V2F_T2F_Quad), NULL, GL_DYNAMIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, 0); + + // Indices + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, total * 6 * sizeof(GLushort), NULL, GL_DYNAMIC_DRAW); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + + CHECK_GL_ERROR_DEBUG(); +} + +// TMXLayer2 - setup Tiles +void TMXLayer2::setupTiles() +{ + // 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 TMXOrientationOrtho: + _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 TMXOrientationIso: + _screenGridSize.width = ceil(screenSize.width / _mapTileSize.width) + 2; + _screenGridSize.height = ceil(screenSize.height / (_mapTileSize.height/2)) + 4; + break; + case TMXOrientationHex: + break; + } + + _screenTileCount = _screenGridSize.width * _screenGridSize.height; + + setupVBO(); +} + +Mat4 TMXLayer2::tileToNodeTransform() +{ + float w = _mapTileSize.width / CC_CONTENT_SCALE_FACTOR(); + float h = _mapTileSize.height / CC_CONTENT_SCALE_FACTOR(); + float offY = (_layerSize.height - 1) * h; + + switch(_layerOrientation) + { + case TMXOrientationOrtho: + { + _tileToNodeTransform = Mat4 + ( + w, 0.0f, 0.0f, 0.0f, + 0.0f, -h, 0.0f, 0.0f, + 0.0f, 0.0f, 1.0f, 0.0f, + 0.0f, offY, 0.0f, 1.0f + ); + + return _tileToNodeTransform; + } + case TMXOrientationIso: + { + float offX = (_layerSize.width - 1) * w / 2; + _tileToNodeTransform = Mat4 + ( + w/2, -h/2, 0.0f, 0.0f, + -w/2, -h/2, 0.0f, 0.0f, + 0.0f, 0.0f, 1.0f, 0.0f, + offX, offY, 0.0f, 1.0f + + ); + return _tileToNodeTransform; + } + case TMXOrientationHex: + { + _tileToNodeTransform = Mat4::IDENTITY; + return _tileToNodeTransform; + } + default: + { + _tileToNodeTransform = Mat4::IDENTITY; + return _tileToNodeTransform; + } + } + +} + +// removing / getting tiles +Sprite* TMXLayer2::getTileAt(const Point& tileCoordinate) +{ + 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"); + + Sprite *tile = nullptr; + int gid = this->getTileGIDAt(tileCoordinate); + + // if GID == 0, then no tile is present + if( gid ) { + int index = tileCoordinate.x + tileCoordinate.y * _layerSize.width; + + auto it = _spriteContainer.find(index); + if (it != _spriteContainer.end()) + { + tile = it->second.first; + } + else + { + // tile not created yet. create it + Rect rect = _tileSet->getRectForGID(gid); + rect = CC_RECT_PIXELS_TO_POINTS(rect); + tile = Sprite::createWithTexture(_texture, rect); + + Point p = this->getPositionAt(tileCoordinate); + tile->setAnchorPoint(Point::ZERO); + tile->setPosition(p); + tile->setPositionZ((float)getVertexZForPos(tileCoordinate)); + tile->setOpacity(this->getOpacity()); + tile->setTag(index); + this->addChild(tile, -index); + _spriteContainer.insert(std::pair >(index, std::pair(tile, gid))); + + // tile is converted to sprite. + _tiles[index] = 0; + } + } + return tile; +} + +int TMXLayer2::getTileGIDAt(const Point& tileCoordinate, TMXTileFlags* flags/* = nullptr*/) +{ + 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((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); +} + +Point TMXLayer2::getPositionAt(const Point& pos) +{ + return PointApplyTransform(pos, _tileToNodeTransform); +} + +int TMXLayer2::getVertexZForPos(const Point& pos) +{ + int ret = 0; + int maxVal = 0; + if (_useAutomaticVertexZ) + { + switch (_layerOrientation) + { + case TMXOrientationIso: + maxVal = static_cast(_layerSize.width + _layerSize.height); + ret = static_cast(-(maxVal - (pos.x + pos.y))); + break; + case TMXOrientationOrtho: + ret = static_cast(-(_layerSize.height-pos.y)); + break; + case TMXOrientationHex: + CCASSERT(0, "TMX Hexa zOrder not supported"); + break; + default: + CCASSERT(0, "TMX invalid value"); + break; + } + } + else + { + ret = _vertexZvalue; + } + + return ret; +} + +void TMXLayer2::removeTileAt(const Point& tileCoordinate) +{ + + 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"); + + uint32_t gid = this->getTileGIDAt(tileCoordinate); + + if( gid ) { + + int z = tileCoordinate.x + tileCoordinate.y * _layerSize.width; + + // remove tile from GID map + _tiles[z] = 0; + + // remove it from sprites + auto it = _spriteContainer.find(z); + if (it != _spriteContainer.end()) + { + this->removeChild(it->second.first); + } + } +} + + + +void TMXLayer2::removeChild(Node* node, bool cleanup) +{ + int tag = node->getTag(); + auto it = _spriteContainer.find(tag); + if (it != _spriteContainer.end() && it->second.first == node) + { + _spriteContainer.erase(it); + } + Node::removeChild(node, cleanup); +} + +// TMXLayer2 - Properties +Value TMXLayer2::getProperty(const std::string& propertyName) const +{ + if (_properties.find(propertyName) != _properties.end()) + return _properties.at(propertyName); + + return Value(); +} + +void TMXLayer2::parseInternalProperties() +{ + auto vertexz = getProperty("cc_vertexz"); + if (!vertexz.isNull()) + { + std::string vertexZStr = vertexz.asString(); + // If "automatic" is on, then parse the "cc_alpha_func" too + if (vertexZStr == "automatic") + { + _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(); + } + } +} + + +//CCTMXLayer2 - obtaining positions, offset +Point TMXLayer2::calculateLayerOffset(const Point& pos) +{ + Point ret = Point::ZERO; + switch (_layerOrientation) + { + case TMXOrientationOrtho: + ret = Point( pos.x * _mapTileSize.width, -pos.y *_mapTileSize.height); + break; + case TMXOrientationIso: + ret = Point((_mapTileSize.width /2) * (pos.x - pos.y), + (_mapTileSize.height /2 ) * (-pos.x - pos.y)); + break; + case TMXOrientationHex: + CCASSERT(pos.equals(Point::ZERO), "offset for hexagonal map not implemented yet"); + break; + } + return ret; +} + +//void TMXLayer2::releaseMap() +//{ +// if (_tiles) +// { +// delete [] _tiles; +// _tiles = nullptr; +// } +//} + +// TMXLayer - adding / remove tiles +void TMXLayer2::setTileGID(int gid, const Point& tileCoordinate) +{ + setTileGID(gid, tileCoordinate, (TMXTileFlags)0); +} + +void TMXLayer2::setTileGID(int gid, const Point& tileCoordinate, TMXTileFlags flags) +{ + 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, ¤tFlags); + + if (currentGID != gid || currentFlags != flags) + { + int gidAndFlags = gid | flags; + + // setting gid=0 is equal to remove the tile + if (gid == 0) + { + removeTileAt(tileCoordinate); + } + // empty tile. create a new one + else if (currentGID == 0) + { + int z = tileCoordinate.x + tileCoordinate.y * _layerSize.width; + _tiles[z] = gidAndFlags; + } + // 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()) + { + 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; + } + else + { + _tiles[z] = gidAndFlags; + } + } + } +} + +void TMXLayer2::setupTileSprite(Sprite* sprite, Point pos, int gid) +{ + sprite->setPosition(getPositionAt(pos)); + sprite->setPositionZ((float)getVertexZForPos(pos)); + sprite->setAnchorPoint(Point::ZERO); + 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(Point(0.5f,0.5f)); + sprite->setPosition(Point(getPositionAt(pos).x + sprite->getContentSize().height/2, + 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); + } + } +} + +Sprite* TMXLayer2::reusedTileWithRect(Rect rect) +{ + if (! _reusedTile) + { + _reusedTile = Sprite::createWithTexture(_texture, rect); + _reusedTile->retain(); + } + else + { + // Re-init the sprite + _reusedTile->setTextureRect(rect, false, rect.size); + } + + return _reusedTile; +} + +std::string TMXLayer2::getDescription() const +{ + return StringUtils::format("", _tag, (int)_mapTileSize.width, (int)_mapTileSize.height); +} + +NS_CC_END + diff --git a/cocos/2d/CCTMXLayer2.h b/cocos/2d/CCTMXLayer2.h new file mode 100644 index 0000000000..c2e56094e3 --- /dev/null +++ b/cocos/2d/CCTMXLayer2.h @@ -0,0 +1,261 @@ +/**************************************************************************** +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. + +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. +****************************************************************************/ +#ifndef __CCTMX_LAYER2_H__ +#define __CCTMX_LAYER2_H__ + +#include "CCTMXObjectGroup.h" +#include "CCTMXXMLParser.h" +#include "CCNode.h" +#include "renderer/CCCustomCommand.h" + +#include + +NS_CC_BEGIN + +class TMXMapInfo; +class TMXLayerInfo; +class TMXTilesetInfo; +class Texture2D; +class Sprite; +struct _ccCArray; + +/** + * @addtogroup tilemap_parallax_nodes + * @{ + */ + +/** @brief TMXLayer2 represents the TMX layer. + +It is a subclass of SpriteBatchNode. By default the tiles are rendered using a TextureAtlas. +If you modify a tile on runtime, then, that tile will become a Sprite, otherwise no Sprite objects are created. +The benefits of using Sprite objects as tiles are: +- tiles (Sprite) can be rotated/scaled/moved with a nice API + +If the layer contains a property named "cc_vertexz" with an integer (in can be positive or negative), +then all the tiles belonging to the layer will use that value as their OpenGL vertex Z for depth. + +On the other hand, if the "cc_vertexz" property has the "automatic" value, then the tiles will use an automatic vertex Z value. +Also before drawing the tiles, GL_ALPHA_TEST will be enabled, and disabled after drawing them. The used alpha func will be: + +glAlphaFunc( GL_GREATER, value ) + +"value" by default is 0, but you can change it from Tiled by adding the "cc_alpha_func" property to the layer. +The value 0 should work for most cases, but if you have tiles that are semi-transparent, then you might want to use a different +value, like 0.5. + +For further information, please see the programming guide: + +http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:tiled_maps + +@since v0.8.1 +Tiles can have tile flags for additional properties. At the moment only flip horizontal and flip vertical are used. These bit flags are defined in TMXXMLParser.h. + +@since 1.1 +*/ + +class CC_DLL TMXLayer2 : public Node +{ +public: + /** creates a TMXLayer2 with an tileset info, a layer info and a map info */ + static TMXLayer2 * create(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo); + /** + * @js ctor + */ + TMXLayer2(); + /** + * @js NA + * @lua NA + */ + virtual ~TMXLayer2(); + + /** returns the tile gid at a given tile coordinate. It also returns the tile flags. + */ + int getTileGIDAt(const Point& tileCoordinate, TMXTileFlags* flags = nullptr); + CC_DEPRECATED_ATTRIBUTE int tileGIDAt(const Point& tileCoordinate, TMXTileFlags* flags = nullptr){ + return getTileGIDAt(tileCoordinate, flags); + }; + + /** sets the tile gid (gid = tile global id) at a given tile coordinate. + The Tile GID can be obtained by using the method "tileGIDAt" or by using the TMX editor -> Tileset Mgr +1. + If a tile is already placed at that position, then it will be removed. + */ + void setTileGID(int gid, const Point& tileCoordinate); + + /** sets the tile gid (gid = tile global id) at a given tile coordinate. + The Tile GID can be obtained by using the method "tileGIDAt" or by using the TMX editor -> Tileset Mgr +1. + If a tile is already placed at that position, then it will be removed. + + Use withFlags if the tile flags need to be changed as well + */ + + void setTileGID(int gid, const Point& tileCoordinate, TMXTileFlags flags); + + /** removes a tile at given tile coordinate */ + void removeTileAt(const Point& tileCoordinate); + + /** returns the position in points of a given tile coordinate */ + Point getPositionAt(const Point& tileCoordinate); + CC_DEPRECATED_ATTRIBUTE Point positionAt(const Point& tileCoordinate) { return getPositionAt(tileCoordinate); }; + + /** return the value for the specific property name */ + Value getProperty(const std::string& propertyName) const; + CC_DEPRECATED_ATTRIBUTE Value propertyNamed(const std::string& propertyName) const { return getProperty(propertyName); }; + + /** Creates the tiles */ + void setupTiles(); + + inline const std::string& getLayerName(){ return _layerName; } + inline void setLayerName(const std::string& layerName){ _layerName = layerName; } + + /** size of the layer in tiles */ + inline const Size& getLayerSize() const { return _layerSize; }; + inline void setLayerSize(const Size& size) { _layerSize = size; }; + + /** size of the map's tile (could be different from the tile's size) */ + inline const Size& getMapTileSize() const { return _mapTileSize; }; + inline void setMapTileSize(const Size& size) { _mapTileSize = size; }; + + /** pointer to the map of tiles + * @js NA + * @lua NA + */ + uint32_t* getTiles() const { return _tiles; }; + void setTiles(uint32_t* tiles) { _tiles = tiles; }; + + /** Tileset information for the layer */ + inline TMXTilesetInfo* getTileSet() const { return _tileSet; }; + inline void setTileSet(TMXTilesetInfo* info) { + CC_SAFE_RETAIN(info); + CC_SAFE_RELEASE(_tileSet); + _tileSet = info; + }; + + /** Layer orientation, which is the same as the map orientation */ + inline int getLayerOrientation() const { return _layerOrientation; }; + inline void setLayerOrientation(int orientation) { _layerOrientation = orientation; }; + + /** properties from the layer. They can be added using Tiled */ + inline const ValueMap& getProperties() const { return _properties; }; + inline ValueMap& getProperties() { return _properties; }; + inline void setProperties(const ValueMap& properties) { + _properties = properties; + }; + + /** returns the tile (Sprite) at a given a tile coordinate. + The returned Sprite will be already added to the TMXLayer. Don't add it again. + The Sprite can be treated like any other Sprite: rotated, scaled, translated, opacity, color, etc. + You can remove either by calling: + - layer->removeChild(sprite, cleanup); + */ + Sprite* getTileAt(const Point& tileCoordinate); + + Sprite* reusedTileWithRect(Rect rect); + + Sprite* updateTileForGID(int gid, const Point& pos); + + void setupTileSprite(Sprite* sprite, Point pos, int gid); + + // + // Override + // + virtual std::string getDescription() const override; + virtual void draw(Renderer *renderer, const Mat4& transform, uint32_t flags) override; + void removeChild(Node* child, bool cleanup = true) override; + +protected: + + bool initWithTilesetInfo(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo); + int updateTiles(const Rect& culledRect, V2F_T2F_Quad *quads, GLushort *indices); + void setupVBO(); + Point calculateLayerOffset(const Point& offset); + + /* The layer recognizes some special properties, like cc_vertez */ + void parseInternalProperties(); + + Mat4 tileToNodeTransform(); + Rect tileBoundsForClipTransform(const Mat4 &tileToClip); + + void onDraw(const Mat4 &transform, bool transformUpdated); + + int getVertexZForPos(const Point& pos); + + void insertTileForGID(int gid, const Point& pos); + +protected: + CustomCommand _customCommand; + + //! name of the layer + std::string _layerName; + + /** size of the layer in tiles */ + Size _layerSize; + /** size of the map's tile (could be different from the tile's size) */ + Size _mapTileSize; + /** pointer to the map of tiles */ + uint32_t* _tiles; + /** Tileset information for the layer */ + TMXTilesetInfo* _tileSet; + /** Layer orientation, which is the same as the map orientation */ + int _layerOrientation; + /** properties from the layer. They can be added using Tiled */ + ValueMap _properties; + + Texture2D *_texture; + Sprite* _reusedTile; + + /** container for sprite children. map > */ + std::map > _spriteContainer; + + GLuint _buffersVBO[3]; //0: vertex, 1: tex coords, 2: indices + + Size _screenGridSize; + Rect _screenGridRect; + int _screenTileCount; + Rect _previousRect; + int _verticesToDraw; + + int _vertexZvalue; + bool _useAutomaticVertexZ; + + + /** tile coordinate to node coordinate transform */ + Mat4 _tileToNodeTransform; + /** quads to be rendered */ + V2F_T2F_Quad* _quads; + /** number of quads */ + int _numQuads; + /** indices */ + GLushort* _indices; +}; + +// end of tilemap_parallax_nodes group +/// @} + +NS_CC_END + +#endif //__CCTMX_LAYER2_H__ + diff --git a/cocos/2d/CCTMXTiledMap2.cpp b/cocos/2d/CCTMXTiledMap2.cpp new file mode 100644 index 0000000000..b737cafd9b --- /dev/null +++ b/cocos/2d/CCTMXTiledMap2.cpp @@ -0,0 +1,259 @@ +/**************************************************************************** +Copyright (c) 2009-2010 Ricardo Quesada +Copyright (c) 2010-2012 cocos2d-x.org +Copyright (c) 2011 Zynga Inc. +Copyright (c) 2013-2014 Chukong Technologies 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 "CCTMXTiledMap2.h" + +#include + +#include "CCTMXXMLParser.h" +#include "CCTMXLayer2.h" +#include "CCSprite.h" +#include "deprecated/CCString.h" + +NS_CC_BEGIN + +// implementation TMXTiledMap2 + +TMXTiledMap2 * TMXTiledMap2::create(const std::string& tmxFile) +{ + TMXTiledMap2 *ret = new TMXTiledMap2(); + if (ret->initWithTMXFile(tmxFile)) + { + ret->autorelease(); + return ret; + } + CC_SAFE_DELETE(ret); + return nullptr; +} + +TMXTiledMap2* TMXTiledMap2::createWithXML(const std::string& tmxString, const std::string& resourcePath) +{ + TMXTiledMap2 *ret = new TMXTiledMap2(); + if (ret->initWithXML(tmxString, resourcePath)) + { + ret->autorelease(); + return ret; + } + CC_SAFE_DELETE(ret); + return nullptr; +} + +bool TMXTiledMap2::initWithTMXFile(const std::string& tmxFile) +{ + CCASSERT(tmxFile.size()>0, "TMXTiledMap2: tmx file should not be empty"); + + setContentSize(Size::ZERO); + + TMXMapInfo *mapInfo = TMXMapInfo::create(tmxFile); + + if (! mapInfo) + { + return false; + } + CCASSERT( !mapInfo->getTilesets().empty(), "TMXTiledMap2: Map not found. Please check the filename."); + buildWithMapInfo(mapInfo); + + return true; +} + +bool TMXTiledMap2::initWithXML(const std::string& tmxString, const std::string& resourcePath) +{ + setContentSize(Size::ZERO); + + TMXMapInfo *mapInfo = TMXMapInfo::createWithXML(tmxString, resourcePath); + + CCASSERT( !mapInfo->getTilesets().empty(), "TMXTiledMap2: Map not found. Please check the filename."); + buildWithMapInfo(mapInfo); + + return true; +} + +TMXTiledMap2::TMXTiledMap2() + :_mapSize(Size::ZERO) + ,_tileSize(Size::ZERO) +{ +} + +TMXTiledMap2::~TMXTiledMap2() +{ +} + +// private +TMXLayer2 * TMXTiledMap2::parseLayer(TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo) +{ + TMXTilesetInfo *tileset = tilesetForLayer(layerInfo, mapInfo); + TMXLayer2 *layer = TMXLayer2::create(tileset, layerInfo, mapInfo); + + // tell the layerinfo to release the ownership of the tiles map. + layerInfo->_ownTiles = false; + layer->setupTiles(); + + return layer; +} + +TMXTilesetInfo * TMXTiledMap2::tilesetForLayer(TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo) +{ + Size size = layerInfo->_layerSize; + auto& tilesets = mapInfo->getTilesets(); + if (tilesets.size()>0) + { + TMXTilesetInfo* tileset = nullptr; + for (auto iter = tilesets.crbegin(); iter != tilesets.crend(); ++iter) + { + tileset = *iter; + if (tileset) + { + for( int y=0; y < size.height; y++ ) + { + for( int x=0; x < size.width; x++ ) + { + int pos = static_cast(x + size.width * y); + int gid = layerInfo->_tiles[ pos ]; + + // gid are stored in little endian. + // if host is big endian, then swap + //if( o == CFByteOrderBigEndian ) + // gid = CFSwapInt32( gid ); + /* We support little endian.*/ + + // XXX: gid == 0 --> empty tile + if( gid != 0 ) + { + // Optimization: quick return + // if the layer is invalid (more than 1 tileset per layer) an CCAssert will be thrown later + if( (gid & kTMXFlippedMask) >= tileset->_firstGid ) + return tileset; + } + } + } + } + } + } + + // If all the tiles are 0, return empty tileset + CCLOG("cocos2d: Warning: TMX Layer '%s' has no tiles", layerInfo->_name.c_str()); + return nullptr; +} + +void TMXTiledMap2::buildWithMapInfo(TMXMapInfo* mapInfo) +{ + _mapSize = mapInfo->getMapSize(); + _tileSize = mapInfo->getTileSize(); + _mapOrientation = mapInfo->getOrientation(); + + _objectGroups = mapInfo->getObjectGroups(); + + _properties = mapInfo->getProperties(); + + _tileProperties = mapInfo->getTileProperties(); + + int idx=0; + + auto& layers = mapInfo->getLayers(); + for(const auto &layerInfo : layers) { + if (layerInfo->_visible) + { + TMXLayer2 *child = parseLayer(layerInfo, mapInfo); + addChild(child, idx, idx); + + // update content size with the max size + const Size& childSize = child->getContentSize(); + Size currentSize = this->getContentSize(); + currentSize.width = std::max( currentSize.width, childSize.width ); + currentSize.height = std::max( currentSize.height, childSize.height ); + this->setContentSize(currentSize); + + idx++; + } + } +} + +// public +TMXLayer2 * TMXTiledMap2::getLayer(const std::string& layerName) const +{ + CCASSERT(layerName.size() > 0, "Invalid layer name!"); + + for (auto& child : _children) + { + TMXLayer2* layer = dynamic_cast(child); + if(layer) + { + if(layerName.compare( layer->getLayerName()) == 0) + { + return layer; + } + } + } + + // layer not found + return nullptr; +} + +TMXObjectGroup * TMXTiledMap2::getObjectGroup(const std::string& groupName) const +{ + CCASSERT(groupName.size() > 0, "Invalid group name!"); + + if (_objectGroups.size()>0) + { + TMXObjectGroup* objectGroup = nullptr; + for (auto iter = _objectGroups.cbegin(); iter != _objectGroups.cend(); ++iter) + { + objectGroup = *iter; + if (objectGroup && objectGroup->getGroupName() == groupName) + { + return objectGroup; + } + } + } + + // objectGroup not found + return nullptr; +} + +Value TMXTiledMap2::getProperty(const std::string& propertyName) const +{ + if (_properties.find(propertyName) != _properties.end()) + return _properties.at(propertyName); + + return Value(); +} + +Value TMXTiledMap2::getPropertiesForGID(int GID) const +{ + if (_tileProperties.find(GID) != _tileProperties.end()) + return _tileProperties.at(GID); + + return Value(); +} + +std::string TMXTiledMap2::getDescription() const +{ + return StringUtils::format("(_children.size())); +} + + +NS_CC_END + diff --git a/cocos/2d/CCTMXTiledMap2.h b/cocos/2d/CCTMXTiledMap2.h new file mode 100644 index 0000000000..9f1999af03 --- /dev/null +++ b/cocos/2d/CCTMXTiledMap2.h @@ -0,0 +1,222 @@ +/**************************************************************************** +Copyright (c) 2009-2010 Ricardo Quesada +Copyright (c) 2010-2012 cocos2d-x.org +Copyright (c) 2011 Zynga Inc. +Copyright (c) 2013-2014 Chukong Technologies 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. +****************************************************************************/ +#ifndef __CCTMX_TILE_MAP2_H__ +#define __CCTMX_TILE_MAP2_H__ + +#include "CCNode.h" +#include "CCTMXObjectGroup.h" +#include "CCValue.h" + +NS_CC_BEGIN + +class TMXObjectGroup; +class TMXLayer2; +class TMXLayerInfo; +class TMXTilesetInfo; +class TMXMapInfo; + +/** + * @addtogroup tilemap_parallax_nodes + * @{ + */ + +/** Possible orientations of the TMX map */ +enum +{ + /** Orthogonal orientation */ + TMXOrientationOrtho2, + + /** Hexagonal orientation */ + TMXOrientationHex2, + + /** Isometric orientation */ + TMXOrientationIso2, +}; + +/** @brief TMXTiledMap2 knows how to parse and render a TMX map. + +It adds support for the TMX tiled map format used by http://www.mapeditor.org +It supports isometric, hexagonal and orthogonal tiles. +It also supports object groups, objects, and properties. + +Features: +- Each tile will be treated as an Sprite +- The sprites are created on demand. They will be created only when you call "layer->tileAt(position)" +- Each tile can be rotated / moved / scaled / tinted / "opaqued", since each tile is a Sprite +- Tiles can be added/removed in runtime +- The z-order of the tiles can be modified in runtime +- Each tile has an anchorPoint of (0,0) +- The anchorPoint of the TMXTileMap is (0,0) +- The TMX layers will be added as a child +- The TMX layers will be aliased by default +- The tileset image will be loaded using the TextureCache +- Each tile will have a unique tag +- Each tile will have a unique z value. top-left: z=1, bottom-right: z=max z +- Each object group will be treated as an MutableArray +- Object class which will contain all the properties in a dictionary +- Properties can be assigned to the Map, Layer, Object Group, and Object + +Limitations: +- It only supports one tileset per layer. +- Embedded images are not supported +- It only supports the XML format (the JSON format is not supported) + +Technical description: +Each layer is created using an TMXLayer2 (subclass of SpriteBatchNode). If you have 5 layers, then 5 TMXLayer2 will be created, +unless the layer visibility is off. In that case, the layer won't be created at all. +You can obtain the layers (TMXLayer2 objects) at runtime by: +- map->getChildByTag(tag_number); // 0=1st layer, 1=2nd layer, 2=3rd layer, etc... +- map->getLayer(name_of_the_layer); + +Each object group is created using a TMXObjectGroup which is a subclass of MutableArray. +You can obtain the object groups at runtime by: +- map->getObjectGroup(name_of_the_object_group); + +Each object is a TMXObject. + +Each property is stored as a key-value pair in an MutableDictionary. +You can obtain the properties at runtime by: + +map->getProperty(name_of_the_property); +layer->getProperty(name_of_the_property); +objectGroup->getProperty(name_of_the_property); +object->getProperty(name_of_the_property); + +@since v0.8.1 +*/ +class CC_DLL TMXTiledMap2 : public Node +{ +public: + /** creates a TMX Tiled Map with a TMX file.*/ + static TMXTiledMap2* create(const std::string& tmxFile); + + /** initializes a TMX Tiled Map with a TMX formatted XML string and a path to TMX resources */ + static TMXTiledMap2* createWithXML(const std::string& tmxString, const std::string& resourcePath); + + /** return the TMXLayer2 for the specific layer */ + TMXLayer2* getLayer(const std::string& layerName) const; + /** + * @js NA + * @lua NA + */ + CC_DEPRECATED_ATTRIBUTE TMXLayer2* layerNamed(const std::string& layerName) const { return getLayer(layerName); }; + + /** return the TMXObjectGroup for the specific group */ + TMXObjectGroup* getObjectGroup(const std::string& groupName) const; + /** + * @js NA + * @lua NA + */ + CC_DEPRECATED_ATTRIBUTE TMXObjectGroup* objectGroupNamed(const std::string& groupName) const { return getObjectGroup(groupName); }; + + /** return the value for the specific property name */ + Value getProperty(const std::string& propertyName) const; + /** + * @js NA + * @lua NA + */ + CC_DEPRECATED_ATTRIBUTE Value propertyNamed(const char *propertyName) const { return getProperty(propertyName); }; + + /** return properties dictionary for tile GID */ + Value getPropertiesForGID(int GID) const; + CC_DEPRECATED_ATTRIBUTE Value propertiesForGID(int GID) const { return getPropertiesForGID(GID); }; + + /** the map's size property measured in tiles */ + inline const Size& getMapSize() const { return _mapSize; }; + inline void setMapSize(const Size& mapSize) { _mapSize = mapSize; }; + + /** the tiles's size property measured in pixels */ + inline const Size& getTileSize() const { return _tileSize; }; + inline void setTileSize(const Size& tileSize) { _tileSize = tileSize; }; + + /** map orientation */ + inline int getMapOrientation() const { return _mapOrientation; }; + inline void setMapOrientation(int mapOrientation) { _mapOrientation = mapOrientation; }; + + /** object groups */ + inline const Vector& getObjectGroups() const { return _objectGroups; }; + inline Vector& getObjectGroups() { return _objectGroups; }; + inline void setObjectGroups(const Vector& groups) { + _objectGroups = groups; + }; + + /** properties */ + inline ValueMap& getProperties() { return _properties; }; + inline void setProperties(const ValueMap& properties) { + _properties = properties; + }; + + virtual std::string getDescription() const override; + +protected: + /** + * @js ctor + */ + TMXTiledMap2(); + /** + * @js NA + * @lua NA + */ + virtual ~TMXTiledMap2(); + + /** initializes a TMX Tiled Map with a TMX file */ + bool initWithTMXFile(const std::string& tmxFile); + + /** initializes a TMX Tiled Map with a TMX formatted XML string and a path to TMX resources */ + bool initWithXML(const std::string& tmxString, const std::string& resourcePath); + + TMXLayer2 * parseLayer(TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo); + TMXTilesetInfo * tilesetForLayer(TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo); + void buildWithMapInfo(TMXMapInfo* mapInfo); + + /** the map's size property measured in tiles */ + Size _mapSize; + /** the tiles's size property measured in pixels */ + Size _tileSize; + /** map orientation */ + int _mapOrientation; + /** object groups */ + Vector _objectGroups; + /** properties */ + ValueMap _properties; + + //! tile properties + ValueMapIntKey _tileProperties; + +private: + CC_DISALLOW_COPY_AND_ASSIGN(TMXTiledMap2); + +}; + +// end of tilemap_parallax_nodes group +/// @} + +NS_CC_END + +#endif //__CCTMX_TILE_MAP2_H__ + + diff --git a/cocos/base/ccTypes.h b/cocos/base/ccTypes.h index f447c4de5b..5b63599b35 100644 --- a/cocos/base/ccTypes.h +++ b/cocos/base/ccTypes.h @@ -254,6 +254,15 @@ struct V3F_C4B_T2F Tex2F texCoords; // 8 bytes }; +//! a Vec2 with a vertex point, a tex coord point +struct V2F_T2F +{ + //! vertices (2F) + Vec2 vertices; + //! tex coords (2F) + Tex2F texCoords; +}; + //! A Triangle of V2F_C4B_T2F struct V2F_C4B_T2F_Triangle { @@ -304,6 +313,18 @@ struct V2F_C4F_T2F_Quad V2F_C4F_T2F tr; }; +struct V2F_T2F_Quad +{ + //! bottom left + V2F_T2F bl; + //! bottom right + V2F_T2F br; + //! top left + V2F_T2F tl; + //! top right + V2F_T2F tr; +}; + //! Blend Function used for textures struct BlendFunc { From 560053d23ca3eeee30a24dc4d3caae34ace0d3d2 Mon Sep 17 00:00:00 2001 From: boyu0 Date: Fri, 6 Jun 2014 17:33:27 +0800 Subject: [PATCH 02/40] issue #4437: add test and fix transform. --- build/cocos2d_tests.xcodeproj/project.pbxproj | 8 + cocos/2d/CCTMXLayer2.cpp | 11 +- cocos/2d/CCTMXTiledMap2.h | 1 - .../Classes/TileMapTest/TileMapTest2.cpp | 1515 +++++++++++++++++ .../Classes/TileMapTest/TileMapTest2.h | 309 ++++ tests/cpp-tests/Classes/controller.cpp | 1 + tests/cpp-tests/Classes/tests.h | 1 + 7 files changed, 1839 insertions(+), 7 deletions(-) create mode 100644 tests/cpp-tests/Classes/TileMapTest/TileMapTest2.cpp create mode 100644 tests/cpp-tests/Classes/TileMapTest/TileMapTest2.h diff --git a/build/cocos2d_tests.xcodeproj/project.pbxproj b/build/cocos2d_tests.xcodeproj/project.pbxproj index 5169d3949d..6343f01c7c 100644 --- a/build/cocos2d_tests.xcodeproj/project.pbxproj +++ b/build/cocos2d_tests.xcodeproj/project.pbxproj @@ -829,6 +829,8 @@ A07A52C31783B02C0073F6A7 /* AVFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A07A52C11783B01F0073F6A7 /* AVFoundation.framework */; }; B2507B6B192589AF00FA4972 /* Shaders3D in Resources */ = {isa = PBXBuildFile; fileRef = B2507B6A192589AF00FA4972 /* Shaders3D */; }; B2507B6C192589AF00FA4972 /* Shaders3D in Resources */ = {isa = PBXBuildFile; fileRef = B2507B6A192589AF00FA4972 /* Shaders3D */; }; + C04F935A1941B05400E9FEAB /* TileMapTest2.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C04F93581941B05400E9FEAB /* TileMapTest2.cpp */; }; + C04F935B1941B05400E9FEAB /* TileMapTest2.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C04F93581941B05400E9FEAB /* TileMapTest2.cpp */; }; C08689C118D370C90093E810 /* background.caf in Resources */ = {isa = PBXBuildFile; fileRef = C08689C018D370C90093E810 /* background.caf */; }; C08689C218D370C90093E810 /* background.caf in Resources */ = {isa = PBXBuildFile; fileRef = C08689C018D370C90093E810 /* background.caf */; }; C08689C318D370C90093E810 /* background.caf in Resources */ = {isa = PBXBuildFile; fileRef = C08689C018D370C90093E810 /* background.caf */; }; @@ -1864,6 +1866,8 @@ A07A52BB1783AEB80073F6A7 /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.1.sdk/System/Library/Frameworks/CoreGraphics.framework; sourceTree = DEVELOPER_DIR; }; A07A52C11783B01F0073F6A7 /* AVFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AVFoundation.framework; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.1.sdk/System/Library/Frameworks/AVFoundation.framework; sourceTree = DEVELOPER_DIR; }; B2507B6A192589AF00FA4972 /* Shaders3D */ = {isa = PBXFileReference; lastKnownFileType = folder; name = Shaders3D; path = "../tests/cpp-tests/Resources/Shaders3D"; sourceTree = ""; }; + C04F93581941B05400E9FEAB /* TileMapTest2.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = TileMapTest2.cpp; sourceTree = ""; }; + C04F93591941B05400E9FEAB /* TileMapTest2.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TileMapTest2.h; sourceTree = ""; }; C08689C018D370C90093E810 /* background.caf */ = {isa = PBXFileReference; lastKnownFileType = file; name = background.caf; path = "../tests/cpp-tests/Resources/background.caf"; sourceTree = ""; }; D60AE43317F7FFE100757E4B /* CoreMotion.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreMotion.framework; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS7.0.sdk/System/Library/Frameworks/CoreMotion.framework; sourceTree = DEVELOPER_DIR; }; EDCC747E17C455FD007B692C /* IOKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = IOKit.framework; path = System/Library/Frameworks/IOKit.framework; sourceTree = SDKROOT; }; @@ -3134,6 +3138,8 @@ 1AC35B0718CECF0C00F37B72 /* TileMapTest */ = { isa = PBXGroup; children = ( + C04F93581941B05400E9FEAB /* TileMapTest2.cpp */, + C04F93591941B05400E9FEAB /* TileMapTest2.h */, 1AC35B0818CECF0C00F37B72 /* TileMapTest.cpp */, 1AC35B0918CECF0C00F37B72 /* TileMapTest.h */, ); @@ -4584,6 +4590,7 @@ 29080D97191B595E0066F8DF /* CustomImageView.cpp in Sources */, 1AC35C1F18CECF0C00F37B72 /* NodeTest.cpp in Sources */, 29080D95191B595E0066F8DF /* CustomParticleWidgetTest.cpp in Sources */, + C04F935A1941B05400E9FEAB /* TileMapTest2.cpp in Sources */, 1AC35C4118CECF0C00F37B72 /* RenderTextureTest.cpp in Sources */, 29080DE3191B595E0066F8DF /* UIWidgetAddNodeTest.cpp in Sources */, 29080DBB191B595E0066F8DF /* UILoadingBarTest_Editor.cpp in Sources */, @@ -4793,6 +4800,7 @@ 1AC35B2818CECF0C00F37B72 /* ActionsTest.cpp in Sources */, 1AC35C4A18CECF0C00F37B72 /* ShaderTest.cpp in Sources */, 1AC35BFE18CECF0C00F37B72 /* Scale9SpriteTest.cpp in Sources */, + C04F935B1941B05400E9FEAB /* TileMapTest2.cpp in Sources */, 1AC35B4418CECF0C00F37B72 /* Bug-624.cpp in Sources */, 1AC35BF818CECF0C00F37B72 /* SocketIOTest.cpp in Sources */, 1AC35C5018CECF0C00F37B72 /* SpriteTest.cpp in Sources */, diff --git a/cocos/2d/CCTMXLayer2.cpp b/cocos/2d/CCTMXLayer2.cpp index cb9c798755..ea29177399 100644 --- a/cocos/2d/CCTMXLayer2.cpp +++ b/cocos/2d/CCTMXLayer2.cpp @@ -423,9 +423,9 @@ Mat4 TMXLayer2::tileToNodeTransform() _tileToNodeTransform = Mat4 ( w, 0.0f, 0.0f, 0.0f, - 0.0f, -h, 0.0f, 0.0f, + 0.0f, -h, 0.0f, offY, 0.0f, 0.0f, 1.0f, 0.0f, - 0.0f, offY, 0.0f, 1.0f + 0.0f, 0.0, 0.0f, 1.0f ); return _tileToNodeTransform; @@ -435,11 +435,10 @@ Mat4 TMXLayer2::tileToNodeTransform() float offX = (_layerSize.width - 1) * w / 2; _tileToNodeTransform = Mat4 ( - w/2, -h/2, 0.0f, 0.0f, - -w/2, -h/2, 0.0f, 0.0f, + w/2, -w/2, 0.0f, offX, + -h/2, -h/2, 0.0f, offY, 0.0f, 0.0f, 1.0f, 0.0f, - offX, offY, 0.0f, 1.0f - + 0.0f, 0.0f, 0.0f, 1.0f ); return _tileToNodeTransform; } diff --git a/cocos/2d/CCTMXTiledMap2.h b/cocos/2d/CCTMXTiledMap2.h index 9f1999af03..962ea94242 100644 --- a/cocos/2d/CCTMXTiledMap2.h +++ b/cocos/2d/CCTMXTiledMap2.h @@ -29,7 +29,6 @@ THE SOFTWARE. #include "CCNode.h" #include "CCTMXObjectGroup.h" -#include "CCValue.h" NS_CC_BEGIN diff --git a/tests/cpp-tests/Classes/TileMapTest/TileMapTest2.cpp b/tests/cpp-tests/Classes/TileMapTest/TileMapTest2.cpp new file mode 100644 index 0000000000..56e2ed8169 --- /dev/null +++ b/tests/cpp-tests/Classes/TileMapTest/TileMapTest2.cpp @@ -0,0 +1,1515 @@ +#include "TileMapTest2.h" +#include "../testResource.h" + +#include "2d/CCTMXLayer2.h" +#include "2d/CCTMXTiledMap2.h" + +namespace +{ + enum + { + kTagTileMap = 1, + }; + + Layer* nextTileMapActionNew(); + Layer* backTileMapActionNew(); + Layer* restartTileMapAction(); + + //------------------------------------------------------------------ + // + // TileDemoNew + // + //------------------------------------------------------------------ + + enum + { + IDC_NEXT = 100, + IDC_BACK, + IDC_RESTART + }; + + static int sceneIdx = -1; + +#define MAX_LAYER 27 + + static std::function createFunctions[] = { + CLN(TMXIsoZorderNew), + CLN(TMXOrthoZorderNew), + CLN(TMXIsoVertexZNew), + CLN(TMXOrthoVertexZNew), + CLN(TMXOrthoTestNew), + CLN(TMXOrthoTest2New), + CLN(TMXOrthoTest3New), + CLN(TMXOrthoTest4New), + CLN(TMXIsoTestNew), + CLN(TMXIsoTest1New), + CLN(TMXIsoTest2New), + CLN(TMXUncompressedTestNew), + CLN(TMXHexTestNew), + CLN(TMXReadWriteTestNew), + CLN(TMXTilesetTestNew), + CLN(TMXOrthoObjectsTestNew), + CLN(TMXIsoObjectsTestNew), + CLN(TMXResizeTestNew), + CLN(TMXIsoMoveLayerNew), + CLN(TMXOrthoMoveLayerNew), + CLN(TMXOrthoFlipTestNew), + CLN(TMXOrthoFlipRunTimeTestNew), + CLN(TMXOrthoFromXMLTestNew), + CLN(TMXOrthoXMLFormatTestNew), +// CLN(TileMapTestNew), +// CLN(TileMapEditTestNew), + CLN(TMXBug987New), + CLN(TMXBug787New), + CLN(TMXGIDObjectsTestNew), + + }; + + Layer* createTileMalayer(int nIndex) + { + return createFunctions[nIndex](); + } + + Layer* nextTileMapAction() + { + sceneIdx++; + sceneIdx = sceneIdx % MAX_LAYER; + + return createTileMalayer(sceneIdx); + } + + Layer* backTileMapAction() + { + sceneIdx--; + int total = MAX_LAYER; + if( sceneIdx < 0 ) + sceneIdx += total; + + return createTileMalayer(sceneIdx); + } + + Layer* restartTileMapAction() + { + return createTileMalayer(sceneIdx); + } +} + +TileDemoNew::TileDemoNew(void) +: BaseTest() +{ + auto listener = EventListenerTouchAllAtOnce::create(); + listener->onTouchesMoved = CC_CALLBACK_2(TileDemoNew::onTouchesMoved, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); +} + +TileDemoNew::~TileDemoNew(void) +{ +} + +std::string TileDemoNew::title() const +{ + return "No title"; +} + +std::string TileDemoNew::subtitle() const +{ + return "drag the screen"; +} + +void TileDemoNew::onEnter() +{ + BaseTest::onEnter(); +} + +void TileDemoNew::onExit() +{ + BaseTest::onExit(); + Director::getInstance()->setDepthTest(false); +} +void TileDemoNew::restartCallback(Ref* sender) +{ + auto s = new TileMapTestSceneNew(); + s->addChild(restartTileMapAction()); + + Director::getInstance()->replaceScene(s); + s->release(); +} + +void TileDemoNew::nextCallback(Ref* sender) +{ + auto s = new TileMapTestSceneNew(); + s->addChild( nextTileMapAction() ); + Director::getInstance()->replaceScene(s); + s->release(); +} + +void TileDemoNew::backCallback(Ref* sender) +{ + auto s = new TileMapTestSceneNew(); + s->addChild( backTileMapAction() ); + Director::getInstance()->replaceScene(s); + s->release(); +} + +void TileDemoNew::onTouchesMoved(const std::vector& touches, Event *event) +{ + auto touch = touches[0]; + + auto diff = touch->getDelta(); + auto node = getChildByTag(kTagTileMap); + auto currentPos = node->getPosition(); + node->setPosition(currentPos + diff); +} + +void TileMapTestSceneNew::runThisTest() +{ + auto layer = nextTileMapAction(); + addChild(layer); + + // fix bug #486, #419. + // "test" is the default value in Director::setGLDefaultValues() + // but TransitionTest may setDepthTest(false), we should revert it here + Director::getInstance()->setDepthTest(true); + + Director::getInstance()->replaceScene(this); +} + + +//------------------------------------------------------------------ +// +// TileMapTestNew +// +//------------------------------------------------------------------ +TileMapTestNew::TileMapTestNew() +{ + auto map = TileMapAtlas::create(s_TilesPng, s_LevelMapTga, 16, 16); + // Convert it to "alias" (GL_LINEAR filtering) + map->getTexture()->setAntiAliasTexParameters(); + + Size CC_UNUSED s = map->getContentSize(); + CCLOG("ContentSize: %f, %f", s.width,s.height); + + // If you are not going to use the Map, you can free it now + // NEW since v0.7 + map->releaseMap(); + + addChild(map, 0, kTagTileMap); + + map->setAnchorPoint( Vec2(0, 0.5f) ); + + auto scale = ScaleBy::create(4, 0.8f); + auto scaleBack = scale->reverse(); + + auto seq = Sequence::create(scale, scaleBack, NULL); + + map->runAction(RepeatForever::create(seq)); +} + +std::string TileMapTestNew::title() const +{ + return "TileMapAtlas"; +} + +//------------------------------------------------------------------ +// +// TileMapEditTestNew +// +//------------------------------------------------------------------ +TileMapEditTestNew::TileMapEditTestNew() +{ + auto map = TileMapAtlas::create(s_TilesPng, s_LevelMapTga, 16, 16); + // Create an Aliased Atlas + map->getTexture()->setAliasTexParameters(); + + Size CC_UNUSED s = map->getContentSize(); + CCLOG("ContentSize: %f, %f", s.width,s.height); + + // If you are not going to use the Map, you can free it now + // [tilemap releaseMap); + // And if you are going to use, it you can access the data with: + schedule(schedule_selector(TileMapEditTestNew::updateMap), 0.2f); + + addChild(map, 0, kTagTileMap); + + map->setAnchorPoint( Vec2(0, 0) ); + map->setPosition( Vec2(-20,-200) ); +} + +void TileMapEditTestNew::updateMap(float dt) +{ + // IMPORTANT + // The only limitation is that you cannot change an empty, or assign an empty tile to a tile + // The value 0 not rendered so don't assign or change a tile with value 0 + + auto tilemap = (TileMapAtlas*) getChildByTag(kTagTileMap); + + // + // For example you can iterate over all the tiles + // using this code, but try to avoid the iteration + // over all your tiles in every frame. It's very expensive + // for(int x=0; x < tilemap.tgaInfo->width; x++) { + // for(int y=0; y < tilemap.tgaInfo->height; y++) { + // Color3B c =[tilemap tileAt:Size(x,y)); + // if( c.r != 0 ) { + // ////----CCLOG("%d,%d = %d", x,y,c.r); + // } + // } + // } + + // NEW since v0.7 + Color3B c = tilemap->getTileAt(Vec2(13,21)); + c.r++; + c.r %= 50; + if( c.r==0) + c.r=1; + + // NEW since v0.7 + tilemap->setTile(c, Vec2(13,21) ); +} + +std::string TileMapEditTestNew::title() const +{ + return "Editable TileMapAtlas"; +} + +//------------------------------------------------------------------ +// +// TMXOrthoTestNew +// +//------------------------------------------------------------------ +TMXOrthoTestNew::TMXOrthoTestNew() +{ + // + // Test orthogonal with 3d camera and anti-alias textures + // + // it should not flicker. No artifacts should appear + // + //auto color = LayerColor::create( Color4B(64,64,64,255) ); + //addChild(color, -1); + + auto map = TMXTiledMap2::create("TileMaps/orthogonal-test2.tmx"); + + addChild(map, 0, kTagTileMap); + + Size CC_UNUSED s = map->getContentSize(); + CCLOG("ContentSize: %f, %f", s.width,s.height); + + auto scale = ScaleBy::create(10, 0.1f); + auto back = scale->reverse(); + auto seq = Sequence::create(scale, back, NULL); + auto repeat = RepeatForever::create(seq); + map->runAction(repeat); + +// float x, y, z; +// map->getCamera()->getEye(&x, &y, &z); +// map->getCamera()->setEye(x-200, y, z+300); +} + +void TMXOrthoTestNew::onEnter() +{ + TileDemoNew::onEnter(); + + Director::getInstance()->setProjection(Director::Projection::_3D); +} + +void TMXOrthoTestNew::onExit() +{ + Director::getInstance()->setProjection(Director::Projection::DEFAULT); + TileDemoNew::onExit(); +} + +std::string TMXOrthoTestNew::title() const +{ + return "TMX Orthogonal test"; +} + +//------------------------------------------------------------------ +// +// TMXOrthoTest2New +// +//------------------------------------------------------------------ +TMXOrthoTest2New::TMXOrthoTest2New() +{ + auto map = TMXTiledMap2::create("TileMaps/orthogonal-test1.tmx"); + addChild(map, 0, kTagTileMap); + + Size CC_UNUSED s = map->getContentSize(); + CCLOG("ContentSize: %f, %f", s.width,s.height); + + map->runAction( ScaleBy::create(2, 0.5f) ) ; +} + +std::string TMXOrthoTest2New::title() const +{ + return "TMX Ortho test2"; +} + +//------------------------------------------------------------------ +// +// TMXOrthoTest3New +// +//------------------------------------------------------------------ +TMXOrthoTest3New::TMXOrthoTest3New() +{ + auto map = TMXTiledMap2::create("TileMaps/orthogonal-test3.tmx"); + addChild(map, 0, kTagTileMap); + + Size CC_UNUSED s = map->getContentSize(); + CCLOG("ContentSize: %f, %f", s.width,s.height); + + map->setScale(0.2f); + map->setAnchorPoint( Vec2(0.5f, 0.5f) ); +} + +std::string TMXOrthoTest3New::title() const +{ + return "TMX anchorPoint test"; +} + +//------------------------------------------------------------------ +// +// TMXOrthoTest4New +// +//------------------------------------------------------------------ +TMXOrthoTest4New::TMXOrthoTest4New() +{ + auto map = TMXTiledMap2::create("TileMaps/orthogonal-test4.tmx"); + addChild(map, 0, kTagTileMap); + + Size CC_UNUSED s1 = map->getContentSize(); + CCLOG("ContentSize: %f, %f", s1.width,s1.height); + + map->setAnchorPoint(Vec2(0, 0)); + + auto layer = map->getLayer("Layer 0"); + auto s = layer->getLayerSize(); + + Sprite* sprite; + sprite = layer->getTileAt(Vec2(0,0)); + sprite->setScale(2); + sprite = layer->getTileAt(Vec2(s.width-1,0)); + sprite->setScale(2); + sprite = layer->getTileAt(Vec2(0,s.height-1)); + sprite->setScale(2); + sprite = layer->getTileAt(Vec2(s.width-1,s.height-1)); + sprite->setScale(2); + + schedule( schedule_selector(TMXOrthoTest4New::removeSprite), 2 ); + +} + +void TMXOrthoTest4New::removeSprite(float dt) +{ + unschedule(schedule_selector(TMXOrthoTest4New::removeSprite)); + + auto map = static_cast( getChildByTag(kTagTileMap) ); + auto layer = map->getLayer("Layer 0"); + auto s = layer->getLayerSize(); + + auto sprite = layer->getTileAt( Vec2(s.width-1,0) ); + auto sprite2 = layer->getTileAt(Vec2(s.width-1, s.height-1)); + layer->removeChild(sprite, true); + auto sprite3 = layer->getTileAt(Vec2(2, s.height-1)); + layer->removeChild(sprite3, true); + layer->removeChild(sprite2, true); +} + +std::string TMXOrthoTest4New::title() const +{ + return "TMX width/height test"; +} + +//------------------------------------------------------------------ +// +// TMXReadWriteTestNew +// +//------------------------------------------------------------------ +enum +{ + SID_UPDATECOL = 100, + SID_REPAINTWITHGID, + SID_REMOVETILES +}; + +TMXReadWriteTestNew::TMXReadWriteTestNew() +{ + _gid = 0; + + auto map = TMXTiledMap2::create("TileMaps/orthogonal-test2.tmx"); + addChild(map, 0, kTagTileMap); + + Size CC_UNUSED s = map->getContentSize(); + CCLOG("ContentSize: %f, %f", s.width,s.height); + + + auto layer = map->getLayer("Layer 0"); + + map->setScale( 1 ); + + auto tile0 = layer->getTileAt(Vec2(1,63)); + auto tile1 = layer->getTileAt(Vec2(2,63)); + auto tile2 = layer->getTileAt(Vec2(3,62));//Vec2(1,62)); + auto tile3 = layer->getTileAt(Vec2(2,62)); + tile0->setAnchorPoint( Vec2(0.5f, 0.5f) ); + tile1->setAnchorPoint( Vec2(0.5f, 0.5f) ); + tile2->setAnchorPoint( Vec2(0.5f, 0.5f) ); + tile3->setAnchorPoint( Vec2(0.5f, 0.5f) ); + + auto move = MoveBy::create(0.5f, Vec2(0,160)); + auto rotate = RotateBy::create(2, 360); + auto scale = ScaleBy::create(2, 5); + auto opacity = FadeOut::create(2); + auto fadein = FadeIn::create(2); + auto scaleback = ScaleTo::create(1, 1); + auto finish = CallFuncN::create(CC_CALLBACK_1(TMXReadWriteTestNew::removeSprite, this)); + auto seq0 = Sequence::create(move, rotate, scale, opacity, fadein, scaleback, finish, NULL); + auto seq1 = seq0->clone(); + auto seq2 = seq0->clone(); + auto seq3 = seq0->clone(); + + tile0->runAction(seq0); + tile1->runAction(seq1); + tile2->runAction(seq2); + tile3->runAction(seq3); + + + _gid = layer->getTileGIDAt(Vec2(0,63)); + ////----CCLOG("Tile GID at:(0,63) is: %d", _gid); + + schedule(schedule_selector(TMXReadWriteTestNew::updateCol), 2.0f); + schedule(schedule_selector(TMXReadWriteTestNew::repaintWithGID), 2.05f); + schedule(schedule_selector(TMXReadWriteTestNew::removeTiles), 1.0f); + + ////----CCLOG("++++atlas quantity: %d", layer->textureAtlas()->getTotalQuads()); + ////----CCLOG("++++children: %d", layer->getChildren()->count() ); + + _gid2 = 0; +} + +void TMXReadWriteTestNew::removeSprite(Node* sender) +{ + ////----CCLOG("removing tile: %x", sender); + auto p = ((Node*)sender)->getParent(); + + if (p) + { + p->removeChild((Node*)sender, true); + } + + //////----CCLOG("atlas quantity: %d", p->textureAtlas()->totalQuads()); +} + +void TMXReadWriteTestNew::updateCol(float dt) +{ + auto map = (TMXTiledMap2*)getChildByTag(kTagTileMap); + auto layer = (TMXLayer2*)map->getChildByTag(0); + + ////----CCLOG("++++atlas quantity: %d", layer->textureAtlas()->getTotalQuads()); + ////----CCLOG("++++children: %d", layer->getChildren()->count() ); + + + auto s = layer->getLayerSize(); + + for( int y=0; y< s.height; y++ ) + { + layer->setTileGID(_gid2, Vec2((float)3, (float)y)); + } + + _gid2 = (_gid2 + 1) % 80; +} + +void TMXReadWriteTestNew::repaintWithGID(float dt) +{ +// unschedule:_cmd); + + auto map = (TMXTiledMap2*)getChildByTag(kTagTileMap); + auto layer = (TMXLayer2*)map->getChildByTag(0); + + auto s = layer->getLayerSize(); + for( int x=0; xgetTileGIDAt( Vec2((float)x, (float)y) ); + layer->setTileGID(tmpgid+1, Vec2((float)x, (float)y)); + } +} + +void TMXReadWriteTestNew::removeTiles(float dt) +{ + unschedule(schedule_selector(TMXReadWriteTestNew::removeTiles)); + + auto map = (TMXTiledMap2*)getChildByTag(kTagTileMap); + auto layer = (TMXLayer2*)map->getChildByTag(0); + auto s = layer->getLayerSize(); + + for( int y=0; y< s.height; y++ ) + { + layer->removeTileAt( Vec2(5.0, (float)y) ); + } +} + + + +std::string TMXReadWriteTestNew::title() const +{ + return "TMX Read/Write test"; +} + +//------------------------------------------------------------------ +// +// TMXHexTestNew +// +//------------------------------------------------------------------ +TMXHexTestNew::TMXHexTestNew() +{ + auto color = LayerColor::create( Color4B(64,64,64,255) ); + addChild(color, -1); + + auto map = TMXTiledMap2::create("TileMaps/hexa-test.tmx"); + addChild(map, 0, kTagTileMap); + + Size CC_UNUSED s = map->getContentSize(); + CCLOG("ContentSize: %f, %f", s.width,s.height); +} + +std::string TMXHexTestNew::title() const +{ + return "TMX Hex tes"; +} + +//------------------------------------------------------------------ +// +// TMXIsoTestNew +// +//------------------------------------------------------------------ +TMXIsoTestNew::TMXIsoTestNew() +{ + auto color = LayerColor::create( Color4B(64,64,64,255) ); + addChild(color, -1); + + auto map = TMXTiledMap2::create("TileMaps/iso-test.tmx"); + addChild(map, 0, kTagTileMap); + + // move map to the center of the screen + auto ms = map->getMapSize(); + auto ts = map->getTileSize(); + map->runAction( MoveTo::create(1.0f, Vec2( -ms.width * ts.width/2, -ms.height * ts.height/2 )) ); +} + +std::string TMXIsoTestNew::title() const +{ + return "TMX Isometric test 0"; +} + +//------------------------------------------------------------------ +// +// TMXIsoTest1New +// +//------------------------------------------------------------------ +TMXIsoTest1New::TMXIsoTest1New() +{ + auto color = LayerColor::create( Color4B(64,64,64,255) ); + addChild(color, -1); + + auto map = TMXTiledMap2::create("TileMaps/iso-test1.tmx"); + addChild(map, 0, kTagTileMap); + + Size CC_UNUSED s = map->getContentSize(); + CCLOG("ContentSize: %f, %f", s.width,s.height); + + map->setAnchorPoint(Vec2(0.5f, 0.5f)); +} + +std::string TMXIsoTest1New::title() const +{ + return "TMX Isometric test + anchorPoint"; +} + +//------------------------------------------------------------------ +// +// TMXIsoTest2New +// +//------------------------------------------------------------------ +TMXIsoTest2New::TMXIsoTest2New() +{ + auto color = LayerColor::create( Color4B(64,64,64,255) ); + addChild(color, -1); + + auto map = TMXTiledMap2::create("TileMaps/iso-test2.tmx"); + addChild(map, 0, kTagTileMap); + + Size CC_UNUSED s = map->getContentSize(); + CCLOG("ContentSize: %f, %f", s.width,s.height); + + // move map to the center of the screen + auto ms = map->getMapSize(); + auto ts = map->getTileSize(); + map->runAction( MoveTo::create(1.0f, Vec2( -ms.width * ts.width/2, -ms.height * ts.height/2 ) )); +} + +std::string TMXIsoTest2New::title() const +{ + return "TMX Isometric test 2"; +} + +//------------------------------------------------------------------ +// +// TMXUncompressedTestNew +// +//------------------------------------------------------------------ +TMXUncompressedTestNew::TMXUncompressedTestNew() +{ + auto color = LayerColor::create( Color4B(64,64,64,255) ); + addChild(color, -1); + + auto map = TMXTiledMap2::create("TileMaps/iso-test2-uncompressed.tmx"); + addChild(map, 0, kTagTileMap); + + Size CC_UNUSED s = map->getContentSize(); + CCLOG("ContentSize: %f, %f", s.width,s.height); + + // move map to the center of the screen + auto ms = map->getMapSize(); + auto ts = map->getTileSize(); + map->runAction(MoveTo::create(1.0f, Vec2( -ms.width * ts.width/2, -ms.height * ts.height/2 ) )); + + //unsupported +// // testing release map +// TMXLayer2* layer; +// +// auto& children = map->getChildren(); +// for(const auto &node : children) { +// layer= static_cast(node); +// layer->releaseMap(); +// } + +} + +std::string TMXUncompressedTestNew::title() const +{ + return "TMX Uncompressed test"; +} + +//------------------------------------------------------------------ +// +// TMXTilesetTestNew +// +//------------------------------------------------------------------ +TMXTilesetTestNew::TMXTilesetTestNew() +{ + auto map = TMXTiledMap2::create("TileMaps/orthogonal-test5.tmx"); + addChild(map, 0, kTagTileMap); + + Size CC_UNUSED s = map->getContentSize(); + CCLOG("ContentSize: %f, %f", s.width,s.height); +} + +std::string TMXTilesetTestNew::title() const +{ + return "TMX Tileset test"; +} + +//------------------------------------------------------------------ +// +// TMXOrthoObjectsTestNew +// +//------------------------------------------------------------------ +TMXOrthoObjectsTestNew::TMXOrthoObjectsTestNew() +{ + auto map = TMXTiledMap2::create("TileMaps/ortho-objects.tmx"); + addChild(map, -1, kTagTileMap); + + Size CC_UNUSED s = map->getContentSize(); + CCLOG("ContentSize: %f, %f", s.width,s.height); + + auto group = map->getObjectGroup("Object Group 1"); + auto& objects = group->getObjects(); + + Value objectsVal = Value(objects); + CCLOG("%s", objectsVal.getDescription().c_str()); +} + +void TMXOrthoObjectsTestNew::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) +{ + _renderCmd.init(_globalZOrder); + _renderCmd.func = CC_CALLBACK_0(TMXOrthoObjectsTestNew::onDraw, this, transform, flags); + renderer->addCommand(&_renderCmd); +} + +void TMXOrthoObjectsTestNew::onDraw(const Mat4 &transform, uint32_t flags) +{ + Director* director = Director::getInstance(); + CCASSERT(nullptr != director, "Director is null when seting matrix stack"); + director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); + director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, transform); + + auto map = static_cast( getChildByTag(kTagTileMap) ); + auto pos = map->getPosition(); + auto group = map->getObjectGroup("Object Group 1"); + + auto& objects = group->getObjects(); + + for (auto& obj : objects) + { + ValueMap& dict = obj.asValueMap(); + + float x = dict["x"].asFloat(); + float y = dict["y"].asFloat(); + float width = dict["width"].asFloat(); + float height = dict["height"].asFloat(); + + glLineWidth(3); + + DrawPrimitives::drawLine( pos + Vec2(x, y), pos + Vec2((x+width), y) ); + DrawPrimitives::drawLine( pos + Vec2((x+width), y), pos + Vec2((x+width), (y+height)) ); + DrawPrimitives::drawLine( pos + Vec2((x+width), (y+height)), pos + Vec2(x, (y+height)) ); + DrawPrimitives::drawLine( pos + Vec2(x, (y+height)), pos + Vec2(x, y) ); + + glLineWidth(1); + } + + director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); +} + +std::string TMXOrthoObjectsTestNew::title() const +{ + return "TMX Ortho object test"; +} + +std::string TMXOrthoObjectsTestNew::subtitle() const +{ + return "You should see a white box around the 3 platforms"; +} + + +//------------------------------------------------------------------ +// +// TMXIsoObjectsTestNew +// +//------------------------------------------------------------------ + +TMXIsoObjectsTestNew::TMXIsoObjectsTestNew() +{ + auto map = TMXTiledMap2::create("TileMaps/iso-test-objectgroup.tmx"); + addChild(map, -1, kTagTileMap); + + Size CC_UNUSED s = map->getContentSize(); + CCLOG("ContentSize: %f, %f", s.width,s.height); + + auto group = map->getObjectGroup("Object Group 1"); + + auto& objects = group->getObjects(); + + Value objectsVal = Value(objects); + CCLOG("%s", objectsVal.getDescription().c_str()); +} + +void TMXIsoObjectsTestNew::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) +{ + _renderCmd.init(_globalZOrder); + _renderCmd.func = CC_CALLBACK_0(TMXIsoObjectsTestNew::onDraw, this, transform, flags); + renderer->addCommand(&_renderCmd); +} + +void TMXIsoObjectsTestNew::onDraw(const Mat4 &transform, uint32_t flags) +{ + Director* director = Director::getInstance(); + CCASSERT(nullptr != director, "Director is null when seting matrix stack"); + director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); + director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, transform); + + auto map = (TMXTiledMap2*) getChildByTag(kTagTileMap); + auto pos = map->getPosition(); + auto group = map->getObjectGroup("Object Group 1"); + + auto& objects = group->getObjects(); + for (auto& obj : objects) + { + ValueMap& dict = obj.asValueMap(); + float x = dict["x"].asFloat(); + float y = dict["y"].asFloat(); + float width = dict["width"].asFloat(); + float height = dict["height"].asFloat(); + + glLineWidth(3); + + DrawPrimitives::drawLine( pos + Vec2(x,y), pos + Vec2(x+width,y) ); + DrawPrimitives::drawLine( pos + Vec2(x+width,y), pos + Vec2(x+width,y+height) ); + DrawPrimitives::drawLine( pos + Vec2(x+width,y+height), pos + Vec2(x,y+height) ); + DrawPrimitives::drawLine( pos + Vec2(x,y+height), pos + Vec2(x,y) ); + + glLineWidth(1); + } + + director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); +} + +std::string TMXIsoObjectsTestNew::title() const +{ + return "TMX Iso object test"; +} + +std::string TMXIsoObjectsTestNew::subtitle() const +{ + return "You need to parse them manually. See bug #810"; +} + + +//------------------------------------------------------------------ +// +// TMXResizeTestNew +// +//------------------------------------------------------------------ + +TMXResizeTestNew::TMXResizeTestNew() +{ + auto map = TMXTiledMap2::create("TileMaps/orthogonal-test5.tmx"); + addChild(map, 0, kTagTileMap); + + Size CC_UNUSED s = map->getContentSize(); + CCLOG("ContentSize: %f, %f", s.width,s.height); + + TMXLayer2* layer; + layer = map->getLayer("Layer 0"); + + auto ls = layer->getLayerSize(); + for (unsigned int y = 0; y < ls.height; y++) + { + for (unsigned int x = 0; x < ls.width; x++) + { + layer->setTileGID(1, Vec2( x, y ) ); + } + } +} + +std::string TMXResizeTestNew::title() const +{ + return "TMX resize test"; +} + +std::string TMXResizeTestNew::subtitle() const +{ + return "Should not crash. Testing issue #740"; +} + + +//------------------------------------------------------------------ +// +// TMXIsoZorderNew +// +//------------------------------------------------------------------ +TMXIsoZorderNew::TMXIsoZorderNew() +{ + auto map = TMXTiledMap2::create("TileMaps/iso-test-zorder.tmx"); + addChild(map, 0, kTagTileMap); + + auto s = map->getContentSize(); + CCLOG("ContentSize: %f, %f", s.width,s.height); + map->setPosition(Vec2(-s.width/2,0)); + + _tamara = Sprite::create(s_pathSister1); + map->addChild(_tamara, (int)map->getChildren().size() ); + _tamara->retain(); + int mapWidth = map->getMapSize().width * map->getTileSize().width; + _tamara->setPosition(CC_POINT_PIXELS_TO_POINTS(Vec2( mapWidth/2,0))); + _tamara->setAnchorPoint(Vec2(0.5f,0)); + + + auto move = MoveBy::create(10, Vec2(300,250)); + auto back = move->reverse(); + auto seq = Sequence::create(move, back,NULL); + _tamara->runAction( RepeatForever::create(seq) ); + + schedule( schedule_selector(TMXIsoZorderNew::repositionSprite) ); +} + +TMXIsoZorderNew::~TMXIsoZorderNew() +{ + _tamara->release(); +} + +void TMXIsoZorderNew::onExit() +{ + unschedule(schedule_selector(TMXIsoZorderNew::repositionSprite)); + TileDemoNew::onExit(); +} + +void TMXIsoZorderNew::repositionSprite(float dt) +{ + auto p = _tamara->getPosition(); + p = CC_POINT_POINTS_TO_PIXELS(p); + auto map = getChildByTag(kTagTileMap); + + // there are only 4 layers. (grass and 3 trees layers) + // if tamara < 48, z=4 + // if tamara < 96, z=3 + // if tamara < 144,z=2 + + int newZ = 4 - (p.y / 48); + newZ = std::max(newZ,0); + + map->reorderChild(_tamara, newZ); +} + +std::string TMXIsoZorderNew::title() const +{ + return "TMX Iso Zorder"; +} + +std::string TMXIsoZorderNew::subtitle() const +{ + return "Sprite should hide behind the trees"; +} + + +//------------------------------------------------------------------ +// +// TMXOrthoZorderNew +// +//------------------------------------------------------------------ +TMXOrthoZorderNew::TMXOrthoZorderNew() +{ + auto map = TMXTiledMap2::create("TileMaps/orthogonal-test-zorder.tmx"); + addChild(map, 0, kTagTileMap); + + Size CC_UNUSED s = map->getContentSize(); + CCLOG("ContentSize: %f, %f", s.width,s.height); + + _tamara = Sprite::create(s_pathSister1); + map->addChild(_tamara, (int)map->getChildren().size()); + _tamara->retain(); + _tamara->setAnchorPoint(Vec2(0.5f,0)); + + + auto move = MoveBy::create(10, Vec2(400,450)); + auto back = move->reverse(); + auto seq = Sequence::create(move, back,NULL); + _tamara->runAction( RepeatForever::create(seq)); + + schedule( schedule_selector(TMXOrthoZorderNew::repositionSprite)); +} + +TMXOrthoZorderNew::~TMXOrthoZorderNew() +{ + _tamara->release(); +} + +void TMXOrthoZorderNew::repositionSprite(float dt) +{ + auto p = _tamara->getPosition(); + p = CC_POINT_POINTS_TO_PIXELS(p); + auto map = getChildByTag(kTagTileMap); + + // there are only 4 layers. (grass and 3 trees layers) + // if tamara < 81, z=4 + // if tamara < 162, z=3 + // if tamara < 243,z=2 + + // -10: customization for this particular sample + int newZ = 4 - ( (p.y-10) / 81); + newZ = std::max(newZ,0); + + map->reorderChild(_tamara, newZ); +} + +std::string TMXOrthoZorderNew::title() const +{ + return "TMX Ortho Zorder"; +} + +std::string TMXOrthoZorderNew::subtitle() const +{ + return "Sprite should hide behind the trees"; +} + + +//------------------------------------------------------------------ +// +// TMXIsoVertexZNew +// +//------------------------------------------------------------------ +TMXIsoVertexZNew::TMXIsoVertexZNew() +{ + auto map = TMXTiledMap2::create("TileMaps/iso-test-vertexz.tmx"); + addChild(map, 0, kTagTileMap); + + auto s = map->getContentSize(); + map->setPosition( Vec2(-s.width/2,0) ); + CCLOG("ContentSize: %f, %f", s.width,s.height); + + // because I'm lazy, I'm reusing a tile as an sprite, but since this method uses vertexZ, you + // can use any Sprite and it will work OK. + auto layer = map->getLayer("Trees"); + _tamara = layer->getTileAt( Vec2(29,29) ); + _tamara->retain(); + + auto move = MoveBy::create(10, Vec2(300,250) * (1/CC_CONTENT_SCALE_FACTOR())); + auto back = move->reverse(); + auto seq = Sequence::create(move, back,NULL); + _tamara->runAction( RepeatForever::create(seq) ); + + schedule( schedule_selector(TMXIsoVertexZNew::repositionSprite)); + +} + +TMXIsoVertexZNew::~TMXIsoVertexZNew() +{ + _tamara->release(); +} + +void TMXIsoVertexZNew::repositionSprite(float dt) +{ + // tile height is 64x32 + // map size: 30x30 + auto p = _tamara->getPosition(); + p = CC_POINT_POINTS_TO_PIXELS(p); + float newZ = -(p.y+32) /16; + _tamara->setPositionZ( newZ ); +} + +void TMXIsoVertexZNew::onEnter() +{ + TileDemoNew::onEnter(); + + // TIP: 2d projection should be used + Director::getInstance()->setProjection(Director::Projection::_2D); + Director::getInstance()->setDepthTest(true); +} + +void TMXIsoVertexZNew::onExit() +{ + // At exit use any other projection. + Director::getInstance()->setProjection(Director::Projection::DEFAULT); + Director::getInstance()->setDepthTest(false); + TileDemoNew::onExit(); +} + +std::string TMXIsoVertexZNew::title() const +{ + return "TMX Iso VertexZ"; +} + +std::string TMXIsoVertexZNew::subtitle() const +{ + return "Sprite should hide behind the trees"; +} + + +//------------------------------------------------------------------ +// +// TMXOrthoVertexZNew +// +//------------------------------------------------------------------ +TMXOrthoVertexZNew::TMXOrthoVertexZNew() +{ + auto map = TMXTiledMap2::create("TileMaps/orthogonal-test-vertexz.tmx"); + addChild(map, 0, kTagTileMap); + + Size CC_UNUSED s = map->getContentSize(); + CCLOG("ContentSize: %f, %f", s.width,s.height); + + // because I'm lazy, I'm reusing a tile as an sprite, but since this method uses vertexZ, you + // can use any Sprite and it will work OK. + auto layer = map->getLayer("trees"); + _tamara = layer->getTileAt(Vec2(0,11)); + CCLOG("%p vertexZ: %f", _tamara, _tamara->getPositionZ()); + _tamara->retain(); + + auto move = MoveBy::create(10, Vec2(400,450) * (1/CC_CONTENT_SCALE_FACTOR())); + auto back = move->reverse(); + auto seq = Sequence::create(move, back,NULL); + _tamara->runAction( RepeatForever::create(seq)); + + schedule(schedule_selector(TMXOrthoVertexZNew::repositionSprite)); + +} + +TMXOrthoVertexZNew::~TMXOrthoVertexZNew() +{ + _tamara->release(); +} + +void TMXOrthoVertexZNew::repositionSprite(float dt) +{ + // tile height is 101x81 + // map size: 12x12 + auto p = _tamara->getPosition(); + p = CC_POINT_POINTS_TO_PIXELS(p); + _tamara->setPositionZ( -( (p.y+81) /81) ); +} + +void TMXOrthoVertexZNew::onEnter() +{ + TileDemoNew::onEnter(); + + // TIP: 2d projection should be used + Director::getInstance()->setProjection(Director::Projection::_2D); + Director::getInstance()->setDepthTest(true); +} + +void TMXOrthoVertexZNew::onExit() +{ + // At exit use any other projection. + Director::getInstance()->setProjection(Director::Projection::DEFAULT); + Director::getInstance()->setDepthTest(false); + TileDemoNew::onExit(); +} + +std::string TMXOrthoVertexZNew::title() const +{ + return "TMX Ortho vertexZ"; +} + +std::string TMXOrthoVertexZNew::subtitle() const +{ + return "Sprite should hide behind the trees"; +} + + +//------------------------------------------------------------------ +// +// TMXIsoMoveLayerNew +// +//------------------------------------------------------------------ +TMXIsoMoveLayerNew::TMXIsoMoveLayerNew() +{ + auto map = TMXTiledMap2::create("TileMaps/iso-test-movelayer.tmx"); + addChild(map, 0, kTagTileMap); + + map->setPosition(Vec2(-700,-50)); + + Size CC_UNUSED s = map->getContentSize(); + CCLOG("ContentSize: %f, %f", s.width,s.height); +} + +std::string TMXIsoMoveLayerNew::title() const +{ + return "TMX Iso Move Layer"; +} + +std::string TMXIsoMoveLayerNew::subtitle() const +{ + return "Trees should be horizontally aligned"; +} + + +//------------------------------------------------------------------ +// +// TMXOrthoMoveLayerNew +// +//------------------------------------------------------------------ +TMXOrthoMoveLayerNew::TMXOrthoMoveLayerNew() +{ + auto map = TMXTiledMap2::create("TileMaps/orthogonal-test-movelayer.tmx"); + addChild(map, 0, kTagTileMap); + + Size CC_UNUSED s = map->getContentSize(); + CCLOG("ContentSize: %f, %f", s.width,s.height); +} + +std::string TMXOrthoMoveLayerNew::title() const +{ + return "TMX Ortho Move Layer"; +} + +std::string TMXOrthoMoveLayerNew::subtitle() const +{ + return "Trees should be horizontally aligned"; +} + +//------------------------------------------------------------------ +// +// TMXTilePropertyTestNew +// +//------------------------------------------------------------------ + +TMXTilePropertyTestNew::TMXTilePropertyTestNew() +{ + auto map = TMXTiledMap2::create("TileMaps/ortho-tile-property.tmx"); + addChild(map ,0 ,kTagTileMap); + + for(int i=1;i<=20;i++){ + for(const auto& value : map->getPropertiesForGID(i).asValueMap()) + { + log("GID:%i, Properties:%s, %s", i, value.first.c_str(), value.second.asString().c_str()); + } + } +} + +std::string TMXTilePropertyTestNew::title() const +{ + return "TMX Tile Property Test"; +} + +std::string TMXTilePropertyTestNew::subtitle() const +{ + return "In the console you should see tile properties"; +} + +//------------------------------------------------------------------ +// +// TMXOrthoFlipTestNew +// +//------------------------------------------------------------------ + +TMXOrthoFlipTestNew::TMXOrthoFlipTestNew() +{ + auto map = TMXTiledMap2::create("TileMaps/ortho-rotation-test.tmx"); + addChild(map, 0, kTagTileMap); + + Size CC_UNUSED s = map->getContentSize(); + log("ContentSize: %f, %f", s.width,s.height); + + auto action = ScaleBy::create(2, 0.5f); + map->runAction(action); +} + +std::string TMXOrthoFlipTestNew::title() const +{ + return "TMX tile flip test"; +} + +//------------------------------------------------------------------ +// +// TMXOrthoFlipRunTimeTestNew +// +//------------------------------------------------------------------ + +TMXOrthoFlipRunTimeTestNew::TMXOrthoFlipRunTimeTestNew() +{ + auto map = TMXTiledMap2::create("TileMaps/ortho-rotation-test.tmx"); + addChild(map, 0, kTagTileMap); + + auto s = map->getContentSize(); + log("ContentSize: %f, %f", s.width,s.height); + + auto action = ScaleBy::create(2, 0.5f); + map->runAction(action); + + schedule(schedule_selector(TMXOrthoFlipRunTimeTestNew::flipIt), 1.0f); +} + +std::string TMXOrthoFlipRunTimeTestNew::title() const +{ + return "TMX tile flip run time test"; +} + +std::string TMXOrthoFlipRunTimeTestNew::subtitle() const +{ + return "in 2 sec bottom left tiles will flip"; +} + +void TMXOrthoFlipRunTimeTestNew::flipIt(float dt) +{ + auto map = (TMXTiledMap2*) getChildByTag(kTagTileMap); + auto layer = map->getLayer("Layer 0"); + + //blue diamond + auto tileCoord = Vec2(1,10); + int flags; + unsigned int GID = layer->getTileGIDAt(tileCoord, (TMXTileFlags*)&flags); + // Vertical + if( flags & kTMXTileVerticalFlag ) + flags &= ~kTMXTileVerticalFlag; + else + flags |= kTMXTileVerticalFlag; + layer->setTileGID(GID ,tileCoord, (TMXTileFlags)flags); + + + tileCoord = Vec2(1,8); + GID = layer->getTileGIDAt(tileCoord, (TMXTileFlags*)&flags); + // Vertical + if( flags & kTMXTileVerticalFlag ) + flags &= ~kTMXTileVerticalFlag; + else + flags |= kTMXTileVerticalFlag; + layer->setTileGID(GID ,tileCoord, (TMXTileFlags)flags); + + + tileCoord = Vec2(2,8); + GID = layer->getTileGIDAt(tileCoord, (TMXTileFlags*)&flags); + // Horizontal + if( flags & kTMXTileHorizontalFlag ) + flags &= ~kTMXTileHorizontalFlag; + else + flags |= kTMXTileHorizontalFlag; + layer->setTileGID(GID, tileCoord, (TMXTileFlags)flags); +} +//------------------------------------------------------------------ +// +// TMXOrthoFromXMLTestNew +// +//------------------------------------------------------------------ + +TMXOrthoFromXMLTestNew::TMXOrthoFromXMLTestNew() +{ + std::string resources = "TileMaps"; // partial paths are OK as resource paths. + std::string file = resources + "/orthogonal-test1.tmx"; + + auto str = String::createWithContentsOfFile(FileUtils::getInstance()->fullPathForFilename(file.c_str()).c_str()); + CCASSERT(str != NULL, "Unable to open file"); + + auto map = TMXTiledMap2::createWithXML(str->getCString() ,resources.c_str()); + addChild(map, 0, kTagTileMap); + + auto s = map->getContentSize(); + log("ContentSize: %f, %f", s.width,s.height); + + auto action = ScaleBy::create(2, 0.5f); + map->runAction(action); +} + +std::string TMXOrthoFromXMLTestNew::title() const +{ + return "TMX created from XML test"; +} +//------------------------------------------------------------------ +// +// TMXOrthoXMLFormatTestNew +// +//------------------------------------------------------------------ + +TMXOrthoXMLFormatTestNew::TMXOrthoXMLFormatTestNew() +{ + // this test tests for: + // 1. load xml format tilemap + // 2. gid lower than firstgid is ignored + // 3. firstgid in tsx is ignored, tile property in tsx loaded correctly. + auto map = TMXTiledMap2::create("TileMaps/xml-test.tmx"); + addChild(map, 0, kTagTileMap); + + auto s = map->getContentSize(); + log("ContentSize: %f, %f", s.width,s.height); + + for(int i=24;i<=26;i++){ + log("GID:%i, Properties:%s", i, map->getPropertiesForGID(i).asValueMap()["name"].asString().c_str()); + } + + auto action = ScaleBy::create(2, 0.5f); + map->runAction(action); +} + +std::string TMXOrthoXMLFormatTestNew::title() const +{ + return "you should see blue, green and yellow in console."; +} + +//------------------------------------------------------------------ +// +// TMXBug987New +// +//------------------------------------------------------------------ +TMXBug987New::TMXBug987New() +{ + auto map = TMXTiledMap2::create("TileMaps/orthogonal-test6.tmx"); + addChild(map, 0, kTagTileMap); + + Size CC_UNUSED s1 = map->getContentSize(); + CCLOG("ContentSize: %f, %f", s1.width,s1.height); + + map->setAnchorPoint(Vec2(0, 0)); + auto layer = map->getLayer("Tile Layer 1"); + layer->setTileGID(3, Vec2(2,2)); +} + +std::string TMXBug987New::title() const +{ + return "TMX Bug 987"; +} + +std::string TMXBug987New::subtitle() const +{ + return "You should see an square"; +} + +//------------------------------------------------------------------ +// +// TMXBug787New +// +//------------------------------------------------------------------ +TMXBug787New::TMXBug787New() +{ + auto map = TMXTiledMap2::create("TileMaps/iso-test-bug787.tmx"); + addChild(map, 0, kTagTileMap); + + map->setScale(0.25f); +} + +std::string TMXBug787New::title() const +{ + return "TMX Bug 787"; +} + +std::string TMXBug787New::subtitle() const +{ + return "You should see a map"; +} + +//------------------------------------------------------------------ +// +// TMXGIDObjectsTestNew +// +//------------------------------------------------------------------ +TMXGIDObjectsTestNew::TMXGIDObjectsTestNew() +{ + auto map = TMXTiledMap2::create("TileMaps/test-object-layer.tmx"); + addChild(map, -1, kTagTileMap); + + Size CC_UNUSED s = map->getContentSize(); + CCLOG("Contentsize: %f, %f", s.width, s.height); + + CCLOG("----> Iterating over all the group objets"); + //auto group = map->objectGroupNamed("Object Layer 1"); + +} + +void TMXGIDObjectsTestNew::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) +{ + _renderCmd.init(_globalZOrder); + _renderCmd.func = CC_CALLBACK_0(TMXGIDObjectsTestNew::onDraw, this, transform, flags); + renderer->addCommand(&_renderCmd); +} + +void TMXGIDObjectsTestNew::onDraw(const Mat4 &transform, uint32_t flags) +{ + Director* director = Director::getInstance(); + CCASSERT(nullptr != director, "Director is null when seting matrix stack"); + director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); + director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, transform); + + auto map = (TMXTiledMap2*)getChildByTag(kTagTileMap); + auto pos = map->getPosition(); + auto group = map->getObjectGroup("Object Layer 1"); + + auto& objects = group->getObjects(); + for (auto& obj : objects) + { + ValueMap& dict = obj.asValueMap(); + + float x = dict["x"].asFloat(); + float y = dict["y"].asFloat(); + float width = dict["width"].asFloat(); + float height = dict["height"].asFloat(); + + glLineWidth(3); + + DrawPrimitives::drawLine(pos + Vec2(x, y), pos + Vec2(x + width, y)); + DrawPrimitives::drawLine(pos + Vec2(x + width, y), pos + Vec2(x + width, y + height)); + DrawPrimitives::drawLine(pos + Vec2(x + width,y + height), pos + Vec2(x,y + height)); + DrawPrimitives::drawLine(pos + Vec2(x,y + height), pos + Vec2(x,y)); + + glLineWidth(1); + } + + director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); +} + +std::string TMXGIDObjectsTestNew::title() const +{ + return "TMX GID objects"; +} + +std::string TMXGIDObjectsTestNew::subtitle() const +{ + return "Tiles are created from an object group"; +} diff --git a/tests/cpp-tests/Classes/TileMapTest/TileMapTest2.h b/tests/cpp-tests/Classes/TileMapTest/TileMapTest2.h new file mode 100644 index 0000000000..1f7a98a3e9 --- /dev/null +++ b/tests/cpp-tests/Classes/TileMapTest/TileMapTest2.h @@ -0,0 +1,309 @@ +#ifndef _TILEMAP_TEST_NEW_H_ +#define _TILEMAP_TEST_NEW_H_ + +#include "../testBasic.h" +#include "../BaseTest.h" + +class TileDemoNew : public BaseTest +{ +public: + TileDemoNew(void); + virtual ~TileDemoNew(void); + + virtual std::string title() const override; + virtual std::string subtitle() const override; + virtual void onEnter() override; + virtual void onExit()override; + + void restartCallback(Ref* sender); + void nextCallback(Ref* sender); + void backCallback(Ref* sender); + + void onTouchesMoved(const std::vector& touches, Event *event); +}; + +class TileMapTestNew : public TileDemoNew +{ +public: + TileMapTestNew(void); + virtual std::string title() const override; +}; + +class TileMapEditTestNew : public TileDemoNew +{ +public: + TileMapEditTestNew (void); + virtual std::string title() const override; + + void updateMap(float dt); +}; + +class TMXOrthoTestNew : public TileDemoNew +{ +public: + TMXOrthoTestNew(void); + virtual std::string title() const override; + + virtual void onEnter() override; + virtual void onExit() override; +}; + +class TMXOrthoTest2New : public TileDemoNew +{ +public: + TMXOrthoTest2New(void); + virtual std::string title() const override; +}; + +class TMXOrthoTest3New : public TileDemoNew +{ +public: + TMXOrthoTest3New(void); + virtual std::string title() const override; +}; + +class TMXOrthoTest4New : public TileDemoNew +{ +public: + TMXOrthoTest4New(void); + void removeSprite(float dt); + virtual std::string title() const override; +}; + +class TMXReadWriteTestNew : public TileDemoNew +{ + unsigned int _gid; + unsigned int _gid2; +public: + TMXReadWriteTestNew(void); + virtual std::string title() const override; + + void removeSprite(Node* sender); + void updateCol(float dt); + void repaintWithGID(float dt); + void removeTiles(float dt); +}; + +class TMXHexTestNew : public TileDemoNew +{ +public: + TMXHexTestNew(void); + virtual std::string title() const override; +}; + +class TMXIsoTestNew : public TileDemoNew +{ +public: + TMXIsoTestNew(void); + virtual std::string title() const override; +}; + +class TMXIsoTest1New : public TileDemoNew +{ +public: + TMXIsoTest1New(void); + virtual std::string title() const override; +}; + +class TMXIsoTest2New : public TileDemoNew +{ +public: + TMXIsoTest2New(void); + virtual std::string title() const override; +}; + +class TMXUncompressedTestNew : public TileDemoNew +{ +public: + TMXUncompressedTestNew(void); + virtual std::string title() const override; +}; + +class TMXTilesetTestNew : public TileDemoNew +{ +public: + TMXTilesetTestNew(void); + virtual std::string title() const override; +}; + + + +class TMXOrthoObjectsTestNew : public TileDemoNew +{ +public: + TMXOrthoObjectsTestNew(void); + virtual std::string title() const override; + + virtual void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) override; + virtual std::string subtitle() const override; +protected: + CustomCommand _renderCmd; + void onDraw(const Mat4 &transform, uint32_t flags); +}; + +class TMXIsoObjectsTestNew : public TileDemoNew +{ +public: + TMXIsoObjectsTestNew(void); + virtual std::string title() const override; + + virtual void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) override; + virtual std::string subtitle() const override; +protected: + CustomCommand _renderCmd; + void onDraw(const Mat4 &transform, uint32_t flags); +}; + +class TMXResizeTestNew : public TileDemoNew +{ +public: + TMXResizeTestNew(void); + virtual std::string title() const override; + + virtual std::string subtitle() const override; +}; + +class TMXIsoZorderNew : public TileDemoNew +{ + Sprite* _tamara; +public: + TMXIsoZorderNew(void); + virtual std::string title() const override; + virtual std::string subtitle() const override; + virtual void onExit(void); + + ~TMXIsoZorderNew(); + void repositionSprite(float dt); +}; + +class TMXOrthoZorderNew : public TileDemoNew +{ + Sprite* _tamara; +public: + TMXOrthoZorderNew(void); + virtual std::string title() const override; + virtual std::string subtitle() const override; + + virtual ~TMXOrthoZorderNew(); + void repositionSprite(float dt); +}; + +class TMXIsoVertexZNew : public TileDemoNew +{ + Sprite* _tamara; +public: + TMXIsoVertexZNew(void); + virtual std::string title() const override; + virtual std::string subtitle() const override; + + ~TMXIsoVertexZNew(); + void repositionSprite(float dt); + virtual void onEnter() override; + virtual void onExit() override; +}; + +class TMXOrthoVertexZNew : public TileDemoNew +{ + Sprite* _tamara; +public: + TMXOrthoVertexZNew(void); + virtual std::string title() const override; + virtual std::string subtitle() const override; + + ~TMXOrthoVertexZNew(); + void repositionSprite(float dt); + virtual void onEnter() override; + virtual void onExit() override; +}; + +class TMXIsoMoveLayerNew : public TileDemoNew +{ +public: + TMXIsoMoveLayerNew(void); + virtual std::string title() const override; + virtual std::string subtitle() const override; +}; + +class TMXOrthoMoveLayerNew : public TileDemoNew +{ +public: + TMXOrthoMoveLayerNew(void); + virtual std::string title() const override; + virtual std::string subtitle() const override; +}; + +class TMXTilePropertyTestNew : public TileDemoNew +{ +public: + TMXTilePropertyTestNew(); + virtual std::string title() const override; + virtual std::string subtitle() const override; +}; + +class TMXOrthoFlipTestNew : public TileDemoNew +{ +public: + TMXOrthoFlipTestNew(); + virtual std::string title() const override; +}; + +class TMXOrthoFlipRunTimeTestNew : public TileDemoNew +{ +public: + TMXOrthoFlipRunTimeTestNew(); + virtual std::string title() const override; + virtual std::string subtitle() const override; + void flipIt(float dt); +}; + +class TMXOrthoFromXMLTestNew : public TileDemoNew +{ +public: + TMXOrthoFromXMLTestNew(); + virtual std::string title() const override; +}; + +class TMXOrthoXMLFormatTestNew : public TileDemoNew +{ +public: + TMXOrthoXMLFormatTestNew(); + virtual std::string title() const override; +}; + +class TMXBug987New : public TileDemoNew +{ +public: + TMXBug987New(); + virtual std::string title() const override; + virtual std::string subtitle() const override; +}; + +class TMXBug787New : public TileDemoNew +{ +public: + TMXBug787New(); + virtual std::string title() const override; + virtual std::string subtitle() const override; +}; + +class TMXGIDObjectsTestNew : public TileDemoNew +{ +public: + TMXGIDObjectsTestNew(); + virtual std::string title() const override; + virtual std::string subtitle() const override; + virtual void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) override; + +protected: + CustomCommand _renderCmd; + void onDraw(const Mat4 &transform, uint32_t flags); + +}; + +class TileMapTestSceneNew : public TestScene +{ +public: + virtual void runThisTest(); +}; + +#endif diff --git a/tests/cpp-tests/Classes/controller.cpp b/tests/cpp-tests/Classes/controller.cpp index 519a049d7e..b447c4b170 100644 --- a/tests/cpp-tests/Classes/controller.cpp +++ b/tests/cpp-tests/Classes/controller.cpp @@ -85,6 +85,7 @@ Controller g_aTestNames[] = { { "Node: Sprite", [](){return new SpriteTestScene(); } }, { "Node: Sprite3D", [](){ return new Sprite3DTestScene(); }}, { "Node: TileMap", [](){return new TileMapTestScene(); } }, + { "Node: TileMapNew", [](){return new TileMapTestSceneNew(); } }, { "Node: Text Input", [](){return new TextInputTestScene(); } }, { "Node: UI", [](){ return new UITestScene(); }}, { "Mouse", []() { return new MouseTestScene(); } }, diff --git a/tests/cpp-tests/Classes/tests.h b/tests/cpp-tests/Classes/tests.h index 1a552f9bbc..6c1eb0c85d 100644 --- a/tests/cpp-tests/Classes/tests.h +++ b/tests/cpp-tests/Classes/tests.h @@ -22,6 +22,7 @@ #include "SceneTest/SceneTest.h" #include "ParallaxTest/ParallaxTest.h" #include "TileMapTest/TileMapTest.h" +#include "TileMapTest/TileMapTest2.h" #include "IntervalTest/IntervalTest.h" #include "LabelTest/LabelTest.h" #include "LabelTest/LabelTestNew.h" From fc5e58b52147e2374d9d12f630601baf23e9b9f3 Mon Sep 17 00:00:00 2001 From: boyu0 Date: Fri, 6 Jun 2014 18:03:44 +0800 Subject: [PATCH 03/40] issue #4437: add dirty tag. --- cocos/2d/CCTMXLayer2.cpp | 65 +++++++++---------- cocos/2d/CCTMXLayer2.h | 3 +- .../Classes/TileMapTest/TileMapTest2.cpp | 4 +- 3 files changed, 33 insertions(+), 39 deletions(-) diff --git a/cocos/2d/CCTMXLayer2.cpp b/cocos/2d/CCTMXLayer2.cpp index ea29177399..e2fd09bc3f 100644 --- a/cocos/2d/CCTMXLayer2.cpp +++ b/cocos/2d/CCTMXLayer2.cpp @@ -116,13 +116,13 @@ TMXLayer2::TMXLayer2() ,_layerOrientation(TMXOrientationOrtho) ,_texture(nullptr) ,_reusedTile(nullptr) -,_previousRect(0,0,0,0) ,_verticesToDraw(0) ,_vertexZvalue(0) ,_useAutomaticVertexZ(false) ,_quads(nullptr) ,_indices(nullptr) ,_numQuads(0) +,_dirty(false) {} TMXLayer2::~TMXLayer2() @@ -152,7 +152,7 @@ void TMXLayer2::onDraw(const Mat4 &transform, bool transformUpdated) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]); - if( transformUpdated ) { + if( transformUpdated || _dirty ) { Size s = Director::getInstance()->getWinSize(); auto rect = Rect(0, 0, s.width, s.height); @@ -161,32 +161,29 @@ void TMXLayer2::onDraw(const Mat4 &transform, bool transformUpdated) inv.inverse(); rect = RectApplyTransform(rect, inv); - if( !rect.equals(_previousRect) ) + if (Configuration::getInstance()->supportsShareableVAO()) { - if (Configuration::getInstance()->supportsShareableVAO()) + V2F_T2F_Quad* quads = (V2F_T2F_Quad*)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY); + GLushort* indices = (GLushort *)glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY); + _verticesToDraw = updateTiles(rect, quads, indices); + glUnmapBuffer(GL_ARRAY_BUFFER); + glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER); + } + else + { + _verticesToDraw = updateTiles(rect, nullptr, nullptr); + + if (_quads != nullptr && _indices != nullptr && _verticesToDraw > 0) { - V2F_T2F_Quad* quads = (V2F_T2F_Quad*)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY); - GLushort* indices = (GLushort *)glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY); - _verticesToDraw = updateTiles(rect, quads, indices); - glUnmapBuffer(GL_ARRAY_BUFFER); - glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER); + glBufferData(GL_ARRAY_BUFFER, sizeof(_quads[0]) * _numQuads , _quads, GL_DYNAMIC_DRAW); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(_indices[0]) * _numQuads * 6 , _indices, GL_STATIC_DRAW); } - else - { - _verticesToDraw = updateTiles(rect, nullptr, nullptr); - - if (_quads != nullptr && _indices != nullptr && _verticesToDraw > 0) - { - glBufferData(GL_ARRAY_BUFFER, sizeof(_quads[0]) * _numQuads , _quads, GL_DYNAMIC_DRAW); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(_indices[0]) * _numQuads * 6 , _indices, GL_STATIC_DRAW); - } - } - - _previousRect = rect; } // don't draw more than 65535 vertices since we are using GL_UNSIGNED_SHORT for indices _verticesToDraw = std::min(_verticesToDraw, 65535); + + _dirty = false; } if(_verticesToDraw > 0) { @@ -491,7 +488,7 @@ Sprite* TMXLayer2::getTileAt(const Point& tileCoordinate) _spriteContainer.insert(std::pair >(index, std::pair(tile, gid))); // tile is converted to sprite. - _tiles[index] = 0; + setTileForGID(index, 0); } } return tile; @@ -563,16 +560,15 @@ void TMXLayer2::removeTileAt(const Point& tileCoordinate) { 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"); - uint32_t gid = this->getTileGIDAt(tileCoordinate); + int gid = this->getTileGIDAt(tileCoordinate); if( gid ) { int z = tileCoordinate.x + tileCoordinate.y * _layerSize.width; // remove tile from GID map - _tiles[z] = 0; + setTileForGID(z, 0); // remove it from sprites auto it = _spriteContainer.find(z); @@ -583,6 +579,12 @@ void TMXLayer2::removeTileAt(const Point& tileCoordinate) } } +void TMXLayer2::setTileForGID(int index, int gid) +{ + _tiles[index] = gid; + _dirty = true; +} + void TMXLayer2::removeChild(Node* node, bool cleanup) @@ -656,15 +658,6 @@ Point TMXLayer2::calculateLayerOffset(const Point& pos) return ret; } -//void TMXLayer2::releaseMap() -//{ -// if (_tiles) -// { -// delete [] _tiles; -// _tiles = nullptr; -// } -//} - // TMXLayer - adding / remove tiles void TMXLayer2::setTileGID(int gid, const Point& tileCoordinate) { @@ -693,7 +686,7 @@ void TMXLayer2::setTileGID(int gid, const Point& tileCoordinate, TMXTileFlags fl else if (currentGID == 0) { int z = tileCoordinate.x + tileCoordinate.y * _layerSize.width; - _tiles[z] = gidAndFlags; + setTileForGID(z, gidAndFlags); } // modifying an existing tile with a non-empty tile else @@ -717,7 +710,7 @@ void TMXLayer2::setTileGID(int gid, const Point& tileCoordinate, TMXTileFlags fl } else { - _tiles[z] = gidAndFlags; + setTileForGID(z, gidAndFlags); } } } diff --git a/cocos/2d/CCTMXLayer2.h b/cocos/2d/CCTMXLayer2.h index c2e56094e3..d677492a26 100644 --- a/cocos/2d/CCTMXLayer2.h +++ b/cocos/2d/CCTMXLayer2.h @@ -204,6 +204,7 @@ protected: int getVertexZForPos(const Point& pos); void insertTileForGID(int gid, const Point& pos); + void setTileForGID(int index, int gid); protected: CustomCommand _customCommand; @@ -235,7 +236,6 @@ protected: Size _screenGridSize; Rect _screenGridRect; int _screenTileCount; - Rect _previousRect; int _verticesToDraw; int _vertexZvalue; @@ -250,6 +250,7 @@ protected: int _numQuads; /** indices */ GLushort* _indices; + bool _dirty; }; // end of tilemap_parallax_nodes group diff --git a/tests/cpp-tests/Classes/TileMapTest/TileMapTest2.cpp b/tests/cpp-tests/Classes/TileMapTest/TileMapTest2.cpp index 56e2ed8169..3d13a31005 100644 --- a/tests/cpp-tests/Classes/TileMapTest/TileMapTest2.cpp +++ b/tests/cpp-tests/Classes/TileMapTest/TileMapTest2.cpp @@ -1091,7 +1091,7 @@ std::string TMXIsoVertexZNew::title() const std::string TMXIsoVertexZNew::subtitle() const { - return "Sprite should hide behind the trees"; + return "Doesn't support yet"; } @@ -1162,7 +1162,7 @@ std::string TMXOrthoVertexZNew::title() const std::string TMXOrthoVertexZNew::subtitle() const { - return "Sprite should hide behind the trees"; + return "Doesn't support yet"; } From dff0fada287e46cf123b063610b898c2066488f8 Mon Sep 17 00:00:00 2001 From: boyu0 Date: Mon, 9 Jun 2014 11:36:44 +0800 Subject: [PATCH 04/40] issue #4437: fix some bugs. --- cocos/2d/CCTMXLayer2.cpp | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/cocos/2d/CCTMXLayer2.cpp b/cocos/2d/CCTMXLayer2.cpp index e2fd09bc3f..72dadd0791 100644 --- a/cocos/2d/CCTMXLayer2.cpp +++ b/cocos/2d/CCTMXLayer2.cpp @@ -243,14 +243,14 @@ int TMXLayer2::updateTiles(const Rect& culledRect, V2F_T2F_Quad *quads, GLushort indicesTmp = _indices; } - int rows_per_tile = ceil(tileSize.height / mapTileSize.height) - 1; + int tiles_overflow = ceil(tileSize.height / mapTileSize.height) - 1; Size texSize = _tileSet->_imageSize; - for (int y = visibleTiles.origin.y - rows_per_tile; y <= visibleTiles.origin.y + visibleTiles.size.height; ++y) + for (int y = visibleTiles.origin.y; y <= visibleTiles.origin.y + visibleTiles.size.height + tiles_overflow; ++y) { if(y<0 || y >= _layerSize.height) continue; - for (int x = visibleTiles.origin.x; x <= visibleTiles.origin.x + visibleTiles.size.width; x++) + for (int x = visibleTiles.origin.x - tiles_overflow; x <= visibleTiles.origin.x + visibleTiles.size.width; x++) { if(x<0 || x >= _layerSize.width) continue; @@ -258,7 +258,8 @@ int TMXLayer2::updateTiles(const Rect& culledRect, V2F_T2F_Quad *quads, GLushort int tileGID = _tiles[x + y * (int)_layerSize.width]; // GID==0 empty tile - if(tileGID!=0) { + if(tileGID!=0) + { V2F_T2F_Quad *quad = &quadsTmp[tilesUsed]; @@ -268,10 +269,20 @@ int TMXLayer2::updateTiles(const Rect& culledRect, V2F_T2F_Quad *quads, GLushort float left, right, top, bottom; // vertices - left = nodePos.x; - right = nodePos.x + tileSize.width; - bottom = nodePos.y + tileSize.height; - top = nodePos.y; + 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); From 3975f55de7436f52b9bd66d4f3cc1050c1f26c16 Mon Sep 17 00:00:00 2001 From: boyu0 Date: Mon, 9 Jun 2014 15:28:49 +0800 Subject: [PATCH 05/40] issue #4437: fix crash when doesn't support VAO, and make calculation more precisely. --- cocos/2d/CCTMXLayer2.cpp | 48 +++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/cocos/2d/CCTMXLayer2.cpp b/cocos/2d/CCTMXLayer2.cpp index 72dadd0791..ba6367906d 100644 --- a/cocos/2d/CCTMXLayer2.cpp +++ b/cocos/2d/CCTMXLayer2.cpp @@ -152,7 +152,8 @@ void TMXLayer2::onDraw(const Mat4 &transform, bool transformUpdated) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]); - if( transformUpdated || _dirty ) { + if( transformUpdated || _dirty ) + { Size s = Director::getInstance()->getWinSize(); auto rect = Rect(0, 0, s.width, s.height); @@ -199,7 +200,7 @@ void TMXLayer2::onDraw(const Mat4 &transform, bool transformUpdated) // color glVertexAttrib4f(GLProgram::VERTEX_ATTRIB_COLOR, _displayedColor.r/255.0f, _displayedColor.g/255.0f, _displayedColor.b/255.0f, _displayedOpacity/255.0f); - + glDrawElements(GL_TRIANGLES, _verticesToDraw, GL_UNSIGNED_SHORT, nullptr); CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,_verticesToDraw); } @@ -216,22 +217,48 @@ int TMXLayer2::updateTiles(const Rect& culledRect, V2F_T2F_Quad *quads, GLushort Rect visibleTiles = culledRect; Size mapTileSize = CC_SIZE_PIXELS_TO_POINTS(_mapTileSize); Size tileSize = CC_SIZE_PIXELS_TO_POINTS(_tileSet->_tileSize); - Mat4 nodeToTile = _tileToNodeTransform; - nodeToTile.inverse(); + Mat4 nodeToTileTransform = _tileToNodeTransform; + nodeToTileTransform.inverse(); //transform to tile - visibleTiles = RectApplyTransform(visibleTiles, nodeToTile); + visibleTiles = RectApplyTransform(visibleTiles, nodeToTileTransform); + // 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); visibleTiles.origin.x = floor(visibleTiles.origin.x); visibleTiles.origin.y = floor(visibleTiles.origin.y); - visibleTiles.size.width = ceil(visibleTiles.size.width); - visibleTiles.size.height = ceil(visibleTiles.size.height); V2F_T2F_Quad* quadsTmp = quads; GLushort* indicesTmp = indices; + + // for the bigger tiles. + int tilesOverX = 0; + int tilesOverY = 0; + // for diagonal oriention tiles + float tileSizeMax = std::max(tileSize.width, tileSize.height); + if (_layerOrientation == TMXOrientationOrtho) + { + 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 == TMXOrientationIso) + { + 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); + } + // doesn't support VBO if (quadsTmp == nullptr) { - int quadsNeed = std::min(static_cast((visibleTiles.size.width + 1) * (visibleTiles.size.height + 1)), MAX_QUADS_COUNT); + int quadsNeed = std::min(static_cast((visibleTiles.size.width + tilesOverX) * (visibleTiles.size.height + tilesOverY)), MAX_QUADS_COUNT); if (_numQuads < quadsNeed) { _numQuads = quadsNeed; @@ -243,14 +270,13 @@ int TMXLayer2::updateTiles(const Rect& culledRect, V2F_T2F_Quad *quads, GLushort indicesTmp = _indices; } - int tiles_overflow = ceil(tileSize.height / mapTileSize.height) - 1; Size texSize = _tileSet->_imageSize; - for (int y = visibleTiles.origin.y; y <= visibleTiles.origin.y + visibleTiles.size.height + tiles_overflow; ++y) + for (int y = visibleTiles.origin.y; y < visibleTiles.origin.y + visibleTiles.size.height + tilesOverY; ++y) { if(y<0 || y >= _layerSize.height) continue; - for (int x = visibleTiles.origin.x - tiles_overflow; x <= visibleTiles.origin.x + visibleTiles.size.width; x++) + for (int x = visibleTiles.origin.x - tilesOverX; x < visibleTiles.origin.x + visibleTiles.size.width; ++x) { if(x<0 || x >= _layerSize.width) continue; From ee919b673f76c11d16808d573d6f5b49ea6ed58b Mon Sep 17 00:00:00 2001 From: boyu0 Date: Fri, 13 Jun 2014 11:07:21 +0800 Subject: [PATCH 06/40] issue #4437: config for android and linux. --- cocos/2d/CCTMXLayer2.cpp | 45 +++++++++++++++++----------------- cocos/2d/CMakeLists.txt | 2 ++ cocos/Android.mk | 2 ++ tests/cpp-tests/Android.mk | 1 + tests/cpp-tests/CMakeLists.txt | 1 + 5 files changed, 28 insertions(+), 23 deletions(-) diff --git a/cocos/2d/CCTMXLayer2.cpp b/cocos/2d/CCTMXLayer2.cpp index ba6367906d..9410c4ad9b 100644 --- a/cocos/2d/CCTMXLayer2.cpp +++ b/cocos/2d/CCTMXLayer2.cpp @@ -36,14 +36,13 @@ THE SOFTWARE. */ #include "CCTMXLayer2.h" #include "CCTMXXMLParser.h" -#include "CCTMXTiledMap.h" -#include "CCSprite.h" -#include "CCTextureCache.h" -#include "CCGLProgramCache.h" -#include "CCGLProgram.h" -#include "ccCArray.h" -#include "CCDirector.h" -#include "CCConfiguration.h" +#include "CCTMXTiledMap2.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" #include "renderer/CCRenderer.h" #include "deprecated/CCString.h" @@ -113,7 +112,7 @@ TMXLayer2::TMXLayer2() ,_mapTileSize(Size::ZERO) ,_tiles(nullptr) ,_tileSet(nullptr) -,_layerOrientation(TMXOrientationOrtho) +,_layerOrientation(TMXOrientationOrtho2) ,_texture(nullptr) ,_reusedTile(nullptr) ,_verticesToDraw(0) @@ -236,7 +235,7 @@ int TMXLayer2::updateTiles(const Rect& culledRect, V2F_T2F_Quad *quads, GLushort int tilesOverY = 0; // for diagonal oriention tiles float tileSizeMax = std::max(tileSize.width, tileSize.height); - if (_layerOrientation == TMXOrientationOrtho) + if (_layerOrientation == TMXOrientationOrtho2) { tilesOverX = ceil(tileSizeMax / mapTileSize.width) - 1; tilesOverY = ceil(tileSizeMax / mapTileSize.height) - 1; @@ -244,7 +243,7 @@ int TMXLayer2::updateTiles(const Rect& culledRect, V2F_T2F_Quad *quads, GLushort if (tilesOverX < 0) tilesOverX = 0; if (tilesOverY < 0) tilesOverY = 0; } - else if(_layerOrientation == TMXOrientationIso) + else if(_layerOrientation == TMXOrientationIso2) { Rect overTileRect(0, 0, tileSizeMax - mapTileSize.width, tileSizeMax - mapTileSize.height); if (overTileRect.size.width < 0) overTileRect.size.width = 0; @@ -424,18 +423,18 @@ void TMXLayer2::setupTiles() switch (_layerOrientation) { - case TMXOrientationOrtho: + case TMXOrientationOrtho2: _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 TMXOrientationIso: + case TMXOrientationIso2: _screenGridSize.width = ceil(screenSize.width / _mapTileSize.width) + 2; _screenGridSize.height = ceil(screenSize.height / (_mapTileSize.height/2)) + 4; break; - case TMXOrientationHex: + case TMXOrientationHex2: break; } @@ -452,7 +451,7 @@ Mat4 TMXLayer2::tileToNodeTransform() switch(_layerOrientation) { - case TMXOrientationOrtho: + case TMXOrientationOrtho2: { _tileToNodeTransform = Mat4 ( @@ -464,7 +463,7 @@ Mat4 TMXLayer2::tileToNodeTransform() return _tileToNodeTransform; } - case TMXOrientationIso: + case TMXOrientationIso2: { float offX = (_layerSize.width - 1) * w / 2; _tileToNodeTransform = Mat4 @@ -476,7 +475,7 @@ Mat4 TMXLayer2::tileToNodeTransform() ); return _tileToNodeTransform; } - case TMXOrientationHex: + case TMXOrientationHex2: { _tileToNodeTransform = Mat4::IDENTITY; return _tileToNodeTransform; @@ -570,14 +569,14 @@ int TMXLayer2::getVertexZForPos(const Point& pos) { switch (_layerOrientation) { - case TMXOrientationIso: + case TMXOrientationIso2: maxVal = static_cast(_layerSize.width + _layerSize.height); ret = static_cast(-(maxVal - (pos.x + pos.y))); break; - case TMXOrientationOrtho: + case TMXOrientationOrtho2: ret = static_cast(-(_layerSize.height-pos.y)); break; - case TMXOrientationHex: + case TMXOrientationHex2: CCASSERT(0, "TMX Hexa zOrder not supported"); break; default: @@ -681,14 +680,14 @@ Point TMXLayer2::calculateLayerOffset(const Point& pos) Point ret = Point::ZERO; switch (_layerOrientation) { - case TMXOrientationOrtho: + case TMXOrientationOrtho2: ret = Point( pos.x * _mapTileSize.width, -pos.y *_mapTileSize.height); break; - case TMXOrientationIso: + case TMXOrientationIso2: ret = Point((_mapTileSize.width /2) * (pos.x - pos.y), (_mapTileSize.height /2 ) * (-pos.x - pos.y)); break; - case TMXOrientationHex: + case TMXOrientationHex2: CCASSERT(pos.equals(Point::ZERO), "offset for hexagonal map not implemented yet"); break; } diff --git a/cocos/2d/CMakeLists.txt b/cocos/2d/CMakeLists.txt index 630aa69782..d5d7a0b044 100644 --- a/cocos/2d/CMakeLists.txt +++ b/cocos/2d/CMakeLists.txt @@ -65,8 +65,10 @@ set(COCOS_2D_SRC 2d/CCTextFieldTTF.cpp 2d/CCTileMapAtlas.cpp 2d/CCTMXLayer.cpp + 2d/CCTMXLayer2.cpp 2d/CCTMXObjectGroup.cpp 2d/CCTMXTiledMap.cpp + 2d/CCTMXTiledMap2.cpp 2d/CCTMXXMLParser.cpp 2d/CCTransition.cpp 2d/CCTransitionPageTurn.cpp diff --git a/cocos/Android.mk b/cocos/Android.mk index 1112b4abd9..eb8bc29f2a 100644 --- a/cocos/Android.mk +++ b/cocos/Android.mk @@ -62,8 +62,10 @@ cocos2d.cpp \ 2d/CCSpriteFrame.cpp \ 2d/CCSpriteFrameCache.cpp \ 2d/CCTMXLayer.cpp \ +2d/CCTMXLayer2.cpp \ 2d/CCTMXObjectGroup.cpp \ 2d/CCTMXTiledMap.cpp \ +2d/CCTMXTiledMap2.cpp \ 2d/CCTMXXMLParser.cpp \ 2d/CCTextFieldTTF.cpp \ 2d/CCTileMapAtlas.cpp \ diff --git a/tests/cpp-tests/Android.mk b/tests/cpp-tests/Android.mk index cedc1f75aa..d6abfe81e3 100644 --- a/tests/cpp-tests/Android.mk +++ b/tests/cpp-tests/Android.mk @@ -169,6 +169,7 @@ Classes/Texture2dTest/Texture2dTest.cpp \ Classes/TextureCacheTest/TextureCacheTest.cpp \ Classes/TexturePackerEncryptionTest/TextureAtlasEncryptionTest.cpp \ Classes/TileMapTest/TileMapTest.cpp \ +Classes/TileMapTest/TileMapTest2.cpp \ Classes/TouchesTest/Ball.cpp \ Classes/TouchesTest/Paddle.cpp \ Classes/TouchesTest/TouchesTest.cpp \ diff --git a/tests/cpp-tests/CMakeLists.txt b/tests/cpp-tests/CMakeLists.txt index 06a7413060..42a10d6eed 100644 --- a/tests/cpp-tests/CMakeLists.txt +++ b/tests/cpp-tests/CMakeLists.txt @@ -162,6 +162,7 @@ set(SAMPLE_SRC Classes/TexturePackerEncryptionTest/TextureAtlasEncryptionTest.cpp Classes/TextureCacheTest/TextureCacheTest.cpp Classes/TileMapTest/TileMapTest.cpp + Classes/TileMapTest/TileMapTest2.cpp Classes/TouchesTest/Ball.cpp Classes/TouchesTest/Paddle.cpp Classes/TouchesTest/TouchesTest.cpp From d89c2176c3d8f396f6a9e593ecf58a48b60059ad Mon Sep 17 00:00:00 2001 From: boyu0 Date: Wed, 18 Jun 2014 18:19:13 +0800 Subject: [PATCH 07/40] issue #4437: add vertex z support, fix some bugs. --- cocos/2d/CCTMXLayer2.cpp | 53 ++++++++++++++++++---------------------- cocos/2d/CCTMXLayer2.h | 9 +++---- cocos/base/ccTypes.h | 14 +++++------ 3 files changed, 34 insertions(+), 42 deletions(-) diff --git a/cocos/2d/CCTMXLayer2.cpp b/cocos/2d/CCTMXLayer2.cpp index 9410c4ad9b..dc9fff88d0 100644 --- a/cocos/2d/CCTMXLayer2.cpp +++ b/cocos/2d/CCTMXLayer2.cpp @@ -114,7 +114,6 @@ TMXLayer2::TMXLayer2() ,_tileSet(nullptr) ,_layerOrientation(TMXOrientationOrtho2) ,_texture(nullptr) -,_reusedTile(nullptr) ,_verticesToDraw(0) ,_vertexZvalue(0) ,_useAutomaticVertexZ(false) @@ -163,7 +162,7 @@ void TMXLayer2::onDraw(const Mat4 &transform, bool transformUpdated) if (Configuration::getInstance()->supportsShareableVAO()) { - V2F_T2F_Quad* quads = (V2F_T2F_Quad*)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY); + V3F_T2F_Quad* quads = (V3F_T2F_Quad*)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY); GLushort* indices = (GLushort *)glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY); _verticesToDraw = updateTiles(rect, quads, indices); glUnmapBuffer(GL_ARRAY_BUFFER); @@ -192,10 +191,10 @@ void TMXLayer2::onDraw(const Mat4 &transform, bool transformUpdated) getGLProgram()->setUniformsForBuiltins(_modelViewTransform); // vertices - glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_T2F), (GLvoid*) offsetof(V2F_T2F, vertices)); + glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(V3F_T2F), (GLvoid*) offsetof(V3F_T2F, vertices)); // tex coords - glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_T2F), (GLvoid*) offsetof(V2F_T2F, texCoords)); + glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, sizeof(V3F_T2F), (GLvoid*) offsetof(V3F_T2F, texCoords)); // color glVertexAttrib4f(GLProgram::VERTEX_ATTRIB_COLOR, _displayedColor.r/255.0f, _displayedColor.g/255.0f, _displayedColor.b/255.0f, _displayedOpacity/255.0f); @@ -209,7 +208,7 @@ void TMXLayer2::onDraw(const Mat4 &transform, bool transformUpdated) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } -int TMXLayer2::updateTiles(const Rect& culledRect, V2F_T2F_Quad *quads, GLushort *indices) +int TMXLayer2::updateTiles(const Rect& culledRect, V3F_T2F_Quad *quads, GLushort *indices) { int tilesUsed = 0; @@ -220,6 +219,8 @@ int TMXLayer2::updateTiles(const Rect& culledRect, V2F_T2F_Quad *quads, GLushort nodeToTileTransform.inverse(); //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; // 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); @@ -227,7 +228,7 @@ int TMXLayer2::updateTiles(const Rect& culledRect, V2F_T2F_Quad *quads, GLushort visibleTiles.origin.x = floor(visibleTiles.origin.x); visibleTiles.origin.y = floor(visibleTiles.origin.y); - V2F_T2F_Quad* quadsTmp = quads; + V3F_T2F_Quad* quadsTmp = quads; GLushort* indicesTmp = indices; // for the bigger tiles. @@ -261,7 +262,7 @@ int TMXLayer2::updateTiles(const Rect& culledRect, V2F_T2F_Quad *quads, GLushort if (_numQuads < quadsNeed) { _numQuads = quadsNeed; - _quads = (V2F_T2F_Quad*)realloc(_quads, _numQuads * sizeof(V2F_T2F_Quad)); + _quads = (V3F_T2F_Quad*)realloc(_quads, _numQuads * sizeof(V3F_T2F_Quad)); _indices = (GLushort*)realloc(_indices, _numQuads * 6 * sizeof(GLushort)); } @@ -271,11 +272,11 @@ int TMXLayer2::updateTiles(const Rect& culledRect, V2F_T2F_Quad *quads, GLushort Size texSize = _tileSet->_imageSize; - for (int y = visibleTiles.origin.y; y < visibleTiles.origin.y + visibleTiles.size.height + tilesOverY; ++y) + for (int y = visibleTiles.origin.y - tilesOverY; y < visibleTiles.origin.y + visibleTiles.size.height + tilesOverY; ++y) { if(y<0 || y >= _layerSize.height) continue; - for (int x = visibleTiles.origin.x - tilesOverX; x < visibleTiles.origin.x + visibleTiles.size.width; ++x) + for (int x = visibleTiles.origin.x - tilesOverX; x < visibleTiles.origin.x + visibleTiles.size.width + tilesOverX; ++x) { if(x<0 || x >= _layerSize.width) continue; @@ -286,12 +287,14 @@ int TMXLayer2::updateTiles(const Rect& culledRect, V2F_T2F_Quad *quads, GLushort if(tileGID!=0) { - V2F_T2F_Quad *quad = &quadsTmp[tilesUsed]; + V3F_T2F_Quad *quad = &quadsTmp[tilesUsed]; Vec3 nodePos(static_cast(x), static_cast(y), 0); _tileToNodeTransform.transformPoint(&nodePos); - float left, right, top, bottom; + float left, right, top, bottom, z; + + z = getVertexZForPos(Vec2(x, y)); // vertices if (tileGID & kTMXTileDiagonalFlag) @@ -319,23 +322,31 @@ int TMXLayer2::updateTiles(const Rect& culledRect, V2F_T2F_Quad *quads, GLushort // 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 @@ -392,7 +403,7 @@ void TMXLayer2::setupVBO() // Vertex + Tex Coords glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]); - glBufferData(GL_ARRAY_BUFFER, total * sizeof(V2F_T2F_Quad), NULL, GL_DYNAMIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, total * sizeof(V3F_T2F_Quad), NULL, GL_DYNAMIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); // Indices @@ -520,7 +531,7 @@ Sprite* TMXLayer2::getTileAt(const Point& tileCoordinate) tile->setPositionZ((float)getVertexZForPos(tileCoordinate)); tile->setOpacity(this->getOpacity()); tile->setTag(index); - this->addChild(tile, -index); + this->addChild(tile, index); _spriteContainer.insert(std::pair >(index, std::pair(tile, gid))); // tile is converted to sprite. @@ -808,22 +819,6 @@ void TMXLayer2::setupTileSprite(Sprite* sprite, Point pos, int gid) } } -Sprite* TMXLayer2::reusedTileWithRect(Rect rect) -{ - if (! _reusedTile) - { - _reusedTile = Sprite::createWithTexture(_texture, rect); - _reusedTile->retain(); - } - else - { - // Re-init the sprite - _reusedTile->setTextureRect(rect, false, rect.size); - } - - return _reusedTile; -} - std::string TMXLayer2::getDescription() const { return StringUtils::format("", _tag, (int)_mapTileSize.width, (int)_mapTileSize.height); diff --git a/cocos/2d/CCTMXLayer2.h b/cocos/2d/CCTMXLayer2.h index d677492a26..b085c03663 100644 --- a/cocos/2d/CCTMXLayer2.h +++ b/cocos/2d/CCTMXLayer2.h @@ -173,8 +173,6 @@ public: */ Sprite* getTileAt(const Point& tileCoordinate); - Sprite* reusedTileWithRect(Rect rect); - Sprite* updateTileForGID(int gid, const Point& pos); void setupTileSprite(Sprite* sprite, Point pos, int gid); @@ -189,7 +187,7 @@ public: protected: bool initWithTilesetInfo(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo); - int updateTiles(const Rect& culledRect, V2F_T2F_Quad *quads, GLushort *indices); + int updateTiles(const Rect& culledRect, V3F_T2F_Quad *quads, GLushort *indices); void setupVBO(); Point calculateLayerOffset(const Point& offset); @@ -205,7 +203,7 @@ protected: void insertTileForGID(int gid, const Point& pos); void setTileForGID(int index, int gid); - + protected: CustomCommand _customCommand; @@ -226,7 +224,6 @@ protected: ValueMap _properties; Texture2D *_texture; - Sprite* _reusedTile; /** container for sprite children. map > */ std::map > _spriteContainer; @@ -245,7 +242,7 @@ protected: /** tile coordinate to node coordinate transform */ Mat4 _tileToNodeTransform; /** quads to be rendered */ - V2F_T2F_Quad* _quads; + V3F_T2F_Quad* _quads; /** number of quads */ int _numQuads; /** indices */ diff --git a/cocos/base/ccTypes.h b/cocos/base/ccTypes.h index 9eaaf456d2..2c25850967 100644 --- a/cocos/base/ccTypes.h +++ b/cocos/base/ccTypes.h @@ -255,10 +255,10 @@ struct V3F_C4B_T2F }; //! a Vec2 with a vertex point, a tex coord point -struct V2F_T2F +struct V3F_T2F { //! vertices (2F) - Vec2 vertices; + Vec3 vertices; //! tex coords (2F) Tex2F texCoords; }; @@ -313,16 +313,16 @@ struct V2F_C4F_T2F_Quad V2F_C4F_T2F tr; }; -struct V2F_T2F_Quad +struct V3F_T2F_Quad { //! bottom left - V2F_T2F bl; + V3F_T2F bl; //! bottom right - V2F_T2F br; + V3F_T2F br; //! top left - V2F_T2F tl; + V3F_T2F tl; //! top right - V2F_T2F tr; + V3F_T2F tr; }; //! Blend Function used for textures From 5f10ff2c29c5d0f7488f7ad528c4cbb1e2e45a66 Mon Sep 17 00:00:00 2001 From: boyu0 Date: Thu, 19 Jun 2014 15:57:13 +0800 Subject: [PATCH 08/40] issue #4437: change name TMXLayer2 and TMXTiledMap2 to FastTMXLayer and FastTMXTiledMap, change Point to Vec2 --- build/cocos2d_libs.xcodeproj/project.pbxproj | 48 ++++---- .../{CCTMXLayer2.cpp => CCFastTMXLayer.cpp} | 114 +++++++++--------- cocos/2d/{CCTMXLayer2.h => CCFastTMXLayer.h} | 38 +++--- ...TMXTiledMap2.cpp => CCFastTMXTiledMap.cpp} | 52 ++++---- .../{CCTMXTiledMap2.h => CCFastTMXTiledMap.h} | 34 +++--- cocos/2d/CMakeLists.txt | 4 +- cocos/Android.mk | 4 +- .../Classes/TileMapTest/TileMapTest2.cpp | 90 +++++++------- tests/cpp-tests/Classes/controller.cpp | 2 +- 9 files changed, 193 insertions(+), 193 deletions(-) rename cocos/2d/{CCTMXLayer2.cpp => CCFastTMXLayer.cpp} (89%) rename cocos/2d/{CCTMXLayer2.h => CCFastTMXLayer.h} (87%) rename cocos/2d/{CCTMXTiledMap2.cpp => CCFastTMXTiledMap.cpp} (76%) rename cocos/2d/{CCTMXTiledMap2.h => CCFastTMXTiledMap.h} (87%) diff --git a/build/cocos2d_libs.xcodeproj/project.pbxproj b/build/cocos2d_libs.xcodeproj/project.pbxproj index c14fb88d31..605797f9de 100644 --- a/build/cocos2d_libs.xcodeproj/project.pbxproj +++ b/build/cocos2d_libs.xcodeproj/project.pbxproj @@ -1773,14 +1773,14 @@ B3AF01A31842FBA400A98B85 /* b2MotorJoint.h in Headers */ = {isa = PBXBuildFile; fileRef = B3AF019F1842FBA400A98B85 /* b2MotorJoint.h */; }; B3B12A5A17E7F44000026B4A /* libchipmunk Mac.a in Frameworks */ = {isa = PBXBuildFile; fileRef = A03F2CB81780BD04006731B9 /* libchipmunk Mac.a */; }; B3B12A5B17E7F45C00026B4A /* libchipmunk iOS.a in Frameworks */ = {isa = PBXBuildFile; fileRef = A07A4F3B178387670073F6A7 /* libchipmunk iOS.a */; }; - C0940CAC19419BDA00B8BB48 /* CCTMXLayer2.h in Headers */ = {isa = PBXBuildFile; fileRef = C0940CA819419BDA00B8BB48 /* CCTMXLayer2.h */; }; - C0940CAD19419BDA00B8BB48 /* CCTMXLayer2.h in Headers */ = {isa = PBXBuildFile; fileRef = C0940CA819419BDA00B8BB48 /* CCTMXLayer2.h */; }; - C0940CAE19419BDA00B8BB48 /* CCTMXTiledMap2.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C0940CA919419BDA00B8BB48 /* CCTMXTiledMap2.cpp */; }; - C0940CAF19419BDA00B8BB48 /* CCTMXTiledMap2.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C0940CA919419BDA00B8BB48 /* CCTMXTiledMap2.cpp */; }; - C0940CB019419BDA00B8BB48 /* CCTMXTiledMap2.h in Headers */ = {isa = PBXBuildFile; fileRef = C0940CAA19419BDA00B8BB48 /* CCTMXTiledMap2.h */; }; - C0940CB119419BDA00B8BB48 /* CCTMXTiledMap2.h in Headers */ = {isa = PBXBuildFile; fileRef = C0940CAA19419BDA00B8BB48 /* CCTMXTiledMap2.h */; }; - C0940CB219419BDA00B8BB48 /* CCTMXLayer2.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C0940CAB19419BDA00B8BB48 /* CCTMXLayer2.cpp */; }; - C0940CB319419BDA00B8BB48 /* CCTMXLayer2.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C0940CAB19419BDA00B8BB48 /* CCTMXLayer2.cpp */; }; + C0964AE51952C46300FC4E6C /* CCFastTMXLayer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C0964AE11952B9D400FC4E6C /* CCFastTMXLayer.cpp */; }; + C0964AE61952C46500FC4E6C /* CCFastTMXLayer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C0964AE11952B9D400FC4E6C /* CCFastTMXLayer.cpp */; }; + C0964AE71952C46C00FC4E6C /* CCFastTMXLayer.h in Headers */ = {isa = PBXBuildFile; fileRef = C0964AE21952B9D400FC4E6C /* CCFastTMXLayer.h */; }; + C0964AE81952C46C00FC4E6C /* CCFastTMXTiledMap.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C0964AE31952B9D400FC4E6C /* CCFastTMXTiledMap.cpp */; }; + C0964AE91952C46C00FC4E6C /* CCFastTMXTiledMap.h in Headers */ = {isa = PBXBuildFile; fileRef = C0964AE41952B9D400FC4E6C /* CCFastTMXTiledMap.h */; }; + C0964AEA1952C46D00FC4E6C /* CCFastTMXLayer.h in Headers */ = {isa = PBXBuildFile; fileRef = C0964AE21952B9D400FC4E6C /* CCFastTMXLayer.h */; }; + C0964AEB1952C46D00FC4E6C /* CCFastTMXTiledMap.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C0964AE31952B9D400FC4E6C /* CCFastTMXTiledMap.cpp */; }; + C0964AEC1952C46D00FC4E6C /* CCFastTMXTiledMap.h in Headers */ = {isa = PBXBuildFile; fileRef = C0964AE41952B9D400FC4E6C /* CCFastTMXTiledMap.h */; }; ED9C6A9418599AD8000A5232 /* CCNodeGrid.cpp in Sources */ = {isa = PBXBuildFile; fileRef = ED9C6A9218599AD8000A5232 /* CCNodeGrid.cpp */; }; ED9C6A9518599AD8000A5232 /* CCNodeGrid.cpp in Sources */ = {isa = PBXBuildFile; fileRef = ED9C6A9218599AD8000A5232 /* CCNodeGrid.cpp */; }; ED9C6A9618599AD8000A5232 /* CCNodeGrid.h in Headers */ = {isa = PBXBuildFile; fileRef = ED9C6A9318599AD8000A5232 /* CCNodeGrid.h */; }; @@ -2815,10 +2815,10 @@ B375104F1823AC7B00B3BA6A /* CCPhysicsWorldInfo_chipmunk.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCPhysicsWorldInfo_chipmunk.h; sourceTree = ""; }; B3AF019E1842FBA400A98B85 /* b2MotorJoint.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = b2MotorJoint.cpp; sourceTree = ""; }; B3AF019F1842FBA400A98B85 /* b2MotorJoint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = b2MotorJoint.h; sourceTree = ""; }; - C0940CA819419BDA00B8BB48 /* CCTMXLayer2.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCTMXLayer2.h; sourceTree = ""; }; - C0940CA919419BDA00B8BB48 /* CCTMXTiledMap2.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCTMXTiledMap2.cpp; sourceTree = ""; }; - C0940CAA19419BDA00B8BB48 /* CCTMXTiledMap2.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCTMXTiledMap2.h; sourceTree = ""; }; - C0940CAB19419BDA00B8BB48 /* CCTMXLayer2.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCTMXLayer2.cpp; sourceTree = ""; }; + C0964AE11952B9D400FC4E6C /* CCFastTMXLayer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCFastTMXLayer.cpp; sourceTree = ""; }; + C0964AE21952B9D400FC4E6C /* CCFastTMXLayer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCFastTMXLayer.h; sourceTree = ""; }; + C0964AE31952B9D400FC4E6C /* CCFastTMXTiledMap.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCFastTMXTiledMap.cpp; sourceTree = ""; }; + C0964AE41952B9D400FC4E6C /* CCFastTMXTiledMap.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCFastTMXTiledMap.h; sourceTree = ""; }; ED9C6A9218599AD8000A5232 /* CCNodeGrid.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; lineEnding = 0; path = CCNodeGrid.cpp; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.cpp; }; ED9C6A9318599AD8000A5232 /* CCNodeGrid.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCNodeGrid.h; sourceTree = ""; }; /* End PBXFileReference section */ @@ -3316,10 +3316,10 @@ 1A5702DF180BCE610088DEC7 /* tilemap-parallax-nodes */ = { isa = PBXGroup; children = ( - C0940CA819419BDA00B8BB48 /* CCTMXLayer2.h */, - C0940CAB19419BDA00B8BB48 /* CCTMXLayer2.cpp */, - C0940CA919419BDA00B8BB48 /* CCTMXTiledMap2.cpp */, - C0940CAA19419BDA00B8BB48 /* CCTMXTiledMap2.h */, + C0964AE11952B9D400FC4E6C /* CCFastTMXLayer.cpp */, + C0964AE21952B9D400FC4E6C /* CCFastTMXLayer.h */, + C0964AE31952B9D400FC4E6C /* CCFastTMXTiledMap.cpp */, + C0964AE41952B9D400FC4E6C /* CCFastTMXTiledMap.h */, 1A5702FE180BCE890088DEC7 /* CCParallaxNode.cpp */, 1A5702FF180BCE890088DEC7 /* CCParallaxNode.h */, 1A5702E0180BCE750088DEC7 /* CCTileMapAtlas.cpp */, @@ -4997,6 +4997,7 @@ B29594C81926D61F003EEF37 /* CCObjLoader.h in Headers */, 50ABBE911925AB6F00A911A9 /* CCPlatformMacros.h in Headers */, 50ABC05D1926664800A911A9 /* CCApplication.h in Headers */, + C0964AE71952C46C00FC4E6C /* CCFastTMXLayer.h in Headers */, 50ABC0071926664800A911A9 /* CCApplicationProtocol.h in Headers */, 50FCEBA918C72017004AD434 /* LoadingBarReader.h in Headers */, 50ABBD4E1925AB0000A911A9 /* MathUtil.h in Headers */, @@ -5010,7 +5011,6 @@ 50ABBED51925AB6F00A911A9 /* utlist.h in Headers */, 50FCEBAD18C72017004AD434 /* PageViewReader.h in Headers */, 1A5702F4180BCE750088DEC7 /* CCTMXObjectGroup.h in Headers */, - C0940CB019419BDA00B8BB48 /* CCTMXTiledMap2.h in Headers */, 50ABBDAF1925AB4100A911A9 /* CCRenderer.h in Headers */, 1A5702F8180BCE750088DEC7 /* CCTMXTiledMap.h in Headers */, 5034CA21191D591100CE6051 /* ccShader_PositionTextureColorAlphaTest.frag in Headers */, @@ -5127,7 +5127,6 @@ 1AAF536C180E3374000584C8 /* HttpClient.h in Headers */, 50ABBD9D1925AB4100A911A9 /* ccGLStateCache.h in Headers */, 50ABBEB91925AB6F00A911A9 /* ccUTF8.h in Headers */, - C0940CAC19419BDA00B8BB48 /* CCTMXLayer2.h in Headers */, 1AAF536E180E3374000584C8 /* HttpRequest.h in Headers */, 1AAF5370180E3374000584C8 /* HttpResponse.h in Headers */, 50ABBE671925AB6F00A911A9 /* CCEventListenerCustom.h in Headers */, @@ -5200,6 +5199,7 @@ 1A8C5A07180E930E00EF57C3 /* CocoStudio.h in Headers */, 1A8C5A0F180E930E00EF57C3 /* DictionaryHelper.h in Headers */, 50FCEBBD18C72017004AD434 /* TextBMFontReader.h in Headers */, + C0964AE91952C46C00FC4E6C /* CCFastTMXTiledMap.h in Headers */, 50FCEBCB18C72017004AD434 /* WidgetReaderProtocol.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; @@ -5384,6 +5384,7 @@ 5034CA3A191D591100CE6051 /* ccShader_PositionColorLengthTexture.frag in Headers */, 50ABBEC41925AB6F00A911A9 /* CCVector.h in Headers */, 50ABBE501925AB6F00A911A9 /* CCEventCustom.h in Headers */, + C0964AEC1952C46D00FC4E6C /* CCFastTMXTiledMap.h in Headers */, 1A570070180BC5A10088DEC7 /* CCActionEase.h in Headers */, 1A570074180BC5A10088DEC7 /* CCActionGrid.h in Headers */, B37510841823ACA100B3BA6A /* CCPhysicsShapeInfo_chipmunk.h in Headers */, @@ -5480,7 +5481,6 @@ 1A570230180BCC1A0088DEC7 /* CCParticleSystemQuad.h in Headers */, 5034CA2C191D591100CE6051 /* ccShader_PositionTextureA8Color.vert in Headers */, 50ABBE981925AB6F00A911A9 /* CCProtocols.h in Headers */, - C0940CB119419BDA00B8BB48 /* CCTMXTiledMap2.h in Headers */, 2905FA8B18CF08D100240AA3 /* UITextField.h in Headers */, 50FCEBA618C72017004AD434 /* ListViewReader.h in Headers */, 50ABBD431925AB0000A911A9 /* CCMathBase.h in Headers */, @@ -5492,7 +5492,6 @@ 50ABBE701925AB6F00A911A9 /* CCEventListenerKeyboard.h in Headers */, 1A57028D180BCC900088DEC7 /* CCSpriteFrameCache.h in Headers */, 1A570295180BCCAB0088DEC7 /* CCAnimation.h in Headers */, - C0940CAD19419BDA00B8BB48 /* CCTMXLayer2.h in Headers */, 50ABBDB81925AB4100A911A9 /* CCTexture2D.h in Headers */, 50ABBE341925AB6F00A911A9 /* CCConfiguration.h in Headers */, 1A570299180BCCAB0088DEC7 /* CCAnimationCache.h in Headers */, @@ -5648,6 +5647,7 @@ 1A8C59AA180E930E00EF57C3 /* CCArmatureDataManager.h in Headers */, 1A8C59AE180E930E00EF57C3 /* CCArmatureDefine.h in Headers */, 1A8C59B2180E930E00EF57C3 /* CCBatchNode.h in Headers */, + C0964AEA1952C46D00FC4E6C /* CCFastTMXLayer.h in Headers */, 1A8C59B6180E930E00EF57C3 /* CCBone.h in Headers */, 1A8C59BA180E930E00EF57C3 /* CCColliderDetector.h in Headers */, 50ABBEAE1925AB6F00A911A9 /* ccTypes.h in Headers */, @@ -6135,7 +6135,6 @@ 50ABBD831925AB4100A911A9 /* CCBatchCommand.cpp in Sources */, 1A5701C7180BCB5A0088DEC7 /* CCLabelTextFormatter.cpp in Sources */, 1A5701CB180BCB5A0088DEC7 /* CCLabelTTF.cpp in Sources */, - C0940CAE19419BDA00B8BB48 /* CCTMXTiledMap2.cpp in Sources */, 50ABBE711925AB6F00A911A9 /* CCEventListenerMouse.cpp in Sources */, 1A5701DE180BCB8C0088DEC7 /* CCLayer.cpp in Sources */, 1A5701E2180BCB8C0088DEC7 /* CCScene.cpp in Sources */, @@ -6223,7 +6222,6 @@ 1AD71DE5180E26E600808F54 /* CCMenuItemImageLoader.cpp in Sources */, 50ABBD8B1925AB4100A911A9 /* CCGLProgram.cpp in Sources */, 1AD71DE9180E26E600808F54 /* CCMenuItemLoader.cpp in Sources */, - C0940CB219419BDA00B8BB48 /* CCTMXLayer2.cpp in Sources */, 50ABBDA31925AB4100A911A9 /* CCQuadCommand.cpp in Sources */, 2905FA6A18CF08D100240AA3 /* UIPageView.cpp in Sources */, B29594C61926D61F003EEF37 /* CCObjLoader.cpp in Sources */, @@ -6322,6 +6320,7 @@ 1A8C59CF180E930E00EF57C3 /* CCDatas.cpp in Sources */, 1A8C59D3180E930E00EF57C3 /* CCDecorativeDisplay.cpp in Sources */, 50FCEBB318C72017004AD434 /* SliderReader.cpp in Sources */, + C0964AE51952C46300FC4E6C /* CCFastTMXLayer.cpp in Sources */, 50ABBE4D1925AB6F00A911A9 /* CCEventCustom.cpp in Sources */, 50ABBE9F1925AB6F00A911A9 /* CCScheduler.cpp in Sources */, 50ABC0151926664800A911A9 /* CCImage.cpp in Sources */, @@ -6331,6 +6330,7 @@ 50ABBD601925AB0000A911A9 /* Vec4.cpp in Sources */, 1A8C59DB180E930E00EF57C3 /* CCDisplayManager.cpp in Sources */, 2905FA6218CF08D100240AA3 /* UIListView.cpp in Sources */, + C0964AE81952C46C00FC4E6C /* CCFastTMXTiledMap.cpp in Sources */, 1A8C59DF180E930E00EF57C3 /* CCInputDelegate.cpp in Sources */, 1A8C59E3180E930E00EF57C3 /* CCProcessBase.cpp in Sources */, 1A8C59E7180E930E00EF57C3 /* CCSGUIReader.cpp in Sources */, @@ -6566,7 +6566,6 @@ 503DD8E21926736A00CD74DD /* CCCommon.mm in Sources */, 1A5701A2180BCB590088DEC7 /* CCFontAtlas.cpp in Sources */, 50ABC00E1926664800A911A9 /* CCFileUtils.cpp in Sources */, - C0940CB319419BDA00B8BB48 /* CCTMXLayer2.cpp in Sources */, 50ABBE241925AB6F00A911A9 /* base64.cpp in Sources */, 1A5701A6180BCB590088DEC7 /* CCFontAtlasCache.cpp in Sources */, 1A5701B2180BCB590088DEC7 /* CCFontFNT.cpp in Sources */, @@ -6615,6 +6614,7 @@ 1A570287180BCC900088DEC7 /* CCSpriteFrame.cpp in Sources */, 1A57028B180BCC900088DEC7 /* CCSpriteFrameCache.cpp in Sources */, 1A570293180BCCAB0088DEC7 /* CCAnimation.cpp in Sources */, + C0964AEB1952C46D00FC4E6C /* CCFastTMXTiledMap.cpp in Sources */, 1A570297180BCCAB0088DEC7 /* CCAnimationCache.cpp in Sources */, 50FCEBC418C72017004AD434 /* TextReader.cpp in Sources */, 50FCEB9418C72017004AD434 /* ButtonReader.cpp in Sources */, @@ -6727,6 +6727,7 @@ 1A9DCA28180E6955007A3AD4 /* CCGLBufferedNode.cpp in Sources */, 50ABBE201925AB6F00A911A9 /* atitc.cpp in Sources */, 50FCEBA018C72017004AD434 /* LayoutReader.cpp in Sources */, + C0964AE61952C46500FC4E6C /* CCFastTMXLayer.cpp in Sources */, 50E6D33518E174130051CA34 /* UIHBox.cpp in Sources */, 50FCEBB018C72017004AD434 /* ScrollViewReader.cpp in Sources */, 50ABBE9A1925AB6F00A911A9 /* CCRef.cpp in Sources */, @@ -6750,7 +6751,6 @@ 50ABC0001926664800A911A9 /* CCFileUtilsApple.mm in Sources */, 2905FA7918CF08D100240AA3 /* UISlider.cpp in Sources */, 1A8C59A4180E930E00EF57C3 /* CCArmatureAnimation.cpp in Sources */, - C0940CAF19419BDA00B8BB48 /* CCTMXTiledMap2.cpp in Sources */, 1A8C59A8180E930E00EF57C3 /* CCArmatureDataManager.cpp in Sources */, 2905FA8918CF08D100240AA3 /* UITextField.cpp in Sources */, 1A8C59AC180E930E00EF57C3 /* CCArmatureDefine.cpp in Sources */, diff --git a/cocos/2d/CCTMXLayer2.cpp b/cocos/2d/CCFastTMXLayer.cpp similarity index 89% rename from cocos/2d/CCTMXLayer2.cpp rename to cocos/2d/CCFastTMXLayer.cpp index dc9fff88d0..da7999b1e9 100644 --- a/cocos/2d/CCTMXLayer2.cpp +++ b/cocos/2d/CCFastTMXLayer.cpp @@ -34,9 +34,9 @@ THE SOFTWARE. It was rewritten again, and only a small part of the original HK ideas/code remains in this implementation */ -#include "CCTMXLayer2.h" +#include "CCFastTMXLayer.h" #include "CCTMXXMLParser.h" -#include "CCTMXTiledMap2.h" +#include "CCFastTMXTiledMap.h" #include "2d/CCSprite.h" #include "renderer/CCTextureCache.h" #include "renderer/CCGLProgramCache.h" @@ -53,11 +53,11 @@ namespace static const int MAX_QUADS_COUNT = 65536 / 6; } -// TMXLayer2 - init & alloc & dealloc +// FastTMXLayer - init & alloc & dealloc -TMXLayer2 * TMXLayer2::create(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo) +FastTMXLayer * FastTMXLayer::create(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo) { - TMXLayer2 *ret = new TMXLayer2(); + FastTMXLayer *ret = new FastTMXLayer(); if (ret->initWithTilesetInfo(tilesetInfo, layerInfo, mapInfo)) { ret->autorelease(); @@ -65,7 +65,7 @@ TMXLayer2 * TMXLayer2::create(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerIn } return nullptr; } -bool TMXLayer2::initWithTilesetInfo(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo) +bool FastTMXLayer::initWithTilesetInfo(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo) { if( tilesetInfo ) @@ -90,7 +90,7 @@ bool TMXLayer2::initWithTilesetInfo(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *l _layerOrientation = mapInfo->getOrientation(); // offset (after layer orientation is set); - Point offset = this->calculateLayerOffset(layerInfo->_offset); + Vec2 offset = this->calculateLayerOffset(layerInfo->_offset); this->setPosition(CC_POINT_PIXELS_TO_POINTS(offset)); this->setContentSize(CC_SIZE_PIXELS_TO_POINTS(Size(_layerSize.width * _mapTileSize.width, _layerSize.height * _mapTileSize.height))); @@ -106,13 +106,13 @@ bool TMXLayer2::initWithTilesetInfo(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *l return true; } -TMXLayer2::TMXLayer2() +FastTMXLayer::FastTMXLayer() :_layerName("") ,_layerSize(Size::ZERO) ,_mapTileSize(Size::ZERO) ,_tiles(nullptr) ,_tileSet(nullptr) -,_layerOrientation(TMXOrientationOrtho2) +,_layerOrientation(FastTMXOrientationOrtho) ,_texture(nullptr) ,_verticesToDraw(0) ,_vertexZvalue(0) @@ -123,7 +123,7 @@ TMXLayer2::TMXLayer2() ,_dirty(false) {} -TMXLayer2::~TMXLayer2() +FastTMXLayer::~FastTMXLayer() { CC_SAFE_RELEASE(_tileSet); CC_SAFE_RELEASE(_texture); @@ -132,14 +132,14 @@ TMXLayer2::~TMXLayer2() CC_SAFE_FREE(_indices); } -void TMXLayer2::draw(Renderer *renderer, const Mat4& transform, uint32_t flags) +void FastTMXLayer::draw(Renderer *renderer, const Mat4& transform, uint32_t flags) { _customCommand.init(_globalZOrder); - _customCommand.func = CC_CALLBACK_0(TMXLayer2::onDraw, this, transform, flags); + _customCommand.func = CC_CALLBACK_0(FastTMXLayer::onDraw, this, transform, flags); renderer->addCommand(&_customCommand); } -void TMXLayer2::onDraw(const Mat4 &transform, bool transformUpdated) +void FastTMXLayer::onDraw(const Mat4 &transform, bool transformUpdated) { GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_TEX_COORD); GL::bindTexture2D( _texture->getName() ); @@ -208,7 +208,7 @@ void TMXLayer2::onDraw(const Mat4 &transform, bool transformUpdated) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } -int TMXLayer2::updateTiles(const Rect& culledRect, V3F_T2F_Quad *quads, GLushort *indices) +int FastTMXLayer::updateTiles(const Rect& culledRect, V3F_T2F_Quad *quads, GLushort *indices) { int tilesUsed = 0; @@ -236,7 +236,7 @@ int TMXLayer2::updateTiles(const Rect& culledRect, V3F_T2F_Quad *quads, GLushort int tilesOverY = 0; // for diagonal oriention tiles float tileSizeMax = std::max(tileSize.width, tileSize.height); - if (_layerOrientation == TMXOrientationOrtho2) + if (_layerOrientation == FastTMXOrientationOrtho) { tilesOverX = ceil(tileSizeMax / mapTileSize.width) - 1; tilesOverY = ceil(tileSizeMax / mapTileSize.height) - 1; @@ -244,7 +244,7 @@ int TMXLayer2::updateTiles(const Rect& culledRect, V3F_T2F_Quad *quads, GLushort if (tilesOverX < 0) tilesOverX = 0; if (tilesOverY < 0) tilesOverY = 0; } - else if(_layerOrientation == TMXOrientationIso2) + else if(_layerOrientation == FastTMXOrientationIso) { Rect overTileRect(0, 0, tileSizeMax - mapTileSize.width, tileSizeMax - mapTileSize.height); if (overTileRect.size.width < 0) overTileRect.size.width = 0; @@ -394,7 +394,7 @@ int TMXLayer2::updateTiles(const Rect& culledRect, V3F_T2F_Quad *quads, GLushort return tilesUsed * 6; } -void TMXLayer2::setupVBO() +void FastTMXLayer::setupVBO() { glGenBuffers(2, &_buffersVBO[0]); @@ -414,8 +414,8 @@ void TMXLayer2::setupVBO() CHECK_GL_ERROR_DEBUG(); } -// TMXLayer2 - setup Tiles -void TMXLayer2::setupTiles() +// FastTMXLayer - setup Tiles +void FastTMXLayer::setupTiles() { // Optimization: quick hack that sets the image size on the tileset _tileSet->_imageSize = _texture->getContentSizeInPixels(); @@ -434,18 +434,18 @@ void TMXLayer2::setupTiles() switch (_layerOrientation) { - case TMXOrientationOrtho2: + case FastTMXOrientationOrtho: _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 TMXOrientationIso2: + case FastTMXOrientationIso: _screenGridSize.width = ceil(screenSize.width / _mapTileSize.width) + 2; _screenGridSize.height = ceil(screenSize.height / (_mapTileSize.height/2)) + 4; break; - case TMXOrientationHex2: + case FastTMXOrientationHex: break; } @@ -454,7 +454,7 @@ void TMXLayer2::setupTiles() setupVBO(); } -Mat4 TMXLayer2::tileToNodeTransform() +Mat4 FastTMXLayer::tileToNodeTransform() { float w = _mapTileSize.width / CC_CONTENT_SCALE_FACTOR(); float h = _mapTileSize.height / CC_CONTENT_SCALE_FACTOR(); @@ -462,7 +462,7 @@ Mat4 TMXLayer2::tileToNodeTransform() switch(_layerOrientation) { - case TMXOrientationOrtho2: + case FastTMXOrientationOrtho: { _tileToNodeTransform = Mat4 ( @@ -474,7 +474,7 @@ Mat4 TMXLayer2::tileToNodeTransform() return _tileToNodeTransform; } - case TMXOrientationIso2: + case FastTMXOrientationIso: { float offX = (_layerSize.width - 1) * w / 2; _tileToNodeTransform = Mat4 @@ -486,7 +486,7 @@ Mat4 TMXLayer2::tileToNodeTransform() ); return _tileToNodeTransform; } - case TMXOrientationHex2: + case FastTMXOrientationHex: { _tileToNodeTransform = Mat4::IDENTITY; return _tileToNodeTransform; @@ -501,7 +501,7 @@ Mat4 TMXLayer2::tileToNodeTransform() } // removing / getting tiles -Sprite* TMXLayer2::getTileAt(const Point& tileCoordinate) +Sprite* FastTMXLayer::getTileAt(const Vec2& tileCoordinate) { 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"); @@ -525,8 +525,8 @@ Sprite* TMXLayer2::getTileAt(const Point& tileCoordinate) rect = CC_RECT_PIXELS_TO_POINTS(rect); tile = Sprite::createWithTexture(_texture, rect); - Point p = this->getPositionAt(tileCoordinate); - tile->setAnchorPoint(Point::ZERO); + Vec2 p = this->getPositionAt(tileCoordinate); + tile->setAnchorPoint(Vec2::ZERO); tile->setPosition(p); tile->setPositionZ((float)getVertexZForPos(tileCoordinate)); tile->setOpacity(this->getOpacity()); @@ -541,7 +541,7 @@ Sprite* TMXLayer2::getTileAt(const Point& tileCoordinate) return tile; } -int TMXLayer2::getTileGIDAt(const Point& tileCoordinate, TMXTileFlags* flags/* = nullptr*/) +int FastTMXLayer::getTileGIDAt(const Vec2& tileCoordinate, TMXTileFlags* flags/* = nullptr*/) { 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"); @@ -567,12 +567,12 @@ int TMXLayer2::getTileGIDAt(const Point& tileCoordinate, TMXTileFlags* flags/* = return (tile & kTMXFlippedMask); } -Point TMXLayer2::getPositionAt(const Point& pos) +Vec2 FastTMXLayer::getPositionAt(const Vec2& pos) { return PointApplyTransform(pos, _tileToNodeTransform); } -int TMXLayer2::getVertexZForPos(const Point& pos) +int FastTMXLayer::getVertexZForPos(const Vec2& pos) { int ret = 0; int maxVal = 0; @@ -580,14 +580,14 @@ int TMXLayer2::getVertexZForPos(const Point& pos) { switch (_layerOrientation) { - case TMXOrientationIso2: + case FastTMXOrientationIso: maxVal = static_cast(_layerSize.width + _layerSize.height); ret = static_cast(-(maxVal - (pos.x + pos.y))); break; - case TMXOrientationOrtho2: + case FastTMXOrientationOrtho: ret = static_cast(-(_layerSize.height-pos.y)); break; - case TMXOrientationHex2: + case FastTMXOrientationHex: CCASSERT(0, "TMX Hexa zOrder not supported"); break; default: @@ -603,7 +603,7 @@ int TMXLayer2::getVertexZForPos(const Point& pos) return ret; } -void TMXLayer2::removeTileAt(const Point& tileCoordinate) +void FastTMXLayer::removeTileAt(const Vec2& tileCoordinate) { CCASSERT( tileCoordinate.x < _layerSize.width && tileCoordinate.y < _layerSize.height && tileCoordinate.x >=0 && tileCoordinate.y >=0, "TMXLayer: invalid position"); @@ -626,7 +626,7 @@ void TMXLayer2::removeTileAt(const Point& tileCoordinate) } } -void TMXLayer2::setTileForGID(int index, int gid) +void FastTMXLayer::setTileForGID(int index, int gid) { _tiles[index] = gid; _dirty = true; @@ -634,7 +634,7 @@ void TMXLayer2::setTileForGID(int index, int gid) -void TMXLayer2::removeChild(Node* node, bool cleanup) +void FastTMXLayer::removeChild(Node* node, bool cleanup) { int tag = node->getTag(); auto it = _spriteContainer.find(tag); @@ -645,8 +645,8 @@ void TMXLayer2::removeChild(Node* node, bool cleanup) Node::removeChild(node, cleanup); } -// TMXLayer2 - Properties -Value TMXLayer2::getProperty(const std::string& propertyName) const +// FastTMXLayer - Properties +Value FastTMXLayer::getProperty(const std::string& propertyName) const { if (_properties.find(propertyName) != _properties.end()) return _properties.at(propertyName); @@ -654,7 +654,7 @@ Value TMXLayer2::getProperty(const std::string& propertyName) const return Value(); } -void TMXLayer2::parseInternalProperties() +void FastTMXLayer::parseInternalProperties() { auto vertexz = getProperty("cc_vertexz"); if (!vertexz.isNull()) @@ -686,32 +686,32 @@ void TMXLayer2::parseInternalProperties() //CCTMXLayer2 - obtaining positions, offset -Point TMXLayer2::calculateLayerOffset(const Point& pos) +Vec2 FastTMXLayer::calculateLayerOffset(const Vec2& pos) { - Point ret = Point::ZERO; + Vec2 ret = Vec2::ZERO; switch (_layerOrientation) { - case TMXOrientationOrtho2: - ret = Point( pos.x * _mapTileSize.width, -pos.y *_mapTileSize.height); + case FastTMXOrientationOrtho: + ret = Vec2( pos.x * _mapTileSize.width, -pos.y *_mapTileSize.height); break; - case TMXOrientationIso2: - ret = Point((_mapTileSize.width /2) * (pos.x - pos.y), + case FastTMXOrientationIso: + ret = Vec2((_mapTileSize.width /2) * (pos.x - pos.y), (_mapTileSize.height /2 ) * (-pos.x - pos.y)); break; - case TMXOrientationHex2: - CCASSERT(pos.equals(Point::ZERO), "offset for hexagonal map not implemented yet"); + case FastTMXOrientationHex: + CCASSERT(pos.equals(Vec2::ZERO), "offset for hexagonal map not implemented yet"); break; } return ret; } // TMXLayer - adding / remove tiles -void TMXLayer2::setTileGID(int gid, const Point& tileCoordinate) +void FastTMXLayer::setTileGID(int gid, const Vec2& tileCoordinate) { setTileGID(gid, tileCoordinate, (TMXTileFlags)0); } -void TMXLayer2::setTileGID(int gid, const Point& tileCoordinate, TMXTileFlags flags) +void FastTMXLayer::setTileGID(int gid, const Vec2& tileCoordinate, TMXTileFlags flags) { 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"); @@ -763,11 +763,11 @@ void TMXLayer2::setTileGID(int gid, const Point& tileCoordinate, TMXTileFlags fl } } -void TMXLayer2::setupTileSprite(Sprite* sprite, Point pos, int gid) +void FastTMXLayer::setupTileSprite(Sprite* sprite, Vec2 pos, int gid) { sprite->setPosition(getPositionAt(pos)); sprite->setPositionZ((float)getVertexZForPos(pos)); - sprite->setAnchorPoint(Point::ZERO); + sprite->setAnchorPoint(Vec2::ZERO); sprite->setOpacity(this->getOpacity()); //issue 1264, flip can be undone as well @@ -779,8 +779,8 @@ void TMXLayer2::setupTileSprite(Sprite* sprite, Point pos, int gid) if (gid & kTMXTileDiagonalFlag) { // put the anchor in the middle for ease of rotation. - sprite->setAnchorPoint(Point(0.5f,0.5f)); - sprite->setPosition(Point(getPositionAt(pos).x + sprite->getContentSize().height/2, + sprite->setAnchorPoint(Vec2(0.5f,0.5f)); + sprite->setPosition(Vec2(getPositionAt(pos).x + sprite->getContentSize().height/2, getPositionAt(pos).y + sprite->getContentSize().width/2 ) ); int flag = gid & (kTMXTileHorizontalFlag | kTMXTileVerticalFlag ); @@ -819,9 +819,9 @@ void TMXLayer2::setupTileSprite(Sprite* sprite, Point pos, int gid) } } -std::string TMXLayer2::getDescription() const +std::string FastTMXLayer::getDescription() const { - return StringUtils::format("", _tag, (int)_mapTileSize.width, (int)_mapTileSize.height); + return StringUtils::format("", _tag, (int)_mapTileSize.width, (int)_mapTileSize.height); } NS_CC_END diff --git a/cocos/2d/CCTMXLayer2.h b/cocos/2d/CCFastTMXLayer.h similarity index 87% rename from cocos/2d/CCTMXLayer2.h rename to cocos/2d/CCFastTMXLayer.h index b085c03663..4317418e92 100644 --- a/cocos/2d/CCTMXLayer2.h +++ b/cocos/2d/CCFastTMXLayer.h @@ -48,7 +48,7 @@ struct _ccCArray; * @{ */ -/** @brief TMXLayer2 represents the TMX layer. +/** @brief FastTMXLayer represents the TMX layer. It is a subclass of SpriteBatchNode. By default the tiles are rendered using a TextureAtlas. If you modify a tile on runtime, then, that tile will become a Sprite, otherwise no Sprite objects are created. @@ -77,25 +77,25 @@ Tiles can have tile flags for additional properties. At the moment only flip hor @since 1.1 */ -class CC_DLL TMXLayer2 : public Node +class CC_DLL FastTMXLayer : public Node { public: - /** creates a TMXLayer2 with an tileset info, a layer info and a map info */ - static TMXLayer2 * create(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo); + /** creates a FastTMXLayer with an tileset info, a layer info and a map info */ + static FastTMXLayer * create(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo); /** * @js ctor */ - TMXLayer2(); + FastTMXLayer(); /** * @js NA * @lua NA */ - virtual ~TMXLayer2(); + virtual ~FastTMXLayer(); /** returns the tile gid at a given tile coordinate. It also returns the tile flags. */ - int getTileGIDAt(const Point& tileCoordinate, TMXTileFlags* flags = nullptr); - CC_DEPRECATED_ATTRIBUTE int tileGIDAt(const Point& tileCoordinate, TMXTileFlags* flags = nullptr){ + int getTileGIDAt(const Vec2& tileCoordinate, TMXTileFlags* flags = nullptr); + CC_DEPRECATED_ATTRIBUTE int tileGIDAt(const Vec2& tileCoordinate, TMXTileFlags* flags = nullptr){ return getTileGIDAt(tileCoordinate, flags); }; @@ -103,7 +103,7 @@ public: The Tile GID can be obtained by using the method "tileGIDAt" or by using the TMX editor -> Tileset Mgr +1. If a tile is already placed at that position, then it will be removed. */ - void setTileGID(int gid, const Point& tileCoordinate); + void setTileGID(int gid, const Vec2& tileCoordinate); /** sets the tile gid (gid = tile global id) at a given tile coordinate. The Tile GID can be obtained by using the method "tileGIDAt" or by using the TMX editor -> Tileset Mgr +1. @@ -112,14 +112,14 @@ public: Use withFlags if the tile flags need to be changed as well */ - void setTileGID(int gid, const Point& tileCoordinate, TMXTileFlags flags); + void setTileGID(int gid, const Vec2& tileCoordinate, TMXTileFlags flags); /** removes a tile at given tile coordinate */ - void removeTileAt(const Point& tileCoordinate); + void removeTileAt(const Vec2& tileCoordinate); /** returns the position in points of a given tile coordinate */ - Point getPositionAt(const Point& tileCoordinate); - CC_DEPRECATED_ATTRIBUTE Point positionAt(const Point& tileCoordinate) { return getPositionAt(tileCoordinate); }; + Vec2 getPositionAt(const Vec2& tileCoordinate); + CC_DEPRECATED_ATTRIBUTE Vec2 positionAt(const Vec2& tileCoordinate) { return getPositionAt(tileCoordinate); }; /** return the value for the specific property name */ Value getProperty(const std::string& propertyName) const; @@ -171,11 +171,11 @@ public: You can remove either by calling: - layer->removeChild(sprite, cleanup); */ - Sprite* getTileAt(const Point& tileCoordinate); + Sprite* getTileAt(const Vec2& tileCoordinate); - Sprite* updateTileForGID(int gid, const Point& pos); + Sprite* updateTileForGID(int gid, const Vec2& pos); - void setupTileSprite(Sprite* sprite, Point pos, int gid); + void setupTileSprite(Sprite* sprite, Vec2 pos, int gid); // // Override @@ -189,7 +189,7 @@ protected: bool initWithTilesetInfo(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo); int updateTiles(const Rect& culledRect, V3F_T2F_Quad *quads, GLushort *indices); void setupVBO(); - Point calculateLayerOffset(const Point& offset); + Vec2 calculateLayerOffset(const Vec2& offset); /* The layer recognizes some special properties, like cc_vertez */ void parseInternalProperties(); @@ -199,9 +199,9 @@ protected: void onDraw(const Mat4 &transform, bool transformUpdated); - int getVertexZForPos(const Point& pos); + int getVertexZForPos(const Vec2& pos); - void insertTileForGID(int gid, const Point& pos); + void insertTileForGID(int gid, const Vec2& pos); void setTileForGID(int index, int gid); protected: diff --git a/cocos/2d/CCTMXTiledMap2.cpp b/cocos/2d/CCFastTMXTiledMap.cpp similarity index 76% rename from cocos/2d/CCTMXTiledMap2.cpp rename to cocos/2d/CCFastTMXTiledMap.cpp index b737cafd9b..116c372131 100644 --- a/cocos/2d/CCTMXTiledMap2.cpp +++ b/cocos/2d/CCFastTMXTiledMap.cpp @@ -24,22 +24,22 @@ 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 "CCTMXTiledMap2.h" +#include "CCFastTMXTiledMap.h" #include #include "CCTMXXMLParser.h" -#include "CCTMXLayer2.h" +#include "CCFastTMXLayer.h" #include "CCSprite.h" #include "deprecated/CCString.h" NS_CC_BEGIN -// implementation TMXTiledMap2 +// implementation FastTMXTiledMap -TMXTiledMap2 * TMXTiledMap2::create(const std::string& tmxFile) +FastTMXTiledMap * FastTMXTiledMap::create(const std::string& tmxFile) { - TMXTiledMap2 *ret = new TMXTiledMap2(); + FastTMXTiledMap *ret = new FastTMXTiledMap(); if (ret->initWithTMXFile(tmxFile)) { ret->autorelease(); @@ -49,9 +49,9 @@ TMXTiledMap2 * TMXTiledMap2::create(const std::string& tmxFile) return nullptr; } -TMXTiledMap2* TMXTiledMap2::createWithXML(const std::string& tmxString, const std::string& resourcePath) +FastTMXTiledMap* FastTMXTiledMap::createWithXML(const std::string& tmxString, const std::string& resourcePath) { - TMXTiledMap2 *ret = new TMXTiledMap2(); + FastTMXTiledMap *ret = new FastTMXTiledMap(); if (ret->initWithXML(tmxString, resourcePath)) { ret->autorelease(); @@ -61,9 +61,9 @@ TMXTiledMap2* TMXTiledMap2::createWithXML(const std::string& tmxString, const st return nullptr; } -bool TMXTiledMap2::initWithTMXFile(const std::string& tmxFile) +bool FastTMXTiledMap::initWithTMXFile(const std::string& tmxFile) { - CCASSERT(tmxFile.size()>0, "TMXTiledMap2: tmx file should not be empty"); + CCASSERT(tmxFile.size()>0, "FastTMXTiledMap: tmx file should not be empty"); setContentSize(Size::ZERO); @@ -73,39 +73,39 @@ bool TMXTiledMap2::initWithTMXFile(const std::string& tmxFile) { return false; } - CCASSERT( !mapInfo->getTilesets().empty(), "TMXTiledMap2: Map not found. Please check the filename."); + CCASSERT( !mapInfo->getTilesets().empty(), "FastTMXTiledMap: Map not found. Please check the filename."); buildWithMapInfo(mapInfo); return true; } -bool TMXTiledMap2::initWithXML(const std::string& tmxString, const std::string& resourcePath) +bool FastTMXTiledMap::initWithXML(const std::string& tmxString, const std::string& resourcePath) { setContentSize(Size::ZERO); TMXMapInfo *mapInfo = TMXMapInfo::createWithXML(tmxString, resourcePath); - CCASSERT( !mapInfo->getTilesets().empty(), "TMXTiledMap2: Map not found. Please check the filename."); + CCASSERT( !mapInfo->getTilesets().empty(), "FastTMXTiledMap: Map not found. Please check the filename."); buildWithMapInfo(mapInfo); return true; } -TMXTiledMap2::TMXTiledMap2() +FastTMXTiledMap::FastTMXTiledMap() :_mapSize(Size::ZERO) ,_tileSize(Size::ZERO) { } -TMXTiledMap2::~TMXTiledMap2() +FastTMXTiledMap::~FastTMXTiledMap() { } // private -TMXLayer2 * TMXTiledMap2::parseLayer(TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo) +FastTMXLayer * FastTMXTiledMap::parseLayer(TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo) { TMXTilesetInfo *tileset = tilesetForLayer(layerInfo, mapInfo); - TMXLayer2 *layer = TMXLayer2::create(tileset, layerInfo, mapInfo); + FastTMXLayer *layer = FastTMXLayer::create(tileset, layerInfo, mapInfo); // tell the layerinfo to release the ownership of the tiles map. layerInfo->_ownTiles = false; @@ -114,7 +114,7 @@ TMXLayer2 * TMXTiledMap2::parseLayer(TMXLayerInfo *layerInfo, TMXMapInfo *mapInf return layer; } -TMXTilesetInfo * TMXTiledMap2::tilesetForLayer(TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo) +TMXTilesetInfo * FastTMXTiledMap::tilesetForLayer(TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo) { Size size = layerInfo->_layerSize; auto& tilesets = mapInfo->getTilesets(); @@ -158,7 +158,7 @@ TMXTilesetInfo * TMXTiledMap2::tilesetForLayer(TMXLayerInfo *layerInfo, TMXMapIn return nullptr; } -void TMXTiledMap2::buildWithMapInfo(TMXMapInfo* mapInfo) +void FastTMXTiledMap::buildWithMapInfo(TMXMapInfo* mapInfo) { _mapSize = mapInfo->getMapSize(); _tileSize = mapInfo->getTileSize(); @@ -176,7 +176,7 @@ void TMXTiledMap2::buildWithMapInfo(TMXMapInfo* mapInfo) for(const auto &layerInfo : layers) { if (layerInfo->_visible) { - TMXLayer2 *child = parseLayer(layerInfo, mapInfo); + FastTMXLayer *child = parseLayer(layerInfo, mapInfo); addChild(child, idx, idx); // update content size with the max size @@ -192,13 +192,13 @@ void TMXTiledMap2::buildWithMapInfo(TMXMapInfo* mapInfo) } // public -TMXLayer2 * TMXTiledMap2::getLayer(const std::string& layerName) const +FastTMXLayer * FastTMXTiledMap::getLayer(const std::string& layerName) const { CCASSERT(layerName.size() > 0, "Invalid layer name!"); for (auto& child : _children) { - TMXLayer2* layer = dynamic_cast(child); + FastTMXLayer* layer = dynamic_cast(child); if(layer) { if(layerName.compare( layer->getLayerName()) == 0) @@ -212,7 +212,7 @@ TMXLayer2 * TMXTiledMap2::getLayer(const std::string& layerName) const return nullptr; } -TMXObjectGroup * TMXTiledMap2::getObjectGroup(const std::string& groupName) const +TMXObjectGroup * FastTMXTiledMap::getObjectGroup(const std::string& groupName) const { CCASSERT(groupName.size() > 0, "Invalid group name!"); @@ -233,7 +233,7 @@ TMXObjectGroup * TMXTiledMap2::getObjectGroup(const std::string& groupName) cons return nullptr; } -Value TMXTiledMap2::getProperty(const std::string& propertyName) const +Value FastTMXTiledMap::getProperty(const std::string& propertyName) const { if (_properties.find(propertyName) != _properties.end()) return _properties.at(propertyName); @@ -241,7 +241,7 @@ Value TMXTiledMap2::getProperty(const std::string& propertyName) const return Value(); } -Value TMXTiledMap2::getPropertiesForGID(int GID) const +Value FastTMXTiledMap::getPropertiesForGID(int GID) const { if (_tileProperties.find(GID) != _tileProperties.end()) return _tileProperties.at(GID); @@ -249,9 +249,9 @@ Value TMXTiledMap2::getPropertiesForGID(int GID) const return Value(); } -std::string TMXTiledMap2::getDescription() const +std::string FastTMXTiledMap::getDescription() const { - return StringUtils::format("(_children.size())); + return StringUtils::format("(_children.size())); } diff --git a/cocos/2d/CCTMXTiledMap2.h b/cocos/2d/CCFastTMXTiledMap.h similarity index 87% rename from cocos/2d/CCTMXTiledMap2.h rename to cocos/2d/CCFastTMXTiledMap.h index 962ea94242..7a2ccdfa42 100644 --- a/cocos/2d/CCTMXTiledMap2.h +++ b/cocos/2d/CCFastTMXTiledMap.h @@ -33,7 +33,7 @@ THE SOFTWARE. NS_CC_BEGIN class TMXObjectGroup; -class TMXLayer2; +class FastTMXLayer; class TMXLayerInfo; class TMXTilesetInfo; class TMXMapInfo; @@ -47,16 +47,16 @@ class TMXMapInfo; enum { /** Orthogonal orientation */ - TMXOrientationOrtho2, + FastTMXOrientationOrtho, /** Hexagonal orientation */ - TMXOrientationHex2, + FastTMXOrientationHex, /** Isometric orientation */ - TMXOrientationIso2, + FastTMXOrientationIso, }; -/** @brief TMXTiledMap2 knows how to parse and render a TMX map. +/** @brief FastTMXTiledMap knows how to parse and render a TMX map. It adds support for the TMX tiled map format used by http://www.mapeditor.org It supports isometric, hexagonal and orthogonal tiles. @@ -85,9 +85,9 @@ Limitations: - It only supports the XML format (the JSON format is not supported) Technical description: -Each layer is created using an TMXLayer2 (subclass of SpriteBatchNode). If you have 5 layers, then 5 TMXLayer2 will be created, +Each layer is created using an FastTMXLayer (subclass of SpriteBatchNode). If you have 5 layers, then 5 FastTMXLayer will be created, unless the layer visibility is off. In that case, the layer won't be created at all. -You can obtain the layers (TMXLayer2 objects) at runtime by: +You can obtain the layers (FastTMXLayer objects) at runtime by: - map->getChildByTag(tag_number); // 0=1st layer, 1=2nd layer, 2=3rd layer, etc... - map->getLayer(name_of_the_layer); @@ -107,22 +107,22 @@ object->getProperty(name_of_the_property); @since v0.8.1 */ -class CC_DLL TMXTiledMap2 : public Node +class CC_DLL FastTMXTiledMap : public Node { public: /** creates a TMX Tiled Map with a TMX file.*/ - static TMXTiledMap2* create(const std::string& tmxFile); + static FastTMXTiledMap* create(const std::string& tmxFile); /** initializes a TMX Tiled Map with a TMX formatted XML string and a path to TMX resources */ - static TMXTiledMap2* createWithXML(const std::string& tmxString, const std::string& resourcePath); + static FastTMXTiledMap* createWithXML(const std::string& tmxString, const std::string& resourcePath); - /** return the TMXLayer2 for the specific layer */ - TMXLayer2* getLayer(const std::string& layerName) const; + /** return the FastTMXLayer for the specific layer */ + FastTMXLayer* getLayer(const std::string& layerName) const; /** * @js NA * @lua NA */ - CC_DEPRECATED_ATTRIBUTE TMXLayer2* layerNamed(const std::string& layerName) const { return getLayer(layerName); }; + CC_DEPRECATED_ATTRIBUTE FastTMXLayer* layerNamed(const std::string& layerName) const { return getLayer(layerName); }; /** return the TMXObjectGroup for the specific group */ TMXObjectGroup* getObjectGroup(const std::string& groupName) const; @@ -175,12 +175,12 @@ protected: /** * @js ctor */ - TMXTiledMap2(); + FastTMXTiledMap(); /** * @js NA * @lua NA */ - virtual ~TMXTiledMap2(); + virtual ~FastTMXTiledMap(); /** initializes a TMX Tiled Map with a TMX file */ bool initWithTMXFile(const std::string& tmxFile); @@ -188,7 +188,7 @@ protected: /** initializes a TMX Tiled Map with a TMX formatted XML string and a path to TMX resources */ bool initWithXML(const std::string& tmxString, const std::string& resourcePath); - TMXLayer2 * parseLayer(TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo); + FastTMXLayer * parseLayer(TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo); TMXTilesetInfo * tilesetForLayer(TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo); void buildWithMapInfo(TMXMapInfo* mapInfo); @@ -207,7 +207,7 @@ protected: ValueMapIntKey _tileProperties; private: - CC_DISALLOW_COPY_AND_ASSIGN(TMXTiledMap2); + CC_DISALLOW_COPY_AND_ASSIGN(FastTMXTiledMap); }; diff --git a/cocos/2d/CMakeLists.txt b/cocos/2d/CMakeLists.txt index d5d7a0b044..df2077e60e 100644 --- a/cocos/2d/CMakeLists.txt +++ b/cocos/2d/CMakeLists.txt @@ -30,6 +30,8 @@ set(COCOS_2D_SRC 2d/CCComponent.cpp 2d/CCDrawingPrimitives.cpp 2d/CCDrawNode.cpp + 2d/CCFastTMXLayer.cpp + 2d/CCFastTMXTiledMap.cpp 2d/CCFontAtlasCache.cpp 2d/CCFontAtlas.cpp 2d/CCFontCharMap.cpp @@ -65,10 +67,8 @@ set(COCOS_2D_SRC 2d/CCTextFieldTTF.cpp 2d/CCTileMapAtlas.cpp 2d/CCTMXLayer.cpp - 2d/CCTMXLayer2.cpp 2d/CCTMXObjectGroup.cpp 2d/CCTMXTiledMap.cpp - 2d/CCTMXTiledMap2.cpp 2d/CCTMXXMLParser.cpp 2d/CCTransition.cpp 2d/CCTransitionPageTurn.cpp diff --git a/cocos/Android.mk b/cocos/Android.mk index eb8bc29f2a..ce1696c547 100644 --- a/cocos/Android.mk +++ b/cocos/Android.mk @@ -62,10 +62,10 @@ cocos2d.cpp \ 2d/CCSpriteFrame.cpp \ 2d/CCSpriteFrameCache.cpp \ 2d/CCTMXLayer.cpp \ -2d/CCTMXLayer2.cpp \ +2d/CCFastTMXLayer.cpp \ 2d/CCTMXObjectGroup.cpp \ 2d/CCTMXTiledMap.cpp \ -2d/CCTMXTiledMap2.cpp \ +2d/CCFastTMXTiledMap.cpp \ 2d/CCTMXXMLParser.cpp \ 2d/CCTextFieldTTF.cpp \ 2d/CCTileMapAtlas.cpp \ diff --git a/tests/cpp-tests/Classes/TileMapTest/TileMapTest2.cpp b/tests/cpp-tests/Classes/TileMapTest/TileMapTest2.cpp index 3d13a31005..0e8435e133 100644 --- a/tests/cpp-tests/Classes/TileMapTest/TileMapTest2.cpp +++ b/tests/cpp-tests/Classes/TileMapTest/TileMapTest2.cpp @@ -1,8 +1,8 @@ #include "TileMapTest2.h" #include "../testResource.h" -#include "2d/CCTMXLayer2.h" -#include "2d/CCTMXTiledMap2.h" +#include "2d/CCFastTMXLayer.h" +#include "2d/CCFastTMXTiledMap.h" namespace { @@ -287,7 +287,7 @@ TMXOrthoTestNew::TMXOrthoTestNew() //auto color = LayerColor::create( Color4B(64,64,64,255) ); //addChild(color, -1); - auto map = TMXTiledMap2::create("TileMaps/orthogonal-test2.tmx"); + auto map = FastTMXTiledMap::create("TileMaps/orthogonal-test2.tmx"); addChild(map, 0, kTagTileMap); @@ -330,7 +330,7 @@ std::string TMXOrthoTestNew::title() const //------------------------------------------------------------------ TMXOrthoTest2New::TMXOrthoTest2New() { - auto map = TMXTiledMap2::create("TileMaps/orthogonal-test1.tmx"); + auto map = FastTMXTiledMap::create("TileMaps/orthogonal-test1.tmx"); addChild(map, 0, kTagTileMap); Size CC_UNUSED s = map->getContentSize(); @@ -351,7 +351,7 @@ std::string TMXOrthoTest2New::title() const //------------------------------------------------------------------ TMXOrthoTest3New::TMXOrthoTest3New() { - auto map = TMXTiledMap2::create("TileMaps/orthogonal-test3.tmx"); + auto map = FastTMXTiledMap::create("TileMaps/orthogonal-test3.tmx"); addChild(map, 0, kTagTileMap); Size CC_UNUSED s = map->getContentSize(); @@ -373,7 +373,7 @@ std::string TMXOrthoTest3New::title() const //------------------------------------------------------------------ TMXOrthoTest4New::TMXOrthoTest4New() { - auto map = TMXTiledMap2::create("TileMaps/orthogonal-test4.tmx"); + auto map = FastTMXTiledMap::create("TileMaps/orthogonal-test4.tmx"); addChild(map, 0, kTagTileMap); Size CC_UNUSED s1 = map->getContentSize(); @@ -402,7 +402,7 @@ void TMXOrthoTest4New::removeSprite(float dt) { unschedule(schedule_selector(TMXOrthoTest4New::removeSprite)); - auto map = static_cast( getChildByTag(kTagTileMap) ); + auto map = static_cast( getChildByTag(kTagTileMap) ); auto layer = map->getLayer("Layer 0"); auto s = layer->getLayerSize(); @@ -435,7 +435,7 @@ TMXReadWriteTestNew::TMXReadWriteTestNew() { _gid = 0; - auto map = TMXTiledMap2::create("TileMaps/orthogonal-test2.tmx"); + auto map = FastTMXTiledMap::create("TileMaps/orthogonal-test2.tmx"); addChild(map, 0, kTagTileMap); Size CC_UNUSED s = map->getContentSize(); @@ -501,12 +501,12 @@ void TMXReadWriteTestNew::removeSprite(Node* sender) void TMXReadWriteTestNew::updateCol(float dt) { - auto map = (TMXTiledMap2*)getChildByTag(kTagTileMap); - auto layer = (TMXLayer2*)map->getChildByTag(0); + auto map = (FastTMXTiledMap*)getChildByTag(kTagTileMap); + auto layer = (FastTMXLayer*)map->getChildByTag(0); ////----CCLOG("++++atlas quantity: %d", layer->textureAtlas()->getTotalQuads()); ////----CCLOG("++++children: %d", layer->getChildren()->count() ); - + auto s = layer->getLayerSize(); @@ -522,8 +522,8 @@ void TMXReadWriteTestNew::repaintWithGID(float dt) { // unschedule:_cmd); - auto map = (TMXTiledMap2*)getChildByTag(kTagTileMap); - auto layer = (TMXLayer2*)map->getChildByTag(0); + auto map = (FastTMXTiledMap*)getChildByTag(kTagTileMap); + auto layer = (FastTMXLayer*)map->getChildByTag(0); auto s = layer->getLayerSize(); for( int x=0; xgetChildByTag(0); + auto map = (FastTMXTiledMap*)getChildByTag(kTagTileMap); + auto layer = (FastTMXLayer*)map->getChildByTag(0); auto s = layer->getLayerSize(); for( int y=0; y< s.height; y++ ) @@ -565,7 +565,7 @@ TMXHexTestNew::TMXHexTestNew() auto color = LayerColor::create( Color4B(64,64,64,255) ); addChild(color, -1); - auto map = TMXTiledMap2::create("TileMaps/hexa-test.tmx"); + auto map = FastTMXTiledMap::create("TileMaps/hexa-test.tmx"); addChild(map, 0, kTagTileMap); Size CC_UNUSED s = map->getContentSize(); @@ -587,7 +587,7 @@ TMXIsoTestNew::TMXIsoTestNew() auto color = LayerColor::create( Color4B(64,64,64,255) ); addChild(color, -1); - auto map = TMXTiledMap2::create("TileMaps/iso-test.tmx"); + auto map = FastTMXTiledMap::create("TileMaps/iso-test.tmx"); addChild(map, 0, kTagTileMap); // move map to the center of the screen @@ -611,7 +611,7 @@ TMXIsoTest1New::TMXIsoTest1New() auto color = LayerColor::create( Color4B(64,64,64,255) ); addChild(color, -1); - auto map = TMXTiledMap2::create("TileMaps/iso-test1.tmx"); + auto map = FastTMXTiledMap::create("TileMaps/iso-test1.tmx"); addChild(map, 0, kTagTileMap); Size CC_UNUSED s = map->getContentSize(); @@ -635,7 +635,7 @@ TMXIsoTest2New::TMXIsoTest2New() auto color = LayerColor::create( Color4B(64,64,64,255) ); addChild(color, -1); - auto map = TMXTiledMap2::create("TileMaps/iso-test2.tmx"); + auto map = FastTMXTiledMap::create("TileMaps/iso-test2.tmx"); addChild(map, 0, kTagTileMap); Size CC_UNUSED s = map->getContentSize(); @@ -662,7 +662,7 @@ TMXUncompressedTestNew::TMXUncompressedTestNew() auto color = LayerColor::create( Color4B(64,64,64,255) ); addChild(color, -1); - auto map = TMXTiledMap2::create("TileMaps/iso-test2-uncompressed.tmx"); + auto map = FastTMXTiledMap::create("TileMaps/iso-test2-uncompressed.tmx"); addChild(map, 0, kTagTileMap); Size CC_UNUSED s = map->getContentSize(); @@ -675,11 +675,11 @@ TMXUncompressedTestNew::TMXUncompressedTestNew() //unsupported // // testing release map -// TMXLayer2* layer; +// FastTMXLayer* layer; // // auto& children = map->getChildren(); // for(const auto &node : children) { -// layer= static_cast(node); +// layer= static_cast(node); // layer->releaseMap(); // } @@ -697,7 +697,7 @@ std::string TMXUncompressedTestNew::title() const //------------------------------------------------------------------ TMXTilesetTestNew::TMXTilesetTestNew() { - auto map = TMXTiledMap2::create("TileMaps/orthogonal-test5.tmx"); + auto map = FastTMXTiledMap::create("TileMaps/orthogonal-test5.tmx"); addChild(map, 0, kTagTileMap); Size CC_UNUSED s = map->getContentSize(); @@ -716,7 +716,7 @@ std::string TMXTilesetTestNew::title() const //------------------------------------------------------------------ TMXOrthoObjectsTestNew::TMXOrthoObjectsTestNew() { - auto map = TMXTiledMap2::create("TileMaps/ortho-objects.tmx"); + auto map = FastTMXTiledMap::create("TileMaps/ortho-objects.tmx"); addChild(map, -1, kTagTileMap); Size CC_UNUSED s = map->getContentSize(); @@ -743,7 +743,7 @@ void TMXOrthoObjectsTestNew::onDraw(const Mat4 &transform, uint32_t flags) director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, transform); - auto map = static_cast( getChildByTag(kTagTileMap) ); + auto map = static_cast( getChildByTag(kTagTileMap) ); auto pos = map->getPosition(); auto group = map->getObjectGroup("Object Group 1"); @@ -790,7 +790,7 @@ std::string TMXOrthoObjectsTestNew::subtitle() const TMXIsoObjectsTestNew::TMXIsoObjectsTestNew() { - auto map = TMXTiledMap2::create("TileMaps/iso-test-objectgroup.tmx"); + auto map = FastTMXTiledMap::create("TileMaps/iso-test-objectgroup.tmx"); addChild(map, -1, kTagTileMap); Size CC_UNUSED s = map->getContentSize(); @@ -818,7 +818,7 @@ void TMXIsoObjectsTestNew::onDraw(const Mat4 &transform, uint32_t flags) director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, transform); - auto map = (TMXTiledMap2*) getChildByTag(kTagTileMap); + auto map = (FastTMXTiledMap*) getChildByTag(kTagTileMap); auto pos = map->getPosition(); auto group = map->getObjectGroup("Object Group 1"); @@ -863,13 +863,13 @@ std::string TMXIsoObjectsTestNew::subtitle() const TMXResizeTestNew::TMXResizeTestNew() { - auto map = TMXTiledMap2::create("TileMaps/orthogonal-test5.tmx"); + auto map = FastTMXTiledMap::create("TileMaps/orthogonal-test5.tmx"); addChild(map, 0, kTagTileMap); Size CC_UNUSED s = map->getContentSize(); CCLOG("ContentSize: %f, %f", s.width,s.height); - TMXLayer2* layer; + FastTMXLayer* layer; layer = map->getLayer("Layer 0"); auto ls = layer->getLayerSize(); @@ -900,7 +900,7 @@ std::string TMXResizeTestNew::subtitle() const //------------------------------------------------------------------ TMXIsoZorderNew::TMXIsoZorderNew() { - auto map = TMXTiledMap2::create("TileMaps/iso-test-zorder.tmx"); + auto map = FastTMXTiledMap::create("TileMaps/iso-test-zorder.tmx"); addChild(map, 0, kTagTileMap); auto s = map->getContentSize(); @@ -969,7 +969,7 @@ std::string TMXIsoZorderNew::subtitle() const //------------------------------------------------------------------ TMXOrthoZorderNew::TMXOrthoZorderNew() { - auto map = TMXTiledMap2::create("TileMaps/orthogonal-test-zorder.tmx"); + auto map = FastTMXTiledMap::create("TileMaps/orthogonal-test-zorder.tmx"); addChild(map, 0, kTagTileMap); Size CC_UNUSED s = map->getContentSize(); @@ -1030,7 +1030,7 @@ std::string TMXOrthoZorderNew::subtitle() const //------------------------------------------------------------------ TMXIsoVertexZNew::TMXIsoVertexZNew() { - auto map = TMXTiledMap2::create("TileMaps/iso-test-vertexz.tmx"); + auto map = FastTMXTiledMap::create("TileMaps/iso-test-vertexz.tmx"); addChild(map, 0, kTagTileMap); auto s = map->getContentSize(); @@ -1102,7 +1102,7 @@ std::string TMXIsoVertexZNew::subtitle() const //------------------------------------------------------------------ TMXOrthoVertexZNew::TMXOrthoVertexZNew() { - auto map = TMXTiledMap2::create("TileMaps/orthogonal-test-vertexz.tmx"); + auto map = FastTMXTiledMap::create("TileMaps/orthogonal-test-vertexz.tmx"); addChild(map, 0, kTagTileMap); Size CC_UNUSED s = map->getContentSize(); @@ -1173,7 +1173,7 @@ std::string TMXOrthoVertexZNew::subtitle() const //------------------------------------------------------------------ TMXIsoMoveLayerNew::TMXIsoMoveLayerNew() { - auto map = TMXTiledMap2::create("TileMaps/iso-test-movelayer.tmx"); + auto map = FastTMXTiledMap::create("TileMaps/iso-test-movelayer.tmx"); addChild(map, 0, kTagTileMap); map->setPosition(Vec2(-700,-50)); @@ -1200,7 +1200,7 @@ std::string TMXIsoMoveLayerNew::subtitle() const //------------------------------------------------------------------ TMXOrthoMoveLayerNew::TMXOrthoMoveLayerNew() { - auto map = TMXTiledMap2::create("TileMaps/orthogonal-test-movelayer.tmx"); + auto map = FastTMXTiledMap::create("TileMaps/orthogonal-test-movelayer.tmx"); addChild(map, 0, kTagTileMap); Size CC_UNUSED s = map->getContentSize(); @@ -1225,7 +1225,7 @@ std::string TMXOrthoMoveLayerNew::subtitle() const TMXTilePropertyTestNew::TMXTilePropertyTestNew() { - auto map = TMXTiledMap2::create("TileMaps/ortho-tile-property.tmx"); + auto map = FastTMXTiledMap::create("TileMaps/ortho-tile-property.tmx"); addChild(map ,0 ,kTagTileMap); for(int i=1;i<=20;i++){ @@ -1254,7 +1254,7 @@ std::string TMXTilePropertyTestNew::subtitle() const TMXOrthoFlipTestNew::TMXOrthoFlipTestNew() { - auto map = TMXTiledMap2::create("TileMaps/ortho-rotation-test.tmx"); + auto map = FastTMXTiledMap::create("TileMaps/ortho-rotation-test.tmx"); addChild(map, 0, kTagTileMap); Size CC_UNUSED s = map->getContentSize(); @@ -1277,7 +1277,7 @@ std::string TMXOrthoFlipTestNew::title() const TMXOrthoFlipRunTimeTestNew::TMXOrthoFlipRunTimeTestNew() { - auto map = TMXTiledMap2::create("TileMaps/ortho-rotation-test.tmx"); + auto map = FastTMXTiledMap::create("TileMaps/ortho-rotation-test.tmx"); addChild(map, 0, kTagTileMap); auto s = map->getContentSize(); @@ -1301,7 +1301,7 @@ std::string TMXOrthoFlipRunTimeTestNew::subtitle() const void TMXOrthoFlipRunTimeTestNew::flipIt(float dt) { - auto map = (TMXTiledMap2*) getChildByTag(kTagTileMap); + auto map = (FastTMXTiledMap*) getChildByTag(kTagTileMap); auto layer = map->getLayer("Layer 0"); //blue diamond @@ -1349,7 +1349,7 @@ TMXOrthoFromXMLTestNew::TMXOrthoFromXMLTestNew() auto str = String::createWithContentsOfFile(FileUtils::getInstance()->fullPathForFilename(file.c_str()).c_str()); CCASSERT(str != NULL, "Unable to open file"); - auto map = TMXTiledMap2::createWithXML(str->getCString() ,resources.c_str()); + auto map = FastTMXTiledMap::createWithXML(str->getCString() ,resources.c_str()); addChild(map, 0, kTagTileMap); auto s = map->getContentSize(); @@ -1375,7 +1375,7 @@ TMXOrthoXMLFormatTestNew::TMXOrthoXMLFormatTestNew() // 1. load xml format tilemap // 2. gid lower than firstgid is ignored // 3. firstgid in tsx is ignored, tile property in tsx loaded correctly. - auto map = TMXTiledMap2::create("TileMaps/xml-test.tmx"); + auto map = FastTMXTiledMap::create("TileMaps/xml-test.tmx"); addChild(map, 0, kTagTileMap); auto s = map->getContentSize(); @@ -1401,7 +1401,7 @@ std::string TMXOrthoXMLFormatTestNew::title() const //------------------------------------------------------------------ TMXBug987New::TMXBug987New() { - auto map = TMXTiledMap2::create("TileMaps/orthogonal-test6.tmx"); + auto map = FastTMXTiledMap::create("TileMaps/orthogonal-test6.tmx"); addChild(map, 0, kTagTileMap); Size CC_UNUSED s1 = map->getContentSize(); @@ -1429,7 +1429,7 @@ std::string TMXBug987New::subtitle() const //------------------------------------------------------------------ TMXBug787New::TMXBug787New() { - auto map = TMXTiledMap2::create("TileMaps/iso-test-bug787.tmx"); + auto map = FastTMXTiledMap::create("TileMaps/iso-test-bug787.tmx"); addChild(map, 0, kTagTileMap); map->setScale(0.25f); @@ -1452,7 +1452,7 @@ std::string TMXBug787New::subtitle() const //------------------------------------------------------------------ TMXGIDObjectsTestNew::TMXGIDObjectsTestNew() { - auto map = TMXTiledMap2::create("TileMaps/test-object-layer.tmx"); + auto map = FastTMXTiledMap::create("TileMaps/test-object-layer.tmx"); addChild(map, -1, kTagTileMap); Size CC_UNUSED s = map->getContentSize(); @@ -1477,7 +1477,7 @@ void TMXGIDObjectsTestNew::onDraw(const Mat4 &transform, uint32_t flags) director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, transform); - auto map = (TMXTiledMap2*)getChildByTag(kTagTileMap); + auto map = (FastTMXTiledMap*)getChildByTag(kTagTileMap); auto pos = map->getPosition(); auto group = map->getObjectGroup("Object Layer 1"); diff --git a/tests/cpp-tests/Classes/controller.cpp b/tests/cpp-tests/Classes/controller.cpp index b447c4b170..1ec3e85da2 100644 --- a/tests/cpp-tests/Classes/controller.cpp +++ b/tests/cpp-tests/Classes/controller.cpp @@ -85,7 +85,7 @@ Controller g_aTestNames[] = { { "Node: Sprite", [](){return new SpriteTestScene(); } }, { "Node: Sprite3D", [](){ return new Sprite3DTestScene(); }}, { "Node: TileMap", [](){return new TileMapTestScene(); } }, - { "Node: TileMapNew", [](){return new TileMapTestSceneNew(); } }, + { "Node: FastTileMap", [](){return new TileMapTestSceneNew(); } }, { "Node: Text Input", [](){return new TextInputTestScene(); } }, { "Node: UI", [](){ return new UITestScene(); }}, { "Mouse", []() { return new MouseTestScene(); } }, From e03c412e712a472677efd0f8b3be624d485d7b64 Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Thu, 19 Jun 2014 18:05:21 +0800 Subject: [PATCH 09/40] refactor fast tmx rendering --- cocos/2d/CCTMXLayer2.cpp | 112 +++++++++++++++------------------------ cocos/2d/CCTMXLayer2.h | 8 ++- 2 files changed, 47 insertions(+), 73 deletions(-) diff --git a/cocos/2d/CCTMXLayer2.cpp b/cocos/2d/CCTMXLayer2.cpp index dc9fff88d0..48580da5c0 100644 --- a/cocos/2d/CCTMXLayer2.cpp +++ b/cocos/2d/CCTMXLayer2.cpp @@ -117,9 +117,6 @@ TMXLayer2::TMXLayer2() ,_verticesToDraw(0) ,_vertexZvalue(0) ,_useAutomaticVertexZ(false) -,_quads(nullptr) -,_indices(nullptr) -,_numQuads(0) ,_dirty(false) {} @@ -128,8 +125,6 @@ TMXLayer2::~TMXLayer2() CC_SAFE_RELEASE(_tileSet); CC_SAFE_RELEASE(_texture); CC_SAFE_DELETE_ARRAY(_tiles); - CC_SAFE_FREE(_quads); - CC_SAFE_FREE(_indices); } void TMXLayer2::draw(Renderer *renderer, const Mat4& transform, uint32_t flags) @@ -154,29 +149,18 @@ void TMXLayer2::onDraw(const Mat4 &transform, bool transformUpdated) { Size s = Director::getInstance()->getWinSize(); - auto rect = Rect(0, 0, s.width, s.height); + auto rect = Rect(0, 0, s.width, s.height); Mat4 inv = transform; inv.inverse(); rect = RectApplyTransform(rect, inv); - - if (Configuration::getInstance()->supportsShareableVAO()) + + _verticesToDraw = updateTiles(rect); + + if (_quads.size() > 0 && _indices.size() > 0 && _verticesToDraw > 0) { - V3F_T2F_Quad* quads = (V3F_T2F_Quad*)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY); - GLushort* indices = (GLushort *)glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY); - _verticesToDraw = updateTiles(rect, quads, indices); - glUnmapBuffer(GL_ARRAY_BUFFER); - glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER); - } - else - { - _verticesToDraw = updateTiles(rect, nullptr, nullptr); - - if (_quads != nullptr && _indices != nullptr && _verticesToDraw > 0) - { - glBufferData(GL_ARRAY_BUFFER, sizeof(_quads[0]) * _numQuads , _quads, GL_DYNAMIC_DRAW); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(_indices[0]) * _numQuads * 6 , _indices, GL_STATIC_DRAW); - } + glBufferData(GL_ARRAY_BUFFER, sizeof(_quads[0]) * _quads.size() , &_quads[0], GL_DYNAMIC_DRAW); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(_indices[0]) * _quads.size() * 6 , &_indices[0], GL_STATIC_DRAW); } // don't draw more than 65535 vertices since we are using GL_UNSIGNED_SHORT for indices @@ -208,15 +192,14 @@ void TMXLayer2::onDraw(const Mat4 &transform, bool transformUpdated) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } -int TMXLayer2::updateTiles(const Rect& culledRect, V3F_T2F_Quad *quads, GLushort *indices) +int TMXLayer2::updateTiles(const Rect& culledRect) { int tilesUsed = 0; Rect visibleTiles = culledRect; Size mapTileSize = CC_SIZE_PIXELS_TO_POINTS(_mapTileSize); Size tileSize = CC_SIZE_PIXELS_TO_POINTS(_tileSet->_tileSize); - Mat4 nodeToTileTransform = _tileToNodeTransform; - nodeToTileTransform.inverse(); + Mat4 nodeToTileTransform = _tileToNodeTransform.getInversed(); //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. @@ -227,9 +210,7 @@ int TMXLayer2::updateTiles(const Rect& culledRect, V3F_T2F_Quad *quads, GLushort visibleTiles.size.height = ceil(visibleTiles.origin.y + visibleTiles.size.height) - floor(visibleTiles.origin.y); visibleTiles.origin.x = floor(visibleTiles.origin.x); visibleTiles.origin.y = floor(visibleTiles.origin.y); - - V3F_T2F_Quad* quadsTmp = quads; - GLushort* indicesTmp = indices; + // for the bigger tiles. int tilesOverX = 0; @@ -256,20 +237,15 @@ int TMXLayer2::updateTiles(const Rect& culledRect, V3F_T2F_Quad *quads, GLushort } // doesn't support VBO - if (quadsTmp == nullptr) + int quadsNeed = std::min(static_cast((visibleTiles.size.width + tilesOverX) * (visibleTiles.size.height + tilesOverY)), MAX_QUADS_COUNT); + if (_quads.size() < quadsNeed) { - int quadsNeed = std::min(static_cast((visibleTiles.size.width + tilesOverX) * (visibleTiles.size.height + tilesOverY)), MAX_QUADS_COUNT); - if (_numQuads < quadsNeed) - { - _numQuads = quadsNeed; - _quads = (V3F_T2F_Quad*)realloc(_quads, _numQuads * sizeof(V3F_T2F_Quad)); - _indices = (GLushort*)realloc(_indices, _numQuads * 6 * sizeof(GLushort)); - } - - quadsTmp = _quads; - indicesTmp = _indices; + _quads.resize(quadsNeed); + _indices.resize(quadsNeed * 6); } - + + V3F_T2F_Quad* quadsTmp = &_quads[0]; + GLushort* indicesTmp = &_indices[0]; Size texSize = _tileSet->_imageSize; for (int y = visibleTiles.origin.y - tilesOverY; y < visibleTiles.origin.y + visibleTiles.size.height + tilesOverY; ++y) @@ -456,13 +432,13 @@ void TMXLayer2::setupTiles() Mat4 TMXLayer2::tileToNodeTransform() { - float w = _mapTileSize.width / CC_CONTENT_SCALE_FACTOR(); - float h = _mapTileSize.height / CC_CONTENT_SCALE_FACTOR(); - float offY = (_layerSize.height - 1) * h; + float w = _mapTileSize.width / CC_CONTENT_SCALE_FACTOR(); + float h = _mapTileSize.height / CC_CONTENT_SCALE_FACTOR(); + float offY = (_layerSize.height - 1) * h; - switch(_layerOrientation) + switch(_layerOrientation) { - case TMXOrientationOrtho2: + case TMXOrientationOrtho2: { _tileToNodeTransform = Mat4 ( @@ -474,7 +450,7 @@ Mat4 TMXLayer2::tileToNodeTransform() return _tileToNodeTransform; } - case TMXOrientationIso2: + case TMXOrientationIso2: { float offX = (_layerSize.width - 1) * w / 2; _tileToNodeTransform = Mat4 @@ -484,8 +460,8 @@ Mat4 TMXLayer2::tileToNodeTransform() 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f ); - return _tileToNodeTransform; - } + return _tileToNodeTransform; + } case TMXOrientationHex2: { _tileToNodeTransform = Mat4::IDENTITY; @@ -496,22 +472,22 @@ Mat4 TMXLayer2::tileToNodeTransform() _tileToNodeTransform = Mat4::IDENTITY; return _tileToNodeTransform; } - } + } } // removing / getting tiles Sprite* TMXLayer2::getTileAt(const Point& tileCoordinate) { - 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( 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"); - Sprite *tile = nullptr; - int gid = this->getTileGIDAt(tileCoordinate); + Sprite *tile = nullptr; + int gid = this->getTileGIDAt(tileCoordinate); - // if GID == 0, then no tile is present - if( gid ) { - int index = tileCoordinate.x + tileCoordinate.y * _layerSize.width; + // if GID == 0, then no tile is present + if( gid ) { + int index = tileCoordinate.x + tileCoordinate.y * _layerSize.width; auto it = _spriteContainer.find(index); if (it != _spriteContainer.end()) @@ -521,7 +497,7 @@ Sprite* TMXLayer2::getTileAt(const Point& tileCoordinate) else { // tile not created yet. create it - Rect rect = _tileSet->getRectForGID(gid); + Rect rect = _tileSet->getRectForGID(gid); rect = CC_RECT_PIXELS_TO_POINTS(rect); tile = Sprite::createWithTexture(_texture, rect); @@ -537,8 +513,8 @@ Sprite* TMXLayer2::getTileAt(const Point& tileCoordinate) // tile is converted to sprite. setTileForGID(index, 0); } - } - return tile; + } + return tile; } int TMXLayer2::getTileGIDAt(const Point& tileCoordinate, TMXTileFlags* flags/* = nullptr*/) @@ -606,24 +582,24 @@ int TMXLayer2::getVertexZForPos(const Point& pos) void TMXLayer2::removeTileAt(const Point& tileCoordinate) { - CCASSERT( tileCoordinate.x < _layerSize.width && tileCoordinate.y < _layerSize.height && tileCoordinate.x >=0 && tileCoordinate.y >=0, "TMXLayer: invalid position"); + CCASSERT( tileCoordinate.x < _layerSize.width && tileCoordinate.y < _layerSize.height && tileCoordinate.x >=0 && tileCoordinate.y >=0, "TMXLayer: invalid position"); - int gid = this->getTileGIDAt(tileCoordinate); + int gid = this->getTileGIDAt(tileCoordinate); - if( gid ) { + if( gid ) { - int z = tileCoordinate.x + tileCoordinate.y * _layerSize.width; + int z = tileCoordinate.x + tileCoordinate.y * _layerSize.width; - // remove tile from GID map - setTileForGID(z, 0); + // remove tile from GID map + setTileForGID(z, 0); - // remove it from sprites + // remove it from sprites auto it = _spriteContainer.find(z); if (it != _spriteContainer.end()) { this->removeChild(it->second.first); } - } + } } void TMXLayer2::setTileForGID(int index, int gid) diff --git a/cocos/2d/CCTMXLayer2.h b/cocos/2d/CCTMXLayer2.h index b085c03663..d7cfd34a27 100644 --- a/cocos/2d/CCTMXLayer2.h +++ b/cocos/2d/CCTMXLayer2.h @@ -187,7 +187,7 @@ public: protected: bool initWithTilesetInfo(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo); - int updateTiles(const Rect& culledRect, V3F_T2F_Quad *quads, GLushort *indices); + int updateTiles(const Rect& culledRect); void setupVBO(); Point calculateLayerOffset(const Point& offset); @@ -242,11 +242,9 @@ protected: /** tile coordinate to node coordinate transform */ Mat4 _tileToNodeTransform; /** quads to be rendered */ - V3F_T2F_Quad* _quads; - /** number of quads */ - int _numQuads; + std::vector _quads; /** indices */ - GLushort* _indices; + std::vector _indices; bool _dirty; }; From bb4128036c98bd379e4a838508a7d74004b92525 Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Fri, 20 Jun 2014 18:10:18 +0800 Subject: [PATCH 10/40] use quad command for tile map rendering --- cocos/2d/CCFastTMXLayer.cpp | 101 +++++++++++++----------------------- cocos/2d/CCFastTMXLayer.h | 6 ++- 2 files changed, 41 insertions(+), 66 deletions(-) diff --git a/cocos/2d/CCFastTMXLayer.cpp b/cocos/2d/CCFastTMXLayer.cpp index c99820dd09..4f7ef7956d 100644 --- a/cocos/2d/CCFastTMXLayer.cpp +++ b/cocos/2d/CCFastTMXLayer.cpp @@ -45,6 +45,7 @@ THE SOFTWARE. #include "base/CCConfiguration.h" #include "renderer/CCRenderer.h" #include "deprecated/CCString.h" +#include "renderer/CCGLProgramStateCache.h" NS_CC_BEGIN @@ -129,70 +130,42 @@ FastTMXLayer::~FastTMXLayer() void FastTMXLayer::draw(Renderer *renderer, const Mat4& transform, uint32_t flags) { - _customCommand.init(_globalZOrder); - _customCommand.func = CC_CALLBACK_0(FastTMXLayer::onDraw, this, transform, flags); - renderer->addCommand(&_customCommand); -} - -void FastTMXLayer::onDraw(const Mat4 &transform, bool transformUpdated) -{ - GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_TEX_COORD); - GL::bindTexture2D( _texture->getName() ); - - - // tex coords + indices - glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]); - - - if( transformUpdated || _dirty ) + if( flags != 0 || _dirty ) { - Size s = Director::getInstance()->getWinSize(); auto rect = Rect(0, 0, s.width, s.height); - + Mat4 inv = transform; inv.inverse(); rect = RectApplyTransform(rect, inv); - _verticesToDraw = updateTiles(rect); + _verticesToDraw = updateTiles(rect, renderer); - if (_quads.size() > 0 && _indices.size() > 0 && _verticesToDraw > 0) - { - glBufferData(GL_ARRAY_BUFFER, sizeof(_quads[0]) * _quads.size() , &_quads[0], GL_DYNAMIC_DRAW); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(_indices[0]) * _quads.size() * 6 , &_indices[0], GL_STATIC_DRAW); - } - // don't draw more than 65535 vertices since we are using GL_UNSIGNED_SHORT for indices _verticesToDraw = std::min(_verticesToDraw, 65535); _dirty = false; - } - - if(_verticesToDraw > 0) { - - getGLProgram()->use(); - getGLProgram()->setUniformsForBuiltins(_modelViewTransform); - - // vertices - glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(V3F_T2F), (GLvoid*) offsetof(V3F_T2F, vertices)); - - // tex coords - glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, sizeof(V3F_T2F), (GLvoid*) offsetof(V3F_T2F, texCoords)); - - // color - glVertexAttrib4f(GLProgram::VERTEX_ATTRIB_COLOR, _displayedColor.r/255.0f, _displayedColor.g/255.0f, _displayedColor.b/255.0f, _displayedOpacity/255.0f); - glDrawElements(GL_TRIANGLES, _verticesToDraw, GL_UNSIGNED_SHORT, nullptr); - CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,_verticesToDraw); } - - // cleanup - glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + + auto glprogramState = GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP); + for( int index = 0; index < _verticesToDraw/6; ++index) + { + float z = _quads[index].bl.vertices.z; + _renderCommands[index].init(z, _texture->getName(), glprogramState, BlendFunc::ALPHA_PREMULTIPLIED, &_quads[index], 1, transform); + renderer->addCommand(&_renderCommands[index]); + } + +// _customCommand.init(_globalZOrder); +// _customCommand.func = CC_CALLBACK_0(FastTMXLayer::onDraw, this, transform, flags); +// renderer->addCommand(&_customCommand); } -int FastTMXLayer::updateTiles(const Rect& culledRect) +void FastTMXLayer::onDraw(const Mat4 &transform, bool transformUpdated) +{ +} + +int FastTMXLayer::updateTiles(const Rect& culledRect, Renderer* renderer) { int tilesUsed = 0; @@ -237,15 +210,13 @@ int FastTMXLayer::updateTiles(const Rect& culledRect) } // doesn't support VBO - int quadsNeed = std::min(static_cast((visibleTiles.size.width + tilesOverX) * (visibleTiles.size.height + tilesOverY)), MAX_QUADS_COUNT); + int quadsNeed = std::min(static_cast((visibleTiles.size.width + 2 *tilesOverX) * (visibleTiles.size.height + 2 * tilesOverY)), MAX_QUADS_COUNT); if (_quads.size() < quadsNeed) { _quads.resize(quadsNeed); + _renderCommands.resize(quadsNeed); _indices.resize(quadsNeed * 6); } - - V3F_T2F_Quad* quadsTmp = &_quads[0]; - GLushort* indicesTmp = &_indices[0]; Size texSize = _tileSet->_imageSize; for (int y = visibleTiles.origin.y - tilesOverY; y < visibleTiles.origin.y + visibleTiles.size.height + tilesOverY; ++y) @@ -263,7 +234,7 @@ int FastTMXLayer::updateTiles(const Rect& culledRect) if(tileGID!=0) { - V3F_T2F_Quad *quad = &quadsTmp[tilesUsed]; + V3F_C4B_T2F_Quad *quad = &_quads[tilesUsed]; Vec3 nodePos(static_cast(x), static_cast(y), 0); _tileToNodeTransform.transformPoint(&nodePos); @@ -340,18 +311,20 @@ int FastTMXLayer::updateTiles(const Rect& culledRect) quad->tl.texCoords.v = top; quad->tr.texCoords.u = right; quad->tr.texCoords.v = top; - - - GLushort *idxbase = indicesTmp + tilesUsed * 6; + + quad->bl.colors = Color4B::WHITE; + quad->br.colors = Color4B::WHITE; + quad->tl.colors = Color4B::WHITE; + quad->tr.colors = Color4B::WHITE; + int vertexbase = tilesUsed * 4; - - idxbase[0] = vertexbase; - idxbase[1] = vertexbase + 1; - idxbase[2] = vertexbase + 2; - idxbase[3] = vertexbase + 3; - idxbase[4] = vertexbase + 2; - idxbase[5] = vertexbase + 1; - + _indices[tilesUsed * 6 + 0] = vertexbase; + _indices[tilesUsed * 6 + 1] = vertexbase + 1; + _indices[tilesUsed * 6 + 2] = vertexbase + 2; + _indices[tilesUsed * 6 + 3] = vertexbase + 3; + _indices[tilesUsed * 6 + 4] = vertexbase + 2; + _indices[tilesUsed * 6 + 5]= vertexbase + 1; + tilesUsed++; } diff --git a/cocos/2d/CCFastTMXLayer.h b/cocos/2d/CCFastTMXLayer.h index a9e57e256e..052c6a8184 100644 --- a/cocos/2d/CCFastTMXLayer.h +++ b/cocos/2d/CCFastTMXLayer.h @@ -31,6 +31,7 @@ THE SOFTWARE. #include "CCTMXXMLParser.h" #include "CCNode.h" #include "renderer/CCCustomCommand.h" +#include "renderer/CCQuadCommand.h" #include @@ -187,7 +188,7 @@ public: protected: bool initWithTilesetInfo(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo); - int updateTiles(const Rect& culledRect); + int updateTiles(const Rect& culledRect, Renderer* renderer); void setupVBO(); Vec2 calculateLayerOffset(const Vec2& offset); @@ -242,7 +243,8 @@ protected: /** tile coordinate to node coordinate transform */ Mat4 _tileToNodeTransform; /** quads to be rendered */ - std::vector _quads; + std::vector _quads; + std::vector _renderCommands; /** indices */ std::vector _indices; bool _dirty; From 740372ceb3122fa7d51edb1a2c2e6cbaf7e3a73b Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Tue, 24 Jun 2014 11:04:17 +0800 Subject: [PATCH 11/40] new version of rendering for tile map --- cocos/2d/CCFastTMXLayer.cpp | 42 ++++++++++++++++++++++++++------ cocos/2d/CCFastTMXLayer.h | 4 +++ cocos/base/CCDirector.cpp | 2 +- cocos/renderer/CCQuadCommand.cpp | 1 + cocos/renderer/CCQuadCommand.h | 28 ++++++++++++++++++--- cocos/renderer/CCRenderer.cpp | 8 +++++- 6 files changed, 73 insertions(+), 12 deletions(-) diff --git a/cocos/2d/CCFastTMXLayer.cpp b/cocos/2d/CCFastTMXLayer.cpp index 4f7ef7956d..169df9c211 100644 --- a/cocos/2d/CCFastTMXLayer.cpp +++ b/cocos/2d/CCFastTMXLayer.cpp @@ -128,6 +128,11 @@ FastTMXLayer::~FastTMXLayer() CC_SAFE_DELETE_ARRAY(_tiles); } +bool sortQuadCommand(const V3F_C4B_T2F_Quad& a, const V3F_C4B_T2F_Quad& b) +{ + return a.bl.vertices.z < b.bl.vertices.z; +} + void FastTMXLayer::draw(Renderer *renderer, const Mat4& transform, uint32_t flags) { if( flags != 0 || _dirty ) @@ -140,7 +145,7 @@ void FastTMXLayer::draw(Renderer *renderer, const Mat4& transform, uint32_t flag rect = RectApplyTransform(rect, inv); _verticesToDraw = updateTiles(rect, renderer); - + std::sort(_quads.begin(), _quads.end(), sortQuadCommand); // don't draw more than 65535 vertices since we are using GL_UNSIGNED_SHORT for indices _verticesToDraw = std::min(_verticesToDraw, 65535); @@ -148,13 +153,22 @@ void FastTMXLayer::draw(Renderer *renderer, const Mat4& transform, uint32_t flag } - auto glprogramState = GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP); - for( int index = 0; index < _verticesToDraw/6; ++index) + auto glprogramState = GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR); + int index = 0; + for(const auto& quadNumberIter : _quadsNumber) { - float z = _quads[index].bl.vertices.z; - _renderCommands[index].init(z, _texture->getName(), glprogramState, BlendFunc::ALPHA_PREMULTIPLIED, &_quads[index], 1, transform); - renderer->addCommand(&_renderCommands[index]); + int z = quadNumberIter.first; + auto& quadCommand = _renderCommands2[quadNumberIter.first]; + quadCommand.init(z, _texture->getName(), glprogramState, BlendFunc::ALPHA_PREMULTIPLIED, &_quads[index], quadNumberIter.second, transform); + index += quadNumberIter.second; + renderer->addCommand(&quadCommand); } +// for( int index = 0; index < _verticesToDraw/6; ++index) +// { +// float z = _quads[index].bl.vertices.z; +// _renderCommands[index].init(z, _texture->getName(), glprogramState, BlendFunc::ALPHA_PREMULTIPLIED, &_quads[index], 1, transform); +// renderer->addCommand(&_renderCommands[index]); +// } // _customCommand.init(_globalZOrder); // _customCommand.func = CC_CALLBACK_0(FastTMXLayer::onDraw, this, transform, flags); @@ -218,6 +232,9 @@ int FastTMXLayer::updateTiles(const Rect& culledRect, Renderer* renderer) _indices.resize(quadsNeed * 6); } + //clear quadsNumber + _quadsNumber.clear(); + Size texSize = _tileSet->_imageSize; for (int y = visibleTiles.origin.y - tilesOverY; y < visibleTiles.origin.y + visibleTiles.size.height + tilesOverY; ++y) { @@ -242,7 +259,18 @@ int FastTMXLayer::updateTiles(const Rect& culledRect, Renderer* renderer) float left, right, top, bottom, z; z = getVertexZForPos(Vec2(x, y)); - + + //add quadsNumber + auto quadsNumberIter = _quadsNumber.find(z); + if(quadsNumberIter == _quadsNumber.end()) + { + _quadsNumber.insert(std::make_pair(z, 1)); + } + else + { + quadsNumberIter->second += 1; + } + // vertices if (tileGID & kTMXTileDiagonalFlag) { diff --git a/cocos/2d/CCFastTMXLayer.h b/cocos/2d/CCFastTMXLayer.h index 052c6a8184..7291921f4c 100644 --- a/cocos/2d/CCFastTMXLayer.h +++ b/cocos/2d/CCFastTMXLayer.h @@ -34,6 +34,7 @@ THE SOFTWARE. #include "renderer/CCQuadCommand.h" #include +#include NS_CC_BEGIN @@ -245,6 +246,9 @@ protected: /** quads to be rendered */ std::vector _quads; std::vector _renderCommands; + + std::map _quadsNumber; + std::unordered_map _renderCommands2; /** indices */ std::vector _indices; bool _dirty; diff --git a/cocos/base/CCDirector.cpp b/cocos/base/CCDirector.cpp index 9d1e02f187..a2d22b46e9 100644 --- a/cocos/base/CCDirector.cpp +++ b/cocos/base/CCDirector.cpp @@ -1066,7 +1066,7 @@ void Director::showStats() { char buffer[30]; - if (_accumDt > CC_DIRECTOR_STATS_INTERVAL) + if (_accumDt > CC_DIRECTOR_STATS_INTERVAL * 20) { _frameRate = _frames / _accumDt; _frames = 0; diff --git a/cocos/renderer/CCQuadCommand.cpp b/cocos/renderer/CCQuadCommand.cpp index 4b497ff253..eb4fe6e66e 100644 --- a/cocos/renderer/CCQuadCommand.cpp +++ b/cocos/renderer/CCQuadCommand.cpp @@ -40,6 +40,7 @@ QuadCommand::QuadCommand() ,_blendType(BlendFunc::DISABLE) ,_quads(nullptr) ,_quadsCount(0) +,_preMultiplyMV(true) { _type = RenderCommand::Type::QUAD_COMMAND; } diff --git a/cocos/renderer/CCQuadCommand.h b/cocos/renderer/CCQuadCommand.h index 8f3d3622f1..7024a2050f 100644 --- a/cocos/renderer/CCQuadCommand.h +++ b/cocos/renderer/CCQuadCommand.h @@ -38,7 +38,7 @@ public: static const int MATERIAL_ID_DO_NOT_BATCH = 0; QuadCommand(); - ~QuadCommand(); + virtual ~QuadCommand(); /** Initializes the command with a globalZOrder, a texture ID, a `GLProgram`, a blending function, a pointer to quads, * quantity of quads, and the Model View transform to be used for the quads */ @@ -55,10 +55,10 @@ public: inline GLProgramState* getGLProgramState() const { return _glProgramState; } inline BlendFunc getBlendType() const { return _blendType; } inline const Mat4& getModelView() const { return _mv; } - + inline bool getPremultiplyMV() const { return _preMultiplyMV; } protected: - void generateMaterialID(); + virtual void generateMaterialID(); uint32_t _materialID; GLuint _textureID; @@ -67,7 +67,29 @@ protected: V3F_C4B_T2F_Quad* _quads; ssize_t _quadsCount; Mat4 _mv; + bool _preMultiplyMV; }; + +class NopreMultiplyMVQuadCommand : public QuadCommand +{ +public: + NopreMultiplyMVQuadCommand() + :QuadCommand() + { + _preMultiplyMV = false; + } + + virtual ~NopreMultiplyMVQuadCommand() + { + } + +protected: + virtual void generateMaterialID() + { + _materialID = QuadCommand::MATERIAL_ID_DO_NOT_BATCH; + } +}; + NS_CC_END #endif //_CC_QUADCOMMAND_H_ diff --git a/cocos/renderer/CCRenderer.cpp b/cocos/renderer/CCRenderer.cpp index 222118da6f..5a5964a28d 100644 --- a/cocos/renderer/CCRenderer.cpp +++ b/cocos/renderer/CCRenderer.cpp @@ -296,7 +296,13 @@ void Renderer::visitRenderQueue(const RenderQueue& queue) _batchedQuadCommands.push_back(cmd); memcpy(_quads + _numQuads, cmd->getQuads(), sizeof(V3F_C4B_T2F_Quad) * cmd->getQuadCount()); - convertToWorldCoordinates(_quads + _numQuads, cmd->getQuadCount(), cmd->getModelView()); + + //only multiply mv when getPremultiplyMV is true + if(cmd->getPremultiplyMV()) + { + convertToWorldCoordinates(_quads + _numQuads, cmd->getQuadCount(), cmd->getModelView()); + } + _numQuads += cmd->getQuadCount(); From 67f83f43b042fcf4b4cb3f42a52ab06d4ff0c3f8 Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Wed, 25 Jun 2014 10:04:20 +0800 Subject: [PATCH 12/40] clean up Fast TMX code --- cocos/2d/CCFastTMXLayer.cpp | 30 ++---------------------------- cocos/2d/CCFastTMXLayer.h | 10 +--------- 2 files changed, 3 insertions(+), 37 deletions(-) diff --git a/cocos/2d/CCFastTMXLayer.cpp b/cocos/2d/CCFastTMXLayer.cpp index 169df9c211..aef37b38b3 100644 --- a/cocos/2d/CCFastTMXLayer.cpp +++ b/cocos/2d/CCFastTMXLayer.cpp @@ -144,7 +144,7 @@ void FastTMXLayer::draw(Renderer *renderer, const Mat4& transform, uint32_t flag inv.inverse(); rect = RectApplyTransform(rect, inv); - _verticesToDraw = updateTiles(rect, renderer); + _verticesToDraw = updateTiles(rect); std::sort(_quads.begin(), _quads.end(), sortQuadCommand); // don't draw more than 65535 vertices since we are using GL_UNSIGNED_SHORT for indices _verticesToDraw = std::min(_verticesToDraw, 65535); @@ -163,23 +163,9 @@ void FastTMXLayer::draw(Renderer *renderer, const Mat4& transform, uint32_t flag index += quadNumberIter.second; renderer->addCommand(&quadCommand); } -// for( int index = 0; index < _verticesToDraw/6; ++index) -// { -// float z = _quads[index].bl.vertices.z; -// _renderCommands[index].init(z, _texture->getName(), glprogramState, BlendFunc::ALPHA_PREMULTIPLIED, &_quads[index], 1, transform); -// renderer->addCommand(&_renderCommands[index]); -// } - -// _customCommand.init(_globalZOrder); -// _customCommand.func = CC_CALLBACK_0(FastTMXLayer::onDraw, this, transform, flags); -// renderer->addCommand(&_customCommand); } -void FastTMXLayer::onDraw(const Mat4 &transform, bool transformUpdated) -{ -} - -int FastTMXLayer::updateTiles(const Rect& culledRect, Renderer* renderer) +int FastTMXLayer::updateTiles(const Rect& culledRect) { int tilesUsed = 0; @@ -228,8 +214,6 @@ int FastTMXLayer::updateTiles(const Rect& culledRect, Renderer* renderer) if (_quads.size() < quadsNeed) { _quads.resize(quadsNeed); - _renderCommands.resize(quadsNeed); - _indices.resize(quadsNeed * 6); } //clear quadsNumber @@ -345,14 +329,6 @@ int FastTMXLayer::updateTiles(const Rect& culledRect, Renderer* renderer) quad->tl.colors = Color4B::WHITE; quad->tr.colors = Color4B::WHITE; - int vertexbase = tilesUsed * 4; - _indices[tilesUsed * 6 + 0] = vertexbase; - _indices[tilesUsed * 6 + 1] = vertexbase + 1; - _indices[tilesUsed * 6 + 2] = vertexbase + 2; - _indices[tilesUsed * 6 + 3] = vertexbase + 3; - _indices[tilesUsed * 6 + 4] = vertexbase + 2; - _indices[tilesUsed * 6 + 5]= vertexbase + 1; - tilesUsed++; } @@ -609,8 +585,6 @@ void FastTMXLayer::setTileForGID(int index, int gid) _dirty = true; } - - void FastTMXLayer::removeChild(Node* node, bool cleanup) { int tag = node->getTag(); diff --git a/cocos/2d/CCFastTMXLayer.h b/cocos/2d/CCFastTMXLayer.h index 7291921f4c..e1eb458d63 100644 --- a/cocos/2d/CCFastTMXLayer.h +++ b/cocos/2d/CCFastTMXLayer.h @@ -189,7 +189,7 @@ public: protected: bool initWithTilesetInfo(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo); - int updateTiles(const Rect& culledRect, Renderer* renderer); + int updateTiles(const Rect& culledRect); void setupVBO(); Vec2 calculateLayerOffset(const Vec2& offset); @@ -198,8 +198,6 @@ protected: Mat4 tileToNodeTransform(); Rect tileBoundsForClipTransform(const Mat4 &tileToClip); - - void onDraw(const Mat4 &transform, bool transformUpdated); int getVertexZForPos(const Vec2& pos); @@ -207,7 +205,6 @@ protected: void setTileForGID(int index, int gid); protected: - CustomCommand _customCommand; //! name of the layer std::string _layerName; @@ -240,17 +237,12 @@ protected: int _vertexZvalue; bool _useAutomaticVertexZ; - /** tile coordinate to node coordinate transform */ Mat4 _tileToNodeTransform; /** quads to be rendered */ std::vector _quads; - std::vector _renderCommands; - std::map _quadsNumber; std::unordered_map _renderCommands2; - /** indices */ - std::vector _indices; bool _dirty; }; From bda970820610ce4cded05ba0d28942a3d99d6c32 Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Wed, 25 Jun 2014 10:06:37 +0800 Subject: [PATCH 13/40] clean up Fast TMX code2 --- cocos/2d/CCFastTMXLayer.cpp | 4 +--- cocos/2d/CCFastTMXLayer.h | 1 - 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/cocos/2d/CCFastTMXLayer.cpp b/cocos/2d/CCFastTMXLayer.cpp index aef37b38b3..8683acba89 100644 --- a/cocos/2d/CCFastTMXLayer.cpp +++ b/cocos/2d/CCFastTMXLayer.cpp @@ -55,7 +55,6 @@ namespace } // FastTMXLayer - init & alloc & dealloc - FastTMXLayer * FastTMXLayer::create(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo) { FastTMXLayer *ret = new FastTMXLayer(); @@ -66,6 +65,7 @@ FastTMXLayer * FastTMXLayer::create(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *l } return nullptr; } + bool FastTMXLayer::initWithTilesetInfo(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo) { @@ -635,7 +635,6 @@ void FastTMXLayer::parseInternalProperties() } } - //CCTMXLayer2 - obtaining positions, offset Vec2 FastTMXLayer::calculateLayerOffset(const Vec2& pos) { @@ -776,4 +775,3 @@ std::string FastTMXLayer::getDescription() const } NS_CC_END - diff --git a/cocos/2d/CCFastTMXLayer.h b/cocos/2d/CCFastTMXLayer.h index e1eb458d63..07571f2fdf 100644 --- a/cocos/2d/CCFastTMXLayer.h +++ b/cocos/2d/CCFastTMXLayer.h @@ -252,4 +252,3 @@ protected: NS_CC_END #endif //__CCTMX_LAYER2_H__ - From 1919ebc61f74d85df5c299891caed878f2c9db69 Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Wed, 25 Jun 2014 11:08:28 +0800 Subject: [PATCH 14/40] add fast tmx files to Xcode project file --- build/cocos2d_libs.xcodeproj/project.pbxproj | 24 ++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/build/cocos2d_libs.xcodeproj/project.pbxproj b/build/cocos2d_libs.xcodeproj/project.pbxproj index b217e523e1..d281a49e78 100644 --- a/build/cocos2d_libs.xcodeproj/project.pbxproj +++ b/build/cocos2d_libs.xcodeproj/project.pbxproj @@ -1751,6 +1751,14 @@ 50FCEBCB18C72017004AD434 /* WidgetReaderProtocol.h in Headers */ = {isa = PBXBuildFile; fileRef = 50FCEB9218C72017004AD434 /* WidgetReaderProtocol.h */; }; 50FCEBCC18C72017004AD434 /* WidgetReaderProtocol.h in Headers */ = {isa = PBXBuildFile; fileRef = 50FCEB9218C72017004AD434 /* WidgetReaderProtocol.h */; }; A07A4CAF1783777C0073F6A7 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1551A342158F2AB200E66CFE /* Foundation.framework */; }; + B24AA985195A675C007B4522 /* CCFastTMXLayer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B24AA981195A675C007B4522 /* CCFastTMXLayer.cpp */; }; + B24AA986195A675C007B4522 /* CCFastTMXLayer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B24AA981195A675C007B4522 /* CCFastTMXLayer.cpp */; }; + B24AA987195A675C007B4522 /* CCFastTMXLayer.h in Headers */ = {isa = PBXBuildFile; fileRef = B24AA982195A675C007B4522 /* CCFastTMXLayer.h */; }; + B24AA988195A675C007B4522 /* CCFastTMXLayer.h in Headers */ = {isa = PBXBuildFile; fileRef = B24AA982195A675C007B4522 /* CCFastTMXLayer.h */; }; + B24AA989195A675C007B4522 /* CCFastTMXTiledMap.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B24AA983195A675C007B4522 /* CCFastTMXTiledMap.cpp */; }; + B24AA98A195A675C007B4522 /* CCFastTMXTiledMap.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B24AA983195A675C007B4522 /* CCFastTMXTiledMap.cpp */; }; + B24AA98B195A675C007B4522 /* CCFastTMXTiledMap.h in Headers */ = {isa = PBXBuildFile; fileRef = B24AA984195A675C007B4522 /* CCFastTMXTiledMap.h */; }; + B24AA98C195A675C007B4522 /* CCFastTMXTiledMap.h in Headers */ = {isa = PBXBuildFile; fileRef = B24AA984195A675C007B4522 /* CCFastTMXTiledMap.h */; }; B29594B41926D5EC003EEF37 /* CCMeshCommand.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B29594B21926D5EC003EEF37 /* CCMeshCommand.cpp */; }; B29594B51926D5EC003EEF37 /* CCMeshCommand.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B29594B21926D5EC003EEF37 /* CCMeshCommand.cpp */; }; B29594B61926D5EC003EEF37 /* CCMeshCommand.h in Headers */ = {isa = PBXBuildFile; fileRef = B29594B31926D5EC003EEF37 /* CCMeshCommand.h */; }; @@ -2840,6 +2848,10 @@ A07A4F3B178387670073F6A7 /* libchipmunk iOS.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libchipmunk iOS.a"; sourceTree = BUILT_PRODUCTS_DIR; }; A07A4F9E1783876B0073F6A7 /* libbox2d iOS.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libbox2d iOS.a"; sourceTree = BUILT_PRODUCTS_DIR; }; A07A4FB4178387730073F6A7 /* libCocosDenshion iOS.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libCocosDenshion iOS.a"; sourceTree = BUILT_PRODUCTS_DIR; }; + B24AA981195A675C007B4522 /* CCFastTMXLayer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCFastTMXLayer.cpp; sourceTree = ""; }; + B24AA982195A675C007B4522 /* CCFastTMXLayer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCFastTMXLayer.h; sourceTree = ""; }; + B24AA983195A675C007B4522 /* CCFastTMXTiledMap.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCFastTMXTiledMap.cpp; sourceTree = ""; }; + B24AA984195A675C007B4522 /* CCFastTMXTiledMap.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCFastTMXTiledMap.h; sourceTree = ""; }; B29594AF1926D5D9003EEF37 /* ccShader_3D_Color.frag */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; path = ccShader_3D_Color.frag; sourceTree = ""; }; B29594B01926D5D9003EEF37 /* ccShader_3D_ColorTex.frag */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; path = ccShader_3D_ColorTex.frag; sourceTree = ""; }; B29594B11926D5D9003EEF37 /* ccShader_3D_PositionTex.vert */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; path = ccShader_3D_PositionTex.vert; sourceTree = ""; }; @@ -3392,6 +3404,10 @@ 1A5702DF180BCE610088DEC7 /* tilemap-parallax-nodes */ = { isa = PBXGroup; children = ( + B24AA981195A675C007B4522 /* CCFastTMXLayer.cpp */, + B24AA982195A675C007B4522 /* CCFastTMXLayer.h */, + B24AA983195A675C007B4522 /* CCFastTMXTiledMap.cpp */, + B24AA984195A675C007B4522 /* CCFastTMXTiledMap.h */, 1A5702FE180BCE890088DEC7 /* CCParallaxNode.cpp */, 1A5702FF180BCE890088DEC7 /* CCParallaxNode.h */, 1A5702E0180BCE750088DEC7 /* CCTileMapAtlas.cpp */, @@ -5187,6 +5203,7 @@ 1AD71EB7180E26E600808F54 /* BoneData.h in Headers */, 50ABBD5A1925AB0000A911A9 /* Vec2.h in Headers */, 1AD71EBB180E26E600808F54 /* CCSkeleton.h in Headers */, + B24AA987195A675C007B4522 /* CCFastTMXLayer.h in Headers */, 50FCEBA118C72017004AD434 /* LayoutReader.h in Headers */, 50ABBEBD1925AB6F00A911A9 /* ccUtils.h in Headers */, 1AD71EBF180E26E600808F54 /* CCSkeletonAnimation.h in Headers */, @@ -5240,6 +5257,7 @@ 50ABBE211925AB6F00A911A9 /* atitc.h in Headers */, 1A8C5999180E930E00EF57C3 /* CCActionNode.h in Headers */, 1A8C599D180E930E00EF57C3 /* CCActionObject.h in Headers */, + B24AA98B195A675C007B4522 /* CCFastTMXTiledMap.h in Headers */, B29594B61926D5EC003EEF37 /* CCMeshCommand.h in Headers */, 50ABBE371925AB6F00A911A9 /* CCConsole.h in Headers */, 1A8C59A1180E930E00EF57C3 /* CCArmature.h in Headers */, @@ -5581,6 +5599,7 @@ 1A570228180BCC1A0088DEC7 /* CCParticleExamples.h in Headers */, 1A57022C180BCC1A0088DEC7 /* CCParticleSystem.h in Headers */, 1A570230180BCC1A0088DEC7 /* CCParticleSystemQuad.h in Headers */, + B24AA988195A675C007B4522 /* CCFastTMXLayer.h in Headers */, 5034CA2C191D591100CE6051 /* ccShader_PositionTextureA8Color.vert in Headers */, 50ABBE981925AB6F00A911A9 /* CCProtocols.h in Headers */, 2905FA8B18CF08D100240AA3 /* UITextField.h in Headers */, @@ -5753,6 +5772,7 @@ 1A8C59B2180E930E00EF57C3 /* CCBatchNode.h in Headers */, 1A8C59B6180E930E00EF57C3 /* CCBone.h in Headers */, 1A8C59BA180E930E00EF57C3 /* CCColliderDetector.h in Headers */, + B24AA98C195A675C007B4522 /* CCFastTMXTiledMap.h in Headers */, 50ABBEAE1925AB6F00A911A9 /* ccTypes.h in Headers */, 1A8C59BE180E930E00EF57C3 /* CCComAttribute.h in Headers */, 50ABBE261925AB6F00A911A9 /* base64.h in Headers */, @@ -6250,6 +6270,7 @@ 1A5701E2180BCB8C0088DEC7 /* CCScene.cpp in Sources */, 1A12775C18DFCC590005F345 /* CCTweenFunction.cpp in Sources */, 1A5701E6180BCB8C0088DEC7 /* CCTransition.cpp in Sources */, + B24AA985195A675C007B4522 /* CCFastTMXLayer.cpp in Sources */, 1A5701EA180BCB8C0088DEC7 /* CCTransitionPageTurn.cpp in Sources */, 50ABBDAD1925AB4100A911A9 /* CCRenderer.cpp in Sources */, 1A5701EE180BCB8C0088DEC7 /* CCTransitionProgress.cpp in Sources */, @@ -6271,6 +6292,7 @@ 1A57027E180BCC900088DEC7 /* CCSprite.cpp in Sources */, 1A570282180BCC900088DEC7 /* CCSpriteBatchNode.cpp in Sources */, 1A570286180BCC900088DEC7 /* CCSpriteFrame.cpp in Sources */, + B24AA989195A675C007B4522 /* CCFastTMXTiledMap.cpp in Sources */, 50ABC0191926664800A911A9 /* CCSAXParser.cpp in Sources */, 1A57028A180BCC900088DEC7 /* CCSpriteFrameCache.cpp in Sources */, 1A570292180BCCAB0088DEC7 /* CCAnimation.cpp in Sources */, @@ -6724,6 +6746,8 @@ 1A570222180BCC1A0088DEC7 /* CCParticleBatchNode.cpp in Sources */, 1A570226180BCC1A0088DEC7 /* CCParticleExamples.cpp in Sources */, 1A57022A180BCC1A0088DEC7 /* CCParticleSystem.cpp in Sources */, + B24AA98A195A675C007B4522 /* CCFastTMXTiledMap.cpp in Sources */, + B24AA986195A675C007B4522 /* CCFastTMXLayer.cpp in Sources */, 1A57022E180BCC1A0088DEC7 /* CCParticleSystemQuad.cpp in Sources */, 50ABBD901925AB4100A911A9 /* CCGLProgramCache.cpp in Sources */, 2905FA5718CF08D100240AA3 /* UILayout.cpp in Sources */, From c1b5984ce88bc07f394191735683c7dfe77720dc Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Wed, 25 Jun 2014 11:08:47 +0800 Subject: [PATCH 15/40] using stable sort for quads --- cocos/2d/CCFastTMXLayer.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cocos/2d/CCFastTMXLayer.cpp b/cocos/2d/CCFastTMXLayer.cpp index 8683acba89..a5b6638af8 100644 --- a/cocos/2d/CCFastTMXLayer.cpp +++ b/cocos/2d/CCFastTMXLayer.cpp @@ -118,8 +118,9 @@ FastTMXLayer::FastTMXLayer() ,_verticesToDraw(0) ,_vertexZvalue(0) ,_useAutomaticVertexZ(false) -,_dirty(false) -{} +,_dirty(true) +{ +} FastTMXLayer::~FastTMXLayer() { @@ -145,10 +146,9 @@ void FastTMXLayer::draw(Renderer *renderer, const Mat4& transform, uint32_t flag rect = RectApplyTransform(rect, inv); _verticesToDraw = updateTiles(rect); - std::sort(_quads.begin(), _quads.end(), sortQuadCommand); + std::stable_sort(_quads.begin(), _quads.end(), sortQuadCommand); // don't draw more than 65535 vertices since we are using GL_UNSIGNED_SHORT for indices _verticesToDraw = std::min(_verticesToDraw, 65535); - _dirty = false; } From e19ce7054bc84846df681c95bd5d3e19bc429885 Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Thu, 26 Jun 2014 11:21:29 +0800 Subject: [PATCH 16/40] add comments --- cocos/renderer/CCQuadCommand.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cocos/renderer/CCQuadCommand.h b/cocos/renderer/CCQuadCommand.h index 7024a2050f..2f50fb2ee1 100644 --- a/cocos/renderer/CCQuadCommand.h +++ b/cocos/renderer/CCQuadCommand.h @@ -70,6 +70,8 @@ protected: bool _preMultiplyMV; }; +//This type of quad command will not multiply vertex by modelview matrix, so you need a shader together with modelview matrix +//OR the rendering result will be not correct class NopreMultiplyMVQuadCommand : public QuadCommand { public: From 87e5e1460ef04fb9fd4774405dc7b6855e954194 Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Thu, 26 Jun 2014 11:45:15 +0800 Subject: [PATCH 17/40] adjust some code to reduce indentions --- cocos/2d/CCFastTMXLayer.cpp | 112 ++++++++++++++++++------------------ 1 file changed, 55 insertions(+), 57 deletions(-) diff --git a/cocos/2d/CCFastTMXLayer.cpp b/cocos/2d/CCFastTMXLayer.cpp index a5b6638af8..5f78160bdc 100644 --- a/cocos/2d/CCFastTMXLayer.cpp +++ b/cocos/2d/CCFastTMXLayer.cpp @@ -608,30 +608,29 @@ Value FastTMXLayer::getProperty(const std::string& propertyName) const void FastTMXLayer::parseInternalProperties() { auto vertexz = getProperty("cc_vertexz"); - if (!vertexz.isNull()) + if (vertexz.isNull()) return; + + std::string vertexZStr = vertexz.asString(); + // If "automatic" is on, then parse the "cc_alpha_func" too + if (vertexZStr == "automatic") { - std::string vertexZStr = vertexz.asString(); - // If "automatic" is on, then parse the "cc_alpha_func" too - if (vertexZStr == "automatic") - { - _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(); - } + _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(); } } @@ -670,45 +669,44 @@ void FastTMXLayer::setTileGID(int gid, const Vec2& tileCoordinate, TMXTileFlags TMXTileFlags currentFlags; int currentGID = getTileGIDAt(tileCoordinate, ¤tFlags); - if (currentGID != gid || currentFlags != flags) + if (currentGID == gid && currentFlags == flags) return; + + int gidAndFlags = gid | flags; + + // setting gid=0 is equal to remove the tile + if (gid == 0) { - int gidAndFlags = gid | flags; - - // setting gid=0 is equal to remove the tile - if (gid == 0) + removeTileAt(tileCoordinate); + } + // empty tile. create a new one + else if (currentGID == 0) + { + int z = tileCoordinate.x + tileCoordinate.y * _layerSize.width; + setTileForGID(z, gidAndFlags); + } + // 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()) { - removeTileAt(tileCoordinate); + 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; } - // empty tile. create a new one - else if (currentGID == 0) - { - int z = tileCoordinate.x + tileCoordinate.y * _layerSize.width; - setTileForGID(z, gidAndFlags); - } - // 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()) - { - 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; - } - else - { - setTileForGID(z, gidAndFlags); - } + setTileForGID(z, gidAndFlags); } } } From 81aa9c2c7443f3d8f973986cc983093d875aa09f Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Thu, 26 Jun 2014 20:45:08 +0800 Subject: [PATCH 18/40] use custom command for rendering --- cocos/2d/CCFastTMXLayer.cpp | 304 +++++++++++++++++++++--------------- cocos/2d/CCFastTMXLayer.h | 25 ++- 2 files changed, 195 insertions(+), 134 deletions(-) diff --git a/cocos/2d/CCFastTMXLayer.cpp b/cocos/2d/CCFastTMXLayer.cpp index 5f78160bdc..26ec7ec085 100644 --- a/cocos/2d/CCFastTMXLayer.cpp +++ b/cocos/2d/CCFastTMXLayer.cpp @@ -79,6 +79,7 @@ bool FastTMXLayer::initWithTilesetInfo(TMXTilesetInfo *tilesetInfo, TMXLayerInfo _layerName = layerInfo->_name; _layerSize = layerInfo->_layerSize; _tiles = layerInfo->_tiles; + _quadsDirty = true; setOpacity( layerInfo->_opacity ); setProperties(layerInfo->getProperties()); @@ -119,6 +120,7 @@ FastTMXLayer::FastTMXLayer() ,_vertexZvalue(0) ,_useAutomaticVertexZ(false) ,_dirty(true) +, _quadsDirty(true) { } @@ -136,6 +138,8 @@ bool sortQuadCommand(const V3F_C4B_T2F_Quad& a, const V3F_C4B_T2F_Quad& b) void FastTMXLayer::draw(Renderer *renderer, const Mat4& transform, uint32_t flags) { + updateTotalQuads(); + if( flags != 0 || _dirty ) { Size s = Director::getInstance()->getWinSize(); @@ -146,28 +150,50 @@ void FastTMXLayer::draw(Renderer *renderer, const Mat4& transform, uint32_t flag rect = RectApplyTransform(rect, inv); _verticesToDraw = updateTiles(rect); - std::stable_sort(_quads.begin(), _quads.end(), sortQuadCommand); // don't draw more than 65535 vertices since we are using GL_UNSIGNED_SHORT for indices _verticesToDraw = std::min(_verticesToDraw, 65535); _dirty = false; - } - auto glprogramState = GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR); - int index = 0; - for(const auto& quadNumberIter : _quadsNumber) + if(_renderCommands.size() < _quadsIndices.size()) { - int z = quadNumberIter.first; - auto& quadCommand = _renderCommands2[quadNumberIter.first]; - quadCommand.init(z, _texture->getName(), glprogramState, BlendFunc::ALPHA_PREMULTIPLIED, &_quads[index], quadNumberIter.second, transform); - index += quadNumberIter.second; - renderer->addCommand(&quadCommand); + _renderCommands.resize(_quadsIndices.size()); } + + int index = 0; + for(const auto& iter : _quadsIndices) + { + auto& cmd = _renderCommands[index++]; + + cmd.init(iter.first); + printf("in draw function of FastTMXLayer %d\n", iter.second.size()); + cmd.func = CC_CALLBACK_0(FastTMXLayer::onDraw, this, &iter.second); + renderer->addCommand(&cmd); + } + +} + +void FastTMXLayer::onDraw(const std::vector *indices) +{ + GL::bindTexture2D(_texture->getName()); + getGLProgramState()->apply(_modelViewTransform); + + glBindVertexArray(0); + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + + glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION); + glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_COLOR); + glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD); + glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B_T2F), &_totalQuads[0]); + glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V3F_C4B_T2F), ((char*)&_totalQuads[0]) + offsetof(V3F_C4B_T2F, colors)); + glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B_T2F), ((char*)&_totalQuads[0]) + offsetof(V3F_C4B_T2F, texCoords)); + printf("in onDraw function of FastTMXLayer %d\n", indices->size()); + glDrawElements(GL_TRIANGLES, (GLsizei)indices->size(), GL_UNSIGNED_INT, &(*indices)[0]); } int FastTMXLayer::updateTiles(const Rect& culledRect) { - int tilesUsed = 0; Rect visibleTiles = culledRect; Size mapTileSize = CC_SIZE_PIXELS_TO_POINTS(_mapTileSize); @@ -209,17 +235,11 @@ int FastTMXLayer::updateTiles(const Rect& culledRect) tilesOverY = ceil(overTileRect.origin.y + overTileRect.size.height) - floor(overTileRect.origin.y); } - // doesn't support VBO - int quadsNeed = std::min(static_cast((visibleTiles.size.width + 2 *tilesOverX) * (visibleTiles.size.height + 2 * tilesOverY)), MAX_QUADS_COUNT); - if (_quads.size() < quadsNeed) - { - _quads.resize(quadsNeed); - } + _quadsIndices.clear(); //clear quadsNumber - _quadsNumber.clear(); - - Size texSize = _tileSet->_imageSize; + + int tilesUsed = 0; for (int y = visibleTiles.origin.y - tilesOverY; y < visibleTiles.origin.y + visibleTiles.size.height + tilesOverY; ++y) { if(y<0 || y >= _layerSize.height) @@ -228,110 +248,26 @@ int FastTMXLayer::updateTiles(const Rect& culledRect) { if(x<0 || x >= _layerSize.width) continue; - - int tileGID = _tiles[x + y * (int)_layerSize.width]; - - // GID==0 empty tile - if(tileGID!=0) + + int tileIndex = getTileIndexByPos(x, y); + if(_tiles[tileIndex] == 0) continue; + + int vertexZ = getVertexZForPos(Vec2(x,y)); + auto& indices = _quadsIndices[vertexZ]; + if(indices.capacity() == indices.size()) { - - V3F_C4B_T2F_Quad *quad = &_quads[tilesUsed]; - - Vec3 nodePos(static_cast(x), static_cast(y), 0); - _tileToNodeTransform.transformPoint(&nodePos); - - float left, right, top, bottom, z; - - z = getVertexZForPos(Vec2(x, y)); - - //add quadsNumber - auto quadsNumberIter = _quadsNumber.find(z); - if(quadsNumberIter == _quadsNumber.end()) - { - _quadsNumber.insert(std::make_pair(z, 1)); - } - else - { - quadsNumberIter->second += 1; - } - - // 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; - - tilesUsed++; + indices.reserve(indices.size() + 4096 * 6); } + CC_ASSERT(_tileToQuadIndex.find(tileIndex) != _tileToQuadIndex.end() && _tileToQuadIndex[tileIndex] <= _totalQuads.size()-1); + int quadIndex = (int)_tileToQuadIndex[tileIndex]; + indices.push_back(quadIndex * 4); + indices.push_back(quadIndex * 4 + 1); + indices.push_back(quadIndex * 4 + 2); + indices.push_back(quadIndex * 4 + 3); + indices.push_back(quadIndex * 4 + 2); + indices.push_back(quadIndex * 4 + 1); + if (tilesUsed >= MAX_QUADS_COUNT) { break; @@ -453,6 +389,120 @@ Mat4 FastTMXLayer::tileToNodeTransform() } +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)); + + 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)); + + // 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; + } + } + + //TODO: update VBOs + + _quadsDirty = false; + } +} + // removing / getting tiles Sprite* FastTMXLayer::getTileAt(const Vec2& tileCoordinate) { @@ -488,7 +538,7 @@ Sprite* FastTMXLayer::getTileAt(const Vec2& tileCoordinate) _spriteContainer.insert(std::pair >(index, std::pair(tile, gid))); // tile is converted to sprite. - setTileForGID(index, 0); + setFlaggedTileGIDByIndex(index, 0); } } return tile; @@ -568,7 +618,7 @@ void FastTMXLayer::removeTileAt(const Vec2& tileCoordinate) int z = tileCoordinate.x + tileCoordinate.y * _layerSize.width; // remove tile from GID map - setTileForGID(z, 0); + setFlaggedTileGIDByIndex(z, 0); // remove it from sprites auto it = _spriteContainer.find(z); @@ -579,9 +629,11 @@ void FastTMXLayer::removeTileAt(const Vec2& tileCoordinate) } } -void FastTMXLayer::setTileForGID(int index, int gid) +void FastTMXLayer::setFlaggedTileGIDByIndex(int index, int gid) { + if(gid == _tiles[index]) return; _tiles[index] = gid; + _quadsDirty = true; _dirty = true; } @@ -682,7 +734,7 @@ void FastTMXLayer::setTileGID(int gid, const Vec2& tileCoordinate, TMXTileFlags else if (currentGID == 0) { int z = tileCoordinate.x + tileCoordinate.y * _layerSize.width; - setTileForGID(z, gidAndFlags); + setFlaggedTileGIDByIndex(z, gidAndFlags); } // modifying an existing tile with a non-empty tile else @@ -706,7 +758,7 @@ void FastTMXLayer::setTileGID(int gid, const Vec2& tileCoordinate, TMXTileFlags } else { - setTileForGID(z, gidAndFlags); + setFlaggedTileGIDByIndex(z, gidAndFlags); } } } diff --git a/cocos/2d/CCFastTMXLayer.h b/cocos/2d/CCFastTMXLayer.h index 07571f2fdf..a47174de9a 100644 --- a/cocos/2d/CCFastTMXLayer.h +++ b/cocos/2d/CCFastTMXLayer.h @@ -145,8 +145,8 @@ public: * @js NA * @lua NA */ - uint32_t* getTiles() const { return _tiles; }; - void setTiles(uint32_t* tiles) { _tiles = tiles; }; + const uint32_t* getTiles() const { return _tiles; }; + void setTiles(uint32_t* tiles) { _tiles = tiles; _quadsDirty = true;}; /** Tileset information for the layer */ inline TMXTilesetInfo* getTileSet() const { return _tileSet; }; @@ -163,7 +163,8 @@ public: /** properties from the layer. They can be added using Tiled */ inline const ValueMap& getProperties() const { return _properties; }; inline ValueMap& getProperties() { return _properties; }; - inline void setProperties(const ValueMap& properties) { + inline void setProperties(const ValueMap& properties) + { _properties = properties; }; @@ -174,8 +175,6 @@ public: - layer->removeChild(sprite, cleanup); */ Sprite* getTileAt(const Vec2& tileCoordinate); - - Sprite* updateTileForGID(int gid, const Vec2& pos); void setupTileSprite(Sprite* sprite, Vec2 pos, int gid); @@ -201,9 +200,15 @@ protected: int getVertexZForPos(const Vec2& pos); - void insertTileForGID(int gid, const Vec2& pos); - void setTileForGID(int index, int gid); + //Flip flags is packed into gid + void setFlaggedTileGIDByIndex(int index, int gid); + // + void updateTotalQuads(); + + void onDraw(const std::vector* indices); + + inline int getTileIndexByPos(int x, int y) const { return x + y * (int) _layerSize.width; } protected: //! name of the layer @@ -240,9 +245,13 @@ protected: /** tile coordinate to node coordinate transform */ Mat4 _tileToNodeTransform; /** quads to be rendered */ + bool _quadsDirty; + std::map _tileToQuadIndex; + std::vector _totalQuads; + std::map> _quadsIndices; std::vector _quads; std::map _quadsNumber; - std::unordered_map _renderCommands2; + std::vector _renderCommands; bool _dirty; }; From a04b5fc5d5dd04f7c5542c70598b10720ff63e30 Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Fri, 27 Jun 2014 10:30:21 +0800 Subject: [PATCH 19/40] use custom command for fast_tmx rendering --- cocos/2d/CCFastTMXLayer.cpp | 84 ++++++++++++++++++++++++------------- cocos/2d/CCFastTMXLayer.h | 6 ++- 2 files changed, 58 insertions(+), 32 deletions(-) diff --git a/cocos/2d/CCFastTMXLayer.cpp b/cocos/2d/CCFastTMXLayer.cpp index 26ec7ec085..e96cc269b7 100644 --- a/cocos/2d/CCFastTMXLayer.cpp +++ b/cocos/2d/CCFastTMXLayer.cpp @@ -109,19 +109,20 @@ bool FastTMXLayer::initWithTilesetInfo(TMXTilesetInfo *tilesetInfo, TMXLayerInfo } FastTMXLayer::FastTMXLayer() -:_layerName("") -,_layerSize(Size::ZERO) -,_mapTileSize(Size::ZERO) -,_tiles(nullptr) -,_tileSet(nullptr) -,_layerOrientation(FastTMXOrientationOrtho) +: _layerName("") +, _layerSize(Size::ZERO) +, _mapTileSize(Size::ZERO) +, _tiles(nullptr) +, _tileSet(nullptr) +, _layerOrientation(FastTMXOrientationOrtho) ,_texture(nullptr) -,_verticesToDraw(0) -,_vertexZvalue(0) -,_useAutomaticVertexZ(false) -,_dirty(true) +, _verticesToDraw(0) +, _vertexZvalue(0) +, _useAutomaticVertexZ(false) +, _dirty(true) , _quadsDirty(true) { + _buffersVBO[0] = _buffersVBO[1] = 0; } FastTMXLayer::~FastTMXLayer() @@ -129,6 +130,15 @@ FastTMXLayer::~FastTMXLayer() 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]); + } } bool sortQuadCommand(const V3F_C4B_T2F_Quad& a, const V3F_C4B_T2F_Quad& b) @@ -166,7 +176,6 @@ void FastTMXLayer::draw(Renderer *renderer, const Mat4& transform, uint32_t flag auto& cmd = _renderCommands[index++]; cmd.init(iter.first); - printf("in draw function of FastTMXLayer %d\n", iter.second.size()); cmd.func = CC_CALLBACK_0(FastTMXLayer::onDraw, this, &iter.second); renderer->addCommand(&cmd); } @@ -179,7 +188,7 @@ void FastTMXLayer::onDraw(const std::vector *indices) getGLProgramState()->apply(_modelViewTransform); glBindVertexArray(0); - glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION); @@ -188,7 +197,6 @@ void FastTMXLayer::onDraw(const std::vector *indices) glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B_T2F), &_totalQuads[0]); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V3F_C4B_T2F), ((char*)&_totalQuads[0]) + offsetof(V3F_C4B_T2F, colors)); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B_T2F), ((char*)&_totalQuads[0]) + offsetof(V3F_C4B_T2F, texCoords)); - printf("in onDraw function of FastTMXLayer %d\n", indices->size()); glDrawElements(GL_TRIANGLES, (GLsizei)indices->size(), GL_UNSIGNED_INT, &(*indices)[0]); } @@ -283,26 +291,43 @@ int FastTMXLayer::updateTiles(const Rect& culledRect) return tilesUsed * 6; } -void FastTMXLayer::setupVBO() +void FastTMXLayer::updateVertexBuffer() { - glGenBuffers(2, &_buffersVBO[0]); - - // 10922 = 65536/6 - int total = std::min(static_cast(_layerSize.width * _layerSize.height), MAX_QUADS_COUNT); - - // Vertex + Tex Coords + glBindVertexArray(0); + if(!glIsBuffer(_buffersVBO[0])) + { + glGenBuffers(1, &_buffersVBO[0]); + } + glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]); - glBufferData(GL_ARRAY_BUFFER, total * sizeof(V3F_T2F_Quad), NULL, GL_DYNAMIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, sizeof(V3F_C4B_T2F_Quad) * _quads.size(), (GLvoid*)&_quads[0], GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); - - // Indices - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, total * 6 * sizeof(GLushort), NULL, GL_DYNAMIC_DRAW); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); - - CHECK_GL_ERROR_DEBUG(); } +//void FastTMXLayer::updateIndexBuffer() +//{ +//} + +//void FastTMXLayer::setupVBO() +//{ +// glGenBuffers(2, &_buffersVBO[0]); +// +// // 10922 = 65536/6 +// int total = std::min(static_cast(_layerSize.width * _layerSize.height), MAX_QUADS_COUNT); +// +// // Vertex + Tex Coords +// glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]); +// glBufferData(GL_ARRAY_BUFFER, total * sizeof(V3F_T2F_Quad), NULL, GL_DYNAMIC_DRAW); +// glBindBuffer(GL_ARRAY_BUFFER, 0); +// +// // Indices +// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]); +// glBufferData(GL_ELEMENT_ARRAY_BUFFER, total * 6 * sizeof(GLushort), NULL, GL_DYNAMIC_DRAW); +// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); +// +// CHECK_GL_ERROR_DEBUG(); +//} + // FastTMXLayer - setup Tiles void FastTMXLayer::setupTiles() { @@ -340,7 +365,6 @@ void FastTMXLayer::setupTiles() _screenTileCount = _screenGridSize.width * _screenGridSize.height; - setupVBO(); } Mat4 FastTMXLayer::tileToNodeTransform() @@ -497,7 +521,7 @@ void FastTMXLayer::updateTotalQuads() } } - //TODO: update VBOs + updateVertexBuffer(); _quadsDirty = false; } diff --git a/cocos/2d/CCFastTMXLayer.h b/cocos/2d/CCFastTMXLayer.h index a47174de9a..455c2c00bd 100644 --- a/cocos/2d/CCFastTMXLayer.h +++ b/cocos/2d/CCFastTMXLayer.h @@ -189,7 +189,6 @@ protected: bool initWithTilesetInfo(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo); int updateTiles(const Rect& culledRect); - void setupVBO(); Vec2 calculateLayerOffset(const Vec2& offset); /* The layer recognizes some special properties, like cc_vertez */ @@ -209,6 +208,9 @@ protected: void onDraw(const std::vector* indices); inline int getTileIndexByPos(int x, int y) const { return x + y * (int) _layerSize.width; } + + void updateVertexBuffer(); + //void updateIndexBuffer(); protected: //! name of the layer @@ -232,7 +234,7 @@ protected: /** container for sprite children. map > */ std::map > _spriteContainer; - GLuint _buffersVBO[3]; //0: vertex, 1: tex coords, 2: indices + GLuint _buffersVBO[2]; //0: vertex, 1: indices Size _screenGridSize; Rect _screenGridRect; From 6292ed05be3b9b0295d67cb7c63ddfb14be5834c Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Fri, 27 Jun 2014 11:50:44 +0800 Subject: [PATCH 20/40] improvement for fast tmx rendering --- cocos/2d/CCFastTMXLayer.cpp | 106 +++++++++++++++++++++++------------- cocos/2d/CCFastTMXLayer.h | 12 ++-- 2 files changed, 75 insertions(+), 43 deletions(-) diff --git a/cocos/2d/CCFastTMXLayer.cpp b/cocos/2d/CCFastTMXLayer.cpp index e96cc269b7..f44ed061dc 100644 --- a/cocos/2d/CCFastTMXLayer.cpp +++ b/cocos/2d/CCFastTMXLayer.cpp @@ -141,16 +141,11 @@ FastTMXLayer::~FastTMXLayer() } } -bool sortQuadCommand(const V3F_C4B_T2F_Quad& a, const V3F_C4B_T2F_Quad& b) -{ - return a.bl.vertices.z < b.bl.vertices.z; -} - void FastTMXLayer::draw(Renderer *renderer, const Mat4& transform, uint32_t flags) { updateTotalQuads(); - if( flags != 0 || _dirty ) + if( flags != 0 || _dirty || _quadsDirty ) { Size s = Director::getInstance()->getWinSize(); auto rect = Rect(0, 0, s.width, s.height); @@ -160,44 +155,47 @@ void FastTMXLayer::draw(Renderer *renderer, const Mat4& transform, uint32_t flag rect = RectApplyTransform(rect, inv); _verticesToDraw = updateTiles(rect); + updateIndexBuffer(); // don't draw more than 65535 vertices since we are using GL_UNSIGNED_SHORT for indices _verticesToDraw = std::min(_verticesToDraw, 65535); _dirty = false; } - if(_renderCommands.size() < _quadsIndices.size()) + if(_renderCommands.size() < _indicesVertexZNumber.size()) { - _renderCommands.resize(_quadsIndices.size()); + _renderCommands.resize(_indicesVertexZNumber.size()); } int index = 0; - for(const auto& iter : _quadsIndices) + for(const auto& iter : _indicesVertexZNumber) { auto& cmd = _renderCommands[index++]; cmd.init(iter.first); - cmd.func = CC_CALLBACK_0(FastTMXLayer::onDraw, this, &iter.second); + cmd.func = CC_CALLBACK_0(FastTMXLayer::onDraw, this, _indicesVertexZOffsets[iter.first], iter.second); renderer->addCommand(&cmd); } } -void FastTMXLayer::onDraw(const std::vector *indices) +void FastTMXLayer::onDraw(int offset, int count) { GL::bindTexture2D(_texture->getName()); getGLProgramState()->apply(_modelViewTransform); glBindVertexArray(0); glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]); glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION); glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_COLOR); glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD); - glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B_T2F), &_totalQuads[0]); - glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V3F_C4B_T2F), ((char*)&_totalQuads[0]) + offsetof(V3F_C4B_T2F, colors)); - glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B_T2F), ((char*)&_totalQuads[0]) + offsetof(V3F_C4B_T2F, texCoords)); - glDrawElements(GL_TRIANGLES, (GLsizei)indices->size(), GL_UNSIGNED_INT, &(*indices)[0]); + 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)); + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } int FastTMXLayer::updateTiles(const Rect& culledRect) @@ -243,9 +241,12 @@ int FastTMXLayer::updateTiles(const Rect& culledRect) tilesOverY = ceil(overTileRect.origin.y + overTileRect.size.height) - floor(overTileRect.origin.y); } - _quadsIndices.clear(); - - //clear quadsNumber + _indicesVertexZNumber.clear(); + + for(const auto& iter : _indicesVertexZOffsets) + { + _indicesVertexZNumber[iter.first] = iter.second; + } int tilesUsed = 0; for (int y = visibleTiles.origin.y - tilesOverY; y < visibleTiles.origin.y + visibleTiles.size.height + tilesOverY; ++y) @@ -261,20 +262,19 @@ int FastTMXLayer::updateTiles(const Rect& culledRect) if(_tiles[tileIndex] == 0) continue; int vertexZ = getVertexZForPos(Vec2(x,y)); - auto& indices = _quadsIndices[vertexZ]; - if(indices.capacity() == indices.size()) - { - indices.reserve(indices.size() + 4096 * 6); - } + auto iter = _indicesVertexZNumber.find(vertexZ); + int offset = iter->second; + iter->second++; - CC_ASSERT(_tileToQuadIndex.find(tileIndex) != _tileToQuadIndex.end() && _tileToQuadIndex[tileIndex] <= _totalQuads.size()-1); + //CC_ASSERT(_tileToQuadIndex.find(tileIndex) != _tileToQuadIndex.end() && _tileToQuadIndex[tileIndex] <= _totalQuads.size()-1); int quadIndex = (int)_tileToQuadIndex[tileIndex]; - indices.push_back(quadIndex * 4); - indices.push_back(quadIndex * 4 + 1); - indices.push_back(quadIndex * 4 + 2); - indices.push_back(quadIndex * 4 + 3); - indices.push_back(quadIndex * 4 + 2); - indices.push_back(quadIndex * 4 + 1); + + _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; if (tilesUsed >= MAX_QUADS_COUNT) { @@ -287,7 +287,16 @@ int FastTMXLayer::updateTiles(const Rect& culledRect) break; } } // for y - + + for(const auto& iter : _indicesVertexZOffsets) + { + _indicesVertexZNumber[iter.first] -= iter.second; + if(_indicesVertexZNumber[iter.first] == 0) + { + _indicesVertexZNumber.erase(iter.first); + } + } + return tilesUsed * 6; } @@ -300,13 +309,20 @@ void FastTMXLayer::updateVertexBuffer() } glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]); - glBufferData(GL_ARRAY_BUFFER, sizeof(V3F_C4B_T2F_Quad) * _quads.size(), (GLvoid*)&_quads[0], GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, sizeof(V3F_C4B_T2F_Quad) * _totalQuads.size(), (GLvoid*)&_totalQuads[0], GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); } -//void FastTMXLayer::updateIndexBuffer() -//{ -//} +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); +} //void FastTMXLayer::setupVBO() //{ @@ -421,6 +437,8 @@ void FastTMXLayer::updateTotalQuads() Size texSize = _tileSet->_imageSize; _tileToQuadIndex.clear(); _totalQuads.resize(int(_layerSize.width * _layerSize.height)); + _indices.resize(6 * int(_layerSize.width * _layerSize.height)); + _indicesVertexZOffsets.clear(); int quadIndex = 0; for(int y = 0; y < _layerSize.height; ++y) @@ -442,7 +460,15 @@ void FastTMXLayer::updateTotalQuads() float left, right, top, bottom, z; z = getVertexZForPos(Vec2(x, y)); - + auto iter = _indicesVertexZOffsets.find(z); + if(iter == _indicesVertexZOffsets.end()) + { + _indicesVertexZOffsets[z] = 1; + } + else + { + iter->second++; + } // vertices if (tileGID & kTMXTileDiagonalFlag) { @@ -521,6 +547,12 @@ void FastTMXLayer::updateTotalQuads() } } + int offset = 0; + for(auto iter = _indicesVertexZOffsets.begin(); iter != _indicesVertexZOffsets.end(); ++iter) + { + std::swap(offset, iter->second); + offset += iter->second; + } updateVertexBuffer(); _quadsDirty = false; diff --git a/cocos/2d/CCFastTMXLayer.h b/cocos/2d/CCFastTMXLayer.h index 455c2c00bd..02766653fc 100644 --- a/cocos/2d/CCFastTMXLayer.h +++ b/cocos/2d/CCFastTMXLayer.h @@ -205,12 +205,12 @@ protected: // void updateTotalQuads(); - void onDraw(const std::vector* indices); + void onDraw(int offset, int count); inline int getTileIndexByPos(int x, int y) const { return x + y * (int) _layerSize.width; } void updateVertexBuffer(); - //void updateIndexBuffer(); + void updateIndexBuffer(); protected: //! name of the layer @@ -248,11 +248,11 @@ protected: Mat4 _tileToNodeTransform; /** quads to be rendered */ bool _quadsDirty; - std::map _tileToQuadIndex; + std::unordered_map _tileToQuadIndex; std::vector _totalQuads; - std::map> _quadsIndices; - std::vector _quads; - std::map _quadsNumber; + std::vector _indices; + std::map _indicesVertexZOffsets; + std::unordered_map _indicesVertexZNumber; std::vector _renderCommands; bool _dirty; }; From 7254977358b453caeca80dd32d256afe027327ea Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Fri, 27 Jun 2014 12:44:19 +0800 Subject: [PATCH 21/40] roll back quadcommand --- cocos/renderer/CCQuadCommand.cpp | 1 - cocos/renderer/CCQuadCommand.h | 29 ++--------------------------- cocos/renderer/CCRenderer.cpp | 8 +------- 3 files changed, 3 insertions(+), 35 deletions(-) diff --git a/cocos/renderer/CCQuadCommand.cpp b/cocos/renderer/CCQuadCommand.cpp index c2f5d2a933..d5cff627f6 100644 --- a/cocos/renderer/CCQuadCommand.cpp +++ b/cocos/renderer/CCQuadCommand.cpp @@ -40,7 +40,6 @@ QuadCommand::QuadCommand() ,_blendType(BlendFunc::DISABLE) ,_quads(nullptr) ,_quadsCount(0) -,_preMultiplyMV(true) { _type = RenderCommand::Type::QUAD_COMMAND; } diff --git a/cocos/renderer/CCQuadCommand.h b/cocos/renderer/CCQuadCommand.h index 2f50fb2ee1..094291d9a6 100644 --- a/cocos/renderer/CCQuadCommand.h +++ b/cocos/renderer/CCQuadCommand.h @@ -38,7 +38,7 @@ public: static const int MATERIAL_ID_DO_NOT_BATCH = 0; QuadCommand(); - virtual ~QuadCommand(); + ~QuadCommand(); /** Initializes the command with a globalZOrder, a texture ID, a `GLProgram`, a blending function, a pointer to quads, * quantity of quads, and the Model View transform to be used for the quads */ @@ -47,7 +47,6 @@ public: void useMaterial() const; - inline uint32_t getMaterialID() const { return _materialID; } inline GLuint getTextureID() const { return _textureID; } inline V3F_C4B_T2F_Quad* getQuads() const { return _quads; } @@ -55,10 +54,9 @@ public: inline GLProgramState* getGLProgramState() const { return _glProgramState; } inline BlendFunc getBlendType() const { return _blendType; } inline const Mat4& getModelView() const { return _mv; } - inline bool getPremultiplyMV() const { return _preMultiplyMV; } protected: - virtual void generateMaterialID(); + void generateMaterialID(); uint32_t _materialID; GLuint _textureID; @@ -67,29 +65,6 @@ protected: V3F_C4B_T2F_Quad* _quads; ssize_t _quadsCount; Mat4 _mv; - bool _preMultiplyMV; -}; - -//This type of quad command will not multiply vertex by modelview matrix, so you need a shader together with modelview matrix -//OR the rendering result will be not correct -class NopreMultiplyMVQuadCommand : public QuadCommand -{ -public: - NopreMultiplyMVQuadCommand() - :QuadCommand() - { - _preMultiplyMV = false; - } - - virtual ~NopreMultiplyMVQuadCommand() - { - } - -protected: - virtual void generateMaterialID() - { - _materialID = QuadCommand::MATERIAL_ID_DO_NOT_BATCH; - } }; NS_CC_END diff --git a/cocos/renderer/CCRenderer.cpp b/cocos/renderer/CCRenderer.cpp index 5a5964a28d..222118da6f 100644 --- a/cocos/renderer/CCRenderer.cpp +++ b/cocos/renderer/CCRenderer.cpp @@ -296,13 +296,7 @@ void Renderer::visitRenderQueue(const RenderQueue& queue) _batchedQuadCommands.push_back(cmd); memcpy(_quads + _numQuads, cmd->getQuads(), sizeof(V3F_C4B_T2F_Quad) * cmd->getQuadCount()); - - //only multiply mv when getPremultiplyMV is true - if(cmd->getPremultiplyMV()) - { - convertToWorldCoordinates(_quads + _numQuads, cmd->getQuadCount(), cmd->getModelView()); - } - + convertToWorldCoordinates(_quads + _numQuads, cmd->getQuadCount(), cmd->getModelView()); _numQuads += cmd->getQuadCount(); From b232896f85ddfe18f32c95c8985ad14d7860a7ae Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Fri, 27 Jun 2014 13:41:06 +0800 Subject: [PATCH 22/40] roll back fps compute logic fix crash bug --- cocos/2d/CCFastTMXLayer.cpp | 8 +++----- cocos/base/CCDirector.cpp | 2 +- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/cocos/2d/CCFastTMXLayer.cpp b/cocos/2d/CCFastTMXLayer.cpp index f44ed061dc..cd15df6a8b 100644 --- a/cocos/2d/CCFastTMXLayer.cpp +++ b/cocos/2d/CCFastTMXLayer.cpp @@ -187,13 +187,11 @@ void FastTMXLayer::onDraw(int offset, int count) glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]); - glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION); - glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_COLOR); - glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD); + GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX); 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)); + glDrawElements(GL_TRIANGLES, (GLsizei)count * 6, GL_UNSIGNED_INT, (GLvoid*)(offset * 6 * sizeof(int))); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } @@ -302,7 +300,7 @@ int FastTMXLayer::updateTiles(const Rect& culledRect) void FastTMXLayer::updateVertexBuffer() { - glBindVertexArray(0); + GL::bindVAO(0); if(!glIsBuffer(_buffersVBO[0])) { glGenBuffers(1, &_buffersVBO[0]); diff --git a/cocos/base/CCDirector.cpp b/cocos/base/CCDirector.cpp index 4754a790e2..ac7519a062 100644 --- a/cocos/base/CCDirector.cpp +++ b/cocos/base/CCDirector.cpp @@ -1078,7 +1078,7 @@ void Director::showStats() { char buffer[30]; - if (_accumDt > CC_DIRECTOR_STATS_INTERVAL * 20) + if (_accumDt > CC_DIRECTOR_STATS_INTERVAL) { _frameRate = _frames / _accumDt; _frames = 0; From 1152f77ed7afd72885ace462c823f43797561dbb Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Fri, 27 Jun 2014 14:42:53 +0800 Subject: [PATCH 23/40] clean up fast tmx code --- cocos/2d/CCFastTMXLayer.cpp | 43 +++++-------------------------------- cocos/2d/CCFastTMXLayer.h | 5 ++--- 2 files changed, 7 insertions(+), 41 deletions(-) diff --git a/cocos/2d/CCFastTMXLayer.cpp b/cocos/2d/CCFastTMXLayer.cpp index cd15df6a8b..d1429e5689 100644 --- a/cocos/2d/CCFastTMXLayer.cpp +++ b/cocos/2d/CCFastTMXLayer.cpp @@ -116,7 +116,6 @@ FastTMXLayer::FastTMXLayer() , _tileSet(nullptr) , _layerOrientation(FastTMXOrientationOrtho) ,_texture(nullptr) -, _verticesToDraw(0) , _vertexZvalue(0) , _useAutomaticVertexZ(false) , _dirty(true) @@ -154,10 +153,8 @@ void FastTMXLayer::draw(Renderer *renderer, const Mat4& transform, uint32_t flag inv.inverse(); rect = RectApplyTransform(rect, inv); - _verticesToDraw = updateTiles(rect); + updateTiles(rect); updateIndexBuffer(); - // don't draw more than 65535 vertices since we are using GL_UNSIGNED_SHORT for indices - _verticesToDraw = std::min(_verticesToDraw, 65535); _dirty = false; } @@ -194,11 +191,11 @@ void FastTMXLayer::onDraw(int offset, int count) glDrawElements(GL_TRIANGLES, (GLsizei)count * 6, GL_UNSIGNED_INT, (GLvoid*)(offset * 6 * sizeof(int))); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, count * 4); } -int FastTMXLayer::updateTiles(const Rect& culledRect) +void FastTMXLayer::updateTiles(const Rect& culledRect) { - Rect visibleTiles = culledRect; Size mapTileSize = CC_SIZE_PIXELS_TO_POINTS(_mapTileSize); Size tileSize = CC_SIZE_PIXELS_TO_POINTS(_tileSet->_tileSize); @@ -214,7 +211,6 @@ int FastTMXLayer::updateTiles(const Rect& culledRect) visibleTiles.origin.x = floor(visibleTiles.origin.x); visibleTiles.origin.y = floor(visibleTiles.origin.y); - // for the bigger tiles. int tilesOverX = 0; int tilesOverY = 0; @@ -246,7 +242,6 @@ int FastTMXLayer::updateTiles(const Rect& culledRect) _indicesVertexZNumber[iter.first] = iter.second; } - int tilesUsed = 0; for (int y = visibleTiles.origin.y - tilesOverY; y < visibleTiles.origin.y + visibleTiles.size.height + tilesOverY; ++y) { if(y<0 || y >= _layerSize.height) @@ -274,16 +269,7 @@ int FastTMXLayer::updateTiles(const Rect& culledRect) _indices[6 * offset + 4] = quadIndex * 4 + 2; _indices[6 * offset + 5] = quadIndex * 4 + 1; - if (tilesUsed >= MAX_QUADS_COUNT) - { - break; - } } // for x - - if (tilesUsed >= MAX_QUADS_COUNT) - { - break; - } } // for y for(const auto& iter : _indicesVertexZOffsets) @@ -295,7 +281,6 @@ int FastTMXLayer::updateTiles(const Rect& culledRect) } } - return tilesUsed * 6; } void FastTMXLayer::updateVertexBuffer() @@ -322,26 +307,6 @@ void FastTMXLayer::updateIndexBuffer() glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } -//void FastTMXLayer::setupVBO() -//{ -// glGenBuffers(2, &_buffersVBO[0]); -// -// // 10922 = 65536/6 -// int total = std::min(static_cast(_layerSize.width * _layerSize.height), MAX_QUADS_COUNT); -// -// // Vertex + Tex Coords -// glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]); -// glBufferData(GL_ARRAY_BUFFER, total * sizeof(V3F_T2F_Quad), NULL, GL_DYNAMIC_DRAW); -// glBindBuffer(GL_ARRAY_BUFFER, 0); -// -// // Indices -// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]); -// glBufferData(GL_ELEMENT_ARRAY_BUFFER, total * 6 * sizeof(GLushort), NULL, GL_DYNAMIC_DRAW); -// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); -// -// CHECK_GL_ERROR_DEBUG(); -//} - // FastTMXLayer - setup Tiles void FastTMXLayer::setupTiles() { @@ -374,6 +339,8 @@ void FastTMXLayer::setupTiles() _screenGridSize.height = ceil(screenSize.height / (_mapTileSize.height/2)) + 4; break; case FastTMXOrientationHex: + default: + CCLOGERROR("FastTMX does not support type %d", _layerOrientation); break; } diff --git a/cocos/2d/CCFastTMXLayer.h b/cocos/2d/CCFastTMXLayer.h index 02766653fc..5b54eb018b 100644 --- a/cocos/2d/CCFastTMXLayer.h +++ b/cocos/2d/CCFastTMXLayer.h @@ -188,7 +188,7 @@ public: protected: bool initWithTilesetInfo(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo); - int updateTiles(const Rect& culledRect); + void updateTiles(const Rect& culledRect); Vec2 calculateLayerOffset(const Vec2& offset); /* The layer recognizes some special properties, like cc_vertez */ @@ -239,14 +239,13 @@ protected: Size _screenGridSize; Rect _screenGridRect; int _screenTileCount; - int _verticesToDraw; int _vertexZvalue; bool _useAutomaticVertexZ; /** tile coordinate to node coordinate transform */ Mat4 _tileToNodeTransform; - /** quads to be rendered */ + /** data for rendering */ bool _quadsDirty; std::unordered_map _tileToQuadIndex; std::vector _totalQuads; From eba84b5dea4f0073495a2dbaf84deb62a4d05529 Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Fri, 27 Jun 2014 14:45:30 +0800 Subject: [PATCH 24/40] fix wrong indentions --- cocos/2d/CCFastTMXLayer.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cocos/2d/CCFastTMXLayer.cpp b/cocos/2d/CCFastTMXLayer.cpp index d1429e5689..bfa615f775 100644 --- a/cocos/2d/CCFastTMXLayer.cpp +++ b/cocos/2d/CCFastTMXLayer.cpp @@ -356,7 +356,7 @@ Mat4 FastTMXLayer::tileToNodeTransform() switch(_layerOrientation) { - case FastTMXOrientationOrtho: + case FastTMXOrientationOrtho: { _tileToNodeTransform = Mat4 ( @@ -368,7 +368,7 @@ Mat4 FastTMXLayer::tileToNodeTransform() return _tileToNodeTransform; } - case FastTMXOrientationIso: + case FastTMXOrientationIso: { float offX = (_layerSize.width - 1) * w / 2; _tileToNodeTransform = Mat4 @@ -378,8 +378,8 @@ Mat4 FastTMXLayer::tileToNodeTransform() 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f ); - return _tileToNodeTransform; - } + return _tileToNodeTransform; + } case FastTMXOrientationHex: { _tileToNodeTransform = Mat4::IDENTITY; From 33a8b6563f032a9b8521559514eb2b773b34fdf8 Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Fri, 27 Jun 2014 16:56:26 +0800 Subject: [PATCH 25/40] windows works for fast tmx --- cocos/2d/cocos2d.vcxproj | 4 ++++ cocos/2d/cocos2d.vcxproj.filters | 12 ++++++++++++ tests/cpp-tests/proj.win32/cpp-tests.vcxproj | 2 ++ tests/cpp-tests/proj.win32/cpp-tests.vcxproj.filters | 6 ++++++ 4 files changed, 24 insertions(+) diff --git a/cocos/2d/cocos2d.vcxproj b/cocos/2d/cocos2d.vcxproj index 96e90dd061..848640c11f 100644 --- a/cocos/2d/cocos2d.vcxproj +++ b/cocos/2d/cocos2d.vcxproj @@ -305,6 +305,8 @@ xcopy /Y /Q "$(ProjectDir)..\..\external\win32-specific\gles\prebuilt\*.*" "$(Ou + + @@ -507,6 +509,8 @@ xcopy /Y /Q "$(ProjectDir)..\..\external\win32-specific\gles\prebuilt\*.*" "$(Ou + + diff --git a/cocos/2d/cocos2d.vcxproj.filters b/cocos/2d/cocos2d.vcxproj.filters index 9007db529c..78e53c85c8 100644 --- a/cocos/2d/cocos2d.vcxproj.filters +++ b/cocos/2d/cocos2d.vcxproj.filters @@ -580,6 +580,12 @@ 3d + + 2d + + + 2d + @@ -1180,6 +1186,12 @@ 3d + + 2d + + + 2d + diff --git a/tests/cpp-tests/proj.win32/cpp-tests.vcxproj b/tests/cpp-tests/proj.win32/cpp-tests.vcxproj index 892c9c7e74..6e59447926 100644 --- a/tests/cpp-tests/proj.win32/cpp-tests.vcxproj +++ b/tests/cpp-tests/proj.win32/cpp-tests.vcxproj @@ -184,6 +184,7 @@ + @@ -366,6 +367,7 @@ + diff --git a/tests/cpp-tests/proj.win32/cpp-tests.vcxproj.filters b/tests/cpp-tests/proj.win32/cpp-tests.vcxproj.filters index 4f77ffe13f..399fe8f162 100644 --- a/tests/cpp-tests/proj.win32/cpp-tests.vcxproj.filters +++ b/tests/cpp-tests/proj.win32/cpp-tests.vcxproj.filters @@ -846,6 +846,9 @@ Classes\ExtensionsTest\CocoStudioActionTimelineTest + + Classes\TileMapTest + @@ -1562,5 +1565,8 @@ Classes\ExtensionsTest\CocoStudioActionTimelineTest + + Classes\TileMapTest + \ No newline at end of file From 3771654e660c018ad8d33303934325a1e5545bb0 Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Fri, 27 Jun 2014 18:00:06 +0800 Subject: [PATCH 26/40] remove deprecated function rename enum to static const member of class remove hex tile map(we do not support it yet) --- cocos/2d/CCFastTMXLayer.cpp | 45 ++++++++++--------- cocos/2d/CCFastTMXLayer.h | 20 ++++----- cocos/2d/CCFastTMXTiledMap.h | 42 ++--------------- .../Classes/TileMapTest/TileMapTest2.cpp | 6 +-- 4 files changed, 41 insertions(+), 72 deletions(-) diff --git a/cocos/2d/CCFastTMXLayer.cpp b/cocos/2d/CCFastTMXLayer.cpp index bfa615f775..7bec79a9b6 100644 --- a/cocos/2d/CCFastTMXLayer.cpp +++ b/cocos/2d/CCFastTMXLayer.cpp @@ -49,10 +49,9 @@ THE SOFTWARE. NS_CC_BEGIN -namespace -{ - static const int MAX_QUADS_COUNT = 65536 / 6; -} +const int FastTMXLayer::FAST_TMX_ORIENTATION_ORTHO = 0; +const int FastTMXLayer::FAST_TMX_ORIENTATION_HEX = 1; +const int FastTMXLayer::FAST_TMX_ORIENTATION_ISO = 2; // FastTMXLayer - init & alloc & dealloc FastTMXLayer * FastTMXLayer::create(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo) @@ -114,7 +113,7 @@ FastTMXLayer::FastTMXLayer() , _mapTileSize(Size::ZERO) , _tiles(nullptr) , _tileSet(nullptr) -, _layerOrientation(FastTMXOrientationOrtho) +, _layerOrientation(FAST_TMX_ORIENTATION_ORTHO) ,_texture(nullptr) , _vertexZvalue(0) , _useAutomaticVertexZ(false) @@ -216,7 +215,7 @@ void FastTMXLayer::updateTiles(const Rect& culledRect) int tilesOverY = 0; // for diagonal oriention tiles float tileSizeMax = std::max(tileSize.width, tileSize.height); - if (_layerOrientation == FastTMXOrientationOrtho) + if (_layerOrientation == FAST_TMX_ORIENTATION_ORTHO) { tilesOverX = ceil(tileSizeMax / mapTileSize.width) - 1; tilesOverY = ceil(tileSizeMax / mapTileSize.height) - 1; @@ -224,7 +223,7 @@ void FastTMXLayer::updateTiles(const Rect& culledRect) if (tilesOverX < 0) tilesOverX = 0; if (tilesOverY < 0) tilesOverY = 0; } - else if(_layerOrientation == FastTMXOrientationIso) + else if(_layerOrientation == FAST_TMX_ORIENTATION_ISO) { Rect overTileRect(0, 0, tileSizeMax - mapTileSize.width, tileSizeMax - mapTileSize.height); if (overTileRect.size.width < 0) overTileRect.size.width = 0; @@ -234,6 +233,11 @@ void FastTMXLayer::updateTiles(const Rect& culledRect) tilesOverX = ceil(overTileRect.origin.x + overTileRect.size.width) - floor(overTileRect.origin.x); tilesOverY = ceil(overTileRect.origin.y + overTileRect.size.height) - floor(overTileRect.origin.y); } + else + { + //do nothing, do not support + CCASSERT(0, "TMX invalid value"); + } _indicesVertexZNumber.clear(); @@ -327,18 +331,18 @@ void FastTMXLayer::setupTiles() switch (_layerOrientation) { - case FastTMXOrientationOrtho: + case FAST_TMX_ORIENTATION_ORTHO: _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: + case FAST_TMX_ORIENTATION_ISO: _screenGridSize.width = ceil(screenSize.width / _mapTileSize.width) + 2; _screenGridSize.height = ceil(screenSize.height / (_mapTileSize.height/2)) + 4; break; - case FastTMXOrientationHex: + case FAST_TMX_ORIENTATION_HEX: default: CCLOGERROR("FastTMX does not support type %d", _layerOrientation); break; @@ -356,7 +360,7 @@ Mat4 FastTMXLayer::tileToNodeTransform() switch(_layerOrientation) { - case FastTMXOrientationOrtho: + case FAST_TMX_ORIENTATION_ORTHO: { _tileToNodeTransform = Mat4 ( @@ -368,7 +372,7 @@ Mat4 FastTMXLayer::tileToNodeTransform() return _tileToNodeTransform; } - case FastTMXOrientationIso: + case FAST_TMX_ORIENTATION_ISO: { float offX = (_layerSize.width - 1) * w / 2; _tileToNodeTransform = Mat4 @@ -380,7 +384,7 @@ Mat4 FastTMXLayer::tileToNodeTransform() ); return _tileToNodeTransform; } - case FastTMXOrientationHex: + case FAST_TMX_ORIENTATION_HEX: { _tileToNodeTransform = Mat4::IDENTITY; return _tileToNodeTransform; @@ -604,14 +608,14 @@ int FastTMXLayer::getVertexZForPos(const Vec2& pos) { switch (_layerOrientation) { - case FastTMXOrientationIso: + case FAST_TMX_ORIENTATION_ISO: maxVal = static_cast(_layerSize.width + _layerSize.height); ret = static_cast(-(maxVal - (pos.x + pos.y))); break; - case FastTMXOrientationOrtho: + case FAST_TMX_ORIENTATION_ORTHO: ret = static_cast(-(_layerSize.height-pos.y)); break; - case FastTMXOrientationHex: + case FAST_TMX_ORIENTATION_HEX: CCASSERT(0, "TMX Hexa zOrder not supported"); break; default: @@ -713,15 +717,16 @@ Vec2 FastTMXLayer::calculateLayerOffset(const Vec2& pos) Vec2 ret = Vec2::ZERO; switch (_layerOrientation) { - case FastTMXOrientationOrtho: + case FAST_TMX_ORIENTATION_ORTHO: ret = Vec2( pos.x * _mapTileSize.width, -pos.y *_mapTileSize.height); break; - case FastTMXOrientationIso: + case FAST_TMX_ORIENTATION_ISO: ret = Vec2((_mapTileSize.width /2) * (pos.x - pos.y), (_mapTileSize.height /2 ) * (-pos.x - pos.y)); break; - case FastTMXOrientationHex: - CCASSERT(pos.equals(Vec2::ZERO), "offset for hexagonal map not implemented yet"); + case FAST_TMX_ORIENTATION_HEX: + default: + CCASSERT(pos.equals(Vec2::ZERO), "offset for this map not implemented yet"); break; } return ret; diff --git a/cocos/2d/CCFastTMXLayer.h b/cocos/2d/CCFastTMXLayer.h index 5b54eb018b..f007c698a5 100644 --- a/cocos/2d/CCFastTMXLayer.h +++ b/cocos/2d/CCFastTMXLayer.h @@ -24,8 +24,8 @@ 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. ****************************************************************************/ -#ifndef __CCTMX_LAYER2_H__ -#define __CCTMX_LAYER2_H__ +#ifndef __CC_FAST_TMX_LAYER_H__ +#define __CC_FAST_TMX_LAYER_H__ #include "CCTMXObjectGroup.h" #include "CCTMXXMLParser.h" @@ -73,10 +73,7 @@ For further information, please see the programming guide: http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:tiled_maps -@since v0.8.1 -Tiles can have tile flags for additional properties. At the moment only flip horizontal and flip vertical are used. These bit flags are defined in TMXXMLParser.h. - -@since 1.1 +@since v3.2 */ class CC_DLL FastTMXLayer : public Node @@ -97,9 +94,6 @@ public: /** returns the tile gid at a given tile coordinate. It also returns the tile flags. */ int getTileGIDAt(const Vec2& tileCoordinate, TMXTileFlags* flags = nullptr); - CC_DEPRECATED_ATTRIBUTE int tileGIDAt(const Vec2& tileCoordinate, TMXTileFlags* flags = nullptr){ - return getTileGIDAt(tileCoordinate, flags); - }; /** sets the tile gid (gid = tile global id) at a given tile coordinate. The Tile GID can be obtained by using the method "tileGIDAt" or by using the TMX editor -> Tileset Mgr +1. @@ -121,11 +115,9 @@ public: /** returns the position in points of a given tile coordinate */ Vec2 getPositionAt(const Vec2& tileCoordinate); - CC_DEPRECATED_ATTRIBUTE Vec2 positionAt(const Vec2& tileCoordinate) { return getPositionAt(tileCoordinate); }; /** return the value for the specific property name */ Value getProperty(const std::string& propertyName) const; - CC_DEPRECATED_ATTRIBUTE Value propertyNamed(const std::string& propertyName) const { return getProperty(propertyName); }; /** Creates the tiles */ void setupTiles(); @@ -254,6 +246,12 @@ protected: std::unordered_map _indicesVertexZNumber; std::vector _renderCommands; bool _dirty; + +public: + /** Possible orientations of the TMX map */ + static const int FAST_TMX_ORIENTATION_ORTHO; + static const int FAST_TMX_ORIENTATION_HEX; + static const int FAST_TMX_ORIENTATION_ISO; }; // end of tilemap_parallax_nodes group diff --git a/cocos/2d/CCFastTMXTiledMap.h b/cocos/2d/CCFastTMXTiledMap.h index 7a2ccdfa42..9dfc098552 100644 --- a/cocos/2d/CCFastTMXTiledMap.h +++ b/cocos/2d/CCFastTMXTiledMap.h @@ -24,8 +24,8 @@ 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. ****************************************************************************/ -#ifndef __CCTMX_TILE_MAP2_H__ -#define __CCTMX_TILE_MAP2_H__ +#ifndef __CC_FAST_TMX_TILEMAP_H__ +#define __CC_FAST_TMX_TILEMAP_H__ #include "CCNode.h" #include "CCTMXObjectGroup.h" @@ -38,24 +38,6 @@ class TMXLayerInfo; class TMXTilesetInfo; class TMXMapInfo; -/** - * @addtogroup tilemap_parallax_nodes - * @{ - */ - -/** Possible orientations of the TMX map */ -enum -{ - /** Orthogonal orientation */ - FastTMXOrientationOrtho, - - /** Hexagonal orientation */ - FastTMXOrientationHex, - - /** Isometric orientation */ - FastTMXOrientationIso, -}; - /** @brief FastTMXTiledMap knows how to parse and render a TMX map. It adds support for the TMX tiled map format used by http://www.mapeditor.org @@ -105,7 +87,7 @@ layer->getProperty(name_of_the_property); objectGroup->getProperty(name_of_the_property); object->getProperty(name_of_the_property); -@since v0.8.1 +@since v3.2 */ class CC_DLL FastTMXTiledMap : public Node { @@ -118,31 +100,15 @@ public: /** return the FastTMXLayer for the specific layer */ FastTMXLayer* getLayer(const std::string& layerName) const; - /** - * @js NA - * @lua NA - */ - CC_DEPRECATED_ATTRIBUTE FastTMXLayer* layerNamed(const std::string& layerName) const { return getLayer(layerName); }; /** return the TMXObjectGroup for the specific group */ TMXObjectGroup* getObjectGroup(const std::string& groupName) const; - /** - * @js NA - * @lua NA - */ - CC_DEPRECATED_ATTRIBUTE TMXObjectGroup* objectGroupNamed(const std::string& groupName) const { return getObjectGroup(groupName); }; /** return the value for the specific property name */ Value getProperty(const std::string& propertyName) const; - /** - * @js NA - * @lua NA - */ - CC_DEPRECATED_ATTRIBUTE Value propertyNamed(const char *propertyName) const { return getProperty(propertyName); }; /** return properties dictionary for tile GID */ Value getPropertiesForGID(int GID) const; - CC_DEPRECATED_ATTRIBUTE Value propertiesForGID(int GID) const { return getPropertiesForGID(GID); }; /** the map's size property measured in tiles */ inline const Size& getMapSize() const { return _mapSize; }; @@ -164,7 +130,7 @@ public: }; /** properties */ - inline ValueMap& getProperties() { return _properties; }; + inline const ValueMap& getProperties() const { return _properties; }; inline void setProperties(const ValueMap& properties) { _properties = properties; }; diff --git a/tests/cpp-tests/Classes/TileMapTest/TileMapTest2.cpp b/tests/cpp-tests/Classes/TileMapTest/TileMapTest2.cpp index 0e8435e133..9df7f7cad5 100644 --- a/tests/cpp-tests/Classes/TileMapTest/TileMapTest2.cpp +++ b/tests/cpp-tests/Classes/TileMapTest/TileMapTest2.cpp @@ -30,8 +30,6 @@ namespace static int sceneIdx = -1; -#define MAX_LAYER 27 - static std::function createFunctions[] = { CLN(TMXIsoZorderNew), CLN(TMXOrthoZorderNew), @@ -45,7 +43,7 @@ namespace CLN(TMXIsoTest1New), CLN(TMXIsoTest2New), CLN(TMXUncompressedTestNew), - CLN(TMXHexTestNew), + //CLN(TMXHexTestNew), CLN(TMXReadWriteTestNew), CLN(TMXTilesetTestNew), CLN(TMXOrthoObjectsTestNew), @@ -64,6 +62,8 @@ namespace CLN(TMXGIDObjectsTestNew), }; + +#define MAX_LAYER (sizeof(createFunctions) / sizeof(createFunctions[0])) Layer* createTileMalayer(int nIndex) { From 46e5b8e5bd6f7701a17746a50484ede88b43c8d6 Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Fri, 27 Jun 2014 18:28:59 +0800 Subject: [PATCH 27/40] refactor FastTMXTiledMap::tilesetForLayer to a more elegant implementation --- cocos/2d/CCFastTMXTiledMap.cpp | 46 ++++++++++++++++------------------ 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/cocos/2d/CCFastTMXTiledMap.cpp b/cocos/2d/CCFastTMXTiledMap.cpp index 116c372131..fb61989b5f 100644 --- a/cocos/2d/CCFastTMXTiledMap.cpp +++ b/cocos/2d/CCFastTMXTiledMap.cpp @@ -118,37 +118,33 @@ TMXTilesetInfo * FastTMXTiledMap::tilesetForLayer(TMXLayerInfo *layerInfo, TMXMa { Size size = layerInfo->_layerSize; auto& tilesets = mapInfo->getTilesets(); - if (tilesets.size()>0) + + for (const auto& iter : tilesets) { - TMXTilesetInfo* tileset = nullptr; - for (auto iter = tilesets.crbegin(); iter != tilesets.crend(); ++iter) + if (iter) { - tileset = *iter; - if (tileset) + for( int y=0; y < size.height; y++ ) { - for( int y=0; y < size.height; y++ ) + for( int x=0; x < size.width; x++ ) { - for( int x=0; x < size.width; x++ ) + int pos = static_cast(x + size.width * y); + int gid = layerInfo->_tiles[ pos ]; + + // gid are stored in little endian. + // if host is big endian, then swap + //if( o == CFByteOrderBigEndian ) + // gid = CFSwapInt32( gid ); + /* We support little endian.*/ + + // XXX: gid == 0 --> empty tile + if( gid != 0 ) { - int pos = static_cast(x + size.width * y); - int gid = layerInfo->_tiles[ pos ]; - - // gid are stored in little endian. - // if host is big endian, then swap - //if( o == CFByteOrderBigEndian ) - // gid = CFSwapInt32( gid ); - /* We support little endian.*/ - - // XXX: gid == 0 --> empty tile - if( gid != 0 ) - { - // Optimization: quick return - // if the layer is invalid (more than 1 tileset per layer) an CCAssert will be thrown later - if( (gid & kTMXFlippedMask) >= tileset->_firstGid ) - return tileset; - } + // Optimization: quick return + // if the layer is invalid (more than 1 tileset per layer) an CCAssert will be thrown later + if( (gid & kTMXFlippedMask) >= iter->_firstGid ) + return iter; } - } + } } } } From 9227c4f30aa619a038f4e6daa60cc2a9ff9203b6 Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Fri, 27 Jun 2014 19:41:34 +0800 Subject: [PATCH 28/40] refactor updateTiles --- cocos/2d/CCFastTMXLayer.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/cocos/2d/CCFastTMXLayer.cpp b/cocos/2d/CCFastTMXLayer.cpp index 7bec79a9b6..0d613a6a5a 100644 --- a/cocos/2d/CCFastTMXLayer.cpp +++ b/cocos/2d/CCFastTMXLayer.cpp @@ -46,6 +46,7 @@ THE SOFTWARE. #include "renderer/CCRenderer.h" #include "deprecated/CCString.h" #include "renderer/CCGLProgramStateCache.h" +#include NS_CC_BEGIN @@ -246,15 +247,15 @@ void FastTMXLayer::updateTiles(const Rect& culledRect) _indicesVertexZNumber[iter.first] = iter.second; } - for (int y = visibleTiles.origin.y - tilesOverY; y < visibleTiles.origin.y + visibleTiles.size.height + tilesOverY; ++y) + int yBegin = std::max(0.f,visibleTiles.origin.y - tilesOverY); + int yEnd = std::min(_layerSize.height,visibleTiles.origin.y + visibleTiles.size.height + tilesOverY); + int xBegin = std::max(0.f,visibleTiles.origin.x - tilesOverX); + int xEnd = std::min(_layerSize.width,visibleTiles.origin.x + visibleTiles.size.width + tilesOverX); + + for (int y = yBegin; y < yEnd; ++y) { - if(y<0 || y >= _layerSize.height) - continue; - for (int x = visibleTiles.origin.x - tilesOverX; x < visibleTiles.origin.x + visibleTiles.size.width + tilesOverX; ++x) + for (int x = xBegin; x < xEnd; ++x) { - if(x<0 || x >= _layerSize.width) - continue; - int tileIndex = getTileIndexByPos(x, y); if(_tiles[tileIndex] == 0) continue; From bb9df773a9ad518ad6e0cc37bb3b6ff773678533 Mon Sep 17 00:00:00 2001 From: CocosRobot Date: Sun, 29 Jun 2014 09:28:33 +0000 Subject: [PATCH 29/40] [AUTO]: updating luabinding automatically --- .../lua-bindings/auto/api/PhysicsShapeBox.lua | 26 +- .../auto/api/PhysicsShapeEdgeBox.lua | 7 +- .../api/lua_cocos2dx_physics_auto_api.lua | 16 +- .../auto/lua_cocos2dx_physics_auto.cpp | 478 ++++++------------ .../auto/lua_cocos2dx_physics_auto.hpp | 4 - 5 files changed, 162 insertions(+), 369 deletions(-) diff --git a/cocos/scripting/lua-bindings/auto/api/PhysicsShapeBox.lua b/cocos/scripting/lua-bindings/auto/api/PhysicsShapeBox.lua index 6914ae2440..338ac5add2 100644 --- a/cocos/scripting/lua-bindings/auto/api/PhysicsShapeBox.lua +++ b/cocos/scripting/lua-bindings/auto/api/PhysicsShapeBox.lua @@ -1,14 +1,9 @@ -------------------------------- -- @module PhysicsShapeBox --- @extend PhysicsShape +-- @extend PhysicsShapePolygon -- @parent_module cc --------------------------------- --- @function [parent=#PhysicsShapeBox] getPointsCount --- @param self --- @return int#int ret (return value: int) - -------------------------------- -- @function [parent=#PhysicsShapeBox] getSize -- @param self @@ -22,28 +17,9 @@ -- @param #vec2_table vec2 -- @return PhysicsShapeBox#PhysicsShapeBox ret (return value: cc.PhysicsShapeBox) --------------------------------- --- @function [parent=#PhysicsShapeBox] calculateArea --- @param self --- @param #size_table size --- @return float#float ret (return value: float) - --------------------------------- --- @function [parent=#PhysicsShapeBox] calculateMoment --- @param self --- @param #float float --- @param #size_table size --- @param #vec2_table vec2 --- @return float#float ret (return value: float) - -------------------------------- -- @function [parent=#PhysicsShapeBox] getOffset -- @param self -- @return vec2_table#vec2_table ret (return value: vec2_table) --------------------------------- --- @function [parent=#PhysicsShapeBox] calculateDefaultMoment --- @param self --- @return float#float ret (return value: float) - return nil diff --git a/cocos/scripting/lua-bindings/auto/api/PhysicsShapeEdgeBox.lua b/cocos/scripting/lua-bindings/auto/api/PhysicsShapeEdgeBox.lua index 3a0c275544..6e879ad541 100644 --- a/cocos/scripting/lua-bindings/auto/api/PhysicsShapeEdgeBox.lua +++ b/cocos/scripting/lua-bindings/auto/api/PhysicsShapeEdgeBox.lua @@ -1,14 +1,9 @@ -------------------------------- -- @module PhysicsShapeEdgeBox --- @extend PhysicsShape +-- @extend PhysicsShapeEdgePolygon -- @parent_module cc --------------------------------- --- @function [parent=#PhysicsShapeEdgeBox] getPointsCount --- @param self --- @return int#int ret (return value: int) - -------------------------------- -- @function [parent=#PhysicsShapeEdgeBox] create -- @param self diff --git a/cocos/scripting/lua-bindings/auto/api/lua_cocos2dx_physics_auto_api.lua b/cocos/scripting/lua-bindings/auto/api/lua_cocos2dx_physics_auto_api.lua index 4b7ba7721b..2d62955ee1 100644 --- a/cocos/scripting/lua-bindings/auto/api/lua_cocos2dx_physics_auto_api.lua +++ b/cocos/scripting/lua-bindings/auto/api/lua_cocos2dx_physics_auto_api.lua @@ -22,13 +22,13 @@ -------------------------------------------------------- --- the cc PhysicsShapeBox --- @field [parent=#cc] PhysicsShapeBox#PhysicsShapeBox PhysicsShapeBox preloaded module +-- the cc PhysicsShapePolygon +-- @field [parent=#cc] PhysicsShapePolygon#PhysicsShapePolygon PhysicsShapePolygon preloaded module -------------------------------------------------------- --- the cc PhysicsShapePolygon --- @field [parent=#cc] PhysicsShapePolygon#PhysicsShapePolygon PhysicsShapePolygon preloaded module +-- the cc PhysicsShapeBox +-- @field [parent=#cc] PhysicsShapeBox#PhysicsShapeBox PhysicsShapeBox preloaded module -------------------------------------------------------- @@ -37,13 +37,13 @@ -------------------------------------------------------- --- the cc PhysicsShapeEdgeBox --- @field [parent=#cc] PhysicsShapeEdgeBox#PhysicsShapeEdgeBox PhysicsShapeEdgeBox preloaded module +-- the cc PhysicsShapeEdgePolygon +-- @field [parent=#cc] PhysicsShapeEdgePolygon#PhysicsShapeEdgePolygon PhysicsShapeEdgePolygon preloaded module -------------------------------------------------------- --- the cc PhysicsShapeEdgePolygon --- @field [parent=#cc] PhysicsShapeEdgePolygon#PhysicsShapeEdgePolygon PhysicsShapeEdgePolygon preloaded module +-- the cc PhysicsShapeEdgeBox +-- @field [parent=#cc] PhysicsShapeEdgeBox#PhysicsShapeEdgeBox PhysicsShapeEdgeBox preloaded module -------------------------------------------------------- diff --git a/cocos/scripting/lua-bindings/auto/lua_cocos2dx_physics_auto.cpp b/cocos/scripting/lua-bindings/auto/lua_cocos2dx_physics_auto.cpp index cf3d79938c..799661839b 100644 --- a/cocos/scripting/lua-bindings/auto/lua_cocos2dx_physics_auto.cpp +++ b/cocos/scripting/lua-bindings/auto/lua_cocos2dx_physics_auto.cpp @@ -2416,259 +2416,6 @@ int lua_register_cocos2dx_physics_PhysicsShapeCircle(lua_State* tolua_S) return 1; } -int lua_cocos2dx_physics_PhysicsShapeBox_getPointsCount(lua_State* tolua_S) -{ - int argc = 0; - cocos2d::PhysicsShapeBox* cobj = nullptr; - bool ok = true; - -#if COCOS2D_DEBUG >= 1 - tolua_Error tolua_err; -#endif - - -#if COCOS2D_DEBUG >= 1 - if (!tolua_isusertype(tolua_S,1,"cc.PhysicsShapeBox",0,&tolua_err)) goto tolua_lerror; -#endif - - cobj = (cocos2d::PhysicsShapeBox*)tolua_tousertype(tolua_S,1,0); - -#if COCOS2D_DEBUG >= 1 - if (!cobj) - { - tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_physics_PhysicsShapeBox_getPointsCount'", nullptr); - return 0; - } -#endif - - argc = lua_gettop(tolua_S)-1; - if (argc == 0) - { - if(!ok) - return 0; - int ret = cobj->getPointsCount(); - tolua_pushnumber(tolua_S,(lua_Number)ret); - return 1; - } - CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "getPointsCount",argc, 0); - return 0; - -#if COCOS2D_DEBUG >= 1 - tolua_lerror: - tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_physics_PhysicsShapeBox_getPointsCount'.",&tolua_err); -#endif - - return 0; -} -int lua_cocos2dx_physics_PhysicsShapeBox_getSize(lua_State* tolua_S) -{ - int argc = 0; - cocos2d::PhysicsShapeBox* cobj = nullptr; - bool ok = true; - -#if COCOS2D_DEBUG >= 1 - tolua_Error tolua_err; -#endif - - -#if COCOS2D_DEBUG >= 1 - if (!tolua_isusertype(tolua_S,1,"cc.PhysicsShapeBox",0,&tolua_err)) goto tolua_lerror; -#endif - - cobj = (cocos2d::PhysicsShapeBox*)tolua_tousertype(tolua_S,1,0); - -#if COCOS2D_DEBUG >= 1 - if (!cobj) - { - tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_physics_PhysicsShapeBox_getSize'", nullptr); - return 0; - } -#endif - - argc = lua_gettop(tolua_S)-1; - if (argc == 0) - { - if(!ok) - return 0; - cocos2d::Size ret = cobj->getSize(); - size_to_luaval(tolua_S, ret); - return 1; - } - CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "getSize",argc, 0); - return 0; - -#if COCOS2D_DEBUG >= 1 - tolua_lerror: - tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_physics_PhysicsShapeBox_getSize'.",&tolua_err); -#endif - - return 0; -} -int lua_cocos2dx_physics_PhysicsShapeBox_create(lua_State* tolua_S) -{ - int argc = 0; - bool ok = true; - -#if COCOS2D_DEBUG >= 1 - tolua_Error tolua_err; -#endif - -#if COCOS2D_DEBUG >= 1 - if (!tolua_isusertable(tolua_S,1,"cc.PhysicsShapeBox",0,&tolua_err)) goto tolua_lerror; -#endif - - argc = lua_gettop(tolua_S) - 1; - - if (argc == 1) - { - cocos2d::Size arg0; - ok &= luaval_to_size(tolua_S, 2, &arg0); - if(!ok) - return 0; - cocos2d::PhysicsShapeBox* ret = cocos2d::PhysicsShapeBox::create(arg0); - object_to_luaval(tolua_S, "cc.PhysicsShapeBox",(cocos2d::PhysicsShapeBox*)ret); - return 1; - } - if (argc == 2) - { - cocos2d::Size arg0; - cocos2d::PhysicsMaterial arg1; - ok &= luaval_to_size(tolua_S, 2, &arg0); - ok &= luaval_to_physics_material(tolua_S, 3, &arg1); - if(!ok) - return 0; - cocos2d::PhysicsShapeBox* ret = cocos2d::PhysicsShapeBox::create(arg0, arg1); - object_to_luaval(tolua_S, "cc.PhysicsShapeBox",(cocos2d::PhysicsShapeBox*)ret); - return 1; - } - if (argc == 3) - { - cocos2d::Size arg0; - cocos2d::PhysicsMaterial arg1; - cocos2d::Vec2 arg2; - ok &= luaval_to_size(tolua_S, 2, &arg0); - ok &= luaval_to_physics_material(tolua_S, 3, &arg1); - ok &= luaval_to_vec2(tolua_S, 4, &arg2); - if(!ok) - return 0; - cocos2d::PhysicsShapeBox* ret = cocos2d::PhysicsShapeBox::create(arg0, arg1, arg2); - object_to_luaval(tolua_S, "cc.PhysicsShapeBox",(cocos2d::PhysicsShapeBox*)ret); - return 1; - } - CCLOG("%s has wrong number of arguments: %d, was expecting %d\n ", "create",argc, 1); - return 0; -#if COCOS2D_DEBUG >= 1 - tolua_lerror: - tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_physics_PhysicsShapeBox_create'.",&tolua_err); -#endif - return 0; -} -int lua_cocos2dx_physics_PhysicsShapeBox_calculateArea(lua_State* tolua_S) -{ - int argc = 0; - bool ok = true; - -#if COCOS2D_DEBUG >= 1 - tolua_Error tolua_err; -#endif - -#if COCOS2D_DEBUG >= 1 - if (!tolua_isusertable(tolua_S,1,"cc.PhysicsShapeBox",0,&tolua_err)) goto tolua_lerror; -#endif - - argc = lua_gettop(tolua_S) - 1; - - if (argc == 1) - { - cocos2d::Size arg0; - ok &= luaval_to_size(tolua_S, 2, &arg0); - if(!ok) - return 0; - double ret = cocos2d::PhysicsShapeBox::calculateArea(arg0); - tolua_pushnumber(tolua_S,(lua_Number)ret); - return 1; - } - CCLOG("%s has wrong number of arguments: %d, was expecting %d\n ", "calculateArea",argc, 1); - return 0; -#if COCOS2D_DEBUG >= 1 - tolua_lerror: - tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_physics_PhysicsShapeBox_calculateArea'.",&tolua_err); -#endif - return 0; -} -int lua_cocos2dx_physics_PhysicsShapeBox_calculateMoment(lua_State* tolua_S) -{ - int argc = 0; - bool ok = true; - -#if COCOS2D_DEBUG >= 1 - tolua_Error tolua_err; -#endif - -#if COCOS2D_DEBUG >= 1 - if (!tolua_isusertable(tolua_S,1,"cc.PhysicsShapeBox",0,&tolua_err)) goto tolua_lerror; -#endif - - argc = lua_gettop(tolua_S) - 1; - - if (argc == 2) - { - double arg0; - cocos2d::Size arg1; - ok &= luaval_to_number(tolua_S, 2,&arg0); - ok &= luaval_to_size(tolua_S, 3, &arg1); - if(!ok) - return 0; - double ret = cocos2d::PhysicsShapeBox::calculateMoment(arg0, arg1); - tolua_pushnumber(tolua_S,(lua_Number)ret); - return 1; - } - if (argc == 3) - { - double arg0; - cocos2d::Size arg1; - cocos2d::Vec2 arg2; - ok &= luaval_to_number(tolua_S, 2,&arg0); - ok &= luaval_to_size(tolua_S, 3, &arg1); - ok &= luaval_to_vec2(tolua_S, 4, &arg2); - if(!ok) - return 0; - double ret = cocos2d::PhysicsShapeBox::calculateMoment(arg0, arg1, arg2); - tolua_pushnumber(tolua_S,(lua_Number)ret); - return 1; - } - CCLOG("%s has wrong number of arguments: %d, was expecting %d\n ", "calculateMoment",argc, 2); - return 0; -#if COCOS2D_DEBUG >= 1 - tolua_lerror: - tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_physics_PhysicsShapeBox_calculateMoment'.",&tolua_err); -#endif - return 0; -} -static int lua_cocos2dx_physics_PhysicsShapeBox_finalize(lua_State* tolua_S) -{ - printf("luabindings: finalizing LUA object (PhysicsShapeBox)"); - return 0; -} - -int lua_register_cocos2dx_physics_PhysicsShapeBox(lua_State* tolua_S) -{ - tolua_usertype(tolua_S,"cc.PhysicsShapeBox"); - tolua_cclass(tolua_S,"PhysicsShapeBox","cc.PhysicsShapeBox","cc.PhysicsShape",nullptr); - - tolua_beginmodule(tolua_S,"PhysicsShapeBox"); - tolua_function(tolua_S,"getPointsCount",lua_cocos2dx_physics_PhysicsShapeBox_getPointsCount); - tolua_function(tolua_S,"getSize",lua_cocos2dx_physics_PhysicsShapeBox_getSize); - tolua_function(tolua_S,"create", lua_cocos2dx_physics_PhysicsShapeBox_create); - tolua_function(tolua_S,"calculateArea", lua_cocos2dx_physics_PhysicsShapeBox_calculateArea); - tolua_function(tolua_S,"calculateMoment", lua_cocos2dx_physics_PhysicsShapeBox_calculateMoment); - tolua_endmodule(tolua_S); - std::string typeName = typeid(cocos2d::PhysicsShapeBox).name(); - g_luaType[typeName] = "cc.PhysicsShapeBox"; - g_typeCast["PhysicsShapeBox"] = "cc.PhysicsShapeBox"; - return 1; -} - int lua_cocos2dx_physics_PhysicsShapePolygon_getPointsCount(lua_State* tolua_S) { int argc = 0; @@ -2781,6 +2528,130 @@ int lua_register_cocos2dx_physics_PhysicsShapePolygon(lua_State* tolua_S) return 1; } +int lua_cocos2dx_physics_PhysicsShapeBox_getSize(lua_State* tolua_S) +{ + int argc = 0; + cocos2d::PhysicsShapeBox* cobj = nullptr; + bool ok = true; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertype(tolua_S,1,"cc.PhysicsShapeBox",0,&tolua_err)) goto tolua_lerror; +#endif + + cobj = (cocos2d::PhysicsShapeBox*)tolua_tousertype(tolua_S,1,0); + +#if COCOS2D_DEBUG >= 1 + if (!cobj) + { + tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_physics_PhysicsShapeBox_getSize'", nullptr); + return 0; + } +#endif + + argc = lua_gettop(tolua_S)-1; + if (argc == 0) + { + if(!ok) + return 0; + cocos2d::Size ret = cobj->getSize(); + size_to_luaval(tolua_S, ret); + return 1; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "getSize",argc, 0); + return 0; + +#if COCOS2D_DEBUG >= 1 + tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_physics_PhysicsShapeBox_getSize'.",&tolua_err); +#endif + + return 0; +} +int lua_cocos2dx_physics_PhysicsShapeBox_create(lua_State* tolua_S) +{ + int argc = 0; + bool ok = true; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertable(tolua_S,1,"cc.PhysicsShapeBox",0,&tolua_err)) goto tolua_lerror; +#endif + + argc = lua_gettop(tolua_S) - 1; + + if (argc == 1) + { + cocos2d::Size arg0; + ok &= luaval_to_size(tolua_S, 2, &arg0); + if(!ok) + return 0; + cocos2d::PhysicsShapeBox* ret = cocos2d::PhysicsShapeBox::create(arg0); + object_to_luaval(tolua_S, "cc.PhysicsShapeBox",(cocos2d::PhysicsShapeBox*)ret); + return 1; + } + if (argc == 2) + { + cocos2d::Size arg0; + cocos2d::PhysicsMaterial arg1; + ok &= luaval_to_size(tolua_S, 2, &arg0); + ok &= luaval_to_physics_material(tolua_S, 3, &arg1); + if(!ok) + return 0; + cocos2d::PhysicsShapeBox* ret = cocos2d::PhysicsShapeBox::create(arg0, arg1); + object_to_luaval(tolua_S, "cc.PhysicsShapeBox",(cocos2d::PhysicsShapeBox*)ret); + return 1; + } + if (argc == 3) + { + cocos2d::Size arg0; + cocos2d::PhysicsMaterial arg1; + cocos2d::Vec2 arg2; + ok &= luaval_to_size(tolua_S, 2, &arg0); + ok &= luaval_to_physics_material(tolua_S, 3, &arg1); + ok &= luaval_to_vec2(tolua_S, 4, &arg2); + if(!ok) + return 0; + cocos2d::PhysicsShapeBox* ret = cocos2d::PhysicsShapeBox::create(arg0, arg1, arg2); + object_to_luaval(tolua_S, "cc.PhysicsShapeBox",(cocos2d::PhysicsShapeBox*)ret); + return 1; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d\n ", "create",argc, 1); + return 0; +#if COCOS2D_DEBUG >= 1 + tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_physics_PhysicsShapeBox_create'.",&tolua_err); +#endif + return 0; +} +static int lua_cocos2dx_physics_PhysicsShapeBox_finalize(lua_State* tolua_S) +{ + printf("luabindings: finalizing LUA object (PhysicsShapeBox)"); + return 0; +} + +int lua_register_cocos2dx_physics_PhysicsShapeBox(lua_State* tolua_S) +{ + tolua_usertype(tolua_S,"cc.PhysicsShapeBox"); + tolua_cclass(tolua_S,"PhysicsShapeBox","cc.PhysicsShapeBox","cc.PhysicsShapePolygon",nullptr); + + tolua_beginmodule(tolua_S,"PhysicsShapeBox"); + tolua_function(tolua_S,"getSize",lua_cocos2dx_physics_PhysicsShapeBox_getSize); + tolua_function(tolua_S,"create", lua_cocos2dx_physics_PhysicsShapeBox_create); + tolua_endmodule(tolua_S); + std::string typeName = typeid(cocos2d::PhysicsShapeBox).name(); + g_luaType[typeName] = "cc.PhysicsShapeBox"; + g_typeCast["PhysicsShapeBox"] = "cc.PhysicsShapeBox"; + return 1; +} + int lua_cocos2dx_physics_PhysicsShapeEdgeSegment_getPointB(lua_State* tolua_S) { int argc = 0; @@ -2956,10 +2827,10 @@ int lua_register_cocos2dx_physics_PhysicsShapeEdgeSegment(lua_State* tolua_S) return 1; } -int lua_cocos2dx_physics_PhysicsShapeEdgeBox_getPointsCount(lua_State* tolua_S) +int lua_cocos2dx_physics_PhysicsShapeEdgePolygon_getPointsCount(lua_State* tolua_S) { int argc = 0; - cocos2d::PhysicsShapeEdgeBox* cobj = nullptr; + cocos2d::PhysicsShapeEdgePolygon* cobj = nullptr; bool ok = true; #if COCOS2D_DEBUG >= 1 @@ -2968,15 +2839,15 @@ int lua_cocos2dx_physics_PhysicsShapeEdgeBox_getPointsCount(lua_State* tolua_S) #if COCOS2D_DEBUG >= 1 - if (!tolua_isusertype(tolua_S,1,"cc.PhysicsShapeEdgeBox",0,&tolua_err)) goto tolua_lerror; + if (!tolua_isusertype(tolua_S,1,"cc.PhysicsShapeEdgePolygon",0,&tolua_err)) goto tolua_lerror; #endif - cobj = (cocos2d::PhysicsShapeEdgeBox*)tolua_tousertype(tolua_S,1,0); + cobj = (cocos2d::PhysicsShapeEdgePolygon*)tolua_tousertype(tolua_S,1,0); #if COCOS2D_DEBUG >= 1 if (!cobj) { - tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_physics_PhysicsShapeEdgeBox_getPointsCount'", nullptr); + tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_physics_PhysicsShapeEdgePolygon_getPointsCount'", nullptr); return 0; } #endif @@ -2995,11 +2866,31 @@ int lua_cocos2dx_physics_PhysicsShapeEdgeBox_getPointsCount(lua_State* tolua_S) #if COCOS2D_DEBUG >= 1 tolua_lerror: - tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_physics_PhysicsShapeEdgeBox_getPointsCount'.",&tolua_err); + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_physics_PhysicsShapeEdgePolygon_getPointsCount'.",&tolua_err); #endif return 0; } +static int lua_cocos2dx_physics_PhysicsShapeEdgePolygon_finalize(lua_State* tolua_S) +{ + printf("luabindings: finalizing LUA object (PhysicsShapeEdgePolygon)"); + return 0; +} + +int lua_register_cocos2dx_physics_PhysicsShapeEdgePolygon(lua_State* tolua_S) +{ + tolua_usertype(tolua_S,"cc.PhysicsShapeEdgePolygon"); + tolua_cclass(tolua_S,"PhysicsShapeEdgePolygon","cc.PhysicsShapeEdgePolygon","cc.PhysicsShape",nullptr); + + tolua_beginmodule(tolua_S,"PhysicsShapeEdgePolygon"); + tolua_function(tolua_S,"getPointsCount",lua_cocos2dx_physics_PhysicsShapeEdgePolygon_getPointsCount); + tolua_endmodule(tolua_S); + std::string typeName = typeid(cocos2d::PhysicsShapeEdgePolygon).name(); + g_luaType[typeName] = "cc.PhysicsShapeEdgePolygon"; + g_typeCast["PhysicsShapeEdgePolygon"] = "cc.PhysicsShapeEdgePolygon"; + return 1; +} + int lua_cocos2dx_physics_PhysicsShapeEdgeBox_create(lua_State* tolua_S) { int argc = 0; @@ -3084,10 +2975,9 @@ static int lua_cocos2dx_physics_PhysicsShapeEdgeBox_finalize(lua_State* tolua_S) int lua_register_cocos2dx_physics_PhysicsShapeEdgeBox(lua_State* tolua_S) { tolua_usertype(tolua_S,"cc.PhysicsShapeEdgeBox"); - tolua_cclass(tolua_S,"PhysicsShapeEdgeBox","cc.PhysicsShapeEdgeBox","cc.PhysicsShape",nullptr); + tolua_cclass(tolua_S,"PhysicsShapeEdgeBox","cc.PhysicsShapeEdgeBox","cc.PhysicsShapeEdgePolygon",nullptr); tolua_beginmodule(tolua_S,"PhysicsShapeEdgeBox"); - tolua_function(tolua_S,"getPointsCount",lua_cocos2dx_physics_PhysicsShapeEdgeBox_getPointsCount); tolua_function(tolua_S,"create", lua_cocos2dx_physics_PhysicsShapeEdgeBox_create); tolua_endmodule(tolua_S); std::string typeName = typeid(cocos2d::PhysicsShapeEdgeBox).name(); @@ -3096,70 +2986,6 @@ int lua_register_cocos2dx_physics_PhysicsShapeEdgeBox(lua_State* tolua_S) return 1; } -int lua_cocos2dx_physics_PhysicsShapeEdgePolygon_getPointsCount(lua_State* tolua_S) -{ - int argc = 0; - cocos2d::PhysicsShapeEdgePolygon* cobj = nullptr; - bool ok = true; - -#if COCOS2D_DEBUG >= 1 - tolua_Error tolua_err; -#endif - - -#if COCOS2D_DEBUG >= 1 - if (!tolua_isusertype(tolua_S,1,"cc.PhysicsShapeEdgePolygon",0,&tolua_err)) goto tolua_lerror; -#endif - - cobj = (cocos2d::PhysicsShapeEdgePolygon*)tolua_tousertype(tolua_S,1,0); - -#if COCOS2D_DEBUG >= 1 - if (!cobj) - { - tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_physics_PhysicsShapeEdgePolygon_getPointsCount'", nullptr); - return 0; - } -#endif - - argc = lua_gettop(tolua_S)-1; - if (argc == 0) - { - if(!ok) - return 0; - int ret = cobj->getPointsCount(); - tolua_pushnumber(tolua_S,(lua_Number)ret); - return 1; - } - CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "getPointsCount",argc, 0); - return 0; - -#if COCOS2D_DEBUG >= 1 - tolua_lerror: - tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_physics_PhysicsShapeEdgePolygon_getPointsCount'.",&tolua_err); -#endif - - return 0; -} -static int lua_cocos2dx_physics_PhysicsShapeEdgePolygon_finalize(lua_State* tolua_S) -{ - printf("luabindings: finalizing LUA object (PhysicsShapeEdgePolygon)"); - return 0; -} - -int lua_register_cocos2dx_physics_PhysicsShapeEdgePolygon(lua_State* tolua_S) -{ - tolua_usertype(tolua_S,"cc.PhysicsShapeEdgePolygon"); - tolua_cclass(tolua_S,"PhysicsShapeEdgePolygon","cc.PhysicsShapeEdgePolygon","cc.PhysicsShape",nullptr); - - tolua_beginmodule(tolua_S,"PhysicsShapeEdgePolygon"); - tolua_function(tolua_S,"getPointsCount",lua_cocos2dx_physics_PhysicsShapeEdgePolygon_getPointsCount); - tolua_endmodule(tolua_S); - std::string typeName = typeid(cocos2d::PhysicsShapeEdgePolygon).name(); - g_luaType[typeName] = "cc.PhysicsShapeEdgePolygon"; - g_typeCast["PhysicsShapeEdgePolygon"] = "cc.PhysicsShapeEdgePolygon"; - return 1; -} - int lua_cocos2dx_physics_PhysicsShapeEdgeChain_getPointsCount(lua_State* tolua_S) { int argc = 0; @@ -11090,6 +10916,7 @@ TOLUA_API int register_all_cocos2dx_physics(lua_State* tolua_S) lua_register_cocos2dx_physics_EventListenerPhysicsContact(tolua_S); lua_register_cocos2dx_physics_EventListenerPhysicsContactWithGroup(tolua_S); lua_register_cocos2dx_physics_PhysicsShape(tolua_S); + lua_register_cocos2dx_physics_PhysicsShapePolygon(tolua_S); lua_register_cocos2dx_physics_PhysicsShapeBox(tolua_S); lua_register_cocos2dx_physics_PhysicsJointMotor(tolua_S); lua_register_cocos2dx_physics_PhysicsJointRatchet(tolua_S); @@ -11103,7 +10930,6 @@ TOLUA_API int register_all_cocos2dx_physics(lua_State* tolua_S) lua_register_cocos2dx_physics_PhysicsShapeEdgeSegment(tolua_S); lua_register_cocos2dx_physics_PhysicsJointGear(tolua_S); lua_register_cocos2dx_physics_PhysicsContact(tolua_S); - lua_register_cocos2dx_physics_PhysicsShapePolygon(tolua_S); lua_register_cocos2dx_physics_EventListenerPhysicsContactWithBodies(tolua_S); lua_register_cocos2dx_physics_PhysicsJointRotarySpring(tolua_S); lua_register_cocos2dx_physics_PhysicsContactPostSolve(tolua_S); diff --git a/cocos/scripting/lua-bindings/auto/lua_cocos2dx_physics_auto.hpp b/cocos/scripting/lua-bindings/auto/lua_cocos2dx_physics_auto.hpp index 58175d4bf4..ac13735604 100644 --- a/cocos/scripting/lua-bindings/auto/lua_cocos2dx_physics_auto.hpp +++ b/cocos/scripting/lua-bindings/auto/lua_cocos2dx_physics_auto.hpp @@ -255,10 +255,6 @@ int register_all_cocos2dx_physics(lua_State* tolua_S); - - - - From c50394c4f362d6017c1ef49ff4ddbc672664be35 Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Sun, 29 Jun 2014 20:30:26 +0800 Subject: [PATCH 30/40] fix merge conflitcts on windows --- cocos/2d/cocos2d.vcxproj.filters | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/cocos/2d/cocos2d.vcxproj.filters b/cocos/2d/cocos2d.vcxproj.filters index ede51800c6..af367c9ab0 100644 --- a/cocos/2d/cocos2d.vcxproj.filters +++ b/cocos/2d/cocos2d.vcxproj.filters @@ -583,6 +583,12 @@ 3d + + 2d + + + 2d + @@ -1186,6 +1192,12 @@ 3d + + 2d + + + 2d + From 496de72d6ee3d2740eb409fa28bdfce41c7a392c Mon Sep 17 00:00:00 2001 From: minggo Date: Mon, 30 Jun 2014 09:39:38 +0800 Subject: [PATCH 31/40] [ci skip] --- CHANGELOG | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG b/CHANGELOG index efa4c700c1..38559fb475 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,4 +1,5 @@ cocos2d-x-3.2 ??? + [NEW] FastTMXTiledMap: added fast tmx, which is much more faster for static tiled map [NEW] GLProgramState: can use uniform location to get/set uniform values [NEW] HttpClient: added sendImmediate() [NEW] Label: support setting line height and additional kerning of label that not using system font From f06061dfec29bf9b84ad3b959f1a30223b8dc218 Mon Sep 17 00:00:00 2001 From: CocosRobot Date: Mon, 30 Jun 2014 01:42:06 +0000 Subject: [PATCH 32/40] [AUTO][ci skip]: updating cocos2dx_files.json --- templates/cocos2dx_files.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/templates/cocos2dx_files.json b/templates/cocos2dx_files.json index 6e623dca86..5cab9f5015 100644 --- a/templates/cocos2dx_files.json +++ b/templates/cocos2dx_files.json @@ -91,6 +91,10 @@ "cocos/2d/CCDrawNode.h", "cocos/2d/CCDrawingPrimitives.cpp", "cocos/2d/CCDrawingPrimitives.h", + "cocos/2d/CCFastTMXLayer.cpp", + "cocos/2d/CCFastTMXLayer.h", + "cocos/2d/CCFastTMXTiledMap.cpp", + "cocos/2d/CCFastTMXTiledMap.h", "cocos/2d/CCFont.cpp", "cocos/2d/CCFont.h", "cocos/2d/CCFontAtlas.cpp", From 26db31ebc6a6b28b56414d8601ddeb0ee737bde1 Mon Sep 17 00:00:00 2001 From: minggo Date: Mon, 30 Jun 2014 09:44:50 +0800 Subject: [PATCH 33/40] [ci skip] --- CHANGELOG | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG b/CHANGELOG index 38559fb475..f5c7198eae 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -17,6 +17,8 @@ cocos2d-x-3.2 ??? [FIX] Lua-binding: Sequence:create will cause drop-dead issue [FIX] Lua-binding: lua-tests can’t be loaded on 64 bits iOS devices and Linux [FIX] Node: Node::setScale(float) may not work properly + [FIX] Physics integration: child node can move with its father + [FIX] Physics integration: support scale [FIX] Studio support: NodeReader may cause crash [FIX] UIButton: doesn't support TTF font [FIX] UIButton: `getTitleColor()` doesn't equal to the value set by `setTitleColor()` From 1a9e42ef85ee5acb134dde7498316f853953c0de Mon Sep 17 00:00:00 2001 From: 2youyou2 Date: Mon, 30 Jun 2014 10:11:43 +0800 Subject: [PATCH 34/40] fixed armature blend func has no effect --- cocos/editor-support/cocostudio/CCArmature.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cocos/editor-support/cocostudio/CCArmature.cpp b/cocos/editor-support/cocostudio/CCArmature.cpp index 15273ac4ac..c7a465485c 100644 --- a/cocos/editor-support/cocostudio/CCArmature.cpp +++ b/cocos/editor-support/cocostudio/CCArmature.cpp @@ -408,6 +408,10 @@ void Armature::draw(cocos2d::Renderer *renderer, const Mat4 &transform, uint32_t { skin->setBlendFunc(bone->getBlendFunc()); } + else + { + skin->setBlendFunc(_blendFunc); + } skin->draw(renderer, transform, flags); } break; From aba7421ef6f0dbdeae40f9249262df09939a6274 Mon Sep 17 00:00:00 2001 From: 2youyou2 Date: Mon, 30 Jun 2014 10:14:31 +0800 Subject: [PATCH 35/40] fixed crashed when remove armature in frame event. --- cocos/editor-support/cocostudio/CCArmatureAnimation.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cocos/editor-support/cocostudio/CCArmatureAnimation.cpp b/cocos/editor-support/cocostudio/CCArmatureAnimation.cpp index 6dda544d42..febd77e6f2 100644 --- a/cocos/editor-support/cocostudio/CCArmatureAnimation.cpp +++ b/cocos/editor-support/cocostudio/CCArmatureAnimation.cpp @@ -342,6 +342,12 @@ void ArmatureAnimation::update(float dt) tween->update(dt); } + if(_frameEventQueue.size() > 0 || _movementEventQueue.size() > 0) + { + _armature->retain(); + _armature->autorelease(); + } + while (_frameEventQueue.size() > 0) { FrameEvent *event = _frameEventQueue.front(); From 707f5c2c931f3db6ee7ae473753bbe3f67015395 Mon Sep 17 00:00:00 2001 From: minggo Date: Mon, 30 Jun 2014 11:08:13 +0800 Subject: [PATCH 36/40] [ci skip] --- CHANGELOG | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG b/CHANGELOG index f5c7198eae..92cddab058 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -9,16 +9,20 @@ cocos2d-x-3.2 ??? [NEW] Node: added getName(), setName(), getChildByName(), enumerateChildren() and addChild(Node* node, int localZOrder, const std::string &name) [NEW] Node: physical body supports rotation + [NEW] Sprite3D: support c3b binary format [NEW] utils: added findChildren() to find all children by name [NEW] Value: added operator == != + [FIX] Animation3D: doesn't load original pose, which leads to wrong effect if not playing animation + [FIX] Animation3D: animation for unbinded bones lost [FIX] FileUtils: getStringFromFile may return a unterminated string [FIX] Lua-binding: Sequence:create will cause drop-dead issue [FIX] Lua-binding: lua-tests can’t be loaded on 64 bits iOS devices and Linux [FIX] Node: Node::setScale(float) may not work properly [FIX] Physics integration: child node can move with its father [FIX] Physics integration: support scale + [FIX] Sprite3D: 20% performce improved, simplify shader, use VAO and batch draw [FIX] Studio support: NodeReader may cause crash [FIX] UIButton: doesn't support TTF font [FIX] UIButton: `getTitleColor()` doesn't equal to the value set by `setTitleColor()` From d506cf613a435b2475565ff5b5406c42d227442a Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Mon, 30 Jun 2014 11:08:57 +0800 Subject: [PATCH 37/40] fix bug for tileset searching --- cocos/2d/CCFastTMXTiledMap.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/cocos/2d/CCFastTMXTiledMap.cpp b/cocos/2d/CCFastTMXTiledMap.cpp index fb61989b5f..af81acc6e1 100644 --- a/cocos/2d/CCFastTMXTiledMap.cpp +++ b/cocos/2d/CCFastTMXTiledMap.cpp @@ -119,9 +119,10 @@ TMXTilesetInfo * FastTMXTiledMap::tilesetForLayer(TMXLayerInfo *layerInfo, TMXMa Size size = layerInfo->_layerSize; auto& tilesets = mapInfo->getTilesets(); - for (const auto& iter : tilesets) + for (auto iter = tilesets.crbegin(); iter != tilesets.crend(); ++iter) { - if (iter) + TMXTilesetInfo* tilesetInfo = *iter; + if (tilesetInfo) { for( int y=0; y < size.height; y++ ) { @@ -141,8 +142,8 @@ TMXTilesetInfo * FastTMXTiledMap::tilesetForLayer(TMXLayerInfo *layerInfo, TMXMa { // Optimization: quick return // if the layer is invalid (more than 1 tileset per layer) an CCAssert will be thrown later - if( (gid & kTMXFlippedMask) >= iter->_firstGid ) - return iter; + if( (gid & kTMXFlippedMask) >= tilesetInfo->_firstGid ) + return tilesetInfo; } } } From c85c6928b470fd8eee7637ed0e03e97a7bc781db Mon Sep 17 00:00:00 2001 From: minggo Date: Mon, 30 Jun 2014 11:09:00 +0800 Subject: [PATCH 38/40] [ci skip] --- CHANGELOG | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 92cddab058..f7f394fff2 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -15,7 +15,7 @@ cocos2d-x-3.2 ??? [FIX] Animation3D: doesn't load original pose, which leads to wrong effect if not playing animation - [FIX] Animation3D: animation for unbinded bones lost + [FIX] Animation3D: animation for unskined bones lost [FIX] FileUtils: getStringFromFile may return a unterminated string [FIX] Lua-binding: Sequence:create will cause drop-dead issue [FIX] Lua-binding: lua-tests can’t be loaded on 64 bits iOS devices and Linux From 41379fb551f24119743c21c88e30e701e70df8aa Mon Sep 17 00:00:00 2001 From: minggo Date: Mon, 30 Jun 2014 11:35:03 +0800 Subject: [PATCH 39/40] [ci skip] --- CHANGELOG | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG b/CHANGELOG index f7f394fff2..1e15fa39d9 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -14,6 +14,8 @@ cocos2d-x-3.2 ??? [NEW] Value: added operator == != + [FIX] Armature: blend func has no effect + [FIX] Armature: crashed when remove armature in frame event [FIX] Animation3D: doesn't load original pose, which leads to wrong effect if not playing animation [FIX] Animation3D: animation for unskined bones lost [FIX] FileUtils: getStringFromFile may return a unterminated string From bee41712aded003869bb94e3f9b04da54ed4f382 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Mon, 30 Jun 2014 11:58:15 +0800 Subject: [PATCH 40/40] Fix Travis CI compilation issue --- cocos/editor-support/cocostudio/Android.mk | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cocos/editor-support/cocostudio/Android.mk b/cocos/editor-support/cocostudio/Android.mk index de25e273e2..192f0e8f46 100644 --- a/cocos/editor-support/cocostudio/Android.mk +++ b/cocos/editor-support/cocostudio/Android.mk @@ -57,8 +57,8 @@ WidgetReader/TextReader/TextReader.cpp \ ActionTimeline/CCNodeReader.cpp \ ActionTimeline/CCActionTimelineCache.cpp \ ActionTimeline/CCFrame.cpp \ -ActionTimeline/CCTimeline.cpp \ -ActionTimeline/CCActionTimeline.cpp \ +ActionTimeline/CCTimeLine.cpp \ +ActionTimeline/CCActionTimeline.cpp LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/.. \ $(LOCAL_PATH)/../../../external