From 47e28b44615b9b068776f61b8741ea8692461c1d Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sat, 14 Mar 2026 23:05:04 +0200 Subject: fix: Replace rotation with spinning --- graphics.c | 63 ++++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 35 insertions(+), 28 deletions(-) (limited to 'graphics.c') diff --git a/graphics.c b/graphics.c index cd4f561..499bd6c 100644 --- a/graphics.c +++ b/graphics.c @@ -126,7 +126,7 @@ typedef struct Animation { AnimationEventNode* events; } Animation; -typedef ARGB (*PixelRenderer)(const Animation* anim, u32 currentFrame, ARGB pixel, u32 row, u32 col, void* priv); +typedef ARGB (*PixelRenderer)(const Animation* anim, u32 currentFrame, ARGB pixel, i32 row, i32 col, void* priv); struct AnimationEventNode { AnimationEventNode* next; @@ -205,8 +205,8 @@ Animation_render(Animation* anim) { for (int currFrame = 0; currFrame < anim->frameCount; ++currFrame) { if (anim->events != NULL) - for (int r = 0; r < anim->frameBuffer.width; ++r) - for (int c = 0; c < anim->frameBuffer.height; ++c) { + for (i32 r = 0; r < anim->frameBuffer.width; ++r) + for (i32 c = 0; c < anim->frameBuffer.height; ++c) { for (AnimationEventNode* aen = anim->events; aen != NULL && aen->startFrame <= currFrame; aen = aen->next) { ARGB* pixel = RGBImage_at(anim->frameBuffer, r, c); ARGB newPixel = aen->renderCallback(anim, currFrame, *pixel, r, c, aen->privateData); @@ -245,7 +245,7 @@ struct CheckerPattern { }; ARGB -CheckerPattern(const Animation* anim, u32 frameIndex, ARGB pixel, u32 r, u32 c, void* priv) { +CheckerPattern(const Animation* anim, u32 frameIndex, ARGB pixel, i32 r, i32 c, void* priv) { struct CheckerPattern chp = *(struct CheckerPattern*)priv; r /= chp.squareSize; @@ -268,7 +268,7 @@ struct SquareSettings { }; ARGB -Square(const Animation* anim, u32 frameIndex, ARGB pixel, u32 r, u32 c, void* priv) { +Square(const Animation* anim, u32 frameIndex, ARGB pixel, i32 r, i32 c, void* priv) { struct SquareSettings* set = (struct SquareSettings*)priv; if (0 <= r && r <= set->width && 0 <= c && c <= set->height) { @@ -285,12 +285,12 @@ struct Image { }; ARGB -Image(const Animation* anim, u32 frameIndex, ARGB pixel, u32 r, u32 c, void* priv) { +Image(const Animation* anim, u32 frameIndex, ARGB pixel, i32 r, i32 c, void* priv) { struct Image img = *(struct Image*)priv; if (img.noRepeat == true && - (img.img.width * img.zoom <= c || - img.img.height * img.zoom <= r)) + (c < 0 || img.img.width * img.zoom <= c || + r < 0 || img.img.height * img.zoom <= r)) { return pixel; } @@ -307,7 +307,7 @@ struct Line { }; ARGB -Line(const Animation* anim, u32 frameIndex, ARGB pixel, u32 r, u32 c, void* priv) { +Line(const Animation* anim, u32 frameIndex, ARGB pixel, i32 r, i32 c, void* priv) { struct Line l = *(struct Line*)priv; if (fabs(l.a * c + l.b * r + l.c) / sqrt(l.a * l.a + l.b * l.b) < l.width) { @@ -348,19 +348,19 @@ struct MoveLinear { PixelRenderer callback; void* priv; - u32 startRow; - u32 startCol; + i32 startRow; + i32 startCol; float dRow; float dCol; bool wrapCoordinates; }; ARGB -MoveLinear(const Animation* anim, u32 frameIndex, ARGB pixel, u32 r, u32 c, void* priv) { +MoveLinear(const Animation* anim, u32 frameIndex, ARGB pixel, i32 r, i32 c, void* priv) { struct MoveLinear ml = *(struct MoveLinear*)priv; - r = (r - ml.startRow - (u32)(frameIndex * ml.dRow)); - c = (c - ml.startCol - (u32)(frameIndex * ml.dCol)); + r = (r - ml.startRow - (i32)(frameIndex * ml.dRow)); + c = (c - ml.startCol - (i32)(frameIndex * ml.dCol)); if (ml.wrapCoordinates == true) { r %= anim->width; @@ -370,24 +370,31 @@ MoveLinear(const Animation* anim, u32 frameIndex, ARGB pixel, u32 r, u32 c, void return ml.callback(anim, frameIndex, pixel, r, c, ml.priv); } -struct MoveRotate { +struct MoveSpin { PixelRenderer callback; void* priv; - float radius; float theta; + i32 centerRow; + i32 centerCol; }; ARGB -MoveRotate(const Animation* anim, u32 frameIndex, ARGB pixel, u32 r, u32 c, void* priv) { - struct MoveRotate mr = *(struct MoveRotate*)priv; +MoveSpin(const Animation* anim, u32 frameIndex, ARGB pixel, i32 r, i32 c, void* priv) { + struct MoveSpin ms = *(struct MoveSpin*)priv; - r = (r + (u32)(mr.radius * cos(mr.theta * frameIndex))) % anim->width; - c = (c + (u32)(mr.radius * sin(mr.theta * frameIndex))) % anim->height; + double radius = sqrt(r * r + c * c); + double phi = atan2(c, r); - return mr.callback(anim, frameIndex, pixel, r, c, mr.priv); + phi += ms.theta * frameIndex; + + r = (ms.centerRow + (i32)(radius * cos(phi))); + c = (ms.centerCol + (i32)(radius * sin(phi))); + + return ms.callback(anim, frameIndex, pixel, r, c, ms.priv); } + struct Interpolate { PixelRenderer callback; void* priv; @@ -401,7 +408,7 @@ struct Interpolate { }; ARGB -Interpolate(const Animation* anim, u32 frameIndex, ARGB pixel, u32 r, u32 c, void* priv) { +Interpolate(const Animation* anim, u32 frameIndex, ARGB pixel, i32 r, i32 c, void* priv) { struct Interpolate in = *(struct Interpolate*)priv; double perc = (double)(frameIndex - in.startFrame) / (in.endFrame - in.startFrame); @@ -474,20 +481,20 @@ main() { struct SquareSettings s1 = { .width = 20, .height = 20, .color = 0xFFFFFF00, }; - struct MoveRotate mr1 = { - .radius = 20, .theta = 3.14159 / 20, + struct MoveSpin ms1 = { + .theta = 3.14159 / 15, .centerCol = 10, .centerRow = 10, .callback = Square, .priv = &s1, }; struct MoveLinear ml3 = { - .startRow = 0, .startCol = 0, - .dRow = -3, .dCol = 3, + .startRow = 100, .startCol = 100, + .dRow = 3, .dCol = 1, - .callback = MoveRotate, .priv = &mr1, + .callback = MoveSpin, .priv = &ms1, }; Animation_pushEvent(&anim, 0, 80, MoveLinear, &ml3); - // Yellow square + // White square struct SquareSettings s4 = { .width = 20, .height = 20, .color = 0xFFFFFFFF, }; -- cgit v1.2.3