-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathmain.cpp
More file actions
7315 lines (6879 loc) · 294 KB
/
main.cpp
File metadata and controls
7315 lines (6879 loc) · 294 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
/*
Tilemancer
Copyright (C) 2016 Lucca Pedersoli Junior
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define _USE_MATH_DEFINES
enum {
Windows = 0x1,
Apple = 0x2,
Linux = 0x4,
Unix = Apple | Linux
};
#ifdef _WIN32
#define GLEW_STATIC
#include <SDL.h>
#include <windows.h>
#include <GL\glew.h>
#include <gl\glu.h>
#include "minizip/unzip.h"
#include <SDL_image.h>
#include <SDL_mixer.h>
#include <SDL_ttf.h>
#include "mingw.mutex.h"
#include "mingw.thread.h"
#include <Shlobj.h>
#define SDL_GetGlobalMouseState SDL_GetMouseState
#define glGenFramebuffers glGenFramebuffersEXT
#define glBindFramebuffer glBindFramebufferEXT
#define glFramebufferTexture2D glFramebufferTexture2DEXT
#define GL_FRAMEBUFFER GL_FRAMEBUFFER_EXT
#define GL_COLOR_ATTACHMENT0 GL_COLOR_ATTACHMENT0_EXT
#define GL_COLOR_ATTACHMENT1 GL_COLOR_ATTACHMENT1_EXT
#define GL_COLOR_ATTACHMENT2 GL_COLOR_ATTACHMENT2_EXT
#define GL_COLOR_ATTACHMENT3 GL_COLOR_ATTACHMENT3_EXT
const char* light_vertex_shader_source = (char*)"#version 130\nin vec4 position; uniform int frame; uniform vec2 frameSize; uniform vec2 off; uniform vec2 texSize; uniform mat4 model; uniform mat4 proj; in vec2 t; out vec2 txc; void main() { gl_Position = (proj*model) * position; int tx = int(mod(frame, 5)); int ty = frame/5; txc = vec2(((t.x+tx)*frameSize.x+off.x)/texSize.x, ((t.y+ty)*frameSize.y+off.y)/texSize.y); }";
const char* light_fragment_shader_source = (char*)"#version 130\nout vec4 fragColor; uniform vec3 color; uniform vec3 color2; in vec2 txc; void main() { fragColor = vec4(color*(1.0-txc.x)+color2*txc.x, 1.0); }";
const char* blur_vertex_shader_source = (char*)"#version 130\nin vec4 position; uniform int frame; uniform vec2 frameSize; uniform vec2 texSize; uniform mat4 model; uniform mat4 proj; in vec2 t; out vec2 txc; void main() { gl_Position = (proj*model) * position; int tx = int(mod(frame, 5)); int ty = frame/5; txc = vec2((t.x+tx)*frameSize.x/texSize.x, (t.y+ty)*frameSize.y/texSize.y); }";
const char* blur_fragment_shader_source = (char*)"#version 130\n#define M_PI 3.1415926535897932384626433832795\nout vec4 fragColor; in vec2 txc; uniform sampler2D tex; uniform vec2 texSize; uniform float v; uniform bool tri; uniform bool full; void main() { float a = 0.0; vec2 dist = vec2(0.5, 0.5)-txc; a = ((length(dist)>0.5) ? 0.0 : 1.0); a = ((length(dist)<0.4) ? 0.0 : a); a = (tri ? 1.0 : a); float H = atan(dist.y, dist.x)/M_PI*180.0+180.0; H = (H==360 ? 0.0 : H); H = (tri ? v : H); float S = (tri ? txc.x/txc.y : 1.0); float V = (tri ? txc.y : 1.0); float C = V*S; float X = C*(1.0-abs(mod(H/60.0, 2)-1.0)); float m = V-C; vec3 rgb = vec3(0.0, 0.0, 0.0); if(0 <= H && H < 60) { rgb = vec3(C, X, 0.0); } else if(60 <= H && H < 120) { rgb = vec3(X, C, 0.0); } else if(120 <= H && H < 180) { rgb = vec3(0.0, C, X); } else if(180 <= H && H < 240) { rgb = vec3(0.0, X, C); } else if(240 <= H && H < 300) { rgb = vec3(X, 0.0, C); } else if(300 <= H && H < 360) { rgb = vec3(C, 0.0, X); } rgb = rgb+m; float d = 10.0; vec3 finalColor = vec3(0.0, 0.0, 0.0); for(int x = 0; x < texSize.x; x++) { for(int y = 0; y < texSize.y; y++) { vec3 color = vec3(texture2D(tex, vec2(x/texSize.x, y/texSize.y))); vec3 dist = rgb-color; float d2 = length(dist); if(d2 < d) { d = d2; finalColor = color; } } } if(full) { finalColor = rgb; } fragColor = vec4(finalColor, a); }";
const char* transition_vertex_shader_source = (char*)"#version 130\nin vec4 position; uniform int frame; uniform vec2 frameSize; uniform vec2 texSize; uniform mat4 model; uniform mat4 proj; uniform float tSize; uniform float aspect; uniform vec2 count; uniform int on; uniform vec2 resolution; uniform vec2 ij; uniform float time; in vec2 t; out vec2 txc; void main() { vec2 totalSize = vec2(tSize, tSize)*count; vec2 p = vec2(ij.x*(tSize-1.0)+tSize/2.0, ij.y*(tSize-1.0)+tSize/2.0)+(resolution-totalSize)/2.0; float dx = 5.0; float dy = 5.0/aspect; float sizeX = 1.0-((count.x-ij.x)*(1.0-on)+ij.x*on+dx-time)/dx; float sizeY = 1.0-((count.y-ij.y)*(1.0-on)+ij.y*on+dy-time/aspect)/dy; float size = clamp(sizeX/2.0+sizeY/2.0, 0.0, 1.0); gl_Position = (proj*model) * vec4((position.xy-0.5)*vec2(tSize, tSize)*size+p, position.zw); txc = t; }";
const char* transition_fragment_shader_source = (char*)"#version 130\nlayout(location = 0) out vec4 fragColor; uniform sampler2D tex; in vec2 txc; void main() { fragColor = texture2D(tex, txc); }";
const char* my_vertex_shader_source = (char*)"#version 130\nin vec4 position; uniform int frame; uniform vec2 frameSize; uniform vec2 off; uniform vec2 texSize; uniform mat4 model; uniform mat4 proj; in vec2 t; out vec2 txc; void main() { gl_Position = (proj*model) * position; int tx = int(mod(frame, 5)); int ty = frame/5; txc = vec2(((t.x+tx)*frameSize.x+off.x)/texSize.x, ((t.y+ty)*frameSize.y+off.y)/texSize.y); }";
const char* my_fragment_shader_source = (char*)"#version 130\n uniform sampler2D tex; in vec2 txc; out vec4 fragColor; uniform float alpha; void main() { fragColor = vec4(texture2D(tex, txc).xyz, texture2D(tex, txc).w*alpha); }";
int OS = Windows;
#elif defined(__APPLE__)
#include <SDL2/SDL.h>
#include <OpenGL/gl3.h>
#include "CoreFoundation/CFBundle.h"
#include <SDL2_image/SDL_image.h>
#include <SDL2_mixer/SDL_mixer.h>
#include <SDL2_ttf/SDL_ttf.h>
#include <pwd.h>
#include <future>
#include <mach-o/dyld.h>
const char* light_vertex_shader_source = (char*)"#version 330\nin vec4 position; uniform int frame; uniform vec2 frameSize; uniform vec2 off; uniform vec2 texSize; uniform mat4 model; uniform mat4 proj; in vec2 t; out vec2 txc; void main() { gl_Position = (proj*model) * position; int tx = frame%5; int ty = frame/5; txc = vec2(((t.x+tx)*frameSize.x+off.x)/texSize.x, ((t.y+ty)*frameSize.y+off.y)/texSize.y); }";
const char* light_fragment_shader_source = (char*)"#version 330\nlayout(location = 0) out vec4 fragColor; uniform vec3 color; uniform vec3 color2; in vec2 txc; void main() { fragColor = vec4(color*(1.0-txc.x)+color2*txc.x, 1.0); }";
const char* blur_vertex_shader_source = (char*)"#version 330\nin vec4 position; uniform int frame; uniform vec2 frameSize; uniform vec2 texSize; uniform mat4 model; uniform mat4 proj; in vec2 t; out vec2 txc; void main() { gl_Position = (proj*model) * position; int tx = frame%5; int ty = frame/5; txc = vec2((t.x+tx)*frameSize.x/texSize.x, (t.y+ty)*frameSize.y/texSize.y); }";
const char* blur_fragment_shader_source = (char*)"#version 330\n#define M_PI 3.1415926535897932384626433832795\nlayout(location = 0) out vec4 fragColor; in vec2 txc; uniform sampler2D tex; uniform vec2 texSize; uniform float v; uniform bool tri; uniform bool full; void main() { float a = 0.0; vec2 dist = vec2(0.5, 0.5)-txc; a = ((length(dist)>0.5) ? 0.0 : 1.0); a = ((length(dist)<0.4) ? 0.0 : a); a = (tri ? 1.0 : a); float H = atan(dist.y, dist.x)/M_PI*180.0+180.0; H = (H==360 ? 0.0 : H); H = (tri ? v : H); float S = (tri ? txc.x/txc.y : 1.0); float V = (tri ? txc.y : 1.0); float C = V*S; float X = C*(1.0-abs(mod(H/60.0, 2)-1.0)); float m = V-C; vec3 rgb = vec3(0.0, 0.0, 0.0); if(0 <= H && H < 60) { rgb = vec3(C, X, 0.0); } else if(60 <= H && H < 120) { rgb = vec3(X, C, 0.0); } else if(120 <= H && H < 180) { rgb = vec3(0.0, C, X); } else if(180 <= H && H < 240) { rgb = vec3(0.0, X, C); } else if(240 <= H && H < 300) { rgb = vec3(X, 0.0, C); } else if(300 <= H && H < 360) { rgb = vec3(C, 0.0, X); } rgb = rgb+m; float d = 10.0; vec3 finalColor = vec3(0.0, 0.0, 0.0); for(int x = 0; x < texSize.x; x++) { for(int y = 0; y < texSize.y; y++) { vec3 color = vec3(texture(tex, vec2(x/texSize.x, y/texSize.y))); vec3 dist = rgb-color; float d2 = length(dist); if(d2 < d) { d = d2; finalColor = color; } } } if(full) { finalColor = rgb; } fragColor = vec4(finalColor, a); }";
const char* transition_vertex_shader_source = (char*)"#version 330\nin vec4 position; uniform int frame; uniform vec2 frameSize; uniform vec2 texSize; uniform mat4 model; uniform mat4 proj; in vec2 t; out vec2 txc; void main() { gl_Position = (proj*model) * position; int tx = frame%5; int ty = frame/5; txc = vec2((t.x+tx)*frameSize.x/texSize.x, (t.y+ty)*frameSize.y/texSize.y); }";
const char* transition_fragment_shader_source = (char*)"#version 330\nlayout(location = 0) out vec4 fragColor; uniform float rot; uniform vec2 texSize; uniform sampler2D tex; uniform sampler2D texP; in vec2 txc; void main() { vec3 off = vec3(-1.0/texSize.x, 0.0, 1.0/texSize.x); vec2 size = vec2(2.0, 0.0); float s11 = texture(tex, txc).x; float s01 = texture(tex, txc+off.xy).x; float s21 = texture(tex, txc+off.zy).x; float s10 = texture(tex, txc+off.yx).x; float s12 = texture(tex, txc+off.yz).x; vec3 va = normalize(vec3(size.xy,s21-s01)); vec3 vb = normalize(vec3(size.yx,s12-s10)); vec3 bump = vec3(cross(va,vb)); vec3 light = vec3(sin(rot), -cos(rot), 0.3); float d = max(0.0, dot(normalize(light), bump)); vec2 ptxc = vec2(d, 0.0); vec3 colorFinal = vec3(texture(texP, ptxc)); fragColor = vec4(colorFinal, 1.0)";
const char* my_vertex_shader_source = (char*)"#version 330\nin vec4 position; uniform int frame; uniform vec2 frameSize; uniform vec2 off; uniform vec2 texSize; uniform mat4 model; uniform mat4 proj; in vec2 t; out vec2 txc; void main() { gl_Position = (proj*model) * position; int tx = frame%5; int ty = frame/5; txc = vec2(((t.x+tx)*frameSize.x+off.x)/texSize.x, ((t.y+ty)*frameSize.y+off.y)/texSize.y); }";
const char* my_fragment_shader_source = (char*)"#version 330\nlayout(location = 0) out vec4 fragColor; layout(location = 1) out vec4 fragColorN; layout(location = 2) out vec4 fragColorMisc; layout(location = 3) out vec4 fragColorB; uniform vec2 flip; uniform sampler2D tex; uniform float strength; uniform float alpha; uniform float depth; uniform sampler2D texN; in vec2 txc; void main() { fragColor = vec4(texture(tex, txc).xyz/texture(tex, txc).w, texture(tex, txc).w*alpha); }";
CFBundleRef mainBundle;
int OS = Apple;
#elif defined(__linux__)
int OS = Linux;
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_mixer.h>
#include <SDL2/SDL_ttf.h>
#include <pwd.h>
#include <future>
const char* light_vertex_shader_source = (char*)"#version 330\nin vec4 position; uniform int frame; uniform vec2 frameSize; uniform vec2 off; uniform vec2 texSize; uniform mat4 model; uniform mat4 proj; in vec2 t; out vec2 txc; void main() { gl_Position = (proj*model) * position; int tx = frame%5; int ty = frame/5; txc = vec2(((t.x+tx)*frameSize.x+off.x)/texSize.x, ((t.y+ty)*frameSize.y+off.y)/texSize.y); }";
const char* light_fragment_shader_source = (char*)"#version 330\nlayout(location = 0) out vec4 fragColor; uniform vec3 color; uniform vec3 color2; in vec2 txc; void main() { fragColor = vec4(color*(1.0-txc.x)+color2*txc.x, 1.0); }";
const char* blur_vertex_shader_source = (char*)"#version 330\nin vec4 position; uniform int frame; uniform vec2 frameSize; uniform vec2 texSize; uniform mat4 model; uniform mat4 proj; in vec2 t; out vec2 txc; void main() { gl_Position = (proj*model) * position; int tx = frame%5; int ty = frame/5; txc = vec2((t.x+tx)*frameSize.x/texSize.x, (t.y+ty)*frameSize.y/texSize.y); }";
const char* blur_fragment_shader_source = (char*)"#version 330\n#define M_PI 3.1415926535897932384626433832795\nlayout(location = 0) out vec4 fragColor; in vec2 txc; uniform sampler2D tex; uniform vec2 texSize; uniform float v; uniform bool tri; uniform bool full; void main() { float a = 0.0; vec2 dist = vec2(0.5, 0.5)-txc; a = ((length(dist)>0.5) ? 0.0 : 1.0); a = ((length(dist)<0.4) ? 0.0 : a); a = (tri ? 1.0 : a); float H = atan(dist.y, dist.x)/M_PI*180.0+180.0; H = (H==360 ? 0.0 : H); H = (tri ? v : H); float S = (tri ? txc.x/txc.y : 1.0); float V = (tri ? txc.y : 1.0); float C = V*S; float X = C*(1.0-abs(mod(H/60.0, 2)-1.0)); float m = V-C; vec3 rgb = vec3(0.0, 0.0, 0.0); if(0 <= H && H < 60) { rgb = vec3(C, X, 0.0); } else if(60 <= H && H < 120) { rgb = vec3(X, C, 0.0); } else if(120 <= H && H < 180) { rgb = vec3(0.0, C, X); } else if(180 <= H && H < 240) { rgb = vec3(0.0, X, C); } else if(240 <= H && H < 300) { rgb = vec3(X, 0.0, C); } else if(300 <= H && H < 360) { rgb = vec3(C, 0.0, X); } rgb = rgb+m; float d = 10.0; vec3 finalColor = vec3(0.0, 0.0, 0.0); for(int x = 0; x < texSize.x; x++) { for(int y = 0; y < texSize.y; y++) { vec3 color = vec3(texture(tex, vec2(x/texSize.x, y/texSize.y))); vec3 dist = rgb-color; float d2 = length(dist); if(d2 < d) { d = d2; finalColor = color; } } } if(full) { finalColor = rgb; } fragColor = vec4(finalColor, a); }";
const char* transition_vertex_shader_source = (char*)"#version 330\nin vec4 position; uniform int frame; uniform vec2 frameSize; uniform vec2 texSize; uniform mat4 model; uniform mat4 proj; in vec2 t; out vec2 txc; void main() { gl_Position = (proj*model) * position; int tx = frame%5; int ty = frame/5; txc = vec2((t.x+tx)*frameSize.x/texSize.x, (t.y+ty)*frameSize.y/texSize.y); }";
const char* transition_fragment_shader_source = (char*)"#version 330\nlayout(location = 0) out vec4 fragColor; uniform float rot; uniform vec2 texSize; uniform sampler2D tex; uniform sampler2D texP; in vec2 txc; void main() { vec3 off = vec3(-1.0/texSize.x, 0.0, 1.0/texSize.x); vec2 size = vec2(2.0, 0.0); float s11 = texture(tex, txc).x; float s01 = texture(tex, txc+off.xy).x; float s21 = texture(tex, txc+off.zy).x; float s10 = texture(tex, txc+off.yx).x; float s12 = texture(tex, txc+off.yz).x; vec3 va = normalize(vec3(size.xy,s21-s01)); vec3 vb = normalize(vec3(size.yx,s12-s10)); vec3 bump = vec3(cross(va,vb)); vec3 light = vec3(sin(rot), -cos(rot), 0.3); float d = max(0.0, dot(normalize(light), bump)); vec2 ptxc = vec2(d, 0.0); vec3 colorFinal = vec3(texture(texP, ptxc)); fragColor = vec4(colorFinal, 1.0)";
const char* my_vertex_shader_source = (char*)"#version 330\nin vec4 position; uniform int frame; uniform vec2 frameSize; uniform vec2 off; uniform vec2 texSize; uniform mat4 model; uniform mat4 proj; in vec2 t; out vec2 txc; void main() { gl_Position = (proj*model) * position; int tx = frame%5; int ty = frame/5; txc = vec2(((t.x+tx)*frameSize.x+off.x)/texSize.x, ((t.y+ty)*frameSize.y+off.y)/texSize.y); }";
const char* my_fragment_shader_source = (char*)"#version 330\nlayout(location = 0) out vec4 fragColor; layout(location = 1) out vec4 fragColorN; layout(location = 2) out vec4 fragColorMisc; layout(location = 3) out vec4 fragColorB; uniform vec2 flip; uniform sampler2D tex; uniform float strength; uniform float alpha; uniform float depth; uniform sampler2D texN; in vec2 txc; void main() { fragColor = vec4(texture(tex, txc).xyz/texture(tex, txc).w, texture(tex, txc).w*alpha); }";
#else
int OS = -1;
#endif
#include "glm.hpp"
#include "gtc/type_ptr.hpp"
#include "gtx/transform.hpp"
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "lua.hpp"
#include <unistd.h>
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <sstream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <fstream>
using namespace std;
const int Vmajor = 0;
const int Vminor = 2;
const int Vrevision = 1;
SDL_Window* window = NULL;
SDL_GLContext gContext = NULL;
SDL_Renderer* gRenderer = NULL;
class File {
public:
File(string n, bool f);
~File();
string name;
bool folder;
};
class bPoint {
public:
bPoint();
bPoint(float x, float y);
float x;
float y;
};
class cBezier {
public:
cBezier();
bPoint getTangent(float t);
bPoint getPoint(float t);
void create();
void update();
int d = 100;
GLuint bVbo;
bPoint P0;
bPoint P1;
bPoint P2;
bPoint P3;
};
class Layer;
class CPoint;
class Socket;
class nEffect;
class Parameter {
public:
Parameter(int ID, string name, float x, float y, float w, float h, int value, int value2, int value3, string tt);
~Parameter();
void render(int ex, int ey);
void mouseDown(int mx, int my, int ex, int ey, int layer, nEffect* fx);
void mouseMove(int mx, int my, int ex, int ey, int layer, nEffect* fx);
void mouseUp(int mx, int my, int ex, int ey, int layer, nEffect* fx);
int ID;
string tt;
string name;
Layer* ref;
Socket* s;
vector<CPoint*> points;
Parameter* p;
int index;
int selectedPoint;
float x;
float y;
float w;
float h;
float resetValue;
string typing;
float value;
float value2;
float value3;
float value4;
bool dragging;
bool dragging2;
float oldmX;
float oldmY;
float initmX;
float initmY;
};
class Color {
public:
Color(int r, int g, int b);
~Color();
bool equals(Color*);
int r;
int g;
int b;
int a;
bool disabled;
};
class CPoint {
public:
CPoint();
float r;
float g;
float b;
int i;
float a;
};
class Texture;
class Socket {
public:
Socket();
~Socket();
float y;
bool snapped;
bool infloop;
Socket* s;
cBezier* b;
int index;
float px;
float py;
int futureN;
int futureS;
nEffect* parent;
vector<float> texData;
GLuint texture;
string lastTexDir;
string lastTexName;
};
class Text {
public:
string name;
float y;
};
class nEffect {
public:
nEffect(string luafn, string fxname, bool preset = false);
~nEffect();
lua_State *L;
unsigned int seed;
std::thread side;
void sideUpdate();
void clearTree();
vector<Socket*> inputs;
vector<Socket*> outputs;
vector<Parameter*> params;
vector<Text*> texts;
vector<nEffect*> presetFxs;
string luafn;
string fxname;
string name;
string desc;
int nextP;
bool cleared;
bool presetError;
bool abort;
bool deleted;
bool loading;
bool done;
bool undone;
int doneTimer;
bool loaded;
bool isPreset;
float x;
float y;
float sx;
float sy;
float w;
float h;
float r;
int setName(lua_State *L);
int setDesc(lua_State *L);
int setSize(lua_State *L);
int addInput(lua_State *L);
int addParameter(lua_State *L);
int addInputParameter(lua_State *L);
int addOutput(lua_State *L);
int getTileSize(lua_State *L);
int getValue(lua_State *L);
int setPixel(lua_State *L);
int addCRamp(lua_State *L);
int finalize(lua_State *L);
int luaSeed(lua_State *L);
int luaRand(lua_State *L);
int addNode(lua_State *L);
int setParameter(lua_State *L);
int addConnection(lua_State *L);
void AbortLua(lua_State* L, lua_Debug* ar);
};
class Texture {
public:
~Texture();
std::thread side;
void genTex(bool first = true);
void sideUpdate();
bool loading;
bool done;
int doneTimer;
bool loaded;
bool abort;
vector<float> texData;
vector<nEffect*> fxs;
GLuint texture;
string lastTexDir;
string lastTexName;
};
static double dt = 1.0 / 60.0;
static double currentTime;
static double lastTime;
static double accumulator;
static unsigned int fps;
static int fpsTimer;
static TTF_Font* font;
static float texSizeX;
static float texSizeY;
static float texType;
static float pixelSize;
static int screenW;
static int screenH;
static int displayW;
static int displayH;
static int screenPW;
static int screenPH;
static float camX;
static float camY;
static float nodeCSX;
static float nodeCSY;
static float nodeCX;
static float nodeCY;
static bool camMoving = false;
static bool draggingCam = false;
static bool draggingCam2 = false;
static float camOffset;
static float camOffsetSpeed;
static float camOffsetMagnitude;
static bool camPositive;
static float camRot;
static bool camFixed;
static int camXFinal;
static int camYFinal;
//static int selectedTile = 32;
static int zoom = 1;
static int zoomA = 1;
static float tilesW;
static float tilesH;
static bool draggingLayer;
static int draggedLayer;
static int draggedFX;
static bool draggingNode;
static int draggedNode;
static bool draggingSocket;
static bool cancelDrag;
static int draggedSocket;
static int targetDrag;
static float screenScale = 2.0;
static int logoTimer;
static int logoTimerMax = 150;
static bool logoType;
static string toolTip;
static string errorMessage;
static int errorTimer;
static vector<float> data;
static vector<Color*> palette;
static Color rampA(254, 237, 186);
static Color rampB(54, 42, 96);
static int selectedColor = 0;
static vector<Parameter*> colorParams;
static Parameter* textType;
static nEffect* textTypeFx;
static int textTypeLayer;
static Parameter* textTypeTemp;
static nEffect* textTypeFxTemp;
static int textTypeLayerTemp;
static int blinkTimer;
static bool RoC = true;
static vector<Texture*> texs;
static vector<nEffect*> newEffects;
static int collH = 18;
static int expH = 92;
static int expandedLayer;
static bool layerExpanded;
static int currentTexture;
static Socket* currentSocket;
static float barX;
static float barXRight;
static float barY;
static float barY2;
static float barY3;
static float barY4;
static float barY5;
static float layersScroll;
static float toolsScroll;
static float palScroll;
static float texScroll;
static float newEScroll;
static float layersScrollH;
static bool scrollDir;
static bool scrollSet;
static float prevLayersScroll;
static float browserScroll;
static int selectedFX;
static bool draggingParam;
static bool draggingFX;
static bool draggingNew;
static bool draggingUI;
static bool draggingSBar;
static int sBarDrag;
static float sRatio;
static int rotTimer;
static int UIdragType;
static int UIdragRange = 5;
static float mouseOX;
static float mouseOY;
static float mouseX;
static float mouseY;
static bool gameStarted;
static int view = 0;
static int doubleClickTimer;
static int typeTimer;
static float mouseSX;
static float mouseSY;
static float mouseSDX;
static float mouseSDY;
static float prevC;
static float prevCount;
static Socket* preview;
static int previewTimer;
static vector<string> listUndo;
static vector<string> listRedo;
static string currentDir;
static string filenameB;
static bool browserOpen;
static int browserMode;
static vector<File*> filenames;
static vector<string> fnUndo;
static vector<string> fnRedo;
static string lastPalDir;
static string lastPalName;
static string lastSaveDir;
static string lastSaveName;
static string lastTexDir;
static string lastTexName;
static bool overwrite;
static int selectedFile;
static GLuint gridImg;
static GLuint effectImg;
static GLuint effectImg2;
static GLuint effectImg3;
static GLuint effectImg4;
static GLuint effectImg5;
static GLuint effectImg6;
static GLuint effectImg7;
static GLuint effectImg8;
static GLuint effectImg9;
static GLuint effectImg10;
static GLuint effectImg11;
static GLuint effectImg12;
static GLuint effectImg13;
static GLuint effectImg14;
static GLuint effectImg15;
static GLuint effectImg16;
static GLuint effectImg17;
static GLuint bezierFill;
static GLuint bezierFillError;
static GLuint iconImg0;
static GLuint iconImg1;
static GLuint iconImg2;
static GLuint iconImg3;
static GLuint iconImg4;
static GLuint iconImg5;
static GLuint iconImg6;
static GLuint iconImg7;
static GLuint iconImg8;
static GLuint iconImg9;
static GLuint iconImg10;
static GLuint iconImg11;
static GLuint iconImg12;
static GLuint iconImg13;
static GLuint palImg;
static GLuint palImgReal;
static GLuint postImg;
static GLuint texImgFinal;
static GLuint fontImg;
static GLuint fontImg2;
static GLuint fontImg3;
static GLenum my_program;
static GLenum my_vertex_shader;
static GLenum my_fragment_shader;
static GLenum light_program;
static GLenum light_vertex_shader;
static GLenum light_fragment_shader;
static GLenum blur_program;
static GLenum blur_vertex_shader;
static GLenum blur_fragment_shader;
static GLenum transition_program;
static GLenum transition_vertex_shader;
static GLenum transition_fragment_shader;
static GLuint screenFbo;
static GLuint screenTex;
static GLuint screenTexN;
static GLuint screenTexB;
static GLuint screenTexMisc;
static GLuint screenFboFinal;
static GLuint screenTexFinal;
static GLuint screenFboBloom;
static GLuint screenTexBloom;
static GLuint screenFboBloom2;
static GLuint screenTexBloom2;
static GLuint screenFboFinal2;
static GLuint screenTexFinal2;
static GLuint vbo;
static GLuint vboTri;
static GLuint vao;
static GLuint lightTex;
static GLuint foregroundTex;
static GLuint hiddenImg;
static GLuint hidden2Img;
static GLuint digitsImg;
static GLuint brightnessImg;
static GLuint ditherTex;
static GLuint noTex;
static GLuint vignetteImg;
static GLuint logoImage;
static GLuint titleImage;
static GLuint thanksImage;
static GLuint flashImg;
static GLint mp_tex;
static GLint mp_texN;
static GLint mp_frameSize;
static GLint mp_texSize;
static GLint mp_off;
static GLint mp_frame;
static GLint mp_depth;
static GLint mp_alpha;
static GLint mp_strength;
static GLint mp_model;
static GLint mp_flip;
static GLint mp_proj;
static lua_State *L;
static glm::mat4 model = glm::mat4(1.0);
static glm::mat4 proj = glm::mat4(1.0);
void renderSprite2(int frame, float x, float y, float w, float h, GLuint tex, float frameW, float frameH, float rot, float centerX, float centerY, float alpha, GLuint texN, float depth, float strength, bool flipX, bool flipY, float offX = 0, float offY = 0, int cutoff = -1, int cutoff2 = -1, int cutoff3 = -1, int cutoff4 = -1);
void renderSprite(int frame, float x, float y, float w, float h, GLuint tex, float frameW, float frameH, float rot, float centerX, float centerY, float alpha, GLuint texN, float depth, float strength, bool flipX, bool flipY, float offX = 0, float offY = 0, int cutoff = -1, int cutoff2 = -1, int cutoff3 = -1, int cutoff4 = -1);
#ifdef _WIN32
GLuint loadTexture(string path) {
GLuint tex;
unzFile data = unzOpen("data.lpf");
unz_file_info info;
Uint8* buffer;
SDL_RWops* rw = NULL;
SDL_Surface* ls = NULL;
unzLocateFile(data, path.c_str(), NULL);
unzOpenCurrentFile(data);
unzGetCurrentFileInfo(data, &info, NULL, 0, NULL, 0, NULL, 0);
buffer = (Uint8*)malloc(info.uncompressed_size);
unzReadCurrentFile(data, buffer, info.uncompressed_size);
rw = SDL_RWFromConstMem(buffer, info.uncompressed_size);
ls = IMG_LoadPNG_RW(rw);
free(buffer);
if(ls != NULL) {
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
int mode = GL_RGB;
int mode2 = GL_BGR;
if(ls->format->BytesPerPixel == 4) {
mode = GL_RGBA;
mode2 = GL_BGRA;
}
glTexImage2D(GL_TEXTURE_2D, 0, mode, ls->w, ls->h, 0, mode, GL_UNSIGNED_BYTE, ls->pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
//SDL_FreeSurface(ls);
} else {
//cout << "Failed to load image: "+path << endl;
}
return tex;
}
GLuint loadTexture2(string path) {
GLuint tex;
SDL_Surface* ls = IMG_Load(path.c_str());
if(ls != NULL) {
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
int mode = GL_RGB;
int mode2 = GL_BGR;
if(ls->format->BytesPerPixel == 4) {
mode = GL_RGBA;
mode2 = GL_BGRA;
}
glTexImage2D(GL_TEXTURE_2D, 0, mode, ls->w, ls->h, 0, mode, GL_UNSIGNED_BYTE, ls->pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
//SDL_FreeSurface(ls);
} else {
//cout << "Failed to load image: "+path << endl;
}
return tex;
}
#elif defined(__APPLE__) || defined(__linux__)
GLuint loadTexture(string path) {
GLuint tex;
SDL_Surface* ls = IMG_Load(path.c_str());
if(ls != NULL) {
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
int mode = GL_RGB;
int mode2 = GL_BGR;
if(ls->format->BytesPerPixel == 4) {
mode = GL_RGBA;
mode2 = GL_BGRA;
}
glTexImage2D(GL_TEXTURE_2D, 0, mode, ls->w, ls->h, 0, mode2, GL_UNSIGNED_BYTE, ls->pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
//SDL_FreeSurface(ls);
} else {
//cout << "Failed to load image: "+path << endl;
}
return tex;
}
GLuint loadTexture2(string path) {
return NULL;
}
#else
#endif
void setPixel(vector<float> & data, int x, int y, Color* color, bool wrap) {
bool render = true;
if(x >= texSizeX) {
if(wrap) {
while(x >= texSizeX) {
x -= texSizeX;
}
} else {
render = false;
}
} else if(x < 0) {
if(wrap) {
while(x < 0) {
x += texSizeX;
}
} else {
render = false;
}
}
if(y >= texSizeY) {
if(wrap) {
while(y >= texSizeY) {
y -= texSizeY;
}
} else {
render = false;
}
} else if(y < 0) {
if(wrap) {
while(y < 0) {
y += texSizeY;
}
} else {
render = false;
}
}
if(render && data.size() > (y*texSizeX+x)*4+3 && color != NULL) {
data[(y*texSizeX+x)*4+0] = color->r;
data[(y*texSizeX+x)*4+1] = color->g;
data[(y*texSizeX+x)*4+2] = color->b;
data[(y*texSizeX+x)*4+3] = color->a;
}
}
Color getColor(vector<float> & data, int x, int y, bool wrap) {
Color c = Color(0, 0, 0);
bool render = true;
if(x >= texSizeX) {
if(wrap) {
while(x >= texSizeX) {
x -= texSizeX;
}
} else {
render = false;
}
} else if(x < 0) {
if(wrap) {
while(x < 0) {
x += texSizeX;
}
} else {
render = false;
}
}
if(y >= texSizeY) {
if(wrap) {
while(y >= texSizeY) {
y -= texSizeY;
}
} else {
render = false;
}
} else if(y < 0) {
if(wrap) {
while(y < 0) {
y += texSizeY;
}
} else {
render = false;
}
}
if(render && data.size() > (y*texSizeX+x)*4+3) {
c.r = data[(y*texSizeX+x)*4+0];
c.g = data[(y*texSizeX+x)*4+1];
c.b = data[(y*texSizeX+x)*4+2];
c.a = data[(y*texSizeX+x)*4+3];
}
return c;
}
void saveUndo();
Parameter::~Parameter() {
}
Color::~Color() {
}
File::~File() {
}
Texture::~Texture() {
}
void Texture::sideUpdate() {
}
void nEffect::sideUpdate() {
luaL_dofile(this->L, luafn.c_str());
lua_settop(this->L, 0);
lua_getglobal(this->L, "apply");
lua_pcall(this->L, 0, 0, 0);
loaded = true;
}
void nEffect::clearTree() {
cleared = true;
done = false;
doneTimer = 0;
undone = false;
for(int b = 0; b < outputs.size(); b++) {
outputs.at(b)->texData.clear();
for(int x = 0; x < texSizeX; x++) {
for(int y = 0; y < texSizeY; y++) {
for(int c = 0; c < 4; c++) {
outputs.at(b)->texData.push_back(0);
}
}
}
}
for(int i = 0; i < texs.at(currentTexture)->fxs.size(); i++) {
nEffect* fx = texs.at(currentTexture)->fxs.at(i);
for(int in = 0; in < fx->inputs.size(); in++) {
Socket* input = fx->inputs.at(in);
if(input->s != NULL && input->s->parent == this) {
if(fx->cleared) {
input->infloop = true;
} else {
input->infloop = false;
fx->clearTree();
}
}
}
}
cleared = false;
}
void Texture::genTex(bool first) {
if(first) { //abort all before proceeding
abort = true;
for(int i = 0; i < fxs.size(); i++) {
if(fxs.at(i)->loading) {
fxs.at(i)->abort = true;
}
}
} else {
for(int i = 0; i < fxs.size(); i++) {
if(!fxs.at(i)->done) {
bool ready = true;
for(int b = 0; b < fxs.at(i)->inputs.size(); b++) {
if(fxs.at(i)->inputs.at(b)->s != NULL) {
if(!fxs.at(i)->inputs.at(b)->s->parent->done) {
ready = false;
}
}
}
if(ready) {
if(!fxs.at(i)->loading) {
fxs.at(i)->loading = true;
fxs.at(i)->side = std::thread(&nEffect::sideUpdate, fxs.at(i));
}
}
}
}
}
}
Socket::Socket() {
futureN = -1;
futureS = -1;
index = 0;
infloop = false;
s = NULL;
b = new cBezier();
b->create();
}
void exportTexSingle(string dir) {
SDL_Surface *surface = SDL_CreateRGBSurface(SDL_SWSURFACE, texSizeX, texSizeY, 32, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000);
glBindTexture(GL_TEXTURE_2D, currentSocket->texture);
GLfloat *pixels = new GLfloat[int(texSizeX*texSizeY*4)];
if(OS & Windows) {
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels);
} else if(OS & Unix) {
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, pixels);
for(int i = 0; i < texSizeX*texSizeY*4; i++) {
Uint8 *p = (Uint8 *)surface->pixels+i*surface->format->BytesPerPixel/4;
int val = pixels[i];
if(val > 255) {
val = 255;
} else if(val < 0) {
val = 0;
}
p[0] = (Uint8)val;
}
}
IMG_SavePNG(surface, dir.c_str());
SDL_FreeSurface(surface);
}
void exportPalette(string dir) {
SDL_Surface *surface = SDL_CreateRGBSurface(SDL_SWSURFACE, 16, 16, 24, 0x000000FF, 0x0000FF00, 0x00FF0000, 0);
glBindTexture(GL_TEXTURE_2D, palImgReal);
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, surface->pixels);
IMG_SavePNG(surface, dir.c_str());
SDL_FreeSurface(surface);
}
Color HSVtoRGB(float H, float S, float V) {
V /= 100;
S /= 100;
float C = V*S;
float H2 = H/60.0;
float X = C*(1.0-abs(fmod(H2, 2)-1.0));
float R, G, B;
if(0 <= H2 && H2 < 1) {
R = C;
G = X;
B = 0;
} else if(1 <= H2 && H2 < 2) {
R = X;
G = C;
B = 0;
} else if(2 <= H2 && H2 < 3) {
R = 0;
G = C;
B = X;
} else if(3 <= H2 && H2 < 4) {
R = 0;
G = X;
B = C;
} else if(4 <= H2 && H2 < 5) {
R = X;
G = 0;
B = C;
} else if(5 <= H2 && H2 < 6) {
R = C;
G = 0;
B = X;
}
float m = V-C;
R += m;
G += m;
B += m;
R *= 255;
G *= 255;
B *= 255;
Color rgb = Color(R, G, B);
return rgb;
}
Color RGBtoHSV(float R, float G, float B) {
float R1 = R/255.0;
float G1 = G/255.0;
float B1 = B/255.0;
float cMax = max(max(R1, G1), B1);
float cMin = min(min(R1, G1), B1);
float delta = cMax-cMin;
float H;
if(delta == 0) {
H = 0;
} else if(cMax == R1) {
H = 60*fmod(((G1-B1)/delta), 6);
} else if(cMax == G1) {
H = 60*((B1-R1)/delta+2);
} else if(cMax == B1) {
H = 60*((R1-G1)/delta+4);
}
while(H > 360) {
H -= 360;
}
while(H < 0) {
H += 360;
}
float S;
if(cMax == 0) {
S = 0;
} else {
S = delta/cMax;
}
S *= 100;
float V = cMax*100;
Color hsv = Color(H, S, V);
return hsv;
}
Color RGBtoYUV(float R, float G, float B) {
float Y = (( 0.299 * R + 0.587 * G + 0.114 * B) * 219 / 255) + 16;
float U = ((-0.299 * R - 0.587 * G + 0.886 * B) * 224 / 1.772 / 255) + 128;
float V = (( 0.701 * R - 0.587 * G - 0.114 * B) * 224 / 1.402 / 255) + 128;
Color yuv = Color(Y, U, V);
return yuv;
}