-
Notifications
You must be signed in to change notification settings - Fork 1
/
SpriteHandler.h
1200 lines (1033 loc) · 36.5 KB
/
SpriteHandler.h
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
//
// SpriteHandler.h
// Termin8or
//
// Created by Rasmus Anthin on 2024-10-14.
//
#pragma once
#include "Texture.h"
#include "ScreenHandler.h"
#include "Drawing.h"
#include "AABB.h"
#include <Core/Vec2.h>
#include <map>
#include <memory>
class Sprite
{
protected:
std::string name;
public:
RC pos { 0, 0 };
int layer_id = 0; // 0 is the bottom layer.
bool enabled = true;
std::function<int(int)> func_calc_anim_frame = [](int sim_frame) -> int { return 0; };
virtual ~Sprite() = default;
Sprite(const std::string& a_name) : name(a_name) {}
const std::string& get_name() const { return name; }
virtual void clone_frame(int anim_frame, int from_anim_frame) = 0;
virtual AABB<int> calc_curr_AABB(int /*sim_frame*/) const = 0;
virtual Vec2 calc_curr_centroid(int /*sim_frame*/) const = 0;
virtual int num_frames() const = 0;
virtual bool_vector calc_curr_mask(int sim_frame, const std::vector<int>& mask_materials) = 0;
virtual bool calc_cm() const = 0;
virtual bool is_opaque(int sim_frame, const RC& pt) const = 0;
virtual std::vector<RC> get_opaque_points(int sim_frame) const = 0;
};
// /////////////////////////////////////////////////
class BitmapSprite : public Sprite
{
// Helper function for setting any vector data
template<typename T, typename... Args>
bool set_sprite_data(std::vector<T>& target, Args... args)
{
if (sizeof...(args) != static_cast<size_t>(area))
{
std::cerr << "ERROR in BitmapSprite::set_sprite_data() : Number of arguments must match sprite size." << std::endl;
return false;
}
target = {static_cast<T>(args)...}; // Unpack and assign to the target vector
return true;
}
template<typename T, typename... Args>
bool set_sprite_data(std::vector<T>& target, const ttl::Rectangle& bb, Args... args)
{
int nr = std::min(bb.r_len, size.r);
int nc = std::min(bb.c_len, size.c);
if (sizeof...(args) != static_cast<size_t>(nr * nc))
{
std::cerr << "ERROR in BitmapSprite::set_sprite_data() : Number of arguments must match sprite size and or bounding box size." << std::endl;
return false;
}
std::vector<T> source = {static_cast<T>(args)...}; // Unpack and assign to the target vector
auto N_trg = stlutils::sizeI(target);
auto N_src = stlutils::sizeI(source);
for (int i = 0; i < nr; ++i)
{
for (int j = 0; j < nc; ++j)
{
// Map bounding box (i, j) to the target sprite data
int trg_row = bb.r + i;
int trg_col = bb.c + j;
// Calculate linear index into the target vector and src_data
int trg_index = trg_row * size.c + trg_col;
int src_index = i * nc + j;
// Assign data to the target vector
if (trg_index < N_trg && src_index < N_src)
target[trg_index] = source[src_index];
}
}
return true;
}
template<typename T>
void fill_sprite_data(std::vector<T>& target, const ttl::Rectangle& bb, T arg)
{
int nr = std::min(bb.r_len, size.r);
int nc = std::min(bb.c_len, size.c);
auto N_trg = stlutils::sizeI(target);
for (int i = 0; i < nr; ++i)
{
for (int j = 0; j < nc; ++j)
{
// Map bounding box (i, j) to the target sprite data
int trg_row = bb.r + i;
int trg_col = bb.c + j;
// Calculate linear index into the target vector and src_data
int trg_index = trg_row * size.c + trg_col;
// Assign data to the target vector
if (trg_index < N_trg)
target[trg_index] = arg;
}
}
}
RC size { 0, 0 };
int area = 0;
std::vector<std::unique_ptr<drawing::Texture>> texture_frames;
drawing::Texture* fetch_frame(int anim_frame)
{
while (stlutils::sizeI(texture_frames) <= anim_frame)
texture_frames.emplace_back(std::make_unique<drawing::Texture>());
return texture_frames[anim_frame].get();
}
public:
BitmapSprite(const std::string& a_name) : Sprite(a_name) {}
// Initialize the sprite's dimensions (NR and NC)
void init(int NR, int NC)
{
size = { NR, NC };
area = NR * NC;
}
RC get_size() const
{
return size;
}
void create_frame(int anim_frame)
{
auto* texture = fetch_frame(anim_frame);
texture->clear();
texture->area = area;
texture->size = size;
texture->characters.resize(area);
texture->fg_colors.resize(area);
texture->bg_colors.resize(area);
texture->materials.resize(area);
}
bool load_frame(int anim_frame, const std::string& file_path)
{
auto* texture = fetch_frame(anim_frame);
texture->clear();
texture->load(file_path);
if (texture->size != size)
{
std::cerr << "ERROR in BitmapSprite::load_frame() : Loaded sprite frame doesn't have the same size as the sprite itself." << std::endl;
return false;
}
return true;
}
void save_frame(int anim_frame, const std::string& file_path)
{
auto* texture = fetch_frame(anim_frame);
texture->save(file_path);
}
virtual void clone_frame(int anim_frame, int from_anim_frame) override
{
const auto N = stlutils::sizeI(texture_frames);
if (from_anim_frame < N)
{
if (anim_frame >= N)
{
auto* texture_from = fetch_frame(from_anim_frame);
fetch_frame(anim_frame);
texture_frames[anim_frame] = std::make_unique<drawing::Texture>(*texture_from);
}
else
std::cout << "ERROR in clone_frame() : anim_frame must be larger than or equal to the number of texture frames!" << std::endl;
}
else
std::cout << "ERROR in clone_frame() : from_anim_frame cannot be larger than or equal to the number of texture frames!" << std::endl;
}
drawing::Texture* try_get_frame(int anim_frame)
{
if (anim_frame < stlutils::sizeI(texture_frames))
return texture_frames[anim_frame].get();
return nullptr;
}
void set_frame(int anim_frame, const drawing::Texture& texture)
{
auto* texture_dst = fetch_frame(anim_frame);
*texture_dst = texture;
}
// #FIXME: Perhaps move these varyadic functions to Texture for more versatility.
template<typename... Chars>
void set_sprite_chars(int anim_frame, Chars... ch)
{
auto* texture = fetch_frame(anim_frame);
set_sprite_data(texture->characters, ch...);
}
template<typename... Chars>
void set_sprite_chars(int anim_frame, const ttl::Rectangle& bb, Chars... ch)
{
auto* texture = fetch_frame(anim_frame);
set_sprite_data(texture->characters, bb, ch...);
}
template<typename... Chars>
void set_sprite_chars_vert(int anim_frame, int r0, int r1, int c, Chars... ch)
{
ttl::Rectangle bb { r0, c, r1 - r0 + 1, 1 };
set_sprite_chars(anim_frame, bb, ch...);
}
template<typename... Chars>
void set_sprite_chars_horiz(int anim_frame, int r, int c0, int c1, Chars... ch)
{
ttl::Rectangle bb { r, c0, 1, c1 - c0 + 1 };
set_sprite_chars(anim_frame, bb, ch...);
}
void set_sprite_char(int anim_frame, int r, int c, char ch)
{
auto* texture = fetch_frame(anim_frame);
if (texture != nullptr)
texture->set_textel_char(r, c, ch);
}
void fill_sprite_chars(int anim_frame, char ch)
{
auto* texture = fetch_frame(anim_frame);
texture->characters.assign(area, ch);
}
void fill_sprite_chars(int anim_frame, const ttl::Rectangle& bb, char ch)
{
auto* texture = fetch_frame(anim_frame);
fill_sprite_data(texture->characters, bb, ch);
}
void fill_sprite_chars_vert(int anim_frame, int r0, int r1, int c, char ch)
{
auto* texture = fetch_frame(anim_frame);
if (texture != nullptr)
for (int r = r0; r <= r1; ++r)
texture->set_textel_char(r, c, ch);
}
void fill_sprite_chars_horiz(int anim_frame, int r, int c0, int c1, char ch)
{
auto* texture = fetch_frame(anim_frame);
if (texture != nullptr)
for (int c = c0; c <= c1; ++c)
texture->set_textel_char(r, c, ch);
}
// Set sprite characters from a string for each row.
template<typename... Strings>
bool set_sprite_chars_from_strings(int anim_frame, Strings... rows)
{
auto* texture = fetch_frame(anim_frame);
std::array<std::string, sizeof...(rows)> row_array = { rows... };
// Check that the number of rows matches the texture's height
if (stlutils::sizeI(row_array) != texture->size.r)
{
std::cerr << "ERROR in BitmapSprite::set_sprite_chars_from_strings() : Number of strings must match the number of rows." << std::endl;
return false;
}
for (const auto& row : row_array)
if (stlutils::sizeI(row) != texture->size.c)
{
std::cerr << "ERROR in BitmapSprite::set_sprite_chars_from_strings() : Each string must have exactly NC characters." << std::endl;
return false;
}
// Unpack strings into the characters vector.
int idx = 0;
for (const auto& row : row_array)
for (char ch : row)
texture->characters[idx++] = ch;
return true;
}
template<typename... Colors>
bool set_sprite_fg_colors(int anim_frame, Colors... fg_color)
{
auto* texture = fetch_frame(anim_frame);
return set_sprite_data(texture->fg_colors, fg_color...);
}
template<typename... Colors>
bool set_sprite_fg_colors(int anim_frame, const ttl::Rectangle& bb, Colors... fg_color)
{
auto* texture = fetch_frame(anim_frame);
return set_sprite_data(texture->fg_colors, bb, fg_color...);
}
template<typename... Colors>
void set_sprite_fg_colors_vert(int anim_frame, int r0, int r1, int c, Colors... fg_color)
{
ttl::Rectangle bb { r0, c, r1 - r0 + 1, 1 };
set_sprite_fg_colors(anim_frame, bb, fg_color...);
}
template<typename... Colors>
void set_sprite_fg_colors_horiz(int anim_frame, int r, int c0, int c1, Colors... fg_color)
{
ttl::Rectangle bb { r, c0, 1, c1 - c0 + 1 };
set_sprite_fg_colors(anim_frame, bb, fg_color...);
}
void set_sprite_fg_color(int anim_frame, int r, int c, Color fg_color)
{
auto* texture = fetch_frame(anim_frame);
if (texture != nullptr)
texture->set_textel_fg_color(r, c, fg_color);
}
void fill_sprite_fg_colors(int anim_frame, Color fg_color)
{
auto* texture = fetch_frame(anim_frame);
texture->fg_colors.assign(area, fg_color);
}
void fill_sprite_fg_colors(int anim_frame, const ttl::Rectangle& bb, Color fg_color)
{
auto* texture = fetch_frame(anim_frame);
fill_sprite_data(texture->fg_colors, bb, fg_color);
}
void fill_sprite_fg_colors_vert(int anim_frame, int r0, int r1, int c, Color fg_color)
{
auto* texture = fetch_frame(anim_frame);
if (texture != nullptr)
for (int r = r0; r <= r1; ++r)
texture->set_textel_fg_color(r, c, fg_color);
}
void fill_sprite_fg_colors_horiz(int anim_frame, int r, int c0, int c1, Color fg_color)
{
auto* texture = fetch_frame(anim_frame);
if (texture != nullptr)
for (int c = c0; c <= c1; ++c)
texture->set_textel_fg_color(r, c, fg_color);
}
template<typename... Colors>
bool set_sprite_bg_colors(int anim_frame, Colors... bg_color)
{
auto* texture = fetch_frame(anim_frame);
return set_sprite_data(texture->bg_colors, bg_color...);
}
template<typename... Colors>
bool set_sprite_bg_colors(int anim_frame, const ttl::Rectangle& bb, Colors... bg_color)
{
auto* texture = fetch_frame(anim_frame);
return set_sprite_data(texture->bg_colors, bb, bg_color...);
}
template<typename... Colors>
void set_sprite_bg_colors_vert(int anim_frame, int r0, int r1, int c, Colors... bg_color)
{
ttl::Rectangle bb { r0, c, r1 - r0 + 1, 1 };
set_sprite_bg_colors(anim_frame, bb, bg_color...);
}
template<typename... Colors>
void set_sprite_bg_colors_horiz(int anim_frame, int r, int c0, int c1, Colors... bg_color)
{
ttl::Rectangle bb { r, c0, 1, c1 - c0 + 1 };
set_sprite_bg_colors(anim_frame, bb, bg_color...);
}
void set_sprite_bg_color(int anim_frame, int r, int c, Color bg_color)
{
auto* texture = fetch_frame(anim_frame);
if (texture != nullptr)
texture->set_textel_bg_color(r, c, bg_color);
}
void fill_sprite_bg_colors(int anim_frame, Color bg_color)
{
auto* texture = fetch_frame(anim_frame);
texture->bg_colors.assign(area, bg_color);
}
void fill_sprite_bg_colors(int anim_frame, const ttl::Rectangle& bb, Color bg_color)
{
auto* texture = fetch_frame(anim_frame);
fill_sprite_data(texture->bg_colors, bb, bg_color);
}
void fill_sprite_bg_colors_vert(int anim_frame, int r0, int r1, int c, Color bg_color)
{
auto* texture = fetch_frame(anim_frame);
if (texture != nullptr)
for (int r = r0; r <= r1; ++r)
texture->set_textel_bg_color(r, c, bg_color);
}
void fill_sprite_bg_colors_horiz(int anim_frame, int r, int c0, int c1, Color bg_color)
{
auto* texture = fetch_frame(anim_frame);
if (texture != nullptr)
for (int c = c0; c <= c1; ++c)
texture->set_textel_bg_color(r, c, bg_color);
}
template<typename... Materials>
bool set_sprite_materials(int anim_frame, Materials... mat)
{
auto* texture = fetch_frame(anim_frame);
return set_sprite_data(texture->materials, mat...);
}
template<typename... Materials>
bool set_sprite_materials(int anim_frame, const ttl::Rectangle& bb, Materials... mat)
{
auto* texture = fetch_frame(anim_frame);
return set_sprite_data(texture->materials, bb, mat...);
}
template<typename... Materials>
void set_sprite_materials_vert(int anim_frame, int r0, int r1, int c, Materials... mat)
{
ttl::Rectangle bb { r0, c, r1 - r0 + 1, 1 };
set_sprite_materials(anim_frame, bb, mat...);
}
template<typename... Materials>
void set_sprite_materials_horiz(int anim_frame, int r, int c0, int c1, Materials... mat)
{
ttl::Rectangle bb { r, c0, 1, c1 - c0 + 1 };
set_sprite_materials(anim_frame, bb, mat...);
}
void set_sprite_material(int anim_frame, int r, int c, int mat)
{
auto* texture = fetch_frame(anim_frame);
if (texture != nullptr)
texture->set_textel_material(r, c, mat);
}
void fill_sprite_materials(int anim_frame, int mat)
{
auto* texture = fetch_frame(anim_frame);
texture->materials.assign(area, mat);
}
void fill_sprite_materials(int anim_frame, const ttl::Rectangle& bb, int mat)
{
auto* texture = fetch_frame(anim_frame);
fill_sprite_data(texture->materials, bb, mat);
}
void fill_sprite_materials_vert(int anim_frame, int r0, int r1, int c, int mat)
{
auto* texture = fetch_frame(anim_frame);
if (texture != nullptr)
for (int r = r0; r <= r1; ++r)
texture->set_textel_material(r, c, mat);
}
void fill_sprite_materials_horiz(int anim_frame, int r, int c0, int c1, int mat)
{
auto* texture = fetch_frame(anim_frame);
if (texture != nullptr)
for (int c = c0; c <= c1; ++c)
texture->set_textel_material(r, c, mat);
}
// Replace args mean they replace the attribute if the main argument/attribute was found on that pixel.
// E.g. if bg_color was found on a pixel, then bg_color_replace will be used instead, if set.
// If a replace argument is set but not the main argument, then nothing will happen with that attribute.
// E.g. if ch is nullopt but ch_replace is non-nullopt, then it is the same as if both are nullopt.
// This mechanism allows you to do penumbra / umbra shading techniques and stuff like that.
bool plot_line(int sim_frame, const RC& p0, const RC& p1,
std::optional<char> ch, std::optional<char> ch_replace,
std::optional<Color> fg_color, std::optional<Color> fg_color_replace,
std::optional<Color> bg_color, std::optional<Color> bg_color_replace,
std::optional<int> mat, std::optional<int> mat_replace)
{
auto* texture = get_curr_sim_frame(sim_frame);
if (texture == nullptr)
return false;
std::vector<RC> points;
bresenham::plot_line(p0, p1, points);
auto f_set_attribute = [](auto& dst, const auto& src, const auto& src_replace)
{
if (src.has_value() && src_replace.has_value() && dst == src.value())
dst = src_replace.value();
else
dst = src.value_or(dst);
};
for (const auto& pt : points)
{
auto r = math::roundI(pt.r) - pos.r;
auto c = math::roundI(pt.c) - pos.c;
auto textel = (*texture)(r, c);
f_set_attribute(textel.ch, ch, ch_replace);
f_set_attribute(textel.fg_color, fg_color, fg_color_replace);
f_set_attribute(textel.bg_color, bg_color, bg_color_replace);
f_set_attribute(textel.mat, mat, mat_replace);
texture->set_textel(r, c, textel);
}
return true;
}
void flip_ud(int anim_frame)
{
auto f_flip_char = [](drawing::Textel& txt)
{
auto& ch = txt.ch;
switch(ch)
{
case '_': ch = '-'; break;
case 'W': ch = 'M'; break;
case 'M': ch = 'W'; break;
case 'w': ch = 'm'; break;
case 'm': ch = 'w'; break;
case '^': ch = 'v'; break;
case 'v': ch = '^'; break;
case '/': ch = '\\'; break;
case '\\': ch = '/'; break;
case 'b': ch = 'p'; break;
case 'p': ch = 'b'; break;
case 'z': ch = 's'; break;
case 's': ch = 'z'; break;
case 'Z': ch = 'S'; break;
case 'S': ch = 'Z'; break;
default: break;
}
};
auto* texture = fetch_frame(anim_frame);
const int half_height = size.r/2;
for (int c = 0; c < size.c; ++c)
{
for (int r = 0; r < half_height; ++r)
{
int r_inv = size.r - r - 1;
auto a = texture->operator()(r, c);
auto b = texture->operator()(r_inv, c);
f_flip_char(a);
f_flip_char(b);
texture->set_textel(r, c, b);
texture->set_textel(r_inv, c, a);
}
}
}
void flip_ud()
{
auto num_frames = stlutils::sizeI(texture_frames);
for (int anim_frame = 0; anim_frame < num_frames; ++anim_frame)
flip_ud(anim_frame);
}
void flip_lr(int anim_frame)
{
auto f_flip_char = [](drawing::Textel& txt)
{
auto& ch = txt.ch;
switch(ch)
{
case '/': ch = '\\'; break;
case '\\': ch = '/'; break;
case '(': ch = ')'; break;
case ')': ch = '('; break;
case '[': ch = ']'; break;
case ']': ch = '['; break;
case '}': ch = '{'; break;
case 'd': ch = 'b'; break;
case 'b': ch = 'd'; break;
case 'J': ch = 'L'; break;
case 'L': ch = 'J'; break;
case '<': ch = '>'; break;
case '>': ch = '<'; break;
case 'z': ch = 's'; break;
case 's': ch = 'z'; break;
case 'Z': ch = 'S'; break;
case 'S': ch = 'Z'; break;
default: break;
}
};
auto* texture = fetch_frame(anim_frame);
const int half_width = size.c/2;
for (int r = 0; r < size.r; ++r)
{
for (int c = 0; c < half_width; ++c)
{
int c_inv = size.c - c - 1;
auto a = texture->operator()(r, c);
auto b = texture->operator()(r, c_inv);
f_flip_char(a);
f_flip_char(b);
texture->set_textel(r, c, b);
texture->set_textel(r, c_inv, a);
}
}
}
void flip_lr()
{
auto num_frames = stlutils::sizeI(texture_frames);
for (int anim_frame = 0; anim_frame < num_frames; ++anim_frame)
flip_lr(anim_frame);
}
drawing::Texture* get_curr_sim_frame(int sim_frame) const
{
int frame_id = func_calc_anim_frame(sim_frame);
return get_curr_local_frame(frame_id);
}
drawing::Texture* get_curr_local_frame(int frame_id) const
{
if (frame_id >= stlutils::sizeI(texture_frames))
{
std::cerr << "ERROR in BitmapSprite::get_curr_frame() : Incorrect frame id: " + std::to_string(frame_id) + " for sprite \"" + name + "\"! Sprite only has " + std::to_string(texture_frames.size()) + " frames." << std::endl;
return nullptr;
}
return texture_frames[frame_id].get();
}
virtual int num_frames() const override
{
return stlutils::sizeI(texture_frames);
}
template<int NR, int NC>
bool draw(ScreenHandler<NR, NC>& sh, int sim_frame)
{
auto* texture = get_curr_sim_frame(sim_frame);
if (texture == nullptr)
return false;
drawing::draw_box_textured(sh,
pos.r - 1, pos.c - 1,
texture->size.r + 2, texture->size.c + 2,
drawing::SolarDirection::Zenith,
*texture);
return true;
}
virtual AABB<int> calc_curr_AABB(int /*sim_frame*/) const override
{
return { pos.r, pos.c, size.r, size.c };
}
virtual Vec2 calc_curr_centroid(int /*sim_frame*/) const override
{
return { static_cast<float>(pos.r) + size.r * 0.5f, static_cast<float>(pos.c) + size.c * 0.5f };
}
virtual bool_vector calc_curr_mask(int sim_frame, const std::vector<int>& mask_materials) override
{
const auto* texture = get_curr_sim_frame(sim_frame);
if (texture == nullptr)
return {};
const auto num_mats = stlutils::sizeI(texture->materials);
bool_vector mask(num_mats);
for (int mat_idx = 0; mat_idx < num_mats; ++mat_idx)
mask[mat_idx] = stlutils::contains(mask_materials, texture->materials[mat_idx]);
return mask;
}
virtual bool calc_cm() const override { return true; }
virtual bool is_opaque(int sim_frame, const RC& pt) const override
{
const auto* texture = get_curr_sim_frame(sim_frame);
if (texture == nullptr)
return false;
int r = pt.r - pos.r;
int c = pt.c - pos.c;
auto textel = (*texture)(r, c);
return !(textel.bg_color == Color::Transparent || textel.bg_color == Color::Transparent2);
}
virtual std::vector<RC> get_opaque_points(int sim_frame) const override
{
const auto* texture = get_curr_sim_frame(sim_frame);
if (texture == nullptr)
return {};
auto aabb = calc_curr_AABB(sim_frame);
std::vector<RC> opaque_points;
for (int r = aabb.r_min(); r <= aabb.r_max(); ++r)
{
for (int c = aabb.c_min(); c <= aabb.c_max(); ++c)
{
auto textel = (*texture)(r - pos.r, c - pos.c);
if (!(textel.bg_color == Color::Transparent || textel.bg_color == Color::Transparent2))
opaque_points.emplace_back(RC {r, c});
}
}
return opaque_points;
}
};
// /////////////////////////////////////////////////
class VectorSprite : public Sprite
{
struct LineSeg
{
std::array<Vec2, 2> pos;
char ch = 0;
styles::Style style;
int mat = 0;
};
struct VectorFrame
{
std::vector<LineSeg> line_segments;
};
std::vector<std::unique_ptr<VectorFrame>> vector_frames;
float rot_rad = 0.f;
VectorFrame* fetch_frame(int anim_frame)
{
while (stlutils::sizeI(vector_frames) <= anim_frame)
vector_frames.emplace_back(std::make_unique<VectorFrame>());
return vector_frames[anim_frame].get();
}
std::pair<Vec2, Vec2> calc_seg_world_pos_flt(const LineSeg& line_seg) const
{
const auto aspect_ratio = 1.5f;
auto rr0 = line_seg.pos[0].r;
auto cc0 = line_seg.pos[0].c;
auto rr1 = line_seg.pos[1].r;
auto cc1 = line_seg.pos[1].c;
float C = std::cos(rot_rad);
float S = std::sin(rot_rad);
auto r0 = pos.r + (C*rr0 - S*cc0);
auto c0 = pos.c + (S*rr0 + C*cc0)*aspect_ratio;
auto r1 = pos.r + (C*rr1 - S*cc1);
auto c1 = pos.c + (S*rr1 + C*cc1)*aspect_ratio;
Vec2 p0 { r0, c0 };
Vec2 p1 { r1, c1 };
return { p0, p1 };
}
std::pair<RC, RC> calc_seg_world_pos_round(const LineSeg& line_seg) const
{
auto [v0, v1] = calc_seg_world_pos_flt(line_seg);
return { to_RC_round(v0), to_RC_round(v1) };
}
public:
VectorSprite(const std::string& a_name) : Sprite(a_name) {}
// p0
// +-----------+
// | * |
// | ## |
// | ## # |
// | ## # |
// p2 | *# o x # | o = (0, 0) local origo from line segments' coordsys. Corresponds to Sprite::pos in world coords.
// | ### # | x = centre of mass. We rotate points around this point.
// | ###* |
// +-----------+ p1
// p0 : { -4, 4 } // local coordsys
// p1 : { 2, 5 } // local coordsys
// p2 : { 0, -3 } // local coordsys
// 1.
// (5, 5) AABB pos (world coordsys) from Sprite::pos + linesegs.
// +-----------+
// | |
// | ## |
// | ## # |
// | ## # |
// | ## o # | o = (0, 0) @ Sprite::pos = { 3, 5 } (world coordsys).
// | ### # |
// | #### |
// +-----------+
// 2.
//
// +-----------+
// | |
// | ## |
// | ## # |
// | ## # |
// | ## o x # | o = (0, 0) @ Sprite::pos = { 3, 5 } (world coordsys).
// | ### # | x = centre of mass = ~{ 3, 7 } (world coordsys).
// | #### |
// +-----------+
// x = { 0.55, 2 } (local coordsys), { 3.55, 7 } (world coordsys) (Bresenham)
// x = { -0.6667 2.0000 } (local coordsys), { 2.3333, 7 } (world coordsys) (Points)
// x = { 0.2727 2.0303 } (local coordsys), { 3, 7 } (world coordsys) (Bresenham, filled) <-- most correct. aspect ratio's fault?
// 3. Rotate around x.
// 4. Deduce new o from x.
// New idea: The user designs the sprite around an assumed center of mass.
// So "o" becomes the center of mass instead.
void add_line_segment(int anim_frame, const Vec2& p0, const Vec2& p1, char ch, const styles::Style& style, int mat = 0)
{
auto* vector_frame = fetch_frame(anim_frame);
auto& line_seg = vector_frame->line_segments.emplace_back();
line_seg.pos[0] = p0;
line_seg.pos[1] = p1;
line_seg.ch = ch;
line_seg.style = style;
line_seg.mat = mat;
}
void add_line_segment(int anim_frame, const Vec2& p0, const Vec2& p1, const styles::Style& style, int mat = 0)
{
add_line_segment(anim_frame, p0, p1, -1, style, mat);
}
virtual void clone_frame(int anim_frame, int from_anim_frame) override
{
const auto N = stlutils::sizeI(vector_frames);
if (from_anim_frame < N)
{
if (anim_frame >= N)
{
auto* frame_from = fetch_frame(from_anim_frame);
fetch_frame(anim_frame);
vector_frames[anim_frame] = std::make_unique<VectorFrame>(*frame_from);
}
else
std::cout << "ERROR in clone_frame() : anim_frame must be larger than or equal to the number of vector frames!" << std::endl;
}
else
std::cout << "ERROR in clone_frame() : from_anim_frame cannot be larger than or equal to the number of vector frames!" << std::endl;
}
void set_frame(int anim_frame, const VectorFrame& frame)
{
auto* frame_dst = fetch_frame(anim_frame);
*frame_dst = frame;
}
VectorFrame* get_curr_sim_frame(int sim_frame) const
{
int frame_id = func_calc_anim_frame(sim_frame);
return get_curr_local_frame(frame_id);
}
VectorFrame* get_curr_local_frame(int frame_id) const
{
if (frame_id >= stlutils::sizeI(vector_frames))
{
std::cerr << "ERROR in VectorSprite::get_curr_frame() : Incorrect frame id: " + std::to_string(frame_id) + " for sprite \"" + name + "\"! Sprite only has " + std::to_string(vector_frames.size()) + " frames." << std::endl;
return nullptr;
}
return vector_frames[frame_id].get();
}
virtual int num_frames() const override
{
return stlutils::sizeI(vector_frames);
}
void set_rotation(float rot_deg)
{
rot_rad = math::deg2rad(rot_deg);
}
float get_rotation() const
{
return math::rad2deg(rot_rad);
}
template<int NR, int NC>
bool draw(ScreenHandler<NR, NC>& sh, int sim_frame)
{
auto* vector_frame = get_curr_sim_frame(sim_frame);
if (vector_frame == nullptr)
return false;
for (const auto& line_seg : vector_frame->line_segments)
{
auto [p0, p1] = calc_seg_world_pos_round(line_seg);
char ch = 0;
if (line_seg.ch == -1)
{
auto dir = p0 - p1;
if (!(dir.r == 0 && dir.c == 0))
{
auto dr = static_cast<float>(dir.r);
auto dc = static_cast<float>(dir.c);
auto lineseg_rot_deg = math::rad2deg(std::atan2(dr, dc));
if (math::in_range<float>(lineseg_rot_deg, -180.f, -158.f, Range::ClosedOpen))
ch = '-';
else if (math::in_range<float>(lineseg_rot_deg, -158.f, -112.f, Range::ClosedOpen))
ch = '\\';
else if (math::in_range<float>(lineseg_rot_deg, -112.f, -68.f, Range::ClosedOpen))
ch = '|';
else if (math::in_range<float>(lineseg_rot_deg, -68.f, -22.f, Range::ClosedOpen))
ch = '/';
else if (math::in_range<float>(lineseg_rot_deg, -22.f, 22.f, Range::ClosedOpen))
ch = '-';
else if (math::in_range<float>(lineseg_rot_deg, 22.f, 68.f, Range::ClosedOpen))
ch = '\\';
else if (math::in_range<float>(lineseg_rot_deg, 68.f, 112.f, Range::ClosedOpen))
ch = '|';
else if (math::in_range<float>(lineseg_rot_deg, 112.f, 158.f, Range::ClosedOpen))
ch = '/';
else if (math::in_range<float>(lineseg_rot_deg, 158.f, 180.f, Range::Closed))
ch = '-';
else
throw std::invalid_argument(std::to_string(lineseg_rot_deg));
}
}
else
ch = line_seg.ch;
bresenham::plot_line(sh, p0, p1, std::string(1, ch), line_seg.style.fg_color, line_seg.style.bg_color);
}
return true;
}
virtual AABB<int> calc_curr_AABB(int sim_frame) const override
{
auto* vector_frame = get_curr_sim_frame(sim_frame);
if (vector_frame == nullptr)
return {};
AABB<int> aabb;
for (const auto& line_seg : vector_frame->line_segments)
{
auto [p0, p1] = calc_seg_world_pos_round(line_seg);
aabb.add_point(p0);
aabb.add_point(p1);
}
return aabb;
}
virtual Vec2 calc_curr_centroid(int sim_frame) const override
{
return to_Vec2(pos); // Assuming you designed the sprite around the centroid / CoM.
}
virtual bool_vector calc_curr_mask(int sim_frame, const std::vector<int>& mask_materials) override
{
const auto* vector_frame = get_curr_sim_frame(sim_frame);
if (vector_frame == nullptr)
return {};
auto aabb = calc_curr_AABB(sim_frame);
auto rmin = aabb.r_min();
auto cmin = aabb.c_min();
const int num_points = aabb.width()*aabb.height();
bool_vector mask(num_points);
std::vector<RC> points;
for (const auto& line_seg : vector_frame->line_segments)
{
if (!stlutils::contains(mask_materials, line_seg.mat))
continue;