-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.c
2517 lines (2389 loc) · 115 KB
/
main.c
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
/*
--------------------------------------------------
James William Fletcher (github.com/mrbid)
July 2023
--------------------------------------------------
C & SDL / OpenGL ES2 / GLSL ES
Colour Converter: https://www.easyrgb.com
VoxelPainter
https://github.com/mrbid/voxelpaint
*/
#pragma GCC diagnostic ignored "-Wtrigraphs"
const unsigned char icon[] = { // 16, 16, 4
"\377\377\377\000\377\377\377\000\377\377\377\000\377\377\377\000\377\377\377\000\377"
"\377\377\001\377\377\377\032\346\346\346k\346\346\346y\377\377\377%\377\377"
"\377\001\377\377\377\000\377\377\377\000\377\377\377\000\377\377\377\000\377\377\377"
"\000\377\377\377\000\377\377\377\000\377\377\377\000\377\377\377\000\377\377\377\002"
"\377\377\377%\336\336\336\215fff\356RRR\367\326\326\326\237\377\377\377,"
"\377\377\377\002\377\377\377\000\377\377\377\000\377\377\377\000\377\377\377\000\377"
"\377\377\000\377\377\377\000\377\377\377\000\377\377\377\003\377\377\377-\326\326"
"\326\240RRR\367\000JJ\377\000RR\377JJJ\371\314\314\314\247\377\377\377.\377\377"
"\377\003\377\377\377\000\377\377\377\000\377\377\377\000\377\377\377\000\377\377\377"
"\000\377\377\377\004\377\377\377\070\314\314\314\255BBB\372\000JJ\377\000\214\214"
"\377\000\231\231\377\000RR\377<AA\372\314\314\314\255\377\377\377\066\377\377"
"\377\004\377\377\377\000\377\377\377\000\377\377\377\000\377\377\377\005\377\377\377"
"A\275\275\275\273:::\374\000\063\063\377\000\231\231\377\000\314\314\377\000\326\326"
"\377\000\231\231\377\000BB\377:::\374\275\275\275\267\377\377\377=\377\377\377"
"\004\377\377\377\000\377\377\377\000\377\377\377#\275\275\275\267\040..\376\000\031"
"\031\377\000JJ\377\000\326\326\377\000\367\367\377\000\367\367\377\000\336\336\377\000"
"ff\377\000\031\031\377(\061\061\375\275\275\275\263\377\377\377%\377\377\377\000"
"\377\377\377\001\377\377\377M{{{\353\000!!\377\000))\377\000\265\265\377\000\367\367"
"\377\000\377\377\377\000\377\377\377\000\377\377\377\000\275\275\377\000\063\063\377"
"\000!!\377sss\354\377\377\377Q\377\377\377\002\377\377\377\007\357\357\357v<AA"
"\372\000JJ\377\000\255\255\377\000\357\357\377\000\377\377\377\000\377\377\377\000\377"
"\377\377\000\377\377\377\000\367\367\377\000\255\255\377\000JJ\377:::\373\346\346"
"\346{\377\377\377\007\377\377\377\020\326\326\326\234\026\060\060\377\000\204\204"
"\377\000\326\326\377\000\377\377\377\000\377\377\377\000\377\377\377\000\377\377\377"
"\000\377\377\377\000\377\377\377\000\326\326\377\000\214\214\377\017++\377\326\326"
"\326\236\377\377\377\021\377\377\377%\255\255\255\306\000JJ\377\000\245\245\377"
"\000\336\336\377\000\357\357\377\000\377\377\377\000\377\377\377\000\377\377\377\000"
"\377\377\377\000\357\357\377\000\336\336\377\000\255\255\377\000JJ\377\255\255\255"
"\311\377\377\377'\377\377\377A\204\204\204\345\000ss\377\000\245\245\377\000\275"
"\275\377\000\314\314\377\000\346\346\377\000\265\265\377\000\245\245\377\000\346\346"
"\377\000\326\326\377\000\275\275\377\000\245\245\377\000ss\377\204\204\204\346\377"
"\377\377D\377\377\377D{{{\345\000ZZ\377\000ff\377\000\214\214\377\000\245\245\377"
"\000\214\214\377\000JJ\377\000JJ\377\000\214\214\377\000\245\245\377\000\214\214\377"
"\000ff\377\000ZZ\377sss\353\377\377\377J\377\377\377\026\357\357\357p\245\245"
"\245\317JJJ\372\000JJ\377\000RR\377\000ff\377\000ZZ\377\000ZZ\377\000ff\377\000RR\377\000"
"JJ\377<AA\374\231\231\231\330\346\346\346\203\377\377\377\035\377\377\377"
"\000\377\377\377\012\377\377\377\067\346\346\346\210\231\231\231\331\063\063\063"
"\374\000!!\377\000!!\377\000!!\377\000!!\377\063\063\063\375\231\231\231\335\336\336"
"\336\223\377\377\377A\377\377\377\020\377\377\377\001\377\377\377\000\377\377"
"\377\000\377\377\377\001\377\377\377\017\377\377\377A\336\336\336\227\214\214"
"\214\343\040((\376\040..\375\214\214\214\342\336\336\336\231\377\377\377E\377"
"\377\377\022\377\377\377\001\377\377\377\000\377\377\377\000\377\377\377\000\377\377"
"\377\000\377\377\377\000\377\377\377\000\377\377\377\002\377\377\377\024\377\377\377"
"O\314\314\314\232\326\326\326\230\377\377\377M\377\377\377\025\377\377\377"
"\002\377\377\377\000\377\377\377\000\377\377\377\000\377\377\377\000"
};
//#define SKYBLUE
//#define NO_COMPRESSION
//#define VERBOSE
#include <time.h>
#ifndef NO_COMPRESSION
#include <zlib.h>
#endif
#include <SDL2/SDL.h>
#include <SDL2/SDL_opengles2.h>
#ifdef __linux__
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <locale.h>
#endif
#include "inc/esVoxel.h"
#define uint GLuint
#define sint GLint
#define uchar unsigned char
#define forceinline __attribute__((always_inline)) inline
//*************************************
// globals
//*************************************
const char appTitle[] = "Voxel Paint Pro";
const char appVersion[] = "v1.2";
char *basedir, *appdir;
SDL_Window* wnd;
SDL_GLContext glc;
SDL_Surface* s_icon = NULL;
int winw = 1024, winh = 768;
int winw2 = 512, winh2 = 384;
float ww, wh;
float aspect, t = 0.f;
uint maxed=0,size=0,dsx=0,dsy=0;
int mx=0, my=0, xd=0, yd=0, md=0;
uint g_fps = 0;
uint ks[10] = {0}; // keystate
uint focus_mouse = 0; // mouse lock
float ddist = 4096.f; // view distance
float ddist2 = 4096.f*4096.f;
vec ipp; // inverse player position
vec look_dir; // camera look direction
int lray = 0; // pointed at node index
float ptt = 0.f; // place timing trigger (for repeat place)
float dtt = 0.f; // delete timing trigger (for repeat delete)
float rtt = 0.f; // replace timing trigger (for repeat replace)
float rrsp = 0.3f; // repeat replace speed
uint fks = 0; // F-Key state (fast mode toggle)
uint ise = 0; // is selecting
uint itf = 0; // is transforming (rotating)
vec sp1d, sp1, sp2, sdif, sdifo, sp1o; // selecting vars
float bigc = 0.f; // big cursor start time
//*************************************
// utility functions
//*************************************
void timestamp(char* ts){const time_t tt = time(0);strftime(ts, 16, "%H:%M:%S", localtime(&tt));}
forceinline float fTime(){return ((float)SDL_GetTicks())*0.001f;}
#ifdef __linux__
uint64_t microtime()
{
struct timeval tv;
struct timezone tz;
memset(&tz, 0, sizeof(struct timezone));
gettimeofday(&tv, &tz);
return 1000000 * tv.tv_sec + tv.tv_usec;
}
uint dirExist(const char* dir)
{
struct stat st;
return (stat(dir, &st) == 0 && S_ISDIR(st.st_mode));
}
uint fileExist(const char* file)
{
struct stat st;
return (stat(file, &st) == 0);
}
#endif
static SDL_HitTestResult SDLCALL hitTest(SDL_Window *window, const SDL_Point *pt, void *data)
{
if(focus_mouse == 1){return SDL_HITTEST_NORMAL;}
if( SDL_PointInRect(pt, &(SDL_Rect){40, 0, winw2-85, 22}) == SDL_TRUE ||
SDL_PointInRect(pt, &(SDL_Rect){winw2+80, 0, winw2-122, 22}) == SDL_TRUE)
return SDL_HITTEST_DRAGGABLE;
return SDL_HITTEST_NORMAL;
}
//*************************************
// game state functions
//*************************************
// game data (for fast save and load)
#define max_voxels 4194304 // 4.2 million
#define header_size 68
#define max_data_size 67108932 // (max_voxels*sizeof(vec)) + header_size
typedef struct
{
vec pp; // player position
vec pb; // place block pos
float sens; // mouse sensitivity
float xrot; // camera x-axis rotation
float yrot; // camera y-axis rotation
float st; // selected color
float ms; // player move speed
float cms; // custom move speed (high)
float lms; // custom move speed (low)
uchar grav; // gravity on/off toggle
uchar plock;// pitchlock on/off toggle
uchar r1, r2;
uint num_voxels;
vec voxels[max_voxels]; // x,y,z,w (w = color_id)
}
game_state;
game_state g; // ~67mb
int swapfunc(const void *a, const void *b)
{
const float aw = ((vec*)a)->w;
const float bw = ((vec*)b)->w;
if(aw == bw){return 0;}
else if(aw == -1.f){return 1;}
return 0;
}
void optimState()
{
qsort(g.voxels, g.num_voxels, sizeof(vec), swapfunc);
for(uint i = 0; i < g.num_voxels; i++)
{
if(g.voxels[i].w == -1.f)
{
g.num_voxels = i;
break;
}
}
}
void defaultState(const uint type)
{
g.sens = 0.003f;
g.xrot = 0.f;
g.yrot = 1.5f;
g.pp = (vec){0.f, 4.f, 0.f};
if(type == 0)
{
g.ms = 37.2f;
}
g.st = 18.f;
g.pb = (vec){0.f, 0.f, 0.f, -1.f};
if(type == 0)
{
g.lms = 37.2f;
g.cms = 74.4f;
}
g.grav = 0;
g.plock = 0;
if(g.num_voxels == 0){g.num_voxels = 256;}
}
uint loadUncompressedState()
{
#ifdef __linux__
setlocale(LC_NUMERIC, "");
const uint64_t st = microtime();
#endif
char file[256];
sprintf(file, "%sworld.db", appdir);
FILE* f = fopen(file, "rb");
if(f != NULL)
{
fseek(f, 0, SEEK_END);
long rs = ftell(f);
fseek(f, 0, SEEK_SET);
if(fread(&g, 1, rs, f) != rs)
{
char tmp[16];
timestamp(tmp);
printf("[%s] fread() was of an unexpected size.\n", tmp);
}
fclose(f);
fks = (g.ms == g.cms); // update F-Key State
char tmp[16];
timestamp(tmp);
#ifndef __linux__
printf("[%s] Loaded %u voxels.\n", tmp, g.num_voxels);
#else
printf("[%s] Loaded %'u voxels. (%'lu μs)\n", tmp, g.num_voxels, microtime()-st);
#endif
return 1;
}
return 0;
}
#ifdef NO_COMPRESSION
void saveState(const char* fne)
{
optimState();
#ifdef __linux__
setlocale(LC_NUMERIC, "");
const uint64_t st = microtime();
#endif
char file[256];
sprintf(file, "%sworld.db%s", appdir, fne);
FILE* f = fopen(file, "wb");
if(f != NULL)
{
const size_t ws = header_size + (g.num_voxels*sizeof(vec));
if(fwrite(&g, 1, ws, f) != ws)
{
char tmp[16];
timestamp(tmp);
printf("[%s] Save corrupted.\n", tmp);
}
fclose(f);
char tmp[16];
timestamp(tmp);
#ifndef __linux__
printf("[%s] Saved %u voxels.\n", tmp, g.num_voxels);
#else
printf("[%s] Saved %'u voxels. (%'lu μs)\n", tmp, g.num_voxels, microtime()-st);
#endif
}
}
uint loadState(){return loadUncompressedState();}
#else
void saveState(const char* fne)
{
optimState();
#ifdef __linux__
setlocale(LC_NUMERIC, "");
const uint64_t st = microtime();
#endif
char file[256];
sprintf(file, "%sworld.gz%s", appdir, fne);
gzFile f = gzopen(file, "wb1h");
if(f != Z_NULL)
{
const size_t ws = header_size + (g.num_voxels*sizeof(vec));
if(gzwrite(f, &g, ws) != ws)
{
char tmp[16];
timestamp(tmp);
printf("[%s] Save corrupted.\n", tmp);
}
gzclose(f);
char tmp[16];
timestamp(tmp);
#ifndef __linux__
printf("[%s] Saved %'u voxels.\n", tmp, g.num_voxels);
#else
printf("[%s] Saved %'u voxels. (%'lu μs)\n", tmp, g.num_voxels, microtime()-st);
#endif
}
}
uint loadState()
{
#ifdef __linux__
setlocale(LC_NUMERIC, "");
const uint64_t st = microtime();
#endif
char file[256];
sprintf(file, "%sworld.gz", appdir);
gzFile f = gzopen(file, "rb");
if(f != Z_NULL)
{
int gr = gzread(f, &g, max_data_size);
gzclose(f);
fks = (g.ms == g.cms); // update F-Key State
char tmp[16];
timestamp(tmp);
#ifndef __linux__
printf("[%s] Loaded %u voxels\n", tmp, g.num_voxels);
#else
printf("[%s] Loaded %'u voxels. (%'lu μs)\n", tmp, g.num_voxels, microtime()-st);
#endif
return 1;
}
return 0;
}
#endif
//*************************************
// ray & render functions
//*************************************
// render state id's
GLint projection_id;
GLint view_id;
GLint voxel_id;
GLint position_id;
GLint normal_id;
GLint texcoord_id;
GLint sampler_id;
// render state matrices
mat projection;
mat view;
// hit_vec will be untouched by this if there's no collision (function by hax/test_user)
#define RAY_DEPTH 70
int ray(vec *hit_vec, const uint depth, const vec vec_start_pos)
{
int hit = -1;
float bestdist;
float start_pos[] = {vec_start_pos.x, vec_start_pos.y, vec_start_pos.z};
float lookdir[] = {look_dir.x, look_dir.y, look_dir.z};
for (int i = 0; i < g.num_voxels; i++)
{
if(g.voxels[i].w < 0.f){continue;}
float offset[] = {g.voxels[i].x - start_pos[0], g.voxels[i].y - start_pos[1], g.voxels[i].z - start_pos[2]};
int j = 0;
// hmmmmmm... is there some decent way to get around this...
float max = fabsf(offset[0]);
float tmp = fabsf(offset[1]);
if(tmp > max){max = tmp; j = 1;}
tmp = fabsf(offset[2]);
if(tmp > max){max = tmp; j = 2;}
if (max <= 0.5f) { // unlikely but better than breaking... costs some cycles though
*hit_vec = vec_start_pos;
return i;
}
float dist_to_start = offset[j];
if(dist_to_start > 0.f){dist_to_start -= 0.5f;}else{dist_to_start += 0.5f;}
const float multiplier = dist_to_start / lookdir[j];
// too far out (or not in the right direction), don't bother
if(multiplier > depth || (hit != -1 && multiplier > bestdist) || multiplier < 0.f){continue;}
// Might end up taking all 222 of those cycles below... :/
// At least better than what it was originally though
// And with enough out of range there's still a chance average comes out under
{
float pos[] = {start_pos[0] + (lookdir[0] * multiplier), start_pos[1] + (lookdir[1] * multiplier), start_pos[2] + (lookdir[2] * multiplier)};
if (pos[0] > g.voxels[i].x + 1.5f || pos[0] < g.voxels[i].x - 1.5f ||
pos[1] > g.voxels[i].y + 1.5f || pos[1] < g.voxels[i].y - 1.5f ||
pos[2] > g.voxels[i].z + 1.5f || pos[2] < g.voxels[i].z - 1.5f) {continue;} // not even a chance of hitting
///
if ((j == 0 || (pos[0] >= g.voxels[i].x - 0.5f && pos[0] <= g.voxels[i].x + 0.5f)) && // j == n so float inaccuracies won't proceed to tell me that it's out after I just put it on the edge
(j == 1 || (pos[1] >= g.voxels[i].y - 0.5f && pos[1] <= g.voxels[i].y + 0.5f)) &&
(j == 2 || (pos[2] >= g.voxels[i].z - 0.5f && pos[2] <= g.voxels[i].z + 0.5f))) { // hit
hit = i;
float sign;
if(offset[j] > 0.f){sign = -1.f;}else{sign = 1.f;}
if(j == 0){hit_vec->x = sign;}else{hit_vec->x = 0.f;}
if(j == 1){hit_vec->y = sign;}else{hit_vec->y = 0.f;}
if(j == 2){hit_vec->z = sign;}else{hit_vec->z = 0.f;}
bestdist = multiplier;
continue; // no need to check side cases :P
}
}
///
float first_dist;
char first_dir;
float second_dist;
char second_dir;
for (int n = 0, y = 0; n <= 2; n++)
{
if (n != j)
{
float dist;
if(offset[n] > 0){dist = offset[n] - 0.5f;}else{dist = offset[n] + 0.5f;}
dist = dist / lookdir[n];
if (y == 0) {
first_dist = dist;
first_dir = n;
} else {
if (dist < first_dist) {
second_dist = first_dist;
second_dir = first_dir;
first_dist = dist;
first_dir = n;
} else {
second_dist = dist;
second_dir = n;
}
}
y++;
}
}
///
{
if(first_dist > depth || (hit != -1 && first_dist > bestdist)){continue;}
float pos[] = {start_pos[0] + (lookdir[0] * first_dist), start_pos[1] + (lookdir[1] * first_dist), start_pos[2] + (lookdir[2] * first_dist)};
if ((first_dir == 0 || (pos[0] >= g.voxels[i].x - 0.5f && pos[0] <= g.voxels[i].x + 0.5f)) &&
(first_dir == 1 || (pos[1] >= g.voxels[i].y - 0.5f && pos[1] <= g.voxels[i].y + 0.5f)) &&
(first_dir == 2 || (pos[2] >= g.voxels[i].z - 0.5f && pos[2] <= g.voxels[i].z + 0.5f)))
{
hit = i;
float sign;
if(offset[first_dir] > 0.f){sign = -1.f;}else{sign = 1.f;}
if(first_dir == 0){hit_vec->x = sign;}else{hit_vec->x = 0.f;}
if(first_dir == 1){hit_vec->y = sign;}else{hit_vec->y = 0.f;}
if(first_dir == 2){hit_vec->z = sign;}else{hit_vec->z = 0.f;}
bestdist = first_dist;
continue;
}
}
///
{
if (second_dist > depth || (hit != -1 && second_dist > bestdist)){continue;}
float pos[] = {start_pos[0] + (lookdir[0] * second_dist), start_pos[1] + (lookdir[1] * second_dist), start_pos[2] + (lookdir[2] * second_dist)};
if ((second_dir == 0 || (pos[0] >= g.voxels[i].x - 0.5f && pos[0] <= g.voxels[i].x + 0.5f)) &&
(second_dir == 1 || (pos[1] >= g.voxels[i].y - 0.5f && pos[1] <= g.voxels[i].y + 0.5f)) &&
(second_dir == 2 || (pos[2] >= g.voxels[i].z - 0.5f && pos[2] <= g.voxels[i].z + 0.5f)))
{
hit = i;
float sign;
if(offset[second_dir] > 0.f){sign = -1.f;}else{sign = 1.f;}
if(second_dir == 0){hit_vec->x = sign;}else{hit_vec->x = 0.f;}
if(second_dir == 1){hit_vec->y = sign;}else{hit_vec->y = 0.f;}
if(second_dir == 2){hit_vec->z = sign;}else{hit_vec->z = 0.f;}
bestdist = second_dist;
continue;
}
}
}
return hit;
}
void traceViewPath(const uint face)
{
g.pb.w = -1.f;
vec rp = g.pb;
lray = ray(&rp, RAY_DEPTH, ipp);
if(lray > -1 && face == 1)
{
rp.x += g.voxels[lray].x;
rp.y += g.voxels[lray].y;
rp.z += g.voxels[lray].z;
uint rpif = 1;
for(int i = 0; i < g.num_voxels; i++)
{
if(g.voxels[i].w < 0.f){continue;}
if(rp.x == g.voxels[i].x && rp.y == g.voxels[i].y && rp.z == g.voxels[i].z)
{
rpif = 0;
break;
}
}
if(rpif == 1)
{
g.pb = rp;
g.pb.w = 1.f;
}
}
}
int placeVoxel(const float repeat_delay)
{
ptt = t+repeat_delay;
if(g.pb.w == -1){return -1;} // place block invalid color (failed ray indication)
for(uint i = 256; i < g.num_voxels; i++)
{
if(g.voxels[i].w < 0.f)
{
g.voxels[i] = g.pb;
g.voxels[i].w = g.st;
return i;
}
}
if(g.num_voxels < max_voxels)
{
g.voxels[g.num_voxels] = g.pb;
g.voxels[g.num_voxels].w = g.st;
const int r = g.num_voxels;
g.num_voxels++;
return r;
}
return -2; // no space left
}
int placeVoxelArb(const vec v)
{
uint free = -1;
for(uint i = 256; i < g.num_voxels; i++)
{
if(free == -1 && g.voxels[i].w < 0.f){free = i;} // find first free slot (pre-emptive)
if(g.voxels[i].x == v.x && g.voxels[i].y == v.y && g.voxels[i].z == v.z){return -1;} // already exists
}
if(free != -1)
{
g.voxels[free] = v;
return free;
}
if(g.num_voxels < max_voxels)
{
g.voxels[g.num_voxels] = v;
const int r = g.num_voxels;
g.num_voxels++;
return r;
}
return -2; // no space left
}
int forceVoxelArb(const vec v)
{
uint free = -1;
for(uint i = 256; i < g.num_voxels; i++)
{
if(free == -1 && g.voxels[i].w < 0.f){free = i;} // find first free slot (pre-emptive)
if(g.voxels[i].x == v.x && g.voxels[i].y == v.y && g.voxels[i].z == v.z){free = i;break;} // already exists
}
if(free != -1)
{
g.voxels[free] = v;
return free;
}
if(g.num_voxels < max_voxels)
{
g.voxels[g.num_voxels] = v;
const int r = g.num_voxels;
g.num_voxels++;
return r;
}
return -2; // no space left
}
void rotatePointed(const float x, const float y, const float z, const uint type)
{
if(sdif.x == 0.f && sdif.y == 0.f && sdif.z == 0.f){return;} // no selection
// check pointed node is not within selection
uint good = 1;
//traceViewPath(0); // not here, because sometimes we want to inject a fake lray
if(lray > -1)
{
float lx=0.f,ly=0.f,lz=0.f,hx=0.f,hy=0.f,hz=0.f;
if(sp1o.x < sp2.x){lx=sp1o.x;hx=sp2.x;}else{lx=sp2.x;hx=sp1o.x;}
if(sp1o.y < sp2.y){ly=sp1o.y;hy=sp2.y;}else{ly=sp2.y;hy=sp1o.y;}
if(sp1o.z < sp2.z){lz=sp1o.z;hz=sp2.z;}else{lz=sp2.z;hz=sp1o.z;}
if( g.voxels[lray].x >= lx && g.voxels[lray].x <= hx &&
g.voxels[lray].y >= ly && g.voxels[lray].y <= hy &&
g.voxels[lray].z >= lz && g.voxels[lray].z <= hz )
{
good = 0;
}
}
if(good == 1)
{
// copy nodes to new position, from sp1o to g.voxels[lray]
for(uint i = 0; i < g.num_voxels; i++)
{
if(g.voxels[i].w < 0.f){continue;}
float lx=0.f,ly=0.f,lz=0.f,hx=0.f,hy=0.f,hz=0.f;
if(sp1o.x < sp2.x){lx=sp1o.x;hx=sp2.x;}else{lx=sp2.x;hx=sp1o.x;}
if(sp1o.y < sp2.y){ly=sp1o.y;hy=sp2.y;}else{ly=sp2.y;hy=sp1o.y;}
if(sp1o.z < sp2.z){lz=sp1o.z;hz=sp2.z;}else{lz=sp2.z;hz=sp1o.z;}
if( g.voxels[i].x >= lx && g.voxels[i].x <= hx &&
g.voxels[i].y >= ly && g.voxels[i].y <= hy &&
g.voxels[i].z >= lz && g.voxels[i].z <= hz )
{
if(type == 0)
{
forceVoxelArb((vec){g.voxels[lray].x+((g.voxels[i].y-sp1o.y)*x),
g.voxels[lray].y+((g.voxels[i].x-sp1o.x)*y),
g.voxels[lray].z+((g.voxels[i].z-sp1o.z)*z),
g.voxels[i].w});
}
else if(type == 1)
{
forceVoxelArb((vec){g.voxels[lray].x+((g.voxels[i].y-sp1o.y)*x),
g.voxels[lray].y+((g.voxels[i].z-sp1o.z)*z),
g.voxels[lray].z+((g.voxels[i].x-sp1o.x)*y),
g.voxels[i].w});
}
else if(type == 2)
{
forceVoxelArb((vec){g.voxels[lray].x+((g.voxels[i].x-sp1o.x)*y),
g.voxels[lray].y+((g.voxels[i].y-sp1o.y)*x),
g.voxels[lray].z+((g.voxels[i].z-sp1o.z)*z),
g.voxels[i].w});
}
else if(type == 3)
{
forceVoxelArb((vec){g.voxels[lray].x+((g.voxels[i].x-sp1o.x)*y),
g.voxels[lray].y+((g.voxels[i].z-sp1o.z)*z),
g.voxels[lray].z+((g.voxels[i].y-sp1o.y)*x),
g.voxels[i].w});
}
else if(type == 4)
{
forceVoxelArb((vec){g.voxels[lray].x+((g.voxels[i].z-sp1o.z)*z),
g.voxels[lray].y+((g.voxels[i].x-sp1o.x)*y),
g.voxels[lray].z+((g.voxels[i].y-sp1o.y)*x),
g.voxels[i].w});
}
else if(type == 5)
{
forceVoxelArb((vec){g.voxels[lray].x+((g.voxels[i].z-sp1o.z)*z),
g.voxels[lray].y+((g.voxels[i].y-sp1o.y)*x),
g.voxels[lray].z+((g.voxels[i].x-sp1o.x)*y),
g.voxels[i].w});
}
}
}
}
}
void rotatePointedIndex(const uint c)
{
if(c == 0){rotatePointed( 1.f, 1.f, 1.f, 0);} //
if(c == 1){rotatePointed(-1.f, -1.f, -1.f, 0);}
if(c == 2){rotatePointed( 1.f, 1.f, -1.f, 0);}
if(c == 3){rotatePointed(-1.f, -1.f, 1.f, 0);}
if(c == 4){rotatePointed( 1.f, 1.f, 1.f, 1);} //
if(c == 5){rotatePointed(-1.f, -1.f, -1.f, 1);}
if(c == 6){rotatePointed( 1.f, 1.f, -1.f, 1);}
if(c == 7){rotatePointed(-1.f, -1.f, 1.f, 1);}
if(c == 8){rotatePointed( 1.f, 1.f, 1.f, 2);} //
if(c == 9){rotatePointed(-1.f, -1.f, -1.f, 2);}
if(c == 10){rotatePointed( 1.f, 1.f, -1.f, 2);}
if(c == 11){rotatePointed(-1.f, -1.f, 1.f, 2);}
if(c == 12){rotatePointed( 1.f, 1.f, 1.f, 3);} //
if(c == 13){rotatePointed(-1.f, -1.f, -1.f, 3);}
if(c == 14){rotatePointed( 1.f, 1.f, -1.f, 3);}
if(c == 15){rotatePointed(-1.f, -1.f, 1.f, 3);}
if(c == 16){rotatePointed( 1.f, 1.f, 1.f, 4);} //
if(c == 17){rotatePointed(-1.f, -1.f, -1.f, 4);}
if(c == 18){rotatePointed( 1.f, 1.f, -1.f, 4);}
if(c == 19){rotatePointed(-1.f, -1.f, 1.f, 4);}
if(c == 20){rotatePointed( 1.f, 1.f, 1.f, 5);} //
if(c == 21){rotatePointed(-1.f, -1.f, -1.f, 5);}
if(c == 22){rotatePointed( 1.f, -1.f, 1.f, 5);}
if(c == 23){rotatePointed(-1.f, 1.f, -1.f, 5);}
}
//*************************************
// more utility functions
//*************************************
void printAttrib(SDL_GLattr attr, char* name)
{
int i;
SDL_GL_GetAttribute(attr, &i);
printf("%s: %i\n", name, i);
}
SDL_Surface* SDL_RGBA32Surface(Uint32 w, Uint32 h)
{
return SDL_CreateRGBSurfaceWithFormat(0, w, h, 32, SDL_PIXELFORMAT_RGBA32);
}
void drawHud();
void doPerspective()
{
glViewport(0, 0, winw, winh);
if(winw > 500 && winh > 460)
{
SDL_FreeSurface(sHud);
sHud = SDL_RGBA32Surface(winw, winh);
drawHud();
hudmap = esLoadTextureA(winw, winh, sHud->pixels, 0);
}
ww = (float)winw;
wh = (float)winh;
mIdent(&projection);
mPerspective(&projection, 60.0f, ww / wh, 1.0f, ddist);
glUniformMatrix4fv(projection_id, 1, GL_FALSE, (float*)&projection.m[0][0]);
}
forceinline uint insideFrustum(const float x, const float y, const float z)
{
const float xm = x+g.pp.x, ym = y+g.pp.y, zm = z+g.pp.z;
return (xm*look_dir.x) + (ym*look_dir.y) + (zm*look_dir.z) > 0.f; // check the angle
}
SDL_Surface* surfaceFromData(const Uint32* data, Uint32 w, Uint32 h)
{
SDL_Surface* s = SDL_CreateRGBSurfaceWithFormat(0, w, h, 32, SDL_PIXELFORMAT_RGBA32);
memcpy(s->pixels, data, s->pitch*h);
return s;
}
forceinline Uint32 getpixel(const SDL_Surface *surface, Uint32 x, Uint32 y)
{
const Uint8 *p = (Uint8*)surface->pixels + y * surface->pitch + x * surface->format->BytesPerPixel;
return *(Uint32*)p;
}
forceinline void setpixel(SDL_Surface *surface, Uint32 x, Uint32 y, Uint32 pix)
{
const Uint8* pixel = (Uint8*)surface->pixels + (y * surface->pitch) + (x * surface->format->BytesPerPixel);
*((Uint32*)pixel) = pix;
}
void replaceColour(SDL_Surface* surf, SDL_Rect r, Uint32 old_color, Uint32 new_color)
{
const Uint32 max_y = r.y+r.h;
const Uint32 max_x = r.x+r.w;
for(Uint32 y = r.y; y < max_y; ++y)
for(Uint32 x = r.x; x < max_x; ++x)
if(getpixel(surf, x, y) == old_color){setpixel(surf, x, y, new_color);}
}
//*************************************
// Simple Font
//*************************************
int drawText(SDL_Surface* o, const char* s, Uint32 x, Uint32 y, Uint8 colour)
{
static const Uint8 font_map[] = {255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,0,0,255,255,255,255,255,255,255,255,255,255,255,255,255,0,0,255,255,255,255,255,255,255,0,0,0,255,255,255,255,255,255,0,0,255,255,255,255,255,255,255,255,255,0,0,255,255,255,255,0,0,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,0,0,0,255,255,0,0,0,0,0,255,255,0,0,0,0,0,0,0,0,0,0,255,255,0,0,0,0,0,0,0,0,0,0,255,0,0,0,0,0,0,0,0,255,255,255,0,0,0,0,0,0,255,0,0,0,0,0,0,255,255,0,0,0,0,255,255,255,0,0,255,255,255,255,255,0,0,0,0,255,255,255,0,255,0,0,0,0,0,255,0,0,0,0,0,255,255,0,0,0,0,0,255,0,0,0,0,0,255,255,255,0,0,0,0,0,0,0,0,0,0,0,0,0,255,255,255,0,0,0,0,255,255,0,0,0,0,255,255,0,0,255,255,0,0,0,0,255,255,0,0,0,0,255,255,0,0,0,0,0,0,0,0,255,255,255,255,255,255,0,0,255,255,255,255,255,255,255,255,255,255,255,255,255,0,0,255,255,255,255,255,255,0,0,255,255,255,255,255,255,255,255,0,0,255,255,255,255,0,0,255,0,0,0,0,255,255,255,255,0,0,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,0,0,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,0,0,0,0,255,255,0,0,255,255,0,0,0,0,255,255,0,0,0,0,255,255,255,255,255,0,255,255,0,0,0,0,0,255,0,0,0,0,255,0,0,0,0,0,0,255,0,0,0,0,255,255,0,0,0,0,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,0,0,0,255,255,0,0,255,255,0,0,0,0,255,255,255,255,0,0,255,255,0,0,255,0,0,255,255,255,0,0,255,255,255,0,0,255,255,255,255,255,0,0,255,255,255,0,0,255,0,0,255,255,255,255,0,0,0,0,255,0,0,255,0,0,255,255,255,0,0,0,255,255,255,0,0,0,0,0,0,255,255,0,0,0,255,255,255,0,0,0,0,255,255,0,0,0,0,255,255,255,0,0,0,0,255,255,0,0,255,0,0,255,255,255,255,255,255,0,0,255,255,0,0,255,255,255,0,0,0,0,255,255,0,0,0,0,255,255,0,0,255,255,0,0,0,0,255,255,0,0,0,0,255,255,0,0,255,255,255,255,0,0,255,255,255,255,255,255,0,0,255,255,255,255,255,255,255,255,255,255,255,255,255,0,0,255,255,255,255,255,255,0,0,255,255,255,255,255,255,255,255,0,0,255,255,255,255,255,255,255,255,255,0,0,255,255,255,255,0,0,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,0,0,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,0,0,255,255,0,0,0,0,0,255,0,0,255,255,0,0,0,0,255,255,0,0,255,255,255,0,0,255,255,0,0,255,255,255,0,0,255,255,255,255,255,255,255,255,0,0,0,0,255,255,0,0,0,0,255,255,0,0,255,255,255,255,255,255,255,0,255,255,255,255,255,255,255,255,0,0,255,0,0,255,0,0,255,255,0,0,0,0,255,255,255,255,0,0,255,255,255,0,0,0,0,255,255,255,0,0,255,255,255,0,0,255,255,255,255,255,0,0,255,255,255,0,0,255,0,0,255,255,255,255,0,0,0,0,0,0,255,255,0,0,255,255,255,0,0,0,0,255,0,0,0,0,0,0,0,0,255,0,0,0,255,255,255,0,0,0,0,255,255,0,0,0,0,255,255,255,0,0,0,0,255,255,0,0,255,0,0,255,255,255,255,255,255,0,0,255,255,0,0,255,255,255,0,0,0,0,255,255,0,0,0,0,255,255,0,0,255,255,0,0,255,0,0,0,0,255,255,0,0,0,0,255,255,255,255,0,0,0,255,0,0,0,0,255,0,0,0,0,0,255,255,0,0,0,0,255,0,0,0,0,0,255,0,0,0,0,255,0,0,0,255,255,0,0,0,0,0,0,0,0,0,0,255,0,0,0,0,0,0,0,255,255,0,0,0,0,0,0,0,0,0,255,0,0,0,255,0,0,0,0,0,255,255,0,0,0,0,255,0,0,0,0,0,255,255,0,0,0,0,0,0,0,255,0,255,0,0,0,0,0,0,0,0,0,0,255,255,0,0,0,0,255,255,0,0,0,0,255,0,0,255,0,0,0,0,255,255,0,0,0,0,255,255,0,0,0,0,0,0,0,0,0,255,255,0,0,255,0,0,255,255,255,255,255,0,0,255,255,255,255,0,0,255,255,0,0,0,255,255,0,0,255,255,255,0,0,255,255,255,255,255,255,255,0,0,255,0,0,255,255,0,0,0,0,255,255,0,0,0,0,255,255,255,255,255,0,255,255,255,255,255,255,255,255,0,0,255,0,0,255,0,0,0,0,0,255,0,0,255,255,255,255,0,0,255,255,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,255,255,255,255,0,0,0,0,0,0,0,255,0,0,255,255,255,255,0,0,0,0,0,255,255,255,0,0,255,255,255,0,255,0,0,0,0,255,0,0,0,255,0,0,0,0,0,0,255,255,255,0,0,0,0,255,255,0,0,0,0,255,255,255,0,0,0,0,255,255,0,0,255,0,0,0,0,0,255,255,255,0,0,255,255,0,0,255,255,255,0,0,255,0,0,0,0,255,255,0,0,255,0,0,255,0,0,255,255,255,0,0,255,255,255,0,0,0,0,255,255,255,0,0,0,255,255,255,255,255,0,0,0,0,255,255,0,0,0,0,255,255,255,0,0,255,255,0,0,0,0,255,255,0,0,0,0,255,255,0,0,255,255,0,0,0,0,255,255,0,0,0,0,255,0,0,0,0,255,0,0,255,0,0,0,0,255,255,0,0,255,255,0,0,0,0,255,255,0,0,0,0,255,255,0,0,0,0,255,255,0,0,0,0,255,255,0,0,0,0,0,0,0,0,255,255,255,0,0,255,255,0,0,255,255,0,0,0,0,255,255,0,0,0,0,255,0,0,255,0,0,0,0,255,255,0,0,0,0,255,255,0,0,255,255,255,0,0,0,0,255,255,0,0,255,0,0,255,255,255,255,0,0,255,255,255,0,0,0,255,255,0,255,0,0,255,255,0,0,0,0,255,0,0,0,0,0,255,255,255,255,0,0,255,255,0,0,0,0,255,0,0,255,255,0,0,0,0,255,255,255,255,255,0,255,255,255,255,255,255,255,255,0,0,255,0,0,255,0,0,255,255,0,0,0,0,255,255,255,255,0,0,255,255,255,0,0,0,0,255,255,255,0,0,255,255,255,0,0,255,255,0,0,0,0,0,255,255,255,0,0,255,0,0,255,255,255,255,0,0,0,0,0,255,255,255,0,0,255,255,255,0,255,255,0,0,255,255,0,0,0,255,255,0,0,0,0,0,255,255,255,0,0,0,0,0,0,0,255,0,0,255,255,255,0,0,0,0,0,0,0,255,255,255,0,0,0,0,0,255,255,0,0,255,255,0,0,255,255,255,0,0,255,0,0,0,0,255,255,0,0,0,0,0,0,0,0,255,255,255,0,0,255,255,255,255,0,0,255,255,255,0,0,0,255,255,255,0,0,0,0,0,0,0,255,255,0,0,0,0,255,255,255,0,0,255,255,0,0,0,0,0,0,0,0,0,0,255,255,0,0,255,255,0,0,0,0,255,255,0,0,0,0,255,0,0,0,0,0,0,255,255,0,0,0,0,255,255,0,0,255,255,0,0,0,0,255,255,0,0,0,0,255,255,0,0,0,0,255,255,0,0,0,0,255,255,0,0,0,0,255,255,0,0,0,0,255,0,0,255,255,0,0,255,255,0,0,255,0,0,0,0,255,0,0,255,0,0,255,0,0,255,0,0,0,0,255,255,0,0,0,0,255,255,255,0,0,255,0,0,255,255,0,0,255,0,0,255,255,255,0,0,255,255,255,255,255,255,0,0,0,255,255,0,0,255,255,255,255,255,0,0,0,0,255,255,0,0,255,255,255,0,0,255,0,0,255,255,0,0,255,0,0,0,0,0,255,255,255,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,255,0,0,0,0,255,255,255,255,0,0,255,255,255,0,0,0,0,255,255,255,0,0,255,255,255,0,0,255,255,255,0,0,0,0,255,255,255,0,0,255,0,0,255,255,255,255,0,0,0,0,0,0,255,255,0,0,255,255,255,0,255,255,255,255,255,255,0,0,0,255,255,255,0,0,0,0,255,255,255,0,0,0,0,255,255,255,255,0,0,255,255,255,0,0,0,0,255,0,0,255,255,255,255,255,255,0,0,255,255,0,0,255,255,0,0,255,255,255,0,0,255,0,0,0,0,255,255,0,0,0,255,255,0,0,0,255,255,0,0,0,0,255,255,255,0,0,255,255,0,0,0,255,255,255,0,0,255,255,0,0,0,0,255,255,0,0,0,0,255,255,255,0,0,255,255,0,0,0,0,255,255,255,255,0,0,255,255,0,0,255,255,0,0,0,0,255,255,0,0,0,0,255,0,0,0,0,0,0,255,255,0,0,0,0,255,255,0,0,255,255,0,0,0,0,255,255,0,0,0,0,255,255,0,0,0,0,255,255,0,0,0,0,255,255,0,0,0,0,255,255,255,0,0,0,0,0,0,255,255,0,0,255,255,0,0,255,0,0,0,0,255,0,0,0,0,0,0,0,0,255,0,0,0,0,255,255,0,0,0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,0,0,255,255,0,0,255,255,255,255,255,255,255,0,0,0,0,0,0,0,0,255,255,255,255,0,0,0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255,255,255,0,0,255,255,255,255,255,255,255,0,255,255,255,255,255,255,255,0,0,255,255,255,0,0,0,0,255,255,0,0,0,0,255,255,255,255,0,0,255,255,0,0,255,0,0,255,255,255,0,0,255,255,255,0,0,255,255,255,0,0,0,0,255,255,255,0,0,255,0,0,255,255,255,255,0,0,0,0,255,0,0,255,0,0,255,255,255,0,255,255,255,255,255,255,0,0,0,255,255,255,255,0,0,0,255,255,255,0,0,0,0,255,255,255,255,0,0,255,255,255,0,0,0,0,255,255,0,0,255,255,255,255,255,0,0,255,255,0,0,255,255,0,0,255,255,255,0,0,255,255,0,0,255,255,255,255,0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255,255,255,0,0,255,255,0,0,0,0,255,255,0,0,0,0,255,255,255,0,0,255,255,0,0,0,0,255,255,255,255,0,0,255,255,0,0,255,255,0,0,0,0,255,255,0,0,0,0,255,0,0,0,0,255,0,0,255,0,0,0,0,255,255,0,0,255,255,0,0,0,0,255,255,0,0,0,0,255,255,0,0,0,0,255,255,0,0,0,0,255,255,0,0,0,0,255,255,255,255,255,0,0,0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255,255,0,0,255,255,0,0,255,0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255,255,0,0,255,255,0,0,255,0,0,255,0,0,255,255,255,255,0,0,255,255,0,0,255,255,255,0,0,255,0,0,255,255,0,0,0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255,255,255,0,0,0,0,0,0,255,255,255,0,255,255,255,255,255,255,255,0,0,255,255,255,0,0,0,0,0,0,0,255,255,0,0,0,0,0,0,0,0,0,0,255,255,0,0,0,0,0,0,0,255,255,255,255,0,0,0,0,0,0,0,0,255,255,255,0,0,0,0,0,0,0,0,0,0,255,0,0,255,255,0,0,0,0,0,0,0,0,255,255,255,255,255,255,0,0,0,255,255,255,255,0,255,0,0,0,0,0,255,0,0,255,255,255,255,255,0,0,0,0,0,255,0,0,255,255,255,0,0,0,0,0,0,0,255,255,255,0,0,255,255,255,0,0,0,0,0,255,255,255,0,0,255,255,255,255,0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255,0,0,0,0,0,0,255,0,0,0,0,0,0,0,0,0,0,255,255,0,0,0,0,255,0,0,0,0,0,255,0,0,0,0,0,0,0,255,255,255,0,0,0,0,0,0,0,255,255,0,0,0,0,255,0,0,0,0,255,255,0,0,0,0,0,0,255,255,0,0,255,255,0,0,0,0,255,255,0,0,255,0,0,0,0,255,0,0,0,0,0,255,255,0,0,0,0,0,0,0,255,255,0,0,0,0,255,255,0,0,0,255,0,0,0,0,0,255,255,0,0,255,255,255,0,0,255,255,0,0,255,0,0,255,255,0,0,255,255,0,0,255,255,0,0,0,0,0,255,0,0,0,0,255,0,0,0,0,0,0,0,0,0,0,255,0,0,0,0,255,255,255,255,0,0,255,255,0,0,0,0,255,255,0,0,0,0,255,255,255,0,0,255,255,255,0,0,0,0,255,255,0,0,0,0,255,0,0,0,0,255,255,255,0,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,0,0,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,0,0,255,255,255,255,255,255,255,255,255,0,0,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,0,0,255,255,255,255,255,255,255,255,0,0,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,0,0,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,0,0,0,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,0,0,0,0,255,255,255,255,255,255,255,255,255,0,0,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,0,0,255,255,255,255,255,255,255,255,0,0,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,0,0,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255};
static const Uint32 m = 1;
static SDL_Surface* font_black = NULL;
static SDL_Surface* font_white = NULL;
static SDL_Surface* font_aqua = NULL;
static SDL_Surface* font_gold = NULL;
static SDL_Surface* font_cia = NULL;
if(font_black == NULL)
{
font_black = SDL_RGBA32Surface(380, 11);
for(int y = 0; y < font_black->h; y++)
{
for(int x = 0; x < font_black->w; x++)
{
const Uint8 c = font_map[(y*font_black->w)+x];
setpixel(font_black, x, y, SDL_MapRGBA(font_black->format, c, c, c, 255-c));
}
}
font_white = SDL_RGBA32Surface(380, 11);
SDL_BlitSurface(font_black, &font_black->clip_rect, font_white, &font_white->clip_rect);
replaceColour(font_white, font_white->clip_rect, 0xFF000000, 0xFFFFFFFF);
font_aqua = SDL_RGBA32Surface(380, 11);
SDL_BlitSurface(font_black, &font_black->clip_rect, font_aqua, &font_aqua->clip_rect);
replaceColour(font_aqua, font_aqua->clip_rect, 0xFF000000, 0xFFFFFF00);
font_gold = SDL_RGBA32Surface(380, 11);
SDL_BlitSurface(font_black, &font_black->clip_rect, font_gold, &font_gold->clip_rect);
replaceColour(font_gold, font_gold->clip_rect, 0xFF000000, 0xFF00BFFF);
font_cia = SDL_RGBA32Surface(380, 11);
SDL_BlitSurface(font_black, &font_black->clip_rect, font_cia, &font_cia->clip_rect);
replaceColour(font_cia, font_cia->clip_rect, 0xFF000000, 0xFF97C920);
// #20c997(0xFF97C920) #00FF41(0xFF41FF00) #61d97c(0xFF7CD961) #51d4e9(0xFFE9D451)
}
if(s[0] == '*' && s[1] == 'K') // signal cleanup
{
SDL_FreeSurface(font_black);
SDL_FreeSurface(font_white);
SDL_FreeSurface(font_aqua);
SDL_FreeSurface(font_gold);
SDL_FreeSurface(font_cia);
font_black = NULL;
return 0;
}
SDL_Surface* font = font_black;
if( colour == 1){font = font_white;}
else if(colour == 2){font = font_aqua;}
else if(colour == 3){font = font_gold;}
else if(colour == 4){font = font_cia;}
SDL_Rect dr = {x, y, 0, 0};
const Uint32 len = strlen(s);
for(Uint32 i = 0; i < len; i++)
{
if(s[i] == 'A'){SDL_BlitSurface(font, &(SDL_Rect){0,0,7,9}, o, &dr); dr.x += 7+m;}
else if(s[i] == 'B'){SDL_BlitSurface(font, &(SDL_Rect){7,0,6,9}, o, &dr); dr.x += 6+m;}
else if(s[i] == 'C'){SDL_BlitSurface(font, &(SDL_Rect){13,0,6,9}, o, &dr); dr.x += 6+m;}
else if(s[i] == 'D'){SDL_BlitSurface(font, &(SDL_Rect){19,0,7,9}, o, &dr); dr.x += 7+m;}
else if(s[i] == 'E'){SDL_BlitSurface(font, &(SDL_Rect){26,0,5,9}, o, &dr); dr.x += 5+m;}
else if(s[i] == 'F'){SDL_BlitSurface(font, &(SDL_Rect){31,0,5,9}, o, &dr); dr.x += 5+m;}
else if(s[i] == 'G'){SDL_BlitSurface(font, &(SDL_Rect){36,0,7,9}, o, &dr); dr.x += 7+m;}
else if(s[i] == 'H'){SDL_BlitSurface(font, &(SDL_Rect){43,0,7,9}, o, &dr); dr.x += 7+m;}
else if(s[i] == 'I'){SDL_BlitSurface(font, &(SDL_Rect){50,0,4,9}, o, &dr); dr.x += 4+m;}
else if(s[i] == 'J'){SDL_BlitSurface(font, &(SDL_Rect){54,0,5,9}, o, &dr); dr.x += 5+m;}
else if(s[i] == 'K'){SDL_BlitSurface(font, &(SDL_Rect){59,0,6,9}, o, &dr); dr.x += 6+m;}
else if(s[i] == 'L'){SDL_BlitSurface(font, &(SDL_Rect){65,0,5,9}, o, &dr); dr.x += 5+m;}
else if(s[i] == 'M'){SDL_BlitSurface(font, &(SDL_Rect){70,0,9,9}, o, &dr); dr.x += 9+m;}
else if(s[i] == 'N'){SDL_BlitSurface(font, &(SDL_Rect){79,0,6,9}, o, &dr); dr.x += 6+m;}
else if(s[i] == 'O'){SDL_BlitSurface(font, &(SDL_Rect){85,0,7,9}, o, &dr); dr.x += 7+m;}
else if(s[i] == 'P'){SDL_BlitSurface(font, &(SDL_Rect){92,0,6,9}, o, &dr); dr.x += 6+m;}
else if(s[i] == 'Q'){SDL_BlitSurface(font, &(SDL_Rect){98,0,7,11}, o, &dr); dr.x += 7+m;}
else if(s[i] == 'R'){SDL_BlitSurface(font, &(SDL_Rect){105,0,7,9}, o, &dr); dr.x += 7+m;}
else if(s[i] == 'S'){SDL_BlitSurface(font, &(SDL_Rect){112,0,6,9}, o, &dr); dr.x += 6+m;}
else if(s[i] == 'T'){SDL_BlitSurface(font, &(SDL_Rect){118,0,6,9}, o, &dr); dr.x += 6+m;}
else if(s[i] == 'U'){SDL_BlitSurface(font, &(SDL_Rect){124,0,7,9}, o, &dr); dr.x += 7+m;}
else if(s[i] == 'V'){SDL_BlitSurface(font, &(SDL_Rect){131,0,6,9}, o, &dr); dr.x += 6+m;}
else if(s[i] == 'W'){SDL_BlitSurface(font, &(SDL_Rect){137,0,10,9}, o, &dr); dr.x += 10+m;}
else if(s[i] == 'X'){SDL_BlitSurface(font, &(SDL_Rect){147,0,6,9}, o, &dr); dr.x += 6+m;}
else if(s[i] == 'Y'){SDL_BlitSurface(font, &(SDL_Rect){153,0,6,9}, o, &dr); dr.x += 6+m;}
else if(s[i] == 'Z'){SDL_BlitSurface(font, &(SDL_Rect){159,0,6,9}, o, &dr); dr.x += 6+m;}
else if(s[i] == 'a'){SDL_BlitSurface(font, &(SDL_Rect){165,0,6,9}, o, &dr); dr.x += 6+m;}
else if(s[i] == 'b'){SDL_BlitSurface(font, &(SDL_Rect){171,0,6,9}, o, &dr); dr.x += 6+m;}
else if(s[i] == 'c'){SDL_BlitSurface(font, &(SDL_Rect){177,0,5,9}, o, &dr); dr.x += 5+m;}
else if(s[i] == 'd'){SDL_BlitSurface(font, &(SDL_Rect){182,0,6,9}, o, &dr); dr.x += 6+m;}
else if(s[i] == 'e'){SDL_BlitSurface(font, &(SDL_Rect){188,0,6,9}, o, &dr); dr.x += 6+m;}
else if(s[i] == 'f'){SDL_BlitSurface(font, &(SDL_Rect){194,0,4,9}, o, &dr); dr.x += 3+m;}
else if(s[i] == 'g'){SDL_BlitSurface(font, &(SDL_Rect){198,0,6,11}, o, &dr); dr.x += 6+m;}
else if(s[i] == 'h'){SDL_BlitSurface(font, &(SDL_Rect){204,0,6,9}, o, &dr); dr.x += 6+m;}
else if(s[i] == 'i'){SDL_BlitSurface(font, &(SDL_Rect){210,0,2,9}, o, &dr); dr.x += 2+m;}
else if(s[i] == 'j'){SDL_BlitSurface(font, &(SDL_Rect){212,0,3,11}, o, &dr); dr.x += 3+m;}
else if(s[i] == 'k'){SDL_BlitSurface(font, &(SDL_Rect){215,0,6,9}, o, &dr); dr.x += 6+m;}
else if(s[i] == 'l'){SDL_BlitSurface(font, &(SDL_Rect){221,0,2,9}, o, &dr); dr.x += 2+m;}
else if(s[i] == 'm'){SDL_BlitSurface(font, &(SDL_Rect){223,0,10,9}, o, &dr); dr.x += 10+m;}
else if(s[i] == 'n'){SDL_BlitSurface(font, &(SDL_Rect){233,0,6,9}, o, &dr); dr.x += 6+m;}
else if(s[i] == 'o'){SDL_BlitSurface(font, &(SDL_Rect){239,0,6,9}, o, &dr); dr.x += 6+m;}
else if(s[i] == 'p'){SDL_BlitSurface(font, &(SDL_Rect){245,0,6,11}, o, &dr); dr.x += 6+m;}
else if(s[i] == 'q'){SDL_BlitSurface(font, &(SDL_Rect){251,0,6,11}, o, &dr); dr.x += 6+m;}
else if(s[i] == 'r'){SDL_BlitSurface(font, &(SDL_Rect){257,0,4,9}, o, &dr); dr.x += 4+m;}
else if(s[i] == 's'){SDL_BlitSurface(font, &(SDL_Rect){261,0,5,9}, o, &dr); dr.x += 5+m;}
else if(s[i] == 't'){SDL_BlitSurface(font, &(SDL_Rect){266,0,4,9}, o, &dr); dr.x += 4+m;}
else if(s[i] == 'u'){SDL_BlitSurface(font, &(SDL_Rect){270,0,6,9}, o, &dr); dr.x += 6+m;}
else if(s[i] == 'v'){SDL_BlitSurface(font, &(SDL_Rect){276,0,6,9}, o, &dr); dr.x += 6+m;}
else if(s[i] == 'w'){SDL_BlitSurface(font, &(SDL_Rect){282,0,8,9}, o, &dr); dr.x += 8+m;}
else if(s[i] == 'x'){SDL_BlitSurface(font, &(SDL_Rect){290,0,6,9}, o, &dr); dr.x += 6+m;}
else if(s[i] == 'y'){SDL_BlitSurface(font, &(SDL_Rect){296,0,6,11}, o, &dr); dr.x += 6+m;}
else if(s[i] == 'z'){SDL_BlitSurface(font, &(SDL_Rect){302,0,5,9}, o, &dr); dr.x += 5+m;}
else if(s[i] == '0'){SDL_BlitSurface(font, &(SDL_Rect){307,0,6,9}, o, &dr); dr.x += 6+m;}
else if(s[i] == '1'){SDL_BlitSurface(font, &(SDL_Rect){313,0,4,9}, o, &dr); dr.x += 4+m;}
else if(s[i] == '2'){SDL_BlitSurface(font, &(SDL_Rect){317,0,6,9}, o, &dr); dr.x += 6+m;}
else if(s[i] == '3'){SDL_BlitSurface(font, &(SDL_Rect){323,0,6,9}, o, &dr); dr.x += 6+m;}
else if(s[i] == '4'){SDL_BlitSurface(font, &(SDL_Rect){329,0,6,9}, o, &dr); dr.x += 6+m;}
else if(s[i] == '5'){SDL_BlitSurface(font, &(SDL_Rect){335,0,6,9}, o, &dr); dr.x += 6+m;}
else if(s[i] == '6'){SDL_BlitSurface(font, &(SDL_Rect){341,0,6,9}, o, &dr); dr.x += 6+m;}
else if(s[i] == '7'){SDL_BlitSurface(font, &(SDL_Rect){347,0,6,9}, o, &dr); dr.x += 6+m;}
else if(s[i] == '8'){SDL_BlitSurface(font, &(SDL_Rect){353,0,6,9}, o, &dr); dr.x += 6+m;}
else if(s[i] == '9'){SDL_BlitSurface(font, &(SDL_Rect){359,0,6,9}, o, &dr); dr.x += 6+m;}
else if(s[i] == ':'){SDL_BlitSurface(font, &(SDL_Rect){365,0,2,9}, o, &dr); dr.x += 2+m;}
else if(s[i] == '.'){SDL_BlitSurface(font, &(SDL_Rect){367,0,2,9}, o, &dr); dr.x += 2+m;}
else if(s[i] == '+'){SDL_BlitSurface(font, &(SDL_Rect){369,0,7,9}, o, &dr); dr.x += 7+m;}
else if(s[i] == '-'){dr.x++; SDL_BlitSurface(font, &(SDL_Rect){376,0,4,9}, o, &dr); dr.x += 6+m;}
else if(s[i] == ' '){dr.x += 2+m;}
}
return dr.x;
}
int lenText(const char* s)
{
int x = 0;
static const int m = 1;
const Uint32 len = strlen(s);
for(Uint32 i = 0; i < len; i++)
{
if(s[i] == 'A'){x += 7+m;}
else if(s[i] == 'B'){x += 6+m;}
else if(s[i] == 'C'){x += 6+m;}
else if(s[i] == 'D'){x += 7+m;}
else if(s[i] == 'E'){x += 5+m;}
else if(s[i] == 'F'){x += 5+m;}
else if(s[i] == 'G'){x += 7+m;}
else if(s[i] == 'H'){x += 7+m;}
else if(s[i] == 'I'){x += 4+m;}
else if(s[i] == 'J'){x += 5+m;}
else if(s[i] == 'K'){x += 6+m;}
else if(s[i] == 'L'){x += 5+m;}
else if(s[i] == 'M'){x += 9+m;}
else if(s[i] == 'N'){x += 6+m;}
else if(s[i] == 'O'){x += 7+m;}
else if(s[i] == 'P'){x += 6+m;}
else if(s[i] == 'Q'){x += 7+m;}
else if(s[i] == 'R'){x += 7+m;}
else if(s[i] == 'S'){x += 6+m;}
else if(s[i] == 'T'){x += 6+m;}
else if(s[i] == 'U'){x += 7+m;}
else if(s[i] == 'V'){x += 6+m;}
else if(s[i] == 'W'){x += 10+m;}
else if(s[i] == 'X'){x += 6+m;}
else if(s[i] == 'Y'){x += 6+m;}
else if(s[i] == 'Z'){x += 6+m;}
else if(s[i] == 'a'){x += 6+m;}
else if(s[i] == 'b'){x += 6+m;}
else if(s[i] == 'c'){x += 5+m;}
else if(s[i] == 'd'){x += 6+m;}
else if(s[i] == 'e'){x += 6+m;}
else if(s[i] == 'f'){x += 3+m;}
else if(s[i] == 'g'){x += 6+m;}
else if(s[i] == 'h'){x += 6+m;}
else if(s[i] == 'i'){x += 2+m;}
else if(s[i] == 'j'){x += 3+m;}
else if(s[i] == 'k'){x += 6+m;}
else if(s[i] == 'l'){x += 2+m;}
else if(s[i] == 'm'){x += 10+m;}
else if(s[i] == 'n'){x += 6+m;}
else if(s[i] == 'o'){x += 6+m;}
else if(s[i] == 'p'){x += 6+m;}
else if(s[i] == 'q'){x += 6+m;}
else if(s[i] == 'r'){x += 4+m;}
else if(s[i] == 's'){x += 5+m;}
else if(s[i] == 't'){x += 4+m;}
else if(s[i] == 'u'){x += 6+m;}
else if(s[i] == 'v'){x += 6+m;}
else if(s[i] == 'w'){x += 8+m;}
else if(s[i] == 'x'){x += 6+m;}
else if(s[i] == 'y'){x += 6+m;}
else if(s[i] == 'z'){x += 5+m;}
else if(s[i] == '0'){x += 6+m;}
else if(s[i] == '1'){x += 4+m;}
else if(s[i] == '2'){x += 6+m;}
else if(s[i] == '3'){x += 6+m;}
else if(s[i] == '4'){x += 6+m;}
else if(s[i] == '5'){x += 6+m;}
else if(s[i] == '6'){x += 6+m;}
else if(s[i] == '7'){x += 6+m;}
else if(s[i] == '8'){x += 6+m;}
else if(s[i] == '9'){x += 6+m;}
else if(s[i] == ':'){x += 2+m;}
else if(s[i] == '.'){x += 2+m;}
else if(s[i] == '+'){x += 7+m;}
else if(s[i] == '-'){x += 7+m;}
else if(s[i] == ' '){x += 2+m;}
}
return x;
}
//*************************************
// update & render
//*************************************
void WOX_QUIT()
{
SDL_HideWindow(wnd);
saveState("");
drawText(NULL, "*K", 0, 0, 0);
SDL_FreeSurface(s_icon);
SDL_FreeSurface(sHud);
SDL_GL_DeleteContext(glc);
SDL_DestroyWindow(wnd);
SDL_Quit();
exit(0);
}
void WOX_POP(const int w, const int h)
{
winw = w;
winh = h;
winw2 = winw/2;
winh2 = winh/2;
doPerspective();
}
void main_loop()
{
//*************************************
// time delta for interpolation
//*************************************
static float lt = 0;
t = fTime();
const float dt = t-lt;
lt = t;
//*************************************
// input handling
//*************************************
// if user is idle for 3 minutes, save.
static float idle = 0.f;
if(idle != 0.f && t-idle > 180.f)
{
// char tmp[32];
// tmp[0] = '.';
// tmp[1] = 'i';
// tmp[2] = 'd';
// tmp[3] = 'l';