summaryrefslogtreecommitdiff
path: root/AnimationInterpolate.c
blob: d949cc5bf551f4130f5b31208be6aba278e131d3 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "AnimationInterpolate.h"
#include "AnimationObject.h"
#include "AnimationMove.h"
#include <string.h>

#define NUM_INTERPOLATE(a, b, perc) (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;
}

ARGB
A_Interpolate(const Animation* anim, PixelRendererParams a) {
	struct A_Interpolate* in = a.priv;

	if (in->interpolate == NULL) {
		fprintf(stderr, "Interpolation function cannot be NULL!\n");
		ARGB_set(&a.pixel, 0xFFFF00FF);
		return a.pixel;
	}

	double perc = (double)(a.frame.current - a.frame.start) / (a.frame.end - a.frame.start);
	if (perc < 0.0) perc = 0.0;
	if (perc > 1.0) perc = 1.0;

	/*
		 ?               ?
		a*(1-p) + b*p = x
		?
		a*(1-p1) + b*p1 = c   ==> a = (c - b*p1) / (1 - p1)
		     ?            ?
		c*(1-p2) + b*p2 = x

		?                    ?
		a*(1-p) + b*p = c*(1-p2) + b*p2
		                                           ?
		(c - b*p1) / (1 - p1) * (1-p) + b*p = c*(1-p2) + b*p2
		(c - b*p1) / (1 - p1) * (1-p) + b*p = c - c*p2 + b*p2
		(c - b*p1) / (1 - p1) * (1-p) + b*p = c + (b - c) * p2
		((c - b*p1) / (1 - p1) * (1-p) + b*p - c) / (b - c) = p2
		simplified:
		(p - p1) / (1 - p1) = p2
	 */

	in->interpolate(in->value, in->goalValue, (perc - in->prevPercentage) / (1.0 - in->prevPercentage));
	if (a.row == 0 && a.col == 0) {
		fprintf(stderr, "@ %f : %f -> %f ==> %d \n", perc, perc - in->prevPercentage, (perc - in->prevPercentage) / (1.0 - in->prevPercentage), ((struct AM_Linear*)in->value)->startCol);
	}

	in->prevPercentage = perc;

	return a.pixel;
}

void
AO_LineInterpolate(void* valueLine, const void* finalLine, double percentage) {
	struct AO_Line* value = valueLine;
	const struct AO_Line* final = finalLine;

	value->a = NUM_INTERPOLATE(value->a, final->a, percentage);
	value->b = NUM_INTERPOLATE(value->b, final->b, percentage);
	value->c = NUM_INTERPOLATE(value->c, final->c, percentage);
	value->width = NUM_INTERPOLATE(value->width, final->width, percentage);
	value->color = colorInterpolate(value->color, final->color, percentage);
}

void
AM_LinearInterpolate(void* valueIn, const void* finalIn, double percentage) {
	struct AM_Linear* value = valueIn;
	const struct AM_Linear* final = finalIn;

	value->startRow = NUM_INTERPOLATE(value->startRow, final->startRow, percentage);
	value->startCol = NUM_INTERPOLATE(value->startCol, final->startCol, percentage);
	value->dRow = NUM_INTERPOLATE(value->dRow, final->dRow, percentage);
	value->dCol = NUM_INTERPOLATE(value->dCol, final->dCol, percentage);
	value->wrapCoordinates = NUM_INTERPOLATE(value->wrapCoordinates, final->wrapCoordinates, percentage);
}

void
AM_SpinInterpolate(void* valueIn, const void* finalIn, double percentage) {
	struct AM_Spin* value = valueIn;
	const struct AM_Spin* final = finalIn;

	value->theta = NUM_INTERPOLATE(value->theta, final->theta, percentage);
	value->dTheta = NUM_INTERPOLATE(value->dTheta, final->dTheta, percentage);
	value->centerRow = NUM_INTERPOLATE(value->centerRow, final->centerRow, percentage);
	value->centerCol = NUM_INTERPOLATE(value->centerCol, final->centerCol, percentage);
}