forked from msokalski/asciicker
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
asciiid.cpp
8285 lines (6782 loc) · 193 KB
/
asciiid.cpp
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
// nvbug.cpp : Defines the entry point for the console application.
//
#define NOMINMAX // windows fix
#include <wchar.h>
#include <stdio.h>
#include <algorithm>
#ifdef __linux__
#include <linux/limits.h>
#elif defined(__APPLE__)
#include <limits.h>
#else
#define PATH_MAX 1024
#endif
#define _USE_MATH_DEFINES
#include <math.h>
#include <stdint.h>
#include <stdlib.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "gl.h"
#include "gl45_emu.h"
#include "rgba8.h"
#include "imgui/imgui.h"
#include "imgui/imgui_internal.h" // beta: ImGuiItemFlags_Disabled
#include "imgui_impl_opengl3.h"
#include "platform.h"
#include "texheap.h"
#include "terrain.h"
#include "world.h"
#include "sprite.h"
#include "urdo.h"
#include "matrix.h"
#include "fast_rand.h"
#include "term.h"
#include "render.h"
#include "game.h"
#include "enemygen.h"
char base_path[1024] = "./";
Sprite* enemygen_sprite = 0;
void akAPI_Exec(const char* str, int len, bool root)
{
}
void Buzz()
{
}
void SyncConf()
{
}
const char* GetConfPath()
{
// USER_DIR
return "asciicker.cfg";
}
Server* server = 0; // this is to fullfil game.cpp externs!
bool Server::Send(const uint8_t* ptr, int size)
{
return false;
}
void Server::Proc()
{
}
void Server::Log(const char* str)
{
}
// just for write(fd)
#ifndef _WIN32
#include <unistd.h>
#endif
#if 0
A3D_VT* term = 0;
#endif
#define MOUSE_QUEUE
#ifdef MOUSE_QUEUE
struct MouseQueue
{
int x, y;
MouseInfo mi;
};
int mouse_queue_len=0;
const int mouse_queue_size = 256; // enough to handle 15K samples / sec
MouseQueue mouse_queue[mouse_queue_size];
#endif
ImFont* pFont = 0;
char ini_path[4096];
Terrain* terrain = 0;
World* world = 0;
Mesh* active_mesh = 0;
Sprite* active_sprite = 0;
Sprite* item_preview_sprite = 0;
int active_item = 0;
bool hover_story_hover = false;
int hover_story_value = -1;
struct SpritePrefs
{
float yaw;
int anim;
int frame; // use only if all t[] are 0
int t[4]; // rep_first, rep_every_forward, rep_last, rep_every_backward
float height; // used to offset instance's above terrain when inserting (we should have similar thing for meshes)
bool rand_anim;
bool rand_frame;
bool rand_yaw;
};
struct MeshPrefs
{
// float pre_trans[3];
float scale_val[3];
float scale_rnd[3];
float rotate_locZ_val;
float rotate_locZ_rnd;
float rotate_XY_val[2];
float rotate_XY_rnd[2];
// float translate_val[3];
// float translate_rnd[3];
float rotate_align;
};
struct Merge
{
Terrain* _terrain;
World* _world;
static void CommitPatch(Patch* p, int x, int y, int view_flags, void* cookie)
{
Merge* mrg = (Merge*)cookie;
Patch* d = GetTerrainPatch(terrain, x / VISUAL_CELLS, y / VISUAL_CELLS);
uint16_t diag = 0;
if (!d)
{
// d = AddTerrainPatch(terrain, x / VISUAL_CELLS, y / VISUAL_CELLS, 0);
d = URDO_Create(terrain, x / VISUAL_CELLS, y / VISUAL_CELLS, 0);
URDO_Patch(d, true);
uint16_t* src = GetTerrainVisualMap(p);
uint16_t* dst = GetTerrainVisualMap(d);
memcpy(dst, src, sizeof(uint16_t)*VISUAL_CELLS*VISUAL_CELLS);
UpdateTerrainVisualMap(d);
diag = 1;
}
else
{
URDO_Patch(d, false);
}
uint16_t* src = GetTerrainHeightMap(p);
uint16_t* dst = GetTerrainHeightMap(d);
for (int i = 0, y = 0; y < HEIGHT_CELLS + 1; y++)
{
for (int x = 0; x < HEIGHT_CELLS + 1; x++,i++)
{
if (src[i] > dst[i])
{
dst[i] = src[i];
}
}
}
UpdateTerrainHeightMap(d);
if (diag)
{
URDO_Diag(d);
diag = GetTerrainDiag(p);
SetTerrainDiag(d, diag);
}
}
static void CommitSprite(Inst* inst, Sprite* s, float pos[3], float yaw, int anim, int frame, int reps[4], void* cookie)
{
assert(0);
}
static void CommitMesh(Mesh* m, double tm[16], void* cookie)
{
Merge* mrg = (Merge*)cookie;
double ttm[16];
memcpy(ttm, tm, sizeof(double) * 16);
ttm[12] += mrg->dx * VISUAL_CELLS;
ttm[13] += mrg->dy * VISUAL_CELLS;
char mesh_name[256];
GetMeshName(m, mesh_name, 256);
int flags = INST_USE_TREE | INST_VISIBLE;
Mesh* m2 = GetFirstMesh(world);
while (m2)
{
char mesh_name2[256];
GetMeshName(m2, mesh_name2, 256);
if (strcmp(mesh_name, mesh_name2) == 0)
{
//CreateInst(m2, flags, ttm, 0);
URDO_Create(m2, flags, ttm, -1/*dont merge story_id*/);
break;
}
m2 = GetNextMesh(m2);
}
}
int dx,dy;
// todo:
bool flip_x;
bool flip_y;
bool swap_xy;
};
Merge merge = { 0,0 };
float pos_x = 0, pos_y = 0, pos_z = 0;
void MergeCancel()
{
if (merge._terrain)
DeleteTerrain(merge._terrain);
merge._terrain = 0;
if (merge._world)
DeleteWorld(merge._world);
merge._world = 0;
}
static bool MeshScan(A3D_DirItem item, const char* name, void* cookie);
void MergeOpen(const char* path)
{
assert(!merge._terrain && !merge._world);
// URDO_Purge();
FILE* f = fopen(path, "rb");
if (f)
{
merge._terrain = LoadTerrain(f);
if (merge._terrain)
{
// skip mats
for (int i = 0; i < 256; i++)
{
MatCell skip[64];
if (fread(skip, 1, sizeof(MatCell) * 4 * 16, f) != sizeof(MatCell) * 4 * 16)
break;
}
merge._world = LoadWorld(f, true);
if (merge._world)
{
// reload meshes too
Mesh* m = GetFirstMesh(merge._world);
while (m)
{
char mesh_name[256];
GetMeshName(m, mesh_name, 256);
char obj_path[4096];
sprintf(obj_path, "%smeshes/%s", base_path, mesh_name);
if (!UpdateMesh(m, obj_path))
{
// what now?
// missing mesh file!
}
MeshPrefs* mp = (MeshPrefs*)malloc(sizeof(MeshPrefs));
memset(mp, 0, sizeof(MeshPrefs));
SetMeshCookie(m, mp);
m = GetNextMesh(m);
}
}
}
fclose(f);
}
if (!merge._terrain)
merge._terrain = CreateTerrain();
if (!merge._world)
merge._world = CreateWorld();
RebuildWorld(merge._world, true);
}
void MergeCommit()
{
URDO_Open();
merge.dx = (int)floor(pos_x / VISUAL_CELLS + 0.5);
merge.dy = (int)floor(pos_y / VISUAL_CELLS + 0.5);
if (merge._terrain)
{
int t[2];
GetTerrainBase(merge._terrain, t);
int o[2] = { t[0] - merge.dx, t[1] - merge.dy };
SetTerrainBase(merge._terrain, o);
QueryTerrain(merge._terrain, 0, 0, 0xAA, Merge::CommitPatch, &merge);
}
if (merge._world)
{
QueryWorldCB cb = { Merge::CommitMesh, Merge::CommitSprite };
QueryWorld(merge._world, 0, 0, &cb, &merge);
RebuildWorld(world, false);
}
URDO_Close();
MergeCancel();
}
int fonts_loaded = 0;
int palettes_loaded = 0;
GLuint pal_tex = 0;
uint8_t* ipal = 0;
void* GetMaterialArr();
void* GetPaletteArr();
void* GetFontArr();
struct MyMaterial : Material
{
static void Free()
{
glDeleteTextures(1,&tex);
}
static void Init()
{
MyMaterial* m = (MyMaterial*)GetMaterialArr();
uint8_t g[4] = {',',' ','!',' '};
uint8_t f[4] = {0xFF,0xA0,0x64,0x00};
for (int s=0; s<16; s++)
{
for (int r=0; r<4; r++)
{
m[0].shade[r][s].fg[0]=f[r];
m[0].shade[r][s].fg[1]=f[r];
m[0].shade[r][s].fg[2]=f[r];
m[0].shade[r][s].gl = g[r];
m[0].shade[r][s].bg[0]=0xCF;
m[0].shade[r][s].bg[1]=0xCF;
m[0].shade[r][s].bg[2]=0xCF;
m[0].shade[r][s].flags = 0;
}
}
for (int i = 1; i < 256; i++)
{
for (int r = 0; r < 4; r++)
{
for (int s = 0; s < 16; s++)
{
m[i].shade[r][s].bg[0] = fast_rand() & 0xFF;
m[i].shade[r][s].bg[1] = fast_rand() & 0xFF;
m[i].shade[r][s].bg[2] = fast_rand() & 0xFF;
m[i].shade[r][s].fg[0] = fast_rand() & 0xFF;
m[i].shade[r][s].fg[1] = fast_rand() & 0xFF;
m[i].shade[r][s].fg[2] = fast_rand() & 0xFF;
m[i].shade[r][s].gl = fast_rand() & 0xFF;
m[i].shade[r][s].flags = 0;
}
}
}
gl3CreateTextures(GL_TEXTURE_2D, 1, &tex);
gl3TextureStorage2D(tex, 1, GL_RGBA8UI, 128, 256);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
gl3TextureSubImage2D(tex, 0, 0, 0, 128, 256, GL_RGBA_INTEGER, GL_UNSIGNED_BYTE, m->shade );
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
gl3TextureParameteri2D(tex, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
gl3TextureParameteri2D(tex, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
gl3TextureParameteri2D(tex, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
gl3TextureParameteri2D(tex, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}
void Update()
{
MyMaterial* m = (MyMaterial*)GetMaterialArr();
int y = (int)(this-m);
// update this single material texture slice !
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
gl3TextureSubImage2D(tex, 0, 0, y, 128, 1, GL_RGBA_INTEGER, GL_UNSIGNED_BYTE, shade);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
}
static GLuint tex; // single texture for all materials 128x256
// althought we have only 16 cells, shade map has 7bits!
// that makes timed shading 8x more precise spatialy :)
// (last bit is left for elevation/transparency and depends on material mode)
// int time_scale; // -80..-1 , 0 , +1..+80
// TIMED SHADE_MAP EVALUATION:
/*
uint64_t time64_usec = a4dGetTime();
int cell; // = ???
if (time_scale == 0)
{
cell = (shade_map >> 3) &0xF;
}
else
{
int mul_arr[] = { 470, 431, 395, 462, 332, 304, 279, 256 };
int abs_scale;
int multiplier;
if (time_scale>0)
{
abs_scale = time_scale;
multiplier = mul_arr[(abs_scale+6)&7];
}
else
{
abs_scale = -time_scale;
multiplier = -mul_arr[(abs_scale+6)&7];
}
int shift = 30 - ( ( abs_scale + 6 ) >> 3 );
cell = (( time64_usec * multiplier + (shade_map << (shift-3)) ) >> shift ) & 0xF;
// so at every frame every material should cache (time64_usec * multiplier) >> (shift-3)
// then during shading cell is simply = ((mat_cache + shade_map) >> 3 ) & 0xF
}
*/
};
GLuint MyMaterial::tex = 0;
MyMaterial mat[256];
struct MyPalette
{
static void Init()
{
MyPalette* p = (MyPalette*)GetPaletteArr();
for (int j = 0; j < 256; j++)
for (int i = 0; i < 768; i++)
p[j].rgb[i] = fast_rand() & 0xFF;
}
static bool Scan(A3D_DirItem item, const char* name, void* cookie)
{
if (!(item&A3D_FILE))
return true;
char buf[4096];
snprintf(buf, 4095, "%s/%s", (char*)cookie, name);
buf[4095] = 0;
a3dLoadImage(buf, 0, MyPalette::Load);
return true;
}
static void Load(void* cookie, A3D_ImageFormat f, int w, int h, const void* data, int palsize, const void* palbuf)
{
if (palettes_loaded == 256)
return;
MyPalette* p = (MyPalette*)GetPaletteArr() + palettes_loaded;
uint32_t* buf = (uint32_t*)malloc(w*h * sizeof(uint32_t));
Convert_UI32_AABBGGRR(buf, f, w, h, data, palsize, palbuf);
// extract palette by sampling at centers of w/16 x h/16 patches
int hx = (w + 16) / 32;
int hy = (h + 16) / 32;
for (int y = 0; y < 16; y++)
{
int row = w * (y * h / 16 + hy) + hx;
for (int x = 0; x < 16; x++)
{
uint32_t rgb = buf[x * w / 16 + row];
p->rgb[3 * (x + y * 16) + 0] = rgb & 0xFF;
p->rgb[3 * (x + y * 16) + 1] = (rgb>>8) & 0xFF;
p->rgb[3 * (x + y * 16) + 2] = (rgb>>16) & 0xFF;
}
}
free(buf);
palettes_loaded++;
}
uint8_t rgb[3 * 256];
} pal[256];
static const uint16_t cp437[256] =
{
0x0000, 0x263A, 0x263B, 0x2665, 0x2666, 0x2663, 0x2660, 0x2022,
0x25D8, 0x25CB, 0x25D9, 0x2642, 0x2640, 0x266A, 0x266B, 0x263C,
0x25BA, 0x25C4, 0x2195, 0x203C, 0x00B6, 0x00A7, 0x25AC, 0x21A8,
0x2191, 0x2193, 0x2192, 0x2190, 0x221F, 0x2194, 0x25B2, 0x25BC,
0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027,
0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F,
0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F,
0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F,
0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057,
0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F,
0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067,
0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F,
0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077,
0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x2302,
0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x00E0, 0x00E5, 0x00E7,
0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x00EC, 0x00C4, 0x00C5,
0x00C9, 0x00E6, 0x00C6, 0x00F4, 0x00F6, 0x00F2, 0x00FB, 0x00F9,
0x00FF, 0x00D6, 0x00DC, 0x00A2, 0x00A3, 0x00A5, 0x20A7, 0x0192,
0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x00F1, 0x00D1, 0x00AA, 0x00BA,
0x00BF, 0x2310, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB,
0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556,
0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510,
0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F,
0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,
0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B,
0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,
0x03B1, 0x00DF, 0x0393, 0x03C0, 0x03A3, 0x03C3, 0x00B5, 0x03C4,
0x03A6, 0x0398, 0x03A9, 0x03B4, 0x221E, 0x03C6, 0x03B5, 0x2229,
0x2261, 0x00B1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00F7, 0x2248,
0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00FF
};
struct MyFont
{
static bool Scan(A3D_DirItem item, const char* name, void* cookie)
{
if (!(item&A3D_FILE))
return true;
char buf[4096];
snprintf(buf,4095,"%s/%s",(char*)cookie,name);
buf[4095]=0;
a3dLoadImage(buf, buf/*path as cookie*/, MyFont::Load);
return true;
}
static int Sort(const void* a, const void* b)
{
MyFont* fa = (MyFont*)a;
MyFont* fb = (MyFont*)b;
int qa = fa->width*fa->height;
int qb = fb->width*fb->height;
return qa - qb;
}
static void Free()
{
MyFont* fnt = (MyFont*)GetFontArr();
for (int i=0; i<fonts_loaded; i++)
{
glDeleteTextures(1,&fnt[i].tex);
}
}
static bool WritePSF(const char* path, int w, int h, uint32_t* buf, int shift)
{
FILE* f = fopen(path,"wb");
if (!f)
return false;
int cell_w = w>>4;
int cell_h = h>>4;
int chars = 256;
struct psf2_header
{
unsigned char magic[4];
unsigned int version;
unsigned int headersize; /* offset of bitmaps in file */
unsigned int flags;
unsigned int length; /* number of glyphs */
unsigned int charsize; /* number of bytes for each character */
unsigned int height, width; /* max dimensions of glyphs */
/* charsize = height * ((width + 7) / 8) */
};
psf2_header hdr =
{
{0x72,0xb5,0x4a,0x86},
0,
32,
1, // has unicode table
(unsigned)chars,
(unsigned)(cell_h * ((cell_w + 7)>>3)),
(unsigned)cell_h, (unsigned)cell_w
};
fwrite(&hdr,32,1,f);
int index = 0;
while (index<256)
{
int gx = index&15;
int gy = index>>4;
for (int y=0; y<cell_h; y++)
{
uint8_t byte = 0;
for (int x=0; x<cell_w; x++)
{
int px = gx*cell_w + x;
int py = gy*cell_h + y;
int s = x&7;
if ( (buf[px + py*w] >> shift) & 0x80 )
byte |= 128>>s;
if (x == cell_w-1)
fwrite(&byte, 1, 1, f);
else
if (s == 7)
{
fwrite(&byte, 1, 1, f);
byte = 0;
}
}
}
index++;
}
// unicode table
index = 0;
while (index<256)
{
int uni = cp437[index];
uint8_t utf[4];
int len;
if (uni<0x0080)
{
utf[0]=uni&0xFF;
len=1;
}
else
if (uni<0x0800)
{
utf[0] = 0xC0 | ( ( uni >> 6 ) & 0x1F );
utf[1] = 0x80 | ( uni & 0x3F );
len=2;
}
else
{
utf[0] = 0xE0 | ( ( uni >> 12 ) & 0x0F );
utf[1] = 0x80 | ( ( uni >> 6 ) & 0x3F );
utf[2] = 0x80 | ( uni & 0x3F );
len=3;
}
utf[len++] = 0xFF; // glyph term
fwrite(utf, 1, len, f);
index++;
}
fclose(f);
return true;
}
static bool WriteBDF(const char* path, int w, int h, uint32_t* buf, int shift)
{
FILE* f = fopen(path,"wb");
if (!f)
return false;
int cell_w = w>>4;
int cell_h = h>>4;
int chars = 256;
fprintf(f,"STARTFONT 2.1\n");
fprintf(f,"FONT -gumix-asciicker-medium-r-normal--%d-120-72-72-c-120-iso10646-1\n", cell_h);
fprintf(f,"SIZE %d 72 72\n", cell_h);
fprintf(f,"FONTBOUNDINGBOX %d %d 0 0\n", cell_w, cell_h);
fprintf(f,"STARTPROPERTIES 23\n");
fprintf(f,"ADD_STYLE_NAME \"\"\n");
fprintf(f,"AVERAGE_WIDTH 120\n");
fprintf(f,"CHARSET_ENCODING \"1\"\n");
fprintf(f,"CHARSET_REGISTRY \"ISO10646\"\n");
fprintf(f,"COPYRIGHT \"gumix\"\n");
fprintf(f,"FAMILY_NAME \"asciicker\"\n");
fprintf(f,"FOUNDRY \"gumix\"\n");
fprintf(f,"MIN_SPACE %d\n", cell_w);
fprintf(f,"NOTICE \"Licensed\"\n");
fprintf(f,"PIXEL_SIZE %d\n", cell_h);
fprintf(f,"POINT_SIZE 120\n");
fprintf(f,"QUAD_WIDTH %d\n", cell_w);
fprintf(f,"RESOLUTION_X 72\n");
fprintf(f,"RESOLUTION_Y 72\n");
fprintf(f,"SETWIDTH_NAME \"Normal\"\n");
fprintf(f,"SLANT \"R\"\n");
fprintf(f,"SPACING \"M\"\n");
fprintf(f,"WEIGHT 10\n");
fprintf(f,"WEIGHT_NAME \"Bold\"\n");
fprintf(f,"X_HEIGHT 10\n");
fprintf(f,"DEFAULT_CHAR 33\n");
fprintf(f,"FONT_DESCENT %d\n", 0);
fprintf(f,"FONT_ASCENT %d\n", cell_h);
fprintf(f,"ENDPROPERTIES\n");
fprintf(f,"CHARS %d\n", chars);
int index = 0;
while (index<256)
{
int gx = index&15;
int gy = index>>4;
fprintf(f,"STARTCHAR U+%04X\n", cp437[index]);
fprintf(f,"ENCODING %d\n", cp437[index]);
fprintf(f,"SWIDTH 500 0\n");
fprintf(f,"DWIDTH %d 0\n", cell_w);
fprintf(f,"BBX %d %d 0 0\n", cell_w, cell_h);
fprintf(f,"BITMAP\n");
for (int y=0; y<cell_h; y++)
{
uint8_t byte = 0;
for (int x=0; x<cell_w; x++)
{
int px = gx*cell_w + x;
int py = gy*cell_h + y;
int s = x&7;
if ( (buf[px + py*w] >> shift) & 0x80 )
byte |= 128>>s;
if (x == cell_w-1)
fprintf(f,"%02X\n",byte);
else
if (s == 7)
{
fprintf(f,"%02X",byte);
byte = 0;
}
}
}
fprintf(f,"ENDCHAR\n");
index++;
}
fprintf(f,"ENDFONT\n");
fclose(f);
return true;
}
static void Load(void* cookie, A3D_ImageFormat f, int w, int h, const void* data, int palsize, const void* palbuf)
{
if (fonts_loaded==256)
return;
MyFont* fnt = (MyFont*)GetFontArr() + fonts_loaded;
fnt->width = w;
fnt->height = h;
int ifmt = GL_RGBA8;
int fmt = GL_RGBA;
int type = GL_UNSIGNED_BYTE;
uint32_t* buf = (uint32_t*)malloc(w * h * sizeof(uint32_t));
uint8_t rgb[3] = { 0xff,0xff,0xff };
ConvertLuminance_UI32_LLZZYYXX(buf, rgb, f, w, h, data, palsize, palbuf);
char* path = (char*)cookie;
char export_path[1024];
sprintf(export_path,"%s.bdf",path);
WriteBDF(export_path, w,h,buf,24);
sprintf(export_path,"%s.psf",path);
WritePSF(export_path, w,h,buf,24);
gl3CreateTextures(GL_TEXTURE_2D, 1, &fnt->tex);
gl3TextureStorage2D(fnt->tex, 1, ifmt, w, h);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
gl3TextureSubImage2D(fnt->tex, 0, 0, 0, w, h, fmt, type, buf ? buf : data);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
float white_transp[4] = { 1,1,1,0 };
gl3TextureParameteri2D(fnt->tex, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
gl3TextureParameteri2D(fnt->tex, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
gl3TextureParameteri2D(fnt->tex, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
gl3TextureParameteri2D(fnt->tex, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
gl3TextureParameterfv2D(fnt->tex, GL_TEXTURE_BORDER_COLOR, white_transp);
/*
// if we want to filter font we'd have first to
// modify 3 things in font sampling by shader:
// - clamp uv to glyph boundary during sampling
// - fade result by distance normalized to 0.5 of texel
// between unclamped uv to clamping glyph boundary
// - use manual lod as log2(font_zoom)
int max_lod = 0;
while (!((w & 1) | (h & 1)))
{
max_lod++;
w >>= 1;
h >>= 1;
}
glGenerateTextureMipmap(fnt->tex);
glTextureParameteri(fnt->tex, GL_TEXTURE_MAX_LOD, max_lod);
*/
if (buf)
free(buf);
fonts_loaded++;
qsort(GetFontArr(), fonts_loaded, sizeof(MyFont), MyFont::Sort);
}
void SetTexel(int x, int y, uint8_t val)
{
uint8_t texel[4] = { 0xFF,0xFF,0xFF,val };
gl3TextureSubImage2D(tex, 0, x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, texel);
}
uint8_t GetTexel(int x, int y)
{
uint8_t texel[4];
gl3GetTextureSubImage(tex, 0, x, y, 0, 1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 4, texel);
return texel[3];
}
int width;
int height;
GLuint tex;
} font[256];
void* GetMaterialArr()
{
return mat;
}
void* GetPaletteArr()
{
return pal;
}
void* GetFontArr()
{
return font;
}
int active_font = 0;
int active_glyph = 0x40; //@
int active_palette = 0;
int active_material = 0;
int active_elev = 0;
// used by Term
int GetGLFont(int wh[2], const int wnd_wh[2], int* id)
{
MyFont* f = font + active_font;
if (wh)
{
wh[0] = f->width;
wh[1] = f->height;
}
if (id)
*id = active_font;
return f->tex;
}
bool PrevGLFont()
{
active_font--;
if (active_font < 0)
{
active_font = 0;
return false;
}
TermResizeAll();
return true;
}
bool NextGLFont()
{
active_font++;
if (active_font >= fonts_loaded)
{
active_font = fonts_loaded - 1;
return false;
}
TermResizeAll();
return true;
}
/*
float dawn_color[3] = { 1,.8f,0 };
float noon_color[3] = { 1,1,1 };
float dusk_color[3] = { 1,.2f,0 };
float midnight_color[3] = { .1f,.1f,.5f };
*/
float font_size = 10;// 0.125;// 16; // so every visual cell appears as 16px
float rot_yaw = 45;
float rot_pitch = 30;//90;
float global_lt[4] = { 0,0,1,0 };
float inst_yaw = 0.0;
bool inst_yaw_rnd = false;
float inst_pitch_avr = 0.0;
float inst_pitch_var = 0.0;
float inst_roll = 0.0;
bool inst_added = false;
float lit_yaw = 45;
float lit_pitch = 30;//90;
float lit_time = 12.0f;
float ambience = 0.5;
float grid_alpha = 1.0f;
bool spin_anim = false;
int mouse_in = 0;
int panning = 0;
int panning_x = 0;
int panning_y = 0;
double panning_dx = 0;
double panning_dy = 0;
float zoom_wheel = 0;
int spinning = 0;
int spinning_x = 0;
int spinning_y = 0;
int edit_mode = 0;
int creating = 0; // +1 = add, -1 = del
int painting = 0;
const float STAMP_R = 0.50;