[sdl] Formatting pass.

This commit is contained in:
Mario Zechner 2022-08-02 10:46:28 +02:00
parent 2b69bb2af5
commit f3e03467b5
4 changed files with 199 additions and 198 deletions

View File

@ -31,58 +31,59 @@
#include <SDL.h> #include <SDL.h>
int main() { int main() {
if (SDL_Init(SDL_INIT_VIDEO)) { if (SDL_Init(SDL_INIT_VIDEO)) {
printf("Error: %s", SDL_GetError()); printf("Error: %s", SDL_GetError());
return -1; return -1;
} }
SDL_Window *window = SDL_CreateWindow("Spine SDL", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, 0); SDL_Window *window = SDL_CreateWindow("Spine SDL", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, 0);
if (!window) { if (!window) {
printf("Error: %s", SDL_GetError()); printf("Error: %s", SDL_GetError());
return -1; return -1;
} }
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (!renderer) { if (!renderer) {
printf("Error: %s", SDL_GetError()); printf("Error: %s", SDL_GetError());
return -1; return -1;
} }
spAtlas *atlas = spAtlas_createFromFile("/Users/badlogic/workspaces/spine-runtimes/examples/spineboy/export/spineboy.atlas", renderer); spAtlas *atlas = spAtlas_createFromFile("/Users/badlogic/workspaces/spine-runtimes/examples/spineboy/export/spineboy.atlas", renderer);
spSkeletonJson *json = spSkeletonJson_create(atlas); spSkeletonJson *json = spSkeletonJson_create(atlas);
json->scale = 0.5f; json->scale = 0.5f;
spSkeletonData *skeletonData = spSkeletonJson_readSkeletonDataFile(json, "/Users/badlogic/workspaces/spine-runtimes/examples/spineboy/export/spineboy-pro.json"); spSkeletonData *skeletonData = spSkeletonJson_readSkeletonDataFile(json, "/Users/badlogic/workspaces/spine-runtimes/examples/spineboy/export/spineboy-pro.json");
spAnimationStateData *animationStateData = spAnimationStateData_create(skeletonData); spAnimationStateData *animationStateData = spAnimationStateData_create(skeletonData);
spSkeletonDrawable *drawable = spSkeletonDrawable_create(skeletonData, animationStateData); spSkeletonDrawable *drawable = spSkeletonDrawable_create(skeletonData, animationStateData);
drawable->skeleton->x = 400; drawable->skeleton->y = 500; drawable->skeleton->x = 400;
spSkeleton_setToSetupPose(drawable->skeleton); drawable->skeleton->y = 500;
spSkeletonDrawable_update(drawable, 0); spSkeleton_setToSetupPose(drawable->skeleton);
spAnimationState_setAnimationByName(drawable->animationState, 0, "run", -1); spSkeletonDrawable_update(drawable, 0);
spAnimationState_setAnimationByName(drawable->animationState, 0, "run", -1);
int quit = 0; int quit = 0;
uint64_t lastFrameTime = SDL_GetPerformanceCounter(); uint64_t lastFrameTime = SDL_GetPerformanceCounter();
while (!quit) { while (!quit) {
SDL_Event event; SDL_Event event;
while (SDL_PollEvent(&event) != 0) { while (SDL_PollEvent(&event) != 0) {
if (event.type == SDL_QUIT) { if (event.type == SDL_QUIT) {
quit = -1; quit = -1;
break; break;
} }
} }
SDL_SetRenderDrawColor(renderer, 94, 93, 96, 255); SDL_SetRenderDrawColor(renderer, 94, 93, 96, 255);
SDL_RenderClear(renderer); SDL_RenderClear(renderer);
uint64_t now = SDL_GetPerformanceCounter(); uint64_t now = SDL_GetPerformanceCounter();
double deltaTime = (now - lastFrameTime) / (double) SDL_GetPerformanceFrequency(); double deltaTime = (now - lastFrameTime) / (double) SDL_GetPerformanceFrequency();
lastFrameTime = now; lastFrameTime = now;
spSkeletonDrawable_update(drawable, deltaTime); spSkeletonDrawable_update(drawable, deltaTime);
spSkeletonDrawable_draw(drawable, renderer); spSkeletonDrawable_draw(drawable, renderer);
SDL_RenderPresent(renderer); SDL_RenderPresent(renderer);
} }
SDL_DestroyWindow(window); SDL_DestroyWindow(window);
SDL_Quit(); SDL_Quit();
return 0; return 0;
} }

