forked from libretro/RetroArch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgfx_animation.c
2392 lines (2012 loc) · 68.5 KB
/
gfx_animation.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
/* RetroArch - A frontend for libretro.
* Copyright (C) 2014-2018 - Jean-André Santoni
* Copyright (C) 2011-2018 - Daniel De Matteis
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <math.h>
#include <string.h>
#include <compat/strl.h>
#include <encodings/utf.h>
#include <retro_math.h>
#include <retro_miscellaneous.h>
#include <string/stdstring.h>
#include <features/features_cpu.h>
#include <lists/string_list.h>
#define DG_DYNARR_IMPLEMENTATION
#include <retro_assert.h>
#define DG_DYNARR_ASSERT(cond, msg) (void)0
#include <array/dynarray.h>
#undef DG_DYNARR_IMPLEMENTATION
#include "gfx_animation.h"
#include "../performance_counters.h"
typedef float (*easing_cb) (float, float, float, float);
struct tween
{
float duration;
float running_since;
float initial_value;
float target_value;
float *subject;
uintptr_t tag;
easing_cb easing;
tween_cb cb;
void *userdata;
bool deleted;
};
DA_TYPEDEF(struct tween, tween_array_t)
struct gfx_animation
{
bool initialized;
bool pending_deletes;
bool in_update;
bool animation_is_active;
bool ticker_is_active;
uint64_t ticker_idx; /* updated every TICKER_SPEED us */
uint64_t ticker_slow_idx; /* updated every TICKER_SLOW_SPEED us */
uint64_t ticker_pixel_idx; /* updated every frame */
uint64_t ticker_pixel_line_idx; /* updated every frame */
retro_time_t cur_time;
retro_time_t old_time;
float delta_time;
tween_array_t list;
tween_array_t pending;
};
typedef struct gfx_animation gfx_animation_t;
#define TICKER_SPEED 333333
#define TICKER_SLOW_SPEED 1666666
/* Pixel ticker nominally increases by one after each
* ticker_pixel_period ms (actual increase depends upon
* ticker speed setting and display resolution) */
static const float ticker_pixel_period = (1.0f / 60.0f) * 1000.0f;
static const char ticker_spacer_default[] = TICKER_SPACER_DEFAULT;
static gfx_animation_t anim;
/* Forward declarations */
static void gfx_animation_update_time_default(
float *dst,
unsigned video_width, unsigned video_height);
static update_time_cb update_time_callback = gfx_animation_update_time_default;
/* from https://github.com/kikito/tween.lua/blob/master/tween.lua */
static gfx_animation_t *anim_get_ptr(void)
{
return &anim;
}
static float easing_linear(float t, float b, float c, float d)
{
return c * t / d + b;
}
static float easing_in_out_quad(float t, float b, float c, float d)
{
t = t / d * 2;
if (t < 1)
return c / 2 * pow(t, 2) + b;
return -c / 2 * ((t - 1) * (t - 3) - 1) + b;
}
static float easing_in_quad(float t, float b, float c, float d)
{
return c * pow(t / d, 2) + b;
}
static float easing_out_quad(float t, float b, float c, float d)
{
t = t / d;
return -c * t * (t - 2) + b;
}
static float easing_out_in_quad(float t, float b, float c, float d)
{
if (t < d / 2)
return easing_out_quad(t * 2, b, c / 2, d);
return easing_in_quad((t * 2) - d, b + c / 2, c / 2, d);
}
static float easing_in_cubic(float t, float b, float c, float d)
{
return c * pow(t / d, 3) + b;
}
static float easing_out_cubic(float t, float b, float c, float d)
{
return c * (pow(t / d - 1, 3) + 1) + b;
}
static float easing_in_out_cubic(float t, float b, float c, float d)
{
t = t / d * 2;
if (t < 1)
return c / 2 * t * t * t + b;
t = t - 2;
return c / 2 * (t * t * t + 2) + b;
}
static float easing_out_in_cubic(float t, float b, float c, float d)
{
if (t < d / 2)
return easing_out_cubic(t * 2, b, c / 2, d);
return easing_in_cubic((t * 2) - d, b + c / 2, c / 2, d);
}
static float easing_in_quart(float t, float b, float c, float d)
{
return c * pow(t / d, 4) + b;
}
static float easing_out_quart(float t, float b, float c, float d)
{
return -c * (pow(t / d - 1, 4) - 1) + b;
}
static float easing_in_out_quart(float t, float b, float c, float d)
{
t = t / d * 2;
if (t < 1)
return c / 2 * pow(t, 4) + b;
return -c / 2 * (pow(t - 2, 4) - 2) + b;
}
static float easing_out_in_quart(float t, float b, float c, float d)
{
if (t < d / 2)
return easing_out_quart(t * 2, b, c / 2, d);
return easing_in_quart((t * 2) - d, b + c / 2, c / 2, d);
}
static float easing_in_quint(float t, float b, float c, float d)
{
return c * pow(t / d, 5) + b;
}
static float easing_out_quint(float t, float b, float c, float d)
{
return c * (pow(t / d - 1, 5) + 1) + b;
}
static float easing_in_out_quint(float t, float b, float c, float d)
{
t = t / d * 2;
if (t < 1)
return c / 2 * pow(t, 5) + b;
return c / 2 * (pow(t - 2, 5) + 2) + b;
}
static float easing_out_in_quint(float t, float b, float c, float d)
{
if (t < d / 2)
return easing_out_quint(t * 2, b, c / 2, d);
return easing_in_quint((t * 2) - d, b + c / 2, c / 2, d);
}
static float easing_in_sine(float t, float b, float c, float d)
{
return -c * cos(t / d * (M_PI / 2)) + c + b;
}
static float easing_out_sine(float t, float b, float c, float d)
{
return c * sin(t / d * (M_PI / 2)) + b;
}
static float easing_in_out_sine(float t, float b, float c, float d)
{
return -c / 2 * (cos(M_PI * t / d) - 1) + b;
}
static float easing_out_in_sine(float t, float b, float c, float d)
{
if (t < d / 2)
return easing_out_sine(t * 2, b, c / 2, d);
return easing_in_sine((t * 2) -d, b + c / 2, c / 2, d);
}
static float easing_in_expo(float t, float b, float c, float d)
{
if (t == 0)
return b;
return c * powf(2, 10 * (t / d - 1)) + b - c * 0.001;
}
static float easing_out_expo(float t, float b, float c, float d)
{
if (t == d)
return b + c;
return c * 1.001 * (-powf(2, -10 * t / d) + 1) + b;
}
static float easing_in_out_expo(float t, float b, float c, float d)
{
if (t == 0)
return b;
if (t == d)
return b + c;
t = t / d * 2;
if (t < 1)
return c / 2 * powf(2, 10 * (t - 1)) + b - c * 0.0005;
return c / 2 * 1.0005 * (-powf(2, -10 * (t - 1)) + 2) + b;
}
static float easing_out_in_expo(float t, float b, float c, float d)
{
if (t < d / 2)
return easing_out_expo(t * 2, b, c / 2, d);
return easing_in_expo((t * 2) - d, b + c / 2, c / 2, d);
}
static float easing_in_circ(float t, float b, float c, float d)
{
return(-c * (sqrt(1 - powf(t / d, 2)) - 1) + b);
}
static float easing_out_circ(float t, float b, float c, float d)
{
return(c * sqrt(1 - powf(t / d - 1, 2)) + b);
}
static float easing_in_out_circ(float t, float b, float c, float d)
{
t = t / d * 2;
if (t < 1)
return -c / 2 * (sqrt(1 - t * t) - 1) + b;
t = t - 2;
return c / 2 * (sqrt(1 - t * t) + 1) + b;
}
static float easing_out_in_circ(float t, float b, float c, float d)
{
if (t < d / 2)
return easing_out_circ(t * 2, b, c / 2, d);
return easing_in_circ((t * 2) - d, b + c / 2, c / 2, d);
}
static float easing_out_bounce(float t, float b, float c, float d)
{
t = t / d;
if (t < 1 / 2.75)
return c * (7.5625 * t * t) + b;
if (t < 2 / 2.75)
{
t = t - (1.5 / 2.75);
return c * (7.5625 * t * t + 0.75) + b;
}
else if (t < 2.5 / 2.75)
{
t = t - (2.25 / 2.75);
return c * (7.5625 * t * t + 0.9375) + b;
}
t = t - (2.625 / 2.75);
return c * (7.5625 * t * t + 0.984375) + b;
}
static float easing_in_bounce(float t, float b, float c, float d)
{
return c - easing_out_bounce(d - t, 0, c, d) + b;
}
static float easing_in_out_bounce(float t, float b, float c, float d)
{
if (t < d / 2)
return easing_in_bounce(t * 2, 0, c, d) * 0.5 + b;
return easing_out_bounce(t * 2 - d, 0, c, d) * 0.5 + c * .5 + b;
}
static float easing_out_in_bounce(float t, float b, float c, float d)
{
if (t < d / 2)
return easing_out_bounce(t * 2, b, c / 2, d);
return easing_in_bounce((t * 2) - d, b + c / 2, c / 2, d);
}
static void gfx_animation_ticker_generic(uint64_t idx,
size_t max_width, size_t *offset, size_t *width)
{
int ticker_period = (int)(2 * (*width - max_width) + 4);
int phase = idx % ticker_period;
int phase_left_stop = 2;
int phase_left_moving = (int)(phase_left_stop + (*width - max_width));
int phase_right_stop = phase_left_moving + 2;
int left_offset = phase - phase_left_stop;
int right_offset = (int)((*width - max_width) - (phase - phase_right_stop));
if (phase < phase_left_stop)
*offset = 0;
else if (phase < phase_left_moving)
*offset = left_offset;
else if (phase < phase_right_stop)
*offset = *width - max_width;
else
*offset = right_offset;
*width = max_width;
}
static void gfx_animation_ticker_loop(uint64_t idx,
size_t max_width, size_t str_width, size_t spacer_width,
size_t *offset1, size_t *width1,
size_t *offset2, size_t *width2,
size_t *offset3, size_t *width3)
{
int ticker_period = (int)(str_width + spacer_width);
int phase = idx % ticker_period;
/* Output offsets/widths are unsigned size_t, but it's
* easier to perform the required calculations with ints,
* so create some temporary variables... */
int offset;
int width;
/* Looping text is composed of up to three strings,
* where string 1 and 2 are different regions of the
* source text and string 2 is a spacer:
*
* |-----max_width-----|
* [string 1][string 2][string 3]
*
* The following implementation could probably be optimised,
* but any performance gains would be trivial compared with
* all the string manipulation that has to happen afterwards...
*/
/* String 1 */
offset = (phase < (int)str_width) ? phase : 0;
width = (int)(str_width - phase);
width = (width < 0) ? 0 : width;
width = (width > (int)max_width) ? (int)max_width : width;
*offset1 = offset;
*width1 = width;
/* String 2 */
offset = (int)(phase - str_width);
offset = offset < 0 ? 0 : offset;
width = (int)(max_width - *width1);
width = (width > (int)spacer_width) ? (int)spacer_width : width;
width = width - offset;
*offset2 = offset;
*width2 = width;
/* String 3 */
width = (int)(max_width - (*width1 + *width2));
width = width < 0 ? 0 : width;
/* Note: offset is always zero here so offset3 is
* unnecessary - but include it anyway to preserve
* symmetry... */
*offset3 = 0;
*width3 = width;
}
static unsigned get_ticker_smooth_generic_scroll_offset(
uint64_t idx, unsigned str_width, unsigned field_width)
{
unsigned scroll_width = str_width - field_width;
unsigned scroll_offset = 0;
unsigned pause_duration = 32;
unsigned ticker_period = 2 * (scroll_width + pause_duration);
unsigned phase = idx % ticker_period;
/* Determine scroll offset */
if (phase < pause_duration)
scroll_offset = 0;
else if (phase < ticker_period >> 1)
scroll_offset = phase - pause_duration;
else if (phase < (ticker_period >> 1) + pause_duration)
scroll_offset = (ticker_period - (2 * pause_duration)) >> 1;
else
scroll_offset = ticker_period - phase;
return scroll_offset;
}
/* 'Fixed width' font version of ticker_smooth_scan_characters() */
static void ticker_smooth_scan_string_fw(
size_t num_chars, unsigned glyph_width, unsigned field_width, unsigned scroll_offset,
unsigned *char_offset, unsigned *num_chars_to_copy, unsigned *x_offset)
{
unsigned chars_remaining = 0;
/* Initialise output variables to 'sane' values */
*char_offset = 0;
*num_chars_to_copy = 0;
*x_offset = 0;
/* Determine index of first character to copy */
if (scroll_offset > 0)
{
*char_offset = (scroll_offset / glyph_width) + 1;
*x_offset = glyph_width - (scroll_offset % glyph_width);
}
/* Determine number of characters remaining in
* string once offset has been subtracted */
chars_remaining = (*char_offset >= num_chars) ? 0 : num_chars - *char_offset;
/* Determine number of characters to copy */
if ((chars_remaining > 0) && (field_width > *x_offset))
{
*num_chars_to_copy = (field_width - *x_offset) / glyph_width;
*num_chars_to_copy = (*num_chars_to_copy > chars_remaining) ? chars_remaining : *num_chars_to_copy;
}
}
/* 'Fixed width' font version of gfx_animation_ticker_smooth_generic() */
static void gfx_animation_ticker_smooth_generic_fw(uint64_t idx,
unsigned str_width, size_t num_chars,
unsigned glyph_width, unsigned field_width,
unsigned *char_offset, unsigned *num_chars_to_copy, unsigned *x_offset)
{
unsigned scroll_offset = get_ticker_smooth_generic_scroll_offset(
idx, str_width, field_width);
/* Initialise output variables to 'sane' values */
*char_offset = 0;
*num_chars_to_copy = 0;
*x_offset = 0;
/* Sanity check */
if (num_chars < 1)
return;
ticker_smooth_scan_string_fw(
num_chars, glyph_width, field_width, scroll_offset,
char_offset, num_chars_to_copy, x_offset);
}
/* 'Fixed width' font version of gfx_animation_ticker_smooth_loop() */
static void gfx_animation_ticker_smooth_loop_fw(uint64_t idx,
unsigned str_width, size_t num_chars,
unsigned spacer_width, size_t num_spacer_chars,
unsigned glyph_width, unsigned field_width,
unsigned *char_offset1, unsigned *num_chars_to_copy1,
unsigned *char_offset2, unsigned *num_chars_to_copy2,
unsigned *char_offset3, unsigned *num_chars_to_copy3,
unsigned *x_offset)
{
unsigned ticker_period = str_width + spacer_width;
unsigned phase = idx % ticker_period;
unsigned remaining_width = field_width;
/* Initialise output variables to 'sane' values */
*char_offset1 = 0;
*num_chars_to_copy1 = 0;
*char_offset2 = 0;
*num_chars_to_copy2 = 0;
*char_offset3 = 0;
*num_chars_to_copy3 = 0;
*x_offset = 0;
/* Sanity check */
if ((num_chars < 1) || (num_spacer_chars < 1))
return;
/* Looping text is composed of up to three strings,
* where string 1 and 2 are different regions of the
* source text and string 2 is a spacer:
*
* |----field_width----|
* [string 1][string 2][string 3]
*/
/* String 1 */
if (phase < str_width)
{
unsigned scroll_offset = phase;
ticker_smooth_scan_string_fw(
num_chars, glyph_width, remaining_width, scroll_offset,
char_offset1, num_chars_to_copy1, x_offset);
/* Update remaining width
* Note: We can avoid all the display_width shenanigans
* here (c.f. gfx_animation_ticker_smooth_loop()) because
* the font width is constant - i.e. we don't have to wrangle
* out the width of the last 'non-copied' character since it
* is known a priori, so we can just subtract the string width
* + offset here, and perform an 'if (remaining_width > glyph_width)'
* for strings 2 and 3 */
remaining_width -= (*x_offset + (*num_chars_to_copy1 * glyph_width));
}
/* String 2 */
if (remaining_width > glyph_width)
{
unsigned scroll_offset = 0;
unsigned x_offset2 = 0;
/* Check whether we've passed the end of string 1 */
if (phase > str_width)
scroll_offset = phase - str_width;
else
scroll_offset = 0;
ticker_smooth_scan_string_fw(
num_spacer_chars, glyph_width, remaining_width, scroll_offset,
char_offset2, num_chars_to_copy2, &x_offset2);
/* > Update remaining width */
remaining_width -= (x_offset2 + (*num_chars_to_copy2 * glyph_width));
/* If scroll_offset is greater than zero, it means
* string 2 is the first string to be displayed
* > ticker x offset is therefore string 2's offset */
if (scroll_offset > 0)
*x_offset = x_offset2;
}
/* String 3 */
if (remaining_width > glyph_width)
{
/* String 3 is only shown when string 2 is shown,
* so we can take some shortcuts... */
*char_offset3 = 0;
/* Determine number of characters to copy */
*num_chars_to_copy3 = remaining_width / glyph_width;
*num_chars_to_copy3 = (*num_chars_to_copy3 > num_chars) ? num_chars : *num_chars_to_copy3;
}
}
static void ticker_smooth_scan_characters(
const unsigned *char_widths, size_t num_chars, unsigned field_width, unsigned scroll_offset,
unsigned *char_offset, unsigned *num_chars_to_copy, unsigned *x_offset,
unsigned *str_width, unsigned *display_width)
{
unsigned text_width = 0;
unsigned scroll_pos = scroll_offset;
bool deferred_str_width = true;
unsigned i;
/* Initialise output variables to 'sane' values */
*char_offset = 0;
*num_chars_to_copy = 0;
*x_offset = 0;
if (str_width)
*str_width = 0;
if (display_width)
*display_width = 0;
/* Determine index of first character to copy */
if (scroll_pos > 0)
{
for (i = 0; i < num_chars; i++)
{
if (scroll_pos > char_widths[i])
scroll_pos -= char_widths[i];
else
{
/* Note: It's okay for char_offset to go out
* of range here (num_chars_to_copy will be zero
* in this case) */
*char_offset = i + 1;
*x_offset = char_widths[i] - scroll_pos;
break;
}
}
}
/* Determine number of characters to copy */
for (i = *char_offset; i < num_chars; i++)
{
text_width += char_widths[i];
if (*x_offset + text_width <= field_width)
(*num_chars_to_copy)++;
else
{
/* Get actual width of resultant string
* (excluding x offset + end padding)
* Note that this is only set if we exceed the
* field width - if all characters up to the end
* of the string are copied... */
if (str_width)
{
deferred_str_width = false;
*str_width = text_width - char_widths[i];
}
break;
}
}
/* ...then we have to update str_width here instead */
if (str_width)
if (deferred_str_width)
*str_width = text_width;
/* Get total display width of resultant string
* (x offset + text width + end padding) */
if (display_width)
{
*display_width = *x_offset + text_width;
*display_width = (*display_width > field_width) ? field_width : *display_width;
}
}
static void gfx_animation_ticker_smooth_generic(uint64_t idx,
const unsigned *char_widths, size_t num_chars, unsigned str_width, unsigned field_width,
unsigned *char_offset, unsigned *num_chars_to_copy, unsigned *x_offset, unsigned *dst_str_width)
{
unsigned scroll_offset = get_ticker_smooth_generic_scroll_offset(
idx, str_width, field_width);
/* Initialise output variables to 'sane' values */
*char_offset = 0;
*num_chars_to_copy = 0;
*x_offset = 0;
if (dst_str_width)
*dst_str_width = 0;
/* Sanity check */
if (num_chars < 1)
return;
ticker_smooth_scan_characters(
char_widths, num_chars, field_width, scroll_offset,
char_offset, num_chars_to_copy, x_offset, dst_str_width, NULL);
}
static void gfx_animation_ticker_smooth_loop(uint64_t idx,
const unsigned *char_widths, size_t num_chars,
const unsigned *spacer_widths, size_t num_spacer_chars,
unsigned str_width, unsigned spacer_width, unsigned field_width,
unsigned *char_offset1, unsigned *num_chars_to_copy1,
unsigned *char_offset2, unsigned *num_chars_to_copy2,
unsigned *char_offset3, unsigned *num_chars_to_copy3,
unsigned *x_offset, unsigned *dst_str_width)
{
unsigned ticker_period = str_width + spacer_width;
unsigned phase = idx % ticker_period;
unsigned remaining_width = field_width;
/* Initialise output variables to 'sane' values */
*char_offset1 = 0;
*num_chars_to_copy1 = 0;
*char_offset2 = 0;
*num_chars_to_copy2 = 0;
*char_offset3 = 0;
*num_chars_to_copy3 = 0;
*x_offset = 0;
if (dst_str_width)
*dst_str_width = 0;
/* Sanity check */
if ((num_chars < 1) || (num_spacer_chars < 1))
return;
/* Looping text is composed of up to three strings,
* where string 1 and 2 are different regions of the
* source text and string 2 is a spacer:
*
* |----field_width----|
* [string 1][string 2][string 3]
*/
/* String 1 */
if (phase < str_width)
{
unsigned scroll_offset = phase;
unsigned display_width = 0;
unsigned str1_width = 0;
ticker_smooth_scan_characters(
char_widths, num_chars, remaining_width, scroll_offset,
char_offset1, num_chars_to_copy1, x_offset, &str1_width, &display_width);
/* Update remaining width */
remaining_width -= display_width;
/* Update dst_str_width */
if (dst_str_width)
*dst_str_width += str1_width;
}
/* String 2 */
if (remaining_width > 0)
{
unsigned scroll_offset = 0;
unsigned display_width = 0;
unsigned str2_width = 0;
unsigned x_offset2 = 0;
/* Check whether we've passed the end of string 1 */
if (phase > str_width)
scroll_offset = phase - str_width;
else
scroll_offset = 0;
ticker_smooth_scan_characters(
spacer_widths, num_spacer_chars, remaining_width, scroll_offset,
char_offset2, num_chars_to_copy2, &x_offset2, &str2_width, &display_width);
/* > Update remaining width */
remaining_width -= display_width;
/* Update dst_str_width */
if (dst_str_width)
*dst_str_width += str2_width;
/* If scroll_offset is greater than zero, it means
* string 2 is the first string to be displayed
* > ticker x offset is therefore string 2's offset */
if (scroll_offset > 0)
*x_offset = x_offset2;
}
/* String 3 */
if (remaining_width > 0)
{
/* String 3 is only shown when string 2 is shown,
* so we can take some shortcuts... */
unsigned i;
unsigned text_width = 0;
*char_offset3 = 0;
/* Determine number of characters to copy */
for (i = 0; i < num_chars; i++)
{
text_width += char_widths[i];
if (text_width <= remaining_width)
(*num_chars_to_copy3)++;
else
{
/* Update dst_str_width */
if (dst_str_width)
*dst_str_width += text_width - char_widths[i];
break;
}
}
}
}
static size_t get_line_display_ticks(size_t line_len)
{
/* Mean human reading speed for all western languages,
* characters per minute */
float cpm = 1000.0f;
/* Base time for which a line should be shown, in us */
float line_duration = (line_len * 60.0f * 1000.0f * 1000.0f) / cpm;
/* Ticker updates (nominally) once every TICKER_SPEED us
* > Return base number of ticks for which line should be shown */
return (size_t)(line_duration / (float)TICKER_SPEED);
}
static void gfx_animation_line_ticker_generic(uint64_t idx,
size_t line_len, size_t max_lines, size_t num_lines,
size_t *line_offset)
{
size_t line_ticks = get_line_display_ticks(line_len);
/* Note: This function is only called if num_lines > max_lines */
size_t excess_lines = num_lines - max_lines;
/* Ticker will pause for one line duration when the first
* or last line is reached (this is mostly required for the
* case where num_lines == (max_lines + 1), since otherwise
* the text flicks rapidly up and down in disconcerting
* fashion...) */
size_t ticker_period = (excess_lines * 2) + 2;
size_t phase = (idx / line_ticks) % ticker_period;
/* Pause on first line */
if (phase > 0)
phase--;
/* Pause on last line */
if (phase > excess_lines)
phase--;
/* Lines scrolling upwards */
if (phase <= excess_lines)
*line_offset = phase;
/* Lines scrolling downwards */
else
*line_offset = (excess_lines * 2) - phase;
}
static void gfx_animation_line_ticker_loop(uint64_t idx,
size_t line_len, size_t num_lines,
size_t *line_offset)
{
size_t line_ticks = get_line_display_ticks(line_len);
size_t ticker_period = num_lines + 1;
size_t phase = (idx / line_ticks) % ticker_period;
/* In this case, line_offset is simply equal to the phase */
*line_offset = phase;
}
static size_t get_line_smooth_scroll_ticks(size_t line_len)
{
/* Mean human reading speed for all western languages,
* characters per minute */
float cpm = 1000.0f;
/* Base time for which a line should be shown, in ms */
float line_duration = (line_len * 60.0f * 1000.0f) / cpm;
/* Ticker updates (nominally) once every ticker_pixel_period ms
* > Return base number of ticks for which text should scroll
* from one line to the next */
return (size_t)(line_duration / ticker_pixel_period);
}
static void set_line_smooth_fade_parameters(
bool scroll_up, size_t scroll_ticks, size_t line_phase, size_t line_height,
size_t num_lines, size_t num_display_lines, size_t line_offset, float y_offset,
size_t *top_fade_line_offset, float *top_fade_y_offset, float *top_fade_alpha,
size_t *bottom_fade_line_offset, float *bottom_fade_y_offset, float *bottom_fade_alpha)
{
float fade_out_alpha = 0.0f;
float fade_in_alpha = 0.0f;
/* When a line fades out, alpha transitions from
* 1 to 0 over the course of one half of the
* scrolling line height. When a line fades in,
* it's the other way around */
fade_out_alpha = ((float)scroll_ticks - ((float)line_phase * 2.0f)) / (float)scroll_ticks;
fade_in_alpha = -1.0f * fade_out_alpha;
fade_out_alpha = (fade_out_alpha < 0.0f) ? 0.0f : fade_out_alpha;
fade_in_alpha = (fade_in_alpha < 0.0f) ? 0.0f : fade_in_alpha;
*top_fade_line_offset = (line_offset > 0) ? line_offset - 1 : num_lines;
*top_fade_y_offset = y_offset - (float)line_height;
*top_fade_alpha = scroll_up ? fade_out_alpha : fade_in_alpha;
*bottom_fade_line_offset = line_offset + num_display_lines;
*bottom_fade_y_offset = y_offset + (float)(line_height * num_display_lines);
*bottom_fade_alpha = scroll_up ? fade_in_alpha : fade_out_alpha;
}
static void set_line_smooth_fade_parameters_default(
size_t *top_fade_line_offset, float *top_fade_y_offset, float *top_fade_alpha,
size_t *bottom_fade_line_offset, float *bottom_fade_y_offset, float *bottom_fade_alpha)
{
*top_fade_line_offset = 0;
*top_fade_y_offset = 0.0f;
*top_fade_alpha = 0.0f;
*bottom_fade_line_offset = 0;
*bottom_fade_y_offset = 0.0f;
*bottom_fade_alpha = 0.0f;
}
static void gfx_animation_line_ticker_smooth_generic(uint64_t idx,
bool fade_enabled, size_t line_len, size_t line_height,
size_t max_display_lines, size_t num_lines,
size_t *num_display_lines, size_t *line_offset, float *y_offset,
bool *fade_active,
size_t *top_fade_line_offset, float *top_fade_y_offset, float *top_fade_alpha,
size_t *bottom_fade_line_offset, float *bottom_fade_y_offset, float *bottom_fade_alpha)
{
size_t scroll_ticks = get_line_smooth_scroll_ticks(line_len);
/* Note: This function is only called if num_lines > max_display_lines */
size_t excess_lines = num_lines - max_display_lines;
/* Ticker will pause for one line duration when the first
* or last line is reached */
size_t ticker_period = ((excess_lines * 2) + 2) * scroll_ticks;
size_t phase = idx % ticker_period;
size_t line_phase = 0;
bool pause = false;
bool scroll_up = true;
/* Pause on first line */
if (phase < scroll_ticks)
pause = true;
phase = (phase >= scroll_ticks) ? phase - scroll_ticks : 0;
/* Pause on last line and change direction */
if (phase >= excess_lines * scroll_ticks)
{
scroll_up = false;
if (phase < (excess_lines + 1) * scroll_ticks)
{
pause = true;
phase = 0;
}
else
phase -= (excess_lines + 1) * scroll_ticks;
}
line_phase = phase % scroll_ticks;
if (pause || (line_phase == 0))
{
/* Static display of max_display_lines
* (no animation) */
*num_display_lines = max_display_lines;
*y_offset = 0.0f;
*fade_active = false;
if (pause)
*line_offset = scroll_up ? 0 : excess_lines;
else
*line_offset = scroll_up ? (phase / scroll_ticks) : (excess_lines - (phase / scroll_ticks));
}
else
{
/* Scroll animation is active */
*num_display_lines = max_display_lines - 1;
*fade_active = fade_enabled;
if (scroll_up)
{
*line_offset = (phase / scroll_ticks) + 1;
*y_offset = (float)line_height * (float)(scroll_ticks - line_phase) / (float)scroll_ticks;
}
else
{
*line_offset = excess_lines - (phase / scroll_ticks);
*y_offset = (float)line_height * (1.0f - (float)(scroll_ticks - line_phase) / (float)scroll_ticks);
}
/* Set fade parameters if fade animation is active */
if (*fade_active)
set_line_smooth_fade_parameters(
scroll_up, scroll_ticks, line_phase, line_height,
num_lines, *num_display_lines, *line_offset, *y_offset,
top_fade_line_offset, top_fade_y_offset, top_fade_alpha,
bottom_fade_line_offset, bottom_fade_y_offset, bottom_fade_alpha);
}
/* Set 'default' fade parameters if fade animation
* is inactive */
if (!*fade_active)
set_line_smooth_fade_parameters_default(
top_fade_line_offset, top_fade_y_offset, top_fade_alpha,
bottom_fade_line_offset, bottom_fade_y_offset, bottom_fade_alpha);
}
static void gfx_animation_line_ticker_smooth_loop(uint64_t idx,
bool fade_enabled, size_t line_len, size_t line_height,
size_t max_display_lines, size_t num_lines,
size_t *num_display_lines, size_t *line_offset, float *y_offset,
bool *fade_active,
size_t *top_fade_line_offset, float *top_fade_y_offset, float *top_fade_alpha,
size_t *bottom_fade_line_offset, float *bottom_fade_y_offset, float *bottom_fade_alpha)
{
size_t scroll_ticks = get_line_smooth_scroll_ticks(line_len);
size_t ticker_period = (num_lines + 1) * scroll_ticks;