-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathmainmenu.cpp
2382 lines (1995 loc) · 60.2 KB
/
mainmenu.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
#include <math.h>
#include <string.h>
#include "mainmenu.h"
#include "enemygen.h"
#include "fast_rand.h"
#include "font1.h"
#include "gamepad.h"
#include "audio.h"
#include "platform.h"
#include "upng.h"
extern Game* game;
extern Terrain* terrain;
extern World* world;
extern Material mat[256];
extern char base_path[1024];
static int game_loading = 0; // 0-not_loaded, 1-loading, 2-loaded
static bool show_continue = false;
static bool show_gamepad = false;
static uint64_t mainmenu_stamp = 0;
static uint64_t dither_stamp = 0;
static bool mainmenu_shot = false;
static const int mainmenu_dither_hidden = 20;
static int mainmenu_dither = mainmenu_dither_hidden * 2;
extern Sprite* wolfie[2][ARMOR::SIZE][HELMET::SIZE][SHIELD::SIZE][WEAPON::SIZE];
extern Sprite* player[2][ARMOR::SIZE][HELMET::SIZE][SHIELD::SIZE][WEAPON::SIZE];
////////////////////////////////////////
static uint32_t* xxx_table = 0;
static uint32_t xxx_step = 0;
static uint32_t xxx_offs = 0;
static uint32_t xxx_size = 0;
static uint32_t xxx_size2 = 0;
static uint16_t* menu_bk_img=0;
static int menu_bk_width=0;
static int menu_bk_height=0;
static const int pal_size = 216;
static uint8_t pal[pal_size][3] = {{0}};
static uint8_t half_tone[2][216][216][3] = {{{{0}}}};
static Sprite* menu_logo_sprite = 0;
struct MainMenuContext;
struct MainMenu
{
const char* str; // if 0 this is terminator
const MainMenu* sub; // for terminator this is back menu
void (*action)(MainMenuContext* mmc);
bool (*getter)(MainMenuContext* mmc);
void* cookie;
};
static void ResetGame();
static void start_new_game(MainMenuContext* m)
{
if (game_loading)
ResetGame();
game_loading = 1;
}
static const MainMenu dummy_test2[] =
{
{"TEST 2A", 0, start_new_game, 0, /*cookie*/0},
{"TEST 2B", 0, start_new_game, 0, /*cookie*/0},
{"TEST 2C", 0, start_new_game, 0, /*cookie*/0},
{0}
};
static const MainMenu dummy_test1[] =
{
{"TEST 1A", dummy_test2, 0, 0, /*cookie*/0},
{"TEST 1B", dummy_test2, 0, 0, /*cookie*/0},
{"TEST 1C", dummy_test2, 0, 0, /*cookie*/0},
{"TEST 1D", dummy_test2, 0, 0, /*cookie*/0},
{"TEST 1E", dummy_test2, 0, 0, /*cookie*/0},
{0}
};
static const MainMenu dummy[] =
{
{"PRE Y9", 0, start_new_game, 0, /*cookie*/0},
{"TEST", dummy_test1, 0, 0, /*cookie*/0},
{0}
};
// here put parsed entries from the manifest
// it is referenced by some parent MainMenu element
// MainMenu { "title", 0, start_new_game, 0, manifest_cookie}
// static void main_menu_continue(MainMenu* m) { /* get cookie, load files, start new game */ }
static const MainMenu* main_menu_new_game = /*0*/ dummy;
static const MainMenu* MainMenuGetRoot();
struct MainMenuContext
{
int progress; // loading coarse progress in steps currently 0-3
PatchIndex* patch_index;
int patch_num;
int patch_iter;
int font_size[2]; // from OnSize
int input_size[2]; // from OnSize
int render_size[2]; // from Render
// menu context
int menu_stack[4]; // menu_stack[menu_depth] contains current item (hilight)
int menu_depth; // -1 when closed, 0 just after OpenMenu
// menu mouse / touch state
int menu_down; // 0: released, 1:mouse_captured, 2:touch_captured
bool down_back; // true if mouse or touch is holding 'back' item
int menu_down_x;
int menu_down_y;
// re-calc on every menu jump
int menu_scroll;
int menu_smooth_scroll;
int menu_max_scroll;
bool menu_rescroll; // flag it right after keyb/gamepad up/down navigation
// when mouse/touch is taking over, store current hilight here
// so we can revert hilight when pad/keyb is back
int menu_temp;
void Root(bool default_highlight)
{
if (menu_depth != 0)
mainmenu_dither = mainmenu_dither_hidden;
menu_scroll=0;
menu_smooth_scroll=0;
menu_depth=0;
menu_stack[menu_depth] = default_highlight ? 0 : -1;
menu_temp = menu_stack[menu_depth];
}
void Init()
{
progress = 0;
menu_max_scroll = 0;
menu_smooth_scroll = 0;
menu_scroll = 0;
menu_depth = 0;
menu_down = 0;
down_back = false;
menu_down_x = 0;
menu_down_y = 0;
menu_temp = 0;
memset(menu_stack,0,sizeof(menu_stack));
}
//void Open(int method);
//void Close();
//void Toggle(int method);
int CalcMaxScroll(int height) const
{
if (menu_depth<0)
return 0;
const MainMenu* m = MainMenuGetRoot();
const char* title = "";
for (int d=0; d<menu_depth; d++)
{
title = m[ menu_stack[d] ].str;
m = m[ menu_stack[d] ].sub;
}
if (!m[0].str)
return 0;
int y = height-15;
int w = 0, h = 0;
Font1Size(title,&w,&h);
if (title[0])
y -= h+2;
int i=1;
while(m[i].str)
{
y -= h+1;
i++;
}
return y < 0 ? -y : 0;
}
bool Paint(AnsiCell* ptr, int width, int height)
{
if (menu_depth<0)
{
// indicate we didn't take over logo space
return true;
}
menu_max_scroll = CalcMaxScroll(height);
if (menu_scroll > menu_max_scroll)
menu_scroll = menu_max_scroll;
if (menu_smooth_scroll > menu_max_scroll)
menu_smooth_scroll = menu_max_scroll;
if (menu_smooth_scroll < menu_scroll)
menu_smooth_scroll++;
if (menu_smooth_scroll > menu_scroll)
menu_smooth_scroll--;
const MainMenu* m = MainMenuGetRoot();
char title[32]="";
for (int d=0; d<menu_depth; d++)
{
sprintf(title,"\x04%s",m[ menu_stack[d] ].str);
//title = m[ menu_stack[d] ].str;
m = m[ menu_stack[d] ].sub;
}
// right align
int x = width-5;
int y = height-15;
const int font_clip_height = 5;
int scroll_clip_height = y + font_clip_height;
// paint title
if (title[0])
{
int w = 0, h = 0;
Font1Size(title,&w,&h);
Font1Paint(ptr,width,scroll_clip_height,3+x-w,y,title,FONT1_PINK_SKIN);
Font1UnderLine(ptr,width,scroll_clip_height,3+x-w,y,w,FONT1_PINK_SKIN);
y -= h+2;
scroll_clip_height = y + font_clip_height;
}
y += menu_smooth_scroll;
int i=0;
while(m[i].str)
{
int w = 0, h = 0;
Font1Size(m[i].str,&w,&h);
int skin = i == menu_stack[menu_depth] ? FONT1_GOLD_SKIN : FONT1_GREY_SKIN;
Font1Paint(ptr,width,scroll_clip_height,x-w,y,m[i].str,skin);
if (i == menu_stack[menu_depth] && menu_rescroll)
{
menu_rescroll = false;
// check if we should auto scroll
int sharp_y = y - menu_smooth_scroll + menu_scroll;
if (sharp_y<0)
menu_scroll += -sharp_y;
if (sharp_y+font_clip_height > scroll_clip_height)
menu_scroll -= sharp_y+font_clip_height - scroll_clip_height;
}
const char* str = 0;
if (m[i].sub)
str = "\x03";
else
if (m[i].getter)
str = m[i].getter(this) ? "\x02" : "\x01";
if (str)
Font1Paint(ptr,width,scroll_clip_height,x,y,str,FONT1_PINK_SKIN);
y -= h+1;
i++;
}
// indicate we didn't take over logo space
return true;
}
void ScreenToCell(int p[2]) const
{
p[0] = (2*p[0] - input_size[0] + render_size[0] * font_size[0]) / (2 * font_size[0]);
p[1] = (input_size[1]-1 - 2*p[1] + render_size[1] * font_size[1]) / (2 * font_size[1]);
}
int HitMenu(int hx, int hy)
{
if (menu_depth<0)
return -3;
int cp[2] = { hx, hy };
ScreenToCell(cp);
hx=cp[0];
hy=cp[1];
const MainMenu* m = MainMenuGetRoot();
char title[32]="";
for (int d=0; d<menu_depth; d++)
{
sprintf(title,"\x04%s",m[ menu_stack[d] ].str);
//title = m[ menu_stack[d] ].str;
m = m[ menu_stack[d] ].sub;
}
// right align
int x = render_size[0]-5;
int y = render_size[1]-15;
if (title[0])
{
int w = 0, h = 0;
Font1Size(title,&w,&h);
if (hx >= 3+x-w /*&& hx<3+x*/ && hy >=y && hy<y+h)
{
// title hit
return -1;
}
y -= h+2;
}
y += menu_smooth_scroll;
int i=0;
while(m[i].str)
{
int w = 0, h = 0;
Font1Size(m[i].str,&w,&h);
if (hx >= x-w /*&& hx < x*/ && hy>=y && hy<y+h)
{
// item hit
return i;
}
y -= h+1;
i++;
}
return -2;
}
// these things should call things above
void OnFocus(bool set);
void OnSize(int w, int h, int fw, int fh);
void OnKeyb(GAME_KEYB keyb, int key);
void OnMouse(GAME_MOUSE mouse, int x, int y);
void OnTouch(GAME_TOUCH touch, int id, int x, int y);
void OnPadMount(bool connected);
void OnPadButton(int b, bool down);
void OnPadAxis(int a, int16_t pos);
};
static MainMenuContext mainmenu_context = {0};
struct Manifest
{
const char* xp; // this should be embedded using --preload-file
const char* title; // short title (big font)
const char* desc; // long description (small font)
const char* a3d; // world file
const char* ajs; // game script
void* cookie; // this contains menu runtime data (loaded sprites etc. or ad cookie)
// if this is terminator, all fileds should be null
// if this is dir, a3d must be null and ajs must point to Manifest array of children
// if this is server based game, ajs must be null and a3d must contain address
// if this is "coming soon" / ad, both a3d and ajs must be null, cookie may point to url
/*
.ajs is required to initialize world with:
- ak.setWater (55)
- ak.setDir (0)
- ak.setYaw (45)
- ak.setPos (0,15,0)
- ak.setLight (1,0,1,.5)
*/
/*
.ajs optionally may hook these 2 to handle loading/saving game state
- function onRead(arrbuf) -> applies modifications stored in arrbuf to the world
- function onWrite() -> stores modified world state in array buffer, returns arrbuf
// - read will be called only during fresh page load -> CreateGame
// - write will be called on 'beforeunload' event or when process is about to
// terminate when there's game currently playing or is suspended by main menu
*/
};
char cookie_ad[] = "https://twitter.com/mrgumix";
static Manifest dev_toys_manifest_arr[]=
{
{
"dev_toy.xp",
"DEV TOY1",
"Example showing thing1, source: https://...dev_toy1",
"dev_toys.a3d",
"dev_toy1.ajs",
0 // cookie
},
{
"dev_toy.xp",
"DEV TOY2",
"Example showing thing2, source: https://...dev_toy2",
"dev_toys.a3d",
"dev_toy2.ajs",
0 // cookie
},
{
"dev_toy.xp",
"DEV TOY3",
"Example showing thing3, source: https://...dev_toy3",
"dev_toys.a3d",
"dev_toy3.ajs",
0 // cookie
},
{0} // terminator
};
static Manifest manifest[]=
{
{
"tutorial.xp",
"CONTROLS TUTORIAL ",
"Tutorial teaching you how to control the game",
"tutorial.a3d",
"tutorial.ajs",
0 // cookie
},
{
"y9.xp",
"Y9 DEMO",
"Latest official demo world containig few playable quest",
"game_map_y9.a3d",
"game_map_y9.ajs",
0 // cookie
},
{
"y9_online.xp",
"Y9 MULTIPLAYER DEMO",
"Latest official multiplayer demo",
"y9_server", // if ajs (below) is null, this is wss/endpoint
0, // real a3d and ajs files will be sent by server during joining
0 // cookie
},
{
"dev_toys.xp",
"DEV TOYS",
"Latest official dev toys",
0, // this is directory!
(const char*)dev_toys_manifest_arr, // and here are children
0 // cookie
},
{
"gumix.xp",
"GUMIX NEWS",
"",
0, // this
0, // is an ad
cookie_ad // with a cookie
},
{0} // terminator
};
struct Gamma
{
uint16_t dec[256]; // 0..8192 incl
uint8_t enc[8193]; // 0..255 incl
Gamma()
{
for (int i=0; i<256; i++)
{
double t = i / 255.0;
t = t >= 0.04045 ? pow((t+0.055)/1.055, 2.4) : t/12.92;
dec[i] = (uint16_t)round(t * 8192.0);
}
for (int i=0; i<=8192; i++)
{
double t = i / 8192.0;
t = t > 0.0031308 ? 1.055*pow(t, 1.0/2.4) - 0.055 : 12.92*t;
enc[i] = (uint8_t)round(255.0 * t);
}
}
};
static Gamma gamma_tables;
static void Bilinear(const uint16_t* src, int pitch, uint8_t x, uint8_t y, uint16_t* dst)
{
// NEAREST TEST
dst[0] = src[0];
dst[1] = src[1];
dst[2] = src[2];
return;
// +---------+---------+
// | src | src+3 |
// | R,G,B | R,G,B | < y=0
// | | |
// +---------+---------+ < y=128
// | src+p | src+3+p |
// | R,G,B | R,G,B | < y=256
// | | |
// +---------+---------+
// ^ ^ ^
// x=0 x=128 x=256
// src must be (dst will be) normalized to (0..8192 incl)
const uint16_t* lwr = src;
const uint16_t* upr = src + pitch;
const uint32_t qx = x;
const uint32_t qy = y;
const uint32_t px = 256-qx;
const uint32_t py = 256-qy;
const uint32_t r_ofs = 1<<15;
const uint32_t pypx = py * px;
const uint32_t pyqx = py * qx;
const uint32_t qypx = qy * px;
const uint32_t qyqx = qy * qx;
dst[0] = (pypx * lwr[0] + pyqx * lwr[3] + qypx * upr[0] + qyqx * upr[3] + r_ofs) >> 16;
dst[1] = (pypx * lwr[1] + pyqx * lwr[4] + qypx * upr[1] + qyqx * upr[4] + r_ofs) >> 16;
dst[2] = (pypx * lwr[2] + pyqx * lwr[5] + qypx * upr[2] + qyqx * upr[5] + r_ofs) >> 16;
}
static uint32_t Extract4(const uint16_t* c1, const uint16_t* c2, const uint16_t* c3, const uint16_t* c4)
{
const int xxx_3 = 3;
int i =
(gamma_tables.enc[(c1[0] + c2[0] + c3[0] + c4[0] + 2) >> 2] + xxx_offs) / xxx_3 +
(gamma_tables.enc[(c1[1] + c2[1] + c3[1] + c4[1] + 2) >> 2] + xxx_offs) / xxx_3 * xxx_size +
(gamma_tables.enc[(c1[2] + c2[2] + c3[2] + c4[2] + 2) >> 2] + xxx_offs) / xxx_3 * xxx_size2;
return xxx_table[ i ];
}
static uint32_t Extract2(const uint16_t* c1, const uint16_t* c2)
{
const int xxx_3 = 3;
int i =
(gamma_tables.enc[(c1[0] + c2[0]) >> 1] + xxx_offs) / xxx_3 +
(gamma_tables.enc[(c1[1] + c2[1]) >> 1] + xxx_offs) / xxx_3 * xxx_size +
(gamma_tables.enc[(c1[2] + c2[2]) >> 1] + xxx_offs) / xxx_3 * xxx_size2;
return xxx_table[ i ];
}
static void Accumulate(uint16_t a[3], const int16_t v[3])
{
a[0] = std::max(0, std::min(8192, a[0]+v[0] ));
a[1] = std::max(0, std::min(8192, a[1]+v[1] ));
a[2] = std::max(0, std::min(8192, a[2]+v[2] ));
}
#define DITHERING
static void ScaleImg(const uint16_t* src, int src_w, int src_h, const float src_xywh[4],
AnsiCell* dst, int dst_w, int dst_h, int dst_pitch=0)
{
const int src_pitch = src_w * 3;
#ifdef DITHERING
// DITHERING STUFF
int16_t e0[160][3] = {{0}};
int16_t e1[160][3] = {{0}};
int16_t e2[160][3] = {{0}};
// [0]-current line, [1]-next line, [2]-nextnext line
int16_t (*dither[3])[3] = {e0,e1,e2};
#endif
if (dst_pitch<=0)
dst_pitch = dst_w;
// offset start pos by +half dst px and -half src px
const int sx = (int)round(256.0 * src_xywh[0] + 128.0 * src_xywh[2] / (2*dst_w) - 128);
const int sy = (int)round(256.0 * src_xywh[1] + 128.0 * src_xywh[3] / (2*dst_h) - 128);
const int dx = (int)round(256.0 * src_xywh[2] / (2*dst_w));
const int dy = (int)round(256.0 * src_xywh[3] / (2*dst_h));
// for enlarging near src edges, or arbitrary src_rect (partially outside src image)
// we'd need also to handle sampling outside src img !!!
// that's the reason to keep src_w, src_h for clamping
int cy1 = sy;
for (int y=0; y<dst_h; y++)
{
int cx1 = sx;
int cy2 = cy1+dy;
cy1 = sy + (int)round(256.0 * src_xywh[3] * (2 * y + 0) / (2*dst_h));
cy2 = sy + (int)round(256.0 * src_xywh[3] * (2 * y + 1) / (2*dst_h));
uint8_t ry1 = cy1 & 0xFF;
const uint16_t* lwr = src + src_pitch * (cy1 >> 8);
uint8_t ry2 = cy2 & 0xFF;
const uint16_t* upr = src + src_pitch * (cy2 >> 8);
AnsiCell* ptr = dst + y * dst_pitch;
for (int x=0; x<dst_w; x++)
{
int cx2 = cx1+dx;
cx1 = sx + (int)round(256.0 * src_xywh[2] * (2 * x + 0) / (2*dst_w));
cx2 = sx + (int)round(256.0 * src_xywh[2] * (2 * x + 1) / (2*dst_w));
uint8_t rx1 = cx1 & 0xFF;
uint8_t rx2 = cx2 & 0xFF;
if (!((cx1>>8)>=0 && (cx1>>8)<src_w &&
(cx2>>8)>=0 && (cx2>>8)<src_w &&
(cy1>>8)>=0 && (cy1>>8)<src_h &&
(cy2>>8)>=0 && (cy2>>8)<src_h))
{
printf("PROBLEM AT X=%d, Y=%d, (%d,%d)\n", x,y,2*x,2*y);
printf("DST W=%d, H=%d, (%d,%d)\n", dst_w,dst_h,2*dst_w,2*dst_h);
printf("cx1: %d.%d , cx2: %d.%d , cy1: %d.%d , cy2: %d.%d\n",
cx1>>8,cx1&0xff, cx2>>8,cx1&0xff, cy1>>8,cx1&0xff, cy2>>8,cx1&0xff);
printf("src_xywh: %f , %f , %f , %f\n",
src_xywh[0],src_xywh[1],src_xywh[2],src_xywh[3]);
printf("sx: %d , sy: %d , dx: %d , dy: %d\n",
sx,sy,dx,dy);
assert(0);
}
uint16_t LL[3], LR[3], UL[3], UR[3];
Bilinear(lwr + (cx1 >> 8)*3, src_pitch, rx1,ry1, LL);
Bilinear(lwr + (cx2 >> 8)*3, src_pitch, rx2,ry1, LR);
Bilinear(upr + (cx1 >> 8)*3, src_pitch, rx1,ry2, UL);
Bilinear(upr + (cx2 >> 8)*3, src_pitch, rx2,ry2, UR);
// read & apply errors with clamping
#ifdef DITHERING
Accumulate(LL, dither[0][x]);
Accumulate(LR, dither[0][x]);
Accumulate(UL, dither[0][x]);
Accumulate(UR, dither[0][x]);
// reset
dither[0][x][0] = 0;
dither[0][x][1] = 0;
dither[0][x][2] = 0;
#endif
// we have 4 filtered samples, let's ANSIfy them into the single cell
// calc 4 encoded reference colors (for calcing errors)
int ll[3] ={gamma_tables.enc[LL[0]],gamma_tables.enc[LL[1]],gamma_tables.enc[LL[2]]};
int lr[3] ={gamma_tables.enc[LR[0]],gamma_tables.enc[LR[1]],gamma_tables.enc[LR[2]]};
int ul[3] ={gamma_tables.enc[UL[0]],gamma_tables.enc[UL[1]],gamma_tables.enc[UL[2]]};
int ur[3] ={gamma_tables.enc[UR[0]],gamma_tables.enc[UR[1]],gamma_tables.enc[UR[2]]};
// now reconstruct rgb values from the palette
uint32_t l_slot = Extract2(LL,UL);
uint32_t r_slot = Extract2(LR,UR);
uint32_t b_slot = Extract2(LL,LR);
uint32_t t_slot = Extract2(UL,UR);
uint32_t d_slot = Extract4(LL,LR,UL,UR);
const uint8_t* l = pal[(l_slot>>16) & 0xFF];
const uint8_t* r = pal[(r_slot>>16) & 0xFF];
const uint8_t* b = pal[(b_slot>>16) & 0xFF];
const uint8_t* t = pal[(t_slot>>16) & 0xFF];
const uint8_t* d = half_tone[d_slot>>24][d_slot&0xFF][(d_slot>>8)&0xFF];
// calc errors
int lr_err =
2*(std::abs(l[0] - ll[0]) + std::abs(l[0] - ul[0]) + std::abs(r[0] - lr[0]) + std::abs(r[0] - ur[0])) +
3*(std::abs(l[1] - ll[1]) + std::abs(l[1] - ul[1]) + std::abs(r[1] - lr[1]) + std::abs(r[1] - ur[1])) +
1*(std::abs(l[2] - ll[2]) + std::abs(l[2] - ul[2]) + std::abs(r[2] - lr[2]) + std::abs(r[2] - ur[2]));
int bt_err =
2*(std::abs(b[0] - ll[0]) + std::abs(b[0] - lr[0]) + std::abs(t[0] - ul[0]) + std::abs(t[0] - ur[0])) +
3*(std::abs(b[1] - ll[1]) + std::abs(b[1] - lr[1]) + std::abs(t[1] - ul[1]) + std::abs(t[1] - ur[1])) +
1*(std::abs(b[2] - ll[2]) + std::abs(b[2] - lr[2]) + std::abs(t[2] - ul[2]) + std::abs(t[2] - ur[2]));
int ht_err =
2*(std::abs(d[0] - ll[0]) + std::abs(d[0] - lr[0]) + std::abs(d[0] - ul[0]) + std::abs(d[0] - ur[0])) +
3*(std::abs(d[1] - ll[1]) + std::abs(d[1] - lr[1]) + std::abs(d[1] - ul[1]) + std::abs(d[1] - ur[1])) +
1*(std::abs(d[2] - ll[2]) + std::abs(d[2] - lr[2]) + std::abs(d[2] - ul[2]) + std::abs(d[2] - ur[2]));
#ifdef DITHERING
int32_t dev[3] =
{
LL[0]+LR[0]+UL[0]+UR[0],
LL[1]+LR[1]+UL[1]+UR[1],
LL[2]+LR[2]+UL[2]+UR[2]
};
#endif
// pick best and calculate deviations
if (ht_err < lr_err && ht_err < bt_err)
{
#ifdef DITHERING
dev[0] -= 4 * gamma_tables.dec[d[0]];
dev[1] -= 4 * gamma_tables.dec[d[1]];
dev[2] -= 4 * gamma_tables.dec[d[2]];
#endif
dst->fg = ((d_slot>>8) & 0xFF) + 16;
dst->bk = (d_slot & 0xFF) + 16;
dst->gl = (d_slot>>24) + 176;
dst->spare = 0;
}
else
if (bt_err < lr_err)
{
#ifdef DITHERING
dev[0] -= 2 * (gamma_tables.dec[b[0]] + gamma_tables.dec[t[0]]);
dev[1] -= 2 * (gamma_tables.dec[b[1]] + gamma_tables.dec[t[1]]);
dev[2] -= 2 * (gamma_tables.dec[b[2]] + gamma_tables.dec[t[2]]);
#endif
dst->fg = ((b_slot>>16) & 0xFF) + 16;
dst->bk = ((t_slot>>16) & 0xFF) + 16;
dst->gl = 220;
dst->spare = 0;
}
else
{
#ifdef DITHERING
dev[0] -= 2 * (gamma_tables.dec[l[0]] + gamma_tables.dec[r[0]]);
dev[1] -= 2 * (gamma_tables.dec[l[1]] + gamma_tables.dec[r[1]]);
dev[2] -= 2 * (gamma_tables.dec[l[2]] + gamma_tables.dec[r[2]]);
#endif
dst->fg = ((l_slot>>16) & 0xFF) + 16;
dst->bk = ((r_slot>>16) & 0xFF) + 16;
dst->gl = 221;
dst->spare = 0;
}
// finaly distribute deviations
#ifdef DITHERING
dev[0] /= 32;
dev[1] /= 32;
dev[2] /= 32;
if (x<dst_w-1)
{
dither[0][x+1][0] += dev[0];
dither[0][x+1][1] += dev[1];
dither[0][x+1][2] += dev[2];
if (x<dst_w-2)
{
dither[0][x+2][0] += dev[0];
dither[0][x+2][1] += dev[1];
dither[0][x+2][2] += dev[2];
}
}
if (y<dst_h-1)
{
dither[1][x][0] += dev[0];
dither[1][x][1] += dev[1];
dither[1][x][2] += dev[2];
if (x>0)
{
dither[1][x-1][0] += dev[0];
dither[1][x-1][1] += dev[1];
dither[1][x-1][2] += dev[2];
}
if (x<dst_w-1)
{
dither[1][x+1][0] += dev[0];
dither[1][x+1][1] += dev[1];
dither[1][x+1][2] += dev[2];
}
if (y<dst_h-2)
{
dither[2][x][0] += dev[0];
dither[2][x][1] += dev[1];
dither[2][x][2] += dev[2];
}
}
#endif
cx1 = cx2 + dx;
dst++;
}
cy1 = cy2 + dy;
#ifdef DITHERING
int16_t (*roll)[3] = dither[0];
dither[0] = dither[1];
dither[1] = dither[2];
dither[2] = roll;
#endif
}
}
static void FreeImg(uint16_t* img)
{
free(img);
}
static uint16_t* LoadImg(const char* path, int* w, int* h)
{
upng_t* upng = upng_new_from_file(path);
if (!upng)
return 0;
if (upng_get_error(upng) != UPNG_EOK)
{
upng_free(upng);
return 0;
}
if (upng_header(upng) != UPNG_EOK)
{
upng_free(upng);
return 0;
}
int format, width, height, depth;
format = upng_get_format(upng);
width = upng_get_width(upng);
height = upng_get_height(upng);
if (format != UPNG_RGB8)
{
upng_free(upng);
return 0;
}
if (upng_decode(upng) != UPNG_EOK)
{
upng_free(upng);
return 0;
}
const uint8_t* buf = upng_get_buffer(upng);
// allocate extra row and 1 px so Bilinear sampler won't overflow
int wh3 = (width*(height+1) + 1)*3;
uint16_t* pix = (uint16_t*)malloc(wh3*sizeof(uint16_t));
// reflect vertically and decode gamma!
for (int i=0,y=0; y<height; y++)
{
int j = (height - y - 1) * width * 3;
for (int x=0; x<width; x++, i+=3, j+=3)
{
pix[j+0] = gamma_tables.dec[buf[i+0]];
pix[j+1] = gamma_tables.dec[buf[i+1]];
pix[j+2] = gamma_tables.dec[buf[i+2]];
}
}
*w = width;
*h = height;
upng_free(upng);
return pix;
}
extern "C" void *tinfl_decompress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, size_t *pOut_len, int flags);
int LoadMainMenuSprites(const char* base_path)
{
// init palette entries
/*
FILE* ppp = fopen("666.gpl","wb");
fprintf(ppp,"GIMP Palette\n");
fprintf(ppp,"Name: 666\n");
fprintf(ppp,"\n");
fprintf(ppp,"#");
*/
for (int i=0; i<pal_size; i++)
{
int j = i;
pal[i][2] = j%6*51; j /= 6;
pal[i][1] = j%6*51; j /= 6;
pal[i][0] = j%6*51; j /= 6;
//fprintf(ppp,"%3d %3d %3d mycolor %d\n",pal[i][0],pal[i][1],pal[i][2],i);
}
//fclose(ppp);
// init half_tone mapper
for (int gl=1; gl<3; gl++)
{
int g = gl-1;
int c0_w = 4 - gl;
int c1_w = gl;
for (int c0=0; c0<216; c0++)
{
for (int c1=0; c1<216; c1++)
{
for (int c=0; c<3; c++)
{
half_tone[g][c0][c1][c] =
gamma_tables.enc[(
c0_w * gamma_tables.dec[pal[c0][c]] +
c1_w * gamma_tables.dec[pal[c1][c]] + 2) >> 2 ];
}
}
}
}
// load inverse palettizer
char path[1024];
sprintf(path,"%spalettes/palette.gz", base_path);
FILE* f = fopen(path, "rb");
if (!f)
return 0;
/////////////////////////////////
// GZ INTRO:
struct GZ
{
uint8_t id1, id2, cm, flg;
uint8_t mtime[4];
uint8_t xfl, os;
};
GZ gz;
int r;
r=(int)fread(&gz, 10, 1, f);
/*
assert(gz.id1 == 31 && gz.id2 == 139 && "gz identity");
assert(gz.cm == 8 && "deflate method");
*/
if (gz.id1 != 31 || gz.id2 != 139 || gz.cm != 8)
{
fclose(f);
return 0;
}
if (gz.flg & (1 << 2/*FEXTRA*/))
{
int hi, lo;
r=(int)fread(&hi, 1, 1, f);
r=(int)fread(&lo, 1, 1, f);
int len = (hi << 8) | lo;
fseek(f, len, SEEK_CUR);
}
if (gz.flg & (1 << 3/*FNAME*/))
{
uint8_t ch;
do
{
ch = 0;
r=(int)fread(&ch, 1, 1, f);
} while (ch);
}
if (gz.flg & (1 << 4/*FCOMMENT*/))
{
uint8_t ch;
do
{
ch = 0;
r=(int)fread(&ch, 1, 1, f);
} while (ch);
}
if (gz.flg & (1 << 1/*FFHCRC*/))