Blame view

ios/cocos2d/cocos/editor-support/spine/extension.h 11.2 KB
520389e3   xiaoyu   接入cocos源码,编译未通过,继续修改
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
  /******************************************************************************
   * Spine Runtimes Software License v2.5
   *
   * Copyright (c) 2013-2016, Esoteric Software
   * All rights reserved.
   *
   * You are granted a perpetual, non-exclusive, non-sublicensable, and
   * non-transferable license to use, install, execute, and perform the Spine
   * Runtimes software and derivative works solely for personal or internal
   * use. Without the written permission of Esoteric Software (see Section 2 of
   * the Spine Software License Agreement), you may not (a) modify, translate,
   * adapt, or develop new applications using the Spine Runtimes or otherwise
   * create derivative works or improvements of the Spine Runtimes or (b) remove,
   * delete, alter, or obscure any trademarks or any copyright, trademark, patent,
   * or other intellectual property or proprietary rights notices on or in the
   * Software, including any copy thereof. Redistributions in binary or source
   * form must include this license and terms.
   *
   * THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
   * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
   * EVENT SHALL ESOTERIC SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS INTERRUPTION, OR LOSS OF
   * USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
   * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   * POSSIBILITY OF SUCH DAMAGE.
   *****************************************************************************/
  
  /*
   Implementation notes:
  
   - An OOP style is used where each "class" is made up of a struct and a number of functions prefixed with the struct name.
  
   - struct fields that are const are readonly. Either they are set in a create function and can never be changed, or they can only
   be changed by calling a function.
  
   - Inheritance is done using a struct field named "super" as the first field, allowing the struct to be cast to its "super class".
   This works because a pointer to a struct is guaranteed to be a pointer to the first struct field.
  
   - Classes intended for inheritance provide init/deinit functions which subclasses must call in their create/dispose functions.
  
   - Polymorphism is done by a base class providing function pointers in its init function. The public API delegates to these
   function pointers.
  
   - Subclasses do not provide a dispose function, instead the base class' dispose function should be used, which will delegate to
   a dispose function pointer.
  
   - Classes not designed for inheritance cannot be extended because they may use an internal subclass to hide private data and don't
   expose function pointers.
  
   - The public API hides implementation details, such as init/deinit functions. An internal API is exposed by extension.h to allow
   classes to be extended. Internal functions begin with underscore (_).
  
   - 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.
  
   - If SPINE_SHORT_NAMES is defined, the "sp" prefix for all class names is optional.
   */
  
  #ifndef SPINE_EXTENSION_H_
  #define SPINE_EXTENSION_H_
  
  /* All allocation uses these. */
  #define MALLOC(TYPE,COUNT) ((TYPE*)_spMalloc(sizeof(TYPE) * (COUNT), __FILE__, __LINE__))
  #define CALLOC(TYPE,COUNT) ((TYPE*)_spCalloc(COUNT, sizeof(TYPE), __FILE__, __LINE__))
  #define REALLOC(PTR,TYPE,COUNT) ((TYPE*)_spRealloc(PTR, sizeof(TYPE) * (COUNT)))
  #define NEW(TYPE) CALLOC(TYPE,1)
  
  /* Gets the direct super class. Type safe. */
  #define SUPER(VALUE) (&VALUE->super)
  
  /* Cast to a super class. Not type safe, use with care. Prefer SUPER() where possible. */
  #define SUPER_CAST(TYPE,VALUE) ((TYPE*)VALUE)
  
  /* Cast to a sub class. Not type safe, use with care. */
  #define SUB_CAST(TYPE,VALUE) ((TYPE*)VALUE)
  
  /* Casts away const. Can be used as an lvalue. Not type safe, use with care. */
  #define CONST_CAST(TYPE,VALUE) (*(TYPE*)&VALUE)
  
  /* Gets the vtable for the specified type. Not type safe, use with care. */
  #define VTABLE(TYPE,VALUE) ((_##TYPE##Vtable*)((TYPE*)VALUE)->vtable)
  
  /* Frees memory. Can be used on const types. */
  #define FREE(VALUE) _spFree((void*)VALUE)
  
  /* Allocates a new char[], assigns it to TO, and copies FROM to it. Can be used on const types. */
  #define MALLOC_STR(TO,FROM) strcpy(CONST_CAST(char*, TO) = (char*)MALLOC(char, strlen(FROM) + 1), FROM)
  
  #define PI 3.1415926535897932385f
  #define PI2 (PI * 2)
  #define DEG_RAD (PI / 180)
  #define RAD_DEG (180 / PI)
  
  #define ABS(A) ((A) < 0? -(A): (A))
  #define SIGNUM(A) ((A) < 0? -1: (A) > 0 ? 1 : 0)
  
  #ifdef __STDC_VERSION__
  #define FMOD(A,B) fmodf(A, B)
  #define ATAN2(A,B) atan2f(A, B)
  #define SIN(A) sinf(A)
  #define COS(A) cosf(A)
  #define SQRT(A) sqrtf(A)
  #define ACOS(A) acosf(A)
  #define POW(A,B) pow(A, B)
  #else
  #define FMOD(A,B) (float)fmod(A, B)
  #define ATAN2(A,B) (float)atan2(A, B)
  #define COS(A) (float)cos(A)
  #define SIN(A) (float)sin(A)
  #define SQRT(A) (float)sqrt(A)
  #define ACOS(A) (float)acos(A)
  #define POW(A,B) (float)pow(A, B)
  #endif
  
  #define SIN_DEG(A) SIN((A) * DEG_RAD)
  #define COS_DEG(A) COS((A) * DEG_RAD)
  #define CLAMP(x, min, max) ((x) < (min) ? (min) : ((x) > (max) ? (max) : (x)))
  #ifndef MIN
  #define MIN(x, y) ((x) < (y) ? (x) : (y))
  #endif
  #ifndef MAX
  #define MAX(x, y) ((x) > (y) ? (x) : (y))
  #endif
  
  #define UNUSED(x) (void)(x)
  
  #include <stdlib.h>
  #include <string.h>
  #include <math.h>
  #include <spine/Skeleton.h>
  #include <spine/Animation.h>
  #include <spine/Atlas.h>
  #include <spine/AttachmentLoader.h>
  #include <spine/VertexAttachment.h>
  #include <spine/RegionAttachment.h>
  #include <spine/MeshAttachment.h>
  #include <spine/BoundingBoxAttachment.h>
  #include <spine/ClippingAttachment.h>
  #include <spine/PathAttachment.h>
  #include <spine/PointAttachment.h>
  #include <spine/AnimationState.h>
  
  #ifdef __cplusplus
  extern "C" {
  #endif
  
  /*
   * Functions that must be implemented:
   */
  
  void _spAtlasPage_createTexture (spAtlasPage* self, const char* path);
  void _spAtlasPage_disposeTexture (spAtlasPage* self);
  char* _spUtil_readFile (const char* path, int* length);
  
  #ifdef SPINE_SHORT_NAMES
  #define _AtlasPage_createTexture(...) _spAtlasPage_createTexture(__VA_ARGS__)
  #define _AtlasPage_disposeTexture(...) _spAtlasPage_disposeTexture(__VA_ARGS__)
  #define _Util_readFile(...) _spUtil_readFile(__VA_ARGS__)
  #endif
  
  /*
   * Internal API available for extension:
   */
  
  void* _spMalloc (size_t size, const char* file, int line);
  void* _spCalloc (size_t num, size_t size, const char* file, int line);
  void* _spRealloc(void* ptr, size_t size);
  void _spFree (void* ptr);
  float _spRandom ();
  
  void _spSetMalloc (void* (*_malloc) (size_t size));
  void _spSetDebugMalloc (void* (*_malloc) (size_t size, const char* file, int line));
  void _spSetRealloc(void* (*_realloc) (void* ptr, size_t size));
  void _spSetFree (void (*_free) (void* ptr));
  void _spSetRandom(float (*_random) ());
  
  char* _spReadFile (const char* path, int* length);
  
  
  /*
   * Math utilities
   */
  float _spMath_random(float min, float max);
  float _spMath_randomTriangular(float min, float max);
  float _spMath_randomTriangularWith(float min, float max, float mode);
  float _spMath_interpolate(float (*apply) (float a), float start, float end, float a);
  float _spMath_pow2_apply(float a);
  float _spMath_pow2out_apply(float a);
  
  /**/
  
  typedef union _spEventQueueItem {
  	int type;
  	spTrackEntry* entry;
  	spEvent* event;
  } _spEventQueueItem;
  
  typedef struct _spAnimationState _spAnimationState;
  
  typedef struct _spEventQueue {
  	_spAnimationState* state;
  	_spEventQueueItem* objects;
  	int objectsCount;
  	int objectsCapacity;
  	int /*boolean*/ drainDisabled;
  
  #ifdef __cplusplus
  	_spEventQueue() :
  		state(0),
  		objects(0),
  		objectsCount(0),
  		objectsCapacity(0),
  		drainDisabled(0) {
  	}
  #endif
  } _spEventQueue;
  
  struct _spAnimationState {
  	spAnimationState super;
  
  	int eventsCount;
  	spEvent** events;
  
  	_spEventQueue* queue;
  
  	int* propertyIDs;
  	int propertyIDsCount;
  	int propertyIDsCapacity;
  
  	int /*boolean*/ animationsChanged;
  
  #ifdef __cplusplus
  	_spAnimationState() :
  		super(),
  		eventsCount(0),
  		events(0),
  		queue(0),
  		propertyIDs(0),
  		propertyIDsCount(0),
  		propertyIDsCapacity(0),
  		animationsChanged(0) {
  	}
  #endif
  };
  
  
  /**/
  
  /* configureAttachment and disposeAttachment may be 0. */
  void _spAttachmentLoader_init (spAttachmentLoader* self,
  	void (*dispose) (spAttachmentLoader* self),
  	spAttachment* (*createAttachment) (spAttachmentLoader* self, spSkin* skin, spAttachmentType type, const char* name,
  		const char* path),
  	void (*configureAttachment) (spAttachmentLoader* self, spAttachment*),
  	void (*disposeAttachment) (spAttachmentLoader* self, spAttachment*)
  );
  void _spAttachmentLoader_deinit (spAttachmentLoader* self);
  /* Can only be called from createAttachment. */
  void _spAttachmentLoader_setError (spAttachmentLoader* self, const char* error1, const char* error2);
  void _spAttachmentLoader_setUnknownTypeError (spAttachmentLoader* self, spAttachmentType type);
  
  #ifdef SPINE_SHORT_NAMES
  #define _AttachmentLoader_init(...) _spAttachmentLoader_init(__VA_ARGS__)
  #define _AttachmentLoader_deinit(...) _spAttachmentLoader_deinit(__VA_ARGS__)
  #define _AttachmentLoader_setError(...) _spAttachmentLoader_setError(__VA_ARGS__)
  #define _AttachmentLoader_setUnknownTypeError(...) _spAttachmentLoader_setUnknownTypeError(__VA_ARGS__)
  #endif
  
  /**/
  
  void _spAttachment_init (spAttachment* self, const char* name, spAttachmentType type,
  void (*dispose) (spAttachment* self));
  void _spAttachment_deinit (spAttachment* self);
  void _spVertexAttachment_init (spVertexAttachment* self);
  void _spVertexAttachment_deinit (spVertexAttachment* self);
  
  #ifdef SPINE_SHORT_NAMES
  #define _Attachment_init(...) _spAttachment_init(__VA_ARGS__)
  #define _Attachment_deinit(...) _spAttachment_deinit(__VA_ARGS__)
  #define _VertexAttachment_deinit(...) _spVertexAttachment_deinit(__VA_ARGS__)
  #endif
  
  /**/
  
  void _spTimeline_init (spTimeline* self, spTimelineType type,
  	void (*dispose) (spTimeline* self),
  	void (*apply) (const spTimeline* self, spSkeleton* skeleton, float lastTime, float time, spEvent** firedEvents,
  		int* eventsCount, float alpha, spMixPose pose, spMixDirection direction),
  	int (*getPropertyId) (const spTimeline* self));
  void _spTimeline_deinit (spTimeline* self);
  
  #ifdef SPINE_SHORT_NAMES
  #define _Timeline_init(...) _spTimeline_init(__VA_ARGS__)
  #define _Timeline_deinit(...) _spTimeline_deinit(__VA_ARGS__)
  #endif
  
  /**/
  
  void _spCurveTimeline_init (spCurveTimeline* self, spTimelineType type, int framesCount,
  	void (*dispose) (spTimeline* self),
  	void (*apply) (const spTimeline* self, spSkeleton* skeleton, float lastTime, float time, spEvent** firedEvents, int* eventsCount, float alpha, spMixPose pose, spMixDirection direction),
  	int (*getPropertyId) (const spTimeline* self));
  void _spCurveTimeline_deinit (spCurveTimeline* self);
  int _spCurveTimeline_binarySearch (float *values, int valuesLength, float target, int step);
  
  #ifdef SPINE_SHORT_NAMES
  #define _CurveTimeline_init(...) _spCurveTimeline_init(__VA_ARGS__)
  #define _CurveTimeline_deinit(...) _spCurveTimeline_deinit(__VA_ARGS__)
  #define _CurveTimeline_binarySearch(...) _spCurveTimeline_binarySearch(__VA_ARGS__)
  #endif
  
  #ifdef __cplusplus
  }
  #endif
  
  #endif /* SPINE_EXTENSION_H_ */