Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Rubberduckycooly
GitHub Repository: Rubberduckycooly/RSDKv5-Decompilation
Path: blob/master/RSDKv5/RSDK/Core/Math.cpp
1162 views
1
#include "RSDK/Core/RetroEngine.hpp"
2
#include <math.h>
3
4
using namespace RSDK;
5
6
int32 RSDK::sin1024LookupTable[0x400];
7
int32 RSDK::cos1024LookupTable[0x400];
8
int32 RSDK::tan1024LookupTable[0x400];
9
int32 RSDK::asin1024LookupTable[0x400];
10
int32 RSDK::acos1024LookupTable[0x400];
11
12
int32 RSDK::sin512LookupTable[0x200];
13
int32 RSDK::cos512LookupTable[0x200];
14
int32 RSDK::tan512LookupTable[0x200];
15
int32 RSDK::asin512LookupTable[0x200];
16
int32 RSDK::acos512LookupTable[0x200];
17
18
int32 RSDK::sin256LookupTable[0x100];
19
int32 RSDK::cos256LookupTable[0x100];
20
int32 RSDK::tan256LookupTable[0x100];
21
int32 RSDK::asin256LookupTable[0x100];
22
int32 RSDK::acos256LookupTable[0x100];
23
24
uint8 RSDK::arcTan256LookupTable[0x100 * 0x100];
25
26
uint32 RSDK::randSeed = 0;
27
28
void RSDK::ClearTrigLookupTables()
29
{
30
memset(sin256LookupTable, 0, sizeof(sin256LookupTable));
31
memset(cos256LookupTable, 0, sizeof(cos256LookupTable));
32
memset(tan256LookupTable, 0, sizeof(tan256LookupTable));
33
memset(asin256LookupTable, 0, sizeof(asin256LookupTable));
34
memset(acos256LookupTable, 0, sizeof(acos256LookupTable));
35
memset(sin512LookupTable, 0, sizeof(sin512LookupTable));
36
memset(cos512LookupTable, 0, sizeof(cos512LookupTable));
37
memset(tan512LookupTable, 0, sizeof(tan512LookupTable));
38
memset(asin512LookupTable, 0, sizeof(asin512LookupTable));
39
memset(acos512LookupTable, 0, sizeof(acos512LookupTable));
40
memset(sin1024LookupTable, 0, sizeof(sin1024LookupTable));
41
memset(cos1024LookupTable, 0, sizeof(cos1024LookupTable));
42
memset(tan1024LookupTable, 0, sizeof(tan1024LookupTable));
43
memset(asin1024LookupTable, 0, sizeof(asin1024LookupTable));
44
memset(acos1024LookupTable, 0, sizeof(acos1024LookupTable));
45
memset(arcTan256LookupTable, 0, sizeof(arcTan256LookupTable));
46
randSeed = 0;
47
}
48
49
void RSDK::CalculateTrigAngles()
50
{
51
srand((uint32)time(NULL));
52
randSeed = rand();
53
54
for (int32 i = 0; i < 0x400; ++i) {
55
sin1024LookupTable[i] = (int32)(sinf((i / 512.f) * RSDK_PI) * 1024.f);
56
cos1024LookupTable[i] = (int32)(cosf((i / 512.f) * RSDK_PI) * 1024.f);
57
tan1024LookupTable[i] = (int32)(tanf((i / 512.f) * RSDK_PI) * 1024.f);
58
asin1024LookupTable[i] = (int32)((asinf(i / 1023.f) * 512.f) / RSDK_PI);
59
acos1024LookupTable[i] = (int32)((acosf(i / 1023.f) * 512.f) / RSDK_PI);
60
}
61
62
cos1024LookupTable[0x000] = 0x400;
63
cos1024LookupTable[0x100] = 0;
64
cos1024LookupTable[0x200] = -0x400;
65
cos1024LookupTable[0x300] = 0;
66
67
sin1024LookupTable[0x000] = 0;
68
sin1024LookupTable[0x100] = 0x400;
69
sin1024LookupTable[0x200] = 0;
70
sin1024LookupTable[0x300] = -0x400;
71
72
for (int32 i = 0; i < 0x200; ++i) {
73
sin512LookupTable[i] = (int32)(sinf((i / 256.f) * RSDK_PI) * 512.f);
74
cos512LookupTable[i] = (int32)(cosf((i / 256.f) * RSDK_PI) * 512.f);
75
tan512LookupTable[i] = (int32)(tanf((i / 256.f) * RSDK_PI) * 512.f);
76
asin512LookupTable[i] = (int32)((asinf(i / 511.f) * 256.f) / RSDK_PI);
77
acos512LookupTable[i] = (int32)((acosf(i / 511.f) * 256.f) / RSDK_PI);
78
}
79
80
cos512LookupTable[0x00] = 0x200;
81
cos512LookupTable[0x80] = 0;
82
cos512LookupTable[0x100] = -0x200;
83
cos512LookupTable[0x180] = 0;
84
85
sin512LookupTable[0x00] = 0;
86
sin512LookupTable[0x80] = 0x200;
87
sin512LookupTable[0x100] = 0;
88
sin512LookupTable[0x180] = -0x200;
89
90
for (int32 i = 0; i < 0x100; i++) {
91
sin256LookupTable[i] = (int32)((sin512LookupTable[i * 2] >> 1));
92
cos256LookupTable[i] = (int32)((cos512LookupTable[i * 2] >> 1));
93
tan256LookupTable[i] = (int32)((tan512LookupTable[i * 2] >> 1));
94
asin256LookupTable[i] = (int32)((asinf(i / 255.f) * 128.f) / RSDK_PI);
95
acos256LookupTable[i] = (int32)((acosf(i / 255.f) * 128.f) / RSDK_PI);
96
}
97
98
for (int32 y = 0; y < 0x100; ++y) {
99
uint8 *arcTan = (uint8 *)&arcTan256LookupTable[y];
100
101
for (int32 x = 0; x < 0x100; ++x) {
102
// 40.743664 = 0x100 / (2 * M_PI) (roughly)
103
*arcTan = (int32)(float)((float)atan2((float)y, x) * 40.743664f);
104
arcTan += 0x100;
105
}
106
}
107
}
108
109
uint8 RSDK::ArcTanLookup(int32 X, int32 Y)
110
{
111
int32 x = abs(X);
112
int32 y = abs(Y);
113
114
if (x <= y) {
115
while (y > 0xFF) {
116
x >>= 4;
117
y >>= 4;
118
}
119
}
120
else {
121
while (x > 0xFF) {
122
x >>= 4;
123
y >>= 4;
124
}
125
}
126
if (X <= 0) {
127
if (Y <= 0)
128
return arcTan256LookupTable[(x << 8) + y] + 0x80;
129
else
130
return 0x80 - arcTan256LookupTable[(x << 8) + y];
131
}
132
else if (Y <= 0)
133
return -arcTan256LookupTable[(x << 8) + y];
134
else
135
return arcTan256LookupTable[(x << 8) + y];
136
}
137
138