-
Notifications
You must be signed in to change notification settings - Fork 26
/
gamepad.cpp
2090 lines (1763 loc) · 46.3 KB
/
gamepad.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 <string.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include "sprite.h"
#include "gamepad.h"
#include "fast_rand.h"
#if 0 // testing with no gamepad connected
static int nopad()
{
ConnectGamePad("NO GAMEPAD", 6, 15, 0);
return 1;
}
static int no_gamepad = nopad();
#endif
void (*gamepad_close_cb)(void* g) = 0;
void* gamepad_close_g = 0;
extern char base_path[1024];
static Sprite* gamepad_sprite = 0;
static char gamepad_name[256] = "";
static int gamepad_axes = 0;
static int gamepad_buttons = 0;
static bool gamepad_connected = false;
static int gamepad_contact = -1;
static int gamepad_contact_from[2] = {0,0};
static int gamepad_contact_pos[2] = {0,0};
static uint8_t gamepad_contact_output = 0xFF;
static uint8_t gamepad_contact_ui = 0xFF;
// inverse maps - dependences, 0xFF terminated!
// rebuild them every time mapping changes
static uint8_t* button_mapping[15] = {0};
static uint8_t* axis_mapping[6] = {0};
// currently defined mapping
static uint8_t gamepad_mount_mapping[256] = { 0 };
static uint8_t gamepad_mapping[256] = {0};
static int16_t gamepad_input[256]; // all are positive!
// cache for determining if event triggers is neccessary
// also for painting mapped gamepad state
static int16_t gamepad_axis_output[6] = {0};
static int16_t gamepad_button_output[15] = {0};
// used for painting raw inputs
static int16_t gamepad_axis[256] = { 0 };
static int16_t gamepad_button[256] = { 0 };
// input slot positions
static int16_t gamepad_input_xy[2*256] = { 0 };
// upper left corner of the layout
static int16_t gamepad_layout_x = 0;
static int16_t gamepad_layout_y = 0;
static uint8_t gamepad_keyb_focus = 0xFF; // no focus, pressing arrow will set it to 0
static uint8_t gamepad_keyb_edit = 0xFF; // not editing, enter->0x00, char->0x01
static char gamepad_keyb_char[2]; //
// output positions, relative to SPRITE (y is top to bottom!)
static const int16_t xbox_half_axis_xy[2*12] =
{
10,14, 16,14, 13,11, 13,17, 28,20, 34,20, 31,17, 31,23, 16,8,16,8, 34,8,34,8
};
static const int16_t xbox_button_xy[2*15] =
{
37,17, 40,14, 34,14, 37,11, 21,14, 25,12, 29,14, 13,14, 31,20, 13,9, 37,9, 19,17, 19,23, 16,20, 22,20
};
static const int16_t ps5_half_axis_xy[2*12] =
{
16,20, 22,20, 19,17, 19,23, 28,20, 34,20, 31,17, 31,23, 16,8,16,8, 34,8,34,8
};
static const int16_t ps5_button_xy[2*15] =
{
37,17, 40,14, 34,14, 37,11, 17,10, 25,19, 33,10, 19,20, 31,20, 13,9, 37,9, 13,11, 13,17, 10,14, 16,14
};
static const int16_t* gamepad_half_axis_xy = xbox_half_axis_xy;
static const int16_t* gamepad_button_xy = xbox_button_xy;
static int gamepad_assembly = 0; // 0:xbox, 1:ps5
static const char* gamepad_half_axis_name[]=
{
"Ll","Lr", "Lu","Ld", "Rl","Rr", "Ru","Rd", "Lt","Lt", "Rt","Rt", 0
};
static const char* gamepad_button_name[]=
{
"A ","B ","X ","Y ", "E ","G ","F ", "L ","R ", "Ls","Rs", "Du","Dd","Dl","Dr", 0
};
static const char* gamepad_special_name[] = { 0, "L-Joy", "R-Joy", "D-Pad" };
struct SpriteElem
{
int src_x, src_y; // position on atlas (top to bottom!)
int w, h; // element width, height
int dst_x, dst_y; // assembly shift (top to bottom!)
int dst_x2, dst_y2; // assembly shift (for alternate assembly)
};
struct InputElem
{
int src_x, src_y;
int w, h;
};
static const InputElem ui_proto[] =
{
{22,23, 7,3}, // clear (center)
{ 7,23, 6,3}, // init (left)
{38,23, 7,3}, // quit (right)
{43,56, 7,3}, // frame7, hilighting special in-slots
{41,53, 6,3}, // frame6
{41,1, 4,3}, // hilighting normal in-slots
};
static const InputElem axis_proto[] =
{
{ 1,40, 10,3 },
{ 1,43, 10,3 },
{ 1,46, 10,3 },
{ 11,40, 10,3 },
{ 11,43, 10,3 },
{ 11,46, 10,3 },
{ 21,40, 10,3 },
{ 21,43, 10,3 },
{ 21,46, 10,3 },
{0}
};
static const InputElem button_proto[] =
{
{1,50,10,3},
{1,53,10,3},
{1,56,10,3},
{11,50,10,3},
{11,53,10,3},
{11,56,10,3},
{21,50,10,3},
{21,53,10,3},
{21,56,10,3},
{0}
};
static const InputElem slot_proto[] =
{
{42,45, 4,3},
{42,41, 4,3}, // drag frame
{42,49, 4,3}, // prolong slot
{ 0 }
};
static const SpriteElem gamepad_proto[] =
{
{ 8,8, 35,19, 8,8 ,8,8}, // body
{ 4,27, 11,12, 4,20 ,4,20}, // l-handle
{ 36,27, 11,12, 36,20 ,36,20}, // r-handle
{ 1,1, 7,7, 10,11 ,16,17}, // l-stick bk
{ 33,51, 7,7, 28,17, 28,17 }, // r-stick bk
{ 26,28, 9,10, 15,16 ,9,10}, // d-pad
{ 16,28, 9,10, 33,10 ,33,10}, // abxy
// { 20,1, 11,6, 20,11, 20,11 }, // EFG
{ 1,26, 3,4, 20,13, 16,9}, // E
{ 1,30, 3,4, 28,13, 32,9}, // F
{ 1,34, 3,4, 24,11, 24,18}, // G
// overlays
// ...
// movables
{ 2,9, 5,5, 11,12, 17,18}, // l-stick
{ 34,39, 5,5, 29,18, 29,18 }, // r-stick
// movable overlays
{ 2,15, 5,5, 11,12, 17,18 }, // l-stick hilight
{ 34,45, 5,5, 29,18, 29,18 }, // r-stick hilight
// move to overlays ...
{ 46,6, 4,4, 15,19, 9,13 }, // DL
{ 46,10, 4,4, 20,19, 14,13}, // DR
{ 47,1, 3,4, 18,16, 12,10}, // DU
{ 47,14, 3,5, 18,21, 12,15}, // DD
{ 47,35, 3,4, 36,16, 36,16 }, // A
{ 47,39, 3,4, 39,13, 39,13 }, // B
{ 47,27, 3,4, 33,13, 33,13 }, // X
{ 47,31, 3,4, 36,10, 36,10 }, // Y
{ 47,43, 3,4, 20,13, 16,9 }, // E
{ 47,47, 3,4, 28,13, 32,9 }, // F
{ 47,51, 3,4, 24,11, 24,18 }, // G
// move to after stick bkgs
{ 12,1, 6,2, 12,7, 12,7}, // Lt
{ 2,21, 6,2, 12,7, 12,7}, // Lt hi
{ 33,1, 6,2, 33,7, 33,7}, // Rt
{ 43,21, 6,2, 33,7, 33,7}, // Rt hi
{ 11,3, 5,2, 11,8, 11,8}, // Ls
{ 1,23, 5,2, 11,8, 11,8}, // Ls hi
{ 35,3, 5,2, 35,8, 35,8}, // Rs
{ 45,23, 5,2, 35,8, 35,8}, // Rs hi
{ 0 },
{ 18,1, 15,6, 18,1, 18,10} // ps5 touchpad
};
const static int16_t gamepad_swap_xy[2] = { 16, 17 };
static uint64_t gamepad_swap_stamp = 0;
static void Swap(uint64_t stamp)
{
gamepad_assembly = gamepad_assembly^1;
if (gamepad_assembly==0)
{
gamepad_half_axis_xy = xbox_half_axis_xy;
gamepad_button_xy = xbox_button_xy;
}
else
{
gamepad_half_axis_xy = ps5_half_axis_xy;
gamepad_button_xy = ps5_button_xy;
}
gamepad_swap_stamp = stamp;
}
void LoadGamePad()
{
char path[1024+20];
const char* name = "gamepad.xp";
sprintf(path, "%ssprites/%s", base_path, name);
gamepad_sprite = LoadSprite(path, name);
}
void FreeGamePad()
{
FreeSprite(gamepad_sprite);
gamepad_sprite = 0;
}
const char* GetGamePad(int* axes, int* buttons)
{
if (!gamepad_connected)
return 0;
*axes = gamepad_axes;
*buttons = gamepad_buttons;
return gamepad_name;
}
static int UpdateAxisOutput(int a, uint32_t* out)
{
// a in [0..5]
// run inverse map of a
// accumulate input_values
// clamp to int16_t, compare with previous value, if different update and return out
uint8_t* dep = axis_mapping[a];
int accum = 0;
if (dep)
{
while (*dep != 0xFF)
{
uint8_t d = *dep;
int v = gamepad_input[d];
int m = gamepad_mapping[d];
if (m<0xFC)
{
// check hacked unsigned
int in = d>>1;
if (in < gamepad_axes && gamepad_mapping[2*in] == gamepad_mapping[2*in+1])
{
if ((m&0x40)==0)
{
accum += ((int)gamepad_input[2*in+1] - (int)gamepad_input[2*in] + 32768)/2;
}
else
{
// currently not possible to make such mapping
accum -= ((int)gamepad_input[2*in+1] - (int)gamepad_input[2*in] + 32768)/2;
}
}
else
{
if (m&0x40)
{
accum -= v;
}
else
{
accum += v;
}
}
}
else
if (m<0xFF)
{
int in = d>>1;
int hat = gamepad_axis[in];
// otherwise neutral, dont accum anything
if (hat > -32768)
{
// chromium bug work around
hat = (hat+32767)*7/8 - 32767;
float fa = hat*(float)M_PI/32767.0f;
switch(a)
{
case 0: // L-JOY-X
case 2: // R-JOY-X
accum -= (int)(32767*sinf(fa));
break;
case 1: // L-JOY-Y
case 3: // R-JOY-Y
accum += (int)(32767*cosf(fa));
break;
}
}
}
dep++;
}
}
if (accum < -32767)
accum = -32767;
if (accum > +32767)
accum = +32767;
if (accum != gamepad_axis_output[a])
{
gamepad_axis_output[a] = accum;
*out = (accum&0xffff) | (a<<24) | (0<<16);
return 1;
}
return 0;
}
static int UpdateButtonOutput(int b, uint32_t* out)
{
// b in [0..14]
uint8_t* dep = button_mapping[b];
int accum = 0;
if (dep)
{
while (*dep != 0xFF)
{
uint8_t d = *dep;
int v = gamepad_input[d];
int m = gamepad_mapping[d];
if (m<0xFC)
{
if (m&0x40)
accum -= v;
else
accum += v;
}
else
if (m<0xFF)
{
int in = d>>1;
int hat = gamepad_axis[in];
if (hat > -32768)
{
// chromium bug work around
hat = (hat+32767)*7/8 - 32767;
float fa = hat*(float)M_PI/32767.0f;
switch (b)
{
case 11: // up
{
int delta = (int)(32767*cosf(fa));
if (delta<0)
accum -= delta;
break;
}
case 12: // down
{
int delta = (int)(32767*cosf(fa));
if (delta>0)
accum += delta;
break;
}
case 13: // left
{
int delta = -(int)(32767*sinf(fa));
if (delta<0)
accum -= delta;
break;
}
case 14: // right
{
int delta = -(int)(32767*sinf(fa));
if (delta>0)
accum += delta;
break;
}
}
}
}
dep++;
}
}
if (accum < 0)
accum = 0;
if (accum > +32767)
accum = +32767;
if (accum != gamepad_button_output[b])
{
gamepad_button_output[b] = accum;
*out = (accum&0xffff) | (b<<24) | (1<<16);
return 1;
}
return 0;
}
int UpdateGamePadAxis(int a, int16_t v, uint32_t out[4])
{
if (gamepad_keyb_edit == 0xFF)
{
// if axis passes through - 16k / +16k treshold(in any direction)
// make keyb focus over this input
if (gamepad_axis[a] < 16384 && v >= 16384)
{
gamepad_keyb_focus = 2 * a + 1;
}
else
if (gamepad_axis[a] > -16384 && v <= -16384)
{
gamepad_keyb_focus = 2 * a;
}
}
gamepad_axis[a] = v;
int16_t neg = v<0 ? -v : 0;
int16_t pos = v>0 ? +v : 0;
// prevent wrap at -max flipping
if (neg==-32768)
neg=-32767;
int outs = 0;
uint8_t m = gamepad_mapping[2*a];
if (m<0xFF && m>=0xFC)
{
if (m == 0xFE)
{
outs += UpdateAxisOutput(0, out+outs);
outs += UpdateAxisOutput(1, out+outs);
}
else
if (m == 0xFD)
{
outs += UpdateAxisOutput(2, out+outs);
outs += UpdateAxisOutput(3, out+outs);
}
else
if (m == 0xFC)
{
outs += UpdateButtonOutput(11, out+outs);
outs += UpdateButtonOutput(12, out+outs);
outs += UpdateButtonOutput(13, out+outs);
outs += UpdateButtonOutput(14, out+outs);
}
}
else
{
if (gamepad_input[2*a+0] != neg)
{
gamepad_input[2*a+0] = neg;
uint8_t m = gamepad_mapping[2*a+0];
if (m < 0xFC)
{
// update negative part of input
if (m&0x80)
outs += UpdateButtonOutput(m&0x3F, out+outs);
else
outs += UpdateAxisOutput(m&0x3F, out+outs);
}
}
if (gamepad_input[2*a+1] != pos)
{
gamepad_input[2*a+1] = pos;
uint8_t m = gamepad_mapping[2*a+1];
if (m < 0xFC)
{
// update positive part of input
if (m&0x80)
outs += UpdateButtonOutput(m&0x3F, out+outs);
else
outs += UpdateAxisOutput(m&0x3F, out+outs);
}
}
}
return outs;
}
int UpdateGamePadButton(int b, int16_t v, uint32_t out[1])
{
if (gamepad_keyb_edit == 0xFF)
{
// if button passes through +16k treshold (in any direction)
// make keyb focus over this input
if (gamepad_button[b] < 16384 && v >= 16384)
{
gamepad_keyb_focus = 2 * gamepad_axes + b;
}
}
gamepad_button[b] = v;
int16_t pos = v>0 ? +v : 0;
int outs = 0;
if (gamepad_input[2*gamepad_axes + b] != pos)
{
gamepad_input[2*gamepad_axes + b] = pos;
uint8_t m = gamepad_mapping[2*gamepad_axes + b];
if (m < 0xFC)
{
if (m&0x80)
outs += UpdateButtonOutput(m&0x3F, out + outs);
else
outs += UpdateAxisOutput(m&0x3F, out + outs);
}
}
return outs;
}
static void InvertMap(int mappings)
{
// build dependency maps for outputs
int axis_len[6] = {0};
int button_len[15] = {0};
for (int i=0; i<mappings; i++)
{
uint8_t m = gamepad_mapping[i];
if (m<0xFC)
{
int j = m&0x3F;
if ((m&0x80) == 0)
{
// count unsigned once
if ((i&1)==0 || m!=gamepad_mapping[i-1])
axis_len[j]++;
}
else
button_len[j]++;
}
else
if (m<0xFF && (i&1)==0)
{
int j = 0xFF-m;
switch (j)
{
case 1: // l-joy
axis_len[0]++;
axis_len[1]++;
break;
case 2: // r-joy
axis_len[2]++;
axis_len[3]++;
break;
case 3: // d-pad
button_len[11]++;
button_len[12]++;
button_len[13]++;
button_len[14]++;
break;
}
}
}
for (int i=0; i<6; i++)
{
if (axis_len[i])
{
axis_mapping[i] = (uint8_t*)malloc(axis_len[i]+1);
axis_mapping[i][axis_len[i]] = 0xFF;
}
else
axis_mapping[i] = 0;
}
for (int i=0; i<15; i++)
{
if (button_len[i])
{
button_mapping[i] = (uint8_t*)malloc(button_len[i]+1);
button_mapping[i][button_len[i]] = 0xFF;
}
else
button_mapping[i] = 0;
}
memset(axis_len,0,sizeof(int)*6);
memset(button_len,0,sizeof(int)*15);
for (int i=0; i<mappings; i++)
{
uint8_t m = gamepad_mapping[i];
if (m<0xFC)
{
int j = m&0x3F;
if ((m&0x80) == 0)
{
// count unsigned once
if ((i&1)==0 || m!=gamepad_mapping[i-1])
axis_mapping[j][axis_len[j]++] = i;
}
else
button_mapping[j][button_len[j]++] = i;
}
else
if (m<0xFF && (i&1)==0)
{
int j = 0xFF-m;
switch (j)
{
case 1: // l-joy
axis_mapping[0][axis_len[0]++] = i;
axis_mapping[1][axis_len[1]++] = i;
break;
case 2: // r-joy
axis_mapping[2][axis_len[2]++] = i;
axis_mapping[3][axis_len[3]++] = i;
break;
case 3: // d-pad
button_mapping[11][button_len[11]++] = i;
button_mapping[12][button_len[12]++] = i;
button_mapping[13][button_len[13]++] = i;
button_mapping[14][button_len[14]++] = i;
break;
}
}
}
// update all outs
uint32_t spare = 0;
for (int a=0; a<6; a++)
UpdateAxisOutput(a,&spare);
for (int b=0; b<15; b++)
UpdateButtonOutput(b,&spare);
}
void ConnectGamePad(const char* name, int axes, int buttons, const uint8_t mapping[])
{
strcpy(gamepad_name, name);
gamepad_buttons = buttons;
gamepad_axes = axes;
gamepad_connected = true;
int mappings = 2*axes+buttons;
if (mapping)
{
memcpy(gamepad_mapping, mapping, mappings);
memcpy(gamepad_mount_mapping, mapping, mappings);
}
else
{
memset(gamepad_mapping, 0xFF, mappings);
memset(gamepad_mount_mapping, 0xFF, mappings);
}
memset(gamepad_axis_output, 0, sizeof(int16_t)*6);
memset(gamepad_button_output, 0, sizeof(int16_t)*15);
memset(gamepad_input, 0, sizeof(int16_t)*(2*axes+buttons));
memset(gamepad_axis, 0, sizeof(int16_t)*axes);
memset(gamepad_button, 0, sizeof(int16_t)*buttons);
InvertMap(mappings);
gamepad_contact = -1;
gamepad_keyb_focus = 0xFF; // no focus (pressing any arrow will )
gamepad_keyb_edit = 0xFF; // not editing, enter->0x00, char->0x01
gamepad_keyb_focus = 0; // show focus as encouragement for using keyb
}
void DisconnectGamePad()
{
for (int i=0; i<6; i++)
{
if (axis_mapping[i])
{
free(axis_mapping[i]);
axis_mapping[i] = 0;
}
}
for (int i=0; i<15; i++)
{
if (button_mapping[i])
{
free(button_mapping[i]);
button_mapping[i] = 0;
}
}
gamepad_name[0]=0;
gamepad_buttons = 0;
gamepad_axes = 0;
gamepad_connected = false;
gamepad_contact = -1;
gamepad_keyb_focus = 0xFF; // no focus (pressing any arrow will )
gamepad_keyb_edit = 0xFF; // not editing, enter->0x00, char->0x01
}
static bool CalcLayout(int width, int height, int layout[] /*ec,er,ew,eh*/);
static void BlitButton(AnsiCell* ptr, int width, int height, int x, int y, int w, int h, int b, int col, int row, int row_y, const int col_x[]);
void PaintGamePad(AnsiCell* ptr, int width, int height, uint64_t stamp)
{
if (!gamepad_sprite)
return;
int layout[5];
CalcLayout(width, height, layout);
int ec = layout[0];
int er = layout[1];
int ew = layout[2];
int eh = layout[3];
int rows_per_ec = layout[4];
Sprite::Frame* sf = gamepad_sprite->atlas;
int w = sf->width;
int h = sf->height;
/*
int x = (width - w) / 2;
int y = height - h + 8 - 4; // <- num of buttons, axes and screen height dependent !!!
*/
// upper left corner of layout
int x = (width - ew) / 2 -4; // -4 offset from sprite edge to handle
int y = height - (height - eh) / 2 +8-2; // +8 pad body from sprite's top, -2 trigger height
gamepad_layout_x = x;
gamepad_layout_y = y;
int elems = sizeof(gamepad_proto)/sizeof(SpriteElem) - 2; // -(separator+touchpad)
for (int e = 0; e < elems; e++)
{
// overlays
if (e == 10+2 && gamepad_button_output[7] <= 32767 / 2)
continue;
if (e == 11+2 && gamepad_button_output[8] <= 32767 / 2)
continue;
if (e == 12+2 && gamepad_button_output[13] <= 32767 / 2)
continue;
if (e == 13+2 && gamepad_button_output[14] <= 32767 / 2)
continue;
if (e == 14+2 && gamepad_button_output[11] <= 32767 / 2)
continue;
if (e == 15+2 && gamepad_button_output[12] <= 32767 / 2)
continue;
if (e == 16+2 && gamepad_button_output[0] <= 32767 / 2)
continue;
if (e == 17+2 && gamepad_button_output[1] <= 32767 / 2)
continue;
if (e == 18+2 && gamepad_button_output[2] <= 32767 / 2)
continue;
if (e == 19+2 && gamepad_button_output[3] <= 32767 / 2)
continue;
if (e == 20+2 && gamepad_button_output[4] <= 32767 / 2)
continue;
if (e == 21+2 && gamepad_button_output[6] <= 32767 / 2)
continue;
if (e == 22+2 && gamepad_button_output[5] <= 32767 / 2)
continue;
/*
if (e == 24 && gamepad_axis_output[4] <= 32767 / 2)
continue;
if (e == 26 && gamepad_axis_output[5] <= 32767 / 2)
continue;
*/
int clip_left = 0;
int clip_right = 0;
if (e == 24+2)
clip_right = (gamepad_proto[e].w-2) - gamepad_axis_output[4] * (gamepad_proto[e].w-2) / 32767;
if (e == 26+2)
clip_left = (gamepad_proto[e].w-2) - gamepad_axis_output[5] * (gamepad_proto[e].w-2) / 32767;
if (e == 28+2 && gamepad_button_output[9] <= 32767 / 2)
continue;
if (e == 30+2 && gamepad_button_output[10] <= 32767 / 2)
continue;
int dx, dy;
int clip[4];
int dx0,dy0;
if (gamepad_assembly == 0)
{
dx = x + gamepad_proto[e].dst_x;
dy = y - (gamepad_proto[e].dst_y + gamepad_proto[e].h - 1);
dx0 = x + gamepad_proto[e].dst_x2;
dy0 = y - (gamepad_proto[e].dst_y2 + gamepad_proto[e].h - 1);
}
else
{
dx = x + gamepad_proto[e].dst_x2;
dy = y - (gamepad_proto[e].dst_y2 + gamepad_proto[e].h - 1);
dx0 = x + gamepad_proto[e].dst_x;
dy0 = y - (gamepad_proto[e].dst_y + gamepad_proto[e].h - 1);
}
static const int swap_mask =
(1<<7) | (1<<8) | (1<<9) |
(1<<3) | (1<<5) | (1<<(8+2)) | (1<<(10+2)) | (1<<(12+2)) | (1<<(13+2)) | (1<<(14+2)) | (1<<(15+2)) |
(1<<(20+2)) | (1<<(21+2)) | (1<<(22+2));
if (gamepad_swap_stamp && (swap_mask & (1<<e)))
{
int weight = (int)((stamp - gamepad_swap_stamp) >> 14);
if (weight < 0)
weight = 0;
if (weight>=16)
gamepad_swap_stamp = 0;
else
{
dx = (dx * weight + dx0 * (16-weight)) / 16;
dy = (dy * weight + dy0 * (16-weight)) / 16;
}
}
//dx -= 3;
//dy += 5;
if (e == 8+2 || e == 10+2)
{
if (gamepad_axes > 0)
dx += gamepad_axis_output[0] * 2 / 22000;
if (gamepad_axes > 1)
dy -= gamepad_axis_output[1] * 2 / 22000;
}
if (e == 9+2 || e == 11+2)
{
if (gamepad_axes > 2)
dx += gamepad_axis_output[2] * 2 / 22000;
if (gamepad_axes > 3)
dy -= gamepad_axis_output[3] * 2 / 22000;
}
clip[0] = gamepad_proto[e].src_x + clip_left;
clip[1] = h - 1 - (gamepad_proto[e].src_y + gamepad_proto[e].h - 1);
clip[2] = clip[0] + gamepad_proto[e].w - clip_right;
clip[3] = clip[1] + gamepad_proto[e].h;
BlitSprite(ptr, width, height, sf, dx + clip_left, dy, clip);
if (e==4)
{
// after all bk items draw touchpad
// clipped to top = 11
e = elems+1;
if (gamepad_assembly == 0)
{
dx = x + gamepad_proto[e].dst_x;
dy = y - (gamepad_proto[e].dst_y + gamepad_proto[e].h - 1);
dx0 = x + gamepad_proto[e].dst_x2;
dy0 = y - (gamepad_proto[e].dst_y2 + gamepad_proto[e].h - 1);
}
else
{
dx = x + gamepad_proto[e].dst_x2;
dy = y - (gamepad_proto[e].dst_y2 + gamepad_proto[e].h - 1);
dx0 = x + gamepad_proto[e].dst_x;
dy0 = y - (gamepad_proto[e].dst_y + gamepad_proto[e].h - 1);
}
if (gamepad_swap_stamp)
{
int weight = (int)((stamp - gamepad_swap_stamp) >> 14);
if (weight < 0)
weight = 0;
if (weight>=16)
gamepad_swap_stamp = 0;
else
{
dx = (dx * weight + dx0 * (16-weight)) / 16;
dy = (dy * weight + dy0 * (16-weight)) / 16;
}
}
int clip_top = dy - (y - (gamepad_proto[e].dst_y2 + gamepad_proto[e].h - 1));
if (clip_top>=0)
{
clip[0] = gamepad_proto[e].src_x;
clip[1] = h - 1 - (gamepad_proto[e].src_y + gamepad_proto[e].h - 1);
clip[2] = clip[0] + gamepad_proto[e].w;
clip[3] = clip[1] + gamepad_proto[e].h - clip_top;
BlitSprite(ptr, width, height, sf, dx, dy, clip);
}
e = 4;
}
}
int col_x[] = { 4,17,30, 45,58,71,84 };
if (ec>0)
{
// recenter 3 columns, they are
col_x[0]+=2;
col_x[1]+=2;
col_x[2]+=2;
}
int row_y = 35 - (8-rows_per_ec)*3;
if (ec)
row_y -= 3*rows_per_ec;
int row = 0;
int col = 2+ec;
/*
static int t = 0;
t++;
if (t == 100)
t = 0;
*/
for (int a = 0; a < gamepad_axes; a++)
{
// all in col=2
//int v = sinf((a*10 + t)*2*M_PI / 100) * 32767;
int v = gamepad_axis[a];
int i = v * 9 / 2 / 32767 +4;
int dx, dy;
int clip[4];
dx = x + col_x[col];
dy = y - (row_y + row * 3);