Atlas.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. #ifdef SPINE_UE4
  30. #include "SpinePluginPrivatePCH.h"
  31. #endif
  32. #include <spine/Atlas.h>
  33. #include <spine/TextureLoader.h>
  34. #include <spine/ContainerUtil.h>
  35. #include <ctype.h>
  36. using namespace spine;
  37. Atlas::Atlas(const String &path, TextureLoader *textureLoader, bool createTexture) : _textureLoader(textureLoader) {
  38. int dirLength;
  39. char *dir;
  40. int length;
  41. const char *data;
  42. /* Get directory from atlas path. */
  43. const char *lastForwardSlash = strrchr(path.buffer(), '/');
  44. const char *lastBackwardSlash = strrchr(path.buffer(), '\\');
  45. const char *lastSlash = lastForwardSlash > lastBackwardSlash ? lastForwardSlash : lastBackwardSlash;
  46. if (lastSlash == path) lastSlash++; /* Never drop starting slash. */
  47. dirLength = (int) (lastSlash ? lastSlash - path.buffer() : 0);
  48. dir = SpineExtension::calloc<char>(dirLength + 1, __FILE__, __LINE__);
  49. memcpy(dir, path.buffer(), dirLength);
  50. dir[dirLength] = '\0';
  51. data = SpineExtension::readFile(path, &length);
  52. if (data) {
  53. load(data, length, dir, createTexture);
  54. }
  55. SpineExtension::free(data, __FILE__, __LINE__);
  56. SpineExtension::free(dir, __FILE__, __LINE__);
  57. }
  58. Atlas::Atlas(const char *data, int length, const char *dir, TextureLoader *textureLoader, bool createTexture) : _textureLoader(
  59. textureLoader) {
  60. load(data, length, dir, createTexture);
  61. }
  62. Atlas::~Atlas() {
  63. if (_textureLoader) {
  64. for (size_t i = 0, n = _pages.size(); i < n; ++i) {
  65. _textureLoader->unload(_pages[i]->getRendererObject());
  66. }
  67. }
  68. ContainerUtil::cleanUpVectorOfPointers(_pages);
  69. ContainerUtil::cleanUpVectorOfPointers(_regions);
  70. }
  71. void Atlas::flipV() {
  72. for (size_t i = 0, n = _regions.size(); i < n; ++i) {
  73. AtlasRegion *regionP = _regions[i];
  74. AtlasRegion &region = *regionP;
  75. region.v = 1 - region.v;
  76. region.v2 = 1 - region.v2;
  77. }
  78. }
  79. AtlasRegion *Atlas::findRegion(const String &name) {
  80. for (size_t i = 0, n = _regions.size(); i < n; ++i)
  81. if (_regions[i]->name == name) return _regions[i];
  82. return NULL;
  83. }
  84. Vector<AtlasPage*> &Atlas::getPages() {
  85. return _pages;
  86. }
  87. void Atlas::load(const char *begin, int length, const char *dir, bool createTexture) {
  88. static const char *formatNames[] = {"", "Alpha", "Intensity", "LuminanceAlpha", "RGB565", "RGBA4444", "RGB888", "RGBA8888"};
  89. static const char *textureFilterNames[] = {"", "Nearest", "Linear", "MipMap", "MipMapNearestNearest", "MipMapLinearNearest",
  90. "MipMapNearestLinear", "MipMapLinearLinear"};
  91. int count;
  92. const char *end = begin + length;
  93. int dirLength = (int) strlen(dir);
  94. int needsSlash = dirLength > 0 && dir[dirLength - 1] != '/' && dir[dirLength - 1] != '\\';
  95. AtlasPage *page = NULL;
  96. Str str;
  97. Str tuple[4];
  98. while (readLine(&begin, end, &str)) {
  99. if (str.end - str.begin == 0) {
  100. page = 0;
  101. } else if (!page) {
  102. char *name = mallocString(&str);
  103. char *path = SpineExtension::calloc<char>(dirLength + needsSlash + strlen(name) + 1, __FILE__, __LINE__);
  104. memcpy(path, dir, dirLength);
  105. if (needsSlash) path[dirLength] = '/';
  106. strcpy(path + dirLength + needsSlash, name);
  107. page = new(__FILE__, __LINE__) AtlasPage(String(name, true));
  108. int tupleVal = readTuple(&begin, end, tuple);
  109. assert(tupleVal == 2);
  110. /* size is only optional for an atlas packed with an old TexturePacker. */
  111. page->width = toInt(tuple);
  112. page->height = toInt(tuple + 1);
  113. readTuple(&begin, end, tuple);
  114. page->format = (Format) indexOf(formatNames, 8, tuple);
  115. readTuple(&begin, end, tuple);
  116. page->minFilter = (TextureFilter) indexOf(textureFilterNames, 8, tuple);
  117. page->magFilter = (TextureFilter) indexOf(textureFilterNames, 8, tuple + 1);
  118. readValue(&begin, end, &str);
  119. page->uWrap = TextureWrap_ClampToEdge;
  120. page->vWrap = TextureWrap_ClampToEdge;
  121. if (!equals(&str, "none")) {
  122. if (str.end - str.begin == 1) {
  123. if (*str.begin == 'x') {
  124. page->uWrap = TextureWrap_Repeat;
  125. } else if (*str.begin == 'y') {
  126. page->vWrap = TextureWrap_Repeat;
  127. }
  128. } else if (equals(&str, "xy")) {
  129. page->uWrap = TextureWrap_Repeat;
  130. page->vWrap = TextureWrap_Repeat;
  131. }
  132. }
  133. if (createTexture) {
  134. if (_textureLoader) _textureLoader->load(*page, String(path));
  135. SpineExtension::free(path, __FILE__, __LINE__);
  136. } else
  137. page->texturePath = String(path, true);
  138. _pages.add(page);
  139. } else {
  140. AtlasRegion *region = new(__FILE__, __LINE__) AtlasRegion();
  141. region->page = page;
  142. region->name = String(mallocString(&str), true);
  143. readValue(&begin, end, &str);
  144. if (equals(&str, "true")) region->degrees = 90;
  145. else if (equals(&str, "false")) region->degrees = 0;
  146. else region->degrees = toInt(&str);
  147. region->rotate = region->degrees == 90;
  148. readTuple(&begin, end, tuple);
  149. region->x = toInt(tuple);
  150. region->y = toInt(tuple + 1);
  151. readTuple(&begin, end, tuple);
  152. region->width = toInt(tuple);
  153. region->height = toInt(tuple + 1);
  154. region->u = region->x / (float) page->width;
  155. region->v = region->y / (float) page->height;
  156. if (region->rotate) {
  157. region->u2 = (region->x + region->height) / (float) page->width;
  158. region->v2 = (region->y + region->width) / (float) page->height;
  159. } else {
  160. region->u2 = (region->x + region->width) / (float) page->width;
  161. region->v2 = (region->y + region->height) / (float) page->height;
  162. }
  163. count = readTuple(&begin, end, tuple);
  164. assert(count);
  165. if (count == 4) {
  166. /* split is optional */
  167. region->splits.setSize(4, 0);
  168. region->splits[0] = toInt(tuple);
  169. region->splits[1] = toInt(tuple + 1);
  170. region->splits[2] = toInt(tuple + 2);
  171. region->splits[3] = toInt(tuple + 3);
  172. count = readTuple(&begin, end, tuple);
  173. assert(count);
  174. if (count == 4) {
  175. /* pad is optional, but only present with splits */
  176. region->pads.setSize(4, 0);
  177. region->pads[0] = toInt(tuple);
  178. region->pads[1] = toInt(tuple + 1);
  179. region->pads[2] = toInt(tuple + 2);
  180. region->pads[3] = toInt(tuple + 3);
  181. readTuple(&begin, end, tuple);
  182. }
  183. }
  184. region->originalWidth = toInt(tuple);
  185. region->originalHeight = toInt(tuple + 1);
  186. readTuple(&begin, end, tuple);
  187. region->offsetX = (float)toInt(tuple);
  188. region->offsetY = (float)toInt(tuple + 1);
  189. readValue(&begin, end, &str);
  190. region->index = toInt(&str);
  191. _regions.add(region);
  192. }
  193. }
  194. }
  195. void Atlas::trim(Str *str) {
  196. while (isspace((unsigned char) *str->begin) && str->begin < str->end)
  197. (str->begin)++;
  198. if (str->begin == str->end) return;
  199. str->end--;
  200. while (((unsigned char)*str->end == '\r') && str->end >= str->begin)
  201. str->end--;
  202. str->end++;
  203. }
  204. int Atlas::readLine(const char **begin, const char *end, Str *str) {
  205. if (*begin == end) return 0;
  206. str->begin = *begin;
  207. /* Find next delimiter. */
  208. while (*begin != end && **begin != '\n')
  209. (*begin)++;
  210. str->end = *begin;
  211. trim(str);
  212. if (*begin != end) (*begin)++;
  213. return 1;
  214. }
  215. int Atlas::beginPast(Str *str, char c) {
  216. const char *begin = str->begin;
  217. while (true) {
  218. char lastSkippedChar = *begin;
  219. if (begin == str->end) return 0;
  220. begin++;
  221. if (lastSkippedChar == c) break;
  222. }
  223. str->begin = begin;
  224. return 1;
  225. }
  226. int Atlas::readValue(const char **begin, const char *end, Str *str) {
  227. readLine(begin, end, str);
  228. if (!beginPast(str, ':')) return 0;
  229. trim(str);
  230. return 1;
  231. }
  232. int Atlas::readTuple(const char **begin, const char *end, Str tuple[]) {
  233. int i;
  234. Str str = {NULL, NULL};
  235. readLine(begin, end, &str);
  236. if (!beginPast(&str, ':')) return 0;
  237. for (i = 0; i < 3; ++i) {
  238. tuple[i].begin = str.begin;
  239. if (!beginPast(&str, ',')) break;
  240. tuple[i].end = str.begin - 2;
  241. trim(&tuple[i]);
  242. }
  243. tuple[i].begin = str.begin;
  244. tuple[i].end = str.end;
  245. trim(&tuple[i]);
  246. return i + 1;
  247. }
  248. char *Atlas::mallocString(Str *str) {
  249. int length = (int) (str->end - str->begin);
  250. char *string = SpineExtension::calloc<char>(length + 1, __FILE__, __LINE__);
  251. memcpy(string, str->begin, length);
  252. string[length] = '\0';
  253. return string;
  254. }
  255. int Atlas::indexOf(const char **array, int count, Str *str) {
  256. int length = (int) (str->end - str->begin);
  257. int i;
  258. for (i = count - 1; i >= 0; i--)
  259. if (strncmp(array[i], str->begin, length) == 0) return i;
  260. return 0;
  261. }
  262. int Atlas::equals(Str *str, const char *other) {
  263. return strncmp(other, str->begin, str->end - str->begin) == 0;
  264. }
  265. int Atlas::toInt(Str *str) {
  266. return (int) strtol(str->begin, (char **) &str->end, 10);
  267. }