summaryrefslogtreecommitdiff
path: root/AnimationRender/F_Interpolate.c
blob: cc72163429b4407aa34cec51d5094b826522a77a (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
#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) * (1.0 - (perc)) + (b) * (perc))

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);
}