-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.cpp
262 lines (220 loc) · 8.48 KB
/
utils.cpp
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include "utils.h"
int HEIGHT = 1080;
int WIDTH = 1920;
SDL_Texture* LoadTexture(SDL_Renderer* renderer, std::string path) {
SDL_Surface* surface = SDL_LoadBMP(path.c_str());
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_FreeSurface(surface);
return texture;
}
std::vector<std::string> split_string(const std::string& str,
const std::string& delimiter)
{
std::vector<std::string> strings;
std::string::size_type pos = 0;
std::string::size_type prev = 0;
while ((pos = str.find(delimiter, prev)) != std::string::npos)
{
strings.push_back(str.substr(prev, pos - prev));
prev = pos + 1;
}
// To get the last substring (or only, if delimiter is not found)
strings.push_back(str.substr(prev));
return strings;
}
std::string ReadFile(std::string fname) {
FILE* fp = fopen(fname.c_str(), "r");
std::string buff = "";
char c;
while (!feof(fp)) {
buff += fgetc(fp);
}
return buff;
}
float Distance(double x1, double y1, double x2, double y2)
{
// Calculating distance
return sqrt(pow(x2 - x1, 2) +
pow(y2 - y1, 2) * 1.0);
}
double GetCurrentTime() {
auto now = std::chrono::high_resolution_clock::now();
auto now_ms = std::chrono::time_point_cast<std::chrono::nanoseconds>(now);
auto epoch = now_ms.time_since_epoch();
auto value = std::chrono::duration_cast<std::chrono::nanoseconds>(epoch);
double duration = value.count();
double duration_s = duration / 1000000000.0;
return duration_s;
}
double Approach(double current, double goal, double dt) {
double difference = goal - current;
if (difference > dt) {
return current + dt;
} if (difference < -dt) {
return current-dt;
}
return goal;
}
bool CompareDoubles (double A, double B)
{
double diff = A - B;
return (diff < EPSILON) && (-diff < EPSILON);
}
int clamp(int n, int lower, int upper) {
return std::max(lower, std::min(n, upper));
}
std::vector<SDL_Texture*> LoadAnimationFrames(SDL_Renderer* renderer, std::string animation_name) {
// read the entire directory
DIR* animation_dir = opendir(ANIMATION_DIRECTORY);
if (animation_dir == NULL) {
std::cout << "\n[Error] Unable to load animation: " << animation_name << " from " << ANIMATION_DIRECTORY;
}
struct dirent *d;
std::vector<std::string> files;
while ((d = readdir(animation_dir)) != NULL) {
if (d->d_type == DT_REG) {
files.push_back(d->d_name);
}
}
for (size_t i = 0; i < files.size(); i++) {
std::cout << files[i] << std::endl;
}
// go through every file in the directory and pick out the ones matching the animation name
std::string animation_name_temp = animation_name + "_0" + ".bmp";
std::vector<SDL_Texture*> animation_frames;
for (size_t i = 1; i < files.size()+1; i++) {
SDL_Texture* loaded_texture = LoadTexture(renderer, std::string(ANIMATION_DIRECTORY) + "/" + animation_name_temp);
if (loaded_texture != NULL) {
std::cout << "[^] Loaded animation frame: " << std::string(ANIMATION_DIRECTORY) + "/" + animation_name_temp << std::endl;
animation_frames.push_back(loaded_texture);
} else {
std::cout << "[Error] Unable to load animation frame: " << std::string(ANIMATION_DIRECTORY) + "/" + animation_name_temp << std::endl;
}
// remove file extension
size_t lastindex = animation_name_temp.find_last_of(".");
std::string animation_name_temp_rawname = animation_name_temp.substr(0, lastindex);
// change the frame number: ie: animation_0 --> animation_1
std::vector<std::string> split = split_string(animation_name_temp_rawname, "_");
animation_name_temp = split[0] + "_" + std::to_string(i) + ".bmp";
}
return animation_frames;
}
Animation LoadAnimations(SDL_Renderer* renderer, std::string animation_name) {
// read the entire directory
DIR* animation_dir = opendir(ANIMATION_DIRECTORY);
if (animation_dir == NULL) {
std::cout << "\n[Error] Unable to load animation: " << animation_name << " from " << ANIMATION_DIRECTORY;
}
struct dirent *d;
std::vector<std::string> files;
while ((d = readdir(animation_dir)) != NULL) {
if (d->d_type == DT_REG) {
files.push_back(d->d_name);
}
}
for (size_t i = 0; i < files.size(); i++) {
std::cout << files[i] << std::endl;
}
// go through every file in the directory and pick out the ones matching the animation name
std::string animation_name_temp = animation_name + "_0" + ".bmp";
std::vector<SDL_Texture*> animation_frames;
for (size_t i = 1; i < files.size()+1; i++) {
SDL_Texture* loaded_texture = LoadTexture(renderer, std::string(ANIMATION_DIRECTORY) + "/" + animation_name_temp);
if (loaded_texture != NULL) {
std::cout << "[^] Loaded animation frame: " << std::string(ANIMATION_DIRECTORY) + "/" + animation_name_temp << std::endl;
animation_frames.push_back(loaded_texture);
} else {
std::cout << "[Error] Unable to load animation frame: " << std::string(ANIMATION_DIRECTORY) + "/" + animation_name_temp << std::endl;
}
// remove file extension
size_t lastindex = animation_name_temp.find_last_of(".");
std::string animation_name_temp_rawname = animation_name_temp.substr(0, lastindex);
// change the frame number: ie: animation_0 --> animation_1
std::vector<std::string> split = split_string(animation_name_temp_rawname, "_");
animation_name_temp = split[0] + "_" + std::to_string(i) + ".bmp";
}
Animation a(renderer, animation_frames);
return a;
}
template <typename T>
size_t RemoveDuplicatesKeepOrder(std::vector<T>& vec)
{
std::set<T> seen;
auto newEnd = std::remove_if(vec.begin(), vec.end(), [&seen](const T& value)
{
if (seen.find(value) != std::end(seen))
return true;
seen.insert(value);
return false;
});
vec.erase(newEnd, vec.end());
return vec.size();
}
std::vector<std::string> LoadWorldNames() {
DIR* worlds_dir = opendir(WORLD_FOLDER_PATH);
if (worlds_dir == NULL) {
}
struct dirent *d;
std::vector<std::string> files;
while ((d = readdir(worlds_dir)) != NULL) {
if (d->d_type == DT_REG) {
files.push_back(d->d_name);
}
}
// get all the files and split them
std::vector<std::string> split_files;
std::vector<std::string>::iterator it;
for (it = files.begin(); it != files.end(); ++it) {
std::vector<std::string> file_split = split_string(*it, "_");
if (file_split.size() > 0) {
split_files.push_back(file_split[0]);
}
}
RemoveDuplicatesKeepOrder<std::string>(split_files);
for (size_t j = 0; j < split_files.size(); j++) {
std::cout << split_files[j] << std::endl;
}
return split_files;
}
float SampleLayeredNoise(float value, int seed){
float noise = 0;
float frequency = 1;
float factor = 1;
FastNoise myNoise; // Create a FastNoise object
myNoise.SetNoiseType(FastNoise::Simplex); // Set the desired noise type
myNoise.SetSeed(seed);
myNoise.SetFrequency(PERLIN_FREQUENCY);
myNoise.SetInterp(FastNoise::Linear);
for (int i=0; i<PERLIN_OCTAVES; i++){
noise = noise + abs(myNoise.GetNoise(value * frequency + i * 0.72354, ONED_PERLIN_STRIP_INDEX) * factor);
factor *= PERLIN_PERSISTENCE;
frequency *= PERLIN_ROUGHNESS;
}
return noise;
}
bool LeftMouseDown(SDL_Event ev) {
static bool mouse_down = false;
if (ev.type == SDL_MOUSEBUTTONDOWN && ev.button.button == SDL_BUTTON_LEFT) {
mouse_down = true;
} if (ev.type == SDL_MOUSEBUTTONUP && ev.button.button == SDL_BUTTON_LEFT) {
mouse_down = false;
}
return mouse_down;
}
bool RightMouseDown(SDL_Event ev) {
static bool mouse_down = false;
if (ev.type == SDL_MOUSEBUTTONDOWN && ev.button.button == SDL_BUTTON_RIGHT) {
mouse_down = true;
} if (ev.type == SDL_MOUSEBUTTONUP && ev.button.button == SDL_BUTTON_RIGHT) {
mouse_down = false;
}
return mouse_down;
}
void ReduceFraction(int &numerator, int &denominator) {
for (int i = denominator * numerator; i > 1; i--) {
if ((denominator % i == 0) && (numerator % i == 0)) {
denominator /= i;
numerator /= i;
}
}
}