extension.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. /*
  31. Implementation notes:
  32. - An OOP style is used where each "class" is made up of a struct and a number of functions prefixed with the struct name.
  33. - struct fields that are const are readonly. Either they are set in a create function and can never be changed, or they can only
  34. be changed by calling a function.
  35. - Inheritance is done using a struct field named "super" as the first field, allowing the struct to be cast to its "super class".
  36. This works because a pointer to a struct is guaranteed to be a pointer to the first struct field.
  37. - Classes intended for inheritance provide init/deinit functions which subclasses must call in their create/dispose functions.
  38. - Polymorphism is done by a base class providing function pointers in its init function. The public API delegates to these
  39. function pointers.
  40. - Subclasses do not provide a dispose function, instead the base class' dispose function should be used, which will delegate to
  41. a dispose function pointer.
  42. - Classes not designed for inheritance cannot be extended because they may use an internal subclass to hide private data and don't
  43. expose function pointers.
  44. - The public API hides implementation details, such as init/deinit functions. An internal API is exposed by extension.h to allow
  45. classes to be extended. Internal functions begin with underscore (_).
  46. - OOP in C tends to lose type safety. Macros for casting are provided in extension.h to give context for why a cast is being done.
  47. - If SPINE_SHORT_NAMES is defined, the "sp" prefix for all class names is optional.
  48. */
  49. #ifndef SPINE_EXTENSION_H_
  50. #define SPINE_EXTENSION_H_
  51. /* All allocation uses these. */
  52. #define MALLOC(TYPE,COUNT) ((TYPE*)_malloc(sizeof(TYPE) * (COUNT), __FILE__, __LINE__))
  53. #define CALLOC(TYPE,COUNT) ((TYPE*)_calloc(COUNT, sizeof(TYPE), __FILE__, __LINE__))
  54. #define NEW(TYPE) CALLOC(TYPE,1)
  55. /* Gets the direct super class. Type safe. */
  56. #define SUPER(VALUE) (&VALUE->super)
  57. /* Cast to a super class. Not type safe, use with care. Prefer SUPER() where possible. */
  58. #define SUPER_CAST(TYPE,VALUE) ((TYPE*)VALUE)
  59. /* Cast to a sub class. Not type safe, use with care. */
  60. #define SUB_CAST(TYPE,VALUE) ((TYPE*)VALUE)
  61. /* Casts away const. Can be used as an lvalue. Not type safe, use with care. */
  62. #define CONST_CAST(TYPE,VALUE) (*(TYPE*)&VALUE)
  63. /* Gets the vtable for the specified type. Not type safe, use with care. */
  64. #define VTABLE(TYPE,VALUE) ((_##TYPE##Vtable*)((TYPE*)VALUE)->vtable)
  65. /* Frees memory. Can be used on const types. */
  66. #define FREE(VALUE) _free((void*)VALUE)
  67. /* Allocates a new char[], assigns it to TO, and copies FROM to it. Can be used on const types. */
  68. #define MALLOC_STR(TO,FROM) strcpy(CONST_CAST(char*, TO) = (char*)MALLOC(char, strlen(FROM) + 1), FROM)
  69. #define PI 3.1415926535897932385f
  70. #define PI2 (PI * 2)
  71. #define DEG_RAD (PI / 180)
  72. #define RAD_DEG (180 / PI)
  73. #define ABS(A) ((A) < 0? -(A): (A))
  74. #define SIGNUM(A) ((A) < 0? -1: (A) > 0 ? 1 : 0)
  75. #ifdef __STDC_VERSION__
  76. #define FMOD(A,B) fmodf(A, B)
  77. #define ATAN2(A,B) atan2f(A, B)
  78. #define SIN(A) sinf(A)
  79. #define COS(A) cosf(A)
  80. #define SQRT(A) sqrtf(A)
  81. #define ACOS(A) acosf(A)
  82. #else
  83. #define FMOD(A,B) (float)fmod(A, B)
  84. #define ATAN2(A,B) (float)atan2(A, B)
  85. #define COS(A) (float)cos(A)
  86. #define SIN(A) (float)sin(A)
  87. #define SQRT(A) (float)sqrt(A)
  88. #define ACOS(A) (float)acos(A)
  89. #endif
  90. #define SIN_DEG(A) SIN((A) * DEG_RAD)
  91. #define COS_DEG(A) COS((A) * DEG_RAD)
  92. #define CLAMP(x, min, max) ((x) < (min) ? (min) : ((x) > (max) ? (max) : (x)))
  93. #ifndef MIN
  94. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  95. #endif
  96. #ifndef MAX
  97. #define MAX(x, y) ((x) > (y) ? (x) : (y))
  98. #endif
  99. #define UNUSED(x) (void)(x)
  100. #include <stdlib.h>
  101. #include <string.h>
  102. #include <math.h>
  103. #include <spine/Skeleton.h>
  104. #include <spine/Animation.h>
  105. #include <spine/Atlas.h>
  106. #include <spine/AttachmentLoader.h>
  107. #include <spine/VertexAttachment.h>
  108. #include <spine/RegionAttachment.h>
  109. #include <spine/MeshAttachment.h>
  110. #include <spine/BoundingBoxAttachment.h>
  111. #include <spine/PathAttachment.h>
  112. #include <spine/AnimationState.h>
  113. #ifdef __cplusplus
  114. extern "C" {
  115. #endif
  116. /*
  117. * Functions that must be implemented:
  118. */
  119. void _spAtlasPage_createTexture (spAtlasPage* self, const char* path);
  120. void _spAtlasPage_disposeTexture (spAtlasPage* self);
  121. char* _spUtil_readFile (const char* path, int* length);
  122. #ifdef SPINE_SHORT_NAMES
  123. #define _AtlasPage_createTexture(...) _spAtlasPage_createTexture(__VA_ARGS__)
  124. #define _AtlasPage_disposeTexture(...) _spAtlasPage_disposeTexture(__VA_ARGS__)
  125. #define _Util_readFile(...) _spUtil_readFile(__VA_ARGS__)
  126. #endif
  127. /*
  128. * Internal API available for extension:
  129. */
  130. void* _malloc (size_t size, const char* file, int line);
  131. void* _calloc (size_t num, size_t size, const char* file, int line);
  132. void _free (void* ptr);
  133. void _setMalloc (void* (*_malloc) (size_t size));
  134. void _setDebugMalloc (void* (*_malloc) (size_t size, const char* file, int line));
  135. void _setFree (void (*_free) (void* ptr));
  136. char* _readFile (const char* path, int* length);
  137. /**/
  138. typedef union _spEventQueueItem {
  139. int type;
  140. spTrackEntry* entry;
  141. spEvent* event;
  142. } _spEventQueueItem;
  143. typedef struct _spAnimationState _spAnimationState;
  144. typedef struct _spEventQueue {
  145. _spAnimationState* state;
  146. _spEventQueueItem* objects;
  147. int objectsCount;
  148. int objectsCapacity;
  149. int /*boolean*/ drainDisabled;
  150. #ifdef __cplusplus
  151. _spEventQueue() :
  152. state(0),
  153. objects(0),
  154. objectsCount(0),
  155. objectsCapacity(0),
  156. drainDisabled(0) {
  157. }
  158. #endif
  159. } _spEventQueue;
  160. struct _spAnimationState {
  161. spAnimationState super;
  162. int eventsCount;
  163. spEvent** events;
  164. _spEventQueue* queue;
  165. int* propertyIDs;
  166. int propertyIDsCount;
  167. int propertyIDsCapacity;
  168. int /*boolean*/ animationsChanged;
  169. #ifdef __cplusplus
  170. _spAnimationState() :
  171. super(),
  172. eventsCount(0),
  173. events(0),
  174. queue(0),
  175. propertyIDs(0),
  176. propertyIDsCount(0),
  177. propertyIDsCapacity(0),
  178. animationsChanged(0) {
  179. }
  180. #endif
  181. };
  182. /**/
  183. /* configureAttachment and disposeAttachment may be 0. */
  184. void _spAttachmentLoader_init (spAttachmentLoader* self,
  185. void (*dispose) (spAttachmentLoader* self),
  186. spAttachment* (*createAttachment) (spAttachmentLoader* self, spSkin* skin, spAttachmentType type, const char* name,
  187. const char* path),
  188. void (*configureAttachment) (spAttachmentLoader* self, spAttachment*),
  189. void (*disposeAttachment) (spAttachmentLoader* self, spAttachment*)
  190. );
  191. void _spAttachmentLoader_deinit (spAttachmentLoader* self);
  192. /* Can only be called from createAttachment. */
  193. void _spAttachmentLoader_setError (spAttachmentLoader* self, const char* error1, const char* error2);
  194. void _spAttachmentLoader_setUnknownTypeError (spAttachmentLoader* self, spAttachmentType type);
  195. #ifdef SPINE_SHORT_NAMES
  196. #define _AttachmentLoader_init(...) _spAttachmentLoader_init(__VA_ARGS__)
  197. #define _AttachmentLoader_deinit(...) _spAttachmentLoader_deinit(__VA_ARGS__)
  198. #define _AttachmentLoader_setError(...) _spAttachmentLoader_setError(__VA_ARGS__)
  199. #define _AttachmentLoader_setUnknownTypeError(...) _spAttachmentLoader_setUnknownTypeError(__VA_ARGS__)
  200. #endif
  201. /**/
  202. void _spAttachment_init (spAttachment* self, const char* name, spAttachmentType type,
  203. void (*dispose) (spAttachment* self));
  204. void _spAttachment_deinit (spAttachment* self);
  205. void _spVertexAttachment_deinit (spVertexAttachment* self);
  206. #ifdef SPINE_SHORT_NAMES
  207. #define _Attachment_init(...) _spAttachment_init(__VA_ARGS__)
  208. #define _Attachment_deinit(...) _spAttachment_deinit(__VA_ARGS__)
  209. #define _VertexAttachment_deinit(...) _spVertexAttachment_deinit(__VA_ARGS__)
  210. #endif
  211. /**/
  212. void _spTimeline_init (spTimeline* self, spTimelineType type,
  213. void (*dispose) (spTimeline* self),
  214. void (*apply) (const spTimeline* self, spSkeleton* skeleton, float lastTime, float time, spEvent** firedEvents,
  215. int* eventsCount, float alpha, int setupPose, int mixingOut),
  216. int (*getPropertyId) (const spTimeline* self));
  217. void _spTimeline_deinit (spTimeline* self);
  218. #ifdef SPINE_SHORT_NAMES
  219. #define _Timeline_init(...) _spTimeline_init(__VA_ARGS__)
  220. #define _Timeline_deinit(...) _spTimeline_deinit(__VA_ARGS__)
  221. #endif
  222. /**/
  223. void _spCurveTimeline_init (spCurveTimeline* self, spTimelineType type, int framesCount,
  224. void (*dispose) (spTimeline* self),
  225. void (*apply) (const spTimeline* self, spSkeleton* skeleton, float lastTime, float time, spEvent** firedEvents, int* eventsCount, float alpha, int setupPose, int mixingOut),
  226. int (*getPropertyId) (const spTimeline* self));
  227. void _spCurveTimeline_deinit (spCurveTimeline* self);
  228. int _spCurveTimeline_binarySearch (float *values, int valuesLength, float target, int step);
  229. #ifdef SPINE_SHORT_NAMES
  230. #define _CurveTimeline_init(...) _spCurveTimeline_init(__VA_ARGS__)
  231. #define _CurveTimeline_deinit(...) _spCurveTimeline_deinit(__VA_ARGS__)
  232. #define _CurveTimeline_binarySearch(...) _spCurveTimeline_binarySearch(__VA_ARGS__)
  233. #endif
  234. #ifdef __cplusplus
  235. }
  236. #endif
  237. #endif /* SPINE_EXTENSION_H_ */