View File

@ -39,173 +39,173 @@
_SP_ARRAY_IMPLEMENT_TYPE_NO_CONTAINS(spSdlVertexArray, SDL_Vertex) _SP_ARRAY_IMPLEMENT_TYPE_NO_CONTAINS(spSdlVertexArray, SDL_Vertex)
spSkeletonDrawable *spSkeletonDrawable_create(spSkeletonData *skeletonData, spAnimationStateData *animationStateData) { spSkeletonDrawable *spSkeletonDrawable_create(spSkeletonData *skeletonData, spAnimationStateData *animationStateData) {
spBone_setYDown(-1); spBone_setYDown(-1);
spSkeletonDrawable *self = NEW(spSkeletonDrawable); spSkeletonDrawable *self = NEW(spSkeletonDrawable);
self->skeleton = spSkeleton_create(skeletonData); self->skeleton = spSkeleton_create(skeletonData);
self->animationState = spAnimationState_create(animationStateData); self->animationState = spAnimationState_create(animationStateData);
self->sdlIndices = spIntArray_create(12); self->sdlIndices = spIntArray_create(12);
self->sdlVertices = spSdlVertexArray_create(12); self->sdlVertices = spSdlVertexArray_create(12);
self->worldVertices = spFloatArray_create(12); self->worldVertices = spFloatArray_create(12);
self->clipper = spSkeletonClipping_create(); self->clipper = spSkeletonClipping_create();
return self; return self;
} }
void spSkeletonDrawable_dispose(spSkeletonDrawable *self) { void spSkeletonDrawable_dispose(spSkeletonDrawable *self) {
spSkeleton_dispose(self->skeleton); spSkeleton_dispose(self->skeleton);
spAnimationState_dispose(self->animationState); spAnimationState_dispose(self->animationState);
spIntArray_dispose(self->sdlIndices); spIntArray_dispose(self->sdlIndices);
spSdlVertexArray_dispose(self->sdlVertices); spSdlVertexArray_dispose(self->sdlVertices);
spFloatArray_dispose(self->worldVertices); spFloatArray_dispose(self->worldVertices);
spSkeletonClipping_dispose(self->clipper); spSkeletonClipping_dispose(self->clipper);
FREE(self); FREE(self);
} }
void spSkeletonDrawable_update(spSkeletonDrawable *self, float delta) { void spSkeletonDrawable_update(spSkeletonDrawable *self, float delta) {
spAnimationState_update(self->animationState, delta); spAnimationState_update(self->animationState, delta);
spAnimationState_apply(self->animationState, self->skeleton); spAnimationState_apply(self->animationState, self->skeleton);
spSkeleton_updateWorldTransform(self->skeleton); spSkeleton_updateWorldTransform(self->skeleton);
} }
void spSkeletonDrawable_draw(spSkeletonDrawable *self, struct SDL_Renderer *renderer) { void spSkeletonDrawable_draw(spSkeletonDrawable *self, struct SDL_Renderer *renderer) {
static unsigned short quadIndices[] = { 0, 1, 2, 2, 3, 0 }; static unsigned short quadIndices[] = {0, 1, 2, 2, 3, 0};
spSkeleton *skeleton = self->skeleton; spSkeleton *skeleton = self->skeleton;
spSkeletonClipping *clipper = self->clipper; spSkeletonClipping *clipper = self->clipper;
SDL_Texture *texture; SDL_Texture *texture;
SDL_Vertex sdlVertex; SDL_Vertex sdlVertex;
for (int i = 0; i < skeleton->slotsCount; ++i) { for (int i = 0; i < skeleton->slotsCount; ++i) {
spSlot *slot = skeleton->drawOrder[i]; spSlot *slot = skeleton->drawOrder[i];
spAttachment *attachment = slot->attachment; spAttachment *attachment = slot->attachment;
if (!attachment) continue; if (!attachment) continue;
// Early out if the slot color is 0 or the bone is not active // Early out if the slot color is 0 or the bone is not active
if (slot->color.a == 0 || !slot->bone->active) { if (slot->color.a == 0 || !slot->bone->active) {
spSkeletonClipping_clipEnd(clipper, slot); spSkeletonClipping_clipEnd(clipper, slot);
continue; continue;
} }
spFloatArray *vertices = self->worldVertices; spFloatArray *vertices = self->worldVertices;
int verticesCount = 0; int verticesCount = 0;
float *uvs = NULL; float *uvs = NULL;
unsigned short *indices; unsigned short *indices;
int indicesCount = 0; int indicesCount = 0;
spColor *attachmentColor = NULL; spColor *attachmentColor = NULL;
if (attachment->type == SP_ATTACHMENT_REGION) { if (attachment->type == SP_ATTACHMENT_REGION) {
spRegionAttachment *region = (spRegionAttachment *) attachment; spRegionAttachment *region = (spRegionAttachment *) attachment;
attachmentColor = &region->color; attachmentColor = &region->color;
// Early out if the slot color is 0 // Early out if the slot color is 0
if (attachmentColor->a == 0) { if (attachmentColor->a == 0) {
spSkeletonClipping_clipEnd(clipper, slot); spSkeletonClipping_clipEnd(clipper, slot);
continue; continue;
} }
spFloatArray_setSize(vertices, 8); spFloatArray_setSize(vertices, 8);
spRegionAttachment_computeWorldVertices(region, slot, vertices->items, 0, 2); spRegionAttachment_computeWorldVertices(region, slot, vertices->items, 0, 2);
verticesCount = 4; verticesCount = 4;
uvs = region->uvs; uvs = region->uvs;
indices = quadIndices; indices = quadIndices;
indicesCount = 6; indicesCount = 6;
texture = (SDL_Texture *) ((spAtlasRegion*)region->rendererObject)->page->rendererObject; texture = (SDL_Texture *) ((spAtlasRegion *) region->rendererObject)->page->rendererObject;
} else if (attachment->type == SP_ATTACHMENT_MESH) { } else if (attachment->type == SP_ATTACHMENT_MESH) {
spMeshAttachment *mesh = (spMeshAttachment *) attachment; spMeshAttachment *mesh = (spMeshAttachment *) attachment;
attachmentColor = &mesh->color; attachmentColor = &mesh->color;
// Early out if the slot color is 0 // Early out if the slot color is 0
if (attachmentColor->a == 0) { if (attachmentColor->a == 0) {
spSkeletonClipping_clipEnd(clipper, slot); spSkeletonClipping_clipEnd(clipper, slot);
continue; continue;
} }
spFloatArray_setSize(vertices, mesh->super.worldVerticesLength); spFloatArray_setSize(vertices, mesh->super.worldVerticesLength);
spVertexAttachment_computeWorldVertices(SUPER(mesh), slot, 0, mesh->super.worldVerticesLength, vertices->items, 0, 2); spVertexAttachment_computeWorldVertices(SUPER(mesh), slot, 0, mesh->super.worldVerticesLength, vertices->items, 0, 2);
verticesCount = mesh->super.worldVerticesLength >> 1; verticesCount = mesh->super.worldVerticesLength >> 1;
uvs = mesh->uvs; uvs = mesh->uvs;
indices = mesh->triangles; indices = mesh->triangles;
indicesCount = mesh->trianglesCount; indicesCount = mesh->trianglesCount;
texture = (SDL_Texture *) ((spAtlasRegion*)mesh->rendererObject)->page->rendererObject; texture = (SDL_Texture *) ((spAtlasRegion *) mesh->rendererObject)->page->rendererObject;
} else if (attachment->type == SP_ATTACHMENT_CLIPPING) { } else if (attachment->type == SP_ATTACHMENT_CLIPPING) {
spClippingAttachment *clip = (spClippingAttachment *) slot->attachment; spClippingAttachment *clip = (spClippingAttachment *) slot->attachment;
spSkeletonClipping_clipStart(clipper, slot, clip); spSkeletonClipping_clipStart(clipper, slot, clip);
continue; continue;
} else } else
continue; continue;
Uint8 r = (Uint8)(skeleton->color.r * slot->color.r * attachmentColor->r * 255); 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 g = (Uint8) (skeleton->color.g * slot->color.g * attachmentColor->g * 255);
Uint8 b = (Uint8)(skeleton->color.b * slot->color.b * attachmentColor->b * 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); Uint8 a = (Uint8) (skeleton->color.a * slot->color.a * attachmentColor->a * 255);
sdlVertex.color.r = r; sdlVertex.color.r = r;
sdlVertex.color.g = g; sdlVertex.color.g = g;
sdlVertex.color.b = b; sdlVertex.color.b = b;
sdlVertex.color.a = a; sdlVertex.color.a = a;
if (spSkeletonClipping_isClipping(clipper)) { if (spSkeletonClipping_isClipping(clipper)) {
spSkeletonClipping_clipTriangles(clipper, vertices->items, verticesCount << 1, indices, indicesCount, uvs, 2); spSkeletonClipping_clipTriangles(clipper, vertices->items, verticesCount << 1, indices, indicesCount, uvs, 2);
vertices = clipper->clippedVertices; vertices = clipper->clippedVertices;
verticesCount = clipper->clippedVertices->size >> 1; verticesCount = clipper->clippedVertices->size >> 1;
uvs = clipper->clippedUVs->items; uvs = clipper->clippedUVs->items;
indices = clipper->clippedTriangles->items; indices = clipper->clippedTriangles->items;
indicesCount = clipper->clippedTriangles->size; indicesCount = clipper->clippedTriangles->size;
} }
spSdlVertexArray_clear(self->sdlVertices); spSdlVertexArray_clear(self->sdlVertices);
for (int ii = 0; ii < verticesCount << 1; ii += 2) { for (int ii = 0; ii < verticesCount << 1; ii += 2) {
sdlVertex.position.x = vertices->items[ii]; sdlVertex.position.x = vertices->items[ii];
sdlVertex.position.y = vertices->items[ii + 1]; sdlVertex.position.y = vertices->items[ii + 1];
sdlVertex.tex_coord.x = uvs[ii]; sdlVertex.tex_coord.x = uvs[ii];
sdlVertex.tex_coord.y = uvs[ii + 1]; sdlVertex.tex_coord.y = uvs[ii + 1];
spSdlVertexArray_add(self->sdlVertices, sdlVertex); spSdlVertexArray_add(self->sdlVertices, sdlVertex);
} }
spIntArray_clear(self->sdlIndices); spIntArray_clear(self->sdlIndices);
for (int ii = 0; ii < (int) indicesCount; ii++) for (int ii = 0; ii < (int) indicesCount; ii++)
spIntArray_add(self->sdlIndices, indices[ii]); spIntArray_add(self->sdlIndices, indices[ii]);
switch (slot->data->blendMode) { switch (slot->data->blendMode) {
case SP_BLEND_MODE_NORMAL: case SP_BLEND_MODE_NORMAL:
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND); SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
break; break;
case SP_BLEND_MODE_MULTIPLY: case SP_BLEND_MODE_MULTIPLY:
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_MOD); SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_MOD);
break; break;
case SP_BLEND_MODE_ADDITIVE: case SP_BLEND_MODE_ADDITIVE:
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_ADD); SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_ADD);
break; break;
case SP_BLEND_MODE_SCREEN: case SP_BLEND_MODE_SCREEN:
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND); SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
break; break;
} }
SDL_RenderGeometry(renderer, texture, self->sdlVertices->items, self->sdlVertices->size, self->sdlIndices->items, SDL_RenderGeometry(renderer, texture, self->sdlVertices->items, self->sdlVertices->size, self->sdlIndices->items,
indicesCount); indicesCount);
spSkeletonClipping_clipEnd(clipper, slot); spSkeletonClipping_clipEnd(clipper, slot);
} }
spSkeletonClipping_clipEnd2(clipper); spSkeletonClipping_clipEnd2(clipper);
} }
void _spAtlasPage_createTexture(spAtlasPage *self, const char *path) { void _spAtlasPage_createTexture(spAtlasPage *self, const char *path) {
int width, height, components; int width, height, components;
stbi_uc *imageData = stbi_load(path, &width, &height, &components, 4); stbi_uc *imageData = stbi_load(path, &width, &height, &components, 4);
if (!imageData) return; if (!imageData) return;
SDL_Texture *texture = SDL_CreateTexture((SDL_Renderer*)self->atlas->rendererObject, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STATIC, width, SDL_Texture *texture = SDL_CreateTexture((SDL_Renderer *) self->atlas->rendererObject, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STATIC, width,
height); height);
if (!texture) { if (!texture) {
stbi_image_free(imageData); stbi_image_free(imageData);
return; return;
} }
if (SDL_UpdateTexture(texture, NULL, imageData, width * 4)) { if (SDL_UpdateTexture(texture, NULL, imageData, width * 4)) {
stbi_image_free(imageData); stbi_image_free(imageData);
return; return;
} }
stbi_image_free(imageData); stbi_image_free(imageData);
self->rendererObject = texture; self->rendererObject = texture;
return; return;
} }
void _spAtlasPage_disposeTexture(spAtlasPage *self) { 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) { char *_spUtil_readFile(const char *path, int *length) {
return _spReadFile(path, length); return _spReadFile(path, length);
} }

