-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
424 lines (348 loc) · 12.5 KB
/
Copy pathmain.cpp
File metadata and controls
424 lines (348 loc) · 12.5 KB
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#include <iostream>
#include <raylib.h>
#include <raymath.h>
#include <string>
#include <vector>
#include <random>
#include "definitions.hpp" // Contains constants relevent to program
using namespace std;
// Globals
int w = SCREEN_WIDTH;
int h = SCREEN_HEIGHT;
Color bgColor = BG_COLOR;
Color fgColor = {static_cast<unsigned char>(255 - bgColor.r),
static_cast<unsigned char>(255 - bgColor.g),
static_cast<unsigned char>(255 - bgColor.b),
255}; // Opposite of background color
int gridSize = GRID_SIZE; // square grid
float amplitude = AMPLITUDE;
float oscilSpeed = OSCIl_SPEED;
unsigned short oscilOption = OSCIL_OPTION; // for different altitude functions
string imgFiles[IMG_ARRAY_SIZE] = {
"assets/tile_1.png",
"assets/tile_2.png",
"assets/tile_3.png",
"assets/tile_4.png",
"assets/tile_5.png",
};
size_t imgFilesSize = IMG_ARRAY_SIZE;
Image tileImg;
Texture tileTexArray[IMG_ARRAY_SIZE] = {};
Texture tileTex;
float stddev = DIST_STDDEV;
int tileScale = TILE_SCALE;
vector<int> tileMap;
// Function Declarations
void handleEvents();
void drawGame();
void drawTile(Texture &tile, int x, int y, Vector2 startPos, int size, float altitude, bool showOutline = false);
void drawText(bool showText);
unsigned int prepareAssets(string files[], size_t limit, int scale = 2);
Vector2 transform(Vector2 v);
void arrangeRandomTiles();
// Entry Point
int main()
{
SetTargetFPS(FPS);
SetTraceLogLevel(LOG_ERROR);
SetConfigFlags(FLAG_WINDOW_RESIZABLE);
if (FULLSCREEN) // use full screen
{
int monitor = GetCurrentMonitor();
InitWindow(GetMonitorWidth(monitor), GetMonitorHeight(monitor), SCREEN_TITLE);
w = GetScreenWidth();
h = GetScreenHeight();
ToggleFullscreen();
}
else
{
InitWindow(w, h, SCREEN_TITLE);
}
// gridSize = round(w / 100.f);
if (w < 600)
{
tileScale = 1;
}
if (prepareAssets(imgFiles, imgFilesSize, tileScale)) // Load Image, perform relevent pre-operations and load it as a texture.
cout
<< "Loaded texture successfully" << "\n";
else
{
cout << "Texture loading failed" << "\n";
return -1;
}
tileMap.resize(static_cast<long unsigned int>(gridSize * gridSize), 3); // initialize vector with a default value
arrangeRandomTiles(); // allocate a normal distribution biased random index to each tile position
while (!WindowShouldClose())
{
// if (IsWindowFocused()) // does not work well when ported to webassembly
// handleEvents();
handleEvents();
drawGame();
};
return 0;
}
void handleEvents()
{
if (IsWindowResized())
{
w = GetScreenWidth();
h = GetScreenHeight();
cout << w << "\n";
if (500 <= w and w < 900)
{
tileScale = 2;
}
else if (0 < w && w < 500)
{
tileScale = 1;
}
else
{
tileScale = TILE_SCALE;
}
if (prepareAssets(imgFiles, imgFilesSize, tileScale)) // Load Image, perform relevent pre-operations and load it as a texture.
cout
<< "Loaded texture successfully" << "\n";
else
{
cout << "Texture loading failed" << "\n";
}
tileMap.resize(static_cast<long unsigned int>(gridSize * gridSize), 3); // initialize vector with a default value
};
// Amplitude
if (IsKeyDown(KEY_U))
amplitude += .5f;
if (IsKeyDown(KEY_J))
amplitude -= .5f;
// Oscillation Speed
if (IsKeyPressed(KEY_I))
oscilSpeed += .5f;
if (IsKeyPressed(KEY_K))
oscilSpeed -= .5f;
// Grid Size
if (IsKeyPressed(KEY_O))
{
gridSize += 1;
arrangeRandomTiles();
}
if (IsKeyPressed(KEY_L))
{
gridSize -= 1;
arrangeRandomTiles();
}
// Grid Size
if (IsKeyPressed(KEY_Y))
{
stddev += .4f;
stddev = Clamp(float(stddev), 0.0f, 3.0f);
arrangeRandomTiles();
}
if (IsKeyPressed(KEY_H))
{
stddev -= .4f;
stddev = Clamp(float(stddev), 0.0f, 3.0f);
arrangeRandomTiles();
}
if (IsKeyPressed(KEY_ONE))
oscilOption = 1;
if (IsKeyPressed(KEY_TWO))
oscilOption = 2;
if (IsKeyPressed(KEY_THREE))
oscilOption = 3;
if (IsKeyPressed(KEY_FOUR))
oscilOption = 4;
if (IsKeyPressed(KEY_FIVE))
oscilOption = 5;
if (IsKeyPressed(KEY_SIX))
oscilOption = 6;
if (IsKeyPressed(KEY_SEVEN))
oscilOption = 7;
if (IsKeyPressed(KEY_EIGHT))
oscilOption = 8;
if (IsKeyPressed(KEY_NINE))
oscilOption = 9;
// Revert to original values
if (IsKeyPressed(KEY_R))
{
gridSize = GRID_SIZE;
amplitude = AMPLITUDE;
oscilSpeed = OSCIl_SPEED;
stddev = DIST_STDDEV;
arrangeRandomTiles();
}
amplitude = Clamp(amplitude, 0.f, MAX_AMPLITUDE);
oscilSpeed = Clamp(oscilSpeed, 0.f, MAX_OSCIL_SPEED);
gridSize = static_cast<int>(Clamp((float)gridSize, 1.f, float(MAX_GRID_SIZE)));
// cout << "Grid: " << gridSize << "x" << gridSize << " ";
// cout << "Oscillation Speed: " << oscilSpeed << " ";
// cout << "Amplitude: " << amplitude << "\n";
};
void drawGame()
{
BeginDrawing();
ClearBackground(bgColor);
Vector2 startPos = {(((float)w / 2.f)), // to center a unit tile to its center
(float)h / 2.f};
for (int rowIndex = 0; rowIndex < gridSize; rowIndex++)
{
for (int colIndex = 0; colIndex < gridSize; colIndex++)
{
int i = (rowIndex * gridSize) + colIndex;
tileTex = tileTexArray[tileMap[static_cast<unsigned long int>(i)]];
// auto getAlt = [&](float speed, float maxAlt, unsigned short option)
// {
// switch (option)
// {
// case 1:
// return sinf((float)rowIndex + (float)GetTime() * speed) * maxAlt; // along row
// break;
// case 2:
// return sinf((float)colIndex + (float)GetTime() * speed) * maxAlt; // along col
// break;
// case 3:
// default:
// return sinf((float)rowIndex + (float)GetTime() * speed) * sinf((float)colIndex + (float)GetTime() * speed) * maxAlt; // along both
// }
// };
auto getAlt = [&](float speed, float maxAlt, unsigned short option)
{
float t = (float)GetTime() * speed;
float x = (float)rowIndex;
float y = (float)colIndex;
// Center of grid (adjust if needed)
float cx = startPos.x * 0.5f;
float cy = startPos.y * 0.5f;
float dx = x - cx;
float dy = y - cy;
float r = sqrtf(dx * dx + dy * dy);
float theta = atan2f(dy, dx);
switch (option)
{
// -------------------------
// ORIGINAL PATTERNS
// -------------------------
case 1: // wave along rows
return sinf(x + t) * maxAlt;
case 2: // wave along columns
return sinf(y + t) * maxAlt;
case 3: // grid interference
return sinf(x + t) * sinf(y + t) * maxAlt;
case 4: // standing radial wave
return sinf(t) * cosf(r) * maxAlt;
case 5: // traveling radial ripple
return sinf(t - r) * maxAlt;
case 6: // checkerboard pulse
return ((rowIndex + colIndex) & 1 ? 1.0f : -1.0f) * sinf(t) * maxAlt;
case 7: // diagonal plane wave
return sinf(x + y - t) * maxAlt;
case 8: // spiral wave
return sinf(theta + t - r * 0.5f) * maxAlt;
case 9: // row/column phase offset
return (sinf(t + x * 0.5f) + sinf(t + y * 0.5f)) * 0.5f * maxAlt;
default:
return 0.0f;
}
};
drawTile(tileTex,
colIndex,
rowIndex,
startPos,
gridSize,
getAlt(oscilSpeed, amplitude, oscilOption),
false);
// cout << "Grid: " << gridSize << "x" << gridSize << " ";
// cout << "Oscillation Speed: " << oscilSpeed << " ";
// cout << "Amplitude: " << amplitude << "\n";
}
}
drawText(SHOW_TEXT);
EndDrawing();
};
void drawTile(Texture &tile, int x, int y, Vector2 startPos, int size, float altitude, bool showOutline)
{
Vector2 isoCoords = transform({float(x * tile.width), float(y * tile.height)}); // isometric transformation
isoCoords.x = startPos.x + (isoCoords.x / 2.f) - (float)(tile.width / 2);
isoCoords.y = startPos.y + (isoCoords.y / 2.f) - (float)(tile.height * size / 4);
isoCoords.y -= altitude; // Makes the tile appear elevated
if (showOutline)
DrawRectangleLines((int)isoCoords.x, (int)isoCoords.y, tile.width, tile.height, RED); // Show outline of tiles
DrawTexture(tile, (int)isoCoords.x, (int)isoCoords.y, fgColor);
}
void drawText(bool showText)
{
if (showText)
{
// Top Right Text
int vertInterval = 20;
int startDistVert = 5;
DrawText(TextFormat("Grid: %dx%d", gridSize, gridSize), 5, startDistVert + (vertInterval * 0), 20, fgColor);
DrawText(TextFormat("Oscillation Speed: %.1f", oscilSpeed), 5, startDistVert + (vertInterval * 1), 20, fgColor);
DrawText(TextFormat("Amplitude: %.1f", amplitude), 5, startDistVert + (vertInterval * 2), 20, fgColor);
DrawText(TextFormat("Standard Deviation: %.1f", stddev), 5, startDistVert + (vertInterval * 3), 20, fgColor);
// Bottom Left Text
vertInterval = 15;
startDistVert = 15;
DrawText("( O/L ) for Grid Size", 5, h - (5 * vertInterval + startDistVert), 10, fgColor);
DrawText("( I/K ) for Oscillation speed", 5, h - (4 * vertInterval + startDistVert), 10, fgColor);
DrawText("( U/J ) for Amplitude", 5, h - (3 * vertInterval + startDistVert), 10, fgColor);
DrawText("( Y/H ) for Std Dev", 5, h - (2 * vertInterval + startDistVert), 10, fgColor);
DrawText("( 1, 2, ..., 9 ) for Options", 5, h - (1 * vertInterval + startDistVert), 10, fgColor);
DrawText("( R ) to Reset", 5, h - (0 * vertInterval + startDistVert), 10, fgColor);
}
};
unsigned int prepareAssets(string files[], size_t limit, int scale)
{
for (unsigned long int i = 0; i < limit; i++)
{
if (tileTexArray[i].id)
{
UnloadTexture(tileTexArray[i]);
}
// cout << (tileTexArray[i].id) << "\n";
// cout << "Loaded Texture (" << files[i] << ") with width: " << tileTexArray[i].width << " and height: " << tileTexArray[i].height << "\n";
tileImg = LoadImage(files[i].c_str()); // upload to RAM
ImageResizeNN(&tileImg, tileImg.width * scale, tileImg.height * scale); // 32x32 -> 64x64 (w/ nearest neighbour)
tileTexArray[i] = LoadTextureFromImage(tileImg); // upload to VRAM
UnloadImage(tileImg); // unload from RAM
if (!tileTexArray[i].id)
{
return tileTexArray[i].id;
}
}
return 1;
}
Vector2 transform(Vector2 v)
{
/**
* Apply matrix transformation
* M = [+1.0 -1.0]
* [+0.5 +0.5]
* v = <x, y>
* v' = Mv
*/
return {1.0f * v.x - 1.0f * v.y,
0.5f * v.x + 0.5f * v.y};
}
void arrangeRandomTiles()
{
random_device rd;
mt19937 gen(rd());
float mean = floor((float)imgFilesSize / 2.f);
normal_distribution<float> dist(mean, stddev);
tileMap.resize(static_cast<unsigned long int>(gridSize * gridSize), 3);
for (int i = 0; i < gridSize * gridSize; i++)
{
double x;
do
{
x = dist(gen);
} while (x < 0.0f || x > (float)(imgFilesSize)-1);
// cout << round(x) << "\n";
// cout << tileMap.size() << "\t" << tileMap.capacity() << "\n";
tileMap.at(static_cast<unsigned long int>(i)) = int(round(x));
// tileMap[i] = int(round(x));
// tileMap.push_back(GetRandomValue(0, imgFilesSize - 1));
// tileMap.push_back(2);
}
}