extension.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. Implementation notes:
  3. - An OOP style is used where each "class" is made up of a struct and a number of functions prefixed with the struct name.
  4. - struct fields that are const are readonly. Either they are set in a create function and can never be changed, or they can only
  5. be changed by calling a function.
  6. - Inheritance is done using a struct field named "super" as the first field, allowing the struct to be cast to its "super class".
  7. This works because a pointer to a struct is guaranteed to be a pointer to the first struct field.
  8. - Classes intended for inheritance provide init/deinit functions which subclasses must call in their create/dispose functions.
  9. - Polymorphism is done by a base class providing function pointers in its init function. The public API delegates to this
  10. function.
  11. - Subclasses do not provide a dispose function, instead the base class' dispose function should be used, which will delegate to
  12. a dispose function pointer.
  13. - Classes not designed for inheritance cannot be extended because they may use an internal subclass to hide private data and don't
  14. expose function pointers.
  15. - The public API hides implementation details, such as init/deinit functions. An internal API is exposed by extension.h to allow
  16. classes to be extended. Internal functions begin with underscore (_).
  17. - 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.
  18. - If SPINE_SHORT_NAMES is defined, the "sp" prefix for all class names is optional.
  19. */
  20. #ifndef SPINE_EXTENSION_H_
  21. #define SPINE_EXTENSION_H_
  22. /* All allocation uses these. */
  23. #define MALLOC(TYPE,COUNT) ((TYPE*)_malloc(sizeof(TYPE) * COUNT))
  24. #define CALLOC(TYPE,COUNT) ((TYPE*)_calloc(COUNT, sizeof(TYPE)))
  25. #define NEW(TYPE) CALLOC(TYPE,1)
  26. /* Gets the direct super class. Type safe. */
  27. #define SUPER(VALUE) (&VALUE->super)
  28. /* Cast to a super class. Not type safe, use with care. Prefer SUPER() where possible. */
  29. #define SUPER_CAST(TYPE,VALUE) ((TYPE*)VALUE)
  30. /* Cast to a sub class. Not type safe, use with care. */
  31. #define SUB_CAST(TYPE,VALUE) ((TYPE*)VALUE)
  32. /* Casts away const. Can be used as an lvalue. Not type safe, use with care. */
  33. #define CONST_CAST(TYPE,VALUE) (*(TYPE*)&VALUE)
  34. /* Gets the vtable for the specified type. Not type safe, use with care. */
  35. #define VTABLE(TYPE,VALUE) ((_##TYPE##Vtable*)((TYPE*)VALUE)->vtable)
  36. /* Frees memory. Can be used on const types. */
  37. #define FREE(VALUE) _free((void*)VALUE)
  38. /* Allocates a new char[], assigns it to TO, and copies FROM to it. Can be used on const types. */
  39. #define MALLOC_STR(TO,FROM) strcpy(CONST_CAST(char*, TO) = (char*)malloc(strlen(FROM) + 1), FROM)
  40. #ifdef __STDC_VERSION__
  41. #define FMOD(A,B) fmodf(A, B)
  42. #else
  43. #define FMOD(A,B) (float)fmod(A, B)
  44. #endif
  45. #include <stdlib.h>
  46. #include <string.h>
  47. #include <math.h>
  48. #include <spine/Skeleton.h>
  49. #include <spine/RegionAttachment.h>
  50. #include <spine/BoundingBoxAttachment.h>
  51. #include <spine/Animation.h>
  52. #include <spine/Atlas.h>
  53. #include <spine/AttachmentLoader.h>
  54. #ifdef __cplusplus
  55. extern "C" {
  56. #endif
  57. /*
  58. * Functions that must be implemented:
  59. */
  60. void _spAtlasPage_createTexture (spAtlasPage* self, const char* path);
  61. void _spAtlasPage_disposeTexture (spAtlasPage* self);
  62. char* _spUtil_readFile (const char* path, int* length);
  63. #ifdef SPINE_SHORT_NAMES
  64. #define _AtlasPage_createTexture(...) _spAtlasPage_createTexture(__VA_ARGS__)
  65. #define _AtlasPage_disposeTexture(...) _spAtlasPage_disposeTexture(__VA_ARGS__)
  66. #define _Util_readFile(...) _spUtil_readFile(__VA_ARGS__)
  67. #endif
  68. /*
  69. * Internal API available for extension:
  70. */
  71. void* _malloc (size_t size);
  72. void* _calloc (size_t num, size_t size);
  73. void _free (void* ptr);
  74. void _setMalloc (void* (*_malloc) (size_t size));
  75. void _setFree (void (*_free) (void* ptr));
  76. char* _readFile (const char* path, int* length);
  77. /**/
  78. void _spAttachmentLoader_init (spAttachmentLoader* self, /**/
  79. void (*dispose) (spAttachmentLoader* self), /**/
  80. spAttachment* (*newAttachment) (spAttachmentLoader* self, spSkin* skin, spAttachmentType type, const char* name));
  81. void _spAttachmentLoader_deinit (spAttachmentLoader* self);
  82. void _spAttachmentLoader_setError (spAttachmentLoader* self, const char* error1, const char* error2);
  83. void _spAttachmentLoader_setUnknownTypeError (spAttachmentLoader* self, spAttachmentType type);
  84. #ifdef SPINE_SHORT_NAMES
  85. #define _AttachmentLoader_init(...) _spAttachmentLoader_init(__VA_ARGS__)
  86. #define _AttachmentLoader_deinit(...) _spAttachmentLoader_deinit(__VA_ARGS__)
  87. #define _AttachmentLoader_setError(...) _spAttachmentLoader_setError(__VA_ARGS__)
  88. #define _AttachmentLoader_setUnknownTypeError(...) _spAttachmentLoader_setUnknownTypeError(__VA_ARGS__)
  89. #endif
  90. /**/
  91. void _spAttachment_init (spAttachment* self, const char* name, spAttachmentType type, /**/
  92. void (*dispose) (spAttachment* self));
  93. void _spAttachment_deinit (spAttachment* self);
  94. #ifdef SPINE_SHORT_NAMES
  95. #define _Attachment_init(...) _spAttachment_init(__VA_ARGS__)
  96. #define _Attachment_deinit(...) _spAttachment_deinit(__VA_ARGS__)
  97. #endif
  98. /**/
  99. void _spTimeline_init (spTimeline* self, spTimelineType type, /**/
  100. void (*dispose) (spTimeline* self), /**/
  101. void (*apply) (const spTimeline* self, spSkeleton* skeleton, float lastTime, float time, spEvent** firedEvents,
  102. int* eventCount, float alpha));
  103. void _spTimeline_deinit (spTimeline* self);
  104. #ifdef SPINE_SHORT_NAMES
  105. #define _Timeline_init(...) _spTimeline_init(__VA_ARGS__)
  106. #define _Timeline_deinit(...) _spTimeline_deinit(__VA_ARGS__)
  107. #endif
  108. /**/
  109. void _spCurveTimeline_init (spCurveTimeline* self, spTimelineType type, int frameCount, /**/
  110. void (*dispose) (spTimeline* self), /**/
  111. void (*apply) (const spTimeline* self, spSkeleton* skeleton, float lastTime, float time, spEvent** firedEvents,
  112. int* eventCount, float alpha));
  113. void _spCurveTimeline_deinit (spCurveTimeline* self);
  114. #ifdef SPINE_SHORT_NAMES
  115. #define _CurveTimeline_init(...) _spCurveTimeline_init(__VA_ARGS__)
  116. #define _CurveTimeline_deinit(...) _spCurveTimeline_deinit(__VA_ARGS__)
  117. #endif
  118. #ifdef __cplusplus
  119. }
  120. #endif
  121. #endif /* SPINE_EXTENSION_H_ */