View File

@ -42,13 +42,13 @@ struct SDL_Vertex;
_SP_ARRAY_DECLARE_TYPE(spSdlVertexArray, struct SDL_Vertex) _SP_ARRAY_DECLARE_TYPE(spSdlVertexArray, struct SDL_Vertex)
typedef struct spSkeletonDrawable { typedef struct spSkeletonDrawable {
spSkeleton *skeleton; spSkeleton *skeleton;
spAnimationState *animationState; spAnimationState *animationState;
spSkeletonClipping *clipper; spSkeletonClipping *clipper;
spFloatArray *worldVertices; spFloatArray *worldVertices;
spSdlVertexArray *sdlVertices; spSdlVertexArray *sdlVertices;
spIntArray *sdlIndices; spIntArray *sdlIndices;
} spSkeletonDrawable; } spSkeletonDrawable;
SP_API spSkeletonDrawable *spSkeletonDrawable_create(spSkeletonData *skeletonData, spAnimationStateData *animationStateData); SP_API spSkeletonDrawable *spSkeletonDrawable_create(spSkeletonData *skeletonData, spAnimationStateData *animationStateData);

View File

@ -58,13 +58,13 @@ void SkeletonDrawable::update(float delta) {
} }
void SkeletonDrawable::draw(SDL_Renderer *renderer) { void SkeletonDrawable::draw(SDL_Renderer *renderer) {
Vector<unsigned short> quadIndices; Vector<unsigned short> quadIndices;
quadIndices.add(0); quadIndices.add(0);
quadIndices.add(1); quadIndices.add(1);
quadIndices.add(2); quadIndices.add(2);
quadIndices.add(2); quadIndices.add(2);
quadIndices.add(3); quadIndices.add(3);
quadIndices.add(0); quadIndices.add(0);
SDL_Texture *texture; SDL_Texture *texture;
SDL_Vertex sdlVertex; SDL_Vertex sdlVertex;
for (unsigned i = 0; i < skeleton->getSlots().size(); ++i) { for (unsigned i = 0; i < skeleton->getSlots().size(); ++i) {