Atlas.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. #ifndef Spine_Atlas_h
  30. #define Spine_Atlas_h
  31. #include <spine/Vector.h>
  32. #include <spine/extension.h>
  33. #include <spine/SpineObject.h>
  34. #include <spine/SpineString.h>
  35. #include <spine/HasRendererObject.h>
  36. namespace spine {
  37. enum Format {
  38. Format_Alpha,
  39. Format_Intensity,
  40. Format_LuminanceAlpha,
  41. Format_RGB565,
  42. Format_RGBA4444,
  43. Format_RGB888,
  44. Format_RGBA8888
  45. };
  46. enum TextureFilter {
  47. TextureFilter_Unknown,
  48. TextureFilter_Nearest,
  49. TextureFilter_Linear,
  50. TextureFilter_MipMap,
  51. TextureFilter_MipMapNearestNearest,
  52. TextureFilter_MipMapLinearNearest,
  53. TextureFilter_MipMapNearestLinear,
  54. TextureFilter_MipMapLinearLinear
  55. };
  56. enum TextureWrap {
  57. TextureWrap_MirroredRepeat,
  58. TextureWrap_ClampToEdge,
  59. TextureWrap_Repeat
  60. };
  61. class SP_API AtlasPage : public SpineObject, public HasRendererObject {
  62. public:
  63. String name;
  64. String texturePath;
  65. Format format;
  66. TextureFilter minFilter;
  67. TextureFilter magFilter;
  68. TextureWrap uWrap;
  69. TextureWrap vWrap;
  70. int width, height;
  71. explicit AtlasPage(const String &inName) : name(inName), format(Format_RGBA8888), minFilter(TextureFilter_Nearest),
  72. magFilter(TextureFilter_Nearest), uWrap(TextureWrap_ClampToEdge),
  73. vWrap(TextureWrap_ClampToEdge), width(0), height(0) {
  74. }
  75. };
  76. class SP_API AtlasRegion : public SpineObject {
  77. public:
  78. AtlasPage *page;
  79. String name;
  80. int x, y, width, height;
  81. float u, v, u2, v2;
  82. float offsetX, offsetY;
  83. int originalWidth, originalHeight;
  84. int index;
  85. bool rotate;
  86. int degrees;
  87. Vector<int> splits;
  88. Vector<int> pads;
  89. };
  90. class TextureLoader;
  91. class SP_API Atlas : public SpineObject {
  92. public:
  93. Atlas(const String &path, TextureLoader *textureLoader, bool createTexture = true);
  94. Atlas(const char *data, int length, const char *dir, TextureLoader *textureLoader, bool createTexture = true);
  95. ~Atlas();
  96. void flipV();
  97. /// Returns the first region found with the specified name. This method uses String comparison to find the region, so the result
  98. /// should be cached rather than calling this method multiple times.
  99. /// @return The region, or NULL.
  100. AtlasRegion *findRegion(const String &name);
  101. Vector<AtlasPage*> &getPages();
  102. Vector<AtlasRegion*> &getRegions();
  103. private:
  104. Vector<AtlasPage *> _pages;
  105. Vector<AtlasRegion *> _regions;
  106. TextureLoader *_textureLoader;
  107. void load(const char *begin, int length, const char *dir, bool createTexture);
  108. class Str {
  109. public:
  110. const char *begin;
  111. const char *end;
  112. };
  113. static void trim(Str *str);
  114. /// Tokenize string without modification. Returns 0 on failure
  115. static int readLine(const char **begin, const char *end, Str *str);
  116. /// Moves str->begin past the first occurence of c. Returns 0 on failure
  117. static int beginPast(Str *str, char c);
  118. /// Returns 0 on failure
  119. static int readValue(const char **begin, const char *end, Str *str);
  120. /// Returns the number of tuple values read (1, 2, 4, or 0 for failure)
  121. static int readTuple(const char **begin, const char *end, Str tuple[]);
  122. static char *mallocString(Str *str);
  123. static int indexOf(const char **array, int count, Str *str);
  124. static int equals(Str *str, const char *other);
  125. static int toInt(Str *str);
  126. static Atlas *abortAtlas(Atlas *atlas);
  127. };
  128. }
  129. #endif /* Spine_Atlas_h */