-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrendering.cpp
More file actions
353 lines (321 loc) · 15.9 KB
/
Copy pathrendering.cpp
File metadata and controls
353 lines (321 loc) · 15.9 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
#include "rendering.h"
#include <algorithm>
#include <cmath>
#include <string>
using namespace std;
static constexpr float kPi = 3.14159f;
static Color MiniBodyColor(BodyType type, Color fallback) {
if (type == BLACK_HOLE) return {255, 150, 30, 255};
if (type == WHITE_HOLE) return {220, 250, 255, 255};
if (type == NEUTRON_STAR) return {170, 225, 255, 255};
if (type == STAR) return {255, 210, 70, 255};
if (type == ASTEROID) return {170, 145, 110, 255};
return fallback;
}
static const char* ModeLabel(PhysicsMode mode) {
return mode == PHYSICS_REALISM ? "real" : "sandbox";
}
static string StatsLine(const vector<Body>& bodies, const RenderState& state) {
string stats =
to_string(bodies.size()) + " bodies | " +
ModeLabel(state.physicsMode) + " | collisions " +
string(state.collisionsEnabled ? "on" : "off") + " | " +
to_string(state.timeScale).substr(0, 3) + "x | mass " +
to_string((int)state.spawnMass) + " | zoom " +
to_string(state.zoom).substr(0, 4) + "x";
if (IsValidBodyIndex((int)bodies.size(), state.selectedBodyIndex)) {
stats += " | ";
stats += state.followSelectedBody ? "tracking " : "selected ";
stats += BodyTypeLabel(bodies[state.selectedBodyIndex].type);
}
return stats;
}
static void DrawTextLine(const string& text, int x, int y, int size, Color color) {
DrawText(text.c_str(), x, y, size, color);
}
static void DrawFullHud(const vector<Body>& bodies, const RenderState& state) {
DrawRectangle(10, 10, 392, 150, Fade(BLACK, 0.48f));
DrawRectangleLines(10, 10, 392, 150, Fade(WHITE, 0.12f));
DrawText("GraSim", 24, 20, 26, WHITE);
DrawTextLine(StatsLine(bodies, state), 24, 54, 16, LIGHTGRAY);
DrawText("left/right drag: launch | wheel: zoom | mid drag: pan", 24, 78, 16, Fade(WHITE, 0.78f));
DrawText("click body: select | l follow | esc clear", 24, 100, 16, Fade(WHITE, 0.78f));
DrawText("a burst | b binary | h black hole | n neutron | m mode", 24, 122, 16, Fade(WHITE, 0.78f));
DrawText("u ui | / help | tab map | 4-7 presets", 24, 142, 14, Fade(SKYBLUE, 0.85f));
}
static void DrawCompactHud(const vector<Body>& bodies, const RenderState& state) {
string stats = StatsLine(bodies, state);
int width = MeasureText(stats.c_str(), 16) + 28;
DrawRectangle(14, state.screenHeight - 44, width, 30, Fade(BLACK, 0.50f));
DrawRectangleLines(14, state.screenHeight - 44, width, 30, Fade(WHITE, 0.12f));
DrawText(stats.c_str(), 28, state.screenHeight - 36, 16, WHITE);
}
static void DrawHelpOverlay(const RenderState& state) {
int w = 520;
int h = 296;
int x = state.screenWidth / 2 - w / 2;
int y = state.screenHeight / 2 - h / 2;
DrawRectangle(x, y, w, h, Fade(BLACK, 0.72f));
DrawRectangleLines(x, y, w, h, Fade(WHITE, 0.18f));
DrawText("controls", x + 22, y + 18, 24, WHITE);
DrawText("left drag: launch planet right drag: launch heavy star", x + 22, y + 56, 16, LIGHTGRAY);
DrawText("wheel: zoom middle drag: pan", x + 22, y + 80, 16, LIGHTGRAY);
DrawText("a: asteroid burst b: binary stars", x + 22, y + 104, 16, LIGHTGRAY);
DrawText("h: black hole w: white hole (sandbox)", x + 22, y + 128, 16, LIGHTGRAY);
DrawText("n: neutron star m: sandbox / real", x + 22, y + 152, 16, LIGHTGRAY);
DrawText("x: collisions f: gravity fields", x + 22, y + 176, 16, LIGHTGRAY);
DrawText("click body: select l: follow selected", x + 22, y + 200, 16, LIGHTGRAY);
DrawText("esc: clear selection 4/5/6/7: quick presets", x + 22, y + 224, 16, LIGHTGRAY);
DrawText("space: pause r: reset c: clear u: ui tab: minimap", x + 22, y + 248, 16, SKYBLUE);
DrawText("presets: binary collapse / neutron feeding / black hole debris / orbit art", x + 22, y + 272, 14, Fade(SKYBLUE, 0.85f));
}
static void DrawMinimap(const vector<Body>& bodies, const RenderState& state) {
const float mapSize = 178.0f;
const float padding = 10.0f;
float x = (float)state.screenWidth - mapSize - 18.0f;
float y = 18.0f;
DrawRectangle((int)x, (int)y, (int)mapSize, (int)mapSize, Fade(BLACK, 0.52f));
DrawRectangleLines((int)x, (int)y, (int)mapSize, (int)mapSize, Fade(WHITE, 0.16f));
DrawText("map", (int)x + 12, (int)y + 9, 14, Fade(WHITE, 0.75f));
if (bodies.empty()) {
DrawText("empty", (int)x + 62, (int)y + 82, 14, Fade(WHITE, 0.42f));
return;
}
float viewMinX = state.camera.x;
float viewMinY = state.camera.y;
float viewMaxX = state.camera.x + state.screenWidth / state.zoom;
float viewMaxY = state.camera.y + state.screenHeight / state.zoom;
float minX = viewMinX;
float minY = viewMinY;
float maxX = viewMaxX;
float maxY = viewMaxY;
for (const Body& body : bodies) {
minX = min(minX, body.position.x);
minY = min(minY, body.position.y);
maxX = max(maxX, body.position.x);
maxY = max(maxY, body.position.y);
}
float margin = max(100.0f, max(maxX - minX, maxY - minY) * 0.08f);
minX -= margin;
minY -= margin;
maxX += margin;
maxY += margin;
float worldWidth = maxX - minX;
float worldHeight = maxY - minY;
float scale = MinimapWorldScale(worldWidth, worldHeight, mapSize, padding);
float content = mapSize - padding * 2.0f;
float offsetX = x + padding + (content - worldWidth * scale) * 0.5f;
float offsetY = y + padding + (content - worldHeight * scale) * 0.5f;
auto mapPoint = [&](Vector2 p) -> Vector2 {
return {offsetX + (p.x - minX) * scale, offsetY + (p.y - minY) * scale};
};
for (const Body& body : bodies) {
Vector2 p = mapPoint(body.position);
float dot = body.type == BLACK_HOLE || body.type == WHITE_HOLE || body.type == NEUTRON_STAR ? 3.0f : 2.0f;
if (body.type == STAR) dot = 2.6f;
DrawCircleV(p, dot, Fade(MiniBodyColor(body.type, body.color), 0.85f));
}
if (IsValidBodyIndex((int)bodies.size(), state.selectedBodyIndex)) {
Vector2 p = mapPoint(bodies[state.selectedBodyIndex].position);
DrawCircleLinesV(p, state.followSelectedBody ? 7.0f : 5.5f, Fade(WHITE, 0.90f));
if (state.followSelectedBody) DrawCircleLinesV(p, 9.0f, Fade(SKYBLUE, 0.45f));
}
Vector2 viewA = mapPoint({viewMinX, viewMinY});
Vector2 viewB = mapPoint({viewMaxX, viewMaxY});
DrawRectangleLines(
(int)viewA.x,
(int)viewA.y,
max(2, (int)(viewB.x - viewA.x)),
max(2, (int)(viewB.y - viewA.y)),
Fade(WHITE, 0.55f)
);
}
void DrawSimulation(
const vector<Body>& bodies,
const vector<Shockwave>& shockwaves,
const vector<Particle>& particles,
const RenderState& state
)
{
auto W2S = [&](Vector2 w) -> Vector2 {
return {(w.x - state.camera.x) * state.zoom, (w.y - state.camera.y) * state.zoom};
};
ClearBackground({8, 10, 18, 225});
float gridSize = 60.0f;
float gx0 = floor(state.camera.x / gridSize) * gridSize;
float gy0 = floor(state.camera.y / gridSize) * gridSize;
for (float wx = gx0; wx < state.camera.x + state.screenWidth / state.zoom + gridSize; wx += gridSize)
for (float wy = gy0; wy < state.camera.y + state.screenHeight / state.zoom + gridSize; wy += gridSize)
DrawPixelV({(wx - state.camera.x) * state.zoom, (wy - state.camera.y) * state.zoom}, Fade(WHITE, 0.10f));
float starCell = 120.0f;
float sx0 = floor(state.camera.x / starCell) * starCell;
float sy0 = floor(state.camera.y / starCell) * starCell;
for (float wx = sx0; wx < state.camera.x + state.screenWidth / state.zoom + starCell; wx += starCell) {
for (float wy = sy0; wy < state.camera.y + state.screenHeight / state.zoom + starCell; wy += starCell) {
int cx = (int)floor(wx / starCell);
int cy = (int)floor(wy / starCell);
int hash = cx * 73856093 ^ cy * 19349663;
float ox = ((hash & 0xFF) / 255.0f) * starCell;
float oy = (((hash >> 8) & 0xFF) / 255.0f) * starCell;
float sx = (wx + ox - state.camera.x) * state.zoom;
float sy = (wy + oy - state.camera.y) * state.zoom;
if (sx >= 0 && sx < state.screenWidth && sy >= 0 && sy < state.screenHeight)
DrawPixelV({sx, sy}, Fade(WHITE, 0.45f));
}
}
if (state.showGravityField) {
const float thresholds[3] = {20.0f, 5.0f, 1.0f};
const float alphas[3] = {0.35f, 0.22f, 0.12f};
for (const Body& body : bodies) {
for (int t = 0; t < 3; t++) {
float rSq = state.gravityStrength * body.mass / thresholds[t] - state.softening * state.softening;
if (rSq > 0) {
DrawCircleLinesV(W2S(body.position), sqrt(rSq) * state.zoom, Fade(body.color, alphas[t]));
}
}
}
}
for (const Body& body : bodies) {
for (int i = 1; i < (int)body.trail.size(); i++) {
float alpha = (float)i / (float)body.trail.size();
DrawLineV(
W2S(body.trail[i - 1]),
W2S(body.trail[i]),
Fade(body.color, alpha * 0.55f)
);
}
}
for (const Shockwave& sw : shockwaves) {
float r = sw.radius * state.zoom;
Vector2 sp = W2S(sw.position);
float a = sw.life;
if (sw.flash) {
DrawCircleV(sp, r, Fade(WHITE, a * 0.95f));
DrawCircleV(sp, r * 0.6f, Fade(WHITE, a * 0.5f));
} else {
DrawCircleLinesV(sp, r, Fade(sw.color, a * 0.90f));
DrawCircleLinesV(sp, r * 0.98f, Fade(sw.color, a * 0.65f));
DrawCircleLinesV(sp, r * 0.96f, Fade(sw.color, a * 0.40f));
DrawCircleLinesV(sp, r * 0.93f, Fade(WHITE, a * 0.15f));
DrawCircleLinesV(sp, r * 1.02f, Fade(WHITE, a * 0.10f));
}
}
for (const Particle& p : particles) {
Vector2 sp = W2S(p.position);
DrawCircleV(sp, p.radius * state.zoom, Fade(p.color, p.life));
}
for (const Body& body : bodies) {
Vector2 sp = W2S(body.position);
float r = body.visualRadius * state.zoom;
if (body.type == ASTEROID) {
DrawPoly(sp, 7, r, body.rotation, body.color);
DrawPolyLines(sp, 7, r, body.rotation, Fade(WHITE, 0.25f));
} else if (body.type == PLANET) {
DrawCircleV(sp, r + 4.0f * state.zoom, Fade(body.color, 0.15f));
DrawCircleV(sp, r, body.color);
DrawEllipse((int)sp.x, (int)sp.y, r, r * 0.18f, Fade(BLACK, 0.3f));
DrawCircleV(sp, max(2.0f, r * 0.35f), WHITE);
} else if (body.type == STAR) {
DrawCircleV(sp, r + 14.0f * state.zoom, Fade(body.color, 0.05f));
DrawCircleV(sp, r + 8.0f * state.zoom, Fade(body.color, 0.10f));
DrawCircleV(sp, r + 3.0f * state.zoom, Fade(body.color, 0.20f));
DrawCircleV(sp, r, body.color);
for (int ray = 0; ray < 8; ray++) {
float angle = ray * (kPi / 4.0f) + body.rotation * kPi / 180.0f;
float len = r + 18.0f * state.zoom;
DrawLineV(sp, {sp.x + cosf(angle) * len, sp.y + sinf(angle) * len}, Fade(body.color, 0.25f));
}
DrawCircleV(sp, max(3.0f, r * 0.4f), WHITE);
} else if (body.type == BLACK_HOLE) {
DrawCircleV(sp, r * 2.8f, Fade(ORANGE, 0.04f));
DrawCircleV(sp, r * 2.0f, Fade(ORANGE, 0.10f));
DrawCircleV(sp, r * 1.4f, Fade({255, 140, 0, 255}, 0.25f));
float rot = body.rotation * kPi / 180.0f;
for (int ray = 0; ray < 6; ray++) {
float angle = ray * (kPi / 3.0f) + rot;
DrawLineV(
{sp.x + cosf(angle) * r * 1.05f, sp.y + sinf(angle) * r * 1.05f},
{sp.x + cosf(angle) * r * 1.85f, sp.y + sinf(angle) * r * 1.85f},
Fade({255, 180, 30, 255}, 0.5f)
);
}
DrawCircleV(sp, r, {0, 0, 0, 255});
DrawCircleLinesV(sp, r, Fade({255, 120, 0, 255}, 0.9f));
} else if (body.type == WHITE_HOLE) {
DrawCircleV(sp, r * 3.5f, Fade(WHITE, 0.03f));
DrawCircleV(sp, r * 2.2f, Fade(SKYBLUE, 0.07f));
DrawCircleV(sp, r * 1.5f, Fade(WHITE, 0.18f));
DrawCircleV(sp, r, {220, 250, 255, 255});
float rot = body.rotation * kPi / 180.0f;
for (int ray = 0; ray < 12; ray++) {
float angle = ray * (kPi / 6.0f) + rot;
DrawLineV(
{sp.x + cosf(angle) * r, sp.y + sinf(angle) * r},
{sp.x + cosf(angle) * (r + 22.0f * state.zoom), sp.y + sinf(angle) * (r + 22.0f * state.zoom)},
Fade(WHITE, 0.28f)
);
}
DrawCircleV(sp, max(3.0f, r * 0.45f), WHITE);
} else if (body.type == NEUTRON_STAR) {
float pulse = 0.5f + 0.5f * sinf(body.rotation * kPi / 180.0f * 3.0f);
DrawCircleV(sp, r * 4.0f, Fade({100, 180, 255, 255}, 0.03f + 0.04f * pulse));
DrawCircleV(sp, r * 2.5f, Fade({160, 220, 255, 255}, 0.08f + 0.07f * pulse));
DrawCircleV(sp, r * 1.4f, Fade(WHITE, 0.30f));
DrawCircleV(sp, r, {180, 220, 255, 255});
float rot = body.rotation * kPi / 180.0f;
for (int beam = 0; beam < 2; beam++) {
float angle = rot + beam * kPi;
float beamLen = (28.0f + 18.0f * pulse) * state.zoom;
DrawLineEx(
{sp.x + cosf(angle) * r, sp.y + sinf(angle) * r},
{sp.x + cosf(angle) * (r + beamLen), sp.y + sinf(angle) * (r + beamLen)},
3.0f * state.zoom,
Fade({200, 240, 255, 255}, 0.55f + 0.35f * pulse)
);
}
DrawCircleV(sp, max(2.0f, r * 0.5f), WHITE);
}
}
if (IsValidBodyIndex((int)bodies.size(), state.selectedBodyIndex)) {
const Body& selected = bodies[state.selectedBodyIndex];
Vector2 sp = W2S(selected.position);
float ring = max(12.0f, selected.visualRadius * state.zoom + 8.0f);
Color ringColor = state.followSelectedBody ? SKYBLUE : WHITE;
DrawCircleLinesV(sp, ring, Fade(ringColor, 0.92f));
DrawCircleLinesV(sp, ring + 4.0f, Fade(ringColor, 0.28f));
if (state.uiMode == UI_FULL || state.uiMode == UI_COMPACT) {
string label = string(state.followSelectedBody ? "tracking " : "selected ") + BodyTypeLabel(selected.type);
DrawText(label.c_str(), (int)(sp.x + ring + 8.0f), (int)(sp.y - 8.0f), 14, Fade(WHITE, 0.78f));
}
}
if (state.dragging) {
Vector2 ds = W2S(state.dragStart);
Vector2 de = W2S(state.dragEnd);
DrawCircleV(ds, RadiusFromMass(state.spawnMass) * state.zoom, Fade(SKYBLUE, 0.45f));
DrawLineV(ds, de, WHITE);
Vector2 velocityPreview = Mul(Sub(state.dragStart, state.dragEnd), 1.5f);
Vector2 arrowEnd = W2S(Add(state.dragStart, Mul(velocityPreview, 0.35f)));
DrawLineV(ds, arrowEnd, GREEN);
DrawCircleV(arrowEnd, 5.0f, GREEN);
}
if (state.rightDragging) {
Vector2 rs = W2S(state.rightDragStart);
Vector2 re = W2S(state.rightDragEnd);
DrawCircleV(rs, RadiusFromMass(700.0f, STAR) * state.zoom, Fade(PURPLE, 0.45f));
DrawLineV(rs, re, WHITE);
Vector2 velocityPreview = Mul(Sub(state.rightDragStart, state.rightDragEnd), 1.5f);
Vector2 arrowEnd = W2S(Add(state.rightDragStart, Mul(velocityPreview, 0.35f)));
DrawLineV(rs, arrowEnd, PURPLE);
DrawCircleV(arrowEnd, 5.0f, PURPLE);
}
if (ShouldDrawUiPanel(state.uiMode)) {
if (state.uiMode == UI_FULL) {
DrawFullHud(bodies, state);
} else {
DrawCompactHud(bodies, state);
}
if (state.showMinimap) DrawMinimap(bodies, state);
if (ShouldDrawHelpOverlay(state.uiMode, state.showHelp)) DrawHelpOverlay(state);
if (state.paused) DrawText("paused", state.screenWidth / 2 - 52, 25, 28, Fade(RED, 0.90f));
}
}