Cocos2dAttachmentLoader.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /******************************************************************************
  2. * Spine Runtimes Software License v2.5
  3. *
  4. * Copyright (c) 2013-2016, Esoteric Software
  5. * All rights reserved.
  6. *
  7. * You are granted a perpetual, non-exclusive, non-sublicensable, and
  8. * non-transferable license to use, install, execute, and perform the Spine
  9. * Runtimes software and derivative works solely for personal or internal
  10. * use. Without the written permission of Esoteric Software (see Section 2 of
  11. * the Spine Software License Agreement), you may not (a) modify, translate,
  12. * adapt, or develop new applications using the Spine Runtimes or otherwise
  13. * create derivative works or improvements of the Spine Runtimes or (b) remove,
  14. * delete, alter, or obscure any trademarks or any copyright, trademark, patent,
  15. * or other intellectual property or proprietary rights notices on or in the
  16. * Software, including any copy thereof. Redistributions in binary or source
  17. * form must include this license and terms.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
  20. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  22. * EVENT SHALL ESOTERIC SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS INTERRUPTION, OR LOSS OF
  25. * USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  26. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *****************************************************************************/
  30. #include <spine/Cocos2dAttachmentLoader.h>
  31. #include <spine/extension.h>
  32. #include <spine/AttachmentVertices.h>
  33. USING_NS_CC;
  34. using namespace spine;
  35. static unsigned short quadTriangles[6] = {0, 1, 2, 2, 3, 0};
  36. spAttachment* _Cocos2dAttachmentLoader_createAttachment (spAttachmentLoader* loader, spSkin* skin, spAttachmentType type,
  37. const char* name, const char* path) {
  38. Cocos2dAttachmentLoader* self = SUB_CAST(Cocos2dAttachmentLoader, loader);
  39. return spAttachmentLoader_createAttachment(SUPER(self->atlasAttachmentLoader), skin, type, name, path);
  40. }
  41. void _Cocos2dAttachmentLoader_configureAttachment (spAttachmentLoader* loader, spAttachment* attachment) {
  42. attachment->attachmentLoader = loader;
  43. switch (attachment->type) {
  44. case SP_ATTACHMENT_REGION: {
  45. spRegionAttachment* regionAttachment = SUB_CAST(spRegionAttachment, attachment);
  46. spAtlasRegion* region = (spAtlasRegion*)regionAttachment->rendererObject;
  47. AttachmentVertices* attachmentVertices = new AttachmentVertices((Texture2D*)region->page->rendererObject, 4, quadTriangles, 6);
  48. V3F_C4B_T2F* vertices = attachmentVertices->_triangles->verts;
  49. for (int i = 0, ii = 0; i < 4; ++i, ii += 2) {
  50. vertices[i].texCoords.u = regionAttachment->uvs[ii];
  51. vertices[i].texCoords.v = regionAttachment->uvs[ii + 1];
  52. }
  53. regionAttachment->rendererObject = attachmentVertices;
  54. break;
  55. }
  56. case SP_ATTACHMENT_MESH: {
  57. spMeshAttachment* meshAttachment = SUB_CAST(spMeshAttachment, attachment);
  58. spAtlasRegion* region = (spAtlasRegion*)meshAttachment->rendererObject;
  59. AttachmentVertices* attachmentVertices = new AttachmentVertices((Texture2D*)region->page->rendererObject,
  60. meshAttachment->super.worldVerticesLength >> 1, meshAttachment->triangles, meshAttachment->trianglesCount);
  61. V3F_C4B_T2F* vertices = attachmentVertices->_triangles->verts;
  62. for (int i = 0, ii = 0, nn = meshAttachment->super.worldVerticesLength; ii < nn; ++i, ii += 2) {
  63. vertices[i].texCoords.u = meshAttachment->uvs[ii];
  64. vertices[i].texCoords.v = meshAttachment->uvs[ii + 1];
  65. }
  66. meshAttachment->rendererObject = attachmentVertices;
  67. break;
  68. }
  69. default: ;
  70. }
  71. }
  72. void _Cocos2dAttachmentLoader_disposeAttachment (spAttachmentLoader* loader, spAttachment* attachment) {
  73. switch (attachment->type) {
  74. case SP_ATTACHMENT_REGION: {
  75. spRegionAttachment* regionAttachment = SUB_CAST(spRegionAttachment, attachment);
  76. delete (AttachmentVertices*)regionAttachment->rendererObject;
  77. break;
  78. }
  79. case SP_ATTACHMENT_MESH: {
  80. spMeshAttachment* meshAttachment = SUB_CAST(spMeshAttachment, attachment);
  81. delete (AttachmentVertices*)meshAttachment->rendererObject;
  82. break;
  83. }
  84. default: ;
  85. }
  86. }
  87. void _Cocos2dAttachmentLoader_dispose (spAttachmentLoader* loader) {
  88. Cocos2dAttachmentLoader* self = SUB_CAST(Cocos2dAttachmentLoader, loader);
  89. spAttachmentLoader_dispose(SUPER_CAST(spAttachmentLoader, self->atlasAttachmentLoader));
  90. _spAttachmentLoader_deinit(loader);
  91. }
  92. Cocos2dAttachmentLoader* Cocos2dAttachmentLoader_create (spAtlas* atlas) {
  93. Cocos2dAttachmentLoader* self = NEW(Cocos2dAttachmentLoader);
  94. _spAttachmentLoader_init(SUPER(self), _Cocos2dAttachmentLoader_dispose, _Cocos2dAttachmentLoader_createAttachment,
  95. _Cocos2dAttachmentLoader_configureAttachment, _Cocos2dAttachmentLoader_disposeAttachment);
  96. self->atlasAttachmentLoader = spAtlasAttachmentLoader_create(atlas);
  97. return self;
  98. }