Atlas.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated January 1, 2020. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2020, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. * or otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. #include "base/ccMacros.h"
  30. #ifdef SPINE_UE4
  31. #include "SpinePluginPrivatePCH.h"
  32. #endif
  33. #include <spine/Atlas.h>
  34. #include <spine/TextureLoader.h>
  35. #include <spine/ContainerUtil.h>
  36. #include <ctype.h>
  37. using namespace spine;
  38. Atlas::Atlas(const String &path, TextureLoader *textureLoader, bool createTexture) : _textureLoader(textureLoader) {
  39. int dirLength;
  40. char *dir;
  41. int length;
  42. const char *data;
  43. /* Get directory from atlas path. */
  44. const char *lastForwardSlash = strrchr(path.buffer(), '/');
  45. const char *lastBackwardSlash = strrchr(path.buffer(), '\\');
  46. const char *lastSlash = lastForwardSlash > lastBackwardSlash ? lastForwardSlash : lastBackwardSlash;
  47. if (lastSlash == path) lastSlash++; /* Never drop starting slash. */
  48. dirLength = (int) (lastSlash ? lastSlash - path.buffer() : 0);
  49. dir = SpineExtension::calloc<char>(dirLength + 1, __FILE__, __LINE__);
  50. memcpy(dir, path.buffer(), dirLength);
  51. dir[dirLength] = '\0';
  52. data = SpineExtension::readFile(path, &length);
  53. if (data) {
  54. load(data, length, dir, createTexture);
  55. }
  56. SpineExtension::free(data, __FILE__, __LINE__);
  57. SpineExtension::free(dir, __FILE__, __LINE__);
  58. }
  59. Atlas::Atlas(const char *data, int length, const char *dir, TextureLoader *textureLoader, bool createTexture) : _textureLoader(
  60. textureLoader) {
  61. load(data, length, dir, createTexture);
  62. }
  63. Atlas::~Atlas() {
  64. if (_textureLoader) {
  65. for (size_t i = 0, n = _pages.size(); i < n; ++i) {
  66. _textureLoader->unload(_pages[i]->getRendererObject());
  67. }
  68. }
  69. ContainerUtil::cleanUpVectorOfPointers(_pages);
  70. ContainerUtil::cleanUpVectorOfPointers(_regions);
  71. }
  72. void Atlas::flipV() {
  73. for (size_t i = 0, n = _regions.size(); i < n; ++i) {
  74. AtlasRegion *regionP = _regions[i];
  75. AtlasRegion &region = *regionP;
  76. region.v = 1 - region.v;
  77. region.v2 = 1 - region.v2;
  78. }
  79. }
  80. AtlasRegion *Atlas::findRegion(const String &name) {
  81. for (size_t i = 0, n = _regions.size(); i < n; ++i)
  82. if (_regions[i]->name == name) return _regions[i];
  83. return NULL;
  84. }
  85. Vector<AtlasPage*> &Atlas::getPages() {
  86. return _pages;
  87. }
  88. Vector<AtlasRegion*> &Atlas::getRegions() {
  89. return _regions;
  90. }
  91. void Atlas::load(const char *begin, int length, const char *dir, bool createTexture) {
  92. static const char *formatNames[] = {"", "Alpha", "Intensity", "LuminanceAlpha", "RGB565", "RGBA4444", "RGB888", "RGBA8888"};
  93. static const char *textureFilterNames[] = {"", "Nearest", "Linear", "MipMap", "MipMapNearestNearest", "MipMapLinearNearest",
  94. "MipMapNearestLinear", "MipMapLinearLinear"};
  95. int count;
  96. const char *end = begin + length;
  97. int dirLength = (int) strlen(dir);
  98. int needsSlash = dirLength > 0 && dir[dirLength - 1] != '/' && dir[dirLength - 1] != '\\';
  99. AtlasPage *page = NULL;
  100. Str str;
  101. Str tuple[4];
  102. while (readLine(&begin, end, &str)) {
  103. if (str.end - str.begin == 0) {
  104. page = 0;
  105. } else if (!page) {
  106. char *name = mallocString(&str);
  107. char *path = SpineExtension::calloc<char>(dirLength + needsSlash + strlen(name) + 1, __FILE__, __LINE__);
  108. memcpy(path, dir, dirLength);
  109. if (needsSlash) path[dirLength] = '/';
  110. strcpy(path + dirLength + needsSlash, name);
  111. page = new(__FILE__, __LINE__) AtlasPage(String(name, true));
  112. int tupleVal = readTuple(&begin, end, tuple);
  113. assert(tupleVal == 2);
  114. /* size is only optional for an atlas packed with an old TexturePacker. */
  115. page->width = toInt(tuple);
  116. page->height = toInt(tuple + 1);
  117. readTuple(&begin, end, tuple);
  118. int maxTextureSize = 2048;
  119. if (page->width > maxTextureSize || page->height > maxTextureSize)
  120. {
  121. CCLOG("cocos2d: WARNING: Image (%u x %u) is bigger than the supported %u x %u", page->width, page->height, maxTextureSize, maxTextureSize);
  122. CCASSERT(0, "Image is bigger than the supported!");
  123. }
  124. page->format = (Format) indexOf(formatNames, 8, tuple);
  125. readTuple(&begin, end, tuple);
  126. page->minFilter = (TextureFilter) indexOf(textureFilterNames, 8, tuple);
  127. page->magFilter = (TextureFilter) indexOf(textureFilterNames, 8, tuple + 1);
  128. readValue(&begin, end, &str);
  129. page->uWrap = TextureWrap_ClampToEdge;
  130. page->vWrap = TextureWrap_ClampToEdge;
  131. if (!equals(&str, "none")) {
  132. if (str.end - str.begin == 1) {
  133. if (*str.begin == 'x') {
  134. page->uWrap = TextureWrap_Repeat;
  135. } else if (*str.begin == 'y') {
  136. page->vWrap = TextureWrap_Repeat;
  137. }
  138. } else if (equals(&str, "xy")) {
  139. page->uWrap = TextureWrap_Repeat;
  140. page->vWrap = TextureWrap_Repeat;
  141. }
  142. }
  143. if (createTexture) {
  144. if (_textureLoader) _textureLoader->load(*page, String(path));
  145. SpineExtension::free(path, __FILE__, __LINE__);
  146. } else
  147. page->texturePath = String(path, true);
  148. _pages.add(page);
  149. } else {
  150. AtlasRegion *region = new(__FILE__, __LINE__) AtlasRegion();
  151. region->page = page;
  152. region->name = String(mallocString(&str), true);
  153. readValue(&begin, end, &str);
  154. if (equals(&str, "true")) region->degrees = 90;
  155. else if (equals(&str, "false")) region->degrees = 0;
  156. else region->degrees = toInt(&str);
  157. region->rotate = region->degrees == 90;
  158. readTuple(&begin, end, tuple);
  159. region->x = toInt(tuple);
  160. region->y = toInt(tuple + 1);
  161. readTuple(&begin, end, tuple);
  162. region->width = toInt(tuple);
  163. region->height = toInt(tuple + 1);
  164. region->u = region->x / (float) page->width;
  165. region->v = region->y / (float) page->height;
  166. if (region->rotate) {
  167. region->u2 = (region->x + region->height) / (float) page->width;
  168. region->v2 = (region->y + region->width) / (float) page->height;
  169. } else {
  170. region->u2 = (region->x + region->width) / (float) page->width;
  171. region->v2 = (region->y + region->height) / (float) page->height;
  172. }
  173. count = readTuple(&begin, end, tuple);
  174. assert(count);
  175. if (count == 4) {
  176. /* split is optional */
  177. region->splits.setSize(4, 0);
  178. region->splits[0] = toInt(tuple);
  179. region->splits[1] = toInt(tuple + 1);
  180. region->splits[2] = toInt(tuple + 2);
  181. region->splits[3] = toInt(tuple + 3);
  182. count = readTuple(&begin, end, tuple);
  183. assert(count);
  184. if (count == 4) {
  185. /* pad is optional, but only present with splits */
  186. region->pads.setSize(4, 0);
  187. region->pads[0] = toInt(tuple);
  188. region->pads[1] = toInt(tuple + 1);
  189. region->pads[2] = toInt(tuple + 2);
  190. region->pads[3] = toInt(tuple + 3);
  191. readTuple(&begin, end, tuple);
  192. }
  193. }
  194. region->originalWidth = toInt(tuple);
  195. region->originalHeight = toInt(tuple + 1);
  196. readTuple(&begin, end, tuple);
  197. region->offsetX = (float)toInt(tuple);
  198. region->offsetY = (float)toInt(tuple + 1);
  199. readValue(&begin, end, &str);
  200. region->index = toInt(&str);
  201. _regions.add(region);
  202. }
  203. }
  204. }
  205. void Atlas::trim(Str *str) {
  206. while (isspace((unsigned char) *str->begin) && str->begin < str->end)
  207. (str->begin)++;
  208. if (str->begin == str->end) return;
  209. str->end--;
  210. while (((unsigned char)*str->end == '\r') && str->end >= str->begin)
  211. str->end--;
  212. str->end++;
  213. }
  214. int Atlas::readLine(const char **begin, const char *end, Str *str) {
  215. if (*begin == end) return 0;
  216. str->begin = *begin;
  217. /* Find next delimiter. */
  218. while (*begin != end && **begin != '\n')
  219. (*begin)++;
  220. str->end = *begin;
  221. trim(str);
  222. if (*begin != end) (*begin)++;
  223. return 1;
  224. }
  225. int Atlas::beginPast(Str *str, char c) {
  226. const char *begin = str->begin;
  227. while (true) {
  228. char lastSkippedChar = *begin;
  229. if (begin == str->end) return 0;
  230. begin++;
  231. if (lastSkippedChar == c) break;
  232. }
  233. str->begin = begin;
  234. return 1;
  235. }
  236. int Atlas::readValue(const char **begin, const char *end, Str *str) {
  237. readLine(begin, end, str);
  238. if (!beginPast(str, ':')) return 0;
  239. trim(str);
  240. return 1;
  241. }
  242. int Atlas::readTuple(const char **begin, const char *end, Str tuple[]) {
  243. int i;
  244. Str str = {NULL, NULL};
  245. readLine(begin, end, &str);
  246. if (!beginPast(&str, ':')) return 0;
  247. for (i = 0; i < 3; ++i) {
  248. tuple[i].begin = str.begin;
  249. if (!beginPast(&str, ',')) break;
  250. tuple[i].end = str.begin - 2;
  251. trim(&tuple[i]);
  252. }
  253. tuple[i].begin = str.begin;
  254. tuple[i].end = str.end;
  255. trim(&tuple[i]);
  256. return i + 1;
  257. }
  258. char *Atlas::mallocString(Str *str) {
  259. int length = (int) (str->end - str->begin);
  260. char *string = SpineExtension::calloc<char>(length + 1, __FILE__, __LINE__);
  261. memcpy(string, str->begin, length);
  262. string[length] = '\0';
  263. return string;
  264. }
  265. int Atlas::indexOf(const char **array, int count, Str *str) {
  266. int length = (int) (str->end - str->begin);
  267. int i;
  268. for (i = count - 1; i >= 0; i--)
  269. if (strncmp(array[i], str->begin, length) == 0) return i;
  270. return 0;
  271. }
  272. int Atlas::equals(Str *str, const char *other) {
  273. return strncmp(other, str->begin, str->end - str->begin) == 0;
  274. }
  275. int Atlas::toInt(Str *str) {
  276. return (int) strtol(str->begin, (char **) &str->end, 10);
  277. }