-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
spine-cpp-lite.cpp
4901 lines (4111 loc) · 169 KB
/
spine-cpp-lite.cpp
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
* Spine Runtimes License Agreement
* Last updated July 28, 2023. Replaces all prior versions.
*
* Copyright (c) 2013-2023, Esoteric Software LLC
*
* Integration of the Spine Runtimes into software or otherwise creating
* derivative works of the Spine Runtimes is permitted under the terms and
* conditions of Section 2 of the Spine Editor License Agreement:
* http://esotericsoftware.com/spine-editor-license
*
* Otherwise, it is permitted to integrate the Spine Runtimes into software or
* otherwise create derivative works of the Spine Runtimes (collectively,
* "Products"), provided that each user of the Products must obtain their own
* Spine Editor license and redistribution of the Products in any form must
* include this license and copyright notice.
*
* THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "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 LLC 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 THE
* SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
#include "spine-cpp-light.h"
#include <spine/spine.h>
#include <spine/Version.h>
#include <spine/Debug.h>
using namespace spine;
struct Block {
int size;
int allocated;
uint8_t *memory;
int free() {
return size - allocated;
}
bool canFit(int numBytes) {
return free() >= numBytes;
}
uint8_t *allocate(int numBytes) {
uint8_t *ptr = memory + allocated;
allocated += numBytes;
return ptr;
}
};
class BlockAllocator : public SpineObject {
int initialBlockSize;
Vector<Block> blocks;
public:
BlockAllocator(int initialBlockSize) : initialBlockSize(initialBlockSize) {
blocks.add(newBlock(initialBlockSize));
}
~BlockAllocator() {
for (int i = 0, n = (int) blocks.size(); i < n; i++) {
SpineExtension::free(blocks[i].memory, __FILE__, __LINE__);
}
}
Block newBlock(int numBytes) {
Block block = {MathUtil::max(initialBlockSize, numBytes), 0, nullptr};
block.memory = SpineExtension::alloc<uint8_t>(block.size, __FILE__, __LINE__);
return block;
}
template<typename T>
T *allocate(size_t num) {
return (T *) _allocate((int) (sizeof(T) * num));
}
void compress() {
int totalSize = 0;
for (int i = 0, n = blocks.size(); i < n; i++) {
totalSize += blocks[i].size;
SpineExtension::free(blocks[i].memory, __FILE__, __LINE__);
}
blocks.clear();
blocks.add(newBlock(totalSize));
}
private:
void *_allocate(int numBytes) {
// 16-byte align allocations
int alignedNumBytes = numBytes + (numBytes % 16 != 0 ? 16 - (numBytes % 16) : 0);
Block *block = &blocks[blocks.size() - 1];
if (!block->canFit(alignedNumBytes)) {
blocks.add(newBlock(MathUtil::max(initialBlockSize, alignedNumBytes)));
block = &blocks[blocks.size() - 1];
}
return block->allocate(alignedNumBytes);
}
};
struct AnimationStateEvent {
EventType type;
TrackEntry *entry;
Event *event;
AnimationStateEvent(EventType type, TrackEntry *entry, Event *event) : type(type), entry(entry), event(event){};
};
struct EventListener : public AnimationStateListenerObject {
Vector<AnimationStateEvent> events;
void callback(AnimationState *state, EventType type, TrackEntry *entry, Event *event) {
events.add(AnimationStateEvent(type, entry, event));
SP_UNUSED(state);
}
};
typedef struct _spine_atlas {
void *atlas;
utf8 **imagePaths;
int32_t numImagePaths;
utf8 *error;
} _spine_atlas;
typedef struct _spine_skeleton_data_result {
spine_skeleton_data skeletonData;
utf8 *error;
} _spine_skeleton_data_result;
typedef struct _spine_render_command {
float *positions;
float *uvs;
int32_t *colors;
int32_t numVertices;
uint16_t *indices;
int32_t numIndices;
int32_t atlasPage;
spine_blend_mode blendMode;
struct _spine_render_command *next;
} _spine_render_command;
typedef struct _spine_bounds {
float x, y, width, height;
} _spine_bounds;
typedef struct _spine_vector {
float x, y;
} _spine_vector;
typedef struct _spine_skeleton_drawable : public SpineObject {
spine_skeleton skeleton;
spine_animation_state animationState;
spine_animation_state_data animationStateData;
spine_animation_state_events animationStateEvents;
void *clipping;
BlockAllocator *allocator;
Vector<float> worldVertices;
Vector<unsigned short> quadIndices;
Vector<_spine_render_command *> renderCommands;
_spine_skeleton_drawable() {
quadIndices.add(0);
quadIndices.add(1);
quadIndices.add(2);
quadIndices.add(2);
quadIndices.add(3);
quadIndices.add(0);
}
} _spine_skeleton_drawable;
typedef struct _spine_skin_entry {
int32_t slotIndex;
utf8 *name;
spine_attachment attachment;
} _spine_skin_entry;
typedef struct _spine_skin_entries {
int32_t numEntries;
_spine_skin_entry *entries;
} _spine_skin_entries;
static Color NULL_COLOR(0, 0, 0, 0);
static SpineExtension *defaultExtension = nullptr;
static DebugExtension *debugExtension = nullptr;
static void initExtensions() {
if (defaultExtension == nullptr) {
defaultExtension = new DefaultSpineExtension();
debugExtension = new DebugExtension(defaultExtension);
}
}
spine::SpineExtension *spine::getDefaultExtension() {
initExtensions();
return defaultExtension;
}
void spine_enable_debug_extension(int32_t enable) {
initExtensions();
SpineExtension::setInstance(enable ? debugExtension : defaultExtension);
}
int32_t spine_major_version() {
return SPINE_MAJOR_VERSION;
}
int32_t spine_minor_version() {
return SPINE_MINOR_VERSION;
}
void spine_report_leaks() {
initExtensions();
debugExtension->reportLeaks();
fflush(stdout);
}
// Color
float spine_color_get_r(spine_color color) {
if (!color) return 0;
return ((Color *) color)->r;
}
float spine_color_get_g(spine_color color) {
if (!color) return 0;
return ((Color *) color)->g;
}
float spine_color_get_b(spine_color color) {
if (!color) return 0;
return ((Color *) color)->b;
}
float spine_color_get_a(spine_color color) {
if (!color) return 0;
return ((Color *) color)->a;
}
// Bounds
float spine_bounds_get_x(spine_bounds bounds) {
if (!bounds) return 0;
return ((_spine_bounds *) bounds)->x;
}
float spine_bounds_get_y(spine_bounds bounds) {
if (!bounds) return 0;
return ((_spine_bounds *) bounds)->y;
}
float spine_bounds_get_width(spine_bounds bounds) {
if (!bounds) return 0;
return ((_spine_bounds *) bounds)->width;
}
float spine_bounds_get_height(spine_bounds bounds) {
if (!bounds) return 0;
return ((_spine_bounds *) bounds)->height;
}
// Vector
float spine_vector_get_x(spine_vector vector) {
if (!vector) return 0;
return ((_spine_vector *) vector)->x;
}
float spine_vector_get_y(spine_vector vector) {
if (!vector) return 0;
return ((_spine_vector *) vector)->y;
}
// Atlas
spine_atlas spine_atlas_load(const utf8 *atlasData) {
if (!atlasData) return nullptr;
int32_t length = (int32_t) strlen(atlasData);
auto atlas = new (__FILE__, __LINE__) Atlas(atlasData, length, "", (TextureLoader *) nullptr, false);
_spine_atlas *result = SpineExtension::calloc<_spine_atlas>(1, __FILE__, __LINE__);
result->atlas = atlas;
result->numImagePaths = (int32_t) atlas->getPages().size();
result->imagePaths = SpineExtension::calloc<utf8 *>(result->numImagePaths, __FILE__, __LINE__);
for (int i = 0; i < result->numImagePaths; i++) {
result->imagePaths[i] = (utf8 *) strdup(atlas->getPages()[i]->texturePath.buffer());
}
return (spine_atlas) result;
}
int32_t spine_atlas_get_num_image_paths(spine_atlas atlas) {
if (!atlas) return 0;
return ((_spine_atlas *) atlas)->numImagePaths;
}
utf8 *spine_atlas_get_image_path(spine_atlas atlas, int32_t index) {
if (!atlas) return nullptr;
return ((_spine_atlas *) atlas)->imagePaths[index];
}
utf8 *spine_atlas_get_error(spine_atlas atlas) {
if (!atlas) return nullptr;
return ((_spine_atlas *) atlas)->error;
}
void spine_atlas_dispose(spine_atlas atlas) {
if (!atlas) return;
_spine_atlas *_atlas = (_spine_atlas *) atlas;
if (_atlas->atlas) delete (Atlas *) _atlas->atlas;
if (_atlas->error) free(_atlas->error);
for (int i = 0; i < _atlas->numImagePaths; i++) {
free(_atlas->imagePaths[i]);
}
SpineExtension::free(_atlas->imagePaths, __FILE__, __LINE__);
SpineExtension::free(_atlas, __FILE__, __LINE__);
}
// SkeletonData
spine_skeleton_data_result spine_skeleton_data_load_json(spine_atlas atlas, const utf8 *skeletonData) {
_spine_skeleton_data_result *result = SpineExtension::calloc<_spine_skeleton_data_result>(1, __FILE__, __LINE__);
_spine_atlas *_atlas = (_spine_atlas *) atlas;
Bone::setYDown(true);
if (!_atlas) return (spine_skeleton_data_result) result;
if (!_atlas->atlas) return (spine_skeleton_data_result) result;
if (!skeletonData) return (spine_skeleton_data_result) result;
SkeletonJson json((Atlas *) _atlas->atlas);
SkeletonData *data = json.readSkeletonData(skeletonData);
result->skeletonData = (spine_skeleton_data) data;
if (!json.getError().isEmpty()) {
result->error = (utf8 *) strdup(json.getError().buffer());
}
return (spine_skeleton_data_result) result;
}
spine_skeleton_data_result spine_skeleton_data_load_binary(spine_atlas atlas, const uint8_t *skeletonData, int32_t length) {
_spine_skeleton_data_result *result = SpineExtension::calloc<_spine_skeleton_data_result>(1, __FILE__, __LINE__);
_spine_atlas *_atlas = (_spine_atlas *) atlas;
Bone::setYDown(true);
if (!_atlas) return (spine_skeleton_data_result) result;
if (!_atlas->atlas) return (spine_skeleton_data_result) result;
if (!skeletonData) return (spine_skeleton_data_result) result;
if (length <= 0) return (spine_skeleton_data_result) result;
SkeletonBinary binary((Atlas *) _atlas->atlas);
SkeletonData *data = binary.readSkeletonData(skeletonData, length);
result->skeletonData = (spine_skeleton_data) data;
if (!binary.getError().isEmpty()) {
result->error = (utf8 *) strdup(binary.getError().buffer());
}
return (spine_skeleton_data_result) result;
}
utf8 *spine_skeleton_data_result_get_error(spine_skeleton_data_result result) {
if (!result) return nullptr;
return ((_spine_skeleton_data_result *) result)->error;
}
spine_skeleton_data spine_skeleton_data_result_get_data(spine_skeleton_data_result result) {
if (!result) return nullptr;
return ((_spine_skeleton_data_result *) result)->skeletonData;
}
void spine_skeleton_data_result_dispose(spine_skeleton_data_result result) {
if (!result) return;
_spine_skeleton_data_result *_result = (_spine_skeleton_data_result *) result;
if (_result->error) SpineExtension::free(_result->error, __FILE__, __LINE__);
SpineExtension::free(_result, __FILE__, __LINE__);
}
spine_bone_data spine_skeleton_data_find_bone(spine_skeleton_data data, const utf8 *name) {
if (data == nullptr) return nullptr;
SkeletonData *_data = (SkeletonData *) data;
return (spine_bone_data) _data->findBone(name);
}
spine_slot_data spine_skeleton_data_find_slot(spine_skeleton_data data, const utf8 *name) {
if (data == nullptr) return nullptr;
SkeletonData *_data = (SkeletonData *) data;
return (spine_slot_data) _data->findSlot(name);
}
spine_skin spine_skeleton_data_find_skin(spine_skeleton_data data, const utf8 *name) {
if (data == nullptr) return nullptr;
SkeletonData *_data = (SkeletonData *) data;
return (spine_skin) _data->findSkin(name);
}
spine_event_data spine_skeleton_data_find_event(spine_skeleton_data data, const utf8 *name) {
if (data == nullptr) return nullptr;
SkeletonData *_data = (SkeletonData *) data;
return (spine_event_data) _data->findEvent(name);
}
spine_animation spine_skeleton_data_find_animation(spine_skeleton_data data, const utf8 *name) {
if (data == nullptr) return nullptr;
SkeletonData *_data = (SkeletonData *) data;
return (spine_animation) _data->findAnimation(name);
}
spine_ik_constraint_data spine_skeleton_data_find_ik_constraint(spine_skeleton_data data, const utf8 *name) {
if (data == nullptr) return nullptr;
SkeletonData *_data = (SkeletonData *) data;
return (spine_ik_constraint_data) _data->findIkConstraint(name);
}
spine_transform_constraint_data spine_skeleton_data_find_transform_constraint(spine_skeleton_data data, const utf8 *name) {
if (data == nullptr) return nullptr;
SkeletonData *_data = (SkeletonData *) data;
return (spine_transform_constraint_data) _data->findTransformConstraint(name);
}
spine_path_constraint_data spine_skeleton_data_find_path_constraint(spine_skeleton_data data, const utf8 *name) {
if (data == nullptr) return nullptr;
SkeletonData *_data = (SkeletonData *) data;
return (spine_path_constraint_data) _data->findPathConstraint(name);
}
spine_physics_constraint_data spine_skeleton_data_find_physics_constraint(spine_skeleton_data data, const utf8 *name) {
if (data == nullptr) return nullptr;
SkeletonData *_data = (SkeletonData *) data;
return (spine_physics_constraint_data) _data->findPhysicsConstraint(name);
}
const utf8 *spine_skeleton_data_get_name(spine_skeleton_data data) {
if (data == nullptr) return nullptr;
SkeletonData *_data = (SkeletonData *) data;
return (utf8 *) _data->getName().buffer();
}
int32_t spine_skeleton_data_get_num_bones(spine_skeleton_data data) {
if (data == nullptr) return 0;
SkeletonData *_data = (SkeletonData *) data;
return (int32_t) _data->getBones().size();
}
spine_bone_data *spine_skeleton_data_get_bones(spine_skeleton_data data) {
if (data == nullptr) return nullptr;
SkeletonData *_data = (SkeletonData *) data;
return (spine_bone_data *) _data->getBones().buffer();
}
int32_t spine_skeleton_data_get_num_slots(spine_skeleton_data data) {
if (data == nullptr) return 0;
SkeletonData *_data = (SkeletonData *) data;
return (int32_t) _data->getSlots().size();
}
spine_slot_data *spine_skeleton_data_get_slots(spine_skeleton_data data) {
if (data == nullptr) return nullptr;
SkeletonData *_data = (SkeletonData *) data;
return (spine_slot_data *) _data->getSlots().buffer();
}
int32_t spine_skeleton_data_get_num_skins(spine_skeleton_data data) {
if (data == nullptr) return 0;
SkeletonData *_data = (SkeletonData *) data;
return (int32_t) _data->getSkins().size();
}
spine_skin *spine_skeleton_data_get_skins(spine_skeleton_data data) {
if (data == nullptr) return nullptr;
SkeletonData *_data = (SkeletonData *) data;
return (spine_skin *) _data->getSkins().buffer();
}
spine_skin spine_skeleton_data_get_default_skin(spine_skeleton_data data) {
if (data == nullptr) return nullptr;
SkeletonData *_data = (SkeletonData *) data;
return (spine_skin) _data->getDefaultSkin();
}
void spine_skeleton_data_set_default_skin(spine_skeleton_data data, spine_skin skin) {
if (data == nullptr) return;
SkeletonData *_data = (SkeletonData *) data;
_data->setDefaultSkin((Skin *) skin);
}
int32_t spine_skeleton_data_get_num_events(spine_skeleton_data data) {
if (data == nullptr) return 0;
SkeletonData *_data = (SkeletonData *) data;
return (int32_t) _data->getEvents().size();
}
spine_event_data *spine_skeleton_data_get_events(spine_skeleton_data data) {
if (data == nullptr) return nullptr;
SkeletonData *_data = (SkeletonData *) data;
return (spine_event_data *) _data->getEvents().buffer();
}
int32_t spine_skeleton_data_get_num_animations(spine_skeleton_data data) {
if (data == nullptr) return 0;
SkeletonData *_data = (SkeletonData *) data;
return (int32_t) _data->getAnimations().size();
}
spine_animation *spine_skeleton_data_get_animations(spine_skeleton_data data) {
if (data == nullptr) return nullptr;
SkeletonData *_data = (SkeletonData *) data;
return (spine_animation *) _data->getAnimations().buffer();
}
int32_t spine_skeleton_data_get_num_ik_constraints(spine_skeleton_data data) {
if (data == nullptr) return 0;
SkeletonData *_data = (SkeletonData *) data;
return (int32_t) _data->getIkConstraints().size();
}
spine_ik_constraint_data *spine_skeleton_data_get_ik_constraints(spine_skeleton_data data) {
if (data == nullptr) return nullptr;
SkeletonData *_data = (SkeletonData *) data;
return (spine_ik_constraint_data *) _data->getIkConstraints().buffer();
}
int32_t spine_skeleton_data_get_num_transform_constraints(spine_skeleton_data data) {
if (data == nullptr) return 0;
SkeletonData *_data = (SkeletonData *) data;
return (int32_t) _data->getTransformConstraints().size();
}
spine_transform_constraint_data *spine_skeleton_data_get_transform_constraints(spine_skeleton_data data) {
if (data == nullptr) return nullptr;
SkeletonData *_data = (SkeletonData *) data;
return (spine_transform_constraint_data *) _data->getTransformConstraints().buffer();
}
int32_t spine_skeleton_data_get_num_path_constraints(spine_skeleton_data data) {
if (data == nullptr) return 0;
SkeletonData *_data = (SkeletonData *) data;
return (int32_t) _data->getPathConstraints().size();
}
spine_path_constraint_data *spine_skeleton_data_get_path_constraints(spine_skeleton_data data) {
if (data == nullptr) return nullptr;
SkeletonData *_data = (SkeletonData *) data;
return (spine_path_constraint_data *) _data->getPathConstraints().buffer();
}
int32_t spine_skeleton_data_get_num_physics_constraints(spine_skeleton_data data) {
if (data == nullptr) return 0;
SkeletonData *_data = (SkeletonData *) data;
return (int32_t) _data->getPhysicsConstraints().size();
}
spine_physics_constraint_data *spine_skeleton_data_get_physics_constraints(spine_skeleton_data data) {
if (data == nullptr) return nullptr;
SkeletonData *_data = (SkeletonData *) data;
return (spine_physics_constraint_data *) _data->getPhysicsConstraints().buffer();
}
float spine_skeleton_data_get_x(spine_skeleton_data data) {
if (data == nullptr) return 0;
SkeletonData *_data = (SkeletonData *) data;
return _data->getX();
}
void spine_skeleton_data_set_x(spine_skeleton_data data, float x) {
if (data == nullptr) return;
SkeletonData *_data = (SkeletonData *) data;
_data->setX(x);
}
float spine_skeleton_data_get_y(spine_skeleton_data data) {
if (data == nullptr) return 0;
SkeletonData *_data = (SkeletonData *) data;
return _data->getY();
}
void spine_skeleton_data_set_y(spine_skeleton_data data, float y) {
if (data == nullptr) return;
SkeletonData *_data = (SkeletonData *) data;
_data->setY(y);
}
float spine_skeleton_data_get_width(spine_skeleton_data data) {
if (data == nullptr) return 0;
SkeletonData *_data = (SkeletonData *) data;
return _data->getWidth();
}
void spine_skeleton_data_set_width(spine_skeleton_data data, float width) {
if (data == nullptr) return;
SkeletonData *_data = (SkeletonData *) data;
_data->setWidth(width);
}
float spine_skeleton_data_get_height(spine_skeleton_data data) {
if (data == nullptr) return 0;
SkeletonData *_data = (SkeletonData *) data;
return _data->getHeight();
}
void spine_skeleton_data_set_height(spine_skeleton_data data, float height) {
if (data == nullptr) return;
SkeletonData *_data = (SkeletonData *) data;
_data->setHeight(height);
}
const utf8 *spine_skeleton_data_get_version(spine_skeleton_data data) {
if (data == nullptr) return nullptr;
SkeletonData *_data = (SkeletonData *) data;
return (utf8 *) _data->getVersion().buffer();
}
const utf8 *spine_skeleton_data_get_hash(spine_skeleton_data data) {
if (data == nullptr) return nullptr;
SkeletonData *_data = (SkeletonData *) data;
return (utf8 *) _data->getHash().buffer();
}
const utf8 *spine_skeleton_data_get_images_path(spine_skeleton_data data) {
if (data == nullptr) return nullptr;
SkeletonData *_data = (SkeletonData *) data;
return (utf8 *) _data->getImagesPath().buffer();
}
const utf8 *spine_skeleton_data_get_audio_path(spine_skeleton_data data) {
if (data == nullptr) return nullptr;
SkeletonData *_data = (SkeletonData *) data;
return (utf8 *) _data->getAudioPath().buffer();
}
float spine_skeleton_data_get_fps(spine_skeleton_data data) {
if (data == nullptr) return 0;
SkeletonData *_data = (SkeletonData *) data;
return _data->getFps();
}
float spine_skeleton_data_get_reference_scale(spine_skeleton_data data) {
if (data == nullptr) return 0;
SkeletonData *_data = (SkeletonData *) data;
return _data->getReferenceScale();
}
void spine_skeleton_data_dispose(spine_skeleton_data data) {
if (!data) return;
delete (SkeletonData *) data;
}
// RenderCommand
static _spine_render_command *spine_render_command_create(BlockAllocator &allocator, int numVertices, int32_t numIndices, spine_blend_mode blendMode, int32_t pageIndex) {
_spine_render_command *cmd = allocator.allocate<_spine_render_command>(1);
cmd->positions = allocator.allocate<float>(numVertices << 1);
cmd->uvs = allocator.allocate<float>(numVertices << 1);
cmd->colors = allocator.allocate<int32_t>(numVertices);
cmd->numVertices = numVertices;
cmd->indices = allocator.allocate<uint16_t>(numIndices);
cmd->numIndices = numIndices;
cmd->blendMode = blendMode;
cmd->atlasPage = pageIndex;
cmd->next = nullptr;
return cmd;
}
// SkeletonDrawable
spine_skeleton_drawable spine_skeleton_drawable_create(spine_skeleton_data skeletonData) {
_spine_skeleton_drawable *drawable = new (__FILE__, __LINE__) _spine_skeleton_drawable();
drawable->skeleton = (spine_skeleton) new (__FILE__, __LINE__) Skeleton((SkeletonData *) skeletonData);
AnimationStateData *stateData = new (__FILE__, __LINE__) AnimationStateData((SkeletonData *) skeletonData);
drawable->animationStateData = (spine_animation_state_data) stateData;
AnimationState *state = new (__FILE__, __LINE__) AnimationState(stateData);
drawable->animationState = (spine_animation_state) state;
state->setManualTrackEntryDisposal(true);
EventListener *listener = new EventListener();
drawable->animationStateEvents = (spine_animation_state_events) listener;
state->setListener(listener);
drawable->clipping = new (__FILE__, __LINE__) SkeletonClipping();
drawable->allocator = new (__FILE__, __LINE__) BlockAllocator(2048);
return (spine_skeleton_drawable) drawable;
}
void spine_skeleton_drawable_dispose(spine_skeleton_drawable drawable) {
_spine_skeleton_drawable *_drawable = (_spine_skeleton_drawable *) drawable;
if (!_drawable) return;
if (_drawable->skeleton) delete (Skeleton *) _drawable->skeleton;
if (_drawable->animationState) delete (AnimationState *) _drawable->animationState;
if (_drawable->animationStateData) delete (AnimationStateData *) _drawable->animationStateData;
if (_drawable->animationStateEvents) delete (Vector<AnimationStateEvent> *) (_drawable->animationStateEvents);
if (_drawable->clipping) delete (SkeletonClipping *) _drawable->clipping;
if (_drawable->allocator) delete (BlockAllocator *) _drawable->allocator;
SpineExtension::free(drawable, __FILE__, __LINE__);
}
static _spine_render_command *batch_sub_commands(BlockAllocator &allocator, Vector<_spine_render_command *> &commands, int first, int last, int numVertices, int numIndices) {
_spine_render_command *batched = spine_render_command_create(allocator, numVertices, numIndices, commands[first]->blendMode, commands[first]->atlasPage);
float *positions = batched->positions;
float *uvs = batched->uvs;
int32_t *colors = batched->colors;
uint16_t *indices = batched->indices;
int indicesOffset = 0;
for (int i = first; i <= last; i++) {
_spine_render_command *cmd = commands[i];
memcpy(positions, cmd->positions, sizeof(float) * 2 * cmd->numVertices);
memcpy(uvs, cmd->uvs, sizeof(float) * 2 * cmd->numVertices);
memcpy(colors, cmd->colors, sizeof(int32_t) * cmd->numVertices);
for (int ii = 0; ii < cmd->numIndices; ii++)
indices[ii] = cmd->indices[ii] + indicesOffset;
indicesOffset += cmd->numVertices;
positions += 2 * cmd->numVertices;
uvs += 2 * cmd->numVertices;
colors += cmd->numVertices;
indices += cmd->numIndices;
}
return batched;
}
static _spine_render_command *batch_commands(BlockAllocator &allocator, Vector<_spine_render_command *> &commands) {
if (commands.size() == 0) return nullptr;
_spine_render_command *root = nullptr;
_spine_render_command *last = nullptr;
_spine_render_command *first = commands[0];
int startIndex = 0;
int i = 1;
int numVertices = first->numVertices;
int numIndices = first->numIndices;
while (i <= (int) commands.size()) {
_spine_render_command *cmd = i < (int) commands.size() ? commands[i] : nullptr;
if (cmd != nullptr && cmd->atlasPage == first->atlasPage &&
cmd->blendMode == first->blendMode &&
cmd->colors[0] == first->colors[0] &&
numIndices + cmd->numIndices < 0xffff) {
numVertices += cmd->numVertices;
numIndices += cmd->numIndices;
} else {
_spine_render_command *batched = batch_sub_commands(allocator, commands, startIndex, i - 1, numVertices, numIndices);
if (!last) {
root = last = batched;
} else {
last->next = batched;
last = batched;
}
if (i == (int) commands.size()) break;
first = commands[i];
startIndex = i;
numVertices = first->numVertices;
numIndices = first->numIndices;
}
i++;
}
return root;
}
spine_render_command spine_skeleton_drawable_render(spine_skeleton_drawable drawable) {
_spine_skeleton_drawable *_drawable = (_spine_skeleton_drawable *) drawable;
if (!_drawable) return nullptr;
if (!_drawable->skeleton) return nullptr;
_drawable->allocator->compress();
_drawable->renderCommands.clear();
SkeletonClipping &clipper = *(SkeletonClipping *) _drawable->clipping;
Skeleton *skeleton = (Skeleton *) _drawable->skeleton;
for (unsigned i = 0; i < skeleton->getSlots().size(); ++i) {
Slot &slot = *skeleton->getDrawOrder()[i];
Attachment *attachment = slot.getAttachment();
if (!attachment) {
clipper.clipEnd(slot);
continue;
}
// Early out if the slot color is 0 or the bone is not active
if (slot.getColor().a == 0 || !slot.getBone().isActive()) {
clipper.clipEnd(slot);
continue;
}
Vector<float> *worldVertices = &_drawable->worldVertices;
Vector<unsigned short> *quadIndices = &_drawable->quadIndices;
Vector<float> *vertices = worldVertices;
int32_t verticesCount;
Vector<float> *uvs;
Vector<unsigned short> *indices;
int32_t indicesCount;
Color *attachmentColor;
int32_t pageIndex;
if (attachment->getRTTI().isExactly(RegionAttachment::rtti)) {
RegionAttachment *regionAttachment = (RegionAttachment *) attachment;
attachmentColor = ®ionAttachment->getColor();
// Early out if the slot color is 0
if (attachmentColor->a == 0) {
clipper.clipEnd(slot);
continue;
}
worldVertices->setSize(8, 0);
regionAttachment->computeWorldVertices(slot, *worldVertices, 0, 2);
verticesCount = 4;
uvs = ®ionAttachment->getUVs();
indices = quadIndices;
indicesCount = 6;
pageIndex = ((AtlasRegion *) regionAttachment->getRegion())->page->index;
} else if (attachment->getRTTI().isExactly(MeshAttachment::rtti)) {
MeshAttachment *mesh = (MeshAttachment *) attachment;
attachmentColor = &mesh->getColor();
// Early out if the slot color is 0
if (attachmentColor->a == 0) {
clipper.clipEnd(slot);
continue;
}
worldVertices->setSize(mesh->getWorldVerticesLength(), 0);
mesh->computeWorldVertices(slot, 0, mesh->getWorldVerticesLength(), worldVertices->buffer(), 0, 2);
verticesCount = (int32_t) (mesh->getWorldVerticesLength() >> 1);
uvs = &mesh->getUVs();
indices = &mesh->getTriangles();
indicesCount = (int32_t) indices->size();
pageIndex = ((AtlasRegion *) mesh->getRegion())->page->index;
} else if (attachment->getRTTI().isExactly(ClippingAttachment::rtti)) {
ClippingAttachment *clip = (ClippingAttachment *) slot.getAttachment();
clipper.clipStart(slot, clip);
continue;
} else
continue;
uint8_t r = static_cast<uint8_t>(skeleton->getColor().r * slot.getColor().r * attachmentColor->r * 255);
uint8_t g = static_cast<uint8_t>(skeleton->getColor().g * slot.getColor().g * attachmentColor->g * 255);
uint8_t b = static_cast<uint8_t>(skeleton->getColor().b * slot.getColor().b * attachmentColor->b * 255);
uint8_t a = static_cast<uint8_t>(skeleton->getColor().a * slot.getColor().a * attachmentColor->a * 255);
uint32_t color = (a << 24) | (r << 16) | (g << 8) | b;
if (clipper.isClipping()) {
clipper.clipTriangles(*worldVertices, *indices, *uvs, 2);
vertices = &clipper.getClippedVertices();
verticesCount = (int32_t) (clipper.getClippedVertices().size() >> 1);
uvs = &clipper.getClippedUVs();
indices = &clipper.getClippedTriangles();
indicesCount = (int32_t) (clipper.getClippedTriangles().size());
}
_spine_render_command *cmd = spine_render_command_create(*_drawable->allocator, verticesCount, indicesCount, (spine_blend_mode) slot.getData().getBlendMode(), pageIndex);
_drawable->renderCommands.add(cmd);
memcpy(cmd->positions, vertices->buffer(), (verticesCount << 1) * sizeof(float));
memcpy(cmd->uvs, uvs->buffer(), (verticesCount << 1) * sizeof(float));
for (int ii = 0; ii < verticesCount; ii++) cmd->colors[ii] = color;
memcpy(cmd->indices, indices->buffer(), indices->size() * sizeof(uint16_t));
clipper.clipEnd(slot);
}
clipper.clipEnd();
return (spine_render_command) batch_commands(*_drawable->allocator, _drawable->renderCommands);
}
spine_skeleton spine_skeleton_drawable_get_skeleton(spine_skeleton_drawable drawable) {
if (!drawable) return nullptr;
return ((_spine_skeleton_drawable *) drawable)->skeleton;
}
spine_animation_state spine_skeleton_drawable_get_animation_state(spine_skeleton_drawable drawable) {
if (!drawable) return nullptr;
return ((_spine_skeleton_drawable *) drawable)->animationState;
}
spine_animation_state_data spine_skeleton_drawable_get_animation_state_data(spine_skeleton_drawable drawable) {
if (!drawable) return nullptr;
return ((_spine_skeleton_drawable *) drawable)->animationStateData;
}
spine_animation_state_events spine_skeleton_drawable_get_animation_state_events(spine_skeleton_drawable drawable) {
if (!drawable) return nullptr;
return ((_spine_skeleton_drawable *) drawable)->animationStateEvents;
}
// Render command
float *spine_render_command_get_positions(spine_render_command command) {
if (!command) return nullptr;
return ((_spine_render_command *) command)->positions;
}
float *spine_render_command_get_uvs(spine_render_command command) {
if (!command) return nullptr;
return ((_spine_render_command *) command)->uvs;
}
int32_t *spine_render_command_get_colors(spine_render_command command) {
if (!command) return nullptr;
return ((_spine_render_command *) command)->colors;
}
int32_t spine_render_command_get_num_vertices(spine_render_command command) {
if (!command) return 0;
return ((_spine_render_command *) command)->numVertices;
}
uint16_t *spine_render_command_get_indices(spine_render_command command) {
if (!command) return nullptr;
return ((_spine_render_command *) command)->indices;
}
int32_t spine_render_command_get_num_indices(spine_render_command command) {
if (!command) return 0;
return ((_spine_render_command *) command)->numIndices;
}
int32_t spine_render_command_get_atlas_page(spine_render_command command) {
if (!command) return 0;
return ((_spine_render_command *) command)->atlasPage;
}
spine_blend_mode spine_render_command_get_blend_mode(spine_render_command command) {
if (!command) return SPINE_BLEND_MODE_NORMAL;
return ((_spine_render_command *) command)->blendMode;
}
spine_render_command spine_render_command_get_next(spine_render_command command) {
if (!command) return nullptr;
return (spine_render_command) ((_spine_render_command *) command)->next;
}
// Animation
const utf8 *spine_animation_get_name(spine_animation animation) {
if (animation == nullptr) return nullptr;
Animation *_animation = (Animation *) animation;
return (utf8 *) _animation->getName().buffer();
}
float spine_animation_get_duration(spine_animation animation) {
if (animation == nullptr) return 0;
Animation *_animation = (Animation *) animation;
return _animation->getDuration();
}
// AnimationStateData
spine_skeleton_data spine_animation_state_data_get_skeleton_data(spine_animation_state_data stateData) {
if (stateData == nullptr) return nullptr;
AnimationStateData *_stateData = (AnimationStateData *) stateData;
return (spine_skeleton_data) _stateData->getSkeletonData();
}
float spine_animation_state_data_get_default_mix(spine_animation_state_data stateData) {
if (stateData == nullptr) return 0;
AnimationStateData *_stateData = (AnimationStateData *) stateData;
return _stateData->getDefaultMix();
}
void spine_animation_state_data_set_default_mix(spine_animation_state_data stateData, float defaultMix) {
if (stateData == nullptr) return;
AnimationStateData *_stateData = (AnimationStateData *) stateData;
_stateData->setDefaultMix(defaultMix);
}
void spine_animation_state_data_set_mix(spine_animation_state_data stateData, spine_animation from, spine_animation to, float duration) {
if (stateData == nullptr) return;
if (from == nullptr || to == nullptr) return;
AnimationStateData *_stateData = (AnimationStateData *) stateData;
_stateData->setMix((Animation *) from, (Animation *) to, duration);
}
float spine_animation_state_data_get_mix(spine_animation_state_data stateData, spine_animation from, spine_animation to) {
if (stateData == nullptr) return 0;
if (from == nullptr || to == nullptr) return 0;
AnimationStateData *_stateData = (AnimationStateData *) stateData;
return _stateData->getMix((Animation *) from, (Animation *) to);
}
void spine_animation_state_data_set_mix_by_name(spine_animation_state_data stateData, const utf8 *fromName, const utf8 *toName, float duration) {
if (stateData == nullptr) return;
if (fromName == nullptr || toName == nullptr) return;
AnimationStateData *_stateData = (AnimationStateData *) stateData;
_stateData->setMix(fromName, toName, duration);
}
float spine_animation_state_data_get_mix_by_name(spine_animation_state_data stateData, const utf8 *fromName, const utf8 *toName) {
if (stateData == nullptr) return 0;
AnimationStateData *_stateData = (AnimationStateData *) stateData;
Animation *from = _stateData->getSkeletonData()->findAnimation(fromName);
Animation *to = _stateData->getSkeletonData()->findAnimation(toName);
if (from == nullptr || to == nullptr) return 0;
return _stateData->getMix(from, to);
}
void spine_animation_state_data_clear(spine_animation_state_data stateData) {
if (stateData == nullptr) return;
AnimationStateData *_stateData = (AnimationStateData *) stateData;
_stateData->clear();
}
// AnimationState
void spine_animation_state_update(spine_animation_state state, float delta) {
if (state == nullptr) return;
AnimationState *_state = (AnimationState *) state;
_state->update(delta);
}
void spine_animation_state_dispose_track_entry(spine_animation_state state, spine_track_entry entry) {
if (state == nullptr) return;
if (entry == nullptr) return;
AnimationState *_state = (AnimationState *) state;
_state->disposeTrackEntry((TrackEntry *) entry);
}