C_InterfaceTestFixture.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include "C_InterfaceTestFixture.h"
  2. #include "SpineEventMonitor.h"
  3. #include "spine/spine.h"
  4. #include <vector>
  5. #include "KMemory.h" // last include
  6. #define SPINEBOY_JSON "testdata/spineboy/spineboy-ess.json"
  7. #define SPINEBOY_ATLAS "testdata/spineboy/spineboy.atlas"
  8. #define RAPTOR_JSON "testdata/raptor/raptor-pro.json"
  9. #define RAPTOR_ATLAS "testdata/raptor/raptor.atlas"
  10. #define GOBLINS_JSON "testdata/goblins/goblins-pro.json"
  11. #define GOBLINS_ATLAS "testdata/goblins/goblins.atlas"
  12. #define MAX_RUN_TIME 6000 // equal to about 100 seconds of execution
  13. void C_InterfaceTestFixture::setUp() {
  14. }
  15. void C_InterfaceTestFixture::tearDown() {
  16. }
  17. static spSkeletonData *readSkeletonJsonData(const char *filename, spAtlas *atlas) {
  18. spSkeletonJson *json = spSkeletonJson_create(atlas);
  19. ASSERT(json != 0);
  20. spSkeletonData *skeletonData = spSkeletonJson_readSkeletonDataFile(json, filename);
  21. ASSERT(skeletonData != 0);
  22. spSkeletonJson_dispose(json);
  23. return skeletonData;
  24. }
  25. typedef std::vector<std::string> AnimList;
  26. static size_t enumerateAnimations(AnimList &outList, spSkeletonData *skeletonData) {
  27. if (skeletonData) {
  28. for (int n = 0; n < skeletonData->animationsCount; n++)
  29. outList.push_back(skeletonData->animations[n]->name);
  30. }
  31. return outList.size();
  32. }
  33. static void testRunner(const char *jsonName, const char *atlasName) {
  34. ///////////////////////////////////////////////////////////////////////////
  35. // Global Animation Information
  36. spAtlas *atlas = spAtlas_createFromFile(atlasName, 0);
  37. ASSERT(atlas != 0);
  38. spSkeletonData *skeletonData = readSkeletonJsonData(jsonName, atlas);
  39. ASSERT(skeletonData != 0);
  40. spAnimationStateData *stateData = spAnimationStateData_create(skeletonData);
  41. ASSERT(stateData != 0);
  42. stateData->defaultMix = 0.2f; // force mixing
  43. ///////////////////////////////////////////////////////////////////////////
  44. // Animation Instance
  45. spSkeleton *skeleton = spSkeleton_create(skeletonData);
  46. ASSERT(skeleton != 0);
  47. spAnimationState *state = spAnimationState_create(stateData);
  48. ASSERT(state != 0);
  49. ///////////////////////////////////////////////////////////////////////////
  50. // Run animation
  51. spSkeleton_setToSetupPose(skeleton);
  52. SpineEventMonitor eventMonitor(state);
  53. // eventMonitor.SetDebugLogging(true);
  54. AnimList anims; // Let's chain all the animations together as a test
  55. size_t count = enumerateAnimations(anims, skeletonData);
  56. if (count > 0) spAnimationState_setAnimationByName(state, 0, anims[0].c_str(), false);
  57. for (size_t i = 1; i < count; ++i) {
  58. spAnimationState_addAnimationByName(state, 0, anims[i].c_str(), false, 0.0f);
  59. }
  60. // Run Loop
  61. for (int i = 0; i < MAX_RUN_TIME && eventMonitor.isAnimationPlaying(); ++i) {
  62. const float timeSlice = 1.0f / 60.0f;
  63. spSkeleton_update(skeleton, timeSlice);
  64. spAnimationState_update(state, timeSlice);
  65. spAnimationState_apply(state, skeleton);
  66. }
  67. ///////////////////////////////////////////////////////////////////////////
  68. // Dispose Instance
  69. spSkeleton_dispose(skeleton);
  70. spAnimationState_dispose(state);
  71. ///////////////////////////////////////////////////////////////////////////
  72. // Dispose Global
  73. spAnimationStateData_dispose(stateData);
  74. spSkeletonData_dispose(skeletonData);
  75. spAtlas_dispose(atlas);
  76. }
  77. void C_InterfaceTestFixture::spineboyTestCase() {
  78. testRunner(SPINEBOY_JSON, SPINEBOY_ATLAS);
  79. }
  80. void C_InterfaceTestFixture::raptorTestCase() {
  81. testRunner(RAPTOR_JSON, RAPTOR_ATLAS);
  82. }
  83. void C_InterfaceTestFixture::goblinsTestCase() {
  84. testRunner(GOBLINS_JSON, GOBLINS_ATLAS);
  85. }