gfx_animation.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291
  1. /* RetroArch - A frontend for libretro.
  2. * Copyright (C) 2014-2018 - Jean-André Santoni
  3. * Copyright (C) 2011-2018 - Daniel De Matteis
  4. *
  5. * RetroArch is free software: you can redistribute it and/or modify it under the terms
  6. * of the GNU General Public License as published by the Free Software Found-
  7. * ation, either version 3 of the License, or (at your option) any later version.
  8. *
  9. * RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  10. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. * PURPOSE. See the GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with RetroArch.
  14. * If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <math.h>
  17. #include <string.h>
  18. #include <compat/strl.h>
  19. #include <encodings/utf.h>
  20. #include <retro_math.h>
  21. #include <retro_miscellaneous.h>
  22. #include <string/stdstring.h>
  23. #include <features/features_cpu.h>
  24. #include <lists/string_list.h>
  25. #include <array/rbuf.h>
  26. #include "gfx_animation.h"
  27. #include "../performance_counters.h"
  28. #define TICKER_SPEED 333333
  29. #define TICKER_SLOW_SPEED 1666666
  30. /* Pixel ticker nominally increases by one after each
  31. * TICKER_PIXEL_PERIOD ms (actual increase depends upon
  32. * ticker speed setting and display resolution)
  33. *
  34. * Formula is: (1.0f / 60.0f) * 1000.0f
  35. * */
  36. #define TICKER_PIXEL_PERIOD (16.666666666666668f)
  37. /* Mean human reading speed for all western languages,
  38. * characters per minute */
  39. #define TICKER_CPM 1000.0f
  40. /* Base time for which a line should be shown, in us */
  41. #define TICKER_LINE_DURATION_US(line_len) ((line_len * 60.0f * 1000.0f * 1000.0f) / TICKER_CPM)
  42. /* Base time for which a line should be shown, in ms */
  43. #define TICKER_LINE_DURATION_MS(line_len) ((line_len * 60.0f * 1000.0f) / TICKER_CPM)
  44. /* Ticker updates (nominally) once every TICKER_SPEED us
  45. * > Base number of ticks for which line should be shown */
  46. #define TICKER_LINE_DISPLAY_TICKS(line_len) ((size_t)(TICKER_LINE_DURATION_US(line_len) / (float)TICKER_SPEED))
  47. /* Smooth ticker updates (nominally) once every TICKER_PIXEL_PERIOD ms
  48. * > Base number of ticks for which text should scroll
  49. * from one line to the next */
  50. #define TICKER_LINE_SMOOTH_SCROLL_TICKS(line_len) ((size_t)(TICKER_LINE_DURATION_MS(line_len) / TICKER_PIXEL_PERIOD))
  51. static gfx_animation_t anim_st = {
  52. 0, /* ticker_idx */
  53. 0, /* ticker_slow_idx */
  54. 0, /* ticker_pixel_idx */
  55. 0, /* ticker_pixel_line_idx */
  56. 0, /* cur_time */
  57. 0, /* old_time */
  58. NULL, /* updatetime_cb */
  59. NULL, /* list */
  60. NULL, /* pending */
  61. 0.0f, /* delta_time */
  62. 0 /* flags */
  63. };
  64. gfx_animation_t *anim_get_ptr(void)
  65. {
  66. return &anim_st;
  67. }
  68. /* from https://github.com/kikito/tween.lua/blob/master/tween.lua */
  69. static float easing_linear(float t, float b, float c, float d)
  70. {
  71. return c * t / d + b;
  72. }
  73. static float easing_in_out_quad(float t, float b, float c, float d)
  74. {
  75. t = t / d * 2.0f;
  76. if (t < 1.0f)
  77. return c / 2.0f * t * t + b;
  78. return -c / 2.0f * ((t - 1.0f) * (t - 3.0f) - 1.0f) + b;
  79. }
  80. static float easing_in_quad(float t, float b, float c, float d)
  81. {
  82. float base = t / d;
  83. return c * (base * base) + b;
  84. }
  85. static float easing_out_quad(float t, float b, float c, float d)
  86. {
  87. t = t / d;
  88. return -c * t * (t - 2.0f) + b;
  89. }
  90. static float easing_out_in_quad(float t, float b, float c, float d)
  91. {
  92. if (t < d / 2.0f)
  93. return easing_out_quad(t * 2.0f, b, c / 2.0f, d);
  94. return easing_in_quad((t * 2.0f) - d, b + c / 2.0f, c / 2.0f, d);
  95. }
  96. static float easing_in_cubic(float t, float b, float c, float d)
  97. {
  98. float base = t / d;
  99. return c * (base * base * base) + b;
  100. }
  101. static float easing_out_cubic(float t, float b, float c, float d)
  102. {
  103. float base = t / d - 1.0f;
  104. return c * ((base * base * base) + 1.0f) + b;
  105. }
  106. static float easing_in_out_cubic(float t, float b, float c, float d)
  107. {
  108. t = t / d * 2.0f;
  109. if (t < 1.0f)
  110. return c / 2.0f * t * t * t + b;
  111. t = t - 2.0f;
  112. return c / 2.0f * (t * t * t + 2.0f) + b;
  113. }
  114. static float easing_out_in_cubic(float t, float b, float c, float d)
  115. {
  116. if (t < d / 2.0f)
  117. return easing_out_cubic(t * 2.0f, b, c / 2.0f, d);
  118. return easing_in_cubic((t * 2.0f) - d, b + c / 2.0f, c / 2.0f, d);
  119. }
  120. static float easing_in_quart(float t, float b, float c, float d)
  121. {
  122. float base = t / d;
  123. return c * (base * base * base * base) + b;
  124. }
  125. static float easing_out_quart(float t, float b, float c, float d)
  126. {
  127. float base = t / d - 1.0f;
  128. return -c * ((base * base * base * base) - 1.0f) + b;
  129. }
  130. static float easing_in_out_quart(float t, float b, float c, float d)
  131. {
  132. float base;
  133. t = t / d * 2.0f;
  134. if (t < 1.0f)
  135. return c / 2.0f * (t * t * t * t) + b;
  136. base = t - 2.0f;
  137. return -c / 2.0f * ((base * base * base * base) - 2.0f) + b;
  138. }
  139. static float easing_out_in_quart(float t, float b, float c, float d)
  140. {
  141. if (t < d / 2.0f)
  142. return easing_out_quart(t * 2.0f, b, c / 2.0f, d);
  143. return easing_in_quart((t * 2.0f) - d, b + c / 2.0f, c / 2.0f, d);
  144. }
  145. static float easing_in_quint(float t, float b, float c, float d)
  146. {
  147. float base = t / d;
  148. return c * (base * base * base * base * base) + b;
  149. }
  150. static float easing_out_quint(float t, float b, float c, float d)
  151. {
  152. float base = t / d - 1.0f;
  153. return c * ((base * base * base * base * base) + 1.0f) + b;
  154. }
  155. static float easing_in_out_quint(float t, float b, float c, float d)
  156. {
  157. float base;
  158. t = t / d * 2.0f;
  159. if (t < 1.0f)
  160. return c / 2.0f * (t * t * t * t * t) + b;
  161. base = t - 2.0f;
  162. return c / 2.0f * ((base * base * base * base * base) + 2.0f) + b;
  163. }
  164. static float easing_out_in_quint(float t, float b, float c, float d)
  165. {
  166. if (t < d / 2.0f)
  167. return easing_out_quint(t * 2.0f, b, c / 2.0f, d);
  168. return easing_in_quint((t * 2.0f) - d, b + c / 2.0f, c / 2.0f, d);
  169. }
  170. static float easing_in_sine(float t, float b, float c, float d)
  171. {
  172. return -c * cos(t / d * (M_PI / 2.0f)) + c + b;
  173. }
  174. static float easing_out_sine(float t, float b, float c, float d)
  175. {
  176. return c * sin(t / d * (M_PI / 2.0f)) + b;
  177. }
  178. static float easing_in_out_sine(float t, float b, float c, float d)
  179. {
  180. return -c / 2.0f * (cos(M_PI * t / d) - 1.0f) + b;
  181. }
  182. static float easing_out_in_sine(float t, float b, float c, float d)
  183. {
  184. if (t < d / 2.0f)
  185. return easing_out_sine(t * 2.0f, b, c / 2.0f, d);
  186. return easing_in_sine((t * 2.0f) -d, b + c / 2.0f, c / 2.0f, d);
  187. }
  188. static float easing_in_expo(float t, float b, float c, float d)
  189. {
  190. if (t == 0.0f)
  191. return b;
  192. return c * powf(2, 10 * (t / d - 1)) + b - c * 0.001;
  193. }
  194. static float easing_out_expo(float t, float b, float c, float d)
  195. {
  196. if (t == d)
  197. return b + c;
  198. return c * 1.001 * (-powf(2, -10 * t / d) + 1) + b;
  199. }
  200. static float easing_in_out_expo(float t, float b, float c, float d)
  201. {
  202. if (t == 0)
  203. return b;
  204. if (t == d)
  205. return b + c;
  206. t = t / d * 2.0f;
  207. if (t < 1.0f)
  208. return c / 2.0f * powf(2, 10 * (t - 1)) + b - c * 0.0005;
  209. return c / 2.0f * 1.0005 * (-powf(2, -10 * (t - 1)) + 2) + b;
  210. }
  211. static float easing_out_in_expo(float t, float b, float c, float d)
  212. {
  213. if (t < d / 2.0f)
  214. return easing_out_expo(t * 2.0f, b, c / 2.0f, d);
  215. return easing_in_expo((t * 2.0f) - d, b + c / 2.0f, c / 2.0f, d);
  216. }
  217. static float easing_in_circ(float t, float b, float c, float d)
  218. {
  219. float base = t / d;
  220. return(-c * (sqrtf(1.0f - (base * base)) - 1.0f) + b);
  221. }
  222. static float easing_out_circ(float t, float b, float c, float d)
  223. {
  224. float base = t / d - 1;
  225. return(c * sqrtf(1.0f - (base * base)) + b);
  226. }
  227. static float easing_in_out_circ(float t, float b, float c, float d)
  228. {
  229. t = t / d * 2.0f;
  230. if (t < 1.0f)
  231. return -c / 2 * (sqrtf(1.0f - t * t) - 1.0f) + b;
  232. t = t - 2.0f;
  233. return c / 2.0f * (sqrtf(1.0f - t * t) + 1.0f) + b;
  234. }
  235. static float easing_out_in_circ(float t, float b, float c, float d)
  236. {
  237. if (t < d / 2.0f)
  238. return easing_out_circ(t * 2.0f, b, c / 2.0f, d);
  239. return easing_in_circ((t * 2.0f) - d, b + c / 2.0f, c / 2.0f, d);
  240. }
  241. static float easing_out_bounce(float t, float b, float c, float d)
  242. {
  243. t = t / d;
  244. if (t < 1 / 2.75f)
  245. return c * (7.5625f * t * t) + b;
  246. if (t < 2 / 2.75f)
  247. {
  248. t = t - (1.5f / 2.75f);
  249. return c * (7.5625f * t * t + 0.75f) + b;
  250. }
  251. else if (t < 2.5f / 2.75f)
  252. {
  253. t = t - (2.25f / 2.75f);
  254. return c * (7.5625f * t * t + 0.9375f) + b;
  255. }
  256. t = t - (2.625f / 2.75f);
  257. return c * (7.5625f * t * t + 0.984375f) + b;
  258. }
  259. static float easing_in_bounce(float t, float b, float c, float d)
  260. {
  261. return c - easing_out_bounce(d - t, 0, c, d) + b;
  262. }
  263. static float easing_in_out_bounce(float t, float b, float c, float d)
  264. {
  265. if (t < d / 2.0f)
  266. return easing_in_bounce(t * 2.0f, 0.0f, c, d) * 0.5f + b;
  267. return easing_out_bounce(t * 2.0f - d, 0.0f, c, d) * 0.5f + c * .5f + b;
  268. }
  269. static float easing_out_in_bounce(float t, float b, float c, float d)
  270. {
  271. if (t < d / 2.0f)
  272. return easing_out_bounce(t * 2.0f, b, c / 2.0f, d);
  273. return easing_in_bounce((t * 2.0f) - d, b + c / 2.0f, c / 2.0f, d);
  274. }
  275. static size_t gfx_animation_ticker_generic(uint64_t idx,
  276. size_t old_width)
  277. {
  278. const int phase_left_stop = 2;
  279. int ticker_period = (int)(2 * old_width + 4);
  280. int phase = idx % ticker_period;
  281. int phase_left_moving = (int)(phase_left_stop + old_width);
  282. int phase_right_stop = phase_left_moving + 2;
  283. if (phase < phase_left_stop)
  284. return 0;
  285. else if (phase < phase_left_moving) /* left offset? */
  286. return phase - phase_left_stop;
  287. else if (phase < phase_right_stop)
  288. return old_width;
  289. /* right offset */
  290. return (int)(old_width - (phase - phase_right_stop));
  291. }
  292. static void gfx_animation_ticker_loop(uint64_t idx,
  293. size_t max_width, size_t str_width, size_t spacer_width,
  294. size_t *offset1, size_t *width1,
  295. size_t *offset2, size_t *width2,
  296. size_t *offset3, size_t *width3)
  297. {
  298. int ticker_period = (int)(str_width + spacer_width);
  299. int phase = idx % ticker_period;
  300. /* Output offsets/widths are unsigned size_t, but it's
  301. * easier to perform the required calculations with ints,
  302. * so create some temporary variables... */
  303. /* Looping text is composed of up to three strings,
  304. * where string 1 and 2 are different regions of the
  305. * source text and string 2 is a spacer:
  306. *
  307. * |-----max_width-----|
  308. * [string 1][string 2][string 3]
  309. *
  310. * The following implementation could probably be optimised,
  311. * but any performance gains would be trivial compared with
  312. * all the string manipulation that has to happen afterwards...
  313. */
  314. /* String 1 */
  315. int offset = 0;
  316. int width = (int)(str_width - phase);
  317. if (width < 0)
  318. width = 0;
  319. else if ((width > (int)max_width))
  320. width = (int)max_width;
  321. if (phase < (int)str_width)
  322. offset = phase;
  323. *offset1 = offset;
  324. *width1 = width;
  325. /* String 2 */
  326. offset = (int)(phase - str_width);
  327. if (offset < 0)
  328. offset = 0;
  329. width = (int)(max_width - *width1);
  330. if (width > (int)spacer_width)
  331. width = (int)spacer_width;
  332. width -= offset;
  333. *offset2 = offset;
  334. *width2 = width;
  335. /* String 3 */
  336. width = (int)(max_width - (*width1 + *width2));
  337. if (width < 0)
  338. width = 0;
  339. /* Note: offset is always zero here so offset3 is
  340. * unnecessary - but include it anyway to preserve
  341. * symmetry... */
  342. *offset3 = 0;
  343. *width3 = width;
  344. }
  345. static unsigned get_ticker_smooth_generic_scroll_offset(
  346. uint64_t idx, unsigned str_width, unsigned field_width)
  347. {
  348. const unsigned pause_duration = 32;
  349. unsigned scroll_width = str_width - field_width;
  350. unsigned ticker_period = 2 * (scroll_width + pause_duration);
  351. unsigned phase = idx % ticker_period;
  352. /* Determine scroll offset */
  353. if (phase < pause_duration)
  354. return 0;
  355. else if (phase < ticker_period >> 1)
  356. return (phase - pause_duration);
  357. else if (phase < (ticker_period >> 1) + pause_duration)
  358. return ((ticker_period - (2 * pause_duration)) >> 1);
  359. return (ticker_period - phase);
  360. }
  361. /* 'Fixed width' font version of ticker_smooth_scan_characters() */
  362. static void ticker_smooth_scan_string_fw(
  363. size_t num_chars, unsigned glyph_width,
  364. unsigned field_width, unsigned scroll_offset,
  365. unsigned *char_offset, unsigned *num_chars_to_copy,
  366. unsigned *x_offset)
  367. {
  368. /* Initialise output variables to 'sane' values */
  369. *num_chars_to_copy = 0;
  370. /* Determine index of first character to copy */
  371. if (scroll_offset > 0)
  372. {
  373. *char_offset = (scroll_offset / glyph_width) + 1;
  374. *x_offset = glyph_width - (scroll_offset % glyph_width);
  375. }
  376. else
  377. {
  378. *char_offset = 0;
  379. *x_offset = 0;
  380. }
  381. /* Determine number of characters remaining in
  382. * string once offset has been subtracted */
  383. if (*char_offset < num_chars)
  384. {
  385. size_t chars_remaining = num_chars - *char_offset;
  386. /* Determine number of characters to copy */
  387. if ((chars_remaining > 0) && (field_width > *x_offset))
  388. {
  389. *num_chars_to_copy = (field_width - *x_offset) / glyph_width;
  390. if (*num_chars_to_copy > chars_remaining)
  391. *num_chars_to_copy = (unsigned)chars_remaining;
  392. }
  393. }
  394. }
  395. /* 'Fixed width' font version of gfx_animation_ticker_smooth_generic() */
  396. static void gfx_animation_ticker_smooth_generic_fw(uint64_t idx,
  397. unsigned str_width, size_t num_chars,
  398. unsigned glyph_width, unsigned field_width,
  399. unsigned *char_offset, unsigned *num_chars_to_copy, unsigned *x_offset)
  400. {
  401. /* Initialise output variables to 'sane' values */
  402. *char_offset = 0;
  403. *num_chars_to_copy = 0;
  404. *x_offset = 0;
  405. /* Sanity check */
  406. if (num_chars >= 1)
  407. {
  408. unsigned scroll_offset = get_ticker_smooth_generic_scroll_offset(
  409. idx, str_width, field_width);
  410. ticker_smooth_scan_string_fw(
  411. num_chars, glyph_width, field_width, scroll_offset,
  412. char_offset, num_chars_to_copy, x_offset);
  413. }
  414. }
  415. /* 'Fixed width' font version of gfx_animation_ticker_smooth_loop() */
  416. static void gfx_animation_ticker_smooth_loop_fw(uint64_t idx,
  417. unsigned str_width, size_t num_chars,
  418. unsigned spacer_width, size_t num_spacer_chars,
  419. unsigned glyph_width, unsigned field_width,
  420. unsigned *char_offset1, unsigned *num_chars_to_copy1,
  421. unsigned *char_offset2, unsigned *num_chars_to_copy2,
  422. unsigned *char_offset3, unsigned *num_chars_to_copy3,
  423. unsigned *x_offset)
  424. {
  425. unsigned ticker_period = str_width + spacer_width;
  426. unsigned phase = idx % ticker_period;
  427. unsigned remaining_width = field_width;
  428. /* Initialise output variables to 'sane' values */
  429. *char_offset1 = 0;
  430. *num_chars_to_copy1 = 0;
  431. *char_offset2 = 0;
  432. *num_chars_to_copy2 = 0;
  433. *char_offset3 = 0;
  434. *num_chars_to_copy3 = 0;
  435. *x_offset = 0;
  436. /* Sanity check */
  437. if ((num_chars < 1) || (num_spacer_chars < 1))
  438. return;
  439. /* Looping text is composed of up to three strings,
  440. * where string 1 and 2 are different regions of the
  441. * source text and string 2 is a spacer:
  442. *
  443. * |----field_width----|
  444. * [string 1][string 2][string 3]
  445. */
  446. /* String 1 */
  447. if (phase < str_width)
  448. {
  449. unsigned scroll_offset = phase;
  450. ticker_smooth_scan_string_fw(
  451. num_chars, glyph_width, remaining_width, scroll_offset,
  452. char_offset1, num_chars_to_copy1, x_offset);
  453. /* Update remaining width
  454. * Note: We can avoid all the display_width shenanigans
  455. * here (c.f. gfx_animation_ticker_smooth_loop()) because
  456. * the font width is constant - i.e. we don't have to wrangle
  457. * out the width of the last 'non-copied' character since it
  458. * is known a priori, so we can just subtract the string width
  459. * + offset here, and perform an 'if (remaining_width > glyph_width)'
  460. * for strings 2 and 3 */
  461. remaining_width -= (*x_offset + (*num_chars_to_copy1 * glyph_width));
  462. }
  463. /* String 2 */
  464. if (remaining_width > glyph_width)
  465. {
  466. unsigned scroll_offset = 0;
  467. unsigned x_offset2 = 0;
  468. /* Check whether we've passed the end of string 1 */
  469. if (phase > str_width)
  470. scroll_offset = phase - str_width;
  471. ticker_smooth_scan_string_fw(
  472. num_spacer_chars, glyph_width, remaining_width, scroll_offset,
  473. char_offset2, num_chars_to_copy2, &x_offset2);
  474. /* > Update remaining width */
  475. remaining_width -= (x_offset2 + (*num_chars_to_copy2 * glyph_width));
  476. /* If scroll_offset is greater than zero, it means
  477. * string 2 is the first string to be displayed
  478. * > ticker x offset is therefore string 2's offset */
  479. if (scroll_offset > 0)
  480. *x_offset = x_offset2;
  481. }
  482. /* String 3 */
  483. if (remaining_width > glyph_width)
  484. {
  485. /* String 3 is only shown when string 2 is shown,
  486. * so we can take some shortcuts... */
  487. *char_offset3 = 0;
  488. /* Determine number of characters to copy */
  489. *num_chars_to_copy3 = remaining_width / glyph_width;
  490. if (*num_chars_to_copy3 > num_chars)
  491. *num_chars_to_copy3 = (unsigned)num_chars;
  492. }
  493. }
  494. static void ticker_smooth_scan_characters(
  495. const unsigned *char_widths, size_t num_chars,
  496. unsigned field_width, unsigned scroll_offset,
  497. unsigned *char_offset, unsigned *num_chars_to_copy,
  498. unsigned *x_offset, unsigned *str_width,
  499. unsigned *display_width)
  500. {
  501. unsigned i;
  502. unsigned text_width = 0;
  503. unsigned scroll_pos = scroll_offset;
  504. bool deferred_str_width = true;
  505. /* Initialise output variables to 'sane' values */
  506. *char_offset = 0;
  507. *num_chars_to_copy = 0;
  508. *x_offset = 0;
  509. if (str_width)
  510. *str_width = 0;
  511. if (display_width)
  512. *display_width = 0;
  513. /* Determine index of first character to copy */
  514. if (scroll_pos > 0)
  515. {
  516. for (i = 0; i < num_chars; i++)
  517. {
  518. if (scroll_pos > char_widths[i])
  519. scroll_pos -= char_widths[i];
  520. else
  521. {
  522. /* Note: It's okay for char_offset to go out
  523. * of range here (num_chars_to_copy will be zero
  524. * in this case) */
  525. *char_offset = i + 1;
  526. *x_offset = char_widths[i] - scroll_pos;
  527. break;
  528. }
  529. }
  530. }
  531. /* Determine number of characters to copy */
  532. for (i = *char_offset; i < num_chars; i++)
  533. {
  534. text_width += char_widths[i];
  535. if (*x_offset + text_width <= field_width)
  536. (*num_chars_to_copy)++;
  537. else
  538. {
  539. /* Get actual width of resultant string
  540. * (excluding x offset + end padding)
  541. * Note that this is only set if we exceed the
  542. * field width - if all characters up to the end
  543. * of the string are copied... */
  544. if (str_width)
  545. {
  546. deferred_str_width = false;
  547. *str_width = text_width - char_widths[i];
  548. }
  549. break;
  550. }
  551. }
  552. /* ...then we have to update str_width here instead */
  553. if (str_width)
  554. if (deferred_str_width)
  555. *str_width = text_width;
  556. /* Get total display width of resultant string
  557. * (x offset + text width + end padding) */
  558. if (display_width)
  559. {
  560. *display_width = *x_offset + text_width;
  561. if (*display_width > field_width)
  562. *display_width = field_width;
  563. }
  564. }
  565. static void gfx_animation_ticker_smooth_generic(uint64_t idx,
  566. const unsigned *char_widths, size_t num_chars,
  567. unsigned str_width, unsigned field_width,
  568. unsigned *char_offset, unsigned *num_chars_to_copy,
  569. unsigned *x_offset, unsigned *dst_str_width)
  570. {
  571. /* Initialise output variables to 'sane' values */
  572. *char_offset = 0;
  573. *num_chars_to_copy = 0;
  574. *x_offset = 0;
  575. if (dst_str_width)
  576. *dst_str_width = 0;
  577. /* Sanity check */
  578. if (num_chars >= 1)
  579. {
  580. unsigned scroll_offset = get_ticker_smooth_generic_scroll_offset(
  581. idx, str_width, field_width);
  582. ticker_smooth_scan_characters(
  583. char_widths, num_chars, field_width, scroll_offset,
  584. char_offset, num_chars_to_copy, x_offset, dst_str_width, NULL);
  585. }
  586. }
  587. static void gfx_animation_ticker_smooth_loop(uint64_t idx,
  588. const unsigned *char_widths, size_t num_chars,
  589. const unsigned *spacer_widths, size_t num_spacer_chars,
  590. unsigned str_width, unsigned spacer_width, unsigned field_width,
  591. unsigned *char_offset1, unsigned *num_chars_to_copy1,
  592. unsigned *char_offset2, unsigned *num_chars_to_copy2,
  593. unsigned *char_offset3, unsigned *num_chars_to_copy3,
  594. unsigned *x_offset, unsigned *dst_str_width)
  595. {
  596. unsigned ticker_period = str_width + spacer_width;
  597. unsigned phase = idx % ticker_period;
  598. unsigned remaining_width = field_width;
  599. /* Initialise output variables to 'sane' values */
  600. *char_offset1 = 0;
  601. *num_chars_to_copy1 = 0;
  602. *char_offset2 = 0;
  603. *num_chars_to_copy2 = 0;
  604. *char_offset3 = 0;
  605. *num_chars_to_copy3 = 0;
  606. *x_offset = 0;
  607. if (dst_str_width)
  608. *dst_str_width = 0;
  609. /* Sanity check */
  610. if ((num_chars < 1) || (num_spacer_chars < 1))
  611. return;
  612. /* Looping text is composed of up to three strings,
  613. * where string 1 and 2 are different regions of the
  614. * source text and string 2 is a spacer:
  615. *
  616. * |----field_width----|
  617. * [string 1][string 2][string 3]
  618. */
  619. /* String 1 */
  620. if (phase < str_width)
  621. {
  622. unsigned scroll_offset = phase;
  623. unsigned display_width = 0;
  624. unsigned str1_width = 0;
  625. ticker_smooth_scan_characters(
  626. char_widths, num_chars, remaining_width, scroll_offset,
  627. char_offset1, num_chars_to_copy1, x_offset, &str1_width, &display_width);
  628. /* Update remaining width */
  629. remaining_width -= display_width;
  630. /* Update dst_str_width */
  631. if (dst_str_width)
  632. *dst_str_width += str1_width;
  633. }
  634. /* String 2 */
  635. if (remaining_width > 0)
  636. {
  637. unsigned scroll_offset = 0;
  638. unsigned display_width = 0;
  639. unsigned str2_width = 0;
  640. unsigned x_offset2 = 0;
  641. /* Check whether we've passed the end of string 1 */
  642. if (phase > str_width)
  643. scroll_offset = phase - str_width;
  644. ticker_smooth_scan_characters(
  645. spacer_widths, num_spacer_chars, remaining_width, scroll_offset,
  646. char_offset2, num_chars_to_copy2, &x_offset2, &str2_width, &display_width);
  647. /* > Update remaining width */
  648. remaining_width -= display_width;
  649. /* Update dst_str_width */
  650. if (dst_str_width)
  651. *dst_str_width += str2_width;
  652. /* If scroll_offset is greater than zero, it means
  653. * string 2 is the first string to be displayed
  654. * > ticker x offset is therefore string 2's offset */
  655. if (scroll_offset > 0)
  656. *x_offset = x_offset2;
  657. }
  658. /* String 3 */
  659. if (remaining_width > 0)
  660. {
  661. /* String 3 is only shown when string 2 is shown,
  662. * so we can take some shortcuts... */
  663. unsigned i;
  664. unsigned text_width = 0;
  665. *char_offset3 = 0;
  666. /* Determine number of characters to copy */
  667. for (i = 0; i < num_chars; i++)
  668. {
  669. text_width += char_widths[i];
  670. if (text_width <= remaining_width)
  671. (*num_chars_to_copy3)++;
  672. else
  673. {
  674. /* Update dst_str_width */
  675. if (dst_str_width)
  676. *dst_str_width += text_width - char_widths[i];
  677. break;
  678. }
  679. }
  680. }
  681. }
  682. static size_t gfx_animation_line_ticker_generic(uint64_t idx,
  683. size_t line_len, size_t max_lines, size_t num_lines)
  684. {
  685. size_t line_ticks = TICKER_LINE_DISPLAY_TICKS(line_len);
  686. /* Note: This function is only called if num_lines > max_lines */
  687. size_t excess_lines = num_lines - max_lines;
  688. /* Ticker will pause for one line duration when the first
  689. * or last line is reached (this is mostly required for the
  690. * case where num_lines == (max_lines + 1), since otherwise
  691. * the text flicks rapidly up and down in disconcerting
  692. * fashion...) */
  693. size_t ticker_period = (excess_lines * 2) + 2;
  694. size_t phase = (idx / line_ticks) % ticker_period;
  695. /* Pause on first line */
  696. if (phase > 0)
  697. phase--;
  698. /* Pause on last line */
  699. if (phase > excess_lines)
  700. phase--;
  701. /* Lines scrolling upwards */
  702. if (phase <= excess_lines)
  703. return phase;
  704. /* Lines scrolling downwards */
  705. return (excess_lines * 2) - phase;
  706. }
  707. static size_t gfx_animation_line_ticker_loop(uint64_t idx,
  708. size_t line_len, size_t num_lines)
  709. {
  710. size_t line_ticks = TICKER_LINE_DISPLAY_TICKS(line_len);
  711. size_t ticker_period = num_lines + 1;
  712. size_t phase = (idx / line_ticks) % ticker_period;
  713. /* In this case, line_offset is simply equal to the phase */
  714. return phase;
  715. }
  716. static void set_line_smooth_fade_parameters(
  717. bool scroll_up,
  718. size_t scroll_ticks, size_t line_phase, size_t line_height,
  719. size_t num_lines, size_t num_display_lines, size_t line_offset,
  720. float y_offset,
  721. size_t *top_fade_line_offset,
  722. float *top_fade_y_offset, float *top_fade_alpha,
  723. size_t *bottom_fade_line_offset,
  724. float *bottom_fade_y_offset, float *bottom_fade_alpha)
  725. {
  726. /* When a line fades out, alpha transitions from
  727. * 1 to 0 over the course of one half of the
  728. * scrolling line height. When a line fades in,
  729. * it's the other way around */
  730. float fade_out_alpha = ((float)scroll_ticks - ((float)line_phase * 2.0f)) / (float)scroll_ticks;
  731. float fade_in_alpha = -1.0f * fade_out_alpha;
  732. if (fade_out_alpha < 0.0f)
  733. fade_out_alpha = 0.0f;
  734. if (fade_in_alpha < 0.0f)
  735. fade_in_alpha = 0.0f;
  736. *top_fade_line_offset = (line_offset > 0) ? line_offset - 1 : num_lines;
  737. *top_fade_y_offset = y_offset - (float)line_height;
  738. if (scroll_up)
  739. {
  740. *top_fade_alpha = fade_out_alpha;
  741. *bottom_fade_alpha = fade_in_alpha;
  742. }
  743. else
  744. {
  745. *top_fade_alpha = fade_in_alpha;
  746. *bottom_fade_alpha = fade_out_alpha;
  747. }
  748. *bottom_fade_line_offset = line_offset + num_display_lines;
  749. *bottom_fade_y_offset = y_offset + (float)(line_height * num_display_lines);
  750. }
  751. static void set_line_smooth_fade_parameters_default(
  752. size_t *top_fade_line_offset, float *top_fade_y_offset, float *top_fade_alpha,
  753. size_t *bottom_fade_line_offset, float *bottom_fade_y_offset, float *bottom_fade_alpha)
  754. {
  755. *top_fade_line_offset = 0;
  756. *top_fade_y_offset = 0.0f;
  757. *top_fade_alpha = 0.0f;
  758. *bottom_fade_line_offset = 0;
  759. *bottom_fade_y_offset = 0.0f;
  760. *bottom_fade_alpha = 0.0f;
  761. }
  762. static void gfx_animation_line_ticker_smooth_generic(uint64_t idx,
  763. bool fade_enabled, size_t line_len, size_t line_height,
  764. size_t max_display_lines, size_t num_lines,
  765. size_t *num_display_lines, size_t *line_offset,
  766. float *y_offset,
  767. bool *fade_active,
  768. size_t *top_fade_line_offset, float *top_fade_y_offset,
  769. float *top_fade_alpha,
  770. size_t *bottom_fade_line_offset, float *bottom_fade_y_offset,
  771. float *bottom_fade_alpha)
  772. {
  773. size_t scroll_ticks = TICKER_LINE_SMOOTH_SCROLL_TICKS(line_len);
  774. /* Note: This function is only called if num_lines > max_display_lines */
  775. size_t excess_lines = num_lines - max_display_lines;
  776. /* Ticker will pause for one line duration when the first
  777. * or last line is reached */
  778. size_t ticker_period = ((excess_lines * 2) + 2) * scroll_ticks;
  779. size_t phase = idx % ticker_period;
  780. size_t line_phase = 0;
  781. bool pause = false;
  782. bool scroll_up = true;
  783. if (phase >= scroll_ticks)
  784. phase -= scroll_ticks;
  785. else
  786. {
  787. /* Pause on first line */
  788. pause = true;
  789. phase = 0;
  790. }
  791. /* Pause on last line and change direction */
  792. if (phase >= excess_lines * scroll_ticks)
  793. {
  794. scroll_up = false;
  795. if (phase < (excess_lines + 1) * scroll_ticks)
  796. {
  797. pause = true;
  798. phase = 0;
  799. }
  800. else
  801. phase -= (excess_lines + 1) * scroll_ticks;
  802. }
  803. line_phase = phase % scroll_ticks;
  804. if (pause)
  805. {
  806. /* Static display of max_display_lines
  807. * (no animation) */
  808. *num_display_lines = max_display_lines;
  809. *y_offset = 0.0f;
  810. *fade_active = false;
  811. *line_offset = scroll_up ? 0 : excess_lines;
  812. }
  813. else if (line_phase == 0)
  814. {
  815. /* Static display of max_display_lines
  816. * (no animation) */
  817. *num_display_lines = max_display_lines;
  818. *y_offset = 0.0f;
  819. *fade_active = false;
  820. *line_offset = scroll_up ? (phase / scroll_ticks) : (excess_lines - (phase / scroll_ticks));
  821. }
  822. else
  823. {
  824. /* Scroll animation is active */
  825. *num_display_lines = max_display_lines - 1;
  826. *fade_active = fade_enabled;
  827. if (scroll_up)
  828. {
  829. *line_offset = (phase / scroll_ticks) + 1;
  830. *y_offset = (float)line_height * (float)(scroll_ticks - line_phase) / (float)scroll_ticks;
  831. }
  832. else
  833. {
  834. *line_offset = excess_lines - (phase / scroll_ticks);
  835. *y_offset = (float)line_height * (1.0f - (float)(scroll_ticks - line_phase) / (float)scroll_ticks);
  836. }
  837. /* Set fade parameters if fade animation is active */
  838. if (*fade_active)
  839. set_line_smooth_fade_parameters(
  840. scroll_up, scroll_ticks, line_phase, line_height,
  841. num_lines, *num_display_lines, *line_offset, *y_offset,
  842. top_fade_line_offset, top_fade_y_offset, top_fade_alpha,
  843. bottom_fade_line_offset, bottom_fade_y_offset, bottom_fade_alpha);
  844. }
  845. /* Set 'default' fade parameters if fade animation
  846. * is inactive */
  847. if (!*fade_active)
  848. set_line_smooth_fade_parameters_default(
  849. top_fade_line_offset, top_fade_y_offset, top_fade_alpha,
  850. bottom_fade_line_offset, bottom_fade_y_offset, bottom_fade_alpha);
  851. }
  852. static void gfx_animation_line_ticker_smooth_loop(uint64_t idx,
  853. bool fade_enabled, size_t line_len, size_t line_height,
  854. size_t max_display_lines, size_t num_lines,
  855. size_t *num_display_lines, size_t *line_offset, float *y_offset,
  856. bool *fade_active,
  857. size_t *top_fade_line_offset, float *top_fade_y_offset, float *top_fade_alpha,
  858. size_t *bottom_fade_line_offset, float *bottom_fade_y_offset, float *bottom_fade_alpha)
  859. {
  860. size_t scroll_ticks = TICKER_LINE_SMOOTH_SCROLL_TICKS(line_len);
  861. size_t ticker_period = (num_lines + 1) * scroll_ticks;
  862. size_t phase = idx % ticker_period;
  863. size_t line_phase = phase % scroll_ticks;
  864. *line_offset = phase / scroll_ticks;
  865. if (line_phase == (scroll_ticks - 1))
  866. {
  867. /* Static display of max_display_lines
  868. * (no animation) */
  869. *num_display_lines = max_display_lines;
  870. *fade_active = false;
  871. }
  872. else
  873. {
  874. *num_display_lines = max_display_lines - 1;
  875. *fade_active = fade_enabled;
  876. }
  877. *y_offset = (float)line_height * (float)(scroll_ticks - line_phase) / (float)scroll_ticks;
  878. /* Set fade parameters */
  879. if (*fade_active)
  880. set_line_smooth_fade_parameters(
  881. true, scroll_ticks, line_phase, line_height,
  882. num_lines, *num_display_lines, *line_offset, *y_offset,
  883. top_fade_line_offset, top_fade_y_offset, top_fade_alpha,
  884. bottom_fade_line_offset, bottom_fade_y_offset, bottom_fade_alpha);
  885. else
  886. set_line_smooth_fade_parameters_default(
  887. top_fade_line_offset, top_fade_y_offset, top_fade_alpha,
  888. bottom_fade_line_offset, bottom_fade_y_offset, bottom_fade_alpha);
  889. }
  890. static void gfx_delayed_animation_cb(void *userdata)
  891. {
  892. gfx_delayed_animation_t *delayed_animation =
  893. (gfx_delayed_animation_t*) userdata;
  894. gfx_animation_push(&delayed_animation->entry);
  895. free(delayed_animation);
  896. }
  897. void gfx_animation_push_delayed(
  898. unsigned delay, gfx_animation_ctx_entry_t *entry)
  899. {
  900. gfx_timer_ctx_entry_t timer_entry;
  901. gfx_delayed_animation_t *delayed_animation = (gfx_delayed_animation_t*)
  902. malloc(sizeof(gfx_delayed_animation_t));
  903. memcpy(&delayed_animation->entry, entry, sizeof(gfx_animation_ctx_entry_t));
  904. timer_entry.cb = gfx_delayed_animation_cb;
  905. timer_entry.duration = delay;
  906. timer_entry.userdata = delayed_animation;
  907. gfx_animation_timer_start(&delayed_animation->timer, &timer_entry);
  908. }
  909. bool gfx_animation_push(gfx_animation_ctx_entry_t *entry)
  910. {
  911. struct tween t;
  912. gfx_animation_t *p_anim = &anim_st;
  913. t.duration = entry->duration;
  914. t.running_since = 0;
  915. t.initial_value = *entry->subject;
  916. t.target_value = entry->target_value;
  917. t.subject = entry->subject;
  918. t.tag = entry->tag;
  919. t.cb = entry->cb;
  920. t.userdata = entry->userdata;
  921. t.easing = NULL;
  922. t.deleted = false;
  923. switch (entry->easing_enum)
  924. {
  925. case EASING_LINEAR:
  926. t.easing = &easing_linear;
  927. break;
  928. /* Quad */
  929. case EASING_IN_QUAD:
  930. t.easing = &easing_in_quad;
  931. break;
  932. case EASING_OUT_QUAD:
  933. t.easing = &easing_out_quad;
  934. break;
  935. case EASING_IN_OUT_QUAD:
  936. t.easing = &easing_in_out_quad;
  937. break;
  938. case EASING_OUT_IN_QUAD:
  939. t.easing = &easing_out_in_quad;
  940. break;
  941. /* Cubic */
  942. case EASING_IN_CUBIC:
  943. t.easing = &easing_in_cubic;
  944. break;
  945. case EASING_OUT_CUBIC:
  946. t.easing = &easing_out_cubic;
  947. break;
  948. case EASING_IN_OUT_CUBIC:
  949. t.easing = &easing_in_out_cubic;
  950. break;
  951. case EASING_OUT_IN_CUBIC:
  952. t.easing = &easing_out_in_cubic;
  953. break;
  954. /* Quart */
  955. case EASING_IN_QUART:
  956. t.easing = &easing_in_quart;
  957. break;
  958. case EASING_OUT_QUART:
  959. t.easing = &easing_out_quart;
  960. break;
  961. case EASING_IN_OUT_QUART:
  962. t.easing = &easing_in_out_quart;
  963. break;
  964. case EASING_OUT_IN_QUART:
  965. t.easing = &easing_out_in_quart;
  966. break;
  967. /* Quint */
  968. case EASING_IN_QUINT:
  969. t.easing = &easing_in_quint;
  970. break;
  971. case EASING_OUT_QUINT:
  972. t.easing = &easing_out_quint;
  973. break;
  974. case EASING_IN_OUT_QUINT:
  975. t.easing = &easing_in_out_quint;
  976. break;
  977. case EASING_OUT_IN_QUINT:
  978. t.easing = &easing_out_in_quint;
  979. break;
  980. /* Sine */
  981. case EASING_IN_SINE:
  982. t.easing = &easing_in_sine;
  983. break;
  984. case EASING_OUT_SINE:
  985. t.easing = &easing_out_sine;
  986. break;
  987. case EASING_IN_OUT_SINE:
  988. t.easing = &easing_in_out_sine;
  989. break;
  990. case EASING_OUT_IN_SINE:
  991. t.easing = &easing_out_in_sine;
  992. break;
  993. /* Expo */
  994. case EASING_IN_EXPO:
  995. t.easing = &easing_in_expo;
  996. break;
  997. case EASING_OUT_EXPO:
  998. t.easing = &easing_out_expo;
  999. break;
  1000. case EASING_IN_OUT_EXPO:
  1001. t.easing = &easing_in_out_expo;
  1002. break;
  1003. case EASING_OUT_IN_EXPO:
  1004. t.easing = &easing_out_in_expo;
  1005. break;
  1006. /* Circ */
  1007. case EASING_IN_CIRC:
  1008. t.easing = &easing_in_circ;
  1009. break;
  1010. case EASING_OUT_CIRC:
  1011. t.easing = &easing_out_circ;
  1012. break;
  1013. case EASING_IN_OUT_CIRC:
  1014. t.easing = &easing_in_out_circ;
  1015. break;
  1016. case EASING_OUT_IN_CIRC:
  1017. t.easing = &easing_out_in_circ;
  1018. break;
  1019. /* Bounce */
  1020. case EASING_IN_BOUNCE:
  1021. t.easing = &easing_in_bounce;
  1022. break;
  1023. case EASING_OUT_BOUNCE:
  1024. t.easing = &easing_out_bounce;
  1025. break;
  1026. case EASING_IN_OUT_BOUNCE:
  1027. t.easing = &easing_in_out_bounce;
  1028. break;
  1029. case EASING_OUT_IN_BOUNCE:
  1030. t.easing = &easing_out_in_bounce;
  1031. break;
  1032. default:
  1033. break;
  1034. }
  1035. /* ignore born dead tweens */
  1036. if (!t.easing || t.duration == 0 || t.initial_value == t.target_value)
  1037. return false;
  1038. if (p_anim->flags & GFX_ANIM_FLAG_IN_UPDATE)
  1039. RBUF_PUSH(p_anim->pending, t);
  1040. else
  1041. RBUF_PUSH(p_anim->list, t);
  1042. return true;
  1043. }
  1044. bool gfx_animation_update(
  1045. retro_time_t current_time,
  1046. bool timedate_enable,
  1047. float _ticker_speed,
  1048. unsigned video_width,
  1049. unsigned video_height)
  1050. {
  1051. unsigned i;
  1052. gfx_animation_t *p_anim = &anim_st;
  1053. const bool ticker_is_active = p_anim->flags & GFX_ANIM_FLAG_TICKER_IS_ACTIVE;
  1054. static retro_time_t last_clock_update = 0;
  1055. static retro_time_t last_ticker_update = 0;
  1056. static retro_time_t last_ticker_slow_update = 0;
  1057. /* Horizontal smooth ticker parameters */
  1058. static float ticker_pixel_accumulator = 0.0f;
  1059. unsigned ticker_pixel_accumulator_uint = 0;
  1060. float ticker_pixel_increment = 0.0f;
  1061. /* Vertical (line) smooth ticker parameters */
  1062. static float ticker_pixel_line_accumulator = 0.0f;
  1063. unsigned ticker_pixel_line_accumulator_uint = 0;
  1064. float ticker_pixel_line_increment = 0.0f;
  1065. /* Adjust ticker speed */
  1066. float speed_factor =
  1067. (_ticker_speed > 0.0001f) ? _ticker_speed : 1.0f;
  1068. unsigned ticker_speed =
  1069. (unsigned)(((float)TICKER_SPEED / speed_factor) + 0.5);
  1070. unsigned ticker_slow_speed =
  1071. (unsigned)(((float)TICKER_SLOW_SPEED / speed_factor) + 0.5);
  1072. /* Note: cur_time & old_time are in us (microseconds),
  1073. * delta_time is in ms */
  1074. p_anim->cur_time = current_time;
  1075. p_anim->delta_time = (p_anim->old_time == 0)
  1076. ? 0.0f
  1077. : (float)(p_anim->cur_time - p_anim->old_time) / 1000.0f;
  1078. p_anim->old_time = p_anim->cur_time;
  1079. if (((p_anim->cur_time - last_clock_update) > 1000000) /* 1000000 us == 1 second */
  1080. && timedate_enable)
  1081. {
  1082. p_anim->flags |= GFX_ANIM_FLAG_IS_ACTIVE;
  1083. last_clock_update = p_anim->cur_time;
  1084. }
  1085. if (ticker_is_active)
  1086. {
  1087. /* Update non-smooth ticker indices */
  1088. if (p_anim->cur_time - last_ticker_update >= ticker_speed)
  1089. {
  1090. p_anim->ticker_idx++;
  1091. last_ticker_update = p_anim->cur_time;
  1092. }
  1093. if (p_anim->cur_time - last_ticker_slow_update >= ticker_slow_speed)
  1094. {
  1095. p_anim->ticker_slow_idx++;
  1096. last_ticker_slow_update = p_anim->cur_time;
  1097. }
  1098. /* Pixel tickers (horizontal + vertical/line) update
  1099. * every frame (regardless of time delta), so require
  1100. * special handling */
  1101. /* > Get base increment size (+1 every TICKER_PIXEL_PERIOD ms) */
  1102. ticker_pixel_increment = p_anim->delta_time / TICKER_PIXEL_PERIOD;
  1103. /* > Apply ticker speed adjustment */
  1104. ticker_pixel_increment *= speed_factor;
  1105. /* At this point we diverge:
  1106. * > Vertical (line) ticker is based upon text
  1107. * characteristics (number of characters per
  1108. * line) - it is therefore independent of display
  1109. * size/scaling, so speed-adjusted pixel increment
  1110. * is used directly */
  1111. ticker_pixel_line_increment = ticker_pixel_increment;
  1112. /* > Horizontal ticker is based upon physical line
  1113. * width - it is therefore very much dependent upon
  1114. * display size/scaling. Each menu driver is free
  1115. * to handle video scaling as it pleases - a callback
  1116. * function set by the menu driver is thus used to
  1117. * perform menu-specific scaling adjustments */
  1118. if (p_anim->updatetime_cb)
  1119. p_anim->updatetime_cb(&ticker_pixel_increment,
  1120. video_width, video_height);
  1121. /* > Update accumulators */
  1122. ticker_pixel_accumulator += ticker_pixel_increment;
  1123. ticker_pixel_accumulator_uint = (unsigned)ticker_pixel_accumulator;
  1124. ticker_pixel_line_accumulator += ticker_pixel_line_increment;
  1125. ticker_pixel_line_accumulator_uint = (unsigned)ticker_pixel_line_accumulator;
  1126. /* > Check whether we've accumulated enough
  1127. * for an idx update */
  1128. if (ticker_pixel_accumulator_uint > 0)
  1129. {
  1130. p_anim->ticker_pixel_idx += ticker_pixel_accumulator_uint;
  1131. ticker_pixel_accumulator -= (float)ticker_pixel_accumulator_uint;
  1132. }
  1133. if (ticker_pixel_accumulator_uint > 0)
  1134. {
  1135. p_anim->ticker_pixel_line_idx += ticker_pixel_line_accumulator_uint;
  1136. ticker_pixel_line_accumulator -= (float)ticker_pixel_line_accumulator_uint;
  1137. }
  1138. }
  1139. p_anim->flags |= GFX_ANIM_FLAG_IN_UPDATE;
  1140. p_anim->flags &= ~GFX_ANIM_FLAG_PENDING_DELETES;
  1141. for (i = 0; i < RBUF_LEN(p_anim->list); i++)
  1142. {
  1143. struct tween *tween = &p_anim->list[i];
  1144. if (tween->deleted)
  1145. continue;
  1146. tween->running_since += p_anim->delta_time;
  1147. *tween->subject = tween->easing(
  1148. tween->running_since,
  1149. tween->initial_value,
  1150. tween->target_value - tween->initial_value,
  1151. tween->duration);
  1152. if (tween->running_since >= tween->duration)
  1153. {
  1154. *tween->subject = tween->target_value;
  1155. if (tween->cb)
  1156. tween->cb(tween->userdata);
  1157. RBUF_REMOVE(p_anim->list, i);
  1158. i--;
  1159. }
  1160. }
  1161. if (p_anim->flags & GFX_ANIM_FLAG_PENDING_DELETES)
  1162. {
  1163. for (i = 0; i < RBUF_LEN(p_anim->list); i++)
  1164. {
  1165. struct tween *tween = &p_anim->list[i];
  1166. if (tween->deleted)
  1167. {
  1168. RBUF_REMOVE(p_anim->list, i);
  1169. i--;
  1170. }
  1171. }
  1172. p_anim->flags &= ~GFX_ANIM_FLAG_PENDING_DELETES;
  1173. }
  1174. if (RBUF_LEN(p_anim->pending) > 0)
  1175. {
  1176. size_t list_len = RBUF_LEN(p_anim->list);
  1177. size_t pending_len = RBUF_LEN(p_anim->pending);
  1178. RBUF_RESIZE(p_anim->list, list_len + pending_len);
  1179. memcpy(p_anim->list + list_len, p_anim->pending,
  1180. sizeof(*p_anim->pending) * pending_len);
  1181. RBUF_CLEAR(p_anim->pending);
  1182. }
  1183. p_anim->flags &= ~GFX_ANIM_FLAG_IN_UPDATE;
  1184. if (RBUF_LEN(p_anim->list) > 0)
  1185. p_anim->flags |= GFX_ANIM_FLAG_IS_ACTIVE;
  1186. else
  1187. p_anim->flags &= ~GFX_ANIM_FLAG_IS_ACTIVE;
  1188. return ((p_anim->flags & GFX_ANIM_FLAG_IS_ACTIVE) > 0);
  1189. }
  1190. static void build_ticker_loop_string(
  1191. const char* src_str, const char *spacer,
  1192. size_t char_offset1, size_t num_chars1,
  1193. size_t char_offset2, size_t num_chars2,
  1194. size_t char_offset3, size_t num_chars3,
  1195. char *dest_str, size_t dest_str_len)
  1196. {
  1197. /* Copy 'trailing' chunk of source string, if required */
  1198. if (num_chars1 > 0)
  1199. utf8cpy(
  1200. dest_str, dest_str_len,
  1201. utf8skip(src_str, char_offset1), num_chars1);
  1202. else
  1203. dest_str[0] = '\0';
  1204. /* Copy chunk of spacer string, if required */
  1205. if (num_chars2 > 0)
  1206. {
  1207. char tmp[PATH_MAX_LENGTH];
  1208. utf8cpy(
  1209. tmp, sizeof(tmp),
  1210. utf8skip(spacer, char_offset2), num_chars2);
  1211. strlcat(dest_str, tmp, dest_str_len);
  1212. }
  1213. /* Copy 'leading' chunk of source string, if required */
  1214. if (num_chars3 > 0)
  1215. {
  1216. char tmp[PATH_MAX_LENGTH];
  1217. utf8cpy(
  1218. tmp, sizeof(tmp),
  1219. utf8skip(src_str, char_offset3), num_chars3);
  1220. strlcat(dest_str, tmp, dest_str_len);
  1221. }
  1222. }
  1223. static void build_line_ticker_string(
  1224. size_t num_display_lines, size_t line_offset,
  1225. struct string_list *lines,
  1226. char *dest_str, size_t dest_str_len)
  1227. {
  1228. size_t i;
  1229. for (i = 0; i < (num_display_lines-1); i++)
  1230. {
  1231. size_t offset = i + line_offset;
  1232. size_t line_index = offset % (lines->size + 1);
  1233. /* Is line valid? */
  1234. if (line_index < lines->size)
  1235. strlcat(dest_str, lines->elems[line_index].data, dest_str_len);
  1236. strlcat(dest_str, "\n", dest_str_len);
  1237. }
  1238. {
  1239. size_t offset = (num_display_lines-1) + line_offset;
  1240. size_t line_index = offset % (lines->size + 1);
  1241. /* Is line valid? */
  1242. if (line_index < lines->size)
  1243. strlcat(dest_str, lines->elems[line_index].data, dest_str_len);
  1244. }
  1245. }
  1246. bool gfx_animation_ticker(gfx_animation_ctx_ticker_t *ticker)
  1247. {
  1248. gfx_animation_t *p_anim = &anim_st;
  1249. size_t str_len = utf8len(ticker->str);
  1250. if (!ticker->spacer)
  1251. ticker->spacer = TICKER_SPACER_DEFAULT;
  1252. if ((size_t)str_len <= ticker->len)
  1253. {
  1254. utf8cpy(ticker->s,
  1255. PATH_MAX_LENGTH,
  1256. ticker->str,
  1257. ticker->len);
  1258. return false;
  1259. }
  1260. if (!ticker->selected)
  1261. {
  1262. size_t len = utf8cpy(ticker->s,
  1263. PATH_MAX_LENGTH, ticker->str, ticker->len - 3);
  1264. ticker->s[len ] = '.';
  1265. ticker->s[len+1] = '.';
  1266. ticker->s[len+2] = '.';
  1267. ticker->s[len+3] = '\0';
  1268. return false;
  1269. }
  1270. /* Note: If we reach this point then str_len > ticker->len
  1271. * (previously had an unecessary 'if (str_len > ticker->len)'
  1272. * check here...) */
  1273. switch (ticker->type_enum)
  1274. {
  1275. case TICKER_TYPE_LOOP:
  1276. {
  1277. size_t offset1, offset2, offset3;
  1278. size_t width1, width2, width3;
  1279. gfx_animation_ticker_loop(
  1280. ticker->idx,
  1281. ticker->len,
  1282. str_len, utf8len(ticker->spacer),
  1283. &offset1, &width1,
  1284. &offset2, &width2,
  1285. &offset3, &width3);
  1286. build_ticker_loop_string(
  1287. ticker->str, ticker->spacer,
  1288. offset1, width1,
  1289. offset2, width2,
  1290. offset3, width3,
  1291. ticker->s, PATH_MAX_LENGTH);
  1292. }
  1293. break;
  1294. case TICKER_TYPE_BOUNCE:
  1295. default:
  1296. {
  1297. size_t offset = gfx_animation_ticker_generic(
  1298. ticker->idx,
  1299. str_len - ticker->len);
  1300. str_len = ticker->len;
  1301. utf8cpy(
  1302. ticker->s,
  1303. PATH_MAX_LENGTH,
  1304. utf8skip(ticker->str, offset),
  1305. str_len);
  1306. }
  1307. break;
  1308. }
  1309. p_anim->flags |= GFX_ANIM_FLAG_TICKER_IS_ACTIVE;
  1310. return true;
  1311. }
  1312. /* 'Fixed width' font version of gfx_animation_ticker_smooth() */
  1313. static bool gfx_animation_ticker_smooth_fw(
  1314. gfx_animation_t *p_anim,
  1315. gfx_animation_ctx_ticker_smooth_t *ticker)
  1316. {
  1317. size_t spacer_len = 0;
  1318. unsigned glyph_width = ticker->glyph_width;
  1319. unsigned src_str_width = 0;
  1320. unsigned spacer_width = 0;
  1321. bool success = false;
  1322. bool is_active = false;
  1323. /* Sanity check has already been performed by
  1324. * gfx_animation_ticker_smooth() - no need to
  1325. * repeat */
  1326. /* Get length + width of src string */
  1327. size_t src_str_len = utf8len(ticker->src_str);
  1328. if (src_str_len < 1)
  1329. goto end;
  1330. src_str_width = (unsigned)(src_str_len * glyph_width);
  1331. /* If src string width is <= text field width, we
  1332. * can just copy the entire string */
  1333. if (src_str_width <= ticker->field_width)
  1334. {
  1335. utf8cpy(ticker->dst_str, ticker->dst_str_len,
  1336. ticker->src_str, src_str_len);
  1337. if (ticker->dst_str_width)
  1338. *ticker->dst_str_width = src_str_width;
  1339. *ticker->x_offset = 0;
  1340. success = true;
  1341. goto end;
  1342. }
  1343. /* If entry is not selected, just clip input string
  1344. * and add '...' suffix */
  1345. if (!ticker->selected)
  1346. {
  1347. size_t _len;
  1348. unsigned num_chars = 0;
  1349. unsigned suffix_len = 3;
  1350. unsigned suffix_width = suffix_len * glyph_width;
  1351. /* Sanity check */
  1352. if (ticker->field_width < suffix_width)
  1353. goto end;
  1354. /* Determine number of characters to copy */
  1355. num_chars = (ticker->field_width - suffix_width) / glyph_width;
  1356. /* Copy string segment + add suffix */
  1357. _len = utf8cpy(ticker->dst_str, ticker->dst_str_len, ticker->src_str, num_chars);
  1358. ticker->dst_str[_len ] = '.';
  1359. ticker->dst_str[_len+1] = '.';
  1360. ticker->dst_str[_len+2] = '.';
  1361. ticker->dst_str[_len+3] = '\0';
  1362. if (ticker->dst_str_width)
  1363. *ticker->dst_str_width = (num_chars * glyph_width) + suffix_width;
  1364. *ticker->x_offset = 0;
  1365. success = true;
  1366. goto end;
  1367. }
  1368. /* If we get this far, then a scrolling animation
  1369. * is required... */
  1370. /* Use default spacer, if none is provided */
  1371. if (!ticker->spacer)
  1372. ticker->spacer = TICKER_SPACER_DEFAULT;
  1373. /* Get length + width of spacer */
  1374. if ((spacer_len = utf8len(ticker->spacer)) < 1)
  1375. goto end;
  1376. spacer_width = (unsigned)(spacer_len * glyph_width);
  1377. /* Determine animation type */
  1378. switch (ticker->type_enum)
  1379. {
  1380. case TICKER_TYPE_LOOP:
  1381. {
  1382. unsigned char_offset1 = 0;
  1383. unsigned num_chars1 = 0;
  1384. unsigned char_offset2 = 0;
  1385. unsigned num_chars2 = 0;
  1386. unsigned char_offset3 = 0;
  1387. unsigned num_chars3 = 0;
  1388. gfx_animation_ticker_smooth_loop_fw(
  1389. ticker->idx,
  1390. src_str_width, src_str_len, spacer_width, spacer_len,
  1391. glyph_width, ticker->field_width,
  1392. &char_offset1, &num_chars1,
  1393. &char_offset2, &num_chars2,
  1394. &char_offset3, &num_chars3,
  1395. ticker->x_offset);
  1396. build_ticker_loop_string(
  1397. ticker->src_str, ticker->spacer,
  1398. char_offset1, num_chars1,
  1399. char_offset2, num_chars2,
  1400. char_offset3, num_chars3,
  1401. ticker->dst_str, ticker->dst_str_len);
  1402. if (ticker->dst_str_width)
  1403. *ticker->dst_str_width = (num_chars1 + num_chars2 + num_chars3) * glyph_width;
  1404. break;
  1405. }
  1406. case TICKER_TYPE_BOUNCE:
  1407. default:
  1408. {
  1409. unsigned char_offset = 0;
  1410. unsigned num_chars = 0;
  1411. gfx_animation_ticker_smooth_generic_fw(
  1412. ticker->idx,
  1413. src_str_width, src_str_len, glyph_width, ticker->field_width,
  1414. &char_offset, &num_chars, ticker->x_offset);
  1415. /* Copy required substring */
  1416. if (num_chars > 0)
  1417. utf8cpy(
  1418. ticker->dst_str, ticker->dst_str_len,
  1419. utf8skip(ticker->src_str, char_offset), num_chars);
  1420. else
  1421. ticker->dst_str[0] = '\0';
  1422. if (ticker->dst_str_width)
  1423. *ticker->dst_str_width = num_chars * glyph_width;
  1424. break;
  1425. }
  1426. }
  1427. success = true;
  1428. is_active = true;
  1429. p_anim->flags |= GFX_ANIM_FLAG_TICKER_IS_ACTIVE;
  1430. end:
  1431. if (!success)
  1432. {
  1433. *ticker->x_offset = 0;
  1434. if (ticker->dst_str_len > 0)
  1435. ticker->dst_str[0] = '\0';
  1436. }
  1437. return is_active;
  1438. }
  1439. bool gfx_animation_ticker_smooth(gfx_animation_ctx_ticker_smooth_t *ticker)
  1440. {
  1441. size_t i;
  1442. size_t src_str_len = 0;
  1443. size_t spacer_len = 0;
  1444. unsigned small_src_char_widths[64] = {0};
  1445. unsigned src_str_width = 0;
  1446. unsigned spacer_width = 0;
  1447. unsigned *src_char_widths = NULL;
  1448. unsigned *spacer_char_widths = NULL;
  1449. const char *str_ptr = NULL;
  1450. bool success = false;
  1451. bool is_active = false;
  1452. gfx_animation_t *p_anim = &anim_st;
  1453. /* Sanity check */
  1454. if (string_is_empty(ticker->src_str) ||
  1455. (ticker->dst_str_len < 1) ||
  1456. (ticker->field_width < 1) || (
  1457. #ifdef HAVE_FONT
  1458. !ticker->font &&
  1459. #endif
  1460. (ticker->glyph_width < 1))
  1461. )
  1462. goto end;
  1463. /* If we are using a fixed width font (ticker->font == NULL),
  1464. * switch to optimised code path */
  1465. #ifdef HAVE_FONT
  1466. if (!ticker->font)
  1467. #endif
  1468. return gfx_animation_ticker_smooth_fw(p_anim, ticker);
  1469. #ifdef HAVE_FONT
  1470. /* Find the display width of each character in
  1471. * the src string + total width */
  1472. if ((src_str_len = utf8len(ticker->src_str)) < 1)
  1473. goto end;
  1474. src_char_widths = small_src_char_widths;
  1475. if (src_str_len > ARRAY_SIZE(small_src_char_widths))
  1476. {
  1477. if (!(src_char_widths = (unsigned*)calloc(src_str_len, sizeof(unsigned))))
  1478. goto end;
  1479. }
  1480. str_ptr = ticker->src_str;
  1481. for (i = 0; i < src_str_len; i++)
  1482. {
  1483. int glyph_width = font_driver_get_message_width(
  1484. ticker->font, str_ptr, 1, ticker->font_scale);
  1485. if (glyph_width < 0)
  1486. goto end;
  1487. src_char_widths[i] = (unsigned)glyph_width;
  1488. src_str_width += (unsigned)glyph_width;
  1489. str_ptr = utf8skip(str_ptr, 1);
  1490. }
  1491. /* If total src string width is <= text field width, we
  1492. * can just copy the entire string */
  1493. if (src_str_width <= ticker->field_width)
  1494. {
  1495. utf8cpy(ticker->dst_str, ticker->dst_str_len,
  1496. ticker->src_str, src_str_len);
  1497. if (ticker->dst_str_width)
  1498. *ticker->dst_str_width = src_str_width;
  1499. *ticker->x_offset = 0;
  1500. success = true;
  1501. goto end;
  1502. }
  1503. /* If entry is not selected, just clip input string
  1504. * and add '...' suffix */
  1505. if (!ticker->selected)
  1506. {
  1507. size_t _len;
  1508. unsigned text_width;
  1509. unsigned current_width = 0;
  1510. unsigned num_chars = 0;
  1511. int period_width =
  1512. font_driver_get_message_width(ticker->font,
  1513. ".", 1, ticker->font_scale);
  1514. /* Sanity check */
  1515. if (period_width < 0)
  1516. goto end;
  1517. if (ticker->field_width < (3 * (unsigned)period_width))
  1518. goto end;
  1519. /* Determine number of characters to copy */
  1520. text_width = ticker->field_width - (3 * period_width);
  1521. for (;;)
  1522. {
  1523. current_width += src_char_widths[num_chars];
  1524. if (current_width > text_width)
  1525. {
  1526. /* Have to go back one in order to get 'actual'
  1527. * value for dst_str_width */
  1528. current_width -= src_char_widths[num_chars];
  1529. break;
  1530. }
  1531. num_chars++;
  1532. }
  1533. /* Copy string segment + add suffix */
  1534. _len = utf8cpy(ticker->dst_str, ticker->dst_str_len,
  1535. ticker->src_str, num_chars);
  1536. ticker->dst_str[_len ] = '.';
  1537. ticker->dst_str[_len+1] = '.';
  1538. ticker->dst_str[_len+2] = '.';
  1539. ticker->dst_str[_len+3] = '\0';
  1540. if (ticker->dst_str_width)
  1541. *ticker->dst_str_width = current_width + (3 * period_width);
  1542. *ticker->x_offset = 0;
  1543. success = true;
  1544. goto end;
  1545. }
  1546. /* If we get this far, then a scrolling animation
  1547. * is required... */
  1548. /* Use default spacer, if none is provided */
  1549. if (!ticker->spacer)
  1550. ticker->spacer = TICKER_SPACER_DEFAULT;
  1551. /* Find the display width of each character in
  1552. * the spacer */
  1553. if ((spacer_len = utf8len(ticker->spacer)) < 1)
  1554. goto end;
  1555. if (!(spacer_char_widths = (unsigned*)calloc(spacer_len, sizeof(unsigned))))
  1556. goto end;
  1557. str_ptr = ticker->spacer;
  1558. for (i = 0; i < spacer_len; i++)
  1559. {
  1560. int glyph_width = font_driver_get_message_width(
  1561. ticker->font, str_ptr, 1, ticker->font_scale);
  1562. if (glyph_width < 0)
  1563. goto end;
  1564. spacer_char_widths[i] = (unsigned)glyph_width;
  1565. spacer_width += (unsigned)glyph_width;
  1566. str_ptr = utf8skip(str_ptr, 1);
  1567. }
  1568. /* Determine animation type */
  1569. switch (ticker->type_enum)
  1570. {
  1571. case TICKER_TYPE_LOOP:
  1572. {
  1573. unsigned char_offset1 = 0;
  1574. unsigned num_chars1 = 0;
  1575. unsigned char_offset2 = 0;
  1576. unsigned num_chars2 = 0;
  1577. unsigned char_offset3 = 0;
  1578. unsigned num_chars3 = 0;
  1579. gfx_animation_ticker_smooth_loop(
  1580. ticker->idx,
  1581. src_char_widths, src_str_len,
  1582. spacer_char_widths, spacer_len,
  1583. src_str_width, spacer_width, ticker->field_width,
  1584. &char_offset1, &num_chars1,
  1585. &char_offset2, &num_chars2,
  1586. &char_offset3, &num_chars3,
  1587. ticker->x_offset, ticker->dst_str_width);
  1588. build_ticker_loop_string(
  1589. ticker->src_str, ticker->spacer,
  1590. char_offset1, num_chars1,
  1591. char_offset2, num_chars2,
  1592. char_offset3, num_chars3,
  1593. ticker->dst_str, ticker->dst_str_len);
  1594. break;
  1595. }
  1596. case TICKER_TYPE_BOUNCE:
  1597. default:
  1598. {
  1599. unsigned char_offset = 0;
  1600. unsigned num_chars = 0;
  1601. gfx_animation_ticker_smooth_generic(
  1602. ticker->idx,
  1603. src_char_widths, src_str_len,
  1604. src_str_width, ticker->field_width,
  1605. &char_offset, &num_chars,
  1606. ticker->x_offset, ticker->dst_str_width);
  1607. /* Copy required substring */
  1608. if (num_chars > 0)
  1609. utf8cpy(
  1610. ticker->dst_str, ticker->dst_str_len,
  1611. utf8skip(ticker->src_str, char_offset), num_chars);
  1612. else
  1613. ticker->dst_str[0] = '\0';
  1614. break;
  1615. }
  1616. }
  1617. success = true;
  1618. is_active = true;
  1619. p_anim->flags |= GFX_ANIM_FLAG_TICKER_IS_ACTIVE;
  1620. #endif
  1621. end:
  1622. if (src_char_widths != small_src_char_widths && src_char_widths)
  1623. {
  1624. free(src_char_widths);
  1625. src_char_widths = NULL;
  1626. }
  1627. if (spacer_char_widths)
  1628. {
  1629. free(spacer_char_widths);
  1630. spacer_char_widths = NULL;
  1631. }
  1632. if (!success)
  1633. {
  1634. *ticker->x_offset = 0;
  1635. if (ticker->dst_str_len > 0)
  1636. ticker->dst_str[0] = '\0';
  1637. }
  1638. return is_active;
  1639. }
  1640. bool gfx_animation_line_ticker(gfx_animation_ctx_line_ticker_t *line_ticker)
  1641. {
  1642. char *wrapped_str = NULL;
  1643. size_t wrapped_str_len = 0;
  1644. size_t line_ticker_str_len = 0;
  1645. struct string_list lines = {0};
  1646. size_t line_offset = 0;
  1647. bool success = false;
  1648. bool is_active = false;
  1649. gfx_animation_t *p_anim = &anim_st;
  1650. /* Sanity check */
  1651. if (!line_ticker)
  1652. return false;
  1653. if (string_is_empty(line_ticker->str) ||
  1654. (line_ticker->line_len < 1) ||
  1655. (line_ticker->max_lines < 1))
  1656. goto end;
  1657. /* Line wrap input string */
  1658. line_ticker_str_len = strlen(line_ticker->str);
  1659. wrapped_str_len = line_ticker_str_len + 1 + 10; /* 10 bytes use for inserting '\n' */
  1660. if (!(wrapped_str = (char*)malloc(wrapped_str_len)))
  1661. goto end;
  1662. wrapped_str[0] = '\0';
  1663. word_wrap(
  1664. wrapped_str,
  1665. wrapped_str_len,
  1666. line_ticker->str,
  1667. line_ticker_str_len,
  1668. (int)line_ticker->line_len,
  1669. 100, 0);
  1670. if (string_is_empty(wrapped_str))
  1671. goto end;
  1672. /* Split into component lines */
  1673. string_list_initialize(&lines);
  1674. if (!string_split_noalloc(&lines, wrapped_str, "\n"))
  1675. goto end;
  1676. /* Check whether total number of lines fits within
  1677. * the set limit */
  1678. if (lines.size <= line_ticker->max_lines)
  1679. {
  1680. strlcpy(line_ticker->s, wrapped_str, line_ticker->len);
  1681. success = true;
  1682. goto end;
  1683. }
  1684. /* Determine offset of first line in wrapped string */
  1685. switch (line_ticker->type_enum)
  1686. {
  1687. case TICKER_TYPE_LOOP:
  1688. line_offset = gfx_animation_line_ticker_loop(
  1689. line_ticker->idx,
  1690. line_ticker->line_len,
  1691. lines.size);
  1692. break;
  1693. case TICKER_TYPE_BOUNCE:
  1694. default:
  1695. line_offset = gfx_animation_line_ticker_generic(
  1696. line_ticker->idx,
  1697. line_ticker->line_len,
  1698. line_ticker->max_lines,
  1699. lines.size);
  1700. break;
  1701. }
  1702. /* Build output string from required lines */
  1703. build_line_ticker_string(
  1704. line_ticker->max_lines, line_offset, &lines,
  1705. line_ticker->s, line_ticker->len);
  1706. success = true;
  1707. is_active = true;
  1708. p_anim->flags |= GFX_ANIM_FLAG_TICKER_IS_ACTIVE;
  1709. end:
  1710. if (wrapped_str)
  1711. {
  1712. free(wrapped_str);
  1713. wrapped_str = NULL;
  1714. }
  1715. string_list_deinitialize(&lines);
  1716. if (!success)
  1717. if (line_ticker->len > 0)
  1718. line_ticker->s[0] = '\0';
  1719. return is_active;
  1720. }
  1721. bool gfx_animation_line_ticker_smooth(gfx_animation_ctx_line_ticker_smooth_t *line_ticker)
  1722. {
  1723. char *wrapped_str = NULL;
  1724. size_t line_ticker_src_len = 0;
  1725. size_t wrapped_str_len = 0;
  1726. struct string_list lines = {0};
  1727. int glyph_width = 0;
  1728. int glyph_height = 0;
  1729. size_t line_len = 0;
  1730. size_t max_display_lines = 0;
  1731. size_t num_display_lines = 0;
  1732. size_t line_offset = 0;
  1733. size_t top_fade_line_offset = 0;
  1734. size_t bottom_fade_line_offset = 0;
  1735. bool fade_active = false;
  1736. bool success = false;
  1737. bool is_active = false;
  1738. gfx_animation_t *p_anim = &anim_st;
  1739. const char *wideglyph_str = NULL;
  1740. int wideglyph_width = 100;
  1741. void (*word_wrap_func)(char *dst, size_t dst_size,
  1742. const char *src, size_t src_len,
  1743. int line_width, int wideglyph_width, unsigned max_lines);
  1744. /* Sanity check */
  1745. if (!line_ticker)
  1746. return false;
  1747. #ifdef HAVE_FONT
  1748. if (!line_ticker->font ||
  1749. string_is_empty(line_ticker->src_str) ||
  1750. (line_ticker->field_width < 1) ||
  1751. (line_ticker->field_height < 1))
  1752. goto end;
  1753. /* Get font dimensions */
  1754. /* > Width
  1755. * This is a bit of a fudge. Performing a 'font aware'
  1756. * (i.e. character display width) word wrap is too CPU
  1757. * intensive, so we just sample the width of a common
  1758. * character and hope for the best. (We choose 'a' because
  1759. * this is what Ozone uses for spacing calculations, and
  1760. * it is proven to work quite well) */
  1761. if ((glyph_width = font_driver_get_message_width(
  1762. line_ticker->font, "a", 1, line_ticker->font_scale)) <= 0)
  1763. goto end;
  1764. if ((wideglyph_str = msg_hash_get_wideglyph_str()))
  1765. {
  1766. int new_glyph_width = font_driver_get_message_width(
  1767. line_ticker->font, wideglyph_str, strlen(wideglyph_str),
  1768. line_ticker->font_scale);
  1769. if (new_glyph_width > 0)
  1770. wideglyph_width = new_glyph_width * 100 / glyph_width;
  1771. word_wrap_func = word_wrap_wideglyph;
  1772. }
  1773. else
  1774. word_wrap_func = word_wrap;
  1775. /* > Height */
  1776. if ((glyph_height = font_driver_get_line_height(
  1777. line_ticker->font, line_ticker->font_scale)) <= 0)
  1778. goto end;
  1779. /* Determine line wrap parameters */
  1780. line_len = (size_t)(line_ticker->field_width / glyph_width);
  1781. max_display_lines = (size_t)(line_ticker->field_height / glyph_height);
  1782. if ((line_len < 1) || (max_display_lines < 1))
  1783. goto end;
  1784. /* Line wrap input string */
  1785. line_ticker_src_len = strlen(line_ticker->src_str);
  1786. /* 10 bytes use for inserting '\n' */
  1787. wrapped_str_len = line_ticker_src_len + 1 + 10;
  1788. if (!(wrapped_str = (char*)malloc(wrapped_str_len)))
  1789. goto end;
  1790. wrapped_str[0] = '\0';
  1791. (word_wrap_func)(
  1792. wrapped_str,
  1793. wrapped_str_len,
  1794. line_ticker->src_str,
  1795. line_ticker_src_len,
  1796. (int)line_len,
  1797. wideglyph_width, 0);
  1798. if (string_is_empty(wrapped_str))
  1799. goto end;
  1800. string_list_initialize(&lines);
  1801. /* Split into component lines */
  1802. if (!string_split_noalloc(&lines, wrapped_str, "\n"))
  1803. goto end;
  1804. /* Check whether total number of lines fits within
  1805. * the set field limit */
  1806. if (lines.size <= max_display_lines)
  1807. {
  1808. strlcpy(line_ticker->dst_str, wrapped_str, line_ticker->dst_str_len);
  1809. *line_ticker->y_offset = 0.0f;
  1810. /* No fade animation is required */
  1811. if (line_ticker->fade_enabled)
  1812. {
  1813. if (line_ticker->top_fade_str_len > 0)
  1814. line_ticker->top_fade_str[0] = '\0';
  1815. if (line_ticker->bottom_fade_str_len > 0)
  1816. line_ticker->bottom_fade_str[0] = '\0';
  1817. *line_ticker->top_fade_y_offset = 0.0f;
  1818. *line_ticker->bottom_fade_y_offset = 0.0f;
  1819. *line_ticker->top_fade_alpha = 0.0f;
  1820. *line_ticker->bottom_fade_alpha = 0.0f;
  1821. }
  1822. success = true;
  1823. goto end;
  1824. }
  1825. /* Determine which lines should be shown, along with
  1826. * y axis draw offset */
  1827. switch (line_ticker->type_enum)
  1828. {
  1829. case TICKER_TYPE_LOOP:
  1830. gfx_animation_line_ticker_smooth_loop(
  1831. line_ticker->idx,
  1832. line_ticker->fade_enabled,
  1833. line_len, (size_t)glyph_height,
  1834. max_display_lines, lines.size,
  1835. &num_display_lines, &line_offset, line_ticker->y_offset,
  1836. &fade_active,
  1837. &top_fade_line_offset, line_ticker->top_fade_y_offset, line_ticker->top_fade_alpha,
  1838. &bottom_fade_line_offset, line_ticker->bottom_fade_y_offset, line_ticker->bottom_fade_alpha);
  1839. break;
  1840. case TICKER_TYPE_BOUNCE:
  1841. default:
  1842. gfx_animation_line_ticker_smooth_generic(
  1843. line_ticker->idx,
  1844. line_ticker->fade_enabled,
  1845. line_len, (size_t)glyph_height,
  1846. max_display_lines, lines.size,
  1847. &num_display_lines, &line_offset, line_ticker->y_offset,
  1848. &fade_active,
  1849. &top_fade_line_offset, line_ticker->top_fade_y_offset, line_ticker->top_fade_alpha,
  1850. &bottom_fade_line_offset, line_ticker->bottom_fade_y_offset, line_ticker->bottom_fade_alpha);
  1851. break;
  1852. }
  1853. /* Build output string from required lines */
  1854. build_line_ticker_string(
  1855. num_display_lines, line_offset, &lines,
  1856. line_ticker->dst_str, line_ticker->dst_str_len);
  1857. /* Extract top/bottom fade strings, if required */
  1858. if (fade_active)
  1859. {
  1860. /* We waste a handful of clock cycles by using
  1861. * build_line_ticker_string() here, but it saves
  1862. * rewriting a heap of code... */
  1863. build_line_ticker_string(
  1864. 1, top_fade_line_offset, &lines,
  1865. line_ticker->top_fade_str, line_ticker->top_fade_str_len);
  1866. build_line_ticker_string(
  1867. 1, bottom_fade_line_offset, &lines,
  1868. line_ticker->bottom_fade_str, line_ticker->bottom_fade_str_len);
  1869. }
  1870. success = true;
  1871. is_active = true;
  1872. p_anim->flags |= GFX_ANIM_FLAG_TICKER_IS_ACTIVE;
  1873. end:
  1874. #endif
  1875. if (wrapped_str)
  1876. {
  1877. free(wrapped_str);
  1878. wrapped_str = NULL;
  1879. }
  1880. string_list_deinitialize(&lines);
  1881. if (!success)
  1882. {
  1883. if (line_ticker->dst_str_len > 0)
  1884. line_ticker->dst_str[0] = '\0';
  1885. if (line_ticker->fade_enabled)
  1886. {
  1887. if (line_ticker->top_fade_str_len > 0)
  1888. line_ticker->top_fade_str[0] = '\0';
  1889. if (line_ticker->bottom_fade_str_len > 0)
  1890. line_ticker->bottom_fade_str[0] = '\0';
  1891. *line_ticker->top_fade_alpha = 0.0f;
  1892. *line_ticker->bottom_fade_alpha = 0.0f;
  1893. }
  1894. }
  1895. return is_active;
  1896. }
  1897. bool gfx_animation_kill_by_tag(uintptr_t *tag)
  1898. {
  1899. unsigned i;
  1900. gfx_animation_t *p_anim = &anim_st;
  1901. if (!tag || *tag == (uintptr_t)-1)
  1902. return false;
  1903. /* Scan animation list */
  1904. for (i = 0; i < RBUF_LEN(p_anim->list); ++i)
  1905. {
  1906. struct tween *t = &p_anim->list[i];
  1907. if (t->tag != *tag)
  1908. continue;
  1909. /* If we are currently inside gfx_animation_update(),
  1910. * we are already looping over p_anim->list entries
  1911. * > Cannot modify p_anim->list now, so schedule a
  1912. * delete for when the gfx_animation_update() loop
  1913. * is complete */
  1914. if (p_anim->flags & GFX_ANIM_FLAG_IN_UPDATE)
  1915. {
  1916. t->deleted = true;
  1917. p_anim->flags |= GFX_ANIM_FLAG_PENDING_DELETES;
  1918. }
  1919. else
  1920. {
  1921. RBUF_REMOVE(p_anim->list, i);
  1922. --i;
  1923. }
  1924. }
  1925. /* If we are currently inside gfx_animation_update(),
  1926. * also have to scan *pending* animation list
  1927. * (otherwise any entries that are simultaneously added
  1928. * and deleted inside gfx_animation_update() won't get
  1929. * deleted at all, producing utter chaos) */
  1930. if (p_anim->flags & GFX_ANIM_FLAG_IN_UPDATE)
  1931. {
  1932. for (i = 0; i < RBUF_LEN(p_anim->pending); ++i)
  1933. {
  1934. struct tween *t = &p_anim->pending[i];
  1935. if (t->tag != *tag)
  1936. continue;
  1937. RBUF_REMOVE(p_anim->pending, i);
  1938. --i;
  1939. }
  1940. }
  1941. return true;
  1942. }
  1943. void gfx_animation_deinit(void)
  1944. {
  1945. gfx_animation_t *p_anim = &anim_st;
  1946. if (!p_anim)
  1947. return;
  1948. RBUF_FREE(p_anim->list);
  1949. RBUF_FREE(p_anim->pending);
  1950. if (p_anim->updatetime_cb)
  1951. p_anim->updatetime_cb = NULL;
  1952. memset(p_anim, 0, sizeof(*p_anim));
  1953. }
  1954. void gfx_animation_timer_start(float *timer, gfx_timer_ctx_entry_t *timer_entry)
  1955. {
  1956. gfx_animation_ctx_entry_t entry;
  1957. uintptr_t tag = (uintptr_t) timer;
  1958. gfx_animation_kill_by_tag(&tag);
  1959. *timer = 0.0f;
  1960. entry.easing_enum = EASING_LINEAR;
  1961. entry.tag = tag;
  1962. entry.duration = timer_entry->duration;
  1963. entry.target_value = 1.0f;
  1964. entry.subject = timer;
  1965. entry.cb = timer_entry->cb;
  1966. entry.userdata = timer_entry->userdata;
  1967. gfx_animation_push(&entry);
  1968. }