-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
750 lines (616 loc) · 17.6 KB
/
main.cpp
File metadata and controls
750 lines (616 loc) · 17.6 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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
#include <array>
#include <cmath>
#include <fstream>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>
#include <cstdio>
#include "raylib.h"
/**
* One electron in the Bohr-style visual model.
*/
struct Electron
{
int shellIndex;
float orbitRadius;
float angle;
float speed;
};
/**
* One chemical element loaded from elements.txt.
*/
struct Element
{
int protonCount;
std::string symbol;
std::string name;
};
/**
* Current atom state shown on screen.
*/
struct Atom
{
int protons;
int neutrons;
int electrons;
std::vector<Electron> electronCloud;
};
/**
* Simple shell capacities for the visual model.
*/
enum Orbit
{
K = 2,
L = 8,
M = 18,
N = 32,
O = 50,
P = 72,
Q = 98
};
constexpr int WINDOW_WIDTH = 1600;
constexpr int WINDOW_HEIGHT = 900;
constexpr float PANEL_WIDTH = 300.0f;
constexpr float NUCLEUS_RADIUS = 18.0f;
constexpr float FIRST_ORBIT_RADIUS = 50.0f;
constexpr float ORBIT_SPACING = 28.0f;
constexpr float ELECTRON_RADIUS = 5.0f;
/**
* Global element lookup by proton count.
*/
static std::unordered_map<int, Element> g_elements;
/**
* Loads all elements from a text file.
*
* Format per line:
* protonCount|symbol|name
*
* Example:
* 6|C|Carbon
*/
bool LoadElements(const std::string& filePath);
/**
* Returns the loaded element for a proton count.
* Returns nullptr if it is not found.
*/
const Element* GetElement(int protonCount);
/**
* Builds the Wikipedia URL for a proton count.
* Returns an empty string if the element is unknown.
*/
std::string GetElementWikipediaUrl(int protonCount);
/**
* Regenerates the Bohr-style electron shells.
*/
void GenerateElectronCloud(Atom& atom);
/**
* Updates electron angles.
*/
void UpdateElectrons(Atom& atom, float dt);
/**
* Draws the full atom.
*/
void DrawAtom(const Atom& atom, Vector2 center);
/**
* Draws the nucleus.
*/
void DrawNucleus(const Atom& atom, Vector2 center);
/**
* Draws orbit rings and shell labels.
*/
void DrawOrbits(const Atom& atom, Vector2 center);
/**
* Draws all electrons.
*/
void DrawElectrons(const Atom& atom, Vector2 center);
/**
* Draws atom information on the left side.
*/
void DrawAtomInfo(const Atom& atom, Vector2 center);
/**
* Draws the side control panel.
*/
void DrawSidePanel(Atom& atom, bool& electronsChanged, bool& screenshotRequested);
/**
* Draws a clickable button.
* Returns true on click.
*/
bool GuiButton(Rectangle rect, const char* text);
/**
* Draws a simple +/- integer editor.
* Returns true if the value changed.
*/
bool GuiValueEditor(const char* label, int& value, float x, float y, Color labelColor);
/**
* Draws a clickable text link.
* Returns true on click.
*/
bool GuiLink(Rectangle rect, const std::string& text);
int main()
{
InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "atomic2d");
SetTargetFPS(60);
SetWindowState(FLAG_WINDOW_RESIZABLE);
if (!LoadElements("elements.txt"))
{
TraceLog(LOG_ERROR, "Failed to load elements.txt");
}
Atom atom = {};
atom.protons = 6;
atom.neutrons = 6;
atom.electrons = 6;
GenerateElectronCloud(atom);
int screenshotCounter = 1;
float screenshotSavedTimer = 0.0f;
std::string screenshotSavedText;
while (!WindowShouldClose())
{
const float dt = GetFrameTime();
bool electronsChanged = false;
bool screenshotRequested = false;
UpdateElectrons(atom, dt);
if (screenshotSavedTimer > 0.0f)
{
screenshotSavedTimer -= dt;
if (screenshotSavedTimer < 0.0f)
{
screenshotSavedTimer = 0.0f;
}
}
BeginDrawing();
{
ClearBackground(BLACK);
const float usableWidth = static_cast<float>(GetScreenWidth()) - PANEL_WIDTH;
Vector2 atomCenter =
{
usableWidth * 0.5f,
static_cast<float>(GetScreenHeight()) * 0.5f
};
DrawAtom(atom, atomCenter);
DrawSidePanel(atom, electronsChanged, screenshotRequested);
if (screenshotSavedTimer > 0.0f)
{
DrawText(
screenshotSavedText.c_str(),
24,
GetScreenHeight() - 40,
20,
GREEN
);
}
}
EndDrawing();
if (electronsChanged)
{
GenerateElectronCloud(atom);
}
if (screenshotRequested)
{
char fileName[64];
std::snprintf(fileName, sizeof(fileName), "screenshot_%03d.png", screenshotCounter);
screenshotCounter++;
TakeScreenshot(fileName);
screenshotSavedText = std::string("Saved ") + fileName;
screenshotSavedTimer = 2.0f;
}
}
CloseWindow();
return 0;
}
bool LoadElements(const std::string& filePath)
{
std::ifstream file(filePath);
if (!file.is_open())
{
return false;
}
g_elements.clear();
std::string line;
while (std::getline(file, line))
{
if (line.empty())
{
continue;
}
std::stringstream ss(line);
std::string protonPart;
std::string symbolPart;
std::string namePart;
if (!std::getline(ss, protonPart, '|'))
{
continue;
}
if (!std::getline(ss, symbolPart, '|'))
{
continue;
}
if (!std::getline(ss, namePart))
{
continue;
}
std::stringstream protonStream(protonPart);
int protonCount = 0;
if (!(protonStream >> protonCount))
{
continue;
}
Element element = {};
element.protonCount = protonCount;
element.symbol = symbolPart;
element.name = namePart;
g_elements[protonCount] = element;
}
return true;
}
const Element* GetElement(int protonCount)
{
const auto it = g_elements.find(protonCount);
if (it == g_elements.end())
{
return nullptr;
}
return &it->second;
}
std::string GetElementWikipediaUrl(int protonCount)
{
const Element* element = GetElement(protonCount);
if (element == nullptr)
{
return "";
}
return "https://en.wikipedia.org/wiki/" + element->name;
}
void GenerateElectronCloud(Atom& atom)
{
atom.electronCloud.clear();
const std::array<int, 7> shellCapacities = { K, L, M, N, O, P, Q };
const std::array<float, 7> shellSpeeds = { 1.45f, 1.00f, 0.72f, 0.50f, 0.36f, 0.28f, 0.22f };
int remainingElectrons = atom.electrons;
for (int shellIndex = 0; shellIndex < static_cast<int>(shellCapacities.size()); shellIndex++)
{
const int capacity = shellCapacities[shellIndex];
const int electronsInShell = (remainingElectrons < capacity) ? remainingElectrons : capacity;
if (electronsInShell <= 0)
{
break;
}
const float orbitRadius = FIRST_ORBIT_RADIUS + ORBIT_SPACING * static_cast<float>(shellIndex);
const float orbitSpeed = shellSpeeds[shellIndex];
for (int i = 0; i < electronsInShell; i++)
{
Electron electron = {};
electron.shellIndex = shellIndex;
electron.orbitRadius = orbitRadius;
electron.angle = (2.0f * PI / static_cast<float>(electronsInShell)) * static_cast<float>(i);
electron.speed = orbitSpeed;
atom.electronCloud.push_back(electron);
}
remainingElectrons -= electronsInShell;
}
}
void UpdateElectrons(Atom& atom, float dt)
{
for (auto& electron : atom.electronCloud)
{
electron.angle += electron.speed * dt;
}
}
void DrawNucleus(const Atom& atom, Vector2 center)
{
const int totalNucleons = atom.protons + atom.neutrons;
if (totalNucleons <= 0)
{
DrawCircleV(center, NUCLEUS_RADIUS, DARKGRAY);
return;
}
float spreadRadius = 10.0f + static_cast<float>(totalNucleons) * 0.35f;
if (spreadRadius > 34.0f)
{
spreadRadius = 34.0f;
}
int drawnProtons = 0;
int drawnNeutrons = 0;
for (int i = 0; i < totalNucleons; i++)
{
bool drawProton = false;
if (drawnProtons < atom.protons && drawnNeutrons < atom.neutrons)
{
drawProton = (i % 2 == 0);
}
else if (drawnProtons < atom.protons)
{
drawProton = true;
}
const float angle = (2.0f * PI / static_cast<float>(totalNucleons)) * static_cast<float>(i);
float localRadius = spreadRadius * 0.45f;
if (totalNucleons > 1)
{
localRadius = spreadRadius * (0.35f + 0.30f * ((i % 3) / 2.0f));
}
const float x = center.x + cosf(angle) * localRadius;
const float y = center.y + sinf(angle) * localRadius;
if (drawProton)
{
DrawCircleV({ x, y }, 7.0f, RED);
drawnProtons++;
}
else
{
DrawCircleV({ x, y }, 7.0f, LIGHTGRAY);
drawnNeutrons++;
}
}
DrawCircleLines(
static_cast<int>(center.x),
static_cast<int>(center.y),
spreadRadius + 8.0f,
Color { 110, 110, 110, 255 }
);
}
void DrawOrbits(const Atom& atom, Vector2 center)
{
std::array<bool, 7> shellUsed = { false, false, false, false, false, false, false };
for (const auto& electron : atom.electronCloud)
{
if (electron.shellIndex >= 0 && electron.shellIndex < static_cast<int>(shellUsed.size()))
{
shellUsed[electron.shellIndex] = true;
}
}
for (int shellIndex = 0; shellIndex < static_cast<int>(shellUsed.size()); shellIndex++)
{
if (!shellUsed[shellIndex])
{
continue;
}
const float radius = FIRST_ORBIT_RADIUS + ORBIT_SPACING * static_cast<float>(shellIndex);
DrawCircleLines(
static_cast<int>(center.x),
static_cast<int>(center.y),
radius,
Color { 90, 90, 90, 255 }
);
const char* shellName = "?";
switch (shellIndex)
{
case 0:
shellName = "K";
break;
case 1:
shellName = "L";
break;
case 2:
shellName = "M";
break;
case 3:
shellName = "N";
break;
case 4:
shellName = "O";
break;
case 5:
shellName = "P";
break;
case 6:
shellName = "Q";
break;
default:
break;
}
DrawText(
shellName,
static_cast<int>(center.x + radius + 10.0f),
static_cast<int>(center.y - 10.0f),
20,
GRAY
);
}
}
void DrawElectrons(const Atom& atom, Vector2 center)
{
for (const auto& electron : atom.electronCloud)
{
const float x = center.x + cosf(electron.angle) * electron.orbitRadius;
const float y = center.y + sinf(electron.angle) * electron.orbitRadius;
DrawCircleV({ x, y }, ELECTRON_RADIUS, BLUE);
}
}
void DrawAtomInfo(const Atom& atom, Vector2 center)
{
const int charge = atom.protons - atom.electrons;
const int massNumber = atom.protons + atom.neutrons;
const Element* element = GetElement(atom.protons);
DrawText(TextFormat("Protons: %d", atom.protons), 24, 20, 22, RED);
DrawText(TextFormat("Neutrons: %d", atom.neutrons), 24, 48, 22, LIGHTGRAY);
DrawText(TextFormat("Electrons: %d", atom.electrons), 24, 76, 22, BLUE);
DrawText(TextFormat("Mass Number: %d", massNumber), 24, 116, 22, RAYWHITE);
DrawText(TextFormat("Charge: %+d", charge), 24, 144, 22, RAYWHITE);
if (element != nullptr)
{
DrawText(
TextFormat("Element: %s (%s)", element->name.c_str(), element->symbol.c_str()),
24,
172,
22,
GOLD
);
}
else
{
DrawText("Element: Unknown", 24, 172, 22, GOLD);
}
DrawCircleLines(
static_cast<int>(center.x),
static_cast<int>(center.y),
NUCLEUS_RADIUS,
Color { 180, 180, 180, 60 }
);
}
void DrawAtom(const Atom& atom, Vector2 center)
{
DrawNucleus(atom, center);
DrawOrbits(atom, center);
DrawElectrons(atom, center);
DrawAtomInfo(atom, center);
}
void DrawSidePanel(Atom& atom, bool& electronsChanged, bool& screenshotRequested)
{
const float panelX = static_cast<float>(GetScreenWidth()) - PANEL_WIDTH;
DrawRectangle(
static_cast<int>(panelX),
0,
static_cast<int>(PANEL_WIDTH),
GetScreenHeight(),
Color { 20, 20, 28, 255 }
);
DrawLine(
static_cast<int>(panelX),
0,
static_cast<int>(panelX),
GetScreenHeight(),
Color { 90, 90, 110, 255 }
);
DrawText("Atomic Controls", static_cast<int>(panelX + 18.0f), 20, 28, RAYWHITE);
const float startX = panelX + 20.0f;
float y = 80.0f;
GuiValueEditor("Protons", atom.protons, startX, y, RED);
y += 110.0f;
GuiValueEditor("Neutrons", atom.neutrons, startX, y, LIGHTGRAY);
y += 110.0f;
if (GuiValueEditor("Electrons", atom.electrons, startX, y, BLUE))
{
electronsChanged = true;
}
y += 120.0f;
const Rectangle resetButton = { startX, y, 220.0f, 44.0f };
const Rectangle neutralButton = { startX, y + 58.0f, 220.0f, 44.0f };
const Rectangle randomButton = { startX, y + 116.0f, 220.0f, 44.0f };
const Rectangle screenshotButton = { startX, y + 174.0f, 220.0f, 44.0f };
if (GuiButton(resetButton, "Reset"))
{
atom.protons = 6;
atom.neutrons = 6;
atom.electrons = 6;
electronsChanged = true;
}
if (GuiButton(neutralButton, "Make Neutral"))
{
atom.electrons = atom.protons;
electronsChanged = true;
}
if (GuiButton(randomButton, "Random Atom"))
{
atom.protons = GetRandomValue(1, 118);
atom.neutrons = GetRandomValue(atom.protons, atom.protons + 30);
atom.electrons = atom.protons;
electronsChanged = true;
}
if (GuiButton(screenshotButton, "Save Screenshot"))
{
screenshotRequested = true;
}
const int charge = atom.protons - atom.electrons;
const int massNumber = atom.protons + atom.neutrons;
const Element* element = GetElement(atom.protons);
DrawText("Summary", static_cast<int>(startX), static_cast<int>(y + 245.0f), 24, RAYWHITE);
if (element != nullptr)
{
DrawText(
TextFormat("Element: %s (%s)", element->name.c_str(), element->symbol.c_str()),
static_cast<int>(startX),
static_cast<int>(y + 280.0f),
22,
GOLD
);
}
else
{
DrawText("Element: Unknown", static_cast<int>(startX), static_cast<int>(y + 280.0f), 22, GOLD);
}
DrawText(TextFormat("Mass: %d", massNumber), static_cast<int>(startX), static_cast<int>(y + 308.0f), 22, RAYWHITE);
DrawText(TextFormat("Charge: %+d", charge), static_cast<int>(startX), static_cast<int>(y + 336.0f), 22, RAYWHITE);
if (element != nullptr)
{
const Rectangle linkRect =
{
startX,
y + 377.0f,
220.0f,
28.0f
};
if (GuiLink(linkRect, "Open Wikipedia"))
{
const std::string url = GetElementWikipediaUrl(atom.protons);
OpenURL(url.c_str());
}
}
}
bool GuiButton(Rectangle rect, const char* text)
{
const Vector2 mouse = GetMousePosition();
const bool hovered = CheckCollisionPointRec(mouse, rect);
const bool pressed = hovered && IsMouseButtonPressed(MOUSE_LEFT_BUTTON);
Color fillColor = Color { 45, 45, 60, 255 };
if (hovered)
{
fillColor = Color { 75, 75, 95, 255 };
}
DrawRectangleRec(rect, fillColor);
DrawRectangleLinesEx(rect, 2.0f, RAYWHITE);
const int fontSize = 22;
const int textWidth = MeasureText(text, fontSize);
DrawText(
text,
static_cast<int>(rect.x + rect.width * 0.5f - textWidth * 0.5f),
static_cast<int>(rect.y + rect.height * 0.5f - fontSize * 0.5f),
fontSize,
RAYWHITE
);
return pressed;
}
bool GuiValueEditor(const char* label, int& value, float x, float y, Color labelColor)
{
bool changed = false;
DrawText(label, static_cast<int>(x), static_cast<int>(y), 24, labelColor);
const Rectangle minusButton = { x, y + 34.0f, 42.0f, 42.0f };
const Rectangle plusButton = { x + 150.0f, y + 34.0f, 42.0f, 42.0f };
if (GuiButton(minusButton, "-"))
{
if (value > 0)
{
value--;
changed = true;
}
}
if (GuiButton(plusButton, "+"))
{
value++;
changed = true;
}
DrawText(TextFormat("%d", value), static_cast<int>(x + 88.0f), static_cast<int>(y + 43.0f), 26, RAYWHITE);
return changed;
}
bool GuiLink(Rectangle rect, const std::string& text)
{
const Vector2 mouse = GetMousePosition();
const bool hovered = CheckCollisionPointRec(mouse, rect);
const bool clicked = hovered && IsMouseButtonPressed(MOUSE_LEFT_BUTTON);
const Color textColor = hovered ? SKYBLUE : BLUE;
DrawText(
text.c_str(),
static_cast<int>(rect.x),
static_cast<int>(rect.y),
22,
textColor
);
const int textWidth = MeasureText(text.c_str(), 22);
DrawLine(
static_cast<int>(rect.x),
static_cast<int>(rect.y + 24.0f),
static_cast<int>(rect.x + textWidth),
static_cast<int>(rect.y + 24.0f),
textColor
);
return clicked;
}