-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.h
More file actions
2172 lines (2004 loc) · 64.1 KB
/
engine.h
File metadata and controls
2172 lines (2004 loc) · 64.1 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
// Unimaginable Engine
// FotonEngine
#ifndef DEF
#define DEF
#include <iostream>
#include <math.h>
#include <SFML\\Graphics.hpp>
#include <vector>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstring>
#include <time.h>
#include <chrono>
#include <thread>
#include "sprites.h"
#include <ostream>
#include <algorithm>
#include <regex>
using namespace std;
//using namespace concurrency;
const bool True = true;
const bool False = false;
const long double PI = 3.1415926535898;
int index_global;
bool comp_fs(vector<double> a, vector<double> b) {
return a[1] < b[1];
}
void replaceAll(string& s, const string& search, const string& replace) {
for (size_t pos = 0; ; pos += replace.length()) {
// Locate the substring to replace
pos = s.find(search, pos);
if (pos == string::npos) break;
// Replace by erasing and inserting
s.erase(pos, search.length());
s.insert(pos, replace);
}
}
void tokenize(std::string const& str, const char delim,
std::vector<std::string>& out)
{
// ñòðîèì ïîòîê èç ñòðîêè
std::stringstream ss(str);
std::string s;
while (std::getline(ss, s, delim)) {
out.push_back(s);
}
}
struct Vector2{
double x;
double y;
};
struct Vector3 {
double x;
double y;
double z;
};
Vector2 intersection_circle_line(Vector2 v_center, Vector2 v_begin, Vector2 v_end, double r)
{ //íàõîäèì äèñêðèìåíàíò êâàäðàòíîãî óðàâíåíèÿ
double x, y, k, b, x1, y1, x2, y2, x_min, x_max, y_min, y_max;
Vector2 res;
x = v_center.x;
y = v_center.y;
x1 = v_begin.x + 0.00000001;
x2 = v_end.x - 0.00000001;
y1 = v_begin.y + 0.00000001;
y2 = v_end.y - 0.00000001;
if ((x2 - x1) == 0) {
cout << "math error" << endl;
res.x = 0;
res.y = 0;
return res;
}
k = (y2 - y1) / (x2 - x1);
if ((x2 - x1) == 0) {
cout << "math error" << endl;
res.x = 0;
res.y = 0;
return res;
}
b = (y1 * (x2 - x1) - x1 * (y2 - y1)) / (x2 - x1);
double d = (pow((2 * k * b - 2 * x - 2 * y * k), 2) - (4 + 4 * k * k) * (b * b - r * r + x * x + y * y - 2 * y * b));
//åñëè îí ìåíüøå 0, óðàâíåíèå íå èìååò ðåøåíèÿ
if (d < 0)
{
res.x = 0;
res.y = 0;
}
else
{
//èíà÷å íàõîäèì êîðíè êâàäðàòíîãî óðàâíåíèÿ
x1 = ((-(2 * k * b - 2 * x - 2 * y * k) - sqrt(d)) / (2 + 2 * k * k));
x2 = ((-(2 * k * b - 2 * x - 2 * y * k) + sqrt(d)) / (2 + 2 * k * k));
y1 = k * x1 + b;
y2 = k * x2 + b;
if ((x1 - v_begin.x) * (x1 - v_begin.x) + (y1 - v_begin.y) * (y1 - v_begin.y) < (x2 - v_begin.x) * (x2 - v_begin.x) + (y2 - v_begin.y) * (y2 - v_begin.y)) {
res.x = x1;
res.y = y1;
}
else {
res.x = x2;
res.y = y2;
}
x_min = min(v_begin.x, v_end.x);
x_max = max(v_begin.x, v_end.x);
y_min = min(v_begin.y, v_end.y);
y_max = max(v_begin.y, v_end.y);
if ((res.x < x_min || res.x > x_max) && (res.y < y_min || res.y > y_max)) {
res.x = 0;
res.y = 0;
}
}
return res;
}
Vector2 intersection_2_lines(Vector2 v1, Vector2 v2, Vector2 v3, Vector2 v4) {
double x1, y1, x2, y2, k1, b1, k2, b2, x, y;
Vector2 res;
//// Line 1 <<<
x1 = v1.x + 0.00000001;
x2 = v2.x - 0.00000001;
y1 = v1.y + 0.00000001;
y2 = v2.y - 0.00000001;
if ((x2 - x1) == 0) {
cout << "math error" << endl;
res.x = 0;
res.y = 0;
return res;
}
k1 = (y2-y1) / (x2-x1);
if ((x2 - x1) == 0) {
cout << "math error" << endl;
res.x = 0;
res.y = 0;
return res;
}
b1 = (y1 * (x2 - x1) - x1 * (y2 - y1)) / (x2 - x1);
//// Line 1 >>>
//// Line 2 <<<
x1 = v3.x + 0.00000001;
x2 = v4.x - 0.00000001;
y1 = v3.y + 0.00000001;
y2 = v4.y - 0.00000001;
if ((x2 - x1) == 0) {
cout << "math error" << endl;
res.x = 0;
res.y = 0;
return res;
}
k2 = (y2 - y1) / (x2 - x1);
if ((x2 - x1) == 0) {
cout << "math error" << endl;
res.x = 0;
res.y = 0;
return res;
}
b2 = (y1 * (x2 - x1) - x1 * (y2 - y1)) / (x2 - x1);
//// Line 2 >>>
if ((k1 - k2) == 0) {
cout << "math error" << endl;
res.x = 0;
res.y = 0;
return res;
}
x = (b2 - b1) / (k1 - k2);
y = k2 * x + b2;
res.x = x;
res.y = y;
return res;
}
Vector3 double_to_rgb(double x) {
Vector3 res;
x *= 10000.0;
res.x = (int)x % 256;
res.y = ((int)x / 256) % 256;
res.z = (int)x / (256 * 256);
return res;
}
struct OZ_Code_struct {
double oz;
int part;
int a;
int b;
int c;
};
double radians(double a) {
return (a / 180 * 3.141592653);
}
double to_double(int a) {
return a;
}
int to_int(double a) {
return a;
}
int sfUint8_to_int(sf::Uint8 uint8_num) {
return uint8_num * 1;
}
double distance_vec2(Vector2 begin, Vector2 end) {
return sqrt((end.x-begin.x)* (end.x - begin.x) + (end.y-begin.y)* (end.y - begin.y));
}
class UnvisibleRectSprite3D {
public:
double x, y;
double x_size;
double y_size;
long long id;
void init(double x_, double y_, double x_size_, double y_size_, long long id_) {
id = id_;
x = x_;
y = y_;
x_size = x_size_;
y_size = y_size_;
//_engine = ENGINE;
}
bool is_collision(double x_, double y_) {
if (abs(x - x_) <= x_size / 2 && abs(y - y_) <= y_size / 2) {
return true;
}
else return false;
}
bool intersect(Vector2 v1, Vector2 v2) {
if (is_intersect(x - x_size / 2, y - y_size / 2, x - x_size / 2, y + y_size / 2, v1.x, v1.y, v2.x, v2.y)) {
return True;
}
else if (is_intersect(x + x_size / 2, y - y_size / 2, x + x_size / 2, y + y_size / 2, v1.x, v1.y, v2.x, v2.y)) {
return True;
}
else if (is_intersect(x - x_size / 2, y - y_size / 2, x + x_size / 2, y - y_size / 2, v1.x, v1.y, v2.x, v2.y)) {
return True;
}
else if (is_intersect(x - x_size / 2, y + y_size / 2, x + x_size / 2, y + y_size / 2, v1.x, v1.y, v2.x, v2.y)) {
return True;
}
else {
return false;
}
}
Vector2 intersection_rect_line(Vector2 v_begin, Vector2 v_end) {
Vector2 v1, v2, v3, v4, vi1, vi2, vi3, vi4, res;
double d1, d2, d3, d4;
v1.x = x - x_size / 2;
v1.y = y - y_size / 2;
v2.x = x + x_size / 2;
v2.y = y - y_size / 2;
v3.x = x - x_size / 2;
v3.y = y + y_size / 2;
v4.x = x + x_size / 2;
v4.y = y + y_size / 2;
vi1 = intersection_2_lines(v_begin, v_end, v1, v2);
vi2 = intersection_2_lines(v_begin, v_end, v1, v3);
vi3 = intersection_2_lines(v_begin, v_end, v2, v4);
vi4 = intersection_2_lines(v_begin, v_end, v4, v3);
d1 = distance_vec2(v_begin, vi1);
d2 = distance_vec2(v_begin, vi2);
d3 = distance_vec2(v_begin, vi3);
d4 = distance_vec2(v_begin, vi4);
//
if (d1 <= d4 && is_intersect2(v1, v2, v_begin, v_end)) {
return vi1;
}
if (d2 <= d3 && is_intersect2(v1, v3, v_begin, v_end)) {
return vi2;
}
if (d3 <= d2 && is_intersect2(v2, v4, v_begin, v_end)) {
return vi3;
}
if (d4 <= d1 && is_intersect2(v4, v3, v_begin, v_end)) {
return vi4;
}
res.x = 0;
res.y = 0;
cout << "error" << endl;
return res;
}
bool is_intersect(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) {
double dx0 = x2 - x1;
double dx1 = x4 - x3;
double dy0 = y2 - y1;
double dy1 = y4 - y3;
double p0 = dy1 * (x4 - x1) - dx1 * (y4 - y1);
double p1 = dy1 * (x4 - x2) - dx1 * (y4 - y2);
double p2 = dy0 * (x2 - x3) - dx0 * (y2 - y3);
double p3 = dy0 * (x2 - x4) - dx0 * (y2 - y4);
return (p0 * p1 <= 0) && (p2 * p3 <= 0);
}
bool is_intersect2(Vector2 v1, Vector2 v2, Vector2 v3, Vector2 v4) {
double dx0 = v2.x - v1.x;
double dx1 = v4.x - v3.x;
double dy0 = v2.y - v1.y;
double dy1 = v4.y - v3.y;
double p0 = dy1 * (v4.x - v1.x) - dx1 * (v4.y - v1.y);
double p1 = dy1 * (v4.x - v2.x) - dx1 * (v4.y - v2.y);
double p2 = dy0 * (v2.x - v3.x) - dx0 * (v2.y - v3.y);
double p3 = dy0 * (v2.x - v4.x) - dx0 * (v2.y - v4.y);
return (p0 * p1 <= 0) && (p2 * p3 <= 0);
}
};
class RectSprite3D {
public:
double x, y;
double x_size;
double y_size;
bool orient_x;
int res_x_texture, res_y_texture, ry, tc1, tc2, tc3;
vector<vector<vector<vector<char>>>> textures_x;
vector<vector<vector<vector<char>>>> textures_y;
long long id;
void init(double x_, double y_, double x_size_, double y_size_, int res_x_texture_, int res_y_texture_, int ry_, long long id_, int text_code_1, int text_code_2, int text_code_3) {
id = id_;
x = x_;
y = y_;
tc1 = text_code_1;
tc2 = text_code_2;
tc3 = text_code_3;
x_size = x_size_;
y_size = y_size_;
res_x_texture = res_x_texture_;
res_y_texture = res_y_texture_;
ry = ry_;
orient_x = true;
//_engine = ENGINE;
}
void load_texture_x(string filename, int texture_code) {
sf::Image texture_img;
texture_img.loadFromFile(filename);
const sf::Uint8* pixels1 = new sf::Uint8[res_x_texture * ry * 4 + 4];
sf::Uint8* pixels2 = new sf::Uint8[res_x_texture * ry * 4 + 4];
pixels1 = texture_img.getPixelsPtr();
int i, j, k;
for (i = 0; i < res_x_texture; i++) {
for (j = 0; j < ry; j++) {
textures_x[texture_code][i][j][0] = sfUint8_to_int(pixels1[i * ry * 4 + j * 4 - 4]);
textures_x[texture_code][i][j][1] = sfUint8_to_int(pixels1[i * ry * 4 + j * 4 - 3]);
textures_x[texture_code][i][j][2] = sfUint8_to_int(pixels1[i * ry * 4 + j * 4 - 2]);
}
}
}
void load_texture_y(string filename, int texture_code) {
sf::Image texture_img;
texture_img.loadFromFile(filename);
const sf::Uint8* pixels1 = new sf::Uint8[res_y_texture * ry * 4 + 4];
sf::Uint8* pixels2 = new sf::Uint8[res_y_texture * ry * 4 + 4];
pixels1 = texture_img.getPixelsPtr();
int i, j, k;
for (i = 0; i < res_y_texture; i++) {
for (j = 0; j < ry; j++) {
textures_y[texture_code][i][j][0] = sfUint8_to_int(pixels1[i * ry * 4 + j * 4 - 4]);
textures_y[texture_code][i][j][1] = sfUint8_to_int(pixels1[i * ry * 4 + j * 4 - 3]);
textures_y[texture_code][i][j][2] = sfUint8_to_int(pixels1[i * ry * 4 + j * 4 - 2]);
}
}
}
void set_orient_x(bool is23) {
}
bool intersect(Vector2 v1, Vector2 v2) {
if(is_intersect(x-x_size/2, y-y_size/2, x - x_size/2, y+y_size/2, v1.x, v1.y, v2.x, v2.y)) {
return True;
}
else if (is_intersect(x + x_size / 2, y - y_size / 2, x + x_size / 2, y + y_size / 2, v1.x, v1.y, v2.x, v2.y)) {
return True;
}
else if (is_intersect(x - x_size / 2, y - y_size / 2, x + x_size / 2, y - y_size / 2, v1.x, v1.y, v2.x, v2.y)) {
return True;
}
else if (is_intersect(x - x_size / 2, y + y_size / 2, x + x_size / 2, y + y_size / 2, v1.x, v1.y, v2.x, v2.y)) {
return True;
}
else {
return false;
}
}
Vector2 intersection_rect_line(Vector2 v_begin, Vector2 v_end) {
Vector2 v1, v2, v3, v4, vi1, vi2, vi3, vi4, res;
double d1, d2, d3, d4;
v1.x = x - x_size / 2;
v1.y = y - y_size / 2;
v2.x = x + x_size / 2;
v2.y = y - y_size / 2;
v3.x = x - x_size / 2;
v3.y = y + y_size / 2;
v4.x = x + x_size / 2;
v4.y = y + y_size / 2;
vi1 = intersection_2_lines(v_begin, v_end, v1, v2);
vi2 = intersection_2_lines(v_begin, v_end, v1, v3);
vi3 = intersection_2_lines(v_begin, v_end, v2, v4);
vi4 = intersection_2_lines(v_begin, v_end, v4, v3);
d1 = distance_vec2(v_begin, vi1);
d2 = distance_vec2(v_begin, vi2);
d3 = distance_vec2(v_begin, vi3);
d4 = distance_vec2(v_begin, vi4);
//
if (is_intersect2(v1, v2, v_begin, v_end)) {
if (is_intersect2(v1, v3, v_begin, v_end)) {
if (d1 <= d2) {
return vi1;
}
else {
return vi2;
}
}
if (is_intersect2(v2, v4, v_begin, v_end)) {
if (d1 <= d3) {
return vi1;
}
else {
return vi3;
}
}
if (is_intersect2(v4, v3, v_begin, v_end)) {
if (d1 <= d4) {
return vi1;
}
else {
return vi4;
}
}
}
if (is_intersect2(v1, v3, v_begin, v_end)) {
if (is_intersect2(v1, v2, v_begin, v_end)) {
if (d2 <= d1) {
return vi2;
}
else {
return vi1;
}
}
if (is_intersect2(v2, v4, v_begin, v_end)) {
if (d2 <= d3) {
return vi2;
}
else {
return vi3;
}
}
if (is_intersect2(v4, v3, v_begin, v_end)) {
if (d2 <= d4) {
return vi2;
}
else {
return vi4;
}
}
}
if (is_intersect2(v2, v4, v_begin, v_end)) {
if (is_intersect2(v1, v3, v_begin, v_end)) {
if (d3 <= d2) {
return vi3;
}
else {
return vi2;
}
}
if (is_intersect2(v1, v2, v_begin, v_end)) {
if (d3 <= d1) {
return vi3;
}
else {
return vi1;
}
}
if (is_intersect2(v4, v3, v_begin, v_end)) {
if (d3 <= d4) {
return vi3;
}
else {
return vi4;
}
}
}
if (is_intersect2(v4, v3, v_begin, v_end)) {
if (is_intersect2(v1, v3, v_begin, v_end)) {
if (d4 <= d2) {
return vi4;
}
else {
return vi2;
}
}
if (is_intersect2(v1, v2, v_begin, v_end)) {
if (d4 <= d1) {
return vi4;
}
else {
return vi1;
}
}
if (is_intersect2(v2, v4, v_begin, v_end)) {
if (d4 <= d3) {
return vi4;
}
else {
return vi3;
}
}
}
res.x = 0;
res.y = 0;
cout << "error" << endl;
return res;
}
bool is_intersect(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) {
double dx0 = x2 - x1;
double dx1 = x4 - x3;
double dy0 = y2 - y1;
double dy1 = y4 - y3;
double p0 = dy1 * (x4 - x1) - dx1 * (y4 - y1);
double p1 = dy1 * (x4 - x2) - dx1 * (y4 - y2);
double p2 = dy0 * (x2 - x3) - dx0 * (y2 - y3);
double p3 = dy0 * (x2 - x4) - dx0 * (y2 - y4);
return (p0 * p1 <= 0) && (p2 * p3 <= 0);
}
bool is_intersect2(Vector2 v1, Vector2 v2, Vector2 v3, Vector2 v4) {
double dx0 = v2.x - v1.x;
double dx1 = v4.x - v3.x;
double dy0 = v2.y-v1.y;
double dy1 = v4.y-v3.y;
double p0 = dy1 * (v4.x-v1.x) - dx1 * (v4.y - v1.y);
double p1 = dy1 * (v4.x-v2.x) - dx1 * (v4.y - v2.y);
double p2 = dy0 * (v2.x-v3.x) - dx0 * (v2.y-v3.y);
double p3 = dy0 * (v2.x-v4.x) - dx0 * (v2.y-v4.y);
return (p0 * p1 <= 0) && (p2 * p3 <= 0);
}
bool is_collision(double x_, double y_) {
if (abs(x - x_) <= x_size / 2 && abs(y - y_) <= y_size / 2) {
return true;
}
else return false;
}
OZ_Code_struct get_oz(double x_, double y_, double angle) {
double ox, oy;
OZ_Code_struct res;
//cout << orient_x;
if (orient_x) {
ox = (x_ - (x - x_size / 2)) / x_size;
oy = (y_ - (y - y_size / 2)) / y_size;
if (abs(ox - 0.5) < abs(oy - 0.5)) {
res = OZ_Code_struct();
res.oz = ox;
if (id == 1) {
if (angle > 180) {
res.part = tc2;
}
else {
res.part = tc1;
}
}
else {
res.part = tc1;
}
return res;
}
else {
res = OZ_Code_struct();
res.oz = 1 - oy;
if (id == 0) {
res.part = tc2;
}
else if (id == 1) {
res.part = tc3;
}
return res;
}
}
else {
ox = (x_ - (x - x_size / 2)) / x_size;
oy = (y_ - (y - y_size / 2)) / y_size;
if (abs(ox - 0.5) < abs(oy - 0.5)) {
res = OZ_Code_struct();
res.oz = ox;
if (id == 0) {
res.part = tc2;
}
else if (id == 1) {
res.part = tc3;
}
return res;
}
else {
res = OZ_Code_struct();
res.oz = oy;
res.part = tc1;
return res;
}
}
}
};
class FlatSprite3D {
public:
double x1, y1, x2, y2, x, y;
double angle;
long long id;
int ry, texture_size_x, ctc;
vector<vector<vector<vector<char>>>> image;
vector<vector<vector<double>>> triangles;
void (*get_capture)(double, double, long long, bool, vector<vector<vector<char>>>&, vector<vector<vector<double>>>&) {
};
void init(double x1_, double y1_, double x2_, double y2_, long long id_, int ry_, int texture_size_x_, int images_) {
//void (*get_capture_)(double, double, long long, bool, vector<vector<vector<char>>>&, vector<vector<vector<double>>>&), int ry_, int texture_size_x_) {
//get_capture = get_capture_;
x1 = x1_;
x2 = x2_;
y1 = y1_;
y2 = y2_;
id = id_;
ry = ry_;
texture_size_x = texture_size_x_;
image.resize(images_ , vector<vector<vector<char>>>(texture_size_x, vector<vector<char>>(ry, vector<char>(4, 0))));
x = x1 + (x2 - x1)/2;
y = y1 + (y2 - y1)/2;
angle = 0;
ctc = 0;
}
void render_stage(double angle, double x_, double y_) {
}
void load_texture(int texture_code, string filename) {
sf::Image texture_img;
texture_img.loadFromFile(filename);
const sf::Uint8* pixels1 = new sf::Uint8[texture_size_x * ry * 4 + 4];
sf::Uint8* pixels2 = new sf::Uint8[texture_size_x * ry * 4 + 4];
pixels1 = texture_img.getPixelsPtr();
int i, j, k;
for (i = 0; i < texture_size_x; i++) {
for (j = 0; j < ry; j++) {
image[texture_code][i][j][0] = sfUint8_to_int(pixels1[i * ry * 4 + j * 4 - 4]);
image[texture_code][i][j][1] = sfUint8_to_int(pixels1[i * ry * 4 + j * 4 - 3]);
image[texture_code][i][j][2] = sfUint8_to_int(pixels1[i * ry * 4 + j * 4 - 2]);
image[texture_code][i][j][3] = sfUint8_to_int(pixels1[i * ry * 4 + j * 4 - 1]);
}
}
}
void set_diff(double x_, double y_) {
x1 += x_;
x2 += x_;
y1 += y_;
y2 += y_;
x += x_;
y += y_;
}
void set_angle(double angle_) {
double size;
size = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2))/ 2;
x1 = x - cos(angle) * size;
x2 = x + cos(angle) * size;
y1 = y - sin(angle) * size;
y2 = y + sin(angle) * size;
angle = angle_;
}
void set_pos(double x_, double y_) {
x = x_;
y = y_;
double x1_ = x - (x2 - x1) / 2;
double x2_ = x + (x2 - x1) / 2;
double y1_ = y - (y2 - y1) / 2;
double y2_ = y + (y2 - y1) / 2;
x1 = x1_;
x2 = x2_;
y1 = y1_;
y2 = y2_;
}
bool is_collision(double x3, double y3, double x4, double y4) {
double dx0 = x2 - x1;
double dx1 = x4 - x3;
double dy0 = y2 - y1;
double dy1 = y4 - y3;
double p0 = dy1 * (x4 - x1) - dx1 * (y4 - y1);
double p1 = dy1 * (x4 - x2) - dx1 * (y4 - y2);
double p2 = dy0 * (x2 - x3) - dx0 * (y2 - y3);
double p3 = dy0 * (x2 - x4) - dx0 * (y2 - y4);
return (p0 * p1 <= 0) && (p2 * p3 <= 0);
}
double get_oz(double c_x, double c_y) {
if (abs(x2 - x1) > abs(y2 - y1)) {
return abs((c_x - x1) / (x2 - x1));
}
else {
return abs((c_y - y1) / (y2 - y1));
}
}
};
class CircleSprite3D {
public:
double x, y;
double radius;
int texture_resolution;
long long id;
vector<vector<vector<char>>> texture;
int ry;
void init(double x_, double y_, double radius_, int texture_res, long long id_, int ry_) {
x = x_;
y = y_;
radius = radius_;
texture_resolution = texture_res;
id = id_;
ry = ry_;
texture.resize(texture_res, vector<vector<char>>(ry, vector<char>(3, 0)));
}
bool is_collision(double x_, double y_) {
return sqrt((x_ - x) * (x_ - x) + (y_ - y) * (y_ - y)) < radius;
}
double get_oz(double x_, double y_) {
double dx = x_ - x;
double dy = y_ - y;
return (atan2(dx, dy) /4/ PI+0.5);
}
void load_texture(int texture_code, string filename) {
sf::Image texture_img;
texture_img.loadFromFile(filename);
const sf::Uint8* pixels1 = new sf::Uint8[texture_resolution * ry * 4 + 4];
sf::Uint8* pixels2 = new sf::Uint8[texture_resolution * ry * 4 + 4];
pixels1 = texture_img.getPixelsPtr();
int i, j, k;
for (i = 0; i < texture_resolution; i++) {
for (j = 0; j < ry; j++) {
texture[i][j][0] = sfUint8_to_int(pixels1[i * texture_resolution * 4 + j * 4 - 4]);
texture[i][j][1] = sfUint8_to_int(pixels1[i * texture_resolution * 4 + j * 4 - 3]);
texture[i][j][2] = sfUint8_to_int(pixels1[i * texture_resolution * 4 + j * 4 - 2]);
texture[i][j][3] = sfUint8_to_int(pixels1[i * texture_resolution * 4 + j * 4 - 1]);
}
}
}
};
class Engine3D {
public:
int rx, ry, RESOLUTION_X, RESOLUTION_Y;
double x, y, angle, d_angle;
map<char, int> floor_dir;
map<char, int> ceil_dir;
map<char, int> map_dir;
vector<vector<vector<char>>> render_matrix, butt1;
vector<vector<vector<float>>> rays_steps;
vector<vector<int>> mAp, mAp_floor, mAp_ceil;
vector<vector<vector<double>>> sprites_flat_heights;
vector<vector<vector<double>>> sorted_i_sf;
vector<vector<vector<vector<char>>>> textures;
vector<vector<double>> heights;
vector<vector<vector<vector<float>>>> floor_ceil_pre_counted_values;
vector<RectSprite3D> sprites_objects;
vector<FlatSprite3D> flat_sprites;
vector<UnvisibleRectSprite3D> unv_rect_collision;
vector<CircleSprite3D> circle_sprites;
float* floor_ceil_pcv_arr;
thread* threads_heights;
thread* threads_render;
thread* threads_;
//int d_angle;
int threads_num;
int integer_x, integer_y, page, map_width, map_height, n_of_objects, n_flat_sprites, n_colliders, n_circle_sprites;
sf::Uint8* bg_main;
sf::Texture bg_texture;
sf::Image bg_image;
double x_past, y_past;
double ray_step;
bool is_butt_1;
bool is_stoped_heights, is_h;
bool is_render;
bool not_load;
sf::Uint8* h_map;
sf::Uint8* pixels;
double light_force;
bool is_use_shaders;
sf::Glsl::Vec4* u_heights;
void init(double x_, double y_, double angle_, string bg_main_filename, double ray_step_k, int rx_, int ry_, int n_of_objects_, int n_flat_sprites_, int n_colliders_) {
not_load = true;
is_stoped_heights = true;
RESOLUTION_X = rx_;
RESOLUTION_Y = ry_;
index_global = 0;
rx = rx_;
ry = ry_;
is_h = false;
is_butt_1 = false;
n_colliders = n_colliders_;
n_of_objects = n_of_objects_;
threads_num = std::thread::hardware_concurrency();
cout << "threads: " << threads_num << endl;
threads_heights = new thread[threads_num];
floor_ceil_pcv_arr = new float[180 * rx * ry * 2];
sprites_objects.resize(n_of_objects);
unv_rect_collision.resize(n_colliders);
n_flat_sprites = n_flat_sprites_;
//floor_ceil_pre_counted_values.resize(360, vector<vector<vector<float>>>( rx, vector<vector<float>>(ry/2, vector<float>(2, 0))));
//render_matrix = render_matrix(360, vector<vector<int>>(rx, vector<int>(2)));
//rays_steps = rays_steps(360, vector<vector<double>>(rx, vector<double>(2)));
const sf::Uint8* bg_const = new sf::Uint8[RESOLUTION_X * RESOLUTION_Y * 4 + 4];
pixels = new sf::Uint8[RESOLUTION_X * RESOLUTION_Y * 4 + 4];
bg_main = new sf::Uint8[RESOLUTION_X * RESOLUTION_Y * 4 + 4];
u_heights = new sf::Glsl::Vec4[256];
page = 1;
flat_sprites.resize(n_flat_sprites);
heights.resize(rx, vector<double>(10, 0));
sprites_flat_heights.resize(n_flat_sprites, vector<vector<double>>(rx, vector<double>(10, 0)));
int gg, g;
sorted_i_sf.resize(rx, vector<vector<double>>(n_flat_sprites, vector<double>(2,0)));
for (g = 0; g < rx; g++) {
for (gg = 0; gg < n_flat_sprites; gg++) {
sorted_i_sf[g][gg][0] = 0;
sorted_i_sf[g][gg][1] = 0;
}
}
butt1.resize(128, vector<vector<char>>(64, vector<char>(3, 0)));
sf::Image texture_img;
texture_img.loadFromFile("resources\\images\\butt1.png");
const sf::Uint8* pixels1 = new sf::Uint8[RESOLUTION_X * RESOLUTION_Y * 4 + 4];
sf::Uint8* pixels2 = new sf::Uint8[RESOLUTION_X * RESOLUTION_Y * 4 + 4];
h_map = new sf::Uint8[RESOLUTION_X * RESOLUTION_Y * 4 + 4];
pixels1 = texture_img.getPixelsPtr();
int i, j, k;
for (i = 0; i < 128; i++) {
for (j = 0; j < 64; j++) {
butt1[i][j][0] = sfUint8_to_int(pixels1[j * 128 * 4 + i * 4 - 4]);
butt1[i][j][1] = sfUint8_to_int(pixels1[j * 128 * 4 + i * 4 - 3]);
butt1[i][j][2] = sfUint8_to_int(pixels1[j * 128 * 4 + i * 4 - 2]);
}
}
render_matrix.resize(rx, vector<vector<char>>(ry, vector<char>(3, 0)));
rays_steps.resize(360, vector<vector<float>>(rx, vector<float>(2, 0)));
textures.resize(25, vector<vector<vector<char>>>(ry, vector<vector<char>>(ry, vector<char>(3, 0))));
bg_image.loadFromFile(bg_main_filename);
bg_const = bg_image.getPixelsPtr();
int i1;
for (i1 = 0; i1 < rx * ry * 4; i1++) {
bg_main[i1] = bg_const[i1];
}
x = x_;
y = y_;
angle = angle_;
d_angle = 90.0;
ray_step = 1.0 / to_double(rx) * ray_step_k;
std::cout << "inizialisation rays_steps" << endl;
//rays_steps[359][127][1] = 2.4;
//std::cout << to_double(abs(rx / 2 - 0)) / to_double(rx) * 10 << endl;
for (i = 0; i < 360; i++) {
for (j = 0; j < rx; j++) {
k = 0;
rays_steps[i][j][0] = cos(radians(i - (-rx / 2.0 + j) / rx * d_angle)) * ray_step;
rays_steps[i][j][1] = sin(radians(i - (-rx / 2.0 + j) / rx * d_angle)) * ray_step;
}
}
for (k = 0; k < 360; k++) {
for (i = 0; i < rx; i++) {
for (j = 0; j < ry / 2; j++) {
floor_ceil_pcv_arr[k * rx * ry + i * ry + j * 2] = 1.0 / (ry / 2 - j) * ry / 2 / cos(radians(to_double(i) / rx * d_angle - d_angle/2)) * rays_steps[k][i][0] / ray_step;;
floor_ceil_pcv_arr[k * rx * ry + i * ry + j * 2 + 1] = 1.0 / (ry / 2 - j) * ry / 2 / cos(radians(to_double(i) / rx * d_angle - d_angle / 2)) * rays_steps[k][i][1] / ray_step;
//floor_ceil_pre_counted_values[k][i][j][0] = 1.0 / (ry / 2 - j) * ry / 2 / cos(radians(to_double(i) / rx * 90.0 - 45.0)) * rays_steps[k][i][0] / ray_step;
//floor_ceil_pre_counted_values[k][i][j][1] = 1.0 / (ry / 2 - j) * ry / 2 / cos(radians(to_double(i) / rx * 90.0 - 45.0)) * rays_steps[k][i][1] / ray_step;
}
}
}
threads_heights = new thread[threads_num];
threads_render = new thread[threads_num];
threads_ = new thread[threads_num];
//cout << rays_steps[0][0][0] << endl;
is_render = true;
not_load = false;
}
void add_rect_sprite(RectSprite3D sprite, int index_) {
//cout << sprites_objects.size() << endl;
sprites_objects[index_] = sprite;
}
void add_flat_sprite(FlatSprite3D sprite, int index_) {
flat_sprites[index_] = sprite;
}
void add_collider(UnvisibleRectSprite3D sprite, int index_) {
unv_rect_collision[index_] = sprite;
}
void add_circle(CircleSprite3D sprite, int index_) {
circle_sprites[index_] = sprite;
}
void load_texture(int texture_code, string filename) {
sf::Image texture_img;
texture_img.loadFromFile(filename);
const sf::Uint8* pixels1 = new sf::Uint8[RESOLUTION_X * RESOLUTION_Y * 4 + 4];
sf::Uint8* pixels2 = new sf::Uint8[RESOLUTION_X * RESOLUTION_Y * 4 + 4];
pixels1 = texture_img.getPixelsPtr();
int i, j, k;
for (i = 0;i < ry; i++) {
for (j = 0; j < ry; j++) {
textures[texture_code][i][j][0] = sfUint8_to_int(pixels1[i * ry * 4 + j * 4 - 4]);
textures[texture_code][i][j][1] = sfUint8_to_int(pixels1[i * ry * 4 + j * 4 - 3]);
textures[texture_code][i][j][2] = sfUint8_to_int(pixels1[i * ry * 4 + j * 4 - 2]);
}
}
}
void set_map(string filename) {
std::string line;
std::ifstream in(filename);
int str_len, i, k;
k = 0;
if (in.is_open())
{
cout << "File with MAP is loaded" << endl;
while (getline(in, line))
{
str_len = line.length();
k++;
}
}
in.close();
mAp.resize(k, vector<int>(str_len, 0));
int k2;
k2 = k;
cout << "width: " << str_len << " height: " << k << endl;
map_width = str_len;
map_height = k;
std::ifstream in2(filename);
k = 0;
if (in2.is_open())
{
cout << "File with MAP is loaded 2" << endl;
while (getline(in2, line))
{
for (i = 0; i < str_len; i++) {
//cout << line[i] << " " << static_cast<int>(line[i]) - 48 << endl;
mAp[k][i] = map_dir[line[i]];
//cout << map[k][i];
}
str_len = line.length();
k++;
}
}
in2.close();
cout << "File with MAP is loaded 3" << endl;
}
void set_map_floor(string filename) {
std::string line;
std::ifstream in(filename);
int str_len, i, k;
k = 0;
if (in.is_open())
{
cout << "File with MAP is loaded" << endl;
while (getline(in, line))