Skin.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /******************************************************************************
  2. * Spine Runtimes Software License
  3. * Version 2
  4. *
  5. * Copyright (c) 2013, Esoteric Software
  6. * All rights reserved.
  7. *
  8. * You are granted a perpetual, non-exclusive, non-sublicensable and
  9. * non-transferable license to install, execute and perform the Spine Runtimes
  10. * Software (the "Software") solely for internal use. Without the written
  11. * permission of Esoteric Software, you may not (a) modify, translate, adapt or
  12. * otherwise create derivative works, improvements of the Software or develop
  13. * new applications using the Software or (b) remove, delete, alter or obscure
  14. * any trademarks or any copyright, trademark, patent or other intellectual
  15. * property or proprietary rights notices on or in the Software, including
  16. * any copy thereof. Redistributions in binary or source form must include
  17. * this license and terms. THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE
  18. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  19. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  20. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTARE BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. *****************************************************************************/
  28. #include <spine/Skin.h>
  29. #include <spine/extension.h>
  30. typedef struct _Entry _Entry;
  31. struct _Entry {
  32. int slotIndex;
  33. const char* name;
  34. spAttachment* attachment;
  35. _Entry* next;
  36. };
  37. _Entry* _Entry_create (int slotIndex, const char* name, spAttachment* attachment) {
  38. _Entry* self = NEW(_Entry);
  39. self->slotIndex = slotIndex;
  40. MALLOC_STR(self->name, name);
  41. self->attachment = attachment;
  42. return self;
  43. }
  44. void _Entry_dispose (_Entry* self) {
  45. spAttachment_dispose(self->attachment);
  46. FREE(self->name);
  47. FREE(self);
  48. }
  49. /**/
  50. typedef struct {
  51. spSkin super;
  52. _Entry* entries;
  53. } _spSkin;
  54. spSkin* spSkin_create (const char* name) {
  55. spSkin* self = SUPER(NEW(_spSkin));
  56. MALLOC_STR(self->name, name);
  57. return self;
  58. }
  59. void spSkin_dispose (spSkin* self) {
  60. _Entry* entry = SUB_CAST(_spSkin, self)->entries;
  61. while (entry) {
  62. _Entry* nextEntry = entry->next;
  63. _Entry_dispose(entry);
  64. entry = nextEntry;
  65. }
  66. FREE(self->name);
  67. FREE(self);
  68. }
  69. void spSkin_addAttachment (spSkin* self, int slotIndex, const char* name, spAttachment* attachment) {
  70. _Entry* newEntry = _Entry_create(slotIndex, name, attachment);
  71. newEntry->next = SUB_CAST(_spSkin, self)->entries;
  72. SUB_CAST(_spSkin, self)->entries = newEntry;
  73. }
  74. spAttachment* spSkin_getAttachment (const spSkin* self, int slotIndex, const char* name) {
  75. const _Entry* entry = SUB_CAST(_spSkin, self)->entries;
  76. while (entry) {
  77. if (entry->slotIndex == slotIndex && strcmp(entry->name, name) == 0) return entry->attachment;
  78. entry = entry->next;
  79. }
  80. return 0;
  81. }
  82. const char* spSkin_getAttachmentName (const spSkin* self, int slotIndex, int attachmentIndex) {
  83. const _Entry* entry = SUB_CAST(_spSkin, self)->entries;
  84. int i = 0;
  85. while (entry) {
  86. if (entry->slotIndex == slotIndex) {
  87. if (i == attachmentIndex) return entry->name;
  88. i++;
  89. }
  90. entry = entry->next;
  91. }
  92. return 0;
  93. }
  94. void spSkin_attachAll (const spSkin* self, spSkeleton* skeleton, const spSkin* oldSkin) {
  95. const _Entry *entry = SUB_CAST(_spSkin, oldSkin)->entries;
  96. while (entry) {
  97. spSlot *slot = skeleton->slots[entry->slotIndex];
  98. if (slot->attachment == entry->attachment) {
  99. spAttachment *attachment = spSkin_getAttachment(self, entry->slotIndex, entry->name);
  100. if (attachment) spSlot_setAttachment(slot, attachment);
  101. }
  102. entry = entry->next;
  103. }
  104. }