Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rubberduckycooly
GitHub Repository: rubberduckycooly/Sonic-1-2-2013-Decompilation
Path: blob/main/RSDKv4/Math.cpp
817 views
1
#include "RetroEngine.hpp"
2
#include <math.h>
3
#include <time.h>
4
5
int sinM7LookupTable[0x200];
6
int cosM7LookupTable[0x200];
7
8
int sin512LookupTable[0x200];
9
int cos512LookupTable[0x200];
10
11
int sin256LookupTable[0x100];
12
int cos256LookupTable[0x100];
13
14
byte arcTan256LookupTable[0x100 * 0x100];
15
16
void CalculateTrigAngles()
17
{
18
srand(time(NULL));
19
20
for (int i = 0; i < 0x200; ++i) {
21
sinM7LookupTable[i] = (sin((i / 256.0) * M_PI) * 4096.0);
22
cosM7LookupTable[i] = (cos((i / 256.0) * M_PI) * 4096.0);
23
}
24
25
cosM7LookupTable[0x00] = 0x1000;
26
cosM7LookupTable[0x80] = 0;
27
cosM7LookupTable[0x100] = -0x1000;
28
cosM7LookupTable[0x180] = 0;
29
30
sinM7LookupTable[0x00] = 0;
31
sinM7LookupTable[0x80] = 0x1000;
32
sinM7LookupTable[0x100] = 0;
33
sinM7LookupTable[0x180] = -0x1000;
34
35
for (int i = 0; i < 0x200; ++i) {
36
sin512LookupTable[i] = (sinf((i / 256.0) * M_PI) * 512.0);
37
cos512LookupTable[i] = (cosf((i / 256.0) * M_PI) * 512.0);
38
}
39
40
cos512LookupTable[0x00] = 0x200;
41
cos512LookupTable[0x80] = 0;
42
cos512LookupTable[0x100] = -0x200;
43
cos512LookupTable[0x180] = 0;
44
45
sin512LookupTable[0x00] = 0;
46
sin512LookupTable[0x80] = 0x200;
47
sin512LookupTable[0x100] = 0;
48
sin512LookupTable[0x180] = -0x200;
49
50
for (int i = 0; i < 0x100; i++) {
51
sin256LookupTable[i] = (sin512LookupTable[i * 2] >> 1);
52
cos256LookupTable[i] = (cos512LookupTable[i * 2] >> 1);
53
}
54
55
for (int Y = 0; Y < 0x100; ++Y) {
56
byte *atan = (byte *)&arcTan256LookupTable[Y];
57
for (int X = 0; X < 0x100; ++X) {
58
float angle = atan2f(Y, X);
59
*atan = (angle * 40.743664f);
60
atan += 0x100;
61
}
62
}
63
}
64
65
byte ArcTanLookup(int X, int Y)
66
{
67
int x = 0;
68
int y = 0;
69
70
x = abs(X);
71
y = abs(Y);
72
73
if (x <= y) {
74
while (y > 0xFF) {
75
x >>= 4;
76
y >>= 4;
77
}
78
}
79
else {
80
while (x > 0xFF) {
81
x >>= 4;
82
y >>= 4;
83
}
84
}
85
if (X <= 0) {
86
if (Y <= 0)
87
return arcTan256LookupTable[(x << 8) + y] + -0x80;
88
else
89
return -0x80 - arcTan256LookupTable[(x << 8) + y];
90
}
91
else if (Y <= 0)
92
return -arcTan256LookupTable[(x << 8) + y];
93
else
94
return arcTan256LookupTable[(x << 8) + y];
95
}
96
97