-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain-raylib.cpp
More file actions
160 lines (133 loc) · 5.4 KB
/
Copy pathmain-raylib.cpp
File metadata and controls
160 lines (133 loc) · 5.4 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
// Conway's Game of Life using raylib
// Copyright (c) 2025 Faraz Fallahi <fffaraz@gmail.com>
#include "Common.hpp"
#include <raylib.h>
#include <array>
#include <atomic>
#include <chrono>
#include <cstdio>
#include <memory>
#include <string>
#include <thread>
static constexpr std::array<Color, 9> colorMap {
Color { 230, 41, 55, 255 }, // RED - 0 live neighbors
Color { 0, 228, 48, 255 }, // GREEN - 1 live neighbor
Color { 0, 121, 241, 255 }, // BLUE - 2 live neighbors
Color { 102, 191, 255, 255 }, // SKYBLUE - 3 live neighbors (cyan-ish)
Color { 200, 122, 255, 255 }, // MAGENTA - 4 live neighbors
Color { 253, 249, 0, 255 }, // YELLOW - 5 live neighbors
Color { 255, 255, 255, 255 }, // WHITE - 6 live neighbors
Color { 255, 255, 255, 255 }, // WHITE - 7 live neighbors
Color { 255, 255, 255, 255 }, // WHITE - 8 live neighbors
};
std::atomic_bool mouseRightPressed = false;
std::atomic_bool mouseLeftPressed = false;
std::atomic_int mouseX = 0;
std::atomic_int mouseY = 0;
static void updateGrid(GridType& grid)
{
auto [nextGrid, writeLock] = grid.writeBuffer();
if (mouseRightPressed.load()) {
nextGrid.clear();
grid.swap(std::move(writeLock));
return;
}
{
const auto [currGrid, readLock] = grid.readBuffer();
nextGrid.updateGrid(currGrid);
}
nextGrid.addNoise();
if (mouseLeftPressed.load()) {
const int x = mouseX.load() / CELL_SIZE;
const int y = mouseY.load() / CELL_SIZE;
if (x >= 0 && x < GRID_SIZE && y >= 0 && y < GRID_SIZE) {
nextGrid.toggleBlock({ x, y });
}
}
grid.swap(std::move(writeLock));
}
static void DrawTextOutlined(const char* text, int x, int y, int fontSize, Color color, Color outline)
{
DrawText(text, x - 1, y, fontSize, outline);
DrawText(text, x + 1, y, fontSize, outline);
DrawText(text, x, y - 1, fontSize, outline);
DrawText(text, x, y + 1, fontSize, outline);
DrawText(text, x, y, fontSize, color);
}
int main()
{
printAppInfo();
InitWindow(GRID_SIZE * CELL_SIZE, GRID_SIZE * CELL_SIZE, "Conway's Game of Life");
if (targetFPS > 0) {
SetTargetFPS(targetFPS);
}
// Heap-allocated: a GridType holds two grids inline (~8 MB at GRID_SIZE 2048),
// which overflows the stack if placed as a local.
auto gridPtr = std::make_unique<GridType>();
GridType& grid = *gridPtr;
// One GPU texture holds the whole grid at 1 pixel/cell. Each frame we rewrite
// its CPU-side pixel buffer, upload it once, and let the GPU scale it up by
// CELL_SIZE -- replacing up to GRID_SIZE^2 per-cell DrawPixel calls with a
// single upload and a single draw. gridImage.data doubles as our pixel buffer.
Image gridImage = GenImageColor(GRID_SIZE, GRID_SIZE, BLACK);
Texture2D gridTexture = LoadTextureFromImage(gridImage);
Color* const pixels = static_cast<Color*>(gridImage.data);
std::atomic<float> epochsPerSecond = 0.0f;
std::jthread updateThread([&grid, &epochsPerSecond](std::stop_token stop_token) {
auto epochStart = std::chrono::steady_clock::now();
int epochCount = 0;
while (!stop_token.stop_requested()) {
updateGrid(grid);
epochCount++;
const auto now = std::chrono::steady_clock::now();
const float elapsed = std::chrono::duration<float>(now - epochStart).count();
if (elapsed >= 1.0f) {
epochsPerSecond = epochCount / elapsed;
epochCount = 0;
epochStart = now;
}
}
});
while (!WindowShouldClose()) {
mouseLeftPressed.store(IsMouseButtonDown(MOUSE_BUTTON_LEFT));
mouseRightPressed.store(IsMouseButtonPressed(MOUSE_BUTTON_RIGHT));
const Vector2 pos = GetMousePosition();
mouseX.store(static_cast<int>(pos.x));
mouseY.store(static_cast<int>(pos.y));
BeginDrawing();
ClearBackground(BLACK);
int aliveCount = 0;
{
const auto [currGrid, lock] = grid.readBuffer();
// Texture row j is screen row y=j; column i is screen x=i.
for (int j = 0; j < GRID_SIZE; ++j) {
Color* const row = &pixels[j * GRID_SIZE];
for (int i = 0; i < GRID_SIZE; ++i) {
const Point p { i, j };
if (currGrid.get(p)) {
aliveCount++;
row[i] = colorMap[currGrid.countLiveNeighbors(p)];
} else {
row[i] = BLACK;
}
}
}
}
UpdateTexture(gridTexture, pixels);
DrawTextureEx(gridTexture, { 0.0f, 0.0f }, 0.0f, static_cast<float>(CELL_SIZE), WHITE);
const std::string aliveStr = "Alive: " + std::to_string(aliveCount);
DrawTextOutlined(aliveStr.c_str(), 10, 5, 24, WHITE, BLACK);
const float fps = static_cast<float>(GetFPS());
const float eps = epochsPerSecond.load();
const float cups = eps * (GRID_SIZE * GRID_SIZE / 1'000'000'000.0f);
char buffer[64];
snprintf(buffer, sizeof(buffer), "FPS: %.2f\nEPS: %.2f\nCUpS: %.3fe9", fps, eps, cups);
DrawTextOutlined(buffer, GetScreenWidth() - 200, 5, 24, WHITE, BLACK);
EndDrawing();
}
updateThread.request_stop();
UnloadTexture(gridTexture);
UnloadImage(gridImage);
CloseWindow();
return 0;
}