2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
2012-09-24 21:22:20 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2012-04-19 14:35:52 +08:00
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
2014-05-17 05:36:00 +08:00
|
|
|
|
2014-05-10 09:39:25 +08:00
|
|
|
#include "2d/CCTMXLayer.h"
|
|
|
|
#include "2d/CCTMXTiledMap.h"
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "2d/CCSprite.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "base/CCDirector.h"
|
2016-06-15 15:01:26 +08:00
|
|
|
#include "base/ccUTF8.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "renderer/CCTextureCache.h"
|
metal support for cocos2d-x (#19305)
* remove deprecated files
* remove some deprecated codes
* remove more deprecated codes
* remove ui deprecated codes
* remove more deprecated codes
* remove deprecated codes in ccmenuitem
* remove more deprecated codes in ui
* remove more deprecated codes in ui
* remove more deprecated codes in ui
* remove more deprecated codes
* remove more deprecated codes
* remove more deprecated codes
* remove vr related codes and ignore some modules
* remove allocator
* remove some config
* 【Feature】add back-end project file
* [Feature] add back-end file
* add pipeline descriptor and shader cache
* [Feature] support sprite for backend
* [Feature] remove unneeded code
* [Feature] according to es2.0 spec, you must use clamp-to-edge as texture wrap mode, and no mipmapping for non-power-of-two texture
* [Feature] set texture wrap mode to clamp-to-edge, and no mipmapping for non-power-of-two texture
* [Feature] remove macro define to .cpp file
* [Feature] add log info
* [Feature] add PipelineDescriptor for TriangleCommand
* [Feature] add PipelineDescriptor object as member of TriangleCommand
* [Feature] add getPipelineDescriptor method
* add renderbackend
* complete pipeline descriptor
* [Feature] add viewport in RenderCommand
* set viewport when rendrering
* [Feature] occur error when using RendererBackend, to be fixed.
* a workaround to fix black screen on macOS 10.14 (#19090)
* add rendererbackend init function
* fix typo
* [Feature] modify testFile
* [BugFix] modify shader path
* [Feature] set default viewport
* fix projection
* [Feature] modify log info
* [BugFix] change viewport data type to int
* [BugFix] add BindGroup to PipelienDescriptor
* [BugFix] change a_position to vec3 in sprite.vert
* [BugFix] set vertexLayout according to V3F_C4B_T2F structure
* [Feature] revert a_position to vec4
* [Feature] renderer should not use gl codes directly
* [Feature] it's better not use default value parameter
* fix depth test setting
* rendererbackend -> renderer
* clear color and depth at begin
* add metal backend
* metal support normalized attribute
* simplify codes
* update external
* add render pass desctriptor in pipeline descriptor
* fix warnings
* fix crash and memeory leak
* refactor Texture2D
* put pipeline descriptor into render command
* simplify codes
* [Feature] update Sprite
* fix crash when closing app
* [Feature] update SpriteBatchNode and TextureAtlas
* support render texture(not finish)
* [Feature] remove unused code
* make tests work on mac
* fix download-deps path error
* make tests work on iOS
* [Feature] support ttf under normal label effect
* refactor triangle command processing
* let renderer handle more common commands
* refactor backend
* make render texture work
* [Feature] refactor backend for GL
* [Feature]Renaming to make it easy to understand
* [Feature] change warp mode to CLAMP_TO_EDGE
* fix ghost
* simplify visit render queue logic
* support progress timer without rial mode
* support partcile system
* Feature/update label (#149)
* [BugFix] fix compile error
* [Feature] support outline effect in ios
* [Feature] add shader file
* [BugFix] fix begin and end RenderPass
* [Feature] update CustomCommand
* [Feature] revert project.pbxproj
* [Feature] simplify codes
* [BugFix] pack AI88 to RGBA8888 only when outline enable
* [Feature] support shadow effect in Label
* [Feature] support BMFont
* [Feature] support glow effect
* [Feature] simplify shader files
* LabelAtlas work
* handle blend function correctly
* support tile map
* don't share buffer in metal
* alloc buffer size as needed
* support more tilemap
* Merge branch 'minggo/metal-support' into feature/updateLabel
* minggo/metal-support:
support tile map
handle blend function correctly
LabelAtlas work
Feature/update label (#149)
support partcile system
# Conflicts:
# cocos/2d/CCLabel.cpp
# cocos/2d/CCSprite.cpp
# cocos/2d/CCSpriteBatchNode.cpp
# cocos/renderer/CCQuadCommand.cpp
# cocos/renderer/CCQuadCommand.h
* render texture work without saving file
* use global viewport
* grid3d works
* remove grabber
* tiled3d works
* [BugFix] fix label bug
* [Feature] add updateSubData for buffer
* [Feature] remove setVertexCount
* support depth test
* add callback command
* [Feature] add UITest
* [Feature] update UITest
* [Feature] remove unneeded codes
* fix custom command issue
* fix layer color blend issue
* [BugFix] fix iOS compile error
* [Feature] remove unneeded codes
* [Feature] fix updateVertexBuffer
* layerradial works
* add draw test back
* fix batch issue
* fix compiling error
* [BugFix] support ETC1
* [BugFix] get the correct pipelineDescriptor
* [BugFix] skip draw when backendTexture nullptr
* clipping node support
* [Feature] add shader files
* fix stencil issue in metal
* [Feature] update UILayoutTest
* [BugFix] skip drawing when vertexCount is zero
* refactor renderer
* add set global z order for stencil manager commands
* fix warnings caused by type
* remove viewport in render command
* [Feature] fix warnings caused by type
* [BugFix] clear vertexCount and indexCount for CustomComand when needed
* [Feature] update clear for CustomCommand
* ios use metal
* fix viewport issue
* fix LayerColorGradient crash
* [cmake] transport to android and windows (#160)
* save point 1
* compile on windows
* run on android
* revert useless change
* android set CC_ENABLE_CACHE_TEXTURE_DATA to 1
* add initGlew
* fix android crash
* add TODO new-renderer
* review update
* revert onGLFWWindowPosCallback
* fix android compiling error
* Impl progress radial (#162)
* progresstimer add radial impl
* default drawType to element
* dec invoke times of createVertexBuffer (#163)
* support depth/stencil format for gl backend
* simplify progress timer codes
* support motionstreak, effect is wrong
* fix motionstreak issue
* [Feature] update Scissor Test (#161)
* [Feature] update Scissor Test
* [Feature] update ScissorTest
* [Feature] rename function
* [Feature] get constant reference if needed
* [Feature] show render status (#164)
* improve performance
* fix depth state
* fill error that triangle vertex/index number bigger than buffer
* fix compiline error in release mode
* fix buffer conflict between CPU and GPU on iOS/macOS
* Renderer refactor (#165)
* use one vertes/index buffer with opengl
* fix error on windows
* custom command support index format config
* CCLayer: compact vertex data structure
* update comment
* fix doc
* support fast tilemap
* pass index format instead
* fix some wrong effect
* fix render texture error
* fix texture per-element size
* fix texture format error
* BlendFunc type refactor, GLenum -> backend::BlendFactor (#167)
* BlendFunc use backend::BlendFactor as inner field
* update comments
* use int to replace GLenum
* update xcode project fiel
* rename to GLBlendConst
* add ccConstants.h
* update xcode project file
* update copyright
* remove primitive command
* remove CCPrimitive.cpp/.h
* remove deprecated files
* remove unneeded files
* remove multiple view support
* remove multiple view support
* remove the usage of frame buffer in camera
* director don't use frame buffer
* remove FrameBuffer
* remove BatchCommand
* add some api reference
* add physics2d back
* fix crash when close app on mac
* improve render texture
* fix rendertexture issue
* fix rendertexture issue
* simplify codes
* CMake support for mac & ios (#169)
* update cmake
* fix compile error
* update 3rd libs version
* remove CCThread.h/.cpp
* remove ccthread
* use audio engine to implement simple audio engine
* remove unneeded codes
* remove deprecated codes
* remove winrt macro
* remove CC_USE_WIC
* set partcile blend function in more elegant way
* remove unneeded codes
* remove unneeded codes
* cmake works on windows
* update project setting
* improve performance
* GLFloat -> float
* sync v3 cmake improvements into metal-support (#172)
* pick: modern cmake, compile definitions improvement (#19139)
* modern cmake, use target_compile_definitions partly
* simplify macro define, remove USE_*
* modern cmake, macro define
* add physics 2d macro define into ccConfig.h
* remove USE_CHIPMUNK macro in build.gradle
* remove CocosSelectModule.cmake
* shrink useless define
* simplify compile options config, re-add if necessary
* update external for tmp CI test
* un-quote target_compile_options value
* add "-g" parameter only when debug mode
* keep single build type when generator Xcode & VS projecy
* update external for tmp CI tes
* add static_cast<char>(-1), fix -Wc++11-narrowing
* simplify win32 compile define
* not modify code, only improve compile options
# Conflicts:
# .gitignore
# cmake/Modules/CocosConfigDepend.cmake
# cocos/CMakeLists.txt
# external/config.json
# tests/cpp-tests/CMakeLists.txt
* modern cmake, improve cmake_compiler_flags (#19145)
* cmake_compiler_flags
* Fix typo
* Fix typo2
* Remove chanages from Android.mk
* correct lua template cmake build (#19149)
* don't add -Wno-deprecated into jsb target
* correct lua template cmake build
* fix win32 lua template compile error
* prevent cmake in-source-build friendly (#19151)
* pick: Copy resources to "Resources/" on win32 like in linux configuration
* add "/Z7" for cpp-tests on windows
* [cmake] fix iOS xcode property setting failed (#19208)
* fix iOS xcode property setting failed
* use search_depend_libs_recursive at dlls collect
* fix typo
* [cmake] add find_host_library into iOS toolchain file (#19230)
* pick: [lua android] use luajit & template cmake update (#19239)
* increase cmake stability , remove tests/CMakeLists.txt (#19261)
* cmake win32 Precompiled header (#19273)
* Precompiled header
* Fix
* Precompiled header for cocos
* Precompiled header jscocos2d
* Fix for COCOS2D_DEBUG is always 1 on Android (#19291)
Related #19289
* little build fix, tests cpp-tests works on mac
* sync v3 build related codes into metal-support (#173)
* strict initialization for std::array
* remove proj.win32 project configs
* modern cmake, cmake_cleanup_remove_unused_variables (#19146)
* Switch travis CI to xenial (#19207)
* Switch travis CI to xenial
* Remove language: android
* Set language: cpp
* Fix java problem
* Update sdkmanager
* Fix sdkmanger
* next sdkmanager fix
* Remove xenial from android
* revert to sdk-tools-{system}-3859397
* Remove linux cmake install
* Update before-install.sh
* Update .travis.yml
* Simplify install-deps-linux.sh, tested on Ubuntu 16.04 (#19212)
* Simplify install-deps-linux.sh
* Cleanup
* pick: install ninja
* update cocos2d-console submodule
* for metal-support alpha release, we only test cpp
* add HelloCpp into project(Cocos2d-x) for tmp test
* update extenal metal-support-4
* update uniform setting
* [Feature] update BindGroup
* [Feature] empty-test
* [Feature] cpp-test
* [Feature] fix GL compiler error
* [Feature] fix GL crash
* [Feature] empty-test
* [Feature] cpp-tests
* [feature] improve frameRate
* [feature] fix opengl compile error
* [feature] fix opengl compile error
* [BugFix] fix compute maxLocation error
* [Feature] update setting unifrom
* [Feature] fix namespace
* [Feature] remove unneeded code
* [Bugfix] fix project file
* [Feature] update review
* [texture2d] impl texture format support (#175)
* texture update
* update
* update texture
* commit
* compile on windows
* ddd
* rename
* rename methods
* no crash
* save gl
* save
* save
* rename
* move out pixel format convert functions
* metal crash
* update
* update android
* support gles compressed texture format
* support more compress format
* add more conversion methods
* ss
* save
* update conversion methods
* add PVRTC format support
* reformat
* add marco linux
* fix GL marcro
* pvrtc supported only by ios 8.0+
* remove unused cmake
* revert change
* refactor Texture2D::initWithData
* fix conversion log
* refactor Texture2D::initWithData
* remove some OpenGL constants for PVRTC
* add todo
* fix typo
* AutoTest works on mac/iOS by disable part cases, sync v3 bug fix (#174)
* review cpp-tests, and fix part issues on start auto test
* sync png format fix: Node:Particle3D abnormal texture effects #19204
* fix cpp-tests SpritePolygon crash, wrong png format (#19170)
* fix wrong png convert format from sRGB to Gray
* erase plist index if all frames was erased
* test_A8.png have I8 format, fix it
* [CCSpriteCache] allow re-add plist & add testcase (#19175)
* allow re-add plist & add testcase
* remove comments/rename method/update testcase
* fix isSpriteFramesWithFileLoaded & add testcase
* remove used variable
* remove unused variable
* fix double free issues when js/lua-tests exit on iOS (#19236)
* disable part cases, AutoTest works without crash on mac
* update cocos2dx files json, to test cocos new next
* fix spritecache plist parsing issue (#19269)
* [linux] Fix FileUtils::getContents with folder (#19157)
* fix FileUtils::getContents on linux/mac
* use stat.st_mode
* simplify
* [CCFileUtils] win32 getFileSize (#19176)
* win32 getFileSize
* fix stat
* [cpp test-Android]20:FileUtils/2 change title (#19197)
* sync #19200
* sync #19231
* [android lua] improve performance of lua loader (#19234)
* [lua] improve performance of lua loader
* remove cache fix
* Revert "fix spritecache plist parsing issue (#19269)"
This reverts commit f3a85ece4307a7b90816c34489d1ed2c8fd11baf.
* remove win32 project files ref in template.json
* add metal framework lnk ref into cpp template
* test on iOS, and disable part cases
* alBufferData instead of alBufferDataStatic for small audio file on Apple (#19227)
* changes AudioCache to use alBufferData instead of alBufferDataStatic
(also makes test 19 faster to trigger openal bugs faster)
The original problem: CrashIfClientProvidedBogusAudioBufferList
https://github.com/cocos2d/cocos2d-x/issues/18948
is not happening anymore, but there's still a not very frequent issue
that makes OpenAL crash with a call stack like this.
AudioCache::readDataTask > alBufferData > CleanUpDeadBufferList
It happes more frequently when the device is "cold", which means after
half an hour of not using the device (locked).
I could not find the actual source code for iOS OpenAL, so I used the
macOS versions:
https://opensource.apple.com/source/OpenAL/OpenAL-48.7/Source/OpenAL/oalImp.cpp.auto.html
They seem to use CAGuard.h to make sure the dead buffer list
has no threading issues. I'm worried because the CAGuard code I found
has macos and win32 define but no iOS, so I'm not sure. I guess the
iOS version is different and has the guard.
I could not find a place in the code that's unprotected by the locks
except the InitializeBufferMap() which should not be called more than
once from cocos, and there's a workaround in AudioEngine-impl for it.
I reduced the occurence of the CleanUpDeadBufferList crash by moving
the guard in ~AudioCache to cover the alDeleteBuffers call.
* remove hack method "setTimeout" on audio
* AutoTest works on iOS
* support set ios deployment target for root project
* enable all texture2d cases, since Jiang have fixed
* add CCTextureUtils to xcode project file (#176)
* add leak cases for SpriteFrameCache (#177)
* re-add SpriteFrameCache cases
* update template file json
* Update SpriteFrameCacheTest.cpp
* fix compiling error
2019-01-18 15:08:25 +08:00
|
|
|
#include "renderer/ccShaders.h"
|
|
|
|
#include "renderer/backend/Program.h"
|
|
|
|
#include "renderer/backend/ProgramState.h"
|
2014-04-09 22:30:49 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// TMXLayer - init & alloc & dealloc
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
TMXLayer * TMXLayer::create(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
TMXLayer *ret = new (std::nothrow) TMXLayer();
|
2013-12-18 17:47:20 +08:00
|
|
|
if (ret->initWithTilesetInfo(tilesetInfo, layerInfo, mapInfo))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2015-10-19 18:47:36 +08:00
|
|
|
CC_SAFE_DELETE(ret);
|
2013-12-18 17:47:20 +08:00
|
|
|
return nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-06-20 14:13:12 +08:00
|
|
|
bool TMXLayer::initWithTilesetInfo(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-08-30 03:54:24 +08:00
|
|
|
// FIXME:: is 35% a good estimate ?
|
2013-06-20 14:13:12 +08:00
|
|
|
Size size = layerInfo->_layerSize;
|
2012-04-19 14:35:52 +08:00
|
|
|
float totalNumberOfTiles = size.width * size.height;
|
|
|
|
float capacity = totalNumberOfTiles * 0.35f + 1; // 35 percent is occupied ?
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
Texture2D *texture = nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
if( tilesetInfo )
|
|
|
|
{
|
2016-02-03 23:12:37 +08:00
|
|
|
texture = Director::getInstance()->getTextureCache()->addImage(tilesetInfo->_sourceImage);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2015-08-19 14:02:12 +08:00
|
|
|
if (nullptr == texture)
|
|
|
|
return false;
|
|
|
|
|
2013-12-20 09:50:53 +08:00
|
|
|
if (SpriteBatchNode::initWithTexture(texture, static_cast<ssize_t>(capacity)))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// layerInfo
|
2013-06-15 14:03:30 +08:00
|
|
|
_layerName = layerInfo->_name;
|
|
|
|
_layerSize = size;
|
|
|
|
_tiles = layerInfo->_tiles;
|
|
|
|
_opacity = layerInfo->_opacity;
|
2013-12-03 14:47:35 +08:00
|
|
|
setProperties(layerInfo->getProperties());
|
2013-07-12 06:24:23 +08:00
|
|
|
_contentScaleFactor = Director::getInstance()->getContentScaleFactor();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// tilesetInfo
|
2013-06-15 14:03:30 +08:00
|
|
|
_tileSet = tilesetInfo;
|
|
|
|
CC_SAFE_RETAIN(_tileSet);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// mapInfo
|
2013-06-15 14:03:30 +08:00
|
|
|
_mapTileSize = mapInfo->getTileSize();
|
|
|
|
_layerOrientation = mapInfo->getOrientation();
|
2016-07-02 06:51:00 +08:00
|
|
|
_staggerAxis = mapInfo->getStaggerAxis();
|
|
|
|
_staggerIndex = mapInfo->getStaggerIndex();
|
|
|
|
_hexSideLength = mapInfo->getHexSideLength();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// offset (after layer orientation is set);
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 offset = this->calculateLayerOffset(layerInfo->_offset);
|
2012-04-19 14:35:52 +08:00
|
|
|
this->setPosition(CC_POINT_PIXELS_TO_POINTS(offset));
|
|
|
|
|
2013-12-20 09:50:53 +08:00
|
|
|
_atlasIndexArray = ccCArrayNew(totalNumberOfTiles);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2016-08-19 16:28:47 +08:00
|
|
|
float width = 0;
|
|
|
|
float height = 0;
|
|
|
|
if (_layerOrientation == TMXOrientationHex) {
|
|
|
|
if (_staggerAxis == TMXStaggerAxis_X) {
|
|
|
|
height = _mapTileSize.height * (_layerSize.height + 0.5);
|
|
|
|
width = (_mapTileSize.width + _hexSideLength) * ((int)(_layerSize.width / 2)) + _mapTileSize.width * ((int)_layerSize.width % 2);
|
|
|
|
} else {
|
|
|
|
width = _mapTileSize.width * (_layerSize.width + 0.5);
|
|
|
|
height = (_mapTileSize.height + _hexSideLength) * ((int)(_layerSize.height / 2)) + _mapTileSize.height * ((int)_layerSize.height % 2);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
width = _layerSize.width * _mapTileSize.width;
|
|
|
|
height = _layerSize.height * _mapTileSize.height;
|
|
|
|
}
|
|
|
|
this->setContentSize(CC_SIZE_PIXELS_TO_POINTS(Size(width, height)));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_useAutomaticVertexZ = false;
|
|
|
|
_vertexZvalue = 0;
|
2012-08-20 14:06:29 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2012-08-20 14:06:29 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
TMXLayer::TMXLayer()
|
2013-07-29 14:07:57 +08:00
|
|
|
:_layerName("")
|
|
|
|
,_opacity(0)
|
|
|
|
,_vertexZvalue(0)
|
|
|
|
,_useAutomaticVertexZ(false)
|
2013-12-18 17:47:20 +08:00
|
|
|
,_reusedTile(nullptr)
|
|
|
|
,_atlasIndexArray(nullptr)
|
2013-07-29 14:07:57 +08:00
|
|
|
,_contentScaleFactor(1.0f)
|
|
|
|
,_layerSize(Size::ZERO)
|
2013-07-12 14:47:36 +08:00
|
|
|
,_mapTileSize(Size::ZERO)
|
2013-12-18 17:47:20 +08:00
|
|
|
,_tiles(nullptr)
|
|
|
|
,_tileSet(nullptr)
|
2013-07-29 14:07:57 +08:00
|
|
|
,_layerOrientation(TMXOrientationOrtho)
|
2016-07-02 06:51:00 +08:00
|
|
|
,_staggerAxis(TMXStaggerAxis_Y)
|
|
|
|
,_staggerIndex(TMXStaggerIndex_Even)
|
|
|
|
,_hexSideLength(0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{}
|
2012-08-20 14:06:29 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
TMXLayer::~TMXLayer()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_tileSet);
|
|
|
|
CC_SAFE_RELEASE(_reusedTile);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_atlasIndexArray)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
ccCArrayFree(_atlasIndexArray);
|
2013-12-18 17:47:20 +08:00
|
|
|
_atlasIndexArray = nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2016-09-12 10:02:48 +08:00
|
|
|
CC_SAFE_FREE(_tiles);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-08-20 14:06:29 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TMXLayer::releaseMap()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_tiles)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2016-09-26 14:18:48 +08:00
|
|
|
free(_tiles);
|
2013-12-18 17:47:20 +08:00
|
|
|
_tiles = nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_atlasIndexArray)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
ccCArrayFree(_atlasIndexArray);
|
2013-12-18 17:47:20 +08:00
|
|
|
_atlasIndexArray = nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// TMXLayer - setup Tiles
|
|
|
|
void TMXLayer::setupTiles()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// Optimization: quick hack that sets the image size on the tileset
|
2013-06-15 14:03:30 +08:00
|
|
|
_tileSet->_imageSize = _textureAtlas->getTexture()->getContentSizeInPixels();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// By default all the tiles are aliased
|
|
|
|
// pros:
|
|
|
|
// - easier to render
|
|
|
|
// cons:
|
|
|
|
// - difficult to scale / rotate / etc.
|
2013-06-15 14:03:30 +08:00
|
|
|
_textureAtlas->getTexture()->setAliasTexParameters();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
//CFByteOrder o = CFByteOrderGetCurrent();
|
|
|
|
|
|
|
|
// Parse cocos2d properties
|
|
|
|
this->parseInternalProperties();
|
|
|
|
|
2013-12-20 09:50:53 +08:00
|
|
|
for (int y=0; y < _layerSize.height; y++)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-20 09:50:53 +08:00
|
|
|
for (int x=0; x < _layerSize.width; x++)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2016-07-02 06:51:00 +08:00
|
|
|
int newX = x;
|
2016-07-15 02:21:24 +08:00
|
|
|
// fix correct render ordering in Hexagonal maps when stagger axis == x
|
2016-07-02 06:51:00 +08:00
|
|
|
if (_staggerAxis == TMXStaggerAxis_X && _layerOrientation == TMXOrientationHex)
|
|
|
|
{
|
2016-07-20 13:55:05 +08:00
|
|
|
if (_staggerIndex == TMXStaggerIndex_Odd)
|
|
|
|
{
|
|
|
|
if (x >= _layerSize.width/2)
|
|
|
|
newX = (x - std::ceil(_layerSize.width/2)) * 2 + 1;
|
|
|
|
else
|
|
|
|
newX = x * 2;
|
|
|
|
} else {
|
|
|
|
// TMXStaggerIndex_Even
|
|
|
|
if (x >= static_cast<int>(_layerSize.width/2))
|
|
|
|
newX = (x - static_cast<int>(_layerSize.width/2)) * 2;
|
|
|
|
else
|
|
|
|
newX = x * 2 + 1;
|
|
|
|
}
|
2016-07-02 06:51:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int pos = static_cast<int>(newX + _layerSize.width * y);
|
2013-12-20 09:50:53 +08:00
|
|
|
int gid = _tiles[ pos ];
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// gid are stored in little endian.
|
|
|
|
// if host is big endian, then swap
|
|
|
|
//if( o == CFByteOrderBigEndian )
|
|
|
|
// gid = CFSwapInt32( gid );
|
|
|
|
/* We support little endian.*/
|
|
|
|
|
2014-08-30 03:54:24 +08:00
|
|
|
// FIXME:: gid == 0 --> empty tile
|
2012-08-20 14:06:29 +08:00
|
|
|
if (gid != 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2016-07-02 06:51:00 +08:00
|
|
|
this->appendTileForGID(gid, Vec2(newX, y));
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// TMXLayer - Properties
|
2013-12-03 14:47:35 +08:00
|
|
|
Value TMXLayer::getProperty(const std::string& propertyName) const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-03 14:47:35 +08:00
|
|
|
if (_properties.find(propertyName) != _properties.end())
|
|
|
|
return _properties.at(propertyName);
|
|
|
|
|
|
|
|
return Value();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TMXLayer::parseInternalProperties()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// if cc_vertex=automatic, then tiles will be rendered using vertexz
|
|
|
|
|
2013-12-03 14:47:35 +08:00
|
|
|
auto vertexz = getProperty("cc_vertexz");
|
|
|
|
if (!vertexz.isNull())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-03 14:47:35 +08:00
|
|
|
std::string vertexZStr = vertexz.asString();
|
2012-04-19 14:35:52 +08:00
|
|
|
// If "automatic" is on, then parse the "cc_alpha_func" too
|
2013-12-03 14:47:35 +08:00
|
|
|
if (vertexZStr == "automatic")
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_useAutomaticVertexZ = true;
|
2013-12-03 14:47:35 +08:00
|
|
|
auto alphaFuncVal = getProperty("cc_alpha_func");
|
|
|
|
float alphaFuncValue = alphaFuncVal.asFloat();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
metal support for cocos2d-x (#19305)
* remove deprecated files
* remove some deprecated codes
* remove more deprecated codes
* remove ui deprecated codes
* remove more deprecated codes
* remove deprecated codes in ccmenuitem
* remove more deprecated codes in ui
* remove more deprecated codes in ui
* remove more deprecated codes in ui
* remove more deprecated codes
* remove more deprecated codes
* remove more deprecated codes
* remove vr related codes and ignore some modules
* remove allocator
* remove some config
* 【Feature】add back-end project file
* [Feature] add back-end file
* add pipeline descriptor and shader cache
* [Feature] support sprite for backend
* [Feature] remove unneeded code
* [Feature] according to es2.0 spec, you must use clamp-to-edge as texture wrap mode, and no mipmapping for non-power-of-two texture
* [Feature] set texture wrap mode to clamp-to-edge, and no mipmapping for non-power-of-two texture
* [Feature] remove macro define to .cpp file
* [Feature] add log info
* [Feature] add PipelineDescriptor for TriangleCommand
* [Feature] add PipelineDescriptor object as member of TriangleCommand
* [Feature] add getPipelineDescriptor method
* add renderbackend
* complete pipeline descriptor
* [Feature] add viewport in RenderCommand
* set viewport when rendrering
* [Feature] occur error when using RendererBackend, to be fixed.
* a workaround to fix black screen on macOS 10.14 (#19090)
* add rendererbackend init function
* fix typo
* [Feature] modify testFile
* [BugFix] modify shader path
* [Feature] set default viewport
* fix projection
* [Feature] modify log info
* [BugFix] change viewport data type to int
* [BugFix] add BindGroup to PipelienDescriptor
* [BugFix] change a_position to vec3 in sprite.vert
* [BugFix] set vertexLayout according to V3F_C4B_T2F structure
* [Feature] revert a_position to vec4
* [Feature] renderer should not use gl codes directly
* [Feature] it's better not use default value parameter
* fix depth test setting
* rendererbackend -> renderer
* clear color and depth at begin
* add metal backend
* metal support normalized attribute
* simplify codes
* update external
* add render pass desctriptor in pipeline descriptor
* fix warnings
* fix crash and memeory leak
* refactor Texture2D
* put pipeline descriptor into render command
* simplify codes
* [Feature] update Sprite
* fix crash when closing app
* [Feature] update SpriteBatchNode and TextureAtlas
* support render texture(not finish)
* [Feature] remove unused code
* make tests work on mac
* fix download-deps path error
* make tests work on iOS
* [Feature] support ttf under normal label effect
* refactor triangle command processing
* let renderer handle more common commands
* refactor backend
* make render texture work
* [Feature] refactor backend for GL
* [Feature]Renaming to make it easy to understand
* [Feature] change warp mode to CLAMP_TO_EDGE
* fix ghost
* simplify visit render queue logic
* support progress timer without rial mode
* support partcile system
* Feature/update label (#149)
* [BugFix] fix compile error
* [Feature] support outline effect in ios
* [Feature] add shader file
* [BugFix] fix begin and end RenderPass
* [Feature] update CustomCommand
* [Feature] revert project.pbxproj
* [Feature] simplify codes
* [BugFix] pack AI88 to RGBA8888 only when outline enable
* [Feature] support shadow effect in Label
* [Feature] support BMFont
* [Feature] support glow effect
* [Feature] simplify shader files
* LabelAtlas work
* handle blend function correctly
* support tile map
* don't share buffer in metal
* alloc buffer size as needed
* support more tilemap
* Merge branch 'minggo/metal-support' into feature/updateLabel
* minggo/metal-support:
support tile map
handle blend function correctly
LabelAtlas work
Feature/update label (#149)
support partcile system
# Conflicts:
# cocos/2d/CCLabel.cpp
# cocos/2d/CCSprite.cpp
# cocos/2d/CCSpriteBatchNode.cpp
# cocos/renderer/CCQuadCommand.cpp
# cocos/renderer/CCQuadCommand.h
* render texture work without saving file
* use global viewport
* grid3d works
* remove grabber
* tiled3d works
* [BugFix] fix label bug
* [Feature] add updateSubData for buffer
* [Feature] remove setVertexCount
* support depth test
* add callback command
* [Feature] add UITest
* [Feature] update UITest
* [Feature] remove unneeded codes
* fix custom command issue
* fix layer color blend issue
* [BugFix] fix iOS compile error
* [Feature] remove unneeded codes
* [Feature] fix updateVertexBuffer
* layerradial works
* add draw test back
* fix batch issue
* fix compiling error
* [BugFix] support ETC1
* [BugFix] get the correct pipelineDescriptor
* [BugFix] skip draw when backendTexture nullptr
* clipping node support
* [Feature] add shader files
* fix stencil issue in metal
* [Feature] update UILayoutTest
* [BugFix] skip drawing when vertexCount is zero
* refactor renderer
* add set global z order for stencil manager commands
* fix warnings caused by type
* remove viewport in render command
* [Feature] fix warnings caused by type
* [BugFix] clear vertexCount and indexCount for CustomComand when needed
* [Feature] update clear for CustomCommand
* ios use metal
* fix viewport issue
* fix LayerColorGradient crash
* [cmake] transport to android and windows (#160)
* save point 1
* compile on windows
* run on android
* revert useless change
* android set CC_ENABLE_CACHE_TEXTURE_DATA to 1
* add initGlew
* fix android crash
* add TODO new-renderer
* review update
* revert onGLFWWindowPosCallback
* fix android compiling error
* Impl progress radial (#162)
* progresstimer add radial impl
* default drawType to element
* dec invoke times of createVertexBuffer (#163)
* support depth/stencil format for gl backend
* simplify progress timer codes
* support motionstreak, effect is wrong
* fix motionstreak issue
* [Feature] update Scissor Test (#161)
* [Feature] update Scissor Test
* [Feature] update ScissorTest
* [Feature] rename function
* [Feature] get constant reference if needed
* [Feature] show render status (#164)
* improve performance
* fix depth state
* fill error that triangle vertex/index number bigger than buffer
* fix compiline error in release mode
* fix buffer conflict between CPU and GPU on iOS/macOS
* Renderer refactor (#165)
* use one vertes/index buffer with opengl
* fix error on windows
* custom command support index format config
* CCLayer: compact vertex data structure
* update comment
* fix doc
* support fast tilemap
* pass index format instead
* fix some wrong effect
* fix render texture error
* fix texture per-element size
* fix texture format error
* BlendFunc type refactor, GLenum -> backend::BlendFactor (#167)
* BlendFunc use backend::BlendFactor as inner field
* update comments
* use int to replace GLenum
* update xcode project fiel
* rename to GLBlendConst
* add ccConstants.h
* update xcode project file
* update copyright
* remove primitive command
* remove CCPrimitive.cpp/.h
* remove deprecated files
* remove unneeded files
* remove multiple view support
* remove multiple view support
* remove the usage of frame buffer in camera
* director don't use frame buffer
* remove FrameBuffer
* remove BatchCommand
* add some api reference
* add physics2d back
* fix crash when close app on mac
* improve render texture
* fix rendertexture issue
* fix rendertexture issue
* simplify codes
* CMake support for mac & ios (#169)
* update cmake
* fix compile error
* update 3rd libs version
* remove CCThread.h/.cpp
* remove ccthread
* use audio engine to implement simple audio engine
* remove unneeded codes
* remove deprecated codes
* remove winrt macro
* remove CC_USE_WIC
* set partcile blend function in more elegant way
* remove unneeded codes
* remove unneeded codes
* cmake works on windows
* update project setting
* improve performance
* GLFloat -> float
* sync v3 cmake improvements into metal-support (#172)
* pick: modern cmake, compile definitions improvement (#19139)
* modern cmake, use target_compile_definitions partly
* simplify macro define, remove USE_*
* modern cmake, macro define
* add physics 2d macro define into ccConfig.h
* remove USE_CHIPMUNK macro in build.gradle
* remove CocosSelectModule.cmake
* shrink useless define
* simplify compile options config, re-add if necessary
* update external for tmp CI test
* un-quote target_compile_options value
* add "-g" parameter only when debug mode
* keep single build type when generator Xcode & VS projecy
* update external for tmp CI tes
* add static_cast<char>(-1), fix -Wc++11-narrowing
* simplify win32 compile define
* not modify code, only improve compile options
# Conflicts:
# .gitignore
# cmake/Modules/CocosConfigDepend.cmake
# cocos/CMakeLists.txt
# external/config.json
# tests/cpp-tests/CMakeLists.txt
* modern cmake, improve cmake_compiler_flags (#19145)
* cmake_compiler_flags
* Fix typo
* Fix typo2
* Remove chanages from Android.mk
* correct lua template cmake build (#19149)
* don't add -Wno-deprecated into jsb target
* correct lua template cmake build
* fix win32 lua template compile error
* prevent cmake in-source-build friendly (#19151)
* pick: Copy resources to "Resources/" on win32 like in linux configuration
* add "/Z7" for cpp-tests on windows
* [cmake] fix iOS xcode property setting failed (#19208)
* fix iOS xcode property setting failed
* use search_depend_libs_recursive at dlls collect
* fix typo
* [cmake] add find_host_library into iOS toolchain file (#19230)
* pick: [lua android] use luajit & template cmake update (#19239)
* increase cmake stability , remove tests/CMakeLists.txt (#19261)
* cmake win32 Precompiled header (#19273)
* Precompiled header
* Fix
* Precompiled header for cocos
* Precompiled header jscocos2d
* Fix for COCOS2D_DEBUG is always 1 on Android (#19291)
Related #19289
* little build fix, tests cpp-tests works on mac
* sync v3 build related codes into metal-support (#173)
* strict initialization for std::array
* remove proj.win32 project configs
* modern cmake, cmake_cleanup_remove_unused_variables (#19146)
* Switch travis CI to xenial (#19207)
* Switch travis CI to xenial
* Remove language: android
* Set language: cpp
* Fix java problem
* Update sdkmanager
* Fix sdkmanger
* next sdkmanager fix
* Remove xenial from android
* revert to sdk-tools-{system}-3859397
* Remove linux cmake install
* Update before-install.sh
* Update .travis.yml
* Simplify install-deps-linux.sh, tested on Ubuntu 16.04 (#19212)
* Simplify install-deps-linux.sh
* Cleanup
* pick: install ninja
* update cocos2d-console submodule
* for metal-support alpha release, we only test cpp
* add HelloCpp into project(Cocos2d-x) for tmp test
* update extenal metal-support-4
* update uniform setting
* [Feature] update BindGroup
* [Feature] empty-test
* [Feature] cpp-test
* [Feature] fix GL compiler error
* [Feature] fix GL crash
* [Feature] empty-test
* [Feature] cpp-tests
* [feature] improve frameRate
* [feature] fix opengl compile error
* [feature] fix opengl compile error
* [BugFix] fix compute maxLocation error
* [Feature] update setting unifrom
* [Feature] fix namespace
* [Feature] remove unneeded code
* [Bugfix] fix project file
* [Feature] update review
* [texture2d] impl texture format support (#175)
* texture update
* update
* update texture
* commit
* compile on windows
* ddd
* rename
* rename methods
* no crash
* save gl
* save
* save
* rename
* move out pixel format convert functions
* metal crash
* update
* update android
* support gles compressed texture format
* support more compress format
* add more conversion methods
* ss
* save
* update conversion methods
* add PVRTC format support
* reformat
* add marco linux
* fix GL marcro
* pvrtc supported only by ios 8.0+
* remove unused cmake
* revert change
* refactor Texture2D::initWithData
* fix conversion log
* refactor Texture2D::initWithData
* remove some OpenGL constants for PVRTC
* add todo
* fix typo
* AutoTest works on mac/iOS by disable part cases, sync v3 bug fix (#174)
* review cpp-tests, and fix part issues on start auto test
* sync png format fix: Node:Particle3D abnormal texture effects #19204
* fix cpp-tests SpritePolygon crash, wrong png format (#19170)
* fix wrong png convert format from sRGB to Gray
* erase plist index if all frames was erased
* test_A8.png have I8 format, fix it
* [CCSpriteCache] allow re-add plist & add testcase (#19175)
* allow re-add plist & add testcase
* remove comments/rename method/update testcase
* fix isSpriteFramesWithFileLoaded & add testcase
* remove used variable
* remove unused variable
* fix double free issues when js/lua-tests exit on iOS (#19236)
* disable part cases, AutoTest works without crash on mac
* update cocos2dx files json, to test cocos new next
* fix spritecache plist parsing issue (#19269)
* [linux] Fix FileUtils::getContents with folder (#19157)
* fix FileUtils::getContents on linux/mac
* use stat.st_mode
* simplify
* [CCFileUtils] win32 getFileSize (#19176)
* win32 getFileSize
* fix stat
* [cpp test-Android]20:FileUtils/2 change title (#19197)
* sync #19200
* sync #19231
* [android lua] improve performance of lua loader (#19234)
* [lua] improve performance of lua loader
* remove cache fix
* Revert "fix spritecache plist parsing issue (#19269)"
This reverts commit f3a85ece4307a7b90816c34489d1ed2c8fd11baf.
* remove win32 project files ref in template.json
* add metal framework lnk ref into cpp template
* test on iOS, and disable part cases
* alBufferData instead of alBufferDataStatic for small audio file on Apple (#19227)
* changes AudioCache to use alBufferData instead of alBufferDataStatic
(also makes test 19 faster to trigger openal bugs faster)
The original problem: CrashIfClientProvidedBogusAudioBufferList
https://github.com/cocos2d/cocos2d-x/issues/18948
is not happening anymore, but there's still a not very frequent issue
that makes OpenAL crash with a call stack like this.
AudioCache::readDataTask > alBufferData > CleanUpDeadBufferList
It happes more frequently when the device is "cold", which means after
half an hour of not using the device (locked).
I could not find the actual source code for iOS OpenAL, so I used the
macOS versions:
https://opensource.apple.com/source/OpenAL/OpenAL-48.7/Source/OpenAL/oalImp.cpp.auto.html
They seem to use CAGuard.h to make sure the dead buffer list
has no threading issues. I'm worried because the CAGuard code I found
has macos and win32 define but no iOS, so I'm not sure. I guess the
iOS version is different and has the guard.
I could not find a place in the code that's unprotected by the locks
except the InitializeBufferMap() which should not be called more than
once from cocos, and there's a workaround in AudioEngine-impl for it.
I reduced the occurence of the CleanUpDeadBufferList crash by moving
the guard in ~AudioCache to cover the alDeleteBuffers call.
* remove hack method "setTimeout" on audio
* AutoTest works on iOS
* support set ios deployment target for root project
* enable all texture2d cases, since Jiang have fixed
* add CCTextureUtils to xcode project file (#176)
* add leak cases for SpriteFrameCache (#177)
* re-add SpriteFrameCache cases
* update template file json
* Update SpriteFrameCacheTest.cpp
* fix compiling error
2019-01-18 15:08:25 +08:00
|
|
|
auto& pipelineDescriptor = _quadCommand.getPipelineDescriptor();
|
|
|
|
auto& vertexShader = pipelineDescriptor.programState->getProgram()->getVertexShader();
|
|
|
|
updateShaders(vertexShader, positionTextureColorAlphaTest_frag);
|
|
|
|
auto alphaValueLocation = pipelineDescriptor.programState->getUniformLocation("u_alpha_value");
|
|
|
|
pipelineDescriptor.programState->setUniform(alphaValueLocation, &alphaFuncValue, sizeof(alphaFuncValue));
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-03 14:47:35 +08:00
|
|
|
_vertexZvalue = vertexz.asInt();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-09 10:34:50 +08:00
|
|
|
void TMXLayer::setupTileSprite(Sprite* sprite, const Vec2& pos, uint32_t gid)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-19 14:57:54 +08:00
|
|
|
sprite->setPosition(getPositionAt(pos));
|
2014-03-14 15:38:43 +08:00
|
|
|
sprite->setPositionZ((float)getVertexZForPos(pos));
|
2014-05-15 01:07:09 +08:00
|
|
|
sprite->setAnchorPoint(Vec2::ZERO);
|
2013-06-15 14:03:30 +08:00
|
|
|
sprite->setOpacity(_opacity);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
//issue 1264, flip can be undone as well
|
2013-09-16 20:38:03 +08:00
|
|
|
sprite->setFlippedX(false);
|
|
|
|
sprite->setFlippedY(false);
|
2012-06-14 05:26:28 +08:00
|
|
|
sprite->setRotation(0.0f);
|
2014-05-15 01:07:09 +08:00
|
|
|
sprite->setAnchorPoint(Vec2(0,0));
|
2012-06-14 05:26:28 +08:00
|
|
|
|
|
|
|
// Rotation in tiled is achieved using 3 flipped states, flipping across the horizontal, vertical, and diagonal axes of the tiles.
|
2013-06-20 14:13:12 +08:00
|
|
|
if (gid & kTMXTileDiagonalFlag)
|
2012-06-14 05:26:28 +08:00
|
|
|
{
|
|
|
|
// put the anchor in the middle for ease of rotation.
|
2014-05-15 01:07:09 +08:00
|
|
|
sprite->setAnchorPoint(Vec2(0.5f,0.5f));
|
2014-08-28 11:41:18 +08:00
|
|
|
sprite->setPosition(getPositionAt(pos).x + sprite->getContentSize().height/2,
|
|
|
|
getPositionAt(pos).y + sprite->getContentSize().width/2 );
|
2012-06-14 05:26:28 +08:00
|
|
|
|
2016-11-09 10:34:50 +08:00
|
|
|
auto flag = gid & (kTMXTileHorizontalFlag | kTMXTileVerticalFlag );
|
2012-06-14 05:26:28 +08:00
|
|
|
|
|
|
|
// handle the 4 diagonally flipped states.
|
2013-06-20 14:13:12 +08:00
|
|
|
if (flag == kTMXTileHorizontalFlag)
|
2012-06-14 05:26:28 +08:00
|
|
|
{
|
|
|
|
sprite->setRotation(90.0f);
|
|
|
|
}
|
2013-06-20 14:13:12 +08:00
|
|
|
else if (flag == kTMXTileVerticalFlag)
|
2012-06-14 05:26:28 +08:00
|
|
|
{
|
|
|
|
sprite->setRotation(270.0f);
|
|
|
|
}
|
2013-06-20 14:13:12 +08:00
|
|
|
else if (flag == (kTMXTileVerticalFlag | kTMXTileHorizontalFlag) )
|
2012-06-14 05:26:28 +08:00
|
|
|
{
|
|
|
|
sprite->setRotation(90.0f);
|
2013-09-16 20:38:03 +08:00
|
|
|
sprite->setFlippedX(true);
|
2012-06-14 05:26:28 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sprite->setRotation(270.0f);
|
2013-09-16 20:38:03 +08:00
|
|
|
sprite->setFlippedX(true);
|
2012-06-14 05:26:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
if (gid & kTMXTileHorizontalFlag)
|
2012-06-14 05:26:28 +08:00
|
|
|
{
|
2013-09-16 20:38:03 +08:00
|
|
|
sprite->setFlippedX(true);
|
2012-06-14 05:26:28 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
if (gid & kTMXTileVerticalFlag)
|
2012-06-14 05:26:28 +08:00
|
|
|
{
|
2013-09-16 20:38:03 +08:00
|
|
|
sprite->setFlippedY(true);
|
2012-06-14 05:26:28 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-17 12:17:56 +08:00
|
|
|
Sprite* TMXLayer::reusedTileWithRect(const Rect& rect)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (! _reusedTile)
|
2012-08-20 14:06:29 +08:00
|
|
|
{
|
2013-11-14 07:55:36 +08:00
|
|
|
_reusedTile = Sprite::createWithTexture(_textureAtlas->getTexture(), rect);
|
2013-06-15 14:03:30 +08:00
|
|
|
_reusedTile->setBatchNode(this);
|
2013-11-14 07:55:36 +08:00
|
|
|
_reusedTile->retain();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-08-30 03:54:24 +08:00
|
|
|
// FIXME: HACK: Needed because if "batch node" is nil,
|
2015-01-08 10:22:45 +08:00
|
|
|
// then the Sprite'squad will be reset
|
2013-12-18 17:47:20 +08:00
|
|
|
_reusedTile->setBatchNode(nullptr);
|
2012-11-14 18:05:15 +08:00
|
|
|
|
2015-01-08 10:22:45 +08:00
|
|
|
// Re-init the sprite
|
2013-06-15 14:03:30 +08:00
|
|
|
_reusedTile->setTextureRect(rect, false, rect.size);
|
2012-11-14 18:05:15 +08:00
|
|
|
|
2015-01-08 10:22:45 +08:00
|
|
|
// restore the batch node
|
2013-06-15 14:03:30 +08:00
|
|
|
_reusedTile->setBatchNode(this);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
return _reusedTile;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// TMXLayer - obtaining tiles/gids
|
2014-05-15 01:07:09 +08:00
|
|
|
Sprite * TMXLayer::getTileAt(const Vec2& pos)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(pos.x < _layerSize.width && pos.y < _layerSize.height && pos.x >=0 && pos.y >=0, "TMXLayer: invalid position");
|
|
|
|
CCASSERT(_tiles && _atlasIndexArray, "TMXLayer: the tiles map has been released");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-19 14:57:54 +08:00
|
|
|
Sprite *tile = nullptr;
|
2013-12-20 09:50:53 +08:00
|
|
|
int gid = this->getTileGIDAt(pos);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// if GID == 0, then no tile is present
|
2012-08-20 14:06:29 +08:00
|
|
|
if (gid)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
int z = (int)(pos.x + pos.y * _layerSize.width);
|
2013-07-19 14:57:54 +08:00
|
|
|
tile = static_cast<Sprite*>(this->getChildByTag(z));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// tile not created yet. create it
|
2012-08-20 14:06:29 +08:00
|
|
|
if (! tile)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-02-08 10:33:30 +08:00
|
|
|
Rect rect = _tileSet->getRectForGID(gid);
|
2012-04-19 14:35:52 +08:00
|
|
|
rect = CC_RECT_PIXELS_TO_POINTS(rect);
|
|
|
|
|
2013-11-14 07:55:36 +08:00
|
|
|
tile = Sprite::createWithTexture(this->getTexture(), rect);
|
2012-04-19 14:35:52 +08:00
|
|
|
tile->setBatchNode(this);
|
2013-07-19 14:57:54 +08:00
|
|
|
tile->setPosition(getPositionAt(pos));
|
2014-03-14 15:38:43 +08:00
|
|
|
tile->setPositionZ((float)getVertexZForPos(pos));
|
2014-05-15 01:07:09 +08:00
|
|
|
tile->setAnchorPoint(Vec2::ZERO);
|
2013-06-15 14:03:30 +08:00
|
|
|
tile->setOpacity(_opacity);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-12 12:07:20 +08:00
|
|
|
ssize_t indexForZ = atlasIndexForExistantZ(z);
|
|
|
|
this->addSpriteWithoutQuad(tile, static_cast<int>(indexForZ), z);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
2012-08-20 14:06:29 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return tile;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
uint32_t TMXLayer::getTileGIDAt(const Vec2& pos, TMXTileFlags* flags/* = nullptr*/)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(pos.x < _layerSize.width && pos.y < _layerSize.height && pos.x >=0 && pos.y >=0, "TMXLayer: invalid position");
|
|
|
|
CCASSERT(_tiles && _atlasIndexArray, "TMXLayer: the tiles map has been released");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2015-10-11 00:05:51 +08:00
|
|
|
ssize_t idx = static_cast<int>(((int) pos.x + (int) pos.y * _layerSize.width));
|
2012-04-19 14:35:52 +08:00
|
|
|
// Bits on the far end of the 32-bit global tile ID are used for tile flags
|
2014-02-08 10:33:30 +08:00
|
|
|
uint32_t tile = _tiles[idx];
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// issue1264, flipped tiles can be changed dynamically
|
|
|
|
if (flags)
|
|
|
|
{
|
2014-02-08 10:33:30 +08:00
|
|
|
*flags = (TMXTileFlags)(tile & kTMXFlipedAll);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-08-20 14:06:29 +08:00
|
|
|
|
2014-02-08 10:33:30 +08:00
|
|
|
return (tile & kTMXFlippedMask);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// TMXLayer - adding helper methods
|
2014-05-15 01:07:09 +08:00
|
|
|
Sprite * TMXLayer::insertTileForGID(uint32_t gid, const Vec2& pos)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-02-08 10:33:30 +08:00
|
|
|
if (gid != 0 && (static_cast<int>((gid & kTMXFlippedMask)) - _tileSet->_firstGid) >= 0)
|
2013-12-23 10:37:46 +08:00
|
|
|
{
|
2014-02-08 10:33:30 +08:00
|
|
|
Rect rect = _tileSet->getRectForGID(gid);
|
2013-12-23 10:37:46 +08:00
|
|
|
rect = CC_RECT_PIXELS_TO_POINTS(rect);
|
|
|
|
|
2015-10-11 00:05:51 +08:00
|
|
|
intptr_t z = (intptr_t)((int) pos.x + (int) pos.y * _layerSize.width);
|
2013-12-23 10:37:46 +08:00
|
|
|
|
|
|
|
Sprite *tile = reusedTileWithRect(rect);
|
|
|
|
|
|
|
|
setupTileSprite(tile, pos, gid);
|
|
|
|
|
|
|
|
// get atlas index
|
|
|
|
ssize_t indexForZ = atlasIndexForNewZ(static_cast<int>(z));
|
|
|
|
|
|
|
|
// Optimization: add the quad without adding a child
|
|
|
|
this->insertQuadFromSprite(tile, indexForZ);
|
|
|
|
|
|
|
|
// insert it into the local atlasindex array
|
|
|
|
ccCArrayInsertValueAtIndex(_atlasIndexArray, (void*)z, indexForZ);
|
|
|
|
|
|
|
|
// update possible children
|
|
|
|
|
2013-12-23 15:40:33 +08:00
|
|
|
for(const auto &child : _children) {
|
2013-12-23 10:37:46 +08:00
|
|
|
Sprite* sp = static_cast<Sprite*>(child);
|
2019-10-25 16:40:30 +08:00
|
|
|
auto ai = sp->getAtlasIndex();
|
2013-12-23 15:40:33 +08:00
|
|
|
if ( ai >= indexForZ )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-23 15:40:33 +08:00
|
|
|
sp->setAtlasIndex(ai+1);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
2013-12-23 10:37:46 +08:00
|
|
|
|
|
|
|
_tiles[z] = gid;
|
|
|
|
return tile;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-12-03 14:47:35 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Sprite * TMXLayer::updateTileForGID(uint32_t gid, const Vec2& pos)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-02-08 10:33:30 +08:00
|
|
|
Rect rect = _tileSet->getRectForGID(gid);
|
2013-07-12 14:30:26 +08:00
|
|
|
rect = Rect(rect.origin.x / _contentScaleFactor, rect.origin.y / _contentScaleFactor, rect.size.width/ _contentScaleFactor, rect.size.height/ _contentScaleFactor);
|
2015-10-11 00:05:51 +08:00
|
|
|
int z = (int)((int) pos.x + (int) pos.y * _layerSize.width);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Sprite *tile = reusedTileWithRect(rect);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
setupTileSprite(tile ,pos ,gid);
|
|
|
|
|
|
|
|
// get atlas index
|
2019-10-25 16:40:30 +08:00
|
|
|
auto indexForZ = atlasIndexForExistantZ(z);
|
|
|
|
tile->setAtlasIndex(static_cast<unsigned int>(indexForZ) );
|
2012-04-19 14:35:52 +08:00
|
|
|
tile->setDirty(true);
|
|
|
|
tile->updateTransform();
|
2013-06-15 14:03:30 +08:00
|
|
|
_tiles[z] = gid;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
return tile;
|
|
|
|
}
|
|
|
|
|
2016-09-14 12:47:36 +08:00
|
|
|
intptr_t TMXLayer::getZForPos(const Vec2& pos) const
|
|
|
|
{
|
|
|
|
intptr_t z = -1;
|
|
|
|
// fix correct render ordering in Hexagonal maps when stagger axis == x
|
|
|
|
if (_staggerAxis == TMXStaggerAxis_X && _layerOrientation == TMXOrientationHex)
|
|
|
|
{
|
|
|
|
if (_staggerIndex == TMXStaggerIndex_Odd)
|
|
|
|
{
|
|
|
|
if (((int)pos.x % 2) == 0)
|
|
|
|
z = pos.x / 2 + pos.y * _layerSize.width;
|
|
|
|
else
|
|
|
|
z = pos.x / 2 + std::ceil(_layerSize.width / 2) + pos.y * _layerSize.width;
|
|
|
|
} else {
|
|
|
|
// TMXStaggerIndex_Even
|
|
|
|
if (((int)pos.x % 2) == 1)
|
|
|
|
z = pos.x / 2 + pos.y * _layerSize.width;
|
|
|
|
else
|
|
|
|
z = pos.x / 2 + std::floor(_layerSize.width / 2) + pos.y * _layerSize.width;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
z = (pos.x + pos.y * _layerSize.width);
|
|
|
|
}
|
|
|
|
|
|
|
|
CCASSERT(z != -1, "Invalid Z");
|
|
|
|
return z;
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// used only when parsing the map. useless after the map was parsed
|
|
|
|
// since lot's of assumptions are no longer true
|
2014-05-15 01:07:09 +08:00
|
|
|
Sprite * TMXLayer::appendTileForGID(uint32_t gid, const Vec2& pos)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-02-08 10:33:30 +08:00
|
|
|
if (gid != 0 && (static_cast<int>((gid & kTMXFlippedMask)) - _tileSet->_firstGid) >= 0)
|
2013-12-23 10:37:46 +08:00
|
|
|
{
|
2014-02-08 10:33:30 +08:00
|
|
|
Rect rect = _tileSet->getRectForGID(gid);
|
2013-12-23 10:37:46 +08:00
|
|
|
rect = CC_RECT_PIXELS_TO_POINTS(rect);
|
2016-09-14 12:47:36 +08:00
|
|
|
|
|
|
|
// Z could be just an integer that gets incremented each time it is called.
|
|
|
|
// but that wouldn't work on layers with empty tiles.
|
|
|
|
// and it is IMPORTANT that Z returns an unique and bigger number than the previous one.
|
|
|
|
// since _atlasIndexArray must be ordered because `bsearch` is used to find the GID for
|
|
|
|
// a given Z. (github issue #16512)
|
|
|
|
intptr_t z = getZForPos(pos);
|
|
|
|
|
2013-12-23 10:37:46 +08:00
|
|
|
Sprite *tile = reusedTileWithRect(rect);
|
|
|
|
|
|
|
|
setupTileSprite(tile ,pos ,gid);
|
|
|
|
|
|
|
|
// optimization:
|
|
|
|
// The difference between appendTileForGID and insertTileforGID is that append is faster, since
|
|
|
|
// it appends the tile at the end of the texture atlas
|
|
|
|
ssize_t indexForZ = _atlasIndexArray->num;
|
|
|
|
|
|
|
|
// don't add it using the "standard" way.
|
|
|
|
insertQuadFromSprite(tile, indexForZ);
|
|
|
|
|
|
|
|
// append should be after addQuadFromSprite since it modifies the quantity values
|
|
|
|
ccCArrayInsertValueAtIndex(_atlasIndexArray, (void*)z, indexForZ);
|
2016-09-14 12:47:36 +08:00
|
|
|
|
|
|
|
// Validation for issue #16512
|
|
|
|
CCASSERT(_atlasIndexArray->num == 1 ||
|
|
|
|
_atlasIndexArray->arr[_atlasIndexArray->num-1] > _atlasIndexArray->arr[_atlasIndexArray->num-2], "Invalid z for _atlasIndexArray");
|
|
|
|
|
2013-12-23 10:37:46 +08:00
|
|
|
return tile;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// TMXLayer - atlasIndex and Z
|
2012-04-19 14:35:52 +08:00
|
|
|
static inline int compareInts(const void * a, const void * b)
|
|
|
|
{
|
2016-09-14 12:47:36 +08:00
|
|
|
const int ia = *(int*)a;
|
|
|
|
const int ib = *(int*)b;
|
|
|
|
return (ia-ib);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-12-03 14:47:35 +08:00
|
|
|
|
2013-12-20 09:50:53 +08:00
|
|
|
ssize_t TMXLayer::atlasIndexForExistantZ(int z)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
int key=z;
|
2013-06-15 14:03:30 +08:00
|
|
|
int *item = (int*)bsearch((void*)&key, (void*)&_atlasIndexArray->arr[0], _atlasIndexArray->num, sizeof(void*), compareInts);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(item, "TMX atlas index not found. Shall not happen");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-12 12:07:20 +08:00
|
|
|
ssize_t index = ((size_t)item - (size_t)_atlasIndexArray->arr) / sizeof(void*);
|
2012-04-19 14:35:52 +08:00
|
|
|
return index;
|
|
|
|
}
|
2013-12-03 14:47:35 +08:00
|
|
|
|
2013-12-12 12:07:20 +08:00
|
|
|
ssize_t TMXLayer::atlasIndexForNewZ(int z)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-08-30 03:54:24 +08:00
|
|
|
// FIXME:: This can be improved with a sort of binary search
|
2013-12-12 12:07:20 +08:00
|
|
|
ssize_t i=0;
|
2013-06-15 14:03:30 +08:00
|
|
|
for (i=0; i< _atlasIndexArray->num ; i++)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-12 12:07:20 +08:00
|
|
|
ssize_t val = (size_t) _atlasIndexArray->arr[i];
|
2012-08-20 14:06:29 +08:00
|
|
|
if (z < val)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
break;
|
2012-08-20 14:06:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// TMXLayer - adding / remove tiles
|
2014-05-15 01:07:09 +08:00
|
|
|
void TMXLayer::setTileGID(uint32_t gid, const Vec2& pos)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-02-08 10:11:17 +08:00
|
|
|
setTileGID(gid, pos, (TMXTileFlags)0);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void TMXLayer::setTileGID(uint32_t gid, const Vec2& pos, TMXTileFlags flags)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(pos.x < _layerSize.width && pos.y < _layerSize.height && pos.x >=0 && pos.y >=0, "TMXLayer: invalid position");
|
|
|
|
CCASSERT(_tiles && _atlasIndexArray, "TMXLayer: the tiles map has been released");
|
2014-03-24 10:12:40 +08:00
|
|
|
CCASSERT(gid == 0 || (int)gid >= _tileSet->_firstGid, "TMXLayer: invalid gid" );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-02-08 10:11:17 +08:00
|
|
|
TMXTileFlags currentFlags;
|
2014-02-08 10:33:30 +08:00
|
|
|
uint32_t currentGID = getTileGIDAt(pos, ¤tFlags);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-08-20 14:06:29 +08:00
|
|
|
if (currentGID != gid || currentFlags != flags)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-02-08 10:33:30 +08:00
|
|
|
uint32_t gidAndFlags = gid | flags;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// setting gid=0 is equal to remove the tile
|
2012-08-20 14:06:29 +08:00
|
|
|
if (gid == 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
removeTileAt(pos);
|
|
|
|
}
|
|
|
|
// empty tile. create a new one
|
2012-08-20 14:06:29 +08:00
|
|
|
else if (currentGID == 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
insertTileForGID(gidAndFlags, pos);
|
|
|
|
}
|
|
|
|
// modifying an existing tile with a non-empty tile
|
|
|
|
else
|
|
|
|
{
|
2015-10-11 00:05:51 +08:00
|
|
|
int z = (int) pos.x + (int) pos.y * _layerSize.width;
|
2013-07-19 14:57:54 +08:00
|
|
|
Sprite *sprite = static_cast<Sprite*>(getChildByTag(z));
|
2012-08-20 14:06:29 +08:00
|
|
|
if (sprite)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-02-08 10:33:30 +08:00
|
|
|
Rect rect = _tileSet->getRectForGID(gid);
|
2012-04-19 14:35:52 +08:00
|
|
|
rect = CC_RECT_PIXELS_TO_POINTS(rect);
|
|
|
|
|
|
|
|
sprite->setTextureRect(rect, false, rect.size);
|
|
|
|
if (flags)
|
|
|
|
{
|
|
|
|
setupTileSprite(sprite, sprite->getPosition(), gidAndFlags);
|
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
_tiles[z] = gidAndFlags;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
updateTileForGID(gidAndFlags, pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-19 14:57:54 +08:00
|
|
|
|
2016-11-16 09:48:37 +08:00
|
|
|
void TMXLayer::addChild(Node* /*child*/, int /*zOrder*/, int /*tag*/)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(0, "addChild: is not supported on TMXLayer. Instead use setTileGID:at:/tileAt:");
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-07-19 14:57:54 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TMXLayer::removeChild(Node* node, bool cleanup)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Sprite *sprite = (Sprite*)node;
|
2012-04-19 14:35:52 +08:00
|
|
|
// allows removing nil objects
|
2012-08-20 14:06:29 +08:00
|
|
|
if (! sprite)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return;
|
2012-08-20 14:06:29 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-05 10:35:10 +08:00
|
|
|
CCASSERT(_children.contains(sprite), "Tile does not belong to TMXLayer");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-12 12:07:20 +08:00
|
|
|
ssize_t atlasIndex = sprite->getAtlasIndex();
|
2013-12-13 16:41:03 +08:00
|
|
|
ssize_t zz = (ssize_t)_atlasIndexArray->arr[atlasIndex];
|
2013-06-15 14:03:30 +08:00
|
|
|
_tiles[zz] = 0;
|
|
|
|
ccCArrayRemoveValueAtIndex(_atlasIndexArray, atlasIndex);
|
2013-06-20 14:13:12 +08:00
|
|
|
SpriteBatchNode::removeChild(sprite, cleanup);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-07-19 14:57:54 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void TMXLayer::removeTileAt(const Vec2& pos)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(pos.x < _layerSize.width && pos.y < _layerSize.height && pos.x >=0 && pos.y >=0, "TMXLayer: invalid position");
|
|
|
|
CCASSERT(_tiles && _atlasIndexArray, "TMXLayer: the tiles map has been released");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-20 09:50:53 +08:00
|
|
|
int gid = getTileGIDAt(pos);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-08-20 14:06:29 +08:00
|
|
|
if (gid)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-20 09:50:53 +08:00
|
|
|
int z = pos.x + pos.y * _layerSize.width;
|
2013-12-12 12:07:20 +08:00
|
|
|
ssize_t atlasIndex = atlasIndexForExistantZ(z);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// remove tile from GID map
|
2013-06-15 14:03:30 +08:00
|
|
|
_tiles[z] = 0;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// remove tile from atlas position array
|
2013-06-15 14:03:30 +08:00
|
|
|
ccCArrayRemoveValueAtIndex(_atlasIndexArray, atlasIndex);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// remove it from sprites and/or texture atlas
|
2013-06-20 14:13:12 +08:00
|
|
|
Sprite *sprite = (Sprite*)getChildByTag(z);
|
2012-08-20 14:06:29 +08:00
|
|
|
if (sprite)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
SpriteBatchNode::removeChild(sprite, true);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_textureAtlas->removeQuadAtIndex(atlasIndex);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// update possible children
|
2013-12-20 05:34:41 +08:00
|
|
|
for(const auto &obj : _children) {
|
2013-11-28 16:02:03 +08:00
|
|
|
Sprite* child = static_cast<Sprite*>(obj);
|
2019-10-25 16:40:30 +08:00
|
|
|
auto ai = child->getAtlasIndex();
|
2013-12-20 05:34:41 +08:00
|
|
|
if ( ai >= atlasIndex )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-20 05:34:41 +08:00
|
|
|
child->setAtlasIndex(ai-1);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-12-20 05:34:41 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//CCTMXLayer - obtaining positions, offset
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 TMXLayer::calculateLayerOffset(const Vec2& pos)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2015-04-20 01:40:52 +08:00
|
|
|
Vec2 ret;
|
2016-07-02 06:51:00 +08:00
|
|
|
switch (_layerOrientation)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2016-07-02 06:51:00 +08:00
|
|
|
case TMXOrientationOrtho:
|
|
|
|
ret.set( pos.x * _mapTileSize.width, -pos.y *_mapTileSize.height);
|
|
|
|
break;
|
|
|
|
case TMXOrientationIso:
|
|
|
|
ret.set((_mapTileSize.width /2) * (pos.x - pos.y),
|
2013-06-15 14:03:30 +08:00
|
|
|
(_mapTileSize.height /2 ) * (-pos.x - pos.y));
|
2016-07-02 06:51:00 +08:00
|
|
|
break;
|
|
|
|
case TMXOrientationHex:
|
|
|
|
{
|
|
|
|
if(_staggerAxis == TMXStaggerAxis_Y)
|
|
|
|
{
|
|
|
|
int diffX = (_staggerIndex == TMXStaggerIndex_Even) ? _mapTileSize.width/2 : 0;
|
|
|
|
ret.set(pos.x * _mapTileSize.width + diffX, -pos.y * (_mapTileSize.height - (_mapTileSize.width - _hexSideLength) / 2));
|
|
|
|
}
|
|
|
|
else if(_staggerAxis == TMXStaggerAxis_X)
|
|
|
|
{
|
|
|
|
int diffY = (_staggerIndex == TMXStaggerIndex_Odd) ? _mapTileSize.height/2 : 0;
|
|
|
|
ret.set(pos.x * (_mapTileSize.width - (_mapTileSize.width - _hexSideLength) / 2), -pos.y * _mapTileSize.height + diffY);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TMXOrientationStaggered:
|
2014-10-11 14:22:31 +08:00
|
|
|
{
|
|
|
|
float diffX = 0;
|
2015-04-07 03:08:30 +08:00
|
|
|
if ((int)std::abs(pos.y) % 2 == 1)
|
2014-10-11 14:22:31 +08:00
|
|
|
{
|
|
|
|
diffX = _mapTileSize.width/2;
|
|
|
|
}
|
2015-04-20 01:40:52 +08:00
|
|
|
ret.set(pos.x * _mapTileSize.width + diffX,
|
2014-10-11 14:22:31 +08:00
|
|
|
(-pos.y) * _mapTileSize.height/2);
|
|
|
|
}
|
2014-10-10 17:52:13 +08:00
|
|
|
break;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2013-07-19 14:57:54 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 TMXLayer::getPositionAt(const Vec2& pos)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2015-04-20 01:40:52 +08:00
|
|
|
Vec2 ret;
|
2013-06-15 14:03:30 +08:00
|
|
|
switch (_layerOrientation)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
case TMXOrientationOrtho:
|
2013-07-19 14:57:54 +08:00
|
|
|
ret = getPositionForOrthoAt(pos);
|
2012-04-19 14:35:52 +08:00
|
|
|
break;
|
2013-06-20 14:13:12 +08:00
|
|
|
case TMXOrientationIso:
|
2013-07-19 14:57:54 +08:00
|
|
|
ret = getPositionForIsoAt(pos);
|
2012-04-19 14:35:52 +08:00
|
|
|
break;
|
2013-06-20 14:13:12 +08:00
|
|
|
case TMXOrientationHex:
|
2013-07-19 14:57:54 +08:00
|
|
|
ret = getPositionForHexAt(pos);
|
2012-04-19 14:35:52 +08:00
|
|
|
break;
|
2014-10-10 17:52:13 +08:00
|
|
|
case TMXOrientationStaggered:
|
|
|
|
ret = getPositionForStaggeredAt(pos);
|
|
|
|
break;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
ret = CC_POINT_PIXELS_TO_POINTS( ret );
|
|
|
|
return ret;
|
|
|
|
}
|
2013-07-19 14:57:54 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 TMXLayer::getPositionForOrthoAt(const Vec2& pos)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
return Vec2(pos.x * _mapTileSize.width,
|
2013-06-15 14:03:30 +08:00
|
|
|
(_layerSize.height - pos.y - 1) * _mapTileSize.height);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-07-19 14:57:54 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 TMXLayer::getPositionForIsoAt(const Vec2& pos)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
return Vec2(_mapTileSize.width /2 * (_layerSize.width + pos.x - pos.y - 1),
|
2013-06-15 14:03:30 +08:00
|
|
|
_mapTileSize.height /2 * ((_layerSize.height * 2 - pos.x - pos.y) - 2));
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-07-19 14:57:54 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 TMXLayer::getPositionForHexAt(const Vec2& pos)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2016-07-02 06:51:00 +08:00
|
|
|
Vec2 xy;
|
|
|
|
Vec2 offset = _tileSet->_tileOffset;
|
|
|
|
|
|
|
|
int odd_even = (_staggerIndex == TMXStaggerIndex_Odd) ? 1 : -1;
|
|
|
|
switch (_staggerAxis)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2016-07-02 06:51:00 +08:00
|
|
|
case TMXStaggerAxis_Y:
|
|
|
|
{
|
|
|
|
float diffX = 0;
|
|
|
|
if ((int)pos.y % 2 == 1)
|
|
|
|
{
|
|
|
|
diffX = _mapTileSize.width/2 * odd_even;
|
|
|
|
}
|
|
|
|
xy = Vec2(pos.x * _mapTileSize.width+diffX+offset.x,
|
|
|
|
(_layerSize.height - pos.y - 1) * (_mapTileSize.height-(_mapTileSize.height-_hexSideLength)/2)-offset.y);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case TMXStaggerAxis_X:
|
|
|
|
{
|
|
|
|
float diffY = 0;
|
|
|
|
if ((int)pos.x % 2 == 1)
|
|
|
|
{
|
|
|
|
diffY = _mapTileSize.height/2 * -odd_even;
|
|
|
|
}
|
|
|
|
|
|
|
|
xy = Vec2(pos.x * (_mapTileSize.width-(_mapTileSize.width-_hexSideLength)/2)+offset.x,
|
|
|
|
(_layerSize.height - pos.y - 1) * _mapTileSize.height + diffY-offset.y);
|
|
|
|
break;
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
return xy;
|
|
|
|
}
|
2013-07-19 14:57:54 +08:00
|
|
|
|
2014-10-10 17:52:13 +08:00
|
|
|
Vec2 TMXLayer::getPositionForStaggeredAt(const Vec2 &pos)
|
|
|
|
{
|
2014-10-11 14:22:31 +08:00
|
|
|
float diffX = 0;
|
|
|
|
if ((int)pos.y % 2 == 1)
|
|
|
|
{
|
|
|
|
diffX = _mapTileSize.width/2;
|
|
|
|
}
|
|
|
|
return Vec2(pos.x * _mapTileSize.width + diffX,
|
|
|
|
(_layerSize.height - pos.y - 1) * _mapTileSize.height/2);
|
2014-10-10 17:52:13 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
int TMXLayer::getVertexZForPos(const Vec2& pos)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
int ret = 0;
|
2013-12-20 09:50:53 +08:00
|
|
|
int maxVal = 0;
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_useAutomaticVertexZ)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
switch (_layerOrientation)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
case TMXOrientationIso:
|
2013-12-20 09:50:53 +08:00
|
|
|
maxVal = static_cast<int>(_layerSize.width + _layerSize.height);
|
|
|
|
ret = static_cast<int>(-(maxVal - (pos.x + pos.y)));
|
2012-04-19 14:35:52 +08:00
|
|
|
break;
|
2013-06-20 14:13:12 +08:00
|
|
|
case TMXOrientationOrtho:
|
2013-12-20 09:50:53 +08:00
|
|
|
ret = static_cast<int>(-(_layerSize.height-pos.y));
|
2012-04-19 14:35:52 +08:00
|
|
|
break;
|
2014-10-10 17:52:13 +08:00
|
|
|
case TMXOrientationStaggered:
|
|
|
|
ret = static_cast<int>(-(_layerSize.height-pos.y));
|
|
|
|
break;
|
2013-06-20 14:13:12 +08:00
|
|
|
case TMXOrientationHex:
|
2016-07-02 06:51:00 +08:00
|
|
|
ret = static_cast<int>(-(_layerSize.height-pos.y));
|
2012-04-19 14:35:52 +08:00
|
|
|
break;
|
|
|
|
default:
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(0, "TMX invalid value");
|
2012-04-19 14:35:52 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
ret = _vertexZvalue;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-08-20 14:06:29 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-12-13 06:30:22 +08:00
|
|
|
std::string TMXLayer::getDescription() const
|
|
|
|
{
|
2013-12-13 16:51:57 +08:00
|
|
|
return StringUtils::format("<TMXLayer | tag = %d, size = %d,%d>", _tag, (int)_mapTileSize.width, (int)_mapTileSize.height);
|
2013-12-13 06:30:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_END
|