-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
1214 lines (987 loc) · 41.4 KB
/
Copy pathmain.c
File metadata and controls
1214 lines (987 loc) · 41.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
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
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <SDL.h>
#include <SDL_mixer.h>
#include <SDL_ttf.h>
// Return a random float in [0, 1].
static float rand01(void)
{
return (float)rand() / (float)RAND_MAX;
}
// ---------------------------------------------------------
// Constants
// ---------------------------------------------------------
#define FONT_SIZE 14//11//13//22//26
#define CHAR_SPACING 8//8//8//16//16
#define glyph_START_Y -25
#define DEFAULT_SIMULATION_FPS 30
#define ALPHABET_SIZE 62//36
#define MAX_TRAIL_LENGTH 256
// UI overlay
#define UI_COLOR_HIT_WIDTH 220
#define UI_PANEL_WIDTH 420
#define UI_PANEL_HEIGHT 360
#define UI_SLIDER_X 60
#define UI_SLIDER_Y 80
#define UI_SLIDER_W 260
#define UI_SLIDER_H 8
#define UI_COLOR_LABEL_X_OFFSET 60
#define UI_COLOR_LABEL_Y 160
#define UI_COLOR_ROW_SPACING 26
// Spiral-of-death protection
#define MAX_FRAME_TIME_MS 250.0f
#define MAX_ACCUMULATOR_MS 500.0f
// ---------------------------------------------------------
// Globals
// ---------------------------------------------------------
int* mn = NULL;
int RANGE = 0;
int* isActive = NULL;
int* headGlyphIndex = NULL;
int* freeIndexList = NULL;
int freeIndexCount = 0;
int simulationFPS = DEFAULT_SIMULATION_FPS;
float simulationStepMs = 0.0f;
float* speed = NULL;
float* VerticalAccumulator = NULL;
float* ColumnTravel = NULL;
// ---------------------------------------------------------
// Dynamic per-column speed modulation
// ---------------------------------------------------------
float* SpeedFactor = NULL; // Current speed multiplier (smoothed toward target)
float* SpeedTarget = NULL; // Target multiplier we ease toward over time
float* SpeedPhase = NULL; // Phase for subtle oscillation
float* SpeedPhaseStep = NULL; // Phase step per simulation tick
float* SpeedRetargetTimer = NULL; // Countdown (scaled by speed) until picking a new SpeedTarget
// Tunables for dynamic speed behavior
#define SPEED_FACTOR_MIN 0.35f
#define SPEED_FACTOR_MAX 2.80f
#define SPEED_EASE_UP 0.035f // gentle acceleration
#define SPEED_EASE_DOWN 0.450f // very strong braking
#define SPEED_DRAMATIC_BRAKE_THRESHOLD 2.20f // when above this, enforce hard braking events
#define SPEED_DRAMATIC_BRAKE_CHANCE 80 // % chance to force a strong slow target when very fast
#define SPEED_BRAKE_BAND_MIN 0.35f // forced braking target range
#define SPEED_BRAKE_BAND_MAX 0.85f
#define SPEED_FAST_TARGET_CAP 1.55f // cap fast targets when already very fast
#define SPEED_EARLY_BRAKE_POKE_CHANCE 18 // % per tick to force braking even before a retarget
#define SPEED_EARLY_BRAKE_MIN_COOLDOWN 8.0f // frames until next retarget after a poke
#define SPEED_EARLY_BRAKE_MAX_COOLDOWN 18.0f
#define SPEED_RETARGET_BURN_BOOST 1.90f // overall retarget frequency multiplier
#define SPEED_RETARGET_MIN_FRAMES 12
#define SPEED_RETARGET_MAX_FRAMES 40
#define SPEED_WOBBLE_AMPLITUDE 0.22f
#define SPEED_DRIFT_AMPLITUDE 0.22f // continuous speed drift (normal)
#define SPEED_DRIFT_AMPLITUDE_FAST 0.34f // continuous speed drift (fast columns)
#define SPEED_GRAVITY 0.0f // key
float WaveHue = 0.0f;
float FadeDistance = 750.0f; //1500.0f
int headColorMode = 0;
// 0 = green
// 1 = red
// 2 = blue
// 3 = white
// 4 = wave (global hue)
// 5 = rainbow (PER-GLYPH hue stored at spawn)
int emptyTextureWidth = 0;
int emptyTextureHeight = 0;
Mix_Music* music = NULL;
TTF_Font* font1 = NULL;
// ---------------------------------------------------------
// Glyph trail data structures
// ---------------------------------------------------------
typedef struct {
int glyphIndex;
float fadeTimer; // spawn travel position
SDL_Rect rect;
bool isHead;
// Per-glyph hue for RAINBOW mode.
// Only meaningful when headColorMode==5 at spawn time.
float spawnHue;
} StaticGlyph;
StaticGlyph** fadingTrails = NULL;
int* trailCounts = NULL;
// ---------------------------------------------------------
// Glyph textures
// ---------------------------------------------------------
typedef struct {
SDL_Texture* head;
} glyphTextures;
glyphTextures gTextures[ALPHABET_SIZE] = { 0 };
SDL_Texture* emptyTexture = NULL;
// ---------------------------------------------------------
// SDL app state
// ---------------------------------------------------------
typedef struct {
SDL_Renderer* renderer;
SDL_Window* window;
int running;
int dy;
} SDL2APP;
SDL2APP app = { .renderer = NULL, .window = NULL, .running = 1, .dy = 20 };
SDL_Rect** glyph = NULL;
SDL_DisplayMode DM = { .w = 0, .h = 0 };
/*
const char* alphabet[ALPHABET_SIZE] = {
"0","1","2","3","4","5","6","7","8","9",
"a","b","c","d","e","f","g","h","i","j",
"k","l","m","n","o","p","q","r","s","t",
"u","v","w","x","y","z"
};
*/
// Array of string literals representing the glyphs used
const char* alphabet[ALPHABET_SIZE] = {
"0","1","2","3","4","5","6","7","8","9", // Digits 0-9
"A","B","C","D","E","F","G","H","I","J", // Uppercase A-J
"K","L","M","N","O","P","Q","R","S","T", // Uppercase K-T
"U","V","W","X","Y","Z","a","b","c","d", // Uppercase U-Z and lowercase a-d
"e","f","g","h","i","j","k","l","m","n", // Lowercase e-n
"o","p","q","r","s","t","u","v","w","x", // Lowercase o-x
"y","z" // Lowercase y-z
};
// ---------------------------------------------------------
// UI overlay state
// ---------------------------------------------------------
typedef struct {
int visible;
} UIState;
UIState ui = { 0 };
static SDL_Rect ui_panel_rect = { 40, 40, UI_PANEL_WIDTH, UI_PANEL_HEIGHT };
// ---------------------------------------------------------
// Function declarations
// ---------------------------------------------------------
void render_glyph_trails(void);
void initialize(void);
void terminate(int exit_code);
void cleanupMemory(void);
void spawnStaticGlyph(int columnIndex, int glyphIndex, SDL_Rect rect, float initialFade, bool isHead);
int spawn(void);
int move(int i);
void render_ui_overlay(void);
// ---------------------------------------------------------
// Helpers
// ---------------------------------------------------------
static float frand01(void) {
return (float)rand() / (float)RAND_MAX;
}
static float frand_range(float a, float b) {
return a + (b - a) * frand01();
}
static int irand_range(int a, int b) {
// Inclusive range [a, b]
if (b <= a) return a;
return a + (rand() % (b - a + 1));
}
SDL_Texture* createTextTexture(const char* text, SDL_Color fg, SDL_Color bg) {
SDL_Surface* surface = TTF_RenderText_Shaded(font1, text, fg, bg);
if (!surface)
return NULL;
SDL_Texture* texture = SDL_CreateTextureFromSurface(app.renderer, surface);
SDL_FreeSurface(surface);
return texture;
}
static inline Uint8 clamp_u8_float(float v) {
if (v <= 0.0f) return 0;
if (v >= 255.0f) return 255;
return (Uint8)(v + 0.5f);
}
SDL_Rect render_multicolor_text(const char* text,
int x, int y,
const SDL_Color* colors,
int colorCount,
int doDraw)
{
SDL_Rect bounds = { x, y, 0, 0 };
if (!text || !colors || colorCount <= 0) {
return bounds;
}
int len = (int)strlen(text);
if (len > colorCount) len = colorCount;
int cx = x;
int maxH = 0;
SDL_Color bg = { 0, 0, 0, 255 };
for (int i = 0; i < len; ++i) {
char chStr[2];
chStr[0] = text[i];
chStr[1] = '\0';
SDL_Texture* tex = createTextTexture(chStr, colors[i], bg);
if (!tex) continue;
int tw, th;
SDL_QueryTexture(tex, NULL, NULL, &tw, &th);
if (doDraw) {
SDL_Rect dst = { cx, y, tw, th };
SDL_RenderCopy(app.renderer, tex, NULL, &dst);
}
SDL_DestroyTexture(tex);
if (tw > 0) {
cx += tw;
if (th > maxH) maxH = th;
}
}
bounds.w = cx - x;
bounds.h = maxH;
return bounds;
}
// ---------------------------------------------------------
// Cleanup
// ---------------------------------------------------------
void cleanupMemory() {
if (mn) { free(mn); mn = NULL; }
if (speed) { free(speed); speed = NULL; }
if (isActive) { free(isActive); isActive = NULL; }
if (headGlyphIndex) { free(headGlyphIndex); headGlyphIndex = NULL; }
if (VerticalAccumulator) { free(VerticalAccumulator); VerticalAccumulator = NULL; }
if (ColumnTravel) { free(ColumnTravel); ColumnTravel = NULL; }
if (SpeedFactor) { free(SpeedFactor); SpeedFactor = NULL; }
if (SpeedTarget) { free(SpeedTarget); SpeedTarget = NULL; }
if (SpeedPhase) { free(SpeedPhase); SpeedPhase = NULL; }
if (SpeedPhaseStep) { free(SpeedPhaseStep); SpeedPhaseStep = NULL; }
if (SpeedRetargetTimer) { free(SpeedRetargetTimer); SpeedRetargetTimer = NULL; }
if (glyph) {
for (int i = 0; i < RANGE; ++i) {
if (glyph[i]) { free(glyph[i]); glyph[i] = NULL; }
}
free(glyph);
glyph = NULL;
}
if (fadingTrails) {
for (int i = 0; i < RANGE; ++i) {
if (fadingTrails[i]) { free(fadingTrails[i]); fadingTrails[i] = NULL; }
}
free(fadingTrails);
fadingTrails = NULL;
}
if (trailCounts) { free(trailCounts); trailCounts = NULL; }
if (freeIndexList) { free(freeIndexList); freeIndexList = NULL; }
for (int i = 0; i < ALPHABET_SIZE; ++i) {
if (gTextures[i].head) {
SDL_DestroyTexture(gTextures[i].head);
gTextures[i].head = NULL;
}
}
if (emptyTexture) {
SDL_DestroyTexture(emptyTexture);
emptyTexture = NULL;
}
}
void terminate(int exit_code) {
cleanupMemory();
if (music) Mix_FreeMusic(music);
Mix_CloseAudio();
Mix_Quit();
if (font1) TTF_CloseFont(font1);
TTF_Quit();
if (app.renderer) SDL_DestroyRenderer(app.renderer);
if (app.window) SDL_DestroyWindow(app.window);
SDL_Quit();
exit(exit_code);
}
// ---------------------------------------------------------
// Color / hue helpers
// ---------------------------------------------------------
void hueToRGBf(float H, float* r, float* g, float* b) {
if (H >= 360.0f || H < 0.0f) {
H = fmodf(H, 360.0f);
if (H < 0.0f) H += 360.0f;
}
float S = 1.0f;
float V = 1.0f;
float C = V * S;
float Hprime = H / 60.0f;
float X = C * (1.0f - fabsf(fmodf(Hprime, 2.0f) - 1.0f));
float R1 = 0, G1 = 0, B1 = 0;
if (Hprime < 1) { R1 = C; G1 = X; B1 = 0; }
else if (Hprime < 2) { R1 = X; G1 = C; B1 = 0; }
else if (Hprime < 3) { R1 = 0; G1 = C; B1 = X; }
else if (Hprime < 4) { R1 = 0; G1 = X; B1 = C; }
else if (Hprime < 5) { R1 = X; G1 = 0; B1 = C; }
else { R1 = C; G1 = 0; B1 = X; }
float m = V - C;
*r = (R1 + m) * 255.0f;
*g = (G1 + m) * 255.0f;
*b = (B1 + m) * 255.0f;
if (*r < 0.0f) *r = 0.0f; else if (*r > 255.0f) *r = 255.0f;
if (*g < 0.0f) *g = 0.0f; else if (*g > 255.0f) *g = 255.0f;
if (*b < 0.0f) *b = 0.0f; else if (*b > 255.0f) *b = 255.0f;
}
void updateHue() {
if (headColorMode == 4) {
WaveHue += 0.1f;
if (WaveHue >= 360.0f) WaveHue -= 360.0f;
}
}
// ---------------------------------------------------------
// Rendering
// ---------------------------------------------------------
void render_glyph_trails(void) {
SDL_SetRenderDrawBlendMode(app.renderer, SDL_BLENDMODE_ADD);
updateHue();
for (int col = 0; col < RANGE; col++) {
int count = trailCounts[col];
if (count <= 0) continue;
int writeIndex = 0;
float colTravel = ColumnTravel[col];
// Defaults (GREEN)
float baseR = 0.0f, baseG = 128.0f, baseB = 0.0f; // CRT phosphor base (dim)
float headR = 80.0f, headG = 255.0f, headB = 110.0f; // CRT phosphor head (bright)
// For non-rainbow modes, we compute colors per-frame based on headColorMode.
// For rainbow mode, colors are computed PER GLYPH from its stored spawnHue.
if (headColorMode != 5) {
switch (headColorMode) {
case 1: // RED (Predator red)
// Deep, aggressive red with minimal blue to avoid magenta/pink.
baseR = 128.0f; baseG = 0.0f; baseB = 0.0f;
headR = 255.0f; headG = 90.0f; headB = 90.0f;
break;
case 2: // BLUE (digital)
baseR = 0.0f; baseG = 0.0f; baseB = 185.0f;
headR = 120.0f; headG = 160.0f; headB = 255.0f;
break;
case 3: // WHITE
baseR = 128.0f; baseG = 128.0f; baseB = 128.0f;
headR = 255.0f; headG = 255.0f; headB = 255.0f;
break;
default:
break;
}
}
const float brightThreshold = 0.9f;
for (int g = 0; g < count; g++) {
StaticGlyph* SGlyph = &fadingTrails[col][g];
float distanceSinceSpawn = colTravel - SGlyph->fadeTimer;
if (distanceSinceSpawn < 0.0f) distanceSinceSpawn = 0.0f;
float fadeFactor = 1.0f - (distanceSinceSpawn / FadeDistance);
if (fadeFactor <= 0.0f) {
continue;
}
if (fadeFactor > 1.0f) fadeFactor = 1.0f;
fadeFactor = fadeFactor * fadeFactor;
SDL_Texture* texture = gTextures[SGlyph->glyphIndex].head;
// If we are in rainbow mode, compute this glyph's base/head from its stored spawnHue.
float gBaseR = baseR, gBaseG = baseG, gBaseB = baseB;
float gHeadR = headR, gHeadG = headG, gHeadB = headB;
if (headColorMode == 5 || headColorMode == 4) {
// RAINBOW/WAVE: render from per-glyph stored hue
hueToRGBf(SGlyph->spawnHue, &gHeadR, &gHeadG, &gHeadB);
// Same “suite” as GREEN/RED/BLUE/WHITE: base is a dimmer version of head.
gBaseR = gHeadR * 0.50f;
gBaseG = gHeadG * 0.50f;
gBaseB = gHeadB * 0.50f;
}
if (SGlyph->isHead) {
SDL_SetTextureColorMod(texture,
clamp_u8_float(gHeadR),
clamp_u8_float(gHeadG),
clamp_u8_float(gHeadB));
SDL_SetTextureAlphaMod(texture, 255);
SDL_Rect bigRect = SGlyph->rect;
int dw = (int)(bigRect.w * 0.1f);
int dh = (int)(bigRect.h * 0.1f);
bigRect.x -= dw / 2; bigRect.y -= dh / 2;
bigRect.w += dw; bigRect.h += dh;
SDL_RenderCopy(app.renderer, texture, NULL, &bigRect);
float headBoost = (headColorMode == 0) ? 25.0f : (headColorMode == 1 ? 18.0f : (headColorMode == 2 ? 12.0f : 0.0f));
Uint8 brightAlpha = clamp_u8_float(fadeFactor * 255.0f + 100.0f + headBoost);
SDL_SetTextureAlphaMod(texture, brightAlpha);
SDL_RenderCopy(app.renderer, texture, NULL, &SGlyph->rect);
}
else {
float tBright = (fadeFactor - brightThreshold) / (1.0f - brightThreshold);
float tNormal = fadeFactor / brightThreshold;
if (tBright < 0.0f) tBright = 0.0f;
if (tBright > 1.0f) tBright = 1.0f;
if (tNormal < 0.0f) tNormal = 0.0f;
if (tNormal > 1.0f) tNormal = 1.0f;
Uint8 r, gCol, b, a = 255;
if (fadeFactor > brightThreshold) {
float rr = gBaseR + tBright * (gHeadR - gBaseR);
float gg = gBaseG + tBright * (gHeadG - gBaseG);
float bb = gBaseB + tBright * (gHeadB - gBaseB);
r = (Uint8)rr;
gCol = (Uint8)gg;
b = (Uint8)bb;
}
else {
float rr = tNormal * gBaseR;
float gg = tNormal * gBaseG;
float bb = tNormal * gBaseB;
r = (Uint8)rr;
gCol = (Uint8)gg;
b = (Uint8)bb;
}
float glowFactor = fadeFactor * fadeFactor;
// Slightly stronger “phosphor bloom” for GREEN/RED/BLUE modes.
float glowBoost = (headColorMode == 0) ? 1.35f : (headColorMode == 1 ? 1.25f : (headColorMode == 2 ? 1.15f : 1.0f));
float glowA = glowFactor * 50.0f * glowBoost;
if (glowA > 255.0f) glowA = 255.0f;
Uint8 glowAlpha = (Uint8)(glowA);
SDL_SetRenderDrawColor(app.renderer, r, gCol, b, glowAlpha);
SDL_RenderFillRect(app.renderer, &SGlyph->rect);
SDL_SetTextureColorMod(texture, r, gCol, b);
SDL_SetTextureAlphaMod(texture, a);
SDL_RenderCopy(app.renderer, texture, NULL, &SGlyph->rect);
}
SGlyph->isHead = false;
fadingTrails[col][writeIndex++] = *SGlyph;
}
trailCounts[col] = writeIndex;
}
SDL_SetRenderDrawBlendMode(app.renderer, SDL_BLENDMODE_BLEND);
}
// ---------------------------------------------------------
// Spawning / movement
// ---------------------------------------------------------
void spawnStaticGlyph(int columnIndex, int glyphIndex, SDL_Rect rect, float initialFade, bool isHead) {
if (trailCounts[columnIndex] >= MAX_TRAIL_LENGTH) return;
StaticGlyph* fglyph = &fadingTrails[columnIndex][trailCounts[columnIndex]++];
fglyph->glyphIndex = glyphIndex;
fglyph->fadeTimer = initialFade;
fglyph->rect = rect;
fglyph->isHead = isHead;
// Per-glyph hue capture:
if (headColorMode == 5) {
// RAINBOW: random hue per spawned glyph
fglyph->spawnHue = (float)(rand() % 360);
}
else if (headColorMode == 4) {
// WAVE: current wave hue per spawned glyph (keeps cycling pattern)
fglyph->spawnHue = WaveHue;
}
else {
fglyph->spawnHue = 0.0f;
}
}
int spawn(void) {
if (freeIndexCount <= 0) return -1;
int randomIndex = -1;
int maxTries = 10;
for (int tries = 0; tries < maxTries; ++tries) {
int candidate = rand() % RANGE;
if (!isActive[candidate]) {
randomIndex = candidate;
break;
}
}
if (randomIndex == -1) {
randomIndex = freeIndexList[--freeIndexCount];
}
headGlyphIndex[randomIndex] = rand() % ALPHABET_SIZE;
int spawnX = mn[randomIndex];
glyph[randomIndex][0].x = spawnX;
glyph[randomIndex][0].y = glyph_START_Y;
glyph[randomIndex][0].w = emptyTextureWidth;
glyph[randomIndex][0].h = emptyTextureHeight;
float possibleSpeeds[] = { 0.25f, 0.5f, 0.75f };
float chosenSpeed;
int attempts = 0;
do {
chosenSpeed = possibleSpeeds[rand() % 3];
attempts++;
if (attempts > 10) break;
} while (
(randomIndex > 0 && isActive[randomIndex - 1] && speed[randomIndex - 1] == chosenSpeed) ||
(randomIndex < RANGE - 1 && isActive[randomIndex + 1] && speed[randomIndex + 1] == chosenSpeed)
);
speed[randomIndex] = chosenSpeed;
// Give this column its own evolving speed profile (multiplier around the base speed).
SpeedFactor[randomIndex] = frand_range(0.85f, 1.15f);
SpeedTarget[randomIndex] = frand_range(SPEED_FACTOR_MIN, SPEED_FACTOR_MAX);
SpeedPhase[randomIndex] = frand_range(0.0f, 6.2831853f);
SpeedPhaseStep[randomIndex] = frand_range(0.05f, 0.12f);
SpeedRetargetTimer[randomIndex] = (float)irand_range(SPEED_RETARGET_MIN_FRAMES, SPEED_RETARGET_MAX_FRAMES);
// Fast base speed: shorten initial retarget so the column can brake before it exits.
if (speed[randomIndex] >= 2.0f) {
SpeedRetargetTimer[randomIndex] = (float)irand_range(6, 16);
}
isActive[randomIndex] = 1;
for (int i = 0; i < freeIndexCount; ++i) {
if (freeIndexList[i] == randomIndex) {
freeIndexList[i] = freeIndexList[--freeIndexCount];
break;
}
}
VerticalAccumulator[randomIndex] = 0.0f;
return randomIndex;
}
int move(int i) {
if (i < 0 || i >= RANGE) return i;
int cellH = emptyTextureHeight;
// Dynamic speed: each column eases toward a target multiplier and also gets a subtle wobble + gravity bias.
// Retarget countdown burns down faster for faster columns so quick columns still change speed before leaving the screen.
// Burn the retarget timer down faster for fast columns so they change speed before leaving the screen.
float burn = SpeedFactor[i] * SPEED_RETARGET_BURN_BOOST;
if (burn < 0.35f) burn = 0.35f;
if (burn > 6.0f) burn = 6.0f;
SpeedRetargetTimer[i] -= burn;
if (SpeedRetargetTimer[i] <= 0.0f) {
SpeedTarget[i] = frand_range(SPEED_FACTOR_MIN, SPEED_FACTOR_MAX);
// Make fast columns visibly dynamic: force strong braking targets when in rocket territory.
if (SpeedFactor[i] > SPEED_DRAMATIC_BRAKE_THRESHOLD) {
// High chance: force a slowdown target so the column visibly brakes before it exits.
if ((rand() % 100) < SPEED_DRAMATIC_BRAKE_CHANCE) {
float uBrake = frand01();
SpeedTarget[i] = SPEED_BRAKE_BAND_MIN + (SPEED_BRAKE_BAND_MAX - SPEED_BRAKE_BAND_MIN) * uBrake;
}
else {
// Otherwise cap the target to keep it from staying rocket-fast.
if (SpeedTarget[i] > SPEED_FAST_TARGET_CAP) SpeedTarget[i] = SPEED_FAST_TARGET_CAP;
}
}
else if (SpeedFactor[i] > 2.0f) {
// Moderately fast: still encourage occasional braking.
if ((rand() % 100) < 55) {
float uBrake = frand01();
SpeedTarget[i] = 0.45f + 0.55f * uBrake; // 0.45..1.00
}
}
SpeedPhaseStep[i] = frand_range(0.05f, 0.12f);
SpeedRetargetTimer[i] = (float)irand_range(SPEED_RETARGET_MIN_FRAMES, SPEED_RETARGET_MAX_FRAMES);
}
// EARLY-BRAKE POKE: very fast columns can exit before a retarget happens.
// If we're in rocket territory and not currently aiming slower, occasionally force a braking target NOW.
if (SpeedFactor[i] > SPEED_DRAMATIC_BRAKE_THRESHOLD && SpeedTarget[i] >= SpeedFactor[i]) {
if ((rand() % 100) < SPEED_EARLY_BRAKE_POKE_CHANCE) {
float uBrake = frand01();
SpeedTarget[i] = SPEED_BRAKE_BAND_MIN + (SPEED_BRAKE_BAND_MAX - SPEED_BRAKE_BAND_MIN) * uBrake;
// Ensure we get another retarget soon (keeps the 'alive' feel).
SpeedRetargetTimer[i] = (float)irand_range((int)SPEED_EARLY_BRAKE_MIN_COOLDOWN, (int)SPEED_EARLY_BRAKE_MAX_COOLDOWN);
}
}
// Smoothly ease current factor toward its target (stronger braking when slowing down).
float diff = SpeedTarget[i] - SpeedFactor[i];
float ease = (diff < 0.0f) ? SPEED_EASE_DOWN : SPEED_EASE_UP;
// Extra braking for very fast columns so they can visibly slow down on-screen.
if (diff < 0.0f && SpeedFactor[i] > 2.0f) {
ease *= (1.0f + 2.75f * (SpeedFactor[i] - 2.0f));
}
SpeedFactor[i] += diff * ease;
// SNAP-BRAKE: when extremely fast and the target is lower, force an additional immediate slowdown.
if (diff < 0.0f && SpeedFactor[i] > SPEED_DRAMATIC_BRAKE_THRESHOLD) {
float snap = 0.22f * (SpeedFactor[i] - SPEED_DRAMATIC_BRAKE_THRESHOLD);
if (snap > 0.35f) snap = 0.35f;
SpeedFactor[i] -= snap;
}
if (SpeedFactor[i] < SPEED_FACTOR_MIN) SpeedFactor[i] = SPEED_FACTOR_MIN;
if (SpeedFactor[i] > SPEED_FACTOR_MAX) SpeedFactor[i] = SPEED_FACTOR_MAX;
// Gentle oscillation to avoid all columns feeling mechanically uniform.
SpeedPhase[i] += SpeedPhaseStep[i];
if (SpeedPhase[i] > 6.2831853f) SpeedPhase[i] -= 6.2831853f;
float wobble = 1.0f + SPEED_WOBBLE_AMPLITUDE * sinf(SpeedPhase[i]);
// Continuous drift makes speed feel alive even between retargets (stronger on fast columns).
float driftAmp = (SpeedFactor[i] > 2.0f) ? SPEED_DRIFT_AMPLITUDE_FAST : SPEED_DRIFT_AMPLITUDE;
float drift = 1.0f + driftAmp * sinf(SpeedPhase[i] * 0.77f + 1.3f);
// Optional gravity-like bias: slightly faster as the head approaches the bottom of the screen.
float yNorm = 0.0f;
if (DM.h > 0) yNorm = (float)glyph[i][0].y / (float)DM.h;
if (yNorm < 0.0f) yNorm = 0.0f; else if (yNorm > 1.0f) yNorm = 1.0f;
float gravity = 1.0f + SPEED_GRAVITY * yNorm;
float movement = (float)app.dy * speed[i] * SpeedFactor[i] * wobble * drift * gravity; float prevTravel = ColumnTravel[i];
ColumnTravel[i] += movement;
if (!isActive[i]) return i;
VerticalAccumulator[i] += movement;
int startCount = trailCounts[i];
float spawnTravel = prevTravel;
while (VerticalAccumulator[i] >= cellH) {
VerticalAccumulator[i] -= cellH;
SDL_Rect stepRect = glyph[i][0];
stepRect.y += cellH;
int newGlyph = rand() % ALPHABET_SIZE;
if (headGlyphIndex[i] >= 0 && newGlyph == headGlyphIndex[i])
newGlyph = (newGlyph + 1) % ALPHABET_SIZE;
headGlyphIndex[i] = newGlyph;
spawnTravel += (float)cellH;
// Spawn as non-head; we mark newest as head after the loop.
spawnStaticGlyph(i, headGlyphIndex[i], stepRect, spawnTravel, false);
glyph[i][0].y += cellH;
if (glyph[i][0].y >= DM.h) {
isActive[i] = 0;
headGlyphIndex[i] = -1;
if (freeIndexCount < RANGE) {
freeIndexList[freeIndexCount++] = i;
}
VerticalAccumulator[i] = 0.0f;
SpeedFactor[i] = 1.0f;
SpeedTarget[i] = 1.0f;
SpeedPhase[i] = frand_range(0.0f, 6.2831853f);
SpeedPhaseStep[i] = frand_range(0.05f, 0.12f);
SpeedRetargetTimer[i] = (float)irand_range(SPEED_RETARGET_MIN_FRAMES, SPEED_RETARGET_MAX_FRAMES);
break;
}
}
if (trailCounts[i] > startCount) {
fadingTrails[i][trailCounts[i] - 1].isHead = true;
}
return i;
}
// ---------------------------------------------------------
// UI overlay rendering (hotkey-only; slider is visual only)
// ---------------------------------------------------------
void render_ui_overlay(void) {
if (!ui.visible) return;
SDL_SetRenderDrawBlendMode(app.renderer, SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawColor(app.renderer, 38, 38, 46, 220);
SDL_RenderFillRect(app.renderer, &ui_panel_rect);
SDL_SetRenderDrawColor(app.renderer, 90, 90, 100, 255);
SDL_RenderDrawRect(app.renderer, &ui_panel_rect);
SDL_Color fg = { 255, 255, 255, 255 };
SDL_Color bg = { 0, 0, 0, 255 };
SDL_Texture* txtTitle = createTextTexture("MATRIX CODE RAIN CONTROLS", fg, bg);
if (txtTitle) {
int tw, th;
SDL_QueryTexture(txtTitle, NULL, NULL, &tw, &th);
SDL_Rect dst = { ui_panel_rect.x + 16, ui_panel_rect.y + 12, tw, th };
SDL_RenderCopy(app.renderer, txtTitle, NULL, &dst);
SDL_DestroyTexture(txtTitle);
}
SDL_Texture* txtSpeed = createTextTexture("SIMULATION SPEED", fg, bg);
if (txtSpeed) {
int tw, th;
SDL_QueryTexture(txtSpeed, NULL, NULL, &tw, &th);
SDL_Rect dst = {
ui_panel_rect.x + UI_SLIDER_X,
ui_panel_rect.y + UI_SLIDER_Y - 26,
tw, th
};
SDL_RenderCopy(app.renderer, txtSpeed, NULL, &dst);
SDL_DestroyTexture(txtSpeed);
}
char buf[64];
snprintf(buf, sizeof(buf), "%d", simulationFPS);
SDL_Texture* txtValue = createTextTexture(buf, fg, bg);
if (txtValue) {
int tw, th;
SDL_QueryTexture(txtValue, NULL, NULL, &tw, &th);
SDL_Rect dst = {
ui_panel_rect.x + UI_SLIDER_X + UI_SLIDER_W + 12,
ui_panel_rect.y + UI_SLIDER_Y - th / 2,
tw, th
};
SDL_RenderCopy(app.renderer, txtValue, NULL, &dst);
SDL_DestroyTexture(txtValue);
}
int sliderX = ui_panel_rect.x + UI_SLIDER_X;
int sliderY = ui_panel_rect.y + UI_SLIDER_Y;
SDL_Rect track = { sliderX, sliderY, UI_SLIDER_W, UI_SLIDER_H };
SDL_SetRenderDrawColor(app.renderer, 70, 70, 80, 255);
SDL_RenderFillRect(app.renderer, &track);
const float minFPS = 15.0f;
const float maxFPS = 120.0f;
float t = (float)(simulationFPS - minFPS) / (maxFPS - minFPS);
if (t < 0.0f) t = 0.0f;
if (t > 1.0f) t = 1.0f;
int knobX = sliderX + (int)(t * (float)UI_SLIDER_W);
SDL_Rect knob = { knobX - 6, sliderY - 6, 12, 12 };
SDL_SetRenderDrawColor(app.renderer, 220, 220, 230, 255);
SDL_RenderFillRect(app.renderer, &knob);
SDL_Texture* txtColor = createTextTexture("COLOR MODE", fg, bg);
if (txtColor) {
int tw, th;
SDL_QueryTexture(txtColor, NULL, NULL, &tw, &th);
SDL_Rect dst = {
ui_panel_rect.x + UI_SLIDER_X,
ui_panel_rect.y + UI_COLOR_LABEL_Y - 26,
tw, th
};
SDL_RenderCopy(app.renderer, txtColor, NULL, &dst);
SDL_DestroyTexture(txtColor);
}
const char* modeLabels[6] = {
"PHOSPHOR GREEN",
"CINDER RED",
"ELECTRON BLUE",
"WHITE OUT",
"WAVE",
"RAINBOW"
};
SDL_Color modeColors[6] = {
{ 0, 255, 0, 255 },
{ 255, 0, 0, 255 },
{ 40, 80, 255, 255 },
{ 255, 255, 255, 255 },
{ 230, 230, 230, 255 },
{ 255, 255, 255, 255 }
};
int labelBaseX = ui_panel_rect.x + UI_COLOR_LABEL_X_OFFSET;
int labelBaseY = ui_panel_rect.y + UI_COLOR_LABEL_Y;
for (int c = 0; c < 6; ++c) {
int rowY = labelBaseY + c * UI_COLOR_ROW_SPACING;
if (c <= 3) {
SDL_Texture* tLabel = createTextTexture(modeLabels[c], modeColors[c], bg);
if (!tLabel) continue;
int tw, th;
SDL_QueryTexture(tLabel, NULL, NULL, &tw, &th);
SDL_Rect textRect = { labelBaseX, rowY, tw, th };
SDL_Rect hitRect = { labelBaseX - 8, rowY - 2, UI_COLOR_HIT_WIDTH, th + 4 };
if (c == headColorMode) {
SDL_SetRenderDrawColor(app.renderer, 70, 70, 80, 180);
SDL_RenderFillRect(app.renderer, &hitRect);
SDL_SetRenderDrawColor(app.renderer, 200, 200, 210, 255);
SDL_RenderDrawRect(app.renderer, &hitRect);
}
SDL_RenderCopy(app.renderer, tLabel, NULL, &textRect);
SDL_DestroyTexture(tLabel);
}
else if (c == 4) {
SDL_Color waveColors[4] = {
{ 255, 0, 0, 255 },
{ 255, 128, 0, 255 },
{ 255, 255, 0, 255 },
{ 0, 255, 0, 255 }
};
SDL_Rect textRect = render_multicolor_text("WAVE", labelBaseX, rowY, waveColors, 4, 0);
SDL_Rect hitRect = { labelBaseX - 8, rowY - 2, UI_COLOR_HIT_WIDTH, textRect.h + 4 };
if (c == headColorMode) {
SDL_SetRenderDrawColor(app.renderer, 70, 70, 80, 180);
SDL_RenderFillRect(app.renderer, &hitRect);
SDL_SetRenderDrawColor(app.renderer, 200, 200, 210, 255);
SDL_RenderDrawRect(app.renderer, &hitRect);
}
render_multicolor_text("WAVE", labelBaseX, rowY, waveColors, 4, 1);
}
else if (c == 5) {
SDL_Color rainbowColors[7] = {
{ 255, 0, 0, 255 },
{ 255, 128, 0, 255 },
{ 255, 255, 0, 255 },
{ 0, 255, 0, 255 },
{ 0, 0, 255, 255 },
{ 75, 0, 130, 255 },
{ 148, 0, 211, 255 }
};
SDL_Rect textRect = render_multicolor_text("RAINBOW", labelBaseX, rowY, rainbowColors, 7, 0);
SDL_Rect hitRect = { labelBaseX - 8, rowY - 2, UI_COLOR_HIT_WIDTH, textRect.h + 4 };
if (c == headColorMode) {
SDL_SetRenderDrawColor(app.renderer, 70, 70, 80, 180);
SDL_RenderFillRect(app.renderer, &hitRect);
SDL_SetRenderDrawColor(app.renderer, 200, 200, 210, 255);
SDL_RenderDrawRect(app.renderer, &hitRect);
}
render_multicolor_text("RAINBOW", labelBaseX, rowY, rainbowColors, 7, 1);
}
}
SDL_Texture* txtHint = createTextTexture("PRESS F1 TO TOGGLE UI", fg, bg);
if (txtHint) {
int tw, th;
SDL_QueryTexture(txtHint, NULL, NULL, &tw, &th);
SDL_Rect dst = {
ui_panel_rect.x + 16,
ui_panel_rect.y + ui_panel_rect.h - th - 10,
tw, th
};
SDL_RenderCopy(app.renderer, txtHint, NULL, &dst);
SDL_DestroyTexture(txtHint);
}
SDL_SetRenderDrawBlendMode(app.renderer, SDL_BLENDMODE_BLEND);
}
// ---------------------------------------------------------
// Initialization
// ---------------------------------------------------------
void initialize() {
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) terminate(1);
if (TTF_Init() < 0) terminate(1);
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1");
if (Mix_OpenAudio(48000, MIX_DEFAULT_FORMAT, 2, 4096) < 0) {
SDL_Log("Mix_OpenAudio failed: %s", Mix_GetError());
}
SDL_GetCurrentDisplayMode(0, &DM);
RANGE = (DM.w + CHAR_SPACING - 1) / CHAR_SPACING;
mn = (int*)malloc(RANGE * sizeof(int));
if (!mn) { SDL_Log("Out of memory: mn"); terminate(1); }
speed = (float*)malloc(RANGE * sizeof(float));
if (!speed) { SDL_Log("Out of memory: speed"); terminate(1); }
// Dynamic speed modulation arrays (per column)
SpeedFactor = (float*)malloc(RANGE * sizeof(float));
if (!SpeedFactor) { SDL_Log("Out of memory: SpeedFactor"); terminate(1); }
SpeedTarget = (float*)malloc(RANGE * sizeof(float));
if (!SpeedTarget) { SDL_Log("Out of memory: SpeedTarget"); terminate(1); }
SpeedPhase = (float*)malloc(RANGE * sizeof(float));
if (!SpeedPhase) { SDL_Log("Out of memory: SpeedPhase"); terminate(1); }
SpeedPhaseStep = (float*)malloc(RANGE * sizeof(float));
if (!SpeedPhaseStep) { SDL_Log("Out of memory: SpeedPhaseStep"); terminate(1); }
SpeedRetargetTimer = (float*)malloc(RANGE * sizeof(float));
if (!SpeedRetargetTimer) { SDL_Log("Out of memory: SpeedRetargetTimer"); terminate(1); }
isActive = (int*)malloc(RANGE * sizeof(int));
if (!isActive) { SDL_Log("Out of memory: isActive"); terminate(1); }
freeIndexList = (int*)malloc(RANGE * sizeof(int));
if (!freeIndexList) { SDL_Log("Out of memory: freeIndexList"); terminate(1); }
trailCounts = (int*)calloc((size_t)RANGE, sizeof(int));
if (!trailCounts) { SDL_Log("Out of memory: trailCounts"); terminate(1); }
fadingTrails = (StaticGlyph**)malloc(RANGE * sizeof(StaticGlyph*));
if (!fadingTrails) { SDL_Log("Out of memory: fadingTrails"); terminate(1); }