diff --git a/spine-sdl/example/main.c b/spine-sdl/example/main.c index 07cafe8f2..0dcf11ca5 100644 --- a/spine-sdl/example/main.c +++ b/spine-sdl/example/main.c @@ -31,58 +31,59 @@ #include int main() { - if (SDL_Init(SDL_INIT_VIDEO)) { - printf("Error: %s", SDL_GetError()); - return -1; - } - SDL_Window *window = SDL_CreateWindow("Spine SDL", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, 0); - if (!window) { - printf("Error: %s", SDL_GetError()); - return -1; - } - SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); - if (!renderer) { - printf("Error: %s", SDL_GetError()); - return -1; - } + if (SDL_Init(SDL_INIT_VIDEO)) { + printf("Error: %s", SDL_GetError()); + return -1; + } + SDL_Window *window = SDL_CreateWindow("Spine SDL", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, 0); + if (!window) { + printf("Error: %s", SDL_GetError()); + return -1; + } + SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); + if (!renderer) { + printf("Error: %s", SDL_GetError()); + return -1; + } - spAtlas *atlas = spAtlas_createFromFile("/Users/badlogic/workspaces/spine-runtimes/examples/spineboy/export/spineboy.atlas", renderer); - spSkeletonJson *json = spSkeletonJson_create(atlas); - json->scale = 0.5f; - spSkeletonData *skeletonData = spSkeletonJson_readSkeletonDataFile(json, "/Users/badlogic/workspaces/spine-runtimes/examples/spineboy/export/spineboy-pro.json"); - spAnimationStateData *animationStateData = spAnimationStateData_create(skeletonData); - spSkeletonDrawable *drawable = spSkeletonDrawable_create(skeletonData, animationStateData); + spAtlas *atlas = spAtlas_createFromFile("/Users/badlogic/workspaces/spine-runtimes/examples/spineboy/export/spineboy.atlas", renderer); + spSkeletonJson *json = spSkeletonJson_create(atlas); + json->scale = 0.5f; + spSkeletonData *skeletonData = spSkeletonJson_readSkeletonDataFile(json, "/Users/badlogic/workspaces/spine-runtimes/examples/spineboy/export/spineboy-pro.json"); + spAnimationStateData *animationStateData = spAnimationStateData_create(skeletonData); + spSkeletonDrawable *drawable = spSkeletonDrawable_create(skeletonData, animationStateData); - drawable->skeleton->x = 400; drawable->skeleton->y = 500; - spSkeleton_setToSetupPose(drawable->skeleton); - spSkeletonDrawable_update(drawable, 0); - spAnimationState_setAnimationByName(drawable->animationState, 0, "run", -1); + drawable->skeleton->x = 400; + drawable->skeleton->y = 500; + spSkeleton_setToSetupPose(drawable->skeleton); + spSkeletonDrawable_update(drawable, 0); + spAnimationState_setAnimationByName(drawable->animationState, 0, "run", -1); - int quit = 0; - uint64_t lastFrameTime = SDL_GetPerformanceCounter(); - while (!quit) { - SDL_Event event; - while (SDL_PollEvent(&event) != 0) { - if (event.type == SDL_QUIT) { - quit = -1; - break; - } - } + int quit = 0; + uint64_t lastFrameTime = SDL_GetPerformanceCounter(); + while (!quit) { + SDL_Event event; + while (SDL_PollEvent(&event) != 0) { + if (event.type == SDL_QUIT) { + quit = -1; + break; + } + } - SDL_SetRenderDrawColor(renderer, 94, 93, 96, 255); - SDL_RenderClear(renderer); + SDL_SetRenderDrawColor(renderer, 94, 93, 96, 255); + SDL_RenderClear(renderer); - uint64_t now = SDL_GetPerformanceCounter(); - double deltaTime = (now - lastFrameTime) / (double) SDL_GetPerformanceFrequency(); - lastFrameTime = now; + uint64_t now = SDL_GetPerformanceCounter(); + double deltaTime = (now - lastFrameTime) / (double) SDL_GetPerformanceFrequency(); + lastFrameTime = now; - spSkeletonDrawable_update(drawable, deltaTime); - spSkeletonDrawable_draw(drawable, renderer); + spSkeletonDrawable_update(drawable, deltaTime); + spSkeletonDrawable_draw(drawable, renderer); - SDL_RenderPresent(renderer); - } + SDL_RenderPresent(renderer); + } - SDL_DestroyWindow(window); - SDL_Quit(); - return 0; + SDL_DestroyWindow(window); + SDL_Quit(); + return 0; } diff --git a/spine-sdl/src/spine-sdl-c.c b/spine-sdl/src/spine-sdl-c.c index fb485d6a5..4f99357bb 100644 --- a/spine-sdl/src/spine-sdl-c.c +++ b/spine-sdl/src/spine-sdl-c.c @@ -39,173 +39,173 @@ _SP_ARRAY_IMPLEMENT_TYPE_NO_CONTAINS(spSdlVertexArray, SDL_Vertex) spSkeletonDrawable *spSkeletonDrawable_create(spSkeletonData *skeletonData, spAnimationStateData *animationStateData) { - spBone_setYDown(-1); - spSkeletonDrawable *self = NEW(spSkeletonDrawable); - self->skeleton = spSkeleton_create(skeletonData); - self->animationState = spAnimationState_create(animationStateData); - self->sdlIndices = spIntArray_create(12); - self->sdlVertices = spSdlVertexArray_create(12); - self->worldVertices = spFloatArray_create(12); - self->clipper = spSkeletonClipping_create(); - return self; + spBone_setYDown(-1); + spSkeletonDrawable *self = NEW(spSkeletonDrawable); + self->skeleton = spSkeleton_create(skeletonData); + self->animationState = spAnimationState_create(animationStateData); + self->sdlIndices = spIntArray_create(12); + self->sdlVertices = spSdlVertexArray_create(12); + self->worldVertices = spFloatArray_create(12); + self->clipper = spSkeletonClipping_create(); + return self; } void spSkeletonDrawable_dispose(spSkeletonDrawable *self) { - spSkeleton_dispose(self->skeleton); - spAnimationState_dispose(self->animationState); - spIntArray_dispose(self->sdlIndices); - spSdlVertexArray_dispose(self->sdlVertices); - spFloatArray_dispose(self->worldVertices); - spSkeletonClipping_dispose(self->clipper); - FREE(self); + spSkeleton_dispose(self->skeleton); + spAnimationState_dispose(self->animationState); + spIntArray_dispose(self->sdlIndices); + spSdlVertexArray_dispose(self->sdlVertices); + spFloatArray_dispose(self->worldVertices); + spSkeletonClipping_dispose(self->clipper); + FREE(self); } void spSkeletonDrawable_update(spSkeletonDrawable *self, float delta) { - spAnimationState_update(self->animationState, delta); - spAnimationState_apply(self->animationState, self->skeleton); - spSkeleton_updateWorldTransform(self->skeleton); + spAnimationState_update(self->animationState, delta); + spAnimationState_apply(self->animationState, self->skeleton); + spSkeleton_updateWorldTransform(self->skeleton); } void spSkeletonDrawable_draw(spSkeletonDrawable *self, struct SDL_Renderer *renderer) { - static unsigned short quadIndices[] = { 0, 1, 2, 2, 3, 0 }; - spSkeleton *skeleton = self->skeleton; - spSkeletonClipping *clipper = self->clipper; - SDL_Texture *texture; - SDL_Vertex sdlVertex; - for (int i = 0; i < skeleton->slotsCount; ++i) { - spSlot *slot = skeleton->drawOrder[i]; - spAttachment *attachment = slot->attachment; - if (!attachment) continue; + static unsigned short quadIndices[] = {0, 1, 2, 2, 3, 0}; + spSkeleton *skeleton = self->skeleton; + spSkeletonClipping *clipper = self->clipper; + SDL_Texture *texture; + SDL_Vertex sdlVertex; + for (int i = 0; i < skeleton->slotsCount; ++i) { + spSlot *slot = skeleton->drawOrder[i]; + spAttachment *attachment = slot->attachment; + if (!attachment) continue; - // Early out if the slot color is 0 or the bone is not active - if (slot->color.a == 0 || !slot->bone->active) { - spSkeletonClipping_clipEnd(clipper, slot); - continue; - } + // Early out if the slot color is 0 or the bone is not active + if (slot->color.a == 0 || !slot->bone->active) { + spSkeletonClipping_clipEnd(clipper, slot); + continue; + } - spFloatArray *vertices = self->worldVertices; - int verticesCount = 0; - float *uvs = NULL; - unsigned short *indices; - int indicesCount = 0; - spColor *attachmentColor = NULL; + spFloatArray *vertices = self->worldVertices; + int verticesCount = 0; + float *uvs = NULL; + unsigned short *indices; + int indicesCount = 0; + spColor *attachmentColor = NULL; - if (attachment->type == SP_ATTACHMENT_REGION) { - spRegionAttachment *region = (spRegionAttachment *) attachment; - attachmentColor = ®ion->color; + if (attachment->type == SP_ATTACHMENT_REGION) { + spRegionAttachment *region = (spRegionAttachment *) attachment; + attachmentColor = ®ion->color; - // Early out if the slot color is 0 - if (attachmentColor->a == 0) { - spSkeletonClipping_clipEnd(clipper, slot); - continue; - } + // Early out if the slot color is 0 + if (attachmentColor->a == 0) { + spSkeletonClipping_clipEnd(clipper, slot); + continue; + } - spFloatArray_setSize(vertices, 8); - spRegionAttachment_computeWorldVertices(region, slot, vertices->items, 0, 2); - verticesCount = 4; - uvs = region->uvs; - indices = quadIndices; - indicesCount = 6; - texture = (SDL_Texture *) ((spAtlasRegion*)region->rendererObject)->page->rendererObject; - } else if (attachment->type == SP_ATTACHMENT_MESH) { - spMeshAttachment *mesh = (spMeshAttachment *) attachment; - attachmentColor = &mesh->color; + spFloatArray_setSize(vertices, 8); + spRegionAttachment_computeWorldVertices(region, slot, vertices->items, 0, 2); + verticesCount = 4; + uvs = region->uvs; + indices = quadIndices; + indicesCount = 6; + texture = (SDL_Texture *) ((spAtlasRegion *) region->rendererObject)->page->rendererObject; + } else if (attachment->type == SP_ATTACHMENT_MESH) { + spMeshAttachment *mesh = (spMeshAttachment *) attachment; + attachmentColor = &mesh->color; - // Early out if the slot color is 0 - if (attachmentColor->a == 0) { - spSkeletonClipping_clipEnd(clipper, slot); - continue; - } + // Early out if the slot color is 0 + if (attachmentColor->a == 0) { + spSkeletonClipping_clipEnd(clipper, slot); + continue; + } - spFloatArray_setSize(vertices, mesh->super.worldVerticesLength); - spVertexAttachment_computeWorldVertices(SUPER(mesh), slot, 0, mesh->super.worldVerticesLength, vertices->items, 0, 2); - verticesCount = mesh->super.worldVerticesLength >> 1; - uvs = mesh->uvs; - indices = mesh->triangles; - indicesCount = mesh->trianglesCount; - texture = (SDL_Texture *) ((spAtlasRegion*)mesh->rendererObject)->page->rendererObject; - } else if (attachment->type == SP_ATTACHMENT_CLIPPING) { - spClippingAttachment *clip = (spClippingAttachment *) slot->attachment; - spSkeletonClipping_clipStart(clipper, slot, clip); - continue; - } else - continue; + spFloatArray_setSize(vertices, mesh->super.worldVerticesLength); + spVertexAttachment_computeWorldVertices(SUPER(mesh), slot, 0, mesh->super.worldVerticesLength, vertices->items, 0, 2); + verticesCount = mesh->super.worldVerticesLength >> 1; + uvs = mesh->uvs; + indices = mesh->triangles; + indicesCount = mesh->trianglesCount; + texture = (SDL_Texture *) ((spAtlasRegion *) mesh->rendererObject)->page->rendererObject; + } else if (attachment->type == SP_ATTACHMENT_CLIPPING) { + spClippingAttachment *clip = (spClippingAttachment *) slot->attachment; + spSkeletonClipping_clipStart(clipper, slot, clip); + continue; + } else + continue; - Uint8 r = (Uint8)(skeleton->color.r * slot->color.r * attachmentColor->r * 255); - Uint8 g = (Uint8)(skeleton->color.g * slot->color.g * attachmentColor->g * 255); - Uint8 b = (Uint8)(skeleton->color.b * slot->color.b * attachmentColor->b * 255); - Uint8 a = (Uint8)(skeleton->color.a * slot->color.a * attachmentColor->a * 255); - sdlVertex.color.r = r; - sdlVertex.color.g = g; - sdlVertex.color.b = b; - sdlVertex.color.a = a; + Uint8 r = (Uint8) (skeleton->color.r * slot->color.r * attachmentColor->r * 255); + Uint8 g = (Uint8) (skeleton->color.g * slot->color.g * attachmentColor->g * 255); + Uint8 b = (Uint8) (skeleton->color.b * slot->color.b * attachmentColor->b * 255); + Uint8 a = (Uint8) (skeleton->color.a * slot->color.a * attachmentColor->a * 255); + sdlVertex.color.r = r; + sdlVertex.color.g = g; + sdlVertex.color.b = b; + sdlVertex.color.a = a; - if (spSkeletonClipping_isClipping(clipper)) { - spSkeletonClipping_clipTriangles(clipper, vertices->items, verticesCount << 1, indices, indicesCount, uvs, 2); - vertices = clipper->clippedVertices; - verticesCount = clipper->clippedVertices->size >> 1; - uvs = clipper->clippedUVs->items; - indices = clipper->clippedTriangles->items; - indicesCount = clipper->clippedTriangles->size; - } + if (spSkeletonClipping_isClipping(clipper)) { + spSkeletonClipping_clipTriangles(clipper, vertices->items, verticesCount << 1, indices, indicesCount, uvs, 2); + vertices = clipper->clippedVertices; + verticesCount = clipper->clippedVertices->size >> 1; + uvs = clipper->clippedUVs->items; + indices = clipper->clippedTriangles->items; + indicesCount = clipper->clippedTriangles->size; + } - spSdlVertexArray_clear(self->sdlVertices); - for (int ii = 0; ii < verticesCount << 1; ii += 2) { - sdlVertex.position.x = vertices->items[ii]; - sdlVertex.position.y = vertices->items[ii + 1]; - sdlVertex.tex_coord.x = uvs[ii]; - sdlVertex.tex_coord.y = uvs[ii + 1]; - spSdlVertexArray_add(self->sdlVertices, sdlVertex); - } - spIntArray_clear(self->sdlIndices); - for (int ii = 0; ii < (int) indicesCount; ii++) - spIntArray_add(self->sdlIndices, indices[ii]); + spSdlVertexArray_clear(self->sdlVertices); + for (int ii = 0; ii < verticesCount << 1; ii += 2) { + sdlVertex.position.x = vertices->items[ii]; + sdlVertex.position.y = vertices->items[ii + 1]; + sdlVertex.tex_coord.x = uvs[ii]; + sdlVertex.tex_coord.y = uvs[ii + 1]; + spSdlVertexArray_add(self->sdlVertices, sdlVertex); + } + spIntArray_clear(self->sdlIndices); + for (int ii = 0; ii < (int) indicesCount; ii++) + spIntArray_add(self->sdlIndices, indices[ii]); - switch (slot->data->blendMode) { - case SP_BLEND_MODE_NORMAL: - SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND); - break; - case SP_BLEND_MODE_MULTIPLY: - SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_MOD); - break; - case SP_BLEND_MODE_ADDITIVE: - SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_ADD); - break; - case SP_BLEND_MODE_SCREEN: - SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND); - break; - } + switch (slot->data->blendMode) { + case SP_BLEND_MODE_NORMAL: + SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND); + break; + case SP_BLEND_MODE_MULTIPLY: + SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_MOD); + break; + case SP_BLEND_MODE_ADDITIVE: + SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_ADD); + break; + case SP_BLEND_MODE_SCREEN: + SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND); + break; + } - SDL_RenderGeometry(renderer, texture, self->sdlVertices->items, self->sdlVertices->size, self->sdlIndices->items, - indicesCount); - spSkeletonClipping_clipEnd(clipper, slot); - } - spSkeletonClipping_clipEnd2(clipper); + SDL_RenderGeometry(renderer, texture, self->sdlVertices->items, self->sdlVertices->size, self->sdlIndices->items, + indicesCount); + spSkeletonClipping_clipEnd(clipper, slot); + } + spSkeletonClipping_clipEnd2(clipper); } void _spAtlasPage_createTexture(spAtlasPage *self, const char *path) { - int width, height, components; - stbi_uc *imageData = stbi_load(path, &width, &height, &components, 4); - if (!imageData) return; - SDL_Texture *texture = SDL_CreateTexture((SDL_Renderer*)self->atlas->rendererObject, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STATIC, width, - height); - if (!texture) { - stbi_image_free(imageData); - return; - } - if (SDL_UpdateTexture(texture, NULL, imageData, width * 4)) { - stbi_image_free(imageData); - return; - } - stbi_image_free(imageData); - self->rendererObject = texture; - return; + int width, height, components; + stbi_uc *imageData = stbi_load(path, &width, &height, &components, 4); + if (!imageData) return; + SDL_Texture *texture = SDL_CreateTexture((SDL_Renderer *) self->atlas->rendererObject, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STATIC, width, + height); + if (!texture) { + stbi_image_free(imageData); + return; + } + if (SDL_UpdateTexture(texture, NULL, imageData, width * 4)) { + stbi_image_free(imageData); + return; + } + stbi_image_free(imageData); + self->rendererObject = texture; + return; } void _spAtlasPage_disposeTexture(spAtlasPage *self) { - SDL_DestroyTexture((SDL_Texture *) self->rendererObject); + SDL_DestroyTexture((SDL_Texture *) self->rendererObject); } char *_spUtil_readFile(const char *path, int *length) { - return _spReadFile(path, length); + return _spReadFile(path, length); } diff --git a/spine-sdl/src/spine-sdl-c.h b/spine-sdl/src/spine-sdl-c.h index 4b3976c1c..b8cbf2b63 100644 --- a/spine-sdl/src/spine-sdl-c.h +++ b/spine-sdl/src/spine-sdl-c.h @@ -42,13 +42,13 @@ struct SDL_Vertex; _SP_ARRAY_DECLARE_TYPE(spSdlVertexArray, struct SDL_Vertex) typedef struct spSkeletonDrawable { - spSkeleton *skeleton; - spAnimationState *animationState; + spSkeleton *skeleton; + spAnimationState *animationState; - spSkeletonClipping *clipper; - spFloatArray *worldVertices; - spSdlVertexArray *sdlVertices; - spIntArray *sdlIndices; + spSkeletonClipping *clipper; + spFloatArray *worldVertices; + spSdlVertexArray *sdlVertices; + spIntArray *sdlIndices; } spSkeletonDrawable; SP_API spSkeletonDrawable *spSkeletonDrawable_create(spSkeletonData *skeletonData, spAnimationStateData *animationStateData); diff --git a/spine-sdl/src/spine-sdl-cpp.cpp b/spine-sdl/src/spine-sdl-cpp.cpp index 321aad8af..365abf221 100644 --- a/spine-sdl/src/spine-sdl-cpp.cpp +++ b/spine-sdl/src/spine-sdl-cpp.cpp @@ -58,13 +58,13 @@ void SkeletonDrawable::update(float delta) { } void SkeletonDrawable::draw(SDL_Renderer *renderer) { - Vector quadIndices; - quadIndices.add(0); - quadIndices.add(1); - quadIndices.add(2); - quadIndices.add(2); - quadIndices.add(3); - quadIndices.add(0); + Vector quadIndices; + quadIndices.add(0); + quadIndices.add(1); + quadIndices.add(2); + quadIndices.add(2); + quadIndices.add(3); + quadIndices.add(0); SDL_Texture *texture; SDL_Vertex sdlVertex; for (unsigned i = 0; i < skeleton->getSlots().size(); ++i) {