[sdl] Formatting

This commit is contained in:
Mario Zechner 2025-08-27 12:47:18 +02:00
parent e55be33c1e
commit cf39537518
5 changed files with 51 additions and 56 deletions

View File

@ -43,7 +43,7 @@ void *load_texture_callback(const char *path) {
}
void unload_texture_callback(void *texture) {
SDL_DestroyTexture((SDL_Texture*)texture);
SDL_DestroyTexture((SDL_Texture *) texture);
}
char *read_file(const char *path, int *length) {
@ -51,10 +51,10 @@ char *read_file(const char *path, int *length) {
if (!file) return NULL;
fseek(file, 0, SEEK_END);
*length = (int)ftell(file);
*length = (int) ftell(file);
fseek(file, 0, SEEK_SET);
char *data = (char*)malloc(*length + 1);
char *data = (char *) malloc(*length + 1);
fread(data, 1, *length, file);
data[*length] = '\0';
fclose(file);
@ -155,11 +155,11 @@ int main(void) {
SDL_RenderClear(renderer);
uint64_t now = SDL_GetPerformanceCounter();
double deltaTime = (now - lastFrameTime) / (double)SDL_GetPerformanceFrequency();
double deltaTime = (now - lastFrameTime) / (double) SDL_GetPerformanceFrequency();
lastFrameTime = now;
// Update animation
spine_skeleton_drawable_update(drawable, (float)deltaTime);
spine_skeleton_drawable_update(drawable, (float) deltaTime);
// Draw
spine_sdl_draw(drawable, renderer, true);

View File

@ -39,50 +39,50 @@ int main() {
printf("Error: %s\n", 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\n", SDL_GetError());
return -1;
}
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (!renderer) {
printf("Error: %s\n", SDL_GetError());
return -1;
}
// Load atlas and skeleton data
SDLTextureLoader textureLoader(renderer);
Atlas atlas("data/spineboy-pma.atlas", &textureLoader);
SkeletonJson json(atlas);
json.setScale(0.5f);
SkeletonData *skeletonData = json.readSkeletonDataFile("data/spineboy-pro.json");
if (!skeletonData) {
printf("Failed to load skeleton data\n");
return -1;
}
// Create skeleton and animation state
Skeleton skeleton(*skeletonData);
AnimationStateData animationStateData(*skeletonData);
animationStateData.setDefaultMix(0.2f);
AnimationState animationState(animationStateData);
// Setup skeleton
skeleton.setPosition(400, 500);
skeleton.setupPose();
skeleton.update(0);
skeleton.updateWorldTransform(Physics_Update);
// Setup animation
animationState.setAnimation(0, "portal", false);
animationState.addAnimation(0, "run", true, 0);
bool quit = false;
uint64_t lastFrameTime = SDL_GetPerformanceCounter();
while (!quit) {
SDL_Event event;
while (SDL_PollEvent(&event) != 0) {
@ -91,32 +91,32 @@ int main() {
break;
}
}
SDL_SetRenderDrawColor(renderer, 94, 93, 96, 255);
SDL_RenderClear(renderer);
uint64_t now = SDL_GetPerformanceCounter();
double deltaTime = (now - lastFrameTime) / (double)SDL_GetPerformanceFrequency();
double deltaTime = (now - lastFrameTime) / (double) SDL_GetPerformanceFrequency();
lastFrameTime = now;
// Update animation
animationState.update(deltaTime);
animationState.apply(skeleton);
skeleton.update(deltaTime);
skeleton.updateWorldTransform(Physics_Update);
// Draw
SDL_draw(skeleton, renderer, true);
SDL_RenderPresent(renderer);
}
// Cleanup
delete skeletonData;
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}

View File

@ -54,8 +54,8 @@ void spine_sdl_draw_skeleton(spine_skeleton skeleton, struct SDL_Renderer *sdl_r
// Pre-allocate vertex and index arrays
int max_vertices = 1024;
int max_indices = 1024 * 3;
SDL_Vertex *vertices = (SDL_Vertex*)malloc(sizeof(SDL_Vertex) * max_vertices);
int *indices = (int*)malloc(sizeof(int) * max_indices);
SDL_Vertex *vertices = (SDL_Vertex *) malloc(sizeof(SDL_Vertex) * max_vertices);
int *indices = (int *) malloc(sizeof(int) * max_indices);
while (command) {
int num_vertices = spine_render_command_get_num_vertices(command);
@ -64,11 +64,11 @@ void spine_sdl_draw_skeleton(spine_skeleton skeleton, struct SDL_Renderer *sdl_r
// Resize buffers if needed
if (num_vertices > max_vertices) {
max_vertices = num_vertices * 2;
vertices = (SDL_Vertex*)realloc(vertices, sizeof(SDL_Vertex) * max_vertices);
vertices = (SDL_Vertex *) realloc(vertices, sizeof(SDL_Vertex) * max_vertices);
}
if (num_indices > max_indices) {
max_indices = num_indices * 2;
indices = (int*)realloc(indices, sizeof(int) * max_indices);
indices = (int *) realloc(indices, sizeof(int) * max_indices);
}
// Get vertex data from render command
@ -76,7 +76,7 @@ void spine_sdl_draw_skeleton(spine_skeleton skeleton, struct SDL_Renderer *sdl_r
float *uvs = spine_render_command_get_uvs(command);
uint32_t *colors = spine_render_command_get_colors(command);
uint16_t *command_indices = spine_render_command_get_indices(command);
SDL_Texture *texture = (SDL_Texture*)spine_render_command_get_texture(command);
SDL_Texture *texture = (SDL_Texture *) spine_render_command_get_texture(command);
spine_blend_mode blend_mode = spine_render_command_get_blend_mode(command);
// Fill SDL vertices
@ -119,24 +119,21 @@ void spine_sdl_draw_skeleton(spine_skeleton skeleton, struct SDL_Renderer *sdl_r
SDL_BlendMode target;
switch (blend_mode) {
case SPINE_BLEND_MODE_NORMAL:
target = SDL_ComposeCustomBlendMode(
SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD);
target = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD);
SDL_SetTextureBlendMode(texture, target);
break;
case SPINE_BLEND_MODE_MULTIPLY:
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_MOD);
break;
case SPINE_BLEND_MODE_ADDITIVE:
target = SDL_ComposeCustomBlendMode(
SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_ADD);
target = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_ADD, SDL_BLENDFACTOR_ONE,
SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_ADD);
SDL_SetTextureBlendMode(texture, target);
break;
case SPINE_BLEND_MODE_SCREEN:
target = SDL_ComposeCustomBlendMode(
SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD);
target = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD);
SDL_SetTextureBlendMode(texture, target);
break;
}
@ -177,5 +174,5 @@ void *load_texture(const char *path, SDL_Renderer *renderer) {
}
void unload_texture(void *texture) {
SDL_DestroyTexture((SDL_Texture*)texture);
SDL_DestroyTexture((SDL_Texture *) texture);
}

View File

@ -50,8 +50,8 @@ void spine::SDL_draw(Skeleton &skeleton, SDL_Renderer *renderer, bool premultipl
// Pre-allocate vertex and index arrays
int maxVertices = 1024;
int maxIndices = 1024 * 3;
SDL_Vertex *vertices = (SDL_Vertex*)malloc(sizeof(SDL_Vertex) * maxVertices);
int *indices = (int*)malloc(sizeof(int) * maxIndices);
SDL_Vertex *vertices = (SDL_Vertex *) malloc(sizeof(SDL_Vertex) * maxVertices);
int *indices = (int *) malloc(sizeof(int) * maxIndices);
while (command) {
int numVertices = command->numVertices;
@ -60,11 +60,11 @@ void spine::SDL_draw(Skeleton &skeleton, SDL_Renderer *renderer, bool premultipl
// Resize buffers if needed
if (numVertices > maxVertices) {
maxVertices = numVertices * 2;
vertices = (SDL_Vertex*)realloc(vertices, sizeof(SDL_Vertex) * maxVertices);
vertices = (SDL_Vertex *) realloc(vertices, sizeof(SDL_Vertex) * maxVertices);
}
if (numIndices > maxIndices) {
maxIndices = numIndices * 2;
indices = (int*)realloc(indices, sizeof(int) * maxIndices);
indices = (int *) realloc(indices, sizeof(int) * maxIndices);
}
// Get vertex data from render command
@ -72,7 +72,7 @@ void spine::SDL_draw(Skeleton &skeleton, SDL_Renderer *renderer, bool premultipl
float *uvs = command->uvs;
uint32_t *colors = command->colors;
uint16_t *commandIndices = command->indices;
SDL_Texture *texture = (SDL_Texture*)command->texture;
SDL_Texture *texture = (SDL_Texture *) command->texture;
BlendMode blendMode = command->blendMode;
// Fill SDL vertices
@ -115,24 +115,21 @@ void spine::SDL_draw(Skeleton &skeleton, SDL_Renderer *renderer, bool premultipl
SDL_BlendMode target;
switch (blendMode) {
case BlendMode_Normal:
target = SDL_ComposeCustomBlendMode(
SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD);
target = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD);
SDL_SetTextureBlendMode(texture, target);
break;
case BlendMode_Multiply:
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_MOD);
break;
case BlendMode_Additive:
target = SDL_ComposeCustomBlendMode(
SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_ADD);
target = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_ADD, SDL_BLENDFACTOR_ONE,
SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_ADD);
SDL_SetTextureBlendMode(texture, target);
break;
case BlendMode_Screen:
target = SDL_ComposeCustomBlendMode(
SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD);
target = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD);
SDL_SetTextureBlendMode(texture, target);
break;
}
@ -174,7 +171,7 @@ void SDLTextureLoader::load(AtlasPage &page, const String &path) {
}
void SDLTextureLoader::unload(void *texture) {
SDL_DestroyTexture((SDL_Texture*)texture);
SDL_DestroyTexture((SDL_Texture *) texture);
}
// Default extension implementation for spine-cpp

View File

@ -36,14 +36,15 @@
namespace spine {
/// Renders a spine::Skeleton using SDL_Renderer
void SDL_draw(Skeleton &skeleton, SDL_Renderer *renderer, bool premultipliedAlpha);
/// SDL texture loader for use with Atlas
class SDLTextureLoader : public TextureLoader {
SDL_Renderer *renderer;
public:
SDLTextureLoader(SDL_Renderer *renderer) : renderer(renderer) {}
SDLTextureLoader(SDL_Renderer *renderer) : renderer(renderer) {
}
void load(AtlasPage &page, const String &path);
void unload(void *texture);
};