-
-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathsamples_c.c
More file actions
1745 lines (1685 loc) · 62.5 KB
/
samples_c.c
File metadata and controls
1745 lines (1685 loc) · 62.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
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
/*
* libtcod C samples
* This code demonstrates various usages of libtcod modules
* It's in the public domain.
*/
// uncomment this to disable SDL sample (might cause compilation issues on some systems)
// #define NO_SDL_SAMPLE
#define SDL_MAIN_USE_CALLBACKS
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
#include <libtcod.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* a sample has a name and a rendering function */
typedef struct sample_t {
const char* name;
void (*render)(const SDL_Event* event);
} sample_t;
/* sample screen size */
#define SAMPLE_SCREEN_WIDTH 46
#define SAMPLE_SCREEN_HEIGHT 20
/* sample screen position */
#define SAMPLE_SCREEN_X 20
#define SAMPLE_SCREEN_Y 10
// A custom SDL event for when a sample is switched to.
#define ON_ENTER_USEREVENT (SDL_EVENT_USER + 0)
static const SDL_Event ON_ENTER_EVENT = {.type = ON_ENTER_USEREVENT};
// A custom SDL event to tell a sample to draw.
#define ON_DRAW_USEREVENT (SDL_EVENT_USER + 1)
static const SDL_Event ON_DRAW_EVENT = {.type = ON_DRAW_USEREVENT};
static const TCOD_ColorRGB BLACK = {0, 0, 0};
static const TCOD_ColorRGB WHITE = {255, 255, 255};
static const TCOD_ColorRGB GREY = {127, 127, 127};
static const TCOD_ColorRGB RED = {255, 0, 0};
static const TCOD_ColorRGB GREEN = {0, 255, 0};
static const TCOD_ColorRGB BLUE = {0, 0, 255};
static const TCOD_ColorRGB LIGHT_BLUE = {63, 63, 255};
static const TCOD_ColorRGB LIGHT_YELLOW = {255, 255, 63};
static float delta_time = 0.0f; // The time in seconds of the current frame.
#define DELTA_SAMPLES_LENGTH 64
static float delta_samples[DELTA_SAMPLES_LENGTH] = {0};
static int last_delta_sample = 0;
// Report an error message and abort the program.
void fatal(const char* format, ...) {
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
exit(EXIT_FAILURE);
}
// Return true if string ends with suffix.
bool str_ends_with(const char* string, const char* suffix) {
if (!string || !suffix) return false;
size_t str_len = strlen(string);
size_t suffix_len = strlen(suffix);
if (suffix_len > str_len) return false;
return strcmp(string + str_len - suffix_len, suffix) == 0;
}
static TCOD_Context* g_context;
static TCOD_Tileset* tileset;
static TCOD_Console* sample_console; // the offscreen console in which the samples are rendered.
static TCOD_Console* main_console;
static TCOD_ContextParams params;
static int current_sample = 0; // index of the current sample
static uint64_t last_time = 0;
/* ***************************
* true colors sample
* ***************************/
// Return noise sample as a 0 to 255 value.
static uint8_t noise_sample_u8(TCOD_Noise* noise, const float* f) {
return (uint8_t)((TCOD_noise_get(noise, f) + 1.0f) * 0.5f * 255.5f);
}
void render_colors(const SDL_Event* event) {
enum { TOPLEFT, TOPRIGHT, BOTTOMLEFT, BOTTOMRIGHT };
static TCOD_Noise* noise = NULL; // Noise for random colors.
if (!noise) {
noise = TCOD_noise_new(2, 0, 0, NULL);
TCOD_noise_set_type(noise, TCOD_NOISE_SIMPLEX);
}
if (event->type == ON_ENTER_USEREVENT) {
TCOD_console_clear(sample_console);
}
const float t = SDL_GetTicks() * 0.001f;
// Generate color corners from noise samples.
const TCOD_color_t colors[4] = {
{noise_sample_u8(noise, (float[]){t, 0}),
noise_sample_u8(noise, (float[]){t, 1}),
noise_sample_u8(noise, (float[]){t, 2})},
{noise_sample_u8(noise, (float[]){t, 10}),
noise_sample_u8(noise, (float[]){t, 11}),
noise_sample_u8(noise, (float[]){t, 12})},
{noise_sample_u8(noise, (float[]){t, 20}),
noise_sample_u8(noise, (float[]){t, 21}),
noise_sample_u8(noise, (float[]){t, 22})},
{noise_sample_u8(noise, (float[]){t, 30}),
noise_sample_u8(noise, (float[]){t, 31}),
noise_sample_u8(noise, (float[]){t, 32})}};
/* ==== scan the whole screen, interpolating corner colors ==== */
for (int y = 0; y < SAMPLE_SCREEN_HEIGHT; ++y) {
const float y_coef = (float)(y) / (SAMPLE_SCREEN_HEIGHT - 1);
/* get the current column top and bottom colors */
TCOD_color_t left = TCOD_color_lerp(colors[TOPLEFT], colors[BOTTOMLEFT], y_coef);
TCOD_color_t right = TCOD_color_lerp(colors[TOPRIGHT], colors[BOTTOMRIGHT], y_coef);
for (int x = 0; x < SAMPLE_SCREEN_WIDTH; ++x) {
const float x_coef = (float)(x) / (SAMPLE_SCREEN_WIDTH - 1);
/* get the current cell color */
TCOD_ColorRGB current_color = TCOD_color_lerp(left, right, x_coef);
TCOD_console_put_rgb(sample_console, x, y, 0, NULL, ¤t_color, TCOD_BKGND_SET);
}
}
/* ==== print the text ==== */
/* put random text (for performance tests) */
for (int y = 0; y < SAMPLE_SCREEN_HEIGHT; ++y) {
for (int x = 0; x < SAMPLE_SCREEN_WIDTH; ++x) {
TCOD_ColorRGB color = TCOD_console_get_char_background(sample_console, x, y);
color = TCOD_color_lerp(color, BLACK, 0.5f);
const int c = TCOD_random_get_int(NULL, 'a', 'z');
TCOD_console_put_rgb(sample_console, x, y, c, &color, NULL, TCOD_BKGND_SET);
}
}
/* get the background color at the text position */
TCOD_color_t textColor = TCOD_console_get_char_background(sample_console, SAMPLE_SCREEN_WIDTH / 2, 5);
/* and invert it */
textColor.r = 255 - textColor.r;
textColor.g = 255 - textColor.g;
textColor.b = 255 - textColor.b;
/* the background behind the text is slightly darkened using the BKGND_MULTIPLY flag */
TCOD_printf_rgb(
sample_console,
(TCOD_PrintParamsRGB){
.x = 1,
.y = 5,
.width = SAMPLE_SCREEN_WIDTH - 2,
.fg = &textColor,
.bg = &GREY,
.flag = TCOD_BKGND_MULTIPLY,
.alignment = TCOD_CENTER,
},
"The Doryen library uses 24 bits colors, for both background and foreground.");
}
/* ***************************
* offscreen console sample
* ***************************/
void render_offscreen(const SDL_Event* event) {
static TCOD_console_t secondary; /* second screen */
static TCOD_console_t screenshot; /* second screen */
static bool init = false; /* draw the secondary screen only the first time */
static uint64_t counter;
static int x = 0, y = 0; /* secondary screen position */
static int x_dir = 1, y_dir = 1; /* movement direction */
if (!init) {
init = true;
secondary = TCOD_console_new(SAMPLE_SCREEN_WIDTH / 2, SAMPLE_SCREEN_HEIGHT / 2);
screenshot = TCOD_console_new(SAMPLE_SCREEN_WIDTH, SAMPLE_SCREEN_HEIGHT);
TCOD_console_draw_frame_rgb(secondary, 0, 0, secondary->w, secondary->h, NULL, NULL, NULL, 0, 0);
TCOD_printf_rgb(
secondary,
(TCOD_PrintParamsRGB){
.x = 0, .y = 0, .width = secondary->w, .fg = &BLACK, .bg = &WHITE, .alignment = TCOD_CENTER},
" Offscreen console ");
TCOD_printf_rgb(
secondary,
(TCOD_PrintParamsRGB){.x = 1, .y = 2, .width = secondary->w - 2, .alignment = TCOD_CENTER},
"You can render to an offscreen console and blit in on another one, simulating alpha transparency.");
}
if (event->type == ON_ENTER_USEREVENT) {
counter = SDL_GetTicks();
/* get a "screenshot" of the current sample screen */
TCOD_console_blit(sample_console, 0, 0, SAMPLE_SCREEN_WIDTH, SAMPLE_SCREEN_HEIGHT, screenshot, 0, 0, 1.0f, 1.0f);
}
if (SDL_GetTicks() >= counter + 1000) { // Once every second.
counter = SDL_GetTicks();
x += x_dir;
y += y_dir;
if (x == SAMPLE_SCREEN_WIDTH / 2 + 5)
x_dir = -1;
else if (x == -5)
x_dir = 1;
if (y == SAMPLE_SCREEN_HEIGHT / 2 + 5)
y_dir = -1;
else if (y == -5)
y_dir = 1;
}
/* restore the initial screen */
TCOD_console_blit(screenshot, 0, 0, SAMPLE_SCREEN_WIDTH, SAMPLE_SCREEN_HEIGHT, sample_console, 0, 0, 1.0f, 1.0f);
/* blit the overlapping screen */
TCOD_console_blit(
secondary, 0, 0, SAMPLE_SCREEN_WIDTH / 2, SAMPLE_SCREEN_HEIGHT / 2, sample_console, x, y, 1.0f, 0.75f);
}
/* ***************************
* line drawing sample
* ***************************/
static int bk_flag = TCOD_BKGND_SET; /* current blending mode */
bool line_listener(int x, int y) {
if (x >= 0 && y >= 0 && x < SAMPLE_SCREEN_WIDTH && y < SAMPLE_SCREEN_HEIGHT) {
TCOD_console_put_rgb(sample_console, x, y, 0, NULL, &LIGHT_BLUE, (TCOD_bkgnd_flag_t)bk_flag);
}
return true;
}
void render_lines(const SDL_Event* event) {
static TCOD_console_t bk; /* colored background */
static bool init = false;
static const char* flag_names[] = {
"TCOD_BKGND_NONE",
"TCOD_BKGND_SET",
"TCOD_BKGND_MULTIPLY",
"TCOD_BKGND_LIGHTEN",
"TCOD_BKGND_DARKEN",
"TCOD_BKGND_SCREEN",
"TCOD_BKGND_COLOR_DODGE",
"TCOD_BKGND_COLOR_BURN",
"TCOD_BKGND_ADD",
"TCOD_BKGND_ADDALPHA",
"TCOD_BKGND_BURN",
"TCOD_BKGND_OVERLAY",
"TCOD_BKGND_ALPHA"};
switch (event->type) {
case SDL_EVENT_KEY_DOWN:
switch (event->key.scancode) {
case SDL_SCANCODE_RETURN:
case SDL_SCANCODE_RETURN2:
case SDL_SCANCODE_KP_ENTER:
if (event->key.mod & SDL_KMOD_ALT) break;
/* switch to the next blending mode */
++bk_flag;
if ((bk_flag & 0xff) > TCOD_BKGND_ALPH) bk_flag = TCOD_BKGND_NONE;
break;
default:
break;
}
break;
}
float alpha; /* alpha value when blending mode = TCOD_BKGND_ALPHA */
const float current_time = SDL_GetTicks() / 1000.0f;
if ((bk_flag & 0xff) == TCOD_BKGND_ALPH) {
/* for the alpha mode, update alpha every frame */
alpha = (1.0f + cosf(current_time * 2)) / 2.0f;
bk_flag = TCOD_BKGND_ALPHA(alpha);
} else if ((bk_flag & 0xff) == TCOD_BKGND_ADDA) {
/* for the add alpha mode, update alpha every frame */
alpha = (1.0f + cosf(current_time * 2)) / 2.0f;
bk_flag = TCOD_BKGND_ADDALPHA(alpha);
}
if (!init) {
bk = TCOD_console_new(SAMPLE_SCREEN_WIDTH, SAMPLE_SCREEN_HEIGHT);
/* initialize the colored background */
for (int y = 0; y < SAMPLE_SCREEN_HEIGHT; ++y) {
for (int x = 0; x < SAMPLE_SCREEN_WIDTH; ++x) {
TCOD_ColorRGB color = {
.r = (uint8_t)(x * 255 / (SAMPLE_SCREEN_WIDTH - 1)),
.g = (uint8_t)((x + y) * 255 / (SAMPLE_SCREEN_WIDTH - 1 + SAMPLE_SCREEN_HEIGHT - 1)),
.b = (uint8_t)(y * 255 / (SAMPLE_SCREEN_HEIGHT - 1)),
};
TCOD_console_put_rgb(bk, x, y, 0, NULL, &color, TCOD_BKGND_SET);
}
}
init = true;
}
/* blit the background */
TCOD_console_blit(bk, 0, 0, SAMPLE_SCREEN_WIDTH, SAMPLE_SCREEN_HEIGHT, sample_console, 0, 0, 1.0f, 1.0f);
/* render the gradient */
/* gradient vertical position */
const int rect_y = (int)((SAMPLE_SCREEN_HEIGHT - 2) * ((1.0f + cosf(current_time)) / 2.0f));
for (int x = 0; x < SAMPLE_SCREEN_WIDTH; ++x) {
const TCOD_ColorRGB color = {
(uint8_t)(x * 255 / SAMPLE_SCREEN_WIDTH),
(uint8_t)(x * 255 / SAMPLE_SCREEN_WIDTH),
(uint8_t)(x * 255 / SAMPLE_SCREEN_WIDTH),
};
TCOD_console_draw_rect_rgb(sample_console, x, rect_y, 1, 3, 0, NULL, &color, (TCOD_bkgnd_flag_t)bk_flag);
}
/* calculate the segment ends */
const float angle = current_time * 2.0f; /* segment angle data */
/* segment starting and ending positions */
const int xo = (int)(SAMPLE_SCREEN_WIDTH / 2 * (1 + cosf(angle)));
const int yo = (int)(SAMPLE_SCREEN_HEIGHT / 2 + sinf(angle) * SAMPLE_SCREEN_WIDTH / 2);
const int xd = (int)(SAMPLE_SCREEN_WIDTH / 2 * (1 - cosf(angle)));
const int yd = (int)(SAMPLE_SCREEN_HEIGHT / 2 - sinf(angle) * SAMPLE_SCREEN_WIDTH / 2);
/* render the line */
TCOD_line(xo, yo, xd, yd, line_listener);
/* print the current flag */
TCOD_printf_rgb(
sample_console,
(TCOD_PrintParamsRGB){.x = 2, .y = 2, .fg = &(TCOD_ColorRGB){255, 255, 255}},
"%s (ENTER to change)",
flag_names[bk_flag & 0xff]);
}
/* ***************************
* noise sample
* ***************************/
void render_noise(const SDL_Event* event) {
enum {
PERLIN,
SIMPLEX,
WAVELET,
FBM_PERLIN,
TURBULENCE_PERLIN,
FBM_SIMPLEX,
TURBULENCE_SIMPLEX,
FBM_WAVELET,
TURBULENCE_WAVELET
}; /* which function we render */
static const char* funcName[] = {
"1 : perlin noise ",
"2 : simplex noise ",
"3 : wavelet noise ",
"4 : perlin fbm ",
"5 : perlin turbulence ",
"6 : simplex fbm ",
"7 : simplex turbulence ",
"8 : wavelet fbm ",
"9 : wavelet turbulence ",
};
static int func = PERLIN;
static TCOD_noise_t noise = NULL;
static float octaves = 4.0f;
static float hurst = TCOD_NOISE_DEFAULT_HURST;
static float lacunarity = TCOD_NOISE_DEFAULT_LACUNARITY;
static TCOD_image_t img = NULL;
static float zoom = 3.0f;
if (!noise) {
noise = TCOD_noise_new(2, hurst, lacunarity, NULL);
img = TCOD_image_new(SAMPLE_SCREEN_WIDTH * 2, SAMPLE_SCREEN_HEIGHT * 2);
}
TCOD_console_clear(sample_console);
/* texture animation */
const float dx = SDL_GetTicks() * 0.0005f;
const float dy = dx;
/* render the 2d noise function */
for (int y = 0; y < 2 * SAMPLE_SCREEN_HEIGHT; ++y) {
for (int x = 0; x < 2 * SAMPLE_SCREEN_WIDTH; ++x) {
const float sample_xy[2] = {
zoom * x / (2 * SAMPLE_SCREEN_WIDTH) + dx,
zoom * y / (2 * SAMPLE_SCREEN_HEIGHT) + dy,
};
float value = 0.0f;
switch (func) {
case PERLIN:
value = TCOD_noise_get_ex(noise, sample_xy, TCOD_NOISE_PERLIN);
break;
case SIMPLEX:
value = TCOD_noise_get_ex(noise, sample_xy, TCOD_NOISE_SIMPLEX);
break;
case WAVELET:
value = TCOD_noise_get_ex(noise, sample_xy, TCOD_NOISE_WAVELET);
break;
case FBM_PERLIN:
value = TCOD_noise_get_fbm_ex(noise, sample_xy, octaves, TCOD_NOISE_PERLIN);
break;
case TURBULENCE_PERLIN:
value = TCOD_noise_get_turbulence_ex(noise, sample_xy, octaves, TCOD_NOISE_PERLIN);
break;
case FBM_SIMPLEX:
value = TCOD_noise_get_fbm_ex(noise, sample_xy, octaves, TCOD_NOISE_SIMPLEX);
break;
case TURBULENCE_SIMPLEX:
value = TCOD_noise_get_turbulence_ex(noise, sample_xy, octaves, TCOD_NOISE_SIMPLEX);
break;
case FBM_WAVELET:
value = TCOD_noise_get_fbm_ex(noise, sample_xy, octaves, TCOD_NOISE_WAVELET);
break;
case TURBULENCE_WAVELET:
value = TCOD_noise_get_turbulence_ex(noise, sample_xy, octaves, TCOD_NOISE_WAVELET);
break;
}
const uint8_t c = (uint8_t)((value + 1.0f) / 2.0f * 255);
/* use a bluish color */
TCOD_ColorRGB col = {c / 2, c / 2, c};
TCOD_image_put_pixel(img, x, y, col);
}
}
/* blit the noise image with subcell resolution */
TCOD_image_blit_2x(img, sample_console, 0, 0, 0, 0, -1, -1);
/* draw a transparent rectangle */
TCOD_console_draw_rect_rgb(
sample_console, 2, 2, 23, (func <= WAVELET ? 10 : 13), 0, NULL, &GREY, TCOD_BKGND_MULTIPLY);
for (int y = 2; y < 2 + (func <= WAVELET ? 10 : 13); ++y) {
for (int x = 2; x < 2 + 23; ++x) {
TCOD_ConsoleTile* tile_ref = &sample_console->tiles[y * sample_console->w + x];
TCOD_ColorRGB* fg_ref = (TCOD_ColorRGB*)(&tile_ref->fg);
*fg_ref = TCOD_color_multiply(*fg_ref, GREY);
}
}
/* draw the text */
for (int curfunc = PERLIN; curfunc <= TURBULENCE_WAVELET; ++curfunc) {
const bool is_highlighted = curfunc == func;
TCOD_printf_rgb(
sample_console,
(TCOD_PrintParamsRGB){
.x = 2,
.y = 2 + curfunc,
.fg = is_highlighted ? &WHITE : &GREY,
.bg = is_highlighted ? &LIGHT_BLUE : NULL,
},
"%s",
funcName[curfunc]);
}
/* draw parameters */
TCOD_printf_rgb(sample_console, (TCOD_PrintParamsRGB){.x = 2, .y = 11, .fg = &WHITE}, "Y/H : zoom (%2.1f)", zoom);
if (func > WAVELET) {
TCOD_printf_rgb(
sample_console,
(TCOD_PrintParamsRGB){.x = 2, .y = 12, .fg = &WHITE},
"E/D : hurst (%2.1f)\nR/F : lacunarity (%2.1f)\nT/G : octaves (%2.1f)",
hurst,
lacunarity,
octaves);
}
/* handle keypress */
switch (event->type) {
case SDL_EVENT_KEY_DOWN:
switch (event->key.scancode) {
case SDL_SCANCODE_E:
/* increase hurst */
hurst += 0.1f;
TCOD_noise_delete(noise);
noise = TCOD_noise_new(2, hurst, lacunarity, NULL);
break;
case SDL_SCANCODE_D:
/* decrease hurst */
hurst -= 0.1f;
TCOD_noise_delete(noise);
noise = TCOD_noise_new(2, hurst, lacunarity, NULL);
break;
case SDL_SCANCODE_R:
/* increase lacunarity */
lacunarity += 0.5f;
TCOD_noise_delete(noise);
noise = TCOD_noise_new(2, hurst, lacunarity, NULL);
break;
case SDL_SCANCODE_F:
/* decrease lacunarity */
lacunarity -= 0.5f;
TCOD_noise_delete(noise);
noise = TCOD_noise_new(2, hurst, lacunarity, NULL);
break;
case SDL_SCANCODE_T:
/* increase octaves */
octaves += 0.5f;
break;
case SDL_SCANCODE_G:
/* decrease octaves */
octaves -= 0.5f;
break;
case SDL_SCANCODE_Y:
/* increase zoom */
zoom += 0.2f;
break;
case SDL_SCANCODE_H:
/* decrease zoom */
zoom -= 0.2f;
break;
default: {
/* change the noise function */
const int scancode = event->key.scancode;
if (scancode >= SDL_SCANCODE_1 && scancode <= SDL_SCANCODE_9) {
func = scancode - SDL_SCANCODE_1;
}
if (scancode >= SDL_SCANCODE_KP_1 && scancode <= SDL_SCANCODE_KP_9) {
func = scancode - SDL_SCANCODE_KP_1;
}
} break;
}
break;
}
}
/* ***************************
* fov sample
* ***************************/
// clang-format off
static const char* SAMPLE_MAP[] = {
"##############################################",
"####################### #################",
"##################### # ###############",
"###################### ### ###########",
"################## ##### ####",
"################ ######## ###### ####",
"############### #################### ####",
"################ ###### ##",
"######## ####### ###### # # # ##",
"######## ###### ### ##",
"######## ##",
"#### ###### ### # # # ##",
"#### ### ########## #### ##",
"#### ### ########## ###########=##########",
"#### ################## ##### #####",
"#### ### #### ##### #####",
"#### # #### #####",
"######## # #### ##### #####",
"######## ##### ####################",
"##############################################",
};
// clang-format on
static const int CHAR_WINDOW = 0x2550; // "═" glyph.
#define TORCH_RADIUS 10.0f
#define SQUARED_TORCH_RADIUS (TORCH_RADIUS * TORCH_RADIUS)
void render_fov(const SDL_Event* event) {
static int px = 20, py = 10; /* player position */
static bool torch = false;
static bool light_walls = true;
static TCOD_map_t map = NULL;
static const TCOD_color_t dark_wall = {0, 0, 100};
static const TCOD_color_t dark_ground = {50, 50, 150};
static const TCOD_color_t light_ground = {200, 180, 50};
static const TCOD_color_t light_wall = {130, 110, 50};
static TCOD_noise_t noise;
static int algonum = 0;
static const char* algo_names[] = {
"BASIC ",
"DIAMOND ",
"SHADOW ",
"PERMISSIVE0 ",
"PERMISSIVE1 ",
"PERMISSIVE2 ",
"PERMISSIVE3 ",
"PERMISSIVE4 ",
"PERMISSIVE5 ",
"PERMISSIVE6 ",
"PERMISSIVE7 ",
"PERMISSIVE8 ",
"RESTRICTIVE ",
"SYMMETRIC_SHADOWCAST",
};
static float torch_x = 0.0f; /* torch light position in the perlin noise */
/* torch position & intensity variation */
float dx = 0.0f, dy = 0.0f, di = 0.0f;
if (!map) {
map = TCOD_map_new(SAMPLE_SCREEN_WIDTH, SAMPLE_SCREEN_HEIGHT);
for (int y = 0; y < SAMPLE_SCREEN_HEIGHT; ++y) {
for (int x = 0; x < SAMPLE_SCREEN_WIDTH; ++x) {
if (SAMPLE_MAP[y][x] == ' ')
TCOD_map_set_properties(map, x, y, true, true); /* ground */
else if (SAMPLE_MAP[y][x] == '=')
TCOD_map_set_properties(map, x, y, true, false); /* window */
}
}
noise = TCOD_noise_new(1, 1.0f, 1.0f, NULL); /* 1d noise for the torch flickering */
}
if (event->type == ON_DRAW_USEREVENT) {
/* we draw the foreground only the first time.
during the player movement, only the @ is redrawn.
the rest impacts only the background color */
/* draw the help text & player @ */
TCOD_console_clear(sample_console);
TCOD_printf_rgb(
sample_console,
(TCOD_PrintParamsRGB){.x = 1, .y = 0, .fg = &WHITE},
"IJKL : move around\nT : torch fx %s\nW : light walls %s\n+-: algo %s",
torch ? "on " : "off",
light_walls ? "on " : "off",
algo_names[algonum]);
TCOD_console_put_rgb(sample_console, px, py, '@', &BLACK, NULL, 0);
/* draw windows */
for (int y = 0; y < SAMPLE_SCREEN_HEIGHT; ++y) {
for (int x = 0; x < SAMPLE_SCREEN_WIDTH; ++x) {
if (SAMPLE_MAP[y][x] == '=') {
TCOD_console_put_rgb(sample_console, x, y, CHAR_WINDOW, &BLACK, NULL, 0); // ═
}
}
}
TCOD_map_compute_fov(map, px, py, torch ? (int)(TORCH_RADIUS) : 0, light_walls, (TCOD_fov_algorithm_t)algonum);
if (torch) {
/* slightly change the perlin noise parameter */
torch_x += 0.2f;
/* randomize the light position between -1.5 and 1.5 */
float tdx = torch_x + 20.0f;
dx = TCOD_noise_get(noise, &tdx) * 1.5f;
tdx += 30.0f;
dy = TCOD_noise_get(noise, &tdx) * 1.5f;
di = 0.2f * TCOD_noise_get(noise, &torch_x);
}
for (int y = 0; y < SAMPLE_SCREEN_HEIGHT; ++y) {
for (int x = 0; x < SAMPLE_SCREEN_WIDTH; ++x) {
const bool visible = TCOD_map_is_in_fov(map, x, y);
const bool wall = SAMPLE_MAP[y][x] == '#';
if (!visible) {
TCOD_console_put_rgb(sample_console, x, y, 0, NULL, wall ? &dark_wall : &dark_ground, TCOD_BKGND_SET);
} else {
if (!torch) {
TCOD_console_put_rgb(sample_console, x, y, 0, NULL, wall ? &light_wall : &light_ground, TCOD_BKGND_SET);
} else {
TCOD_ColorRGB base = (wall ? dark_wall : dark_ground);
const TCOD_ColorRGB light = (wall ? light_wall : light_ground);
/* cell distance to torch (squared) */
const float radius_squared = (x - px + dx) * (x - px + dx) + (y - py + dy) * (y - py + dy);
if (radius_squared < SQUARED_TORCH_RADIUS) {
const float l = (SQUARED_TORCH_RADIUS - radius_squared) / SQUARED_TORCH_RADIUS + di;
base = TCOD_color_lerp(base, light, TCOD_CLAMP(0.0f, 1.0f, l));
}
TCOD_console_put_rgb(sample_console, x, y, 0, NULL, &base, TCOD_BKGND_SET);
}
}
}
}
}
switch (event->type) {
case SDL_EVENT_KEY_DOWN:
switch (event->key.scancode) {
case SDL_SCANCODE_I:
if (SAMPLE_MAP[py - 1][px] == ' ') {
--py;
}
break;
case SDL_SCANCODE_K:
if (SAMPLE_MAP[py + 1][px] == ' ') {
++py;
}
break;
case SDL_SCANCODE_J:
if (SAMPLE_MAP[py][px - 1] == ' ') {
--px;
}
break;
case SDL_SCANCODE_L:
if (SAMPLE_MAP[py][px + 1] == ' ') {
++px;
}
break;
case SDL_SCANCODE_T:
torch = !torch;
break;
case SDL_SCANCODE_W:
light_walls = !light_walls;
break;
case SDL_SCANCODE_EQUALS:
case SDL_SCANCODE_KP_PLUS:
case SDL_SCANCODE_MINUS:
case SDL_SCANCODE_KP_MINUS:
if (event->key.scancode == SDL_SCANCODE_EQUALS || event->key.scancode == SDL_SCANCODE_KP_PLUS) {
++algonum;
} else {
--algonum;
}
algonum = (algonum + NB_FOV_ALGORITHMS) % NB_FOV_ALGORITHMS;
break;
default:
break;
}
break;
}
}
/* ***************************
* image sample
* ***************************/
void render_image(const SDL_Event* event) {
static TCOD_image_t img = NULL;
static TCOD_image_t circle = NULL;
if (img == NULL) {
img = TCOD_image_load("data/img/skull.png");
TCOD_image_set_key_color(img, BLACK);
circle = TCOD_image_load("data/img/circle.png");
}
if (event->type != ON_DRAW_USEREVENT) return;
TCOD_console_clear(sample_console);
const float current_time = SDL_GetTicks() / 1000.0f;
const float x = SAMPLE_SCREEN_WIDTH / 2 + cosf(current_time) * 10.0f;
const float y = (float)(SAMPLE_SCREEN_HEIGHT / 2);
const float scale_x = 0.2f + 1.8f * (1.0f + cosf(current_time / 2)) / 2.0f;
const float scale_y = scale_x;
const float angle = current_time;
if ((SDL_GetTicks() / 2000) & 1) {
/* split the color channels of circle.png */
/* the red channel */
TCOD_console_draw_rect_rgb(sample_console, 0, 3, 15, 15, 0, NULL, &RED, TCOD_BKGND_SET);
TCOD_image_blit_rect(circle, sample_console, 0, 3, -1, -1, TCOD_BKGND_MULTIPLY);
/* the green channel */
TCOD_console_draw_rect_rgb(sample_console, 15, 3, 15, 15, 0, NULL, &GREEN, TCOD_BKGND_SET);
TCOD_image_blit_rect(circle, sample_console, 15, 3, -1, -1, TCOD_BKGND_MULTIPLY);
/* the blue channel */
TCOD_console_draw_rect_rgb(sample_console, 30, 3, 15, 15, 0, NULL, &BLUE, TCOD_BKGND_SET);
TCOD_image_blit_rect(circle, sample_console, 30, 3, -1, -1, TCOD_BKGND_MULTIPLY);
} else {
/* render circle.png with normal blitting */
TCOD_image_blit_rect(circle, sample_console, 0, 3, -1, -1, TCOD_BKGND_SET);
TCOD_image_blit_rect(circle, sample_console, 15, 3, -1, -1, TCOD_BKGND_SET);
TCOD_image_blit_rect(circle, sample_console, 30, 3, -1, -1, TCOD_BKGND_SET);
}
TCOD_image_blit(img, sample_console, x, y, TCOD_BKGND_SET, scale_x, scale_y, angle);
}
/* ***************************
* mouse sample
* ***************************/
void render_mouse(const SDL_Event* event) {
static int tile_motion_x = 0;
static int tile_motion_y = 0;
float pixel_x;
float pixel_y;
uint32_t mouse_state = SDL_GetMouseState(&pixel_x, &pixel_y);
int tile_x = (int)pixel_x;
int tile_y = (int)pixel_y;
TCOD_context_screen_pixel_to_tile_i(g_context, &tile_x, &tile_y);
switch (event->type) {
case ON_ENTER_USEREVENT:
SDL_WarpMouseInWindow(NULL, 320, 200);
SDL_ShowCursor();
break;
case SDL_EVENT_KEY_DOWN:
switch (event->key.scancode) {
case SDL_SCANCODE_1:
case SDL_SCANCODE_KP_1:
SDL_HideCursor();
break;
case SDL_SCANCODE_2:
case SDL_SCANCODE_KP_2:
SDL_ShowCursor();
break;
default:
break;
}
break;
case SDL_EVENT_MOUSE_MOTION:
tile_motion_x = (int)event->motion.xrel;
tile_motion_y = (int)event->motion.yrel;
break;
}
TCOD_console_draw_rect_rgb(
sample_console, 0, 0, sample_console->w, sample_console->h, 0x20, &LIGHT_YELLOW, &GREY, TCOD_BKGND_SET);
TCOD_printf_rgb(
sample_console,
(TCOD_PrintParamsRGB){.x = 1, .y = 1},
"Pixel position : %4dx%4d\n"
"Tile position : %4dx%4d\n"
"Tile movement : %4dx%4d\n"
"Left button : %s\n"
"Right button : %s\n"
"Middle button : %s\n",
(int)pixel_x,
(int)pixel_y,
tile_x,
tile_y,
tile_motion_x,
tile_motion_y,
mouse_state & SDL_BUTTON_LMASK ? " ON" : "OFF",
mouse_state & SDL_BUTTON_RMASK ? " ON" : "OFF",
mouse_state & SDL_BUTTON_MMASK ? " ON" : "OFF");
TCOD_printf_rgb(sample_console, (TCOD_PrintParamsRGB){.x = 1, .y = 10}, "1 : Hide cursor\n2 : Show cursor");
}
/* ***************************
* path sample
* ***************************/
void render_path(const SDL_Event* event) {
static int px = 20, py = 10; // player position
static int dx = 24, dy = 1; // destination
static TCOD_map_t map = NULL;
static TCOD_color_t dark_wall = {0, 0, 100};
static TCOD_color_t dark_ground = {50, 50, 150};
static TCOD_color_t light_ground = {200, 180, 50};
static TCOD_path_t path = NULL;
static bool usingAstar = true;
static float dijkstraDist = 0;
static TCOD_dijkstra_t dijkstra = NULL;
static float busy = 0.0f;
if (!map) {
/* initialize the map */
map = TCOD_map_new(SAMPLE_SCREEN_WIDTH, SAMPLE_SCREEN_HEIGHT);
for (int y = 0; y < SAMPLE_SCREEN_HEIGHT; ++y) {
for (int x = 0; x < SAMPLE_SCREEN_WIDTH; ++x) {
if (SAMPLE_MAP[y][x] == ' ')
TCOD_map_set_properties(map, x, y, true, true); /* ground */
else if (SAMPLE_MAP[y][x] == '=')
TCOD_map_set_properties(map, x, y, true, false); /* window */
}
}
path = TCOD_path_new_using_map(map, 1.41f);
dijkstra = TCOD_dijkstra_new(map, 1.41f);
}
if (event->type == ON_DRAW_USEREVENT) {
/* we draw the foreground only the first time.
during the player movement, only the @ is redrawn.
the rest impacts only the background color */
/* draw the help text & player @ */
TCOD_console_clear(sample_console);
TCOD_console_put_rgb(sample_console, dx, dy, '+', NULL, NULL, TCOD_BKGND_NONE);
TCOD_console_put_rgb(sample_console, px, py, '@', NULL, NULL, TCOD_BKGND_NONE);
TCOD_printf_rgb(
sample_console, (TCOD_PrintParamsRGB){.x = 1, .y = 1}, "IJKL / mouse :\nmove destination\nTAB : A*/dijkstra");
TCOD_printf_rgb(
sample_console, (TCOD_PrintParamsRGB){.x = 1, .y = 4}, "Using : %s", usingAstar ? "A*" : "Dijkstra");
/* draw windows */
for (int y = 0; y < SAMPLE_SCREEN_HEIGHT; ++y) {
for (int x = 0; x < SAMPLE_SCREEN_WIDTH; ++x) {
if (SAMPLE_MAP[y][x] == '=') {
TCOD_console_put_rgb(sample_console, x, y, CHAR_WINDOW, NULL, NULL, 0); // ═
}
}
}
if (usingAstar) {
TCOD_path_compute(path, px, py, dx, dy);
} else {
dijkstraDist = 0.0f;
/* compute the distance grid */
TCOD_dijkstra_compute(dijkstra, px, py);
/* get the maximum distance (needed for ground shading only) */
for (int y = 0; y < SAMPLE_SCREEN_HEIGHT; ++y) {
for (int x = 0; x < SAMPLE_SCREEN_WIDTH; ++x) {
float d = TCOD_dijkstra_get_distance(dijkstra, x, y);
if (d > dijkstraDist) dijkstraDist = d;
}
}
// compute the path
TCOD_dijkstra_path_set(dijkstra, dx, dy);
}
// draw the dungeon
for (int y = 0; y < SAMPLE_SCREEN_HEIGHT; ++y) {
for (int x = 0; x < SAMPLE_SCREEN_WIDTH; ++x) {
const bool wall = SAMPLE_MAP[y][x] == '#';
TCOD_console_put_rgb(sample_console, x, y, 0, NULL, wall ? &dark_wall : &dark_ground, TCOD_BKGND_SET);
}
}
// draw the path
if (usingAstar) {
for (int i = 0; i < TCOD_path_size(path); ++i) {
int x, y;
TCOD_path_get(path, i, &x, &y);
TCOD_console_put_rgb(sample_console, x, y, 0, NULL, &light_ground, TCOD_BKGND_SET);
}
} else {
for (int y = 0; y < SAMPLE_SCREEN_HEIGHT; ++y) {
for (int x = 0; x < SAMPLE_SCREEN_WIDTH; ++x) {
const bool wall = SAMPLE_MAP[y][x] == '#';
if (!wall) {
const float d = TCOD_dijkstra_get_distance(dijkstra, x, y);
const TCOD_ColorRGB bg = TCOD_color_lerp(light_ground, dark_ground, 0.9f * d / dijkstraDist);
TCOD_console_put_rgb(sample_console, x, y, 0, NULL, &bg, TCOD_BKGND_SET);
}
}
}
for (int i = 0; i < TCOD_dijkstra_size(dijkstra); ++i) {
int x, y;
TCOD_dijkstra_get(dijkstra, i, &x, &y);
TCOD_console_put_rgb(sample_console, x, y, 0, NULL, &light_ground, TCOD_BKGND_SET);
}
}
// move the creature
busy -= delta_time;
if (busy <= 0.0f) {
busy = 0.2f;
if (usingAstar) {
if (!TCOD_path_is_empty(path)) {
TCOD_path_walk(path, &px, &py, true);
}
} else {
if (!TCOD_dijkstra_is_empty(dijkstra)) {
TCOD_dijkstra_path_walk(dijkstra, &px, &py);
}
}
}
}
switch (event->type) {
case SDL_EVENT_KEY_DOWN:
switch (event->key.scancode) {
case SDL_SCANCODE_TAB:
case SDL_SCANCODE_KP_TAB:
usingAstar = !usingAstar;
break;
case SDL_SCANCODE_I: // destination move north
dy = TCOD_MAX(0, dy - 1);
break;
case SDL_SCANCODE_K: // destination move south
dy = TCOD_MIN(dy + 1, SAMPLE_SCREEN_HEIGHT - 1);
break;
case SDL_SCANCODE_J: // destination move west
dx = TCOD_MAX(0, dx - 1);
break;
case SDL_SCANCODE_L: // destination move east
dx = TCOD_MIN(dx + 1, SAMPLE_SCREEN_WIDTH - 1);
break;
default:
break;
}
break;
case SDL_EVENT_MOUSE_MOTION: {
const int mx = (int)event->motion.x - SAMPLE_SCREEN_X;
const int my = (int)event->motion.y - SAMPLE_SCREEN_Y;
if (mx >= 0 && mx < SAMPLE_SCREEN_WIDTH && my >= 0 && my < SAMPLE_SCREEN_HEIGHT && (dx != mx || dy != my)) {
dx = mx;
dy = my;
}
} break;
}
}
// ***************************
// bsp sample
// ***************************
static int bspDepth = 8;
static int minRoomSize = 4;
static bool randomRoom = false; // a room fills a random part of the node or the maximum available space ?
static bool roomWalls = true; // if true, there is always a wall on north & west side of a room
typedef char map_t[SAMPLE_SCREEN_WIDTH][SAMPLE_SCREEN_HEIGHT];
// draw a vertical line
void vline(map_t* map, int x, int y1, int y2) {
int y = y1;
const int dy = (y1 > y2 ? -1 : 1);
(*map)[x][y] = ' ';
if (y1 == y2) return;
do {
y += dy;
(*map)[x][y] = ' ';
} while (y != y2);
}
// draw a vertical line up until we reach an empty space
void vline_up(map_t* map, int x, int y) {
while (y >= 0 && (*map)[x][y] != ' ') {
(*map)[x][y] = ' ';
--y;
}
}
// draw a vertical line down until we reach an empty space
void vline_down(map_t* map, int x, int y) {
while (y < SAMPLE_SCREEN_HEIGHT && (*map)[x][y] != ' ') {
(*map)[x][y] = ' ';
++y;
}
}
// draw a horizontal line
void hline(map_t* map, int x1, int y, int x2) {
int x = x1;
const int dx = (x1 > x2 ? -1 : 1);
(*map)[x][y] = ' ';
if (x1 == x2) return;
do {
x += dx;
(*map)[x][y] = ' ';
} while (x != x2);
}
// draw a horizontal line left until we reach an empty space
void hline_left(map_t* map, int x, int y) {
while (x >= 0 && (*map)[x][y] != ' ') {
(*map)[x][y] = ' ';
--x;
}
}
// draw a horizontal line right until we reach an empty space
void hline_right(map_t* map, int x, int y) {
while (x < SAMPLE_SCREEN_WIDTH && (*map)[x][y] != ' ') {
(*map)[x][y] = ' ';
++x;
}
}
// the class building the dungeon from the bsp nodes
bool traverse_node(TCOD_bsp_t* node, void* userData) {
map_t* map = (map_t*)userData;
if (TCOD_bsp_is_leaf(node)) {
// calculate the room size
int minx = node->x + 1;
int maxx = node->x + node->w - 1;
int miny = node->y + 1;
int maxy = node->y + node->h - 1;
if (!roomWalls) {
if (minx > 1) --minx;
if (miny > 1) --miny;
}
if (maxx == SAMPLE_SCREEN_WIDTH - 1) --maxx;
if (maxy == SAMPLE_SCREEN_HEIGHT - 1) --maxy;
if (randomRoom) {
minx = TCOD_random_get_int(NULL, minx, maxx - minRoomSize + 1);
miny = TCOD_random_get_int(NULL, miny, maxy - minRoomSize + 1);
maxx = TCOD_random_get_int(NULL, minx + minRoomSize - 1, maxx);
maxy = TCOD_random_get_int(NULL, miny + minRoomSize - 1, maxy);
}
// resize the node to fit the room
// printf("node %dx%d %dx%d => room %dx%d
//%dx%d\n",node->x,node->y,node->w,node->h,minx,miny,maxx-minx+1,maxy-miny+1);
node->x = minx;
node->y = miny;