summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSyndamia <kamen@syndamia.com>2026-03-16 20:59:04 +0200
committerSyndamia <kamen@syndamia.com>2026-03-16 20:59:04 +0200
commit3e9c46f82430d7bb609cebb57be17c26cba4acb5 (patch)
tree5b03be91eb637b2ce7fb37ac2c67319d0978dbf8
parente6842e44159d7cc9b1c32dd2baae4df164fa81d2 (diff)
downloadppm_graphics-3e9c46f82430d7bb609cebb57be17c26cba4acb5.tar
ppm_graphics-3e9c46f82430d7bb609cebb57be17c26cba4acb5.tar.gz
ppm_graphics-3e9c46f82430d7bb609cebb57be17c26cba4acb5.zip
chore: Rework renderer functions to use a struct instead of different arguments
-rw-r--r--Animation.c13
-rw-r--r--Animation.h17
-rw-r--r--AnimationInterpolate.c14
-rw-r--r--AnimationInterpolate.h2
-rw-r--r--AnimationMove.c32
-rw-r--r--AnimationMove.h4
-rw-r--r--AnimationObject.c68
-rw-r--r--AnimationObject.h12
8 files changed, 94 insertions, 68 deletions
diff --git a/Animation.c b/Animation.c
index a67c22e..2dc6380 100644
--- a/Animation.c
+++ b/Animation.c
@@ -75,13 +75,22 @@ Animation_pushEvent(Animation* anim, u32 startFrame, u32 endFrame, PixelRenderer
void
Animation_render(Animation* anim) {
- for (int currFrame = 0; currFrame < anim->frameCount; ++currFrame) {
+ for (u32 currFrame = 0; currFrame < anim->frameCount; ++currFrame) {
if (anim->events != NULL)
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);
+ ARGB newPixel = aen->renderCallback(anim, (PixelRendererParams){
+ .frame = {
+ .current = currFrame,
+ .start = aen->startFrame,
+ .end = aen->endFrame,
+ },
+ .pixel = *pixel,
+ .row = r, .col = c,
+ .priv = aen->privateData,
+ });
ARGB_merge(pixel, newPixel);
}
diff --git a/Animation.h b/Animation.h
index 96fa765..6cebdb0 100644
--- a/Animation.h
+++ b/Animation.h
@@ -16,7 +16,22 @@ typedef struct Animation {
AnimationEventNode* events;
} Animation;
-typedef ARGB (*PixelRenderer)(const Animation* anim, u32 currentFrame, ARGB pixel, i32 row, i32 col, void* priv);
+typedef struct PixelRendererParams {
+ struct Frames {
+ u32 current;
+ u32 start;
+ u32 end;
+ } frame;
+
+ ARGB pixel;
+
+ i32 row;
+ i32 col;
+
+ void* priv;
+} PixelRendererParams;
+
+typedef ARGB (*PixelRenderer)(const Animation* anim, PixelRendererParams params);
AnimationEventNode* AnimationEventNode_new(u32 startFrame, u32 endFrame, PixelRenderer renderCallback, void* privateData);
Animation Animation_new(u32 width, u32 height, u32 frameCount);
diff --git a/AnimationInterpolate.c b/AnimationInterpolate.c
index 37a0990..44bdafc 100644
--- a/AnimationInterpolate.c
+++ b/AnimationInterpolate.c
@@ -20,26 +20,26 @@ colorInterpolate(color ina, color inb, double percentage) {
}
ARGB
-A_Interpolate(const Animation* anim, u32 frameIndex, ARGB pixel, i32 r, i32 c, void* value) {
- struct A_Interpolate in = *(struct A_Interpolate*)value;
+A_Interpolate(const Animation* anim, PixelRendererParams a) {
+ struct A_Interpolate in = *(struct A_Interpolate*)a.priv;
if (in.interpolate == NULL) {
fprintf(stderr, "Interpolation function cannot be NULL!\n");
- ARGB_set(&pixel, 0xFFFF00FF);
- return pixel;
+ ARGB_set(&a.pixel, 0xFFFF00FF);
+ return a.pixel;
}
- if (frameIndex == in.startFrame) {
+ if (a.frame.current == in.startFrame) {
memcpy(in.initialValueTemp, in.value, in.initialValueTempSize);
}
- double perc = (double)(frameIndex - in.startFrame) / (in.endFrame - in.startFrame);
+ double perc = (double)(a.frame.current - in.startFrame) / (in.endFrame - in.startFrame);
if (perc < 0.0) perc = 0.0;
if (perc > 1.0) perc = 1.0;
in.interpolate(in.value, in.initialValueTemp, in.goalValue, perc);
- return pixel;
+ return a.pixel;
}
void
diff --git a/AnimationInterpolate.h b/AnimationInterpolate.h
index c033525..4e83054 100644
--- a/AnimationInterpolate.h
+++ b/AnimationInterpolate.h
@@ -15,7 +15,7 @@ struct A_Interpolate {
void (*interpolate)(void* value, const void* initialState, const void* finalState, double percentage);
};
-ARGB A_Interpolate(const Animation* anim, u32 frameIndex, ARGB pixel, i32 r, i32 c, void* priv);
+ARGB A_Interpolate(const Animation* anim, PixelRendererParams params);
void AO_LineInterpolate(void* valueLine, const void* initialLine, const void* finalLine, double percentage);
diff --git a/AnimationMove.c b/AnimationMove.c
index 696150c..7ac52f9 100644
--- a/AnimationMove.c
+++ b/AnimationMove.c
@@ -2,31 +2,33 @@
#include <math.h>
ARGB
-AM_Linear(const Animation* anim, u32 frameIndex, ARGB pixel, i32 r, i32 c, void* priv) {
- struct AM_Linear ml = *(struct AM_Linear*)priv;
+AM_Linear(const Animation* anim, PixelRendererParams a) {
+ struct AM_Linear ml = *(struct AM_Linear*)a.priv;
- r = (r - ml.startRow - (i32)(frameIndex * ml.dRow));
- c = (c - ml.startCol - (i32)(frameIndex * ml.dCol));
+ a.row = (a.row - ml.startRow - (i32)(a.frame.current * ml.dRow));
+ a.col = (a.col - ml.startCol - (i32)(a.frame.current * ml.dCol));
if (ml.wrapCoordinates == true) {
- r %= anim->width;
- c %= anim->height;
+ a.row %= anim->width;
+ a.col %= anim->height;
}
- return ml.callback(anim, frameIndex, pixel, r, c, ml.priv);
+ a.priv = ml.priv;
+ return ml.callback(anim, a);
}
ARGB
-AM_Spin(const Animation* anim, u32 frameIndex, ARGB pixel, i32 r, i32 c, void* priv) {
- struct AM_Spin ms = *(struct AM_Spin*)priv;
+AM_Spin(const Animation* anim, PixelRendererParams a) {
+ struct AM_Spin ms = *(struct AM_Spin*)a.priv;
- double radius = sqrt(r * r + c * c);
- double phi = atan2(c, r);
+ double radius = sqrt(a.row * a.row + a.col * a.col);
+ double phi = atan2(a.col, a.row);
- phi += ms.theta + ms.dTheta * frameIndex;
+ phi += ms.theta + ms.dTheta * a.frame.current;
- r = (ms.centerRow + (i32)(radius * cos(phi)));
- c = (ms.centerCol + (i32)(radius * sin(phi)));
+ a.row = (ms.centerRow + (i32)(radius * cos(phi)));
+ a.col = (ms.centerCol + (i32)(radius * sin(phi)));
- return ms.callback(anim, frameIndex, pixel, r, c, ms.priv);
+ a.priv = ms.priv;
+ return ms.callback(anim, a);
}
diff --git a/AnimationMove.h b/AnimationMove.h
index 342144e..48a3b92 100644
--- a/AnimationMove.h
+++ b/AnimationMove.h
@@ -14,7 +14,7 @@ struct AM_Linear {
bool wrapCoordinates;
};
-ARGB AM_Linear(const Animation* anim, u32 frameIndex, ARGB pixel, i32 r, i32 c, void* priv);
+ARGB AM_Linear(const Animation* anim, PixelRendererParams params);
struct AM_Spin {
PixelRenderer callback;
@@ -26,6 +26,6 @@ struct AM_Spin {
i32 centerCol;
};
-ARGB AM_Spin(const Animation* anim, u32 frameIndex, ARGB pixel, i32 r, i32 c, void* priv);
+ARGB AM_Spin(const Animation* anim, PixelRendererParams params);
#endif /* _ANIMATION_MOVE */
diff --git a/AnimationObject.c b/AnimationObject.c
index 87fe324..63594ce 100644
--- a/AnimationObject.c
+++ b/AnimationObject.c
@@ -3,76 +3,76 @@
#include <stdlib.h>
ARGB
-AO_CheckerPattern(const Animation* anim, u32 frameIndex, ARGB pixel, i32 r, i32 c, void* priv) {
- struct AO_CheckerPattern chp = *(struct AO_CheckerPattern*)priv;
+AO_CheckerPattern(const Animation* anim, PixelRendererParams a) {
+ struct AO_CheckerPattern chp = *(struct AO_CheckerPattern*)a.priv;
- r /= chp.squareSize;
- c /= chp.squareSize;
+ a.row /= chp.squareSize;
+ a.col /= chp.squareSize;
- if ((r + c) % 2 == 0) {
- ARGB_set(&pixel, 0xFF000000);
+ if ((a.row + a.col) % 2 == 0) {
+ ARGB_set(&a.pixel, 0xFF000000);
}
else {
- ARGB_set(&pixel, 0xFFFF00FF);
+ ARGB_set(&a.pixel, 0xFFFF00FF);
}
- return pixel;
+ return a.pixel;
}
ARGB
-AO_Square(const Animation* anim, u32 frameIndex, ARGB pixel, i32 r, i32 c, void* priv) {
- struct AO_Square* set = (struct AO_Square*)priv;
+AO_Square(const Animation* anim, PixelRendererParams a) {
+ struct AO_Square* set = (struct AO_Square*)a.priv;
- if (0 <= r && r <= set->width && 0 <= c && c <= set->height) {
- ARGB_set(&pixel, set->color);
+ if (0 <= a.row && a.row <= set->width && 0 <= a.col && a.col <= set->height) {
+ ARGB_set(&a.pixel, set->color);
}
- return pixel;
+ return a.pixel;
}
ARGB
-AO_Circle(const Animation* anim, u32 frameIndex, ARGB pixel, i32 r, i32 c, void* priv) {
- struct AO_Circle* cir = (struct AO_Circle*)priv;
+AO_Circle(const Animation* anim, PixelRendererParams a) {
+ struct AO_Circle* cir = (struct AO_Circle*)a.priv;
- if (r * r + c * c < cir->radius * cir->radius) {
- ARGB_set(&pixel, cir->color);
+ if (a.row * a.row + a.col * a.col < cir->radius * cir->radius) {
+ ARGB_set(&a.pixel, cir->color);
}
- return pixel;
+ return a.pixel;
}
ARGB
-AO_Image(const Animation* anim, u32 frameIndex, ARGB pixel, i32 r, i32 c, void* priv) {
- struct AO_Image img = *(struct AO_Image*)priv;
+AO_Image(const Animation* anim, PixelRendererParams a) {
+ struct AO_Image img = *(struct AO_Image*)a.priv;
if (img.noRepeat == true &&
- (c < 0 || img.img.width * img.zoom <= c ||
- r < 0 || img.img.height * img.zoom <= r))
+ (a.col < 0 || img.img.width * img.zoom <= a.col ||
+ a.row < 0 || img.img.height * img.zoom <= a.row))
{
- return pixel;
+ return a.pixel;
}
- return *RGBImage_at(*(RGBImage*)priv, r / img.zoom, c / img.zoom);
+ return *RGBImage_at(*(RGBImage*)a.priv, a.row / img.zoom, a.col / img.zoom);
}
ARGB
-AO_Line(const Animation* anim, u32 frameIndex, ARGB pixel, i32 r, i32 c, void* priv) {
- struct AO_Line l = *(struct AO_Line*)priv;
+AO_Line(const Animation* anim, PixelRendererParams a) {
+ struct AO_Line l = *(struct AO_Line*)a.priv;
- if (fabs(l.a * c + l.b * r + l.c) / sqrt(l.a * l.a + l.b * l.b) < l.width) {
- ARGB_set(&pixel, l.color);
+ if (fabs(l.a * a.col + l.b * a.row + l.c) / sqrt(l.a * l.a + l.b * l.b) < l.width) {
+ ARGB_set(&a.pixel, l.color);
}
- return pixel;
+ return a.pixel;
}
ARGB
-AO_HorSegment(const Animation* anim, u32 frameIndex, ARGB pixel, i32 r, i32 c, void* priv) {
- struct AO_HorSegment hs = *(struct AO_HorSegment*)priv;
+AO_HorSegment(const Animation* anim, PixelRendererParams a) {
+ struct AO_HorSegment hs = *(struct AO_HorSegment*)a.priv;
- if (abs(r) <= hs.width && hs.left <= c && c <= hs.right) {
- ARGB_set(&pixel, hs.color);
+ if (abs(a.row) <= hs.width && hs.left <= a.col && a.col <= hs.right) {
+ ARGB_set(&a.pixel, hs.color);
}
- return pixel;
+ return a.pixel;
}
diff --git a/AnimationObject.h b/AnimationObject.h
index 16f0b0e..ef8c3a3 100644
--- a/AnimationObject.h
+++ b/AnimationObject.h
@@ -9,7 +9,7 @@ struct AO_CheckerPattern {
color colorB;
};
-ARGB AO_CheckerPattern(const Animation* anim, u32 frameIndex, ARGB pixel, i32 r, i32 c, void* priv);
+ARGB AO_CheckerPattern(const Animation* anim, PixelRendererParams params);
struct AO_Square {
u32 width;
@@ -17,14 +17,14 @@ struct AO_Square {
color color;
};
-ARGB AO_Square(const Animation* anim, u32 frameIndex, ARGB pixel, i32 r, i32 c, void* priv);
+ARGB AO_Square(const Animation* anim, PixelRendererParams params);
struct AO_Circle {
double radius;
color color;
};
-ARGB AO_Circle(const Animation* anim, u32 frameIndex, ARGB pixel, i32 r, i32 c, void* priv);
+ARGB AO_Circle(const Animation* anim, PixelRendererParams params);
struct AO_Image {
RGBImage img;
@@ -32,7 +32,7 @@ struct AO_Image {
bool noRepeat;
};
-ARGB AO_Image(const Animation* anim, u32 frameIndex, ARGB pixel, i32 r, i32 c, void* priv);
+ARGB AO_Image(const Animation* anim, PixelRendererParams params);
struct AO_Line {
double a;
@@ -42,7 +42,7 @@ struct AO_Line {
color color;
};
-ARGB AO_Line(const Animation* anim, u32 frameIndex, ARGB pixel, i32 r, i32 c, void* priv);
+ARGB AO_Line(const Animation* anim, PixelRendererParams params);
struct AO_HorSegment {
i32 left;
@@ -51,6 +51,6 @@ struct AO_HorSegment {
color color;
};
-ARGB AO_HorSegment(const Animation* anim, u32 frameIndex, ARGB pixel, i32 r, i32 c, void* priv);
+ARGB AO_HorSegment(const Animation* anim, PixelRendererParams params);
#endif /* _ANIMATION_OBJECT */