summaryrefslogtreecommitdiff
path: root/AnimationRender/F_Interpolate.c
blob: 4686adf2c93adb029bebf72373aaa65ac7e9b830 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "F_Interpolate.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void Interpolate_callback(Interpolate* self, struct FrameContext* fc) {
	if (self->interpolate == NULL) {
		fprintf(stderr, "Interpolation function cannot be NULL!\n");
		return;
	}

	double p = (double)(fc->frame - fc->startFrame) / (fc->endFrame - fc->startFrame);

	if (0.0 <= p && p <= 1.0)
		self->interpolate(self->value, self->startValue, self->endValue, p);
}

FrameCallback* Interpolate_setup(Interpolate obj) {
	FrameCallback* fca = malloc(sizeof(struct FrameCallback));

	fca->callback = (FrameCallback_callback)Interpolate_callback;
	fca->callback_self = malloc(sizeof(obj));
	memcpy(fca->callback_self, &obj, sizeof(obj));

	return fca;
}

#define NUM_INTERPOLATE(a, b, perc) ((a) == (b) ? (a) : ((a) * (1.0 - (perc)) + (b) * (perc)))

color
colorInterpolate(color ina, color inb, double percentage) {
	ARGB a, b;
	ARGB_set(&a, ina);
	ARGB_set(&b, inb);

	a.a = NUM_INTERPOLATE(a.a, b.a, percentage);
	a.r = NUM_INTERPOLATE(a.r, b.r, percentage);
	a.g = NUM_INTERPOLATE(a.g, b.g, percentage);
	a.b = NUM_INTERPOLATE(a.b, b.b, percentage);

	return a.a << 24 | a.r << 16 | a.g << 8 | a.b;
}

void Interpolate_Square(Square* value, const Square* start, const Square* end, double progress) {
	value->size = NUM_INTERPOLATE(start->size, end->size, progress);
	value->color = colorInterpolate(start->color, end->color, progress);
}

void Interpolate_Circle(Circle* value, const Circle* start, const Circle* end, double progress) {
	value->radius = NUM_INTERPOLATE(start->radius, end->radius, progress);
	value->color = colorInterpolate(start->color, end->color, progress);
}

void Interpolate_Checker(Checker* value, const Checker* start, const Checker* end, double progress) {
	value->squareSize = NUM_INTERPOLATE(start->squareSize, end->squareSize, progress);
	value->colorA = colorInterpolate(start->colorA, end->colorA, progress);
	value->colorB = colorInterpolate(start->colorB, end->colorB, progress);
}

void Interpolate_Image(Image* value, const Image* start, const Image* end, double progress) {
	value->scale = NUM_INTERPOLATE(start->scale, end->scale, progress);
	if (start->img.img != end->img.img)
		fprintf(stderr, "Refusing to interpolate between images!");
}

void Interpolate_LineSegment(LineSegment* value, const LineSegment* start, const LineSegment* end, double progress) {
	value->aX = NUM_INTERPOLATE(start->aX, end->aX, progress);
	value->aY = NUM_INTERPOLATE(start->aY, end->aY, progress);
	value->bX = NUM_INTERPOLATE(start->bX, end->bX, progress);
	value->bY = NUM_INTERPOLATE(start->bY, end->bY, progress);
	value->width = NUM_INTERPOLATE(start->width, end->width, progress);
	value->color = colorInterpolate(start->color, end->color, progress);
}

void Interpolate_TransformCenter(TransformCenter* value, const TransformCenter* start, const TransformCenter* end, double progress) {
	value->centerX = NUM_INTERPOLATE(start->centerX, end->centerX, progress);
	value->centerY = NUM_INTERPOLATE(start->centerY, end->centerY, progress);
}

void Interpolate_TransformRotate(TransformRotate* value, const TransformRotate* start, const TransformRotate* end, double progress) {
	value->angle = NUM_INTERPOLATE(start->angle, end->angle, progress);
}