SDL_stdinc.h 144 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /**
  19. * # CategoryStdinc
  20. *
  21. * This is a general header that includes C language support. It implements a
  22. * subset of the C runtime APIs, but with an `SDL_` prefix. For most common
  23. * use cases, these should behave the same way as their C runtime equivalents,
  24. * but they may differ in how or whether they handle certain edge cases. When
  25. * in doubt, consult the documentation for details.
  26. */
  27. #ifndef SDL_stdinc_h_
  28. #define SDL_stdinc_h_
  29. #include <SDL3/SDL_platform_defines.h>
  30. #include <stdarg.h>
  31. #include <stdint.h>
  32. #include <string.h>
  33. #include <wchar.h>
  34. #if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || \
  35. defined(SDL_INCLUDE_INTTYPES_H)
  36. #include <inttypes.h>
  37. #endif
  38. #ifndef __cplusplus
  39. #if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || \
  40. (defined(_MSC_VER) && (_MSC_VER >= 1910 /* Visual Studio 2017 */)) || \
  41. defined(SDL_INCLUDE_STDBOOL_H)
  42. #include <stdbool.h>
  43. #elif !defined(__bool_true_false_are_defined) && !defined(bool)
  44. #define bool unsigned char
  45. #define false 0
  46. #define true 1
  47. #define __bool_true_false_are_defined 1
  48. #endif
  49. #endif /* !__cplusplus */
  50. #ifndef SDL_DISABLE_ALLOCA
  51. # ifndef alloca
  52. # ifdef HAVE_ALLOCA_H
  53. # include <alloca.h>
  54. # elif defined(SDL_PLATFORM_NETBSD)
  55. # if defined(__STRICT_ANSI__)
  56. # define SDL_DISABLE_ALLOCA
  57. # else
  58. # include <stdlib.h>
  59. # endif
  60. # elif defined(__GNUC__)
  61. # define alloca __builtin_alloca
  62. # elif defined(_MSC_VER)
  63. # include <malloc.h>
  64. # define alloca _alloca
  65. # elif defined(__WATCOMC__)
  66. # include <malloc.h>
  67. # elif defined(__BORLANDC__)
  68. # include <malloc.h>
  69. # elif defined(__DMC__)
  70. # include <stdlib.h>
  71. # elif defined(SDL_PLATFORM_AIX)
  72. # pragma alloca
  73. # elif defined(__MRC__)
  74. void *alloca(unsigned);
  75. # else
  76. void *alloca(size_t);
  77. # endif
  78. # endif
  79. #endif
  80. #ifdef SIZE_MAX
  81. # define SDL_SIZE_MAX SIZE_MAX
  82. #else
  83. # define SDL_SIZE_MAX ((size_t) -1)
  84. #endif
  85. #ifndef SDL_COMPILE_TIME_ASSERT
  86. #if defined(__cplusplus)
  87. /* Keep C++ case alone: Some versions of gcc will define __STDC_VERSION__ even when compiling in C++ mode. */
  88. #if (__cplusplus >= 201103L)
  89. #define SDL_COMPILE_TIME_ASSERT(name, x) static_assert(x, #x)
  90. #endif
  91. #elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 202311L)
  92. #define SDL_COMPILE_TIME_ASSERT(name, x) static_assert(x, #x)
  93. #elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
  94. #define SDL_COMPILE_TIME_ASSERT(name, x) _Static_assert(x, #x)
  95. #endif
  96. #endif /* !SDL_COMPILE_TIME_ASSERT */
  97. #ifndef SDL_COMPILE_TIME_ASSERT
  98. /* universal, but may trigger -Wunused-local-typedefs */
  99. #define SDL_COMPILE_TIME_ASSERT(name, x) \
  100. typedef int SDL_compile_time_assert_ ## name[(x) * 2 - 1]
  101. #endif
  102. /**
  103. * Check if the compiler supports a given builtin.
  104. * Supported by virtually all clang versions and recent gcc. Use this
  105. * instead of checking the clang version if possible.
  106. */
  107. #ifdef __has_builtin
  108. #define SDL_HAS_BUILTIN(x) __has_builtin(x)
  109. #else
  110. #define SDL_HAS_BUILTIN(x) 0
  111. #endif
  112. /**
  113. * The number of elements in a static array.
  114. *
  115. * This will compile but return incorrect results for a pointer to an array;
  116. * it has to be an array the compiler knows the size of.
  117. *
  118. * This macro looks like it double-evaluates the argument, but it does so
  119. * inside of `sizeof`, so there are no side-effects here, as expressions do
  120. * not actually run any code in these cases.
  121. *
  122. * \since This macro is available since SDL 3.1.3.
  123. */
  124. #define SDL_arraysize(array) (sizeof(array)/sizeof(array[0]))
  125. /**
  126. * Macro useful for building other macros with strings in them.
  127. *
  128. * For example:
  129. *
  130. * ```c
  131. * #define LOG_ERROR(X) OutputDebugString(SDL_STRINGIFY_ARG(__FUNCTION__) ": " X "\n")`
  132. * ```
  133. *
  134. * \since This macro is available since SDL 3.1.3.
  135. */
  136. #define SDL_STRINGIFY_ARG(arg) #arg
  137. /**
  138. * \name Cast operators
  139. *
  140. * Use proper C++ casts when compiled as C++ to be compatible with the option
  141. * -Wold-style-cast of GCC (and -Werror=old-style-cast in GCC 4.2 and above).
  142. */
  143. /* @{ */
  144. #ifdef SDL_WIKI_DOCUMENTATION_SECTION
  145. /**
  146. * Handle a Reinterpret Cast properly whether using C or C++.
  147. *
  148. * If compiled as C++, this macro offers a proper C++ reinterpret_cast<>.
  149. *
  150. * If compiled as C, this macro does a normal C-style cast.
  151. *
  152. * This is helpful to avoid compiler warnings in C++.
  153. *
  154. * \param type the type to cast the expression to.
  155. * \param expression the expression to cast to a different type.
  156. * \returns `expression`, cast to `type`.
  157. *
  158. * \threadsafety It is safe to call this macro from any thread.
  159. *
  160. * \since This macro is available since SDL 3.1.3.
  161. *
  162. * \sa SDL_static_cast
  163. * \sa SDL_const_cast
  164. */
  165. #define SDL_reinterpret_cast(type, expression) reinterpret_cast<type>(expression) /* or `((type)(expression))` in C */
  166. /**
  167. * Handle a Static Cast properly whether using C or C++.
  168. *
  169. * If compiled as C++, this macro offers a proper C++ static_cast<>.
  170. *
  171. * If compiled as C, this macro does a normal C-style cast.
  172. *
  173. * This is helpful to avoid compiler warnings in C++.
  174. *
  175. * \param type the type to cast the expression to.
  176. * \param expression the expression to cast to a different type.
  177. * \returns `expression`, cast to `type`.
  178. *
  179. * \threadsafety It is safe to call this macro from any thread.
  180. *
  181. * \since This macro is available since SDL 3.1.3.
  182. *
  183. * \sa SDL_reinterpret_cast
  184. * \sa SDL_const_cast
  185. */
  186. #define SDL_static_cast(type, expression) static_cast<type>(expression) /* or `((type)(expression))` in C */
  187. /**
  188. * Handle a Const Cast properly whether using C or C++.
  189. *
  190. * If compiled as C++, this macro offers a proper C++ const_cast<>.
  191. *
  192. * If compiled as C, this macro does a normal C-style cast.
  193. *
  194. * This is helpful to avoid compiler warnings in C++.
  195. *
  196. * \param type the type to cast the expression to.
  197. * \param expression the expression to cast to a different type.
  198. * \returns `expression`, cast to `type`.
  199. *
  200. * \threadsafety It is safe to call this macro from any thread.
  201. *
  202. * \since This macro is available since SDL 3.1.3.
  203. *
  204. * \sa SDL_reinterpret_cast
  205. * \sa SDL_static_cast
  206. */
  207. #define SDL_const_cast(type, expression) const_cast<type>(expression) /* or `((type)(expression))` in C */
  208. #elif defined(__cplusplus)
  209. #define SDL_reinterpret_cast(type, expression) reinterpret_cast<type>(expression)
  210. #define SDL_static_cast(type, expression) static_cast<type>(expression)
  211. #define SDL_const_cast(type, expression) const_cast<type>(expression)
  212. #else
  213. #define SDL_reinterpret_cast(type, expression) ((type)(expression))
  214. #define SDL_static_cast(type, expression) ((type)(expression))
  215. #define SDL_const_cast(type, expression) ((type)(expression))
  216. #endif
  217. /* @} *//* Cast operators */
  218. /**
  219. * Define a four character code as a Uint32.
  220. *
  221. * \param A the first ASCII character.
  222. * \param B the second ASCII character.
  223. * \param C the third ASCII character.
  224. * \param D the fourth ASCII character.
  225. * \returns the four characters converted into a Uint32, one character
  226. * per-byte.
  227. *
  228. * \threadsafety It is safe to call this macro from any thread.
  229. *
  230. * \since This macro is available since SDL 3.1.3.
  231. */
  232. #define SDL_FOURCC(A, B, C, D) \
  233. ((SDL_static_cast(Uint32, SDL_static_cast(Uint8, (A))) << 0) | \
  234. (SDL_static_cast(Uint32, SDL_static_cast(Uint8, (B))) << 8) | \
  235. (SDL_static_cast(Uint32, SDL_static_cast(Uint8, (C))) << 16) | \
  236. (SDL_static_cast(Uint32, SDL_static_cast(Uint8, (D))) << 24))
  237. #ifdef SDL_WIKI_DOCUMENTATION_SECTION
  238. /**
  239. * Append the 64 bit integer suffix to a signed integer literal.
  240. *
  241. * This helps compilers that might believe a integer literal larger than
  242. * 0xFFFFFFFF is overflowing a 32-bit value. Use `SDL_SINT64_C(0xFFFFFFFF1)`
  243. * instead of `0xFFFFFFFF1` by itself.
  244. *
  245. * \since This macro is available since SDL 3.1.3.
  246. *
  247. * \sa SDL_UINT64_C
  248. */
  249. #define SDL_SINT64_C(c) c ## LL /* or whatever the current compiler uses. */
  250. /**
  251. * Append the 64 bit integer suffix to an unsigned integer literal.
  252. *
  253. * This helps compilers that might believe a integer literal larger than
  254. * 0xFFFFFFFF is overflowing a 32-bit value. Use `SDL_UINT64_C(0xFFFFFFFF1)`
  255. * instead of `0xFFFFFFFF1` by itself.
  256. *
  257. * \since This macro is available since SDL 3.1.3.
  258. *
  259. * \sa SDL_SINT64_C
  260. */
  261. #define SDL_UINT64_C(c) c ## ULL /* or whatever the current compiler uses. */
  262. #else /* !SDL_WIKI_DOCUMENTATION_SECTION */
  263. #ifndef SDL_SINT64_C
  264. #if defined(INT64_C)
  265. #define SDL_SINT64_C(c) INT64_C(c)
  266. #elif defined(_MSC_VER)
  267. #define SDL_SINT64_C(c) c ## i64
  268. #elif defined(__LP64__) || defined(_LP64)
  269. #define SDL_SINT64_C(c) c ## L
  270. #else
  271. #define SDL_SINT64_C(c) c ## LL
  272. #endif
  273. #endif /* !SDL_SINT64_C */
  274. #ifndef SDL_UINT64_C
  275. #if defined(UINT64_C)
  276. #define SDL_UINT64_C(c) UINT64_C(c)
  277. #elif defined(_MSC_VER)
  278. #define SDL_UINT64_C(c) c ## ui64
  279. #elif defined(__LP64__) || defined(_LP64)
  280. #define SDL_UINT64_C(c) c ## UL
  281. #else
  282. #define SDL_UINT64_C(c) c ## ULL
  283. #endif
  284. #endif /* !SDL_UINT64_C */
  285. #endif /* !SDL_WIKI_DOCUMENTATION_SECTION */
  286. /**
  287. * \name Basic data types
  288. */
  289. /* @{ */
  290. /**
  291. * A signed 8-bit integer type.
  292. *
  293. * \since This macro is available since SDL 3.1.3.
  294. */
  295. typedef int8_t Sint8;
  296. #define SDL_MAX_SINT8 ((Sint8)0x7F) /* 127 */
  297. #define SDL_MIN_SINT8 ((Sint8)(~0x7F)) /* -128 */
  298. /**
  299. * An unsigned 8-bit integer type.
  300. *
  301. * \since This macro is available since SDL 3.1.3.
  302. */
  303. typedef uint8_t Uint8;
  304. #define SDL_MAX_UINT8 ((Uint8)0xFF) /* 255 */
  305. #define SDL_MIN_UINT8 ((Uint8)0x00) /* 0 */
  306. /**
  307. * A signed 16-bit integer type.
  308. *
  309. * \since This macro is available since SDL 3.1.3.
  310. */
  311. typedef int16_t Sint16;
  312. #define SDL_MAX_SINT16 ((Sint16)0x7FFF) /* 32767 */
  313. #define SDL_MIN_SINT16 ((Sint16)(~0x7FFF)) /* -32768 */
  314. /**
  315. * An unsigned 16-bit integer type.
  316. *
  317. * \since This macro is available since SDL 3.1.3.
  318. */
  319. typedef uint16_t Uint16;
  320. #define SDL_MAX_UINT16 ((Uint16)0xFFFF) /* 65535 */
  321. #define SDL_MIN_UINT16 ((Uint16)0x0000) /* 0 */
  322. /**
  323. * A signed 32-bit integer type.
  324. *
  325. * \since This macro is available since SDL 3.1.3.
  326. */
  327. typedef int32_t Sint32;
  328. #define SDL_MAX_SINT32 ((Sint32)0x7FFFFFFF) /* 2147483647 */
  329. #define SDL_MIN_SINT32 ((Sint32)(~0x7FFFFFFF)) /* -2147483648 */
  330. /**
  331. * An unsigned 32-bit integer type.
  332. *
  333. * \since This macro is available since SDL 3.1.3.
  334. */
  335. typedef uint32_t Uint32;
  336. #define SDL_MAX_UINT32 ((Uint32)0xFFFFFFFFu) /* 4294967295 */
  337. #define SDL_MIN_UINT32 ((Uint32)0x00000000) /* 0 */
  338. /**
  339. * A signed 64-bit integer type.
  340. *
  341. * \since This macro is available since SDL 3.1.3.
  342. *
  343. * \sa SDL_SINT64_C
  344. */
  345. typedef int64_t Sint64;
  346. #define SDL_MAX_SINT64 SDL_SINT64_C(0x7FFFFFFFFFFFFFFF) /* 9223372036854775807 */
  347. #define SDL_MIN_SINT64 ~SDL_SINT64_C(0x7FFFFFFFFFFFFFFF) /* -9223372036854775808 */
  348. /**
  349. * An unsigned 64-bit integer type.
  350. *
  351. * \since This macro is available since SDL 3.1.3.
  352. *
  353. * \sa SDL_UINT64_C
  354. */
  355. typedef uint64_t Uint64;
  356. #define SDL_MAX_UINT64 SDL_UINT64_C(0xFFFFFFFFFFFFFFFF) /* 18446744073709551615 */
  357. #define SDL_MIN_UINT64 SDL_UINT64_C(0x0000000000000000) /* 0 */
  358. /**
  359. * SDL times are signed, 64-bit integers representing nanoseconds since the
  360. * Unix epoch (Jan 1, 1970).
  361. *
  362. * They can be converted between POSIX time_t values with SDL_NS_TO_SECONDS()
  363. * and SDL_SECONDS_TO_NS(), and between Windows FILETIME values with
  364. * SDL_TimeToWindows() and SDL_TimeFromWindows().
  365. *
  366. * \since This macro is available since SDL 3.1.3.
  367. *
  368. * \sa SDL_MAX_SINT64
  369. * \sa SDL_MIN_SINT64
  370. */
  371. typedef Sint64 SDL_Time;
  372. #define SDL_MAX_TIME SDL_MAX_SINT64
  373. #define SDL_MIN_TIME SDL_MIN_SINT64
  374. /* @} *//* Basic data types */
  375. /**
  376. * \name Floating-point constants
  377. */
  378. /* @{ */
  379. #ifdef FLT_EPSILON
  380. #define SDL_FLT_EPSILON FLT_EPSILON
  381. #else
  382. /**
  383. * Epsilon constant, used for comparing floating-point numbers.
  384. *
  385. * Equals by default to platform-defined `FLT_EPSILON`, or
  386. * `1.1920928955078125e-07F` if that's not available.
  387. *
  388. * \since This macro is available since SDL 3.1.3.
  389. */
  390. #define SDL_FLT_EPSILON 1.1920928955078125e-07F /* 0x0.000002p0 */
  391. #endif
  392. /* @} *//* Floating-point constants */
  393. /* Make sure we have macros for printing width-based integers.
  394. * <inttypes.h> should define these but this is not true all platforms.
  395. * (for example win32) */
  396. #ifndef SDL_PRIs64
  397. #if defined(SDL_PLATFORM_WINDOWS)
  398. #define SDL_PRIs64 "I64d"
  399. #elif defined(PRId64)
  400. #define SDL_PRIs64 PRId64
  401. #elif defined(__LP64__) && !defined(SDL_PLATFORM_APPLE) && !defined(__EMSCRIPTEN__)
  402. #define SDL_PRIs64 "ld"
  403. #else
  404. #define SDL_PRIs64 "lld"
  405. #endif
  406. #endif
  407. #ifndef SDL_PRIu64
  408. #if defined(SDL_PLATFORM_WINDOWS)
  409. #define SDL_PRIu64 "I64u"
  410. #elif defined(PRIu64)
  411. #define SDL_PRIu64 PRIu64
  412. #elif defined(__LP64__) && !defined(SDL_PLATFORM_APPLE) && !defined(__EMSCRIPTEN__)
  413. #define SDL_PRIu64 "lu"
  414. #else
  415. #define SDL_PRIu64 "llu"
  416. #endif
  417. #endif
  418. #ifndef SDL_PRIx64
  419. #if defined(SDL_PLATFORM_WINDOWS)
  420. #define SDL_PRIx64 "I64x"
  421. #elif defined(PRIx64)
  422. #define SDL_PRIx64 PRIx64
  423. #elif defined(__LP64__) && !defined(SDL_PLATFORM_APPLE)
  424. #define SDL_PRIx64 "lx"
  425. #else
  426. #define SDL_PRIx64 "llx"
  427. #endif
  428. #endif
  429. #ifndef SDL_PRIX64
  430. #if defined(SDL_PLATFORM_WINDOWS)
  431. #define SDL_PRIX64 "I64X"
  432. #elif defined(PRIX64)
  433. #define SDL_PRIX64 PRIX64
  434. #elif defined(__LP64__) && !defined(SDL_PLATFORM_APPLE)
  435. #define SDL_PRIX64 "lX"
  436. #else
  437. #define SDL_PRIX64 "llX"
  438. #endif
  439. #endif
  440. #ifndef SDL_PRIs32
  441. #ifdef PRId32
  442. #define SDL_PRIs32 PRId32
  443. #else
  444. #define SDL_PRIs32 "d"
  445. #endif
  446. #endif
  447. #ifndef SDL_PRIu32
  448. #ifdef PRIu32
  449. #define SDL_PRIu32 PRIu32
  450. #else
  451. #define SDL_PRIu32 "u"
  452. #endif
  453. #endif
  454. #ifndef SDL_PRIx32
  455. #ifdef PRIx32
  456. #define SDL_PRIx32 PRIx32
  457. #else
  458. #define SDL_PRIx32 "x"
  459. #endif
  460. #endif
  461. #ifndef SDL_PRIX32
  462. #ifdef PRIX32
  463. #define SDL_PRIX32 PRIX32
  464. #else
  465. #define SDL_PRIX32 "X"
  466. #endif
  467. #endif
  468. /* Specifically for the `long long` -- SDL-specific. */
  469. #ifdef SDL_PLATFORM_WINDOWS
  470. SDL_COMPILE_TIME_ASSERT(longlong_size64, sizeof(long long) == 8); /* using I64 for windows - make sure `long long` is 64 bits. */
  471. #define SDL_PRILL_PREFIX "I64"
  472. #else
  473. #define SDL_PRILL_PREFIX "ll"
  474. #endif
  475. #ifndef SDL_PRILLd
  476. #define SDL_PRILLd SDL_PRILL_PREFIX "d"
  477. #endif
  478. #ifndef SDL_PRILLu
  479. #define SDL_PRILLu SDL_PRILL_PREFIX "u"
  480. #endif
  481. #ifndef SDL_PRILLx
  482. #define SDL_PRILLx SDL_PRILL_PREFIX "x"
  483. #endif
  484. #ifndef SDL_PRILLX
  485. #define SDL_PRILLX SDL_PRILL_PREFIX "X"
  486. #endif
  487. /* Annotations to help code analysis tools */
  488. #ifdef SDL_DISABLE_ANALYZE_MACROS
  489. #define SDL_IN_BYTECAP(x)
  490. #define SDL_INOUT_Z_CAP(x)
  491. #define SDL_OUT_Z_CAP(x)
  492. #define SDL_OUT_CAP(x)
  493. #define SDL_OUT_BYTECAP(x)
  494. #define SDL_OUT_Z_BYTECAP(x)
  495. #define SDL_PRINTF_FORMAT_STRING
  496. #define SDL_SCANF_FORMAT_STRING
  497. #define SDL_PRINTF_VARARG_FUNC( fmtargnumber )
  498. #define SDL_PRINTF_VARARG_FUNCV( fmtargnumber )
  499. #define SDL_SCANF_VARARG_FUNC( fmtargnumber )
  500. #define SDL_SCANF_VARARG_FUNCV( fmtargnumber )
  501. #define SDL_WPRINTF_VARARG_FUNC( fmtargnumber )
  502. #define SDL_WPRINTF_VARARG_FUNCV( fmtargnumber )
  503. #else
  504. #if defined(_MSC_VER) && (_MSC_VER >= 1600) /* VS 2010 and above */
  505. #include <sal.h>
  506. #define SDL_IN_BYTECAP(x) _In_bytecount_(x)
  507. #define SDL_INOUT_Z_CAP(x) _Inout_z_cap_(x)
  508. #define SDL_OUT_Z_CAP(x) _Out_z_cap_(x)
  509. #define SDL_OUT_CAP(x) _Out_cap_(x)
  510. #define SDL_OUT_BYTECAP(x) _Out_bytecap_(x)
  511. #define SDL_OUT_Z_BYTECAP(x) _Out_z_bytecap_(x)
  512. #define SDL_PRINTF_FORMAT_STRING _Printf_format_string_
  513. #define SDL_SCANF_FORMAT_STRING _Scanf_format_string_impl_
  514. #else
  515. #define SDL_IN_BYTECAP(x)
  516. #define SDL_INOUT_Z_CAP(x)
  517. #define SDL_OUT_Z_CAP(x)
  518. #define SDL_OUT_CAP(x)
  519. #define SDL_OUT_BYTECAP(x)
  520. #define SDL_OUT_Z_BYTECAP(x)
  521. #define SDL_PRINTF_FORMAT_STRING
  522. #define SDL_SCANF_FORMAT_STRING
  523. #endif
  524. #if defined(__GNUC__) || defined(__clang__)
  525. #define SDL_PRINTF_VARARG_FUNC( fmtargnumber ) __attribute__ (( format( __printf__, fmtargnumber, fmtargnumber+1 )))
  526. #define SDL_PRINTF_VARARG_FUNCV( fmtargnumber ) __attribute__(( format( __printf__, fmtargnumber, 0 )))
  527. #define SDL_SCANF_VARARG_FUNC( fmtargnumber ) __attribute__ (( format( __scanf__, fmtargnumber, fmtargnumber+1 )))
  528. #define SDL_SCANF_VARARG_FUNCV( fmtargnumber ) __attribute__(( format( __scanf__, fmtargnumber, 0 )))
  529. #define SDL_WPRINTF_VARARG_FUNC( fmtargnumber ) /* __attribute__ (( format( __wprintf__, fmtargnumber, fmtargnumber+1 ))) */
  530. #define SDL_WPRINTF_VARARG_FUNCV( fmtargnumber ) /* __attribute__ (( format( __wprintf__, fmtargnumber, 0 ))) */
  531. #else
  532. #define SDL_PRINTF_VARARG_FUNC( fmtargnumber )
  533. #define SDL_PRINTF_VARARG_FUNCV( fmtargnumber )
  534. #define SDL_SCANF_VARARG_FUNC( fmtargnumber )
  535. #define SDL_SCANF_VARARG_FUNCV( fmtargnumber )
  536. #define SDL_WPRINTF_VARARG_FUNC( fmtargnumber )
  537. #define SDL_WPRINTF_VARARG_FUNCV( fmtargnumber )
  538. #endif
  539. #endif /* SDL_DISABLE_ANALYZE_MACROS */
  540. /** \cond */
  541. #ifndef DOXYGEN_SHOULD_IGNORE_THIS
  542. SDL_COMPILE_TIME_ASSERT(bool_size, sizeof(bool) == 1);
  543. SDL_COMPILE_TIME_ASSERT(uint8_size, sizeof(Uint8) == 1);
  544. SDL_COMPILE_TIME_ASSERT(sint8_size, sizeof(Sint8) == 1);
  545. SDL_COMPILE_TIME_ASSERT(uint16_size, sizeof(Uint16) == 2);
  546. SDL_COMPILE_TIME_ASSERT(sint16_size, sizeof(Sint16) == 2);
  547. SDL_COMPILE_TIME_ASSERT(uint32_size, sizeof(Uint32) == 4);
  548. SDL_COMPILE_TIME_ASSERT(sint32_size, sizeof(Sint32) == 4);
  549. SDL_COMPILE_TIME_ASSERT(uint64_size, sizeof(Uint64) == 8);
  550. SDL_COMPILE_TIME_ASSERT(sint64_size, sizeof(Sint64) == 8);
  551. SDL_COMPILE_TIME_ASSERT(uint64_longlong, sizeof(Uint64) <= sizeof(unsigned long long));
  552. SDL_COMPILE_TIME_ASSERT(size_t_longlong, sizeof(size_t) <= sizeof(unsigned long long));
  553. typedef struct SDL_alignment_test
  554. {
  555. Uint8 a;
  556. void *b;
  557. } SDL_alignment_test;
  558. SDL_COMPILE_TIME_ASSERT(struct_alignment, sizeof(SDL_alignment_test) == (2 * sizeof(void *)));
  559. SDL_COMPILE_TIME_ASSERT(two_s_complement, (int)~(int)0 == (int)(-1));
  560. #endif /* DOXYGEN_SHOULD_IGNORE_THIS */
  561. /** \endcond */
  562. /* Check to make sure enums are the size of ints, for structure packing.
  563. For both Watcom C/C++ and Borland C/C++ the compiler option that makes
  564. enums having the size of an int must be enabled.
  565. This is "-b" for Borland C/C++ and "-ei" for Watcom C/C++ (v11).
  566. */
  567. /** \cond */
  568. #ifndef DOXYGEN_SHOULD_IGNORE_THIS
  569. #if !defined(SDL_PLATFORM_VITA) && !defined(SDL_PLATFORM_3DS)
  570. /* TODO: include/SDL_stdinc.h:390: error: size of array 'SDL_dummy_enum' is negative */
  571. typedef enum SDL_DUMMY_ENUM
  572. {
  573. DUMMY_ENUM_VALUE
  574. } SDL_DUMMY_ENUM;
  575. SDL_COMPILE_TIME_ASSERT(enum, sizeof(SDL_DUMMY_ENUM) == sizeof(int));
  576. #endif
  577. #endif /* DOXYGEN_SHOULD_IGNORE_THIS */
  578. /** \endcond */
  579. #include <SDL3/SDL_begin_code.h>
  580. /* Set up for C function definitions, even when using C++ */
  581. #ifdef __cplusplus
  582. extern "C" {
  583. #endif
  584. /**
  585. * A macro to initialize an SDL interface.
  586. *
  587. * This macro will initialize an SDL interface structure and should be called
  588. * before you fill out the fields with your implementation.
  589. *
  590. * You can use it like this:
  591. *
  592. * ```c
  593. * SDL_IOStreamInterface iface;
  594. *
  595. * SDL_INIT_INTERFACE(&iface);
  596. *
  597. * // Fill in the interface function pointers with your implementation
  598. * iface.seek = ...
  599. *
  600. * stream = SDL_OpenIO(&iface, NULL);
  601. * ```
  602. *
  603. * If you are using designated initializers, you can use the size of the
  604. * interface as the version, e.g.
  605. *
  606. * ```c
  607. * SDL_IOStreamInterface iface = {
  608. * .version = sizeof(iface),
  609. * .seek = ...
  610. * };
  611. * stream = SDL_OpenIO(&iface, NULL);
  612. * ```
  613. *
  614. * \threadsafety It is safe to call this macro from any thread.
  615. *
  616. * \since This macro is available since SDL 3.1.3.
  617. *
  618. * \sa SDL_IOStreamInterface
  619. * \sa SDL_StorageInterface
  620. * \sa SDL_VirtualJoystickDesc
  621. */
  622. #define SDL_INIT_INTERFACE(iface) \
  623. do { \
  624. SDL_zerop(iface); \
  625. (iface)->version = sizeof(*(iface)); \
  626. } while (0)
  627. #ifndef SDL_DISABLE_ALLOCA
  628. #define SDL_stack_alloc(type, count) (type*)alloca(sizeof(type)*(count))
  629. #define SDL_stack_free(data)
  630. #else
  631. #define SDL_stack_alloc(type, count) (type*)SDL_malloc(sizeof(type)*(count))
  632. #define SDL_stack_free(data) SDL_free(data)
  633. #endif
  634. /**
  635. * Allocate uninitialized memory.
  636. *
  637. * The allocated memory returned by this function must be freed with
  638. * SDL_free().
  639. *
  640. * If `size` is 0, it will be set to 1.
  641. *
  642. * If you want to allocate memory aligned to a specific alignment, consider
  643. * using SDL_aligned_alloc().
  644. *
  645. * \param size the size to allocate.
  646. * \returns a pointer to the allocated memory, or NULL if allocation failed.
  647. *
  648. * \threadsafety It is safe to call this function from any thread.
  649. *
  650. * \since This function is available since SDL 3.1.3.
  651. *
  652. * \sa SDL_free
  653. * \sa SDL_calloc
  654. * \sa SDL_realloc
  655. * \sa SDL_aligned_alloc
  656. */
  657. extern SDL_DECLSPEC SDL_MALLOC void * SDLCALL SDL_malloc(size_t size);
  658. /**
  659. * Allocate a zero-initialized array.
  660. *
  661. * The memory returned by this function must be freed with SDL_free().
  662. *
  663. * If either of `nmemb` or `size` is 0, they will both be set to 1.
  664. *
  665. * \param nmemb the number of elements in the array.
  666. * \param size the size of each element of the array.
  667. * \returns a pointer to the allocated array, or NULL if allocation failed.
  668. *
  669. * \threadsafety It is safe to call this function from any thread.
  670. *
  671. * \since This function is available since SDL 3.1.3.
  672. *
  673. * \sa SDL_free
  674. * \sa SDL_malloc
  675. * \sa SDL_realloc
  676. */
  677. extern SDL_DECLSPEC SDL_MALLOC SDL_ALLOC_SIZE2(1, 2) void * SDLCALL SDL_calloc(size_t nmemb, size_t size);
  678. /**
  679. * Change the size of allocated memory.
  680. *
  681. * The memory returned by this function must be freed with SDL_free().
  682. *
  683. * If `size` is 0, it will be set to 1. Note that this is unlike some other C
  684. * runtime `realloc` implementations, which may treat `realloc(mem, 0)` the
  685. * same way as `free(mem)`.
  686. *
  687. * If `mem` is NULL, the behavior of this function is equivalent to
  688. * SDL_malloc(). Otherwise, the function can have one of three possible
  689. * outcomes:
  690. *
  691. * - If it returns the same pointer as `mem`, it means that `mem` was resized
  692. * in place without freeing.
  693. * - If it returns a different non-NULL pointer, it means that `mem` was freed
  694. * and cannot be dereferenced anymore.
  695. * - If it returns NULL (indicating failure), then `mem` will remain valid and
  696. * must still be freed with SDL_free().
  697. *
  698. * \param mem a pointer to allocated memory to reallocate, or NULL.
  699. * \param size the new size of the memory.
  700. * \returns a pointer to the newly allocated memory, or NULL if allocation
  701. * failed.
  702. *
  703. * \threadsafety It is safe to call this function from any thread.
  704. *
  705. * \since This function is available since SDL 3.1.3.
  706. *
  707. * \sa SDL_free
  708. * \sa SDL_malloc
  709. * \sa SDL_calloc
  710. */
  711. extern SDL_DECLSPEC SDL_ALLOC_SIZE(2) void * SDLCALL SDL_realloc(void *mem, size_t size);
  712. /**
  713. * Free allocated memory.
  714. *
  715. * The pointer is no longer valid after this call and cannot be dereferenced
  716. * anymore.
  717. *
  718. * If `mem` is NULL, this function does nothing.
  719. *
  720. * \param mem a pointer to allocated memory, or NULL.
  721. *
  722. * \threadsafety It is safe to call this function from any thread.
  723. *
  724. * \since This function is available since SDL 3.1.3.
  725. *
  726. * \sa SDL_malloc
  727. * \sa SDL_calloc
  728. * \sa SDL_realloc
  729. */
  730. extern SDL_DECLSPEC void SDLCALL SDL_free(void *mem);
  731. /**
  732. * A callback used to implement SDL_malloc().
  733. *
  734. * SDL will always ensure that the passed `size` is greater than 0.
  735. *
  736. * \param size the size to allocate.
  737. * \returns a pointer to the allocated memory, or NULL if allocation failed.
  738. *
  739. * \threadsafety It should be safe to call this callback from any thread.
  740. *
  741. * \since This datatype is available since SDL 3.1.3.
  742. *
  743. * \sa SDL_malloc
  744. * \sa SDL_GetOriginalMemoryFunctions
  745. * \sa SDL_GetMemoryFunctions
  746. * \sa SDL_SetMemoryFunctions
  747. */
  748. typedef void *(SDLCALL *SDL_malloc_func)(size_t size);
  749. /**
  750. * A callback used to implement SDL_calloc().
  751. *
  752. * SDL will always ensure that the passed `nmemb` and `size` are both greater
  753. * than 0.
  754. *
  755. * \param nmemb the number of elements in the array.
  756. * \param size the size of each element of the array.
  757. * \returns a pointer to the allocated array, or NULL if allocation failed.
  758. *
  759. * \threadsafety It should be safe to call this callback from any thread.
  760. *
  761. * \since This datatype is available since SDL 3.1.3.
  762. *
  763. * \sa SDL_calloc
  764. * \sa SDL_GetOriginalMemoryFunctions
  765. * \sa SDL_GetMemoryFunctions
  766. * \sa SDL_SetMemoryFunctions
  767. */
  768. typedef void *(SDLCALL *SDL_calloc_func)(size_t nmemb, size_t size);
  769. /**
  770. * A callback used to implement SDL_realloc().
  771. *
  772. * SDL will always ensure that the passed `size` is greater than 0.
  773. *
  774. * \param mem a pointer to allocated memory to reallocate, or NULL.
  775. * \param size the new size of the memory.
  776. * \returns a pointer to the newly allocated memory, or NULL if allocation
  777. * failed.
  778. *
  779. * \threadsafety It should be safe to call this callback from any thread.
  780. *
  781. * \since This datatype is available since SDL 3.1.3.
  782. *
  783. * \sa SDL_realloc
  784. * \sa SDL_GetOriginalMemoryFunctions
  785. * \sa SDL_GetMemoryFunctions
  786. * \sa SDL_SetMemoryFunctions
  787. */
  788. typedef void *(SDLCALL *SDL_realloc_func)(void *mem, size_t size);
  789. /**
  790. * A callback used to implement SDL_free().
  791. *
  792. * SDL will always ensure that the passed `mem` is a non-NULL pointer.
  793. *
  794. * \param mem a pointer to allocated memory.
  795. *
  796. * \threadsafety It should be safe to call this callback from any thread.
  797. *
  798. * \since This datatype is available since SDL 3.1.3.
  799. *
  800. * \sa SDL_free
  801. * \sa SDL_GetOriginalMemoryFunctions
  802. * \sa SDL_GetMemoryFunctions
  803. * \sa SDL_SetMemoryFunctions
  804. */
  805. typedef void (SDLCALL *SDL_free_func)(void *mem);
  806. /**
  807. * Get the original set of SDL memory functions.
  808. *
  809. * This is what SDL_malloc and friends will use by default, if there has been
  810. * no call to SDL_SetMemoryFunctions. This is not necessarily using the C
  811. * runtime's `malloc` functions behind the scenes! Different platforms and
  812. * build configurations might do any number of unexpected things.
  813. *
  814. * \param malloc_func filled with malloc function.
  815. * \param calloc_func filled with calloc function.
  816. * \param realloc_func filled with realloc function.
  817. * \param free_func filled with free function.
  818. *
  819. * \threadsafety It is safe to call this function from any thread.
  820. *
  821. * \since This function is available since SDL 3.1.3.
  822. */
  823. extern SDL_DECLSPEC void SDLCALL SDL_GetOriginalMemoryFunctions(SDL_malloc_func *malloc_func,
  824. SDL_calloc_func *calloc_func,
  825. SDL_realloc_func *realloc_func,
  826. SDL_free_func *free_func);
  827. /**
  828. * Get the current set of SDL memory functions.
  829. *
  830. * \param malloc_func filled with malloc function.
  831. * \param calloc_func filled with calloc function.
  832. * \param realloc_func filled with realloc function.
  833. * \param free_func filled with free function.
  834. *
  835. * \threadsafety This does not hold a lock, so do not call this in the
  836. * unlikely event of a background thread calling
  837. * SDL_SetMemoryFunctions simultaneously.
  838. *
  839. * \since This function is available since SDL 3.1.3.
  840. *
  841. * \sa SDL_SetMemoryFunctions
  842. * \sa SDL_GetOriginalMemoryFunctions
  843. */
  844. extern SDL_DECLSPEC void SDLCALL SDL_GetMemoryFunctions(SDL_malloc_func *malloc_func,
  845. SDL_calloc_func *calloc_func,
  846. SDL_realloc_func *realloc_func,
  847. SDL_free_func *free_func);
  848. /**
  849. * Replace SDL's memory allocation functions with a custom set.
  850. *
  851. * It is not safe to call this function once any allocations have been made,
  852. * as future calls to SDL_free will use the new allocator, even if they came
  853. * from an SDL_malloc made with the old one!
  854. *
  855. * If used, usually this needs to be the first call made into the SDL library,
  856. * if not the very first thing done at program startup time.
  857. *
  858. * \param malloc_func custom malloc function.
  859. * \param calloc_func custom calloc function.
  860. * \param realloc_func custom realloc function.
  861. * \param free_func custom free function.
  862. * \returns true on success or false on failure; call SDL_GetError() for more
  863. * information.
  864. *
  865. * \threadsafety It is safe to call this function from any thread, but one
  866. * should not replace the memory functions once any allocations
  867. * are made!
  868. *
  869. * \since This function is available since SDL 3.1.3.
  870. *
  871. * \sa SDL_GetMemoryFunctions
  872. * \sa SDL_GetOriginalMemoryFunctions
  873. */
  874. extern SDL_DECLSPEC bool SDLCALL SDL_SetMemoryFunctions(SDL_malloc_func malloc_func,
  875. SDL_calloc_func calloc_func,
  876. SDL_realloc_func realloc_func,
  877. SDL_free_func free_func);
  878. /**
  879. * Allocate memory aligned to a specific alignment.
  880. *
  881. * The memory returned by this function must be freed with SDL_aligned_free(),
  882. * _not_ SDL_free().
  883. *
  884. * If `alignment` is less than the size of `void *`, it will be increased to
  885. * match that.
  886. *
  887. * The returned memory address will be a multiple of the alignment value, and
  888. * the size of the memory allocated will be a multiple of the alignment value.
  889. *
  890. * \param alignment the alignment of the memory.
  891. * \param size the size to allocate.
  892. * \returns a pointer to the aligned memory, or NULL if allocation failed.
  893. *
  894. * \threadsafety It is safe to call this function from any thread.
  895. *
  896. * \since This function is available since SDL 3.1.3.
  897. *
  898. * \sa SDL_aligned_free
  899. */
  900. extern SDL_DECLSPEC SDL_MALLOC void * SDLCALL SDL_aligned_alloc(size_t alignment, size_t size);
  901. /**
  902. * Free memory allocated by SDL_aligned_alloc().
  903. *
  904. * The pointer is no longer valid after this call and cannot be dereferenced
  905. * anymore.
  906. *
  907. * If `mem` is NULL, this function does nothing.
  908. *
  909. * \param mem a pointer previously returned by SDL_aligned_alloc(), or NULL.
  910. *
  911. * \threadsafety It is safe to call this function from any thread.
  912. *
  913. * \since This function is available since SDL 3.1.3.
  914. *
  915. * \sa SDL_aligned_alloc
  916. */
  917. extern SDL_DECLSPEC void SDLCALL SDL_aligned_free(void *mem);
  918. /**
  919. * Get the number of outstanding (unfreed) allocations.
  920. *
  921. * \returns the number of allocations.
  922. *
  923. * \threadsafety It is safe to call this function from any thread.
  924. *
  925. * \since This function is available since SDL 3.1.3.
  926. */
  927. extern SDL_DECLSPEC int SDLCALL SDL_GetNumAllocations(void);
  928. /**
  929. * A thread-safe set of environment variables
  930. *
  931. * \since This struct is available since SDL 3.1.3.
  932. *
  933. * \sa SDL_GetEnvironment
  934. * \sa SDL_CreateEnvironment
  935. * \sa SDL_GetEnvironmentVariable
  936. * \sa SDL_GetEnvironmentVariables
  937. * \sa SDL_SetEnvironmentVariable
  938. * \sa SDL_UnsetEnvironmentVariable
  939. * \sa SDL_DestroyEnvironment
  940. */
  941. typedef struct SDL_Environment SDL_Environment;
  942. /**
  943. * Get the process environment.
  944. *
  945. * This is initialized at application start and is not affected by setenv()
  946. * and unsetenv() calls after that point. Use SDL_SetEnvironmentVariable() and
  947. * SDL_UnsetEnvironmentVariable() if you want to modify this environment, or
  948. * SDL_setenv_unsafe() or SDL_unsetenv_unsafe() if you want changes to persist
  949. * in the C runtime environment after SDL_Quit().
  950. *
  951. * \returns a pointer to the environment for the process or NULL on failure;
  952. * call SDL_GetError() for more information.
  953. *
  954. * \threadsafety It is safe to call this function from any thread.
  955. *
  956. * \since This function is available since SDL 3.1.3.
  957. *
  958. * \sa SDL_GetEnvironmentVariable
  959. * \sa SDL_GetEnvironmentVariables
  960. * \sa SDL_SetEnvironmentVariable
  961. * \sa SDL_UnsetEnvironmentVariable
  962. */
  963. extern SDL_DECLSPEC SDL_Environment * SDLCALL SDL_GetEnvironment(void);
  964. /**
  965. * Create a set of environment variables
  966. *
  967. * \param populated true to initialize it from the C runtime environment,
  968. * false to create an empty environment.
  969. * \returns a pointer to the new environment or NULL on failure; call
  970. * SDL_GetError() for more information.
  971. *
  972. * \threadsafety If `populated` is false, it is safe to call this function
  973. * from any thread, otherwise it is safe if no other threads are
  974. * calling setenv() or unsetenv()
  975. *
  976. * \since This function is available since SDL 3.1.3.
  977. *
  978. * \sa SDL_GetEnvironmentVariable
  979. * \sa SDL_GetEnvironmentVariables
  980. * \sa SDL_SetEnvironmentVariable
  981. * \sa SDL_UnsetEnvironmentVariable
  982. * \sa SDL_DestroyEnvironment
  983. */
  984. extern SDL_DECLSPEC SDL_Environment * SDLCALL SDL_CreateEnvironment(bool populated);
  985. /**
  986. * Get the value of a variable in the environment.
  987. *
  988. * \param env the environment to query.
  989. * \param name the name of the variable to get.
  990. * \returns a pointer to the value of the variable or NULL if it can't be
  991. * found.
  992. *
  993. * \threadsafety It is safe to call this function from any thread.
  994. *
  995. * \since This function is available since SDL 3.1.3.
  996. *
  997. * \sa SDL_GetEnvironment
  998. * \sa SDL_CreateEnvironment
  999. * \sa SDL_GetEnvironmentVariables
  1000. * \sa SDL_SetEnvironmentVariable
  1001. * \sa SDL_UnsetEnvironmentVariable
  1002. */
  1003. extern SDL_DECLSPEC const char * SDLCALL SDL_GetEnvironmentVariable(SDL_Environment *env, const char *name);
  1004. /**
  1005. * Get all variables in the environment.
  1006. *
  1007. * \param env the environment to query.
  1008. * \returns a NULL terminated array of pointers to environment variables in
  1009. * the form "variable=value" or NULL on failure; call SDL_GetError()
  1010. * for more information. This is a single allocation that should be
  1011. * freed with SDL_free() when it is no longer needed.
  1012. *
  1013. * \threadsafety It is safe to call this function from any thread.
  1014. *
  1015. * \since This function is available since SDL 3.1.3.
  1016. *
  1017. * \sa SDL_GetEnvironment
  1018. * \sa SDL_CreateEnvironment
  1019. * \sa SDL_GetEnvironmentVariables
  1020. * \sa SDL_SetEnvironmentVariable
  1021. * \sa SDL_UnsetEnvironmentVariable
  1022. */
  1023. extern SDL_DECLSPEC char ** SDLCALL SDL_GetEnvironmentVariables(SDL_Environment *env);
  1024. /**
  1025. * Set the value of a variable in the environment.
  1026. *
  1027. * \param env the environment to modify.
  1028. * \param name the name of the variable to set.
  1029. * \param value the value of the variable to set.
  1030. * \param overwrite true to overwrite the variable if it exists, false to
  1031. * return success without setting the variable if it already
  1032. * exists.
  1033. * \returns true on success or false on failure; call SDL_GetError() for more
  1034. * information.
  1035. *
  1036. * \threadsafety It is safe to call this function from any thread.
  1037. *
  1038. * \since This function is available since SDL 3.1.3.
  1039. *
  1040. * \sa SDL_GetEnvironment
  1041. * \sa SDL_CreateEnvironment
  1042. * \sa SDL_GetEnvironmentVariable
  1043. * \sa SDL_GetEnvironmentVariables
  1044. * \sa SDL_UnsetEnvironmentVariable
  1045. */
  1046. extern SDL_DECLSPEC bool SDLCALL SDL_SetEnvironmentVariable(SDL_Environment *env, const char *name, const char *value, bool overwrite);
  1047. /**
  1048. * Clear a variable from the environment.
  1049. *
  1050. * \param env the environment to modify.
  1051. * \param name the name of the variable to unset.
  1052. * \returns true on success or false on failure; call SDL_GetError() for more
  1053. * information.
  1054. *
  1055. * \threadsafety It is safe to call this function from any thread.
  1056. *
  1057. * \since This function is available since SDL 3.1.3.
  1058. *
  1059. * \sa SDL_GetEnvironment
  1060. * \sa SDL_CreateEnvironment
  1061. * \sa SDL_GetEnvironmentVariable
  1062. * \sa SDL_GetEnvironmentVariables
  1063. * \sa SDL_SetEnvironmentVariable
  1064. * \sa SDL_UnsetEnvironmentVariable
  1065. */
  1066. extern SDL_DECLSPEC bool SDLCALL SDL_UnsetEnvironmentVariable(SDL_Environment *env, const char *name);
  1067. /**
  1068. * Destroy a set of environment variables.
  1069. *
  1070. * \param env the environment to destroy.
  1071. *
  1072. * \threadsafety It is safe to call this function from any thread, as long as
  1073. * the environment is no longer in use.
  1074. *
  1075. * \since This function is available since SDL 3.1.3.
  1076. *
  1077. * \sa SDL_CreateEnvironment
  1078. */
  1079. extern SDL_DECLSPEC void SDLCALL SDL_DestroyEnvironment(SDL_Environment *env);
  1080. /**
  1081. * Get the value of a variable in the environment.
  1082. *
  1083. * This function uses SDL's cached copy of the environment and is thread-safe.
  1084. *
  1085. * \param name the name of the variable to get.
  1086. * \returns a pointer to the value of the variable or NULL if it can't be
  1087. * found.
  1088. *
  1089. * \threadsafety It is safe to call this function from any thread.
  1090. *
  1091. * \since This function is available since SDL 3.1.3.
  1092. */
  1093. extern SDL_DECLSPEC const char * SDLCALL SDL_getenv(const char *name);
  1094. /**
  1095. * Get the value of a variable in the environment.
  1096. *
  1097. * This function bypasses SDL's cached copy of the environment and is not
  1098. * thread-safe.
  1099. *
  1100. * \param name the name of the variable to get.
  1101. * \returns a pointer to the value of the variable or NULL if it can't be
  1102. * found.
  1103. *
  1104. * \threadsafety This function is not thread safe, consider using SDL_getenv()
  1105. * instead.
  1106. *
  1107. * \since This function is available since SDL 3.1.3.
  1108. *
  1109. * \sa SDL_getenv
  1110. */
  1111. extern SDL_DECLSPEC const char * SDLCALL SDL_getenv_unsafe(const char *name);
  1112. /**
  1113. * Set the value of a variable in the environment.
  1114. *
  1115. * \param name the name of the variable to set.
  1116. * \param value the value of the variable to set.
  1117. * \param overwrite 1 to overwrite the variable if it exists, 0 to return
  1118. * success without setting the variable if it already exists.
  1119. * \returns 0 on success, -1 on error.
  1120. *
  1121. * \threadsafety This function is not thread safe, consider using
  1122. * SDL_SetEnvironmentVariable() instead.
  1123. *
  1124. * \since This function is available since SDL 3.1.3.
  1125. *
  1126. * \sa SDL_SetEnvironmentVariable
  1127. */
  1128. extern SDL_DECLSPEC int SDLCALL SDL_setenv_unsafe(const char *name, const char *value, int overwrite);
  1129. /**
  1130. * Clear a variable from the environment.
  1131. *
  1132. * \param name the name of the variable to unset.
  1133. * \returns 0 on success, -1 on error.
  1134. *
  1135. * \threadsafety This function is not thread safe, consider using
  1136. * SDL_UnsetEnvironmentVariable() instead.
  1137. *
  1138. * \since This function is available since SDL 3.1.3.
  1139. *
  1140. * \sa SDL_UnsetEnvironmentVariable
  1141. */
  1142. extern SDL_DECLSPEC int SDLCALL SDL_unsetenv_unsafe(const char *name);
  1143. /**
  1144. * A callback used with SDL sorting and binary search functions.
  1145. *
  1146. * \param a a pointer to the first element being compared.
  1147. * \param b a pointer to the second element being compared.
  1148. * \returns -1 if `a` should be sorted before `b`, 1 if `b` should be sorted
  1149. * before `a`, 0 if they are equal. If two elements are equal, their
  1150. * order in the sorted array is undefined.
  1151. *
  1152. * \since This callback is available since SDL 3.1.3.
  1153. *
  1154. * \sa SDL_bsearch
  1155. * \sa SDL_qsort
  1156. */
  1157. typedef int (SDLCALL *SDL_CompareCallback)(const void *a, const void *b);
  1158. /**
  1159. * Sort an array.
  1160. *
  1161. * For example:
  1162. *
  1163. * ```c
  1164. * typedef struct {
  1165. * int key;
  1166. * const char *string;
  1167. * } data;
  1168. *
  1169. * int SDLCALL compare(const void *a, const void *b)
  1170. * {
  1171. * const data *A = (const data *)a;
  1172. * const data *B = (const data *)b;
  1173. *
  1174. * if (A->n < B->n) {
  1175. * return -1;
  1176. * } else if (B->n < A->n) {
  1177. * return 1;
  1178. * } else {
  1179. * return 0;
  1180. * }
  1181. * }
  1182. *
  1183. * data values[] = {
  1184. * { 3, "third" }, { 1, "first" }, { 2, "second" }
  1185. * };
  1186. *
  1187. * SDL_qsort(values, SDL_arraysize(values), sizeof(values[0]), compare);
  1188. * ```
  1189. *
  1190. * \param base a pointer to the start of the array.
  1191. * \param nmemb the number of elements in the array.
  1192. * \param size the size of the elements in the array.
  1193. * \param compare a function used to compare elements in the array.
  1194. *
  1195. * \threadsafety It is safe to call this function from any thread.
  1196. *
  1197. * \since This function is available since SDL 3.1.3.
  1198. *
  1199. * \sa SDL_bsearch
  1200. * \sa SDL_qsort_r
  1201. */
  1202. extern SDL_DECLSPEC void SDLCALL SDL_qsort(void *base, size_t nmemb, size_t size, SDL_CompareCallback compare);
  1203. /**
  1204. * Perform a binary search on a previously sorted array.
  1205. *
  1206. * For example:
  1207. *
  1208. * ```c
  1209. * typedef struct {
  1210. * int key;
  1211. * const char *string;
  1212. * } data;
  1213. *
  1214. * int SDLCALL compare(const void *a, const void *b)
  1215. * {
  1216. * const data *A = (const data *)a;
  1217. * const data *B = (const data *)b;
  1218. *
  1219. * if (A->n < B->n) {
  1220. * return -1;
  1221. * } else if (B->n < A->n) {
  1222. * return 1;
  1223. * } else {
  1224. * return 0;
  1225. * }
  1226. * }
  1227. *
  1228. * data values[] = {
  1229. * { 1, "first" }, { 2, "second" }, { 3, "third" }
  1230. * };
  1231. * data key = { 2, NULL };
  1232. *
  1233. * data *result = SDL_bsearch(&key, values, SDL_arraysize(values), sizeof(values[0]), compare);
  1234. * ```
  1235. *
  1236. * \param key a pointer to a key equal to the element being searched for.
  1237. * \param base a pointer to the start of the array.
  1238. * \param nmemb the number of elements in the array.
  1239. * \param size the size of the elements in the array.
  1240. * \param compare a function used to compare elements in the array.
  1241. * \returns a pointer to the matching element in the array, or NULL if not
  1242. * found.
  1243. *
  1244. * \threadsafety It is safe to call this function from any thread.
  1245. *
  1246. * \since This function is available since SDL 3.1.3.
  1247. *
  1248. * \sa SDL_bsearch_r
  1249. * \sa SDL_qsort
  1250. */
  1251. extern SDL_DECLSPEC void * SDLCALL SDL_bsearch(const void *key, const void *base, size_t nmemb, size_t size, SDL_CompareCallback compare);
  1252. /**
  1253. * A callback used with SDL sorting and binary search functions.
  1254. *
  1255. * \param userdata the `userdata` pointer passed to the sort function.
  1256. * \param a a pointer to the first element being compared.
  1257. * \param b a pointer to the second element being compared.
  1258. * \returns -1 if `a` should be sorted before `b`, 1 if `b` should be sorted
  1259. * before `a`, 0 if they are equal. If two elements are equal, their
  1260. * order in the sorted array is undefined.
  1261. *
  1262. * \since This callback is available since SDL 3.1.3.
  1263. *
  1264. * \sa SDL_qsort_r
  1265. * \sa SDL_bsearch_r
  1266. */
  1267. typedef int (SDLCALL *SDL_CompareCallback_r)(void *userdata, const void *a, const void *b);
  1268. /**
  1269. * Sort an array, passing a userdata pointer to the compare function.
  1270. *
  1271. * For example:
  1272. *
  1273. * ```c
  1274. * typedef enum {
  1275. * sort_increasing,
  1276. * sort_decreasing,
  1277. * } sort_method;
  1278. *
  1279. * typedef struct {
  1280. * int key;
  1281. * const char *string;
  1282. * } data;
  1283. *
  1284. * int SDLCALL compare(const void *userdata, const void *a, const void *b)
  1285. * {
  1286. * sort_method method = (sort_method)(uintptr_t)userdata;
  1287. * const data *A = (const data *)a;
  1288. * const data *B = (const data *)b;
  1289. *
  1290. * if (A->key < B->key) {
  1291. * return (method == sort_increasing) ? -1 : 1;
  1292. * } else if (B->key < A->key) {
  1293. * return (method == sort_increasing) ? 1 : -1;
  1294. * } else {
  1295. * return 0;
  1296. * }
  1297. * }
  1298. *
  1299. * data values[] = {
  1300. * { 3, "third" }, { 1, "first" }, { 2, "second" }
  1301. * };
  1302. *
  1303. * SDL_qsort_r(values, SDL_arraysize(values), sizeof(values[0]), compare, (const void *)(uintptr_t)sort_increasing);
  1304. * ```
  1305. *
  1306. * \param base a pointer to the start of the array.
  1307. * \param nmemb the number of elements in the array.
  1308. * \param size the size of the elements in the array.
  1309. * \param compare a function used to compare elements in the array.
  1310. * \param userdata a pointer to pass to the compare function.
  1311. *
  1312. * \threadsafety It is safe to call this function from any thread.
  1313. *
  1314. * \since This function is available since SDL 3.1.3.
  1315. *
  1316. * \sa SDL_bsearch_r
  1317. * \sa SDL_qsort
  1318. */
  1319. extern SDL_DECLSPEC void SDLCALL SDL_qsort_r(void *base, size_t nmemb, size_t size, SDL_CompareCallback_r compare, void *userdata);
  1320. /**
  1321. * Perform a binary search on a previously sorted array, passing a userdata
  1322. * pointer to the compare function.
  1323. *
  1324. * For example:
  1325. *
  1326. * ```c
  1327. * typedef enum {
  1328. * sort_increasing,
  1329. * sort_decreasing,
  1330. * } sort_method;
  1331. *
  1332. * typedef struct {
  1333. * int key;
  1334. * const char *string;
  1335. * } data;
  1336. *
  1337. * int SDLCALL compare(const void *userdata, const void *a, const void *b)
  1338. * {
  1339. * sort_method method = (sort_method)(uintptr_t)userdata;
  1340. * const data *A = (const data *)a;
  1341. * const data *B = (const data *)b;
  1342. *
  1343. * if (A->key < B->key) {
  1344. * return (method == sort_increasing) ? -1 : 1;
  1345. * } else if (B->key < A->key) {
  1346. * return (method == sort_increasing) ? 1 : -1;
  1347. * } else {
  1348. * return 0;
  1349. * }
  1350. * }
  1351. *
  1352. * data values[] = {
  1353. * { 1, "first" }, { 2, "second" }, { 3, "third" }
  1354. * };
  1355. * data key = { 2, NULL };
  1356. *
  1357. * data *result = SDL_bsearch_r(&key, values, SDL_arraysize(values), sizeof(values[0]), compare, (const void *)(uintptr_t)sort_increasing);
  1358. * ```
  1359. *
  1360. * \param key a pointer to a key equal to the element being searched for.
  1361. * \param base a pointer to the start of the array.
  1362. * \param nmemb the number of elements in the array.
  1363. * \param size the size of the elements in the array.
  1364. * \param compare a function used to compare elements in the array.
  1365. * \param userdata a pointer to pass to the compare function.
  1366. * \returns a pointer to the matching element in the array, or NULL if not
  1367. * found.
  1368. *
  1369. * \threadsafety It is safe to call this function from any thread.
  1370. *
  1371. * \since This function is available since SDL 3.1.3.
  1372. *
  1373. * \sa SDL_bsearch
  1374. * \sa SDL_qsort_r
  1375. */
  1376. extern SDL_DECLSPEC void * SDLCALL SDL_bsearch_r(const void *key, const void *base, size_t nmemb, size_t size, SDL_CompareCallback_r compare, void *userdata);
  1377. extern SDL_DECLSPEC int SDLCALL SDL_abs(int x);
  1378. /* NOTE: these double-evaluate their arguments, so you should never have side effects in the parameters */
  1379. #define SDL_min(x, y) (((x) < (y)) ? (x) : (y))
  1380. #define SDL_max(x, y) (((x) > (y)) ? (x) : (y))
  1381. #define SDL_clamp(x, a, b) (((x) < (a)) ? (a) : (((x) > (b)) ? (b) : (x)))
  1382. /**
  1383. * Query if a character is alphabetic (a letter).
  1384. *
  1385. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  1386. * for English 'a-z' and 'A-Z' as true.
  1387. *
  1388. * \param x character value to check.
  1389. * \returns non-zero if x falls within the character class, zero otherwise.
  1390. *
  1391. * \threadsafety It is safe to call this function from any thread.
  1392. *
  1393. * \since This function is available since SDL 3.1.3.
  1394. */
  1395. extern SDL_DECLSPEC int SDLCALL SDL_isalpha(int x);
  1396. /**
  1397. * Query if a character is alphabetic (a letter) or a number.
  1398. *
  1399. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  1400. * for English 'a-z', 'A-Z', and '0-9' as true.
  1401. *
  1402. * \param x character value to check.
  1403. * \returns non-zero if x falls within the character class, zero otherwise.
  1404. *
  1405. * \threadsafety It is safe to call this function from any thread.
  1406. *
  1407. * \since This function is available since SDL 3.1.3.
  1408. */
  1409. extern SDL_DECLSPEC int SDLCALL SDL_isalnum(int x);
  1410. /**
  1411. * Report if a character is blank (a space or tab).
  1412. *
  1413. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  1414. * 0x20 (space) or 0x9 (tab) as true.
  1415. *
  1416. * \param x character value to check.
  1417. * \returns non-zero if x falls within the character class, zero otherwise.
  1418. *
  1419. * \threadsafety It is safe to call this function from any thread.
  1420. *
  1421. * \since This function is available since SDL 3.1.3.
  1422. */
  1423. extern SDL_DECLSPEC int SDLCALL SDL_isblank(int x);
  1424. /**
  1425. * Report if a character is a control character.
  1426. *
  1427. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  1428. * 0 through 0x1F, and 0x7F, as true.
  1429. *
  1430. * \param x character value to check.
  1431. * \returns non-zero if x falls within the character class, zero otherwise.
  1432. *
  1433. * \threadsafety It is safe to call this function from any thread.
  1434. *
  1435. * \since This function is available since SDL 3.1.3.
  1436. */
  1437. extern SDL_DECLSPEC int SDLCALL SDL_iscntrl(int x);
  1438. /**
  1439. * Report if a character is a numeric digit.
  1440. *
  1441. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  1442. * '0' (0x30) through '9' (0x39), as true.
  1443. *
  1444. * \param x character value to check.
  1445. * \returns non-zero if x falls within the character class, zero otherwise.
  1446. *
  1447. * \threadsafety It is safe to call this function from any thread.
  1448. *
  1449. * \since This function is available since SDL 3.1.3.
  1450. */
  1451. extern SDL_DECLSPEC int SDLCALL SDL_isdigit(int x);
  1452. /**
  1453. * Report if a character is a hexadecimal digit.
  1454. *
  1455. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  1456. * 'A' through 'F', 'a' through 'f', and '0' through '9', as true.
  1457. *
  1458. * \param x character value to check.
  1459. * \returns non-zero if x falls within the character class, zero otherwise.
  1460. *
  1461. * \threadsafety It is safe to call this function from any thread.
  1462. *
  1463. * \since This function is available since SDL 3.1.3.
  1464. */
  1465. extern SDL_DECLSPEC int SDLCALL SDL_isxdigit(int x);
  1466. /**
  1467. * Report if a character is a punctuation mark.
  1468. *
  1469. * **WARNING**: Regardless of system locale, this is equivalent to
  1470. * `((SDL_isgraph(x)) && (!SDL_isalnum(x)))`.
  1471. *
  1472. * \param x character value to check.
  1473. * \returns non-zero if x falls within the character class, zero otherwise.
  1474. *
  1475. * \threadsafety It is safe to call this function from any thread.
  1476. *
  1477. * \since This function is available since SDL 3.1.3.
  1478. *
  1479. * \sa SDL_isgraph
  1480. * \sa SDL_isalnum
  1481. */
  1482. extern SDL_DECLSPEC int SDLCALL SDL_ispunct(int x);
  1483. /**
  1484. * Report if a character is whitespace.
  1485. *
  1486. * **WARNING**: Regardless of system locale, this will only treat the
  1487. * following ASCII values as true:
  1488. *
  1489. * - space (0x20)
  1490. * - tab (0x09)
  1491. * - newline (0x0A)
  1492. * - vertical tab (0x0B)
  1493. * - form feed (0x0C)
  1494. * - return (0x0D)
  1495. *
  1496. * \param x character value to check.
  1497. * \returns non-zero if x falls within the character class, zero otherwise.
  1498. *
  1499. * \threadsafety It is safe to call this function from any thread.
  1500. *
  1501. * \since This function is available since SDL 3.1.3.
  1502. */
  1503. extern SDL_DECLSPEC int SDLCALL SDL_isspace(int x);
  1504. /**
  1505. * Report if a character is upper case.
  1506. *
  1507. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  1508. * 'A' through 'Z' as true.
  1509. *
  1510. * \param x character value to check.
  1511. * \returns non-zero if x falls within the character class, zero otherwise.
  1512. *
  1513. * \threadsafety It is safe to call this function from any thread.
  1514. *
  1515. * \since This function is available since SDL 3.1.3.
  1516. */
  1517. extern SDL_DECLSPEC int SDLCALL SDL_isupper(int x);
  1518. /**
  1519. * Report if a character is lower case.
  1520. *
  1521. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  1522. * 'a' through 'z' as true.
  1523. *
  1524. * \param x character value to check.
  1525. * \returns non-zero if x falls within the character class, zero otherwise.
  1526. *
  1527. * \threadsafety It is safe to call this function from any thread.
  1528. *
  1529. * \since This function is available since SDL 3.1.3.
  1530. */
  1531. extern SDL_DECLSPEC int SDLCALL SDL_islower(int x);
  1532. /**
  1533. * Report if a character is "printable".
  1534. *
  1535. * Be advised that "printable" has a definition that goes back to text
  1536. * terminals from the dawn of computing, making this a sort of special case
  1537. * function that is not suitable for Unicode (or most any) text management.
  1538. *
  1539. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  1540. * ' ' (0x20) through '~' (0x7E) as true.
  1541. *
  1542. * \param x character value to check.
  1543. * \returns non-zero if x falls within the character class, zero otherwise.
  1544. *
  1545. * \threadsafety It is safe to call this function from any thread.
  1546. *
  1547. * \since This function is available since SDL 3.1.3.
  1548. */
  1549. extern SDL_DECLSPEC int SDLCALL SDL_isprint(int x);
  1550. /**
  1551. * Report if a character is any "printable" except space.
  1552. *
  1553. * Be advised that "printable" has a definition that goes back to text
  1554. * terminals from the dawn of computing, making this a sort of special case
  1555. * function that is not suitable for Unicode (or most any) text management.
  1556. *
  1557. * **WARNING**: Regardless of system locale, this is equivalent to
  1558. * `(SDL_isprint(x)) && ((x) != ' ')`.
  1559. *
  1560. * \param x character value to check.
  1561. * \returns non-zero if x falls within the character class, zero otherwise.
  1562. *
  1563. * \threadsafety It is safe to call this function from any thread.
  1564. *
  1565. * \since This function is available since SDL 3.1.3.
  1566. *
  1567. * \sa SDL_isprint
  1568. */
  1569. extern SDL_DECLSPEC int SDLCALL SDL_isgraph(int x);
  1570. /**
  1571. * Convert low-ASCII English letters to uppercase.
  1572. *
  1573. * **WARNING**: Regardless of system locale, this will only convert ASCII
  1574. * values 'a' through 'z' to uppercase.
  1575. *
  1576. * This function returns the uppercase equivalent of `x`. If a character
  1577. * cannot be converted, or is already uppercase, this function returns `x`.
  1578. *
  1579. * \param x character value to check.
  1580. * \returns capitalized version of x, or x if no conversion available.
  1581. *
  1582. * \threadsafety It is safe to call this function from any thread.
  1583. *
  1584. * \since This function is available since SDL 3.1.3.
  1585. */
  1586. extern SDL_DECLSPEC int SDLCALL SDL_toupper(int x);
  1587. /**
  1588. * Convert low-ASCII English letters to lowercase.
  1589. *
  1590. * **WARNING**: Regardless of system locale, this will only convert ASCII
  1591. * values 'A' through 'Z' to lowercase.
  1592. *
  1593. * This function returns the lowercase equivalent of `x`. If a character
  1594. * cannot be converted, or is already lowercase, this function returns `x`.
  1595. *
  1596. * \param x character value to check.
  1597. * \returns lowercase version of x, or x if no conversion available.
  1598. *
  1599. * \threadsafety It is safe to call this function from any thread.
  1600. *
  1601. * \since This function is available since SDL 3.1.3.
  1602. */
  1603. extern SDL_DECLSPEC int SDLCALL SDL_tolower(int x);
  1604. extern SDL_DECLSPEC Uint16 SDLCALL SDL_crc16(Uint16 crc, const void *data, size_t len);
  1605. extern SDL_DECLSPEC Uint32 SDLCALL SDL_crc32(Uint32 crc, const void *data, size_t len);
  1606. extern SDL_DECLSPEC Uint32 SDLCALL SDL_murmur3_32(const void *data, size_t len, Uint32 seed);
  1607. /**
  1608. * Copy non-overlapping memory.
  1609. *
  1610. * The memory regions must not overlap. If they do, use SDL_memmove() instead.
  1611. *
  1612. * \param dst The destination memory region. Must not be NULL, and must not
  1613. * overlap with `src`.
  1614. * \param src The source memory region. Must not be NULL, and must not overlap
  1615. * with `dst`.
  1616. * \param len The length in bytes of both `dst` and `src`.
  1617. * \returns `dst`.
  1618. *
  1619. * \threadsafety It is safe to call this function from any thread.
  1620. *
  1621. * \since This function is available since SDL 3.1.3.
  1622. *
  1623. * \sa SDL_memmove
  1624. */
  1625. extern SDL_DECLSPEC void * SDLCALL SDL_memcpy(SDL_OUT_BYTECAP(len) void *dst, SDL_IN_BYTECAP(len) const void *src, size_t len);
  1626. /* Take advantage of compiler optimizations for memcpy */
  1627. #ifndef SDL_SLOW_MEMCPY
  1628. #ifdef SDL_memcpy
  1629. #undef SDL_memcpy
  1630. #endif
  1631. #define SDL_memcpy memcpy
  1632. #endif
  1633. #define SDL_copyp(dst, src) \
  1634. { SDL_COMPILE_TIME_ASSERT(SDL_copyp, sizeof (*(dst)) == sizeof (*(src))); } \
  1635. SDL_memcpy((dst), (src), sizeof(*(src)))
  1636. /**
  1637. * Copy memory.
  1638. *
  1639. * It is okay for the memory regions to overlap. If you are confident that the
  1640. * regions never overlap, using SDL_memcpy() may improve performance.
  1641. *
  1642. * \param dst The destination memory region. Must not be NULL.
  1643. * \param src The source memory region. Must not be NULL.
  1644. * \param len The length in bytes of both `dst` and `src`.
  1645. * \returns `dst`.
  1646. *
  1647. * \threadsafety It is safe to call this function from any thread.
  1648. *
  1649. * \since This function is available since SDL 3.1.3.
  1650. *
  1651. * \sa SDL_memcpy
  1652. */
  1653. extern SDL_DECLSPEC void * SDLCALL SDL_memmove(SDL_OUT_BYTECAP(len) void *dst, SDL_IN_BYTECAP(len) const void *src, size_t len);
  1654. /* Take advantage of compiler optimizations for memmove */
  1655. #ifndef SDL_SLOW_MEMMOVE
  1656. #ifdef SDL_memmove
  1657. #undef SDL_memmove
  1658. #endif
  1659. #define SDL_memmove memmove
  1660. #endif
  1661. extern SDL_DECLSPEC void * SDLCALL SDL_memset(SDL_OUT_BYTECAP(len) void *dst, int c, size_t len);
  1662. extern SDL_DECLSPEC void * SDLCALL SDL_memset4(void *dst, Uint32 val, size_t dwords);
  1663. /* Take advantage of compiler optimizations for memset */
  1664. #ifndef SDL_SLOW_MEMSET
  1665. #ifdef SDL_memset
  1666. #undef SDL_memset
  1667. #endif
  1668. #define SDL_memset memset
  1669. #endif
  1670. #define SDL_zero(x) SDL_memset(&(x), 0, sizeof((x)))
  1671. #define SDL_zerop(x) SDL_memset((x), 0, sizeof(*(x)))
  1672. #define SDL_zeroa(x) SDL_memset((x), 0, sizeof((x)))
  1673. extern SDL_DECLSPEC int SDLCALL SDL_memcmp(const void *s1, const void *s2, size_t len);
  1674. extern SDL_DECLSPEC size_t SDLCALL SDL_wcslen(const wchar_t *wstr);
  1675. extern SDL_DECLSPEC size_t SDLCALL SDL_wcsnlen(const wchar_t *wstr, size_t maxlen);
  1676. /**
  1677. * Copy a wide string.
  1678. *
  1679. * This function copies `maxlen` - 1 wide characters from `src` to `dst`, then
  1680. * appends a null terminator.
  1681. *
  1682. * `src` and `dst` must not overlap.
  1683. *
  1684. * If `maxlen` is 0, no wide characters are copied and no null terminator is
  1685. * written.
  1686. *
  1687. * \param dst The destination buffer. Must not be NULL, and must not overlap
  1688. * with `src`.
  1689. * \param src The null-terminated wide string to copy. Must not be NULL, and
  1690. * must not overlap with `dst`.
  1691. * \param maxlen The length (in wide characters) of the destination buffer.
  1692. * \returns The length (in wide characters, excluding the null terminator) of
  1693. * `src`.
  1694. *
  1695. * \threadsafety It is safe to call this function from any thread.
  1696. *
  1697. * \since This function is available since SDL 3.1.3.
  1698. *
  1699. * \sa SDL_wcslcat
  1700. */
  1701. extern SDL_DECLSPEC size_t SDLCALL SDL_wcslcpy(SDL_OUT_Z_CAP(maxlen) wchar_t *dst, const wchar_t *src, size_t maxlen);
  1702. /**
  1703. * Concatenate wide strings.
  1704. *
  1705. * This function appends up to `maxlen` - SDL_wcslen(dst) - 1 wide characters
  1706. * from `src` to the end of the wide string in `dst`, then appends a null
  1707. * terminator.
  1708. *
  1709. * `src` and `dst` must not overlap.
  1710. *
  1711. * If `maxlen` - SDL_wcslen(dst) - 1 is less than or equal to 0, then `dst` is
  1712. * unmodified.
  1713. *
  1714. * \param dst The destination buffer already containing the first
  1715. * null-terminated wide string. Must not be NULL and must not
  1716. * overlap with `src`.
  1717. * \param src The second null-terminated wide string. Must not be NULL, and
  1718. * must not overlap with `dst`.
  1719. * \param maxlen The length (in wide characters) of the destination buffer.
  1720. * \returns The length (in wide characters, excluding the null terminator) of
  1721. * the string in `dst` plus the length of `src`.
  1722. *
  1723. * \threadsafety It is safe to call this function from any thread.
  1724. *
  1725. * \since This function is available since SDL 3.1.3.
  1726. *
  1727. * \sa SDL_wcslcpy
  1728. */
  1729. extern SDL_DECLSPEC size_t SDLCALL SDL_wcslcat(SDL_INOUT_Z_CAP(maxlen) wchar_t *dst, const wchar_t *src, size_t maxlen);
  1730. extern SDL_DECLSPEC wchar_t * SDLCALL SDL_wcsdup(const wchar_t *wstr);
  1731. extern SDL_DECLSPEC wchar_t * SDLCALL SDL_wcsstr(const wchar_t *haystack, const wchar_t *needle);
  1732. extern SDL_DECLSPEC wchar_t * SDLCALL SDL_wcsnstr(const wchar_t *haystack, const wchar_t *needle, size_t maxlen);
  1733. /**
  1734. * Compare two null-terminated wide strings.
  1735. *
  1736. * This only compares wchar_t values until it hits a null-terminating
  1737. * character; it does not care if the string is well-formed UTF-16 (or UTF-32,
  1738. * depending on your platform's wchar_t size), or uses valid Unicode values.
  1739. *
  1740. * \param str1 the first string to compare. NULL is not permitted!
  1741. * \param str2 the second string to compare. NULL is not permitted!
  1742. * \returns less than zero if str1 is "less than" str2, greater than zero if
  1743. * str1 is "greater than" str2, and zero if the strings match
  1744. * exactly.
  1745. *
  1746. * \threadsafety It is safe to call this function from any thread.
  1747. *
  1748. * \since This function is available since SDL 3.1.3.
  1749. */
  1750. extern SDL_DECLSPEC int SDLCALL SDL_wcscmp(const wchar_t *str1, const wchar_t *str2);
  1751. /**
  1752. * Compare two wide strings up to a number of wchar_t values.
  1753. *
  1754. * This only compares wchar_t values; it does not care if the string is
  1755. * well-formed UTF-16 (or UTF-32, depending on your platform's wchar_t size),
  1756. * or uses valid Unicode values.
  1757. *
  1758. * Note that while this function is intended to be used with UTF-16 (or
  1759. * UTF-32, depending on your platform's definition of wchar_t), it is
  1760. * comparing raw wchar_t values and not Unicode codepoints: `maxlen` specifies
  1761. * a wchar_t limit! If the limit lands in the middle of a multi-wchar UTF-16
  1762. * sequence, it will only compare a portion of the final character.
  1763. *
  1764. * `maxlen` specifies a maximum number of wchar_t to compare; if the strings
  1765. * match to this number of wide chars (or both have matched to a
  1766. * null-terminator character before this count), they will be considered
  1767. * equal.
  1768. *
  1769. * \param str1 the first string to compare. NULL is not permitted!
  1770. * \param str2 the second string to compare. NULL is not permitted!
  1771. * \param maxlen the maximum number of wchar_t to compare.
  1772. * \returns less than zero if str1 is "less than" str2, greater than zero if
  1773. * str1 is "greater than" str2, and zero if the strings match
  1774. * exactly.
  1775. *
  1776. * \threadsafety It is safe to call this function from any thread.
  1777. *
  1778. * \since This function is available since SDL 3.1.3.
  1779. */
  1780. extern SDL_DECLSPEC int SDLCALL SDL_wcsncmp(const wchar_t *str1, const wchar_t *str2, size_t maxlen);
  1781. /**
  1782. * Compare two null-terminated wide strings, case-insensitively.
  1783. *
  1784. * This will work with Unicode strings, using a technique called
  1785. * "case-folding" to handle the vast majority of case-sensitive human
  1786. * languages regardless of system locale. It can deal with expanding values: a
  1787. * German Eszett character can compare against two ASCII 's' chars and be
  1788. * considered a match, for example. A notable exception: it does not handle
  1789. * the Turkish 'i' character; human language is complicated!
  1790. *
  1791. * Depending on your platform, "wchar_t" might be 2 bytes, and expected to be
  1792. * UTF-16 encoded (like Windows), or 4 bytes in UTF-32 format. Since this
  1793. * handles Unicode, it expects the string to be well-formed and not a
  1794. * null-terminated string of arbitrary bytes. Characters that are not valid
  1795. * UTF-16 (or UTF-32) are treated as Unicode character U+FFFD (REPLACEMENT
  1796. * CHARACTER), which is to say two strings of random bits may turn out to
  1797. * match if they convert to the same amount of replacement characters.
  1798. *
  1799. * \param str1 the first string to compare. NULL is not permitted!
  1800. * \param str2 the second string to compare. NULL is not permitted!
  1801. * \returns less than zero if str1 is "less than" str2, greater than zero if
  1802. * str1 is "greater than" str2, and zero if the strings match
  1803. * exactly.
  1804. *
  1805. * \threadsafety It is safe to call this function from any thread.
  1806. *
  1807. * \since This function is available since SDL 3.1.3.
  1808. */
  1809. extern SDL_DECLSPEC int SDLCALL SDL_wcscasecmp(const wchar_t *str1, const wchar_t *str2);
  1810. /**
  1811. * Compare two wide strings, case-insensitively, up to a number of wchar_t.
  1812. *
  1813. * This will work with Unicode strings, using a technique called
  1814. * "case-folding" to handle the vast majority of case-sensitive human
  1815. * languages regardless of system locale. It can deal with expanding values: a
  1816. * German Eszett character can compare against two ASCII 's' chars and be
  1817. * considered a match, for example. A notable exception: it does not handle
  1818. * the Turkish 'i' character; human language is complicated!
  1819. *
  1820. * Depending on your platform, "wchar_t" might be 2 bytes, and expected to be
  1821. * UTF-16 encoded (like Windows), or 4 bytes in UTF-32 format. Since this
  1822. * handles Unicode, it expects the string to be well-formed and not a
  1823. * null-terminated string of arbitrary bytes. Characters that are not valid
  1824. * UTF-16 (or UTF-32) are treated as Unicode character U+FFFD (REPLACEMENT
  1825. * CHARACTER), which is to say two strings of random bits may turn out to
  1826. * match if they convert to the same amount of replacement characters.
  1827. *
  1828. * Note that while this function might deal with variable-sized characters,
  1829. * `maxlen` specifies a _wchar_ limit! If the limit lands in the middle of a
  1830. * multi-byte UTF-16 sequence, it may convert a portion of the final character
  1831. * to one or more Unicode character U+FFFD (REPLACEMENT CHARACTER) so as not
  1832. * to overflow a buffer.
  1833. *
  1834. * `maxlen` specifies a maximum number of wchar_t values to compare; if the
  1835. * strings match to this number of wchar_t (or both have matched to a
  1836. * null-terminator character before this number of bytes), they will be
  1837. * considered equal.
  1838. *
  1839. * \param str1 the first string to compare. NULL is not permitted!
  1840. * \param str2 the second string to compare. NULL is not permitted!
  1841. * \param maxlen the maximum number of wchar_t values to compare.
  1842. * \returns less than zero if str1 is "less than" str2, greater than zero if
  1843. * str1 is "greater than" str2, and zero if the strings match
  1844. * exactly.
  1845. *
  1846. * \threadsafety It is safe to call this function from any thread.
  1847. *
  1848. * \since This function is available since SDL 3.1.3.
  1849. */
  1850. extern SDL_DECLSPEC int SDLCALL SDL_wcsncasecmp(const wchar_t *str1, const wchar_t *str2, size_t maxlen);
  1851. /**
  1852. * Parse a `long` from a wide string.
  1853. *
  1854. * If `str` starts with whitespace, then those whitespace characters are
  1855. * skipped before attempting to parse the number.
  1856. *
  1857. * If the parsed number does not fit inside a `long`, the result is clamped to
  1858. * the minimum and maximum representable `long` values.
  1859. *
  1860. * \param str The null-terminated wide string to read. Must not be NULL.
  1861. * \param endp If not NULL, the address of the first invalid wide character
  1862. * (i.e. the next character after the parsed number) will be
  1863. * written to this pointer.
  1864. * \param base The base of the integer to read. Supported values are 0 and 2
  1865. * to 36 inclusive. If 0, the base will be inferred from the
  1866. * number's prefix (0x for hexadecimal, 0 for octal, decimal
  1867. * otherwise).
  1868. * \returns The parsed `long`, or 0 if no number could be parsed.
  1869. *
  1870. * \threadsafety It is safe to call this function from any thread.
  1871. *
  1872. * \since This function is available since SDL 3.1.3.
  1873. *
  1874. * \sa SDL_strtol
  1875. */
  1876. extern SDL_DECLSPEC long SDLCALL SDL_wcstol(const wchar_t *str, wchar_t **endp, int base);
  1877. /**
  1878. * This works exactly like strlen() but doesn't require access to a C runtime.
  1879. *
  1880. * Counts the bytes in `str`, excluding the null terminator.
  1881. *
  1882. * If you need the length of a UTF-8 string, consider using SDL_utf8strlen().
  1883. *
  1884. * \param str The null-terminated string to read. Must not be NULL.
  1885. * \returns The length (in bytes, excluding the null terminator) of `src`.
  1886. *
  1887. * \threadsafety It is safe to call this function from any thread.
  1888. *
  1889. * \since This function is available since SDL 3.1.3.
  1890. *
  1891. * \sa SDL_strnlen
  1892. * \sa SDL_utf8strlen
  1893. * \sa SDL_utf8strnlen
  1894. */
  1895. extern SDL_DECLSPEC size_t SDLCALL SDL_strlen(const char *str);
  1896. /**
  1897. * This works exactly like strnlen() but doesn't require access to a C
  1898. * runtime.
  1899. *
  1900. * Counts up to a maximum of `maxlen` bytes in `str`, excluding the null
  1901. * terminator.
  1902. *
  1903. * If you need the length of a UTF-8 string, consider using SDL_utf8strnlen().
  1904. *
  1905. * \param str The null-terminated string to read. Must not be NULL.
  1906. * \param maxlen The maximum amount of bytes to count.
  1907. * \returns The length (in bytes, excluding the null terminator) of `src` but
  1908. * never more than `maxlen`.
  1909. *
  1910. * \threadsafety It is safe to call this function from any thread.
  1911. *
  1912. * \since This function is available since SDL 3.1.3.
  1913. *
  1914. * \sa SDL_strlen
  1915. * \sa SDL_utf8strlen
  1916. * \sa SDL_utf8strnlen
  1917. */
  1918. extern SDL_DECLSPEC size_t SDLCALL SDL_strnlen(const char *str, size_t maxlen);
  1919. /**
  1920. * Copy a string.
  1921. *
  1922. * This function copies up to `maxlen` - 1 characters from `src` to `dst`,
  1923. * then appends a null terminator.
  1924. *
  1925. * If `maxlen` is 0, no characters are copied and no null terminator is
  1926. * written.
  1927. *
  1928. * If you want to copy an UTF-8 string but need to ensure that multi-byte
  1929. * sequences are not truncated, consider using SDL_utf8strlcpy().
  1930. *
  1931. * \param dst The destination buffer. Must not be NULL, and must not overlap
  1932. * with `src`.
  1933. * \param src The null-terminated string to copy. Must not be NULL, and must
  1934. * not overlap with `dst`.
  1935. * \param maxlen The length (in characters) of the destination buffer.
  1936. * \returns The length (in characters, excluding the null terminator) of
  1937. * `src`.
  1938. *
  1939. * \threadsafety It is safe to call this function from any thread.
  1940. *
  1941. * \since This function is available since SDL 3.1.3.
  1942. *
  1943. * \sa SDL_strlcat
  1944. * \sa SDL_utf8strlcpy
  1945. */
  1946. extern SDL_DECLSPEC size_t SDLCALL SDL_strlcpy(SDL_OUT_Z_CAP(maxlen) char *dst, const char *src, size_t maxlen);
  1947. /**
  1948. * Copy an UTF-8 string.
  1949. *
  1950. * This function copies up to `dst_bytes` - 1 bytes from `src` to `dst` while
  1951. * also ensuring that the string written to `dst` does not end in a truncated
  1952. * multi-byte sequence. Finally, it appends a null terminator.
  1953. *
  1954. * `src` and `dst` must not overlap.
  1955. *
  1956. * Note that unlike SDL_strlcpy(), this function returns the number of bytes
  1957. * written, not the length of `src`.
  1958. *
  1959. * \param dst The destination buffer. Must not be NULL, and must not overlap
  1960. * with `src`.
  1961. * \param src The null-terminated UTF-8 string to copy. Must not be NULL, and
  1962. * must not overlap with `dst`.
  1963. * \param dst_bytes The length (in bytes) of the destination buffer. Must not
  1964. * be 0.
  1965. * \returns The number of bytes written, excluding the null terminator.
  1966. *
  1967. * \threadsafety It is safe to call this function from any thread.
  1968. *
  1969. * \since This function is available since SDL 3.1.3.
  1970. *
  1971. * \sa SDL_strlcpy
  1972. */
  1973. extern SDL_DECLSPEC size_t SDLCALL SDL_utf8strlcpy(SDL_OUT_Z_CAP(dst_bytes) char *dst, const char *src, size_t dst_bytes);
  1974. /**
  1975. * Concatenate strings.
  1976. *
  1977. * This function appends up to `maxlen` - SDL_strlen(dst) - 1 characters from
  1978. * `src` to the end of the string in `dst`, then appends a null terminator.
  1979. *
  1980. * `src` and `dst` must not overlap.
  1981. *
  1982. * If `maxlen` - SDL_strlen(dst) - 1 is less than or equal to 0, then `dst` is
  1983. * unmodified.
  1984. *
  1985. * \param dst The destination buffer already containing the first
  1986. * null-terminated string. Must not be NULL and must not overlap
  1987. * with `src`.
  1988. * \param src The second null-terminated string. Must not be NULL, and must
  1989. * not overlap with `dst`.
  1990. * \param maxlen The length (in characters) of the destination buffer.
  1991. * \returns The length (in characters, excluding the null terminator) of the
  1992. * string in `dst` plus the length of `src`.
  1993. *
  1994. * \threadsafety It is safe to call this function from any thread.
  1995. *
  1996. * \since This function is available since SDL 3.1.3.
  1997. *
  1998. * \sa SDL_strlcpy
  1999. */
  2000. extern SDL_DECLSPEC size_t SDLCALL SDL_strlcat(SDL_INOUT_Z_CAP(maxlen) char *dst, const char *src, size_t maxlen);
  2001. extern SDL_DECLSPEC SDL_MALLOC char * SDLCALL SDL_strdup(const char *str);
  2002. extern SDL_DECLSPEC SDL_MALLOC char * SDLCALL SDL_strndup(const char *str, size_t maxlen);
  2003. extern SDL_DECLSPEC char * SDLCALL SDL_strrev(char *str);
  2004. /**
  2005. * Convert a string to uppercase.
  2006. *
  2007. * **WARNING**: Regardless of system locale, this will only convert ASCII
  2008. * values 'A' through 'Z' to uppercase.
  2009. *
  2010. * This function operates on a null-terminated string of bytes--even if it is
  2011. * malformed UTF-8!--and converts ASCII characters 'a' through 'z' to their
  2012. * uppercase equivalents in-place, returning the original `str` pointer.
  2013. *
  2014. * \param str the string to convert in-place. Can not be NULL.
  2015. * \returns the `str` pointer passed into this function.
  2016. *
  2017. * \threadsafety It is safe to call this function from any thread.
  2018. *
  2019. * \since This function is available since SDL 3.1.3.
  2020. *
  2021. * \sa SDL_strlwr
  2022. */
  2023. extern SDL_DECLSPEC char * SDLCALL SDL_strupr(char *str);
  2024. /**
  2025. * Convert a string to lowercase.
  2026. *
  2027. * **WARNING**: Regardless of system locale, this will only convert ASCII
  2028. * values 'A' through 'Z' to lowercase.
  2029. *
  2030. * This function operates on a null-terminated string of bytes--even if it is
  2031. * malformed UTF-8!--and converts ASCII characters 'A' through 'Z' to their
  2032. * lowercase equivalents in-place, returning the original `str` pointer.
  2033. *
  2034. * \param str the string to convert in-place. Can not be NULL.
  2035. * \returns the `str` pointer passed into this function.
  2036. *
  2037. * \threadsafety It is safe to call this function from any thread.
  2038. *
  2039. * \since This function is available since SDL 3.1.3.
  2040. *
  2041. * \sa SDL_strupr
  2042. */
  2043. extern SDL_DECLSPEC char * SDLCALL SDL_strlwr(char *str);
  2044. extern SDL_DECLSPEC char * SDLCALL SDL_strchr(const char *str, int c);
  2045. extern SDL_DECLSPEC char * SDLCALL SDL_strrchr(const char *str, int c);
  2046. extern SDL_DECLSPEC char * SDLCALL SDL_strstr(const char *haystack, const char *needle);
  2047. extern SDL_DECLSPEC char * SDLCALL SDL_strnstr(const char *haystack, const char *needle, size_t maxlen);
  2048. extern SDL_DECLSPEC char * SDLCALL SDL_strcasestr(const char *haystack, const char *needle);
  2049. extern SDL_DECLSPEC char * SDLCALL SDL_strtok_r(char *s1, const char *s2, char **saveptr);
  2050. extern SDL_DECLSPEC size_t SDLCALL SDL_utf8strlen(const char *str);
  2051. extern SDL_DECLSPEC size_t SDLCALL SDL_utf8strnlen(const char *str, size_t bytes);
  2052. extern SDL_DECLSPEC char * SDLCALL SDL_itoa(int value, char *str, int radix);
  2053. extern SDL_DECLSPEC char * SDLCALL SDL_uitoa(unsigned int value, char *str, int radix);
  2054. extern SDL_DECLSPEC char * SDLCALL SDL_ltoa(long value, char *str, int radix);
  2055. extern SDL_DECLSPEC char * SDLCALL SDL_ultoa(unsigned long value, char *str, int radix);
  2056. extern SDL_DECLSPEC char * SDLCALL SDL_lltoa(long long value, char *str, int radix);
  2057. extern SDL_DECLSPEC char * SDLCALL SDL_ulltoa(unsigned long long value, char *str, int radix);
  2058. /**
  2059. * Parse an `int` from a string.
  2060. *
  2061. * The result of calling `SDL_atoi(str)` is equivalent to
  2062. * `(int)SDL_strtol(str, NULL, 10)`.
  2063. *
  2064. * \param str The null-terminated string to read. Must not be NULL.
  2065. * \returns The parsed `int`.
  2066. *
  2067. * \threadsafety It is safe to call this function from any thread.
  2068. *
  2069. * \since This function is available since SDL 3.1.3.
  2070. *
  2071. * \sa SDL_atof
  2072. * \sa SDL_strtol
  2073. * \sa SDL_strtoul
  2074. * \sa SDL_strtoll
  2075. * \sa SDL_strtoull
  2076. * \sa SDL_strtod
  2077. * \sa SDL_itoa
  2078. */
  2079. extern SDL_DECLSPEC int SDLCALL SDL_atoi(const char *str);
  2080. /**
  2081. * Parse a `double` from a string.
  2082. *
  2083. * The result of calling `SDL_atof(str)` is equivalent to `SDL_strtod(str,
  2084. * NULL)`.
  2085. *
  2086. * \param str The null-terminated string to read. Must not be NULL.
  2087. * \returns The parsed `double`.
  2088. *
  2089. * \threadsafety It is safe to call this function from any thread.
  2090. *
  2091. * \since This function is available since SDL 3.1.3.
  2092. *
  2093. * \sa SDL_atoi
  2094. * \sa SDL_strtol
  2095. * \sa SDL_strtoul
  2096. * \sa SDL_strtoll
  2097. * \sa SDL_strtoull
  2098. * \sa SDL_strtod
  2099. */
  2100. extern SDL_DECLSPEC double SDLCALL SDL_atof(const char *str);
  2101. /**
  2102. * Parse a `long` from a string.
  2103. *
  2104. * If `str` starts with whitespace, then those whitespace characters are
  2105. * skipped before attempting to parse the number.
  2106. *
  2107. * If the parsed number does not fit inside a `long`, the result is clamped to
  2108. * the minimum and maximum representable `long` values.
  2109. *
  2110. * \param str The null-terminated string to read. Must not be NULL.
  2111. * \param endp If not NULL, the address of the first invalid character (i.e.
  2112. * the next character after the parsed number) will be written to
  2113. * this pointer.
  2114. * \param base The base of the integer to read. Supported values are 0 and 2
  2115. * to 36 inclusive. If 0, the base will be inferred from the
  2116. * number's prefix (0x for hexadecimal, 0 for octal, decimal
  2117. * otherwise).
  2118. * \returns The parsed `long`, or 0 if no number could be parsed.
  2119. *
  2120. * \threadsafety It is safe to call this function from any thread.
  2121. *
  2122. * \since This function is available since SDL 3.1.3.
  2123. *
  2124. * \sa SDL_atoi
  2125. * \sa SDL_atof
  2126. * \sa SDL_strtoul
  2127. * \sa SDL_strtoll
  2128. * \sa SDL_strtoull
  2129. * \sa SDL_strtod
  2130. * \sa SDL_ltoa
  2131. * \sa SDL_wcstol
  2132. */
  2133. extern SDL_DECLSPEC long SDLCALL SDL_strtol(const char *str, char **endp, int base);
  2134. /**
  2135. * Parse an `unsigned long` from a string.
  2136. *
  2137. * If `str` starts with whitespace, then those whitespace characters are
  2138. * skipped before attempting to parse the number.
  2139. *
  2140. * If the parsed number does not fit inside an `unsigned long`, the result is
  2141. * clamped to the maximum representable `unsigned long` value.
  2142. *
  2143. * \param str The null-terminated string to read. Must not be NULL.
  2144. * \param endp If not NULL, the address of the first invalid character (i.e.
  2145. * the next character after the parsed number) will be written to
  2146. * this pointer.
  2147. * \param base The base of the integer to read. Supported values are 0 and 2
  2148. * to 36 inclusive. If 0, the base will be inferred from the
  2149. * number's prefix (0x for hexadecimal, 0 for octal, decimal
  2150. * otherwise).
  2151. * \returns The parsed `unsigned long`, or 0 if no number could be parsed.
  2152. *
  2153. * \threadsafety It is safe to call this function from any thread.
  2154. *
  2155. * \since This function is available since SDL 3.1.3.
  2156. *
  2157. * \sa SDL_atoi
  2158. * \sa SDL_atof
  2159. * \sa SDL_strtol
  2160. * \sa SDL_strtoll
  2161. * \sa SDL_strtoull
  2162. * \sa SDL_strtod
  2163. * \sa SDL_ultoa
  2164. */
  2165. extern SDL_DECLSPEC unsigned long SDLCALL SDL_strtoul(const char *str, char **endp, int base);
  2166. /**
  2167. * Parse a `long long` from a string.
  2168. *
  2169. * If `str` starts with whitespace, then those whitespace characters are
  2170. * skipped before attempting to parse the number.
  2171. *
  2172. * If the parsed number does not fit inside a `long long`, the result is
  2173. * clamped to the minimum and maximum representable `long long` values.
  2174. *
  2175. * \param str The null-terminated string to read. Must not be NULL.
  2176. * \param endp If not NULL, the address of the first invalid character (i.e.
  2177. * the next character after the parsed number) will be written to
  2178. * this pointer.
  2179. * \param base The base of the integer to read. Supported values are 0 and 2
  2180. * to 36 inclusive. If 0, the base will be inferred from the
  2181. * number's prefix (0x for hexadecimal, 0 for octal, decimal
  2182. * otherwise).
  2183. * \returns The parsed `long long`, or 0 if no number could be parsed.
  2184. *
  2185. * \threadsafety It is safe to call this function from any thread.
  2186. *
  2187. * \since This function is available since SDL 3.1.3.
  2188. *
  2189. * \sa SDL_atoi
  2190. * \sa SDL_atof
  2191. * \sa SDL_strtol
  2192. * \sa SDL_strtoul
  2193. * \sa SDL_strtoull
  2194. * \sa SDL_strtod
  2195. * \sa SDL_lltoa
  2196. */
  2197. extern SDL_DECLSPEC long long SDLCALL SDL_strtoll(const char *str, char **endp, int base);
  2198. /**
  2199. * Parse an `unsigned long long` from a string.
  2200. *
  2201. * If `str` starts with whitespace, then those whitespace characters are
  2202. * skipped before attempting to parse the number.
  2203. *
  2204. * If the parsed number does not fit inside an `unsigned long long`, the
  2205. * result is clamped to the maximum representable `unsigned long long` value.
  2206. *
  2207. * \param str The null-terminated string to read. Must not be NULL.
  2208. * \param endp If not NULL, the address of the first invalid character (i.e.
  2209. * the next character after the parsed number) will be written to
  2210. * this pointer.
  2211. * \param base The base of the integer to read. Supported values are 0 and 2
  2212. * to 36 inclusive. If 0, the base will be inferred from the
  2213. * number's prefix (0x for hexadecimal, 0 for octal, decimal
  2214. * otherwise).
  2215. * \returns The parsed `unsigned long long`, or 0 if no number could be
  2216. * parsed.
  2217. *
  2218. * \threadsafety It is safe to call this function from any thread.
  2219. *
  2220. * \since This function is available since SDL 3.1.3.
  2221. *
  2222. * \sa SDL_atoi
  2223. * \sa SDL_atof
  2224. * \sa SDL_strtol
  2225. * \sa SDL_strtoll
  2226. * \sa SDL_strtoul
  2227. * \sa SDL_strtod
  2228. * \sa SDL_ulltoa
  2229. */
  2230. extern SDL_DECLSPEC unsigned long long SDLCALL SDL_strtoull(const char *str, char **endp, int base);
  2231. /**
  2232. * Parse a `double` from a string.
  2233. *
  2234. * This function makes fewer guarantees than the C runtime `strtod`:
  2235. *
  2236. * - Only decimal notation is guaranteed to be supported. The handling of
  2237. * scientific and hexadecimal notation is unspecified.
  2238. * - Whether or not INF and NAN can be parsed is unspecified.
  2239. * - The precision of the result is unspecified.
  2240. *
  2241. * \param str The null-terminated string to read. Must not be NULL.
  2242. * \param endp If not NULL, the address of the first invalid character (i.e.
  2243. * the next character after the parsed number) will be written to
  2244. * this pointer.
  2245. * \returns The parsed `double`, or 0 if no number could be parsed.
  2246. *
  2247. * \threadsafety It is safe to call this function from any thread.
  2248. *
  2249. * \since This function is available since SDL 3.1.3.
  2250. *
  2251. * \sa SDL_atoi
  2252. * \sa SDL_atof
  2253. * \sa SDL_strtol
  2254. * \sa SDL_strtoll
  2255. * \sa SDL_strtoul
  2256. * \sa SDL_strtoull
  2257. */
  2258. extern SDL_DECLSPEC double SDLCALL SDL_strtod(const char *str, char **endp);
  2259. /**
  2260. * Compare two null-terminated UTF-8 strings.
  2261. *
  2262. * Due to the nature of UTF-8 encoding, this will work with Unicode strings,
  2263. * since effectively this function just compares bytes until it hits a
  2264. * null-terminating character. Also due to the nature of UTF-8, this can be
  2265. * used with SDL_qsort() to put strings in (roughly) alphabetical order.
  2266. *
  2267. * \param str1 the first string to compare. NULL is not permitted!
  2268. * \param str2 the second string to compare. NULL is not permitted!
  2269. * \returns less than zero if str1 is "less than" str2, greater than zero if
  2270. * str1 is "greater than" str2, and zero if the strings match
  2271. * exactly.
  2272. *
  2273. * \threadsafety It is safe to call this function from any thread.
  2274. *
  2275. * \since This function is available since SDL 3.1.3.
  2276. */
  2277. extern SDL_DECLSPEC int SDLCALL SDL_strcmp(const char *str1, const char *str2);
  2278. /**
  2279. * Compare two UTF-8 strings up to a number of bytes.
  2280. *
  2281. * Due to the nature of UTF-8 encoding, this will work with Unicode strings,
  2282. * since effectively this function just compares bytes until it hits a
  2283. * null-terminating character. Also due to the nature of UTF-8, this can be
  2284. * used with SDL_qsort() to put strings in (roughly) alphabetical order.
  2285. *
  2286. * Note that while this function is intended to be used with UTF-8, it is
  2287. * doing a bytewise comparison, and `maxlen` specifies a _byte_ limit! If the
  2288. * limit lands in the middle of a multi-byte UTF-8 sequence, it will only
  2289. * compare a portion of the final character.
  2290. *
  2291. * `maxlen` specifies a maximum number of bytes to compare; if the strings
  2292. * match to this number of bytes (or both have matched to a null-terminator
  2293. * character before this number of bytes), they will be considered equal.
  2294. *
  2295. * \param str1 the first string to compare. NULL is not permitted!
  2296. * \param str2 the second string to compare. NULL is not permitted!
  2297. * \param maxlen the maximum number of _bytes_ to compare.
  2298. * \returns less than zero if str1 is "less than" str2, greater than zero if
  2299. * str1 is "greater than" str2, and zero if the strings match
  2300. * exactly.
  2301. *
  2302. * \threadsafety It is safe to call this function from any thread.
  2303. *
  2304. * \since This function is available since SDL 3.1.3.
  2305. */
  2306. extern SDL_DECLSPEC int SDLCALL SDL_strncmp(const char *str1, const char *str2, size_t maxlen);
  2307. /**
  2308. * Compare two null-terminated UTF-8 strings, case-insensitively.
  2309. *
  2310. * This will work with Unicode strings, using a technique called
  2311. * "case-folding" to handle the vast majority of case-sensitive human
  2312. * languages regardless of system locale. It can deal with expanding values: a
  2313. * German Eszett character can compare against two ASCII 's' chars and be
  2314. * considered a match, for example. A notable exception: it does not handle
  2315. * the Turkish 'i' character; human language is complicated!
  2316. *
  2317. * Since this handles Unicode, it expects the string to be well-formed UTF-8
  2318. * and not a null-terminated string of arbitrary bytes. Bytes that are not
  2319. * valid UTF-8 are treated as Unicode character U+FFFD (REPLACEMENT
  2320. * CHARACTER), which is to say two strings of random bits may turn out to
  2321. * match if they convert to the same amount of replacement characters.
  2322. *
  2323. * \param str1 the first string to compare. NULL is not permitted!
  2324. * \param str2 the second string to compare. NULL is not permitted!
  2325. * \returns less than zero if str1 is "less than" str2, greater than zero if
  2326. * str1 is "greater than" str2, and zero if the strings match
  2327. * exactly.
  2328. *
  2329. * \threadsafety It is safe to call this function from any thread.
  2330. *
  2331. * \since This function is available since SDL 3.1.3.
  2332. */
  2333. extern SDL_DECLSPEC int SDLCALL SDL_strcasecmp(const char *str1, const char *str2);
  2334. /**
  2335. * Compare two UTF-8 strings, case-insensitively, up to a number of bytes.
  2336. *
  2337. * This will work with Unicode strings, using a technique called
  2338. * "case-folding" to handle the vast majority of case-sensitive human
  2339. * languages regardless of system locale. It can deal with expanding values: a
  2340. * German Eszett character can compare against two ASCII 's' chars and be
  2341. * considered a match, for example. A notable exception: it does not handle
  2342. * the Turkish 'i' character; human language is complicated!
  2343. *
  2344. * Since this handles Unicode, it expects the string to be well-formed UTF-8
  2345. * and not a null-terminated string of arbitrary bytes. Bytes that are not
  2346. * valid UTF-8 are treated as Unicode character U+FFFD (REPLACEMENT
  2347. * CHARACTER), which is to say two strings of random bits may turn out to
  2348. * match if they convert to the same amount of replacement characters.
  2349. *
  2350. * Note that while this function is intended to be used with UTF-8, `maxlen`
  2351. * specifies a _byte_ limit! If the limit lands in the middle of a multi-byte
  2352. * UTF-8 sequence, it may convert a portion of the final character to one or
  2353. * more Unicode character U+FFFD (REPLACEMENT CHARACTER) so as not to overflow
  2354. * a buffer.
  2355. *
  2356. * `maxlen` specifies a maximum number of bytes to compare; if the strings
  2357. * match to this number of bytes (or both have matched to a null-terminator
  2358. * character before this number of bytes), they will be considered equal.
  2359. *
  2360. * \param str1 the first string to compare. NULL is not permitted!
  2361. * \param str2 the second string to compare. NULL is not permitted!
  2362. * \param maxlen the maximum number of bytes to compare.
  2363. * \returns less than zero if str1 is "less than" str2, greater than zero if
  2364. * str1 is "greater than" str2, and zero if the strings match
  2365. * exactly.
  2366. *
  2367. * \threadsafety It is safe to call this function from any thread.
  2368. *
  2369. * \since This function is available since SDL 3.1.3.
  2370. */
  2371. extern SDL_DECLSPEC int SDLCALL SDL_strncasecmp(const char *str1, const char *str2, size_t maxlen);
  2372. /**
  2373. * Searches a string for the first occurence of any character contained in a
  2374. * breakset, and returns a pointer from the string to that character.
  2375. *
  2376. * \param str The null-terminated string to be searched. Must not be NULL, and
  2377. * must not overlap with `breakset`.
  2378. * \param breakset A null-terminated string containing the list of characters
  2379. * to look for. Must not be NULL, and must not overlap with
  2380. * `str`.
  2381. * \returns A pointer to the location, in str, of the first occurence of a
  2382. * character present in the breakset, or NULL if none is found.
  2383. *
  2384. * \threadsafety It is safe to call this function from any thread.
  2385. *
  2386. * \since This function is available since SDL 3.1.3.
  2387. */
  2388. extern SDL_DECLSPEC char * SDLCALL SDL_strpbrk(const char *str, const char *breakset);
  2389. /**
  2390. * The Unicode REPLACEMENT CHARACTER codepoint.
  2391. *
  2392. * SDL_StepUTF8() and SDL_StepBackUTF8() report this codepoint when they
  2393. * encounter a UTF-8 string with encoding errors.
  2394. *
  2395. * This tends to render as something like a question mark in most places.
  2396. *
  2397. * \since This macro is available since SDL 3.1.3.
  2398. *
  2399. * \sa SDL_StepBackUTF8
  2400. * \sa SDL_StepUTF8
  2401. */
  2402. #define SDL_INVALID_UNICODE_CODEPOINT 0xFFFD
  2403. /**
  2404. * Decode a UTF-8 string, one Unicode codepoint at a time.
  2405. *
  2406. * This will return the first Unicode codepoint in the UTF-8 encoded string in
  2407. * `*pstr`, and then advance `*pstr` past any consumed bytes before returning.
  2408. *
  2409. * It will not access more than `*pslen` bytes from the string. `*pslen` will
  2410. * be adjusted, as well, subtracting the number of bytes consumed.
  2411. *
  2412. * `pslen` is allowed to be NULL, in which case the string _must_ be
  2413. * NULL-terminated, as the function will blindly read until it sees the NULL
  2414. * char.
  2415. *
  2416. * if `*pslen` is zero, it assumes the end of string is reached and returns a
  2417. * zero codepoint regardless of the contents of the string buffer.
  2418. *
  2419. * If the resulting codepoint is zero (a NULL terminator), or `*pslen` is
  2420. * zero, it will not advance `*pstr` or `*pslen` at all.
  2421. *
  2422. * Generally this function is called in a loop until it returns zero,
  2423. * adjusting its parameters each iteration.
  2424. *
  2425. * If an invalid UTF-8 sequence is encountered, this function returns
  2426. * SDL_INVALID_UNICODE_CODEPOINT and advances the string/length by one byte
  2427. * (which is to say, a multibyte sequence might produce several
  2428. * SDL_INVALID_UNICODE_CODEPOINT returns before it syncs to the next valid
  2429. * UTF-8 sequence).
  2430. *
  2431. * Several things can generate invalid UTF-8 sequences, including overlong
  2432. * encodings, the use of UTF-16 surrogate values, and truncated data. Please
  2433. * refer to
  2434. * [RFC3629](https://www.ietf.org/rfc/rfc3629.txt)
  2435. * for details.
  2436. *
  2437. * \param pstr a pointer to a UTF-8 string pointer to be read and adjusted.
  2438. * \param pslen a pointer to the number of bytes in the string, to be read and
  2439. * adjusted. NULL is allowed.
  2440. * \returns the first Unicode codepoint in the string.
  2441. *
  2442. * \threadsafety It is safe to call this function from any thread.
  2443. *
  2444. * \since This function is available since SDL 3.1.3.
  2445. */
  2446. extern SDL_DECLSPEC Uint32 SDLCALL SDL_StepUTF8(const char **pstr, size_t *pslen);
  2447. /**
  2448. * Decode a UTF-8 string in reverse, one Unicode codepoint at a time.
  2449. *
  2450. * This will go to the start of the previous Unicode codepoint in the string,
  2451. * move `*pstr` to that location and return that codepoint.
  2452. *
  2453. * If `*pstr` is already at the start of the string), it will not advance
  2454. * `*pstr` at all.
  2455. *
  2456. * Generally this function is called in a loop until it returns zero,
  2457. * adjusting its parameter each iteration.
  2458. *
  2459. * If an invalid UTF-8 sequence is encountered, this function returns
  2460. * SDL_INVALID_UNICODE_CODEPOINT.
  2461. *
  2462. * Several things can generate invalid UTF-8 sequences, including overlong
  2463. * encodings, the use of UTF-16 surrogate values, and truncated data. Please
  2464. * refer to
  2465. * [RFC3629](https://www.ietf.org/rfc/rfc3629.txt)
  2466. * for details.
  2467. *
  2468. * \param start a pointer to the beginning of the UTF-8 string.
  2469. * \param pstr a pointer to a UTF-8 string pointer to be read and adjusted.
  2470. * \returns the previous Unicode codepoint in the string.
  2471. *
  2472. * \threadsafety It is safe to call this function from any thread.
  2473. *
  2474. * \since This function is available since SDL 3.1.6.
  2475. */
  2476. extern SDL_DECLSPEC Uint32 SDLCALL SDL_StepBackUTF8(const char *start, const char **pstr);
  2477. /**
  2478. * Convert a single Unicode codepoint to UTF-8.
  2479. *
  2480. * The buffer pointed to by `dst` must be at least 4 bytes long, as this
  2481. * function may generate between 1 and 4 bytes of output.
  2482. *
  2483. * This function returns the first byte _after_ the newly-written UTF-8
  2484. * sequence, which is useful for encoding multiple codepoints in a loop, or
  2485. * knowing where to write a NULL-terminator character to end the string (in
  2486. * either case, plan to have a buffer of _more_ than 4 bytes!).
  2487. *
  2488. * If `codepoint` is an invalid value (outside the Unicode range, or a UTF-16
  2489. * surrogate value, etc), this will use U+FFFD (REPLACEMENT CHARACTER) for the
  2490. * codepoint instead, and not set an error.
  2491. *
  2492. * If `dst` is NULL, this returns NULL immediately without writing to the
  2493. * pointer and without setting an error.
  2494. *
  2495. * \param codepoint a Unicode codepoint to convert to UTF-8.
  2496. * \param dst the location to write the encoded UTF-8. Must point to at least
  2497. * 4 bytes!
  2498. * \returns the first byte past the newly-written UTF-8 sequence.
  2499. *
  2500. * \threadsafety It is safe to call this function from any thread.
  2501. *
  2502. * \since This function is available since SDL 3.1.3.
  2503. */
  2504. extern SDL_DECLSPEC char * SDLCALL SDL_UCS4ToUTF8(Uint32 codepoint, char *dst);
  2505. extern SDL_DECLSPEC int SDLCALL SDL_sscanf(const char *text, SDL_SCANF_FORMAT_STRING const char *fmt, ...) SDL_SCANF_VARARG_FUNC(2);
  2506. extern SDL_DECLSPEC int SDLCALL SDL_vsscanf(const char *text, SDL_SCANF_FORMAT_STRING const char *fmt, va_list ap) SDL_SCANF_VARARG_FUNCV(2);
  2507. extern SDL_DECLSPEC int SDLCALL SDL_snprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) SDL_PRINTF_VARARG_FUNC(3);
  2508. extern SDL_DECLSPEC int SDLCALL SDL_swprintf(SDL_OUT_Z_CAP(maxlen) wchar_t *text, size_t maxlen, SDL_PRINTF_FORMAT_STRING const wchar_t *fmt, ...) SDL_WPRINTF_VARARG_FUNC(3);
  2509. extern SDL_DECLSPEC int SDLCALL SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap) SDL_PRINTF_VARARG_FUNCV(3);
  2510. extern SDL_DECLSPEC int SDLCALL SDL_vswprintf(SDL_OUT_Z_CAP(maxlen) wchar_t *text, size_t maxlen, SDL_PRINTF_FORMAT_STRING const wchar_t *fmt, va_list ap) SDL_WPRINTF_VARARG_FUNCV(3);
  2511. extern SDL_DECLSPEC int SDLCALL SDL_asprintf(char **strp, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) SDL_PRINTF_VARARG_FUNC(2);
  2512. extern SDL_DECLSPEC int SDLCALL SDL_vasprintf(char **strp, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap) SDL_PRINTF_VARARG_FUNCV(2);
  2513. /**
  2514. * Seeds the pseudo-random number generator.
  2515. *
  2516. * Reusing the seed number will cause SDL_rand_*() to repeat the same stream
  2517. * of 'random' numbers.
  2518. *
  2519. * \param seed the value to use as a random number seed, or 0 to use
  2520. * SDL_GetPerformanceCounter().
  2521. *
  2522. * \threadsafety This should be called on the same thread that calls
  2523. * SDL_rand*()
  2524. *
  2525. * \since This function is available since SDL 3.1.3.
  2526. *
  2527. * \sa SDL_rand
  2528. * \sa SDL_rand_bits
  2529. * \sa SDL_randf
  2530. */
  2531. extern SDL_DECLSPEC void SDLCALL SDL_srand(Uint64 seed);
  2532. /**
  2533. * Generate a pseudo-random number less than n for positive n
  2534. *
  2535. * The method used is faster and of better quality than `rand() % n`. Odds are
  2536. * roughly 99.9% even for n = 1 million. Evenness is better for smaller n, and
  2537. * much worse as n gets bigger.
  2538. *
  2539. * Example: to simulate a d6 use `SDL_rand(6) + 1` The +1 converts 0..5 to
  2540. * 1..6
  2541. *
  2542. * If you want to generate a pseudo-random number in the full range of Sint32,
  2543. * you should use: (Sint32)SDL_rand_bits()
  2544. *
  2545. * If you want reproducible output, be sure to initialize with SDL_srand()
  2546. * first.
  2547. *
  2548. * There are no guarantees as to the quality of the random sequence produced,
  2549. * and this should not be used for security (cryptography, passwords) or where
  2550. * money is on the line (loot-boxes, casinos). There are many random number
  2551. * libraries available with different characteristics and you should pick one
  2552. * of those to meet any serious needs.
  2553. *
  2554. * \param n the number of possible outcomes. n must be positive.
  2555. * \returns a random value in the range of [0 .. n-1].
  2556. *
  2557. * \threadsafety All calls should be made from a single thread
  2558. *
  2559. * \since This function is available since SDL 3.1.3.
  2560. *
  2561. * \sa SDL_srand
  2562. * \sa SDL_randf
  2563. */
  2564. extern SDL_DECLSPEC Sint32 SDLCALL SDL_rand(Sint32 n);
  2565. /**
  2566. * Generate a uniform pseudo-random floating point number less than 1.0
  2567. *
  2568. * If you want reproducible output, be sure to initialize with SDL_srand()
  2569. * first.
  2570. *
  2571. * There are no guarantees as to the quality of the random sequence produced,
  2572. * and this should not be used for security (cryptography, passwords) or where
  2573. * money is on the line (loot-boxes, casinos). There are many random number
  2574. * libraries available with different characteristics and you should pick one
  2575. * of those to meet any serious needs.
  2576. *
  2577. * \returns a random value in the range of [0.0, 1.0).
  2578. *
  2579. * \threadsafety All calls should be made from a single thread
  2580. *
  2581. * \since This function is available since SDL 3.1.3.
  2582. *
  2583. * \sa SDL_srand
  2584. * \sa SDL_rand
  2585. */
  2586. extern SDL_DECLSPEC float SDLCALL SDL_randf(void);
  2587. /**
  2588. * Generate 32 pseudo-random bits.
  2589. *
  2590. * You likely want to use SDL_rand() to get a psuedo-random number instead.
  2591. *
  2592. * There are no guarantees as to the quality of the random sequence produced,
  2593. * and this should not be used for security (cryptography, passwords) or where
  2594. * money is on the line (loot-boxes, casinos). There are many random number
  2595. * libraries available with different characteristics and you should pick one
  2596. * of those to meet any serious needs.
  2597. *
  2598. * \returns a random value in the range of [0-SDL_MAX_UINT32].
  2599. *
  2600. * \threadsafety All calls should be made from a single thread
  2601. *
  2602. * \since This function is available since SDL 3.1.3.
  2603. *
  2604. * \sa SDL_rand
  2605. * \sa SDL_randf
  2606. * \sa SDL_srand
  2607. */
  2608. extern SDL_DECLSPEC Uint32 SDLCALL SDL_rand_bits(void);
  2609. /**
  2610. * Generate a pseudo-random number less than n for positive n
  2611. *
  2612. * The method used is faster and of better quality than `rand() % n`. Odds are
  2613. * roughly 99.9% even for n = 1 million. Evenness is better for smaller n, and
  2614. * much worse as n gets bigger.
  2615. *
  2616. * Example: to simulate a d6 use `SDL_rand_r(state, 6) + 1` The +1 converts
  2617. * 0..5 to 1..6
  2618. *
  2619. * If you want to generate a pseudo-random number in the full range of Sint32,
  2620. * you should use: (Sint32)SDL_rand_bits_r(state)
  2621. *
  2622. * There are no guarantees as to the quality of the random sequence produced,
  2623. * and this should not be used for security (cryptography, passwords) or where
  2624. * money is on the line (loot-boxes, casinos). There are many random number
  2625. * libraries available with different characteristics and you should pick one
  2626. * of those to meet any serious needs.
  2627. *
  2628. * \param state a pointer to the current random number state, this may not be
  2629. * NULL.
  2630. * \param n the number of possible outcomes. n must be positive.
  2631. * \returns a random value in the range of [0 .. n-1].
  2632. *
  2633. * \threadsafety This function is thread-safe, as long as the state pointer
  2634. * isn't shared between threads.
  2635. *
  2636. * \since This function is available since SDL 3.1.3.
  2637. *
  2638. * \sa SDL_rand
  2639. * \sa SDL_rand_bits_r
  2640. * \sa SDL_randf_r
  2641. */
  2642. extern SDL_DECLSPEC Sint32 SDLCALL SDL_rand_r(Uint64 *state, Sint32 n);
  2643. /**
  2644. * Generate a uniform pseudo-random floating point number less than 1.0
  2645. *
  2646. * If you want reproducible output, be sure to initialize with SDL_srand()
  2647. * first.
  2648. *
  2649. * There are no guarantees as to the quality of the random sequence produced,
  2650. * and this should not be used for security (cryptography, passwords) or where
  2651. * money is on the line (loot-boxes, casinos). There are many random number
  2652. * libraries available with different characteristics and you should pick one
  2653. * of those to meet any serious needs.
  2654. *
  2655. * \param state a pointer to the current random number state, this may not be
  2656. * NULL.
  2657. * \returns a random value in the range of [0.0, 1.0).
  2658. *
  2659. * \threadsafety This function is thread-safe, as long as the state pointer
  2660. * isn't shared between threads.
  2661. *
  2662. * \since This function is available since SDL 3.1.3.
  2663. *
  2664. * \sa SDL_rand_bits_r
  2665. * \sa SDL_rand_r
  2666. * \sa SDL_randf
  2667. */
  2668. extern SDL_DECLSPEC float SDLCALL SDL_randf_r(Uint64 *state);
  2669. /**
  2670. * Generate 32 pseudo-random bits.
  2671. *
  2672. * You likely want to use SDL_rand_r() to get a psuedo-random number instead.
  2673. *
  2674. * There are no guarantees as to the quality of the random sequence produced,
  2675. * and this should not be used for security (cryptography, passwords) or where
  2676. * money is on the line (loot-boxes, casinos). There are many random number
  2677. * libraries available with different characteristics and you should pick one
  2678. * of those to meet any serious needs.
  2679. *
  2680. * \param state a pointer to the current random number state, this may not be
  2681. * NULL.
  2682. * \returns a random value in the range of [0-SDL_MAX_UINT32].
  2683. *
  2684. * \threadsafety This function is thread-safe, as long as the state pointer
  2685. * isn't shared between threads.
  2686. *
  2687. * \since This function is available since SDL 3.1.3.
  2688. *
  2689. * \sa SDL_rand_r
  2690. * \sa SDL_randf_r
  2691. */
  2692. extern SDL_DECLSPEC Uint32 SDLCALL SDL_rand_bits_r(Uint64 *state);
  2693. #ifndef SDL_PI_D
  2694. #define SDL_PI_D 3.141592653589793238462643383279502884 /**< pi (double) */
  2695. #endif
  2696. #ifndef SDL_PI_F
  2697. #define SDL_PI_F 3.141592653589793238462643383279502884F /**< pi (float) */
  2698. #endif
  2699. /**
  2700. * Compute the arc cosine of `x`.
  2701. *
  2702. * The definition of `y = acos(x)` is `x = cos(y)`.
  2703. *
  2704. * Domain: `-1 <= x <= 1`
  2705. *
  2706. * Range: `0 <= y <= Pi`
  2707. *
  2708. * This function operates on double-precision floating point values, use
  2709. * SDL_acosf for single-precision floats.
  2710. *
  2711. * This function may use a different approximation across different versions,
  2712. * platforms and configurations. i.e, it can return a different value given
  2713. * the same input on different machines or operating systems, or if SDL is
  2714. * updated.
  2715. *
  2716. * \param x floating point value.
  2717. * \returns arc cosine of `x`, in radians.
  2718. *
  2719. * \threadsafety It is safe to call this function from any thread.
  2720. *
  2721. * \since This function is available since SDL 3.1.3.
  2722. *
  2723. * \sa SDL_acosf
  2724. * \sa SDL_asin
  2725. * \sa SDL_cos
  2726. */
  2727. extern SDL_DECLSPEC double SDLCALL SDL_acos(double x);
  2728. /**
  2729. * Compute the arc cosine of `x`.
  2730. *
  2731. * The definition of `y = acos(x)` is `x = cos(y)`.
  2732. *
  2733. * Domain: `-1 <= x <= 1`
  2734. *
  2735. * Range: `0 <= y <= Pi`
  2736. *
  2737. * This function operates on single-precision floating point values, use
  2738. * SDL_acos for double-precision floats.
  2739. *
  2740. * This function may use a different approximation across different versions,
  2741. * platforms and configurations. i.e, it can return a different value given
  2742. * the same input on different machines or operating systems, or if SDL is
  2743. * updated.
  2744. *
  2745. * \param x floating point value.
  2746. * \returns arc cosine of `x`, in radians.
  2747. *
  2748. * \threadsafety It is safe to call this function from any thread.
  2749. *
  2750. * \since This function is available since SDL 3.1.3.
  2751. *
  2752. * \sa SDL_acos
  2753. * \sa SDL_asinf
  2754. * \sa SDL_cosf
  2755. */
  2756. extern SDL_DECLSPEC float SDLCALL SDL_acosf(float x);
  2757. /**
  2758. * Compute the arc sine of `x`.
  2759. *
  2760. * The definition of `y = asin(x)` is `x = sin(y)`.
  2761. *
  2762. * Domain: `-1 <= x <= 1`
  2763. *
  2764. * Range: `-Pi/2 <= y <= Pi/2`
  2765. *
  2766. * This function operates on double-precision floating point values, use
  2767. * SDL_asinf for single-precision floats.
  2768. *
  2769. * This function may use a different approximation across different versions,
  2770. * platforms and configurations. i.e, it can return a different value given
  2771. * the same input on different machines or operating systems, or if SDL is
  2772. * updated.
  2773. *
  2774. * \param x floating point value.
  2775. * \returns arc sine of `x`, in radians.
  2776. *
  2777. * \threadsafety It is safe to call this function from any thread.
  2778. *
  2779. * \since This function is available since SDL 3.1.3.
  2780. *
  2781. * \sa SDL_asinf
  2782. * \sa SDL_acos
  2783. * \sa SDL_sin
  2784. */
  2785. extern SDL_DECLSPEC double SDLCALL SDL_asin(double x);
  2786. /**
  2787. * Compute the arc sine of `x`.
  2788. *
  2789. * The definition of `y = asin(x)` is `x = sin(y)`.
  2790. *
  2791. * Domain: `-1 <= x <= 1`
  2792. *
  2793. * Range: `-Pi/2 <= y <= Pi/2`
  2794. *
  2795. * This function operates on single-precision floating point values, use
  2796. * SDL_asin for double-precision floats.
  2797. *
  2798. * This function may use a different approximation across different versions,
  2799. * platforms and configurations. i.e, it can return a different value given
  2800. * the same input on different machines or operating systems, or if SDL is
  2801. * updated.
  2802. *
  2803. * \param x floating point value.
  2804. * \returns arc sine of `x`, in radians.
  2805. *
  2806. * \threadsafety It is safe to call this function from any thread.
  2807. *
  2808. * \since This function is available since SDL 3.1.3.
  2809. *
  2810. * \sa SDL_asin
  2811. * \sa SDL_acosf
  2812. * \sa SDL_sinf
  2813. */
  2814. extern SDL_DECLSPEC float SDLCALL SDL_asinf(float x);
  2815. /**
  2816. * Compute the arc tangent of `x`.
  2817. *
  2818. * The definition of `y = atan(x)` is `x = tan(y)`.
  2819. *
  2820. * Domain: `-INF <= x <= INF`
  2821. *
  2822. * Range: `-Pi/2 <= y <= Pi/2`
  2823. *
  2824. * This function operates on double-precision floating point values, use
  2825. * SDL_atanf for single-precision floats.
  2826. *
  2827. * To calculate the arc tangent of y / x, use SDL_atan2.
  2828. *
  2829. * This function may use a different approximation across different versions,
  2830. * platforms and configurations. i.e, it can return a different value given
  2831. * the same input on different machines or operating systems, or if SDL is
  2832. * updated.
  2833. *
  2834. * \param x floating point value.
  2835. * \returns arc tangent of of `x` in radians, or 0 if `x = 0`.
  2836. *
  2837. * \threadsafety It is safe to call this function from any thread.
  2838. *
  2839. * \since This function is available since SDL 3.1.3.
  2840. *
  2841. * \sa SDL_atanf
  2842. * \sa SDL_atan2
  2843. * \sa SDL_tan
  2844. */
  2845. extern SDL_DECLSPEC double SDLCALL SDL_atan(double x);
  2846. /**
  2847. * Compute the arc tangent of `x`.
  2848. *
  2849. * The definition of `y = atan(x)` is `x = tan(y)`.
  2850. *
  2851. * Domain: `-INF <= x <= INF`
  2852. *
  2853. * Range: `-Pi/2 <= y <= Pi/2`
  2854. *
  2855. * This function operates on single-precision floating point values, use
  2856. * SDL_atan for dboule-precision floats.
  2857. *
  2858. * To calculate the arc tangent of y / x, use SDL_atan2f.
  2859. *
  2860. * This function may use a different approximation across different versions,
  2861. * platforms and configurations. i.e, it can return a different value given
  2862. * the same input on different machines or operating systems, or if SDL is
  2863. * updated.
  2864. *
  2865. * \param x floating point value.
  2866. * \returns arc tangent of of `x` in radians, or 0 if `x = 0`.
  2867. *
  2868. * \threadsafety It is safe to call this function from any thread.
  2869. *
  2870. * \since This function is available since SDL 3.1.3.
  2871. *
  2872. * \sa SDL_atan
  2873. * \sa SDL_atan2f
  2874. * \sa SDL_tanf
  2875. */
  2876. extern SDL_DECLSPEC float SDLCALL SDL_atanf(float x);
  2877. /**
  2878. * Compute the arc tangent of `y / x`, using the signs of x and y to adjust
  2879. * the result's quadrant.
  2880. *
  2881. * The definition of `z = atan2(x, y)` is `y = x tan(z)`, where the quadrant
  2882. * of z is determined based on the signs of x and y.
  2883. *
  2884. * Domain: `-INF <= x <= INF`, `-INF <= y <= INF`
  2885. *
  2886. * Range: `-Pi/2 <= y <= Pi/2`
  2887. *
  2888. * This function operates on double-precision floating point values, use
  2889. * SDL_atan2f for single-precision floats.
  2890. *
  2891. * To calculate the arc tangent of a single value, use SDL_atan.
  2892. *
  2893. * This function may use a different approximation across different versions,
  2894. * platforms and configurations. i.e, it can return a different value given
  2895. * the same input on different machines or operating systems, or if SDL is
  2896. * updated.
  2897. *
  2898. * \param y floating point value of the numerator (y coordinate).
  2899. * \param x floating point value of the denominator (x coordinate).
  2900. * \returns arc tangent of of `y / x` in radians, or, if `x = 0`, either
  2901. * `-Pi/2`, `0`, or `Pi/2`, depending on the value of `y`.
  2902. *
  2903. * \threadsafety It is safe to call this function from any thread.
  2904. *
  2905. * \since This function is available since SDL 3.1.3.
  2906. *
  2907. * \sa SDL_atan2f
  2908. * \sa SDL_atan
  2909. * \sa SDL_tan
  2910. */
  2911. extern SDL_DECLSPEC double SDLCALL SDL_atan2(double y, double x);
  2912. /**
  2913. * Compute the arc tangent of `y / x`, using the signs of x and y to adjust
  2914. * the result's quadrant.
  2915. *
  2916. * The definition of `z = atan2(x, y)` is `y = x tan(z)`, where the quadrant
  2917. * of z is determined based on the signs of x and y.
  2918. *
  2919. * Domain: `-INF <= x <= INF`, `-INF <= y <= INF`
  2920. *
  2921. * Range: `-Pi/2 <= y <= Pi/2`
  2922. *
  2923. * This function operates on single-precision floating point values, use
  2924. * SDL_atan2 for double-precision floats.
  2925. *
  2926. * To calculate the arc tangent of a single value, use SDL_atanf.
  2927. *
  2928. * This function may use a different approximation across different versions,
  2929. * platforms and configurations. i.e, it can return a different value given
  2930. * the same input on different machines or operating systems, or if SDL is
  2931. * updated.
  2932. *
  2933. * \param y floating point value of the numerator (y coordinate).
  2934. * \param x floating point value of the denominator (x coordinate).
  2935. * \returns arc tangent of of `y / x` in radians, or, if `x = 0`, either
  2936. * `-Pi/2`, `0`, or `Pi/2`, depending on the value of `y`.
  2937. *
  2938. * \threadsafety It is safe to call this function from any thread.
  2939. *
  2940. * \since This function is available since SDL 3.1.3.
  2941. *
  2942. * \sa SDL_atan2f
  2943. * \sa SDL_atan
  2944. * \sa SDL_tan
  2945. */
  2946. extern SDL_DECLSPEC float SDLCALL SDL_atan2f(float y, float x);
  2947. /**
  2948. * Compute the ceiling of `x`.
  2949. *
  2950. * The ceiling of `x` is the smallest integer `y` such that `y > x`, i.e `x`
  2951. * rounded up to the nearest integer.
  2952. *
  2953. * Domain: `-INF <= x <= INF`
  2954. *
  2955. * Range: `-INF <= y <= INF`, y integer
  2956. *
  2957. * This function operates on double-precision floating point values, use
  2958. * SDL_ceilf for single-precision floats.
  2959. *
  2960. * \param x floating point value.
  2961. * \returns the ceiling of `x`.
  2962. *
  2963. * \threadsafety It is safe to call this function from any thread.
  2964. *
  2965. * \since This function is available since SDL 3.1.3.
  2966. *
  2967. * \sa SDL_ceilf
  2968. * \sa SDL_floor
  2969. * \sa SDL_trunc
  2970. * \sa SDL_round
  2971. * \sa SDL_lround
  2972. */
  2973. extern SDL_DECLSPEC double SDLCALL SDL_ceil(double x);
  2974. /**
  2975. * Compute the ceiling of `x`.
  2976. *
  2977. * The ceiling of `x` is the smallest integer `y` such that `y > x`, i.e `x`
  2978. * rounded up to the nearest integer.
  2979. *
  2980. * Domain: `-INF <= x <= INF`
  2981. *
  2982. * Range: `-INF <= y <= INF`, y integer
  2983. *
  2984. * This function operates on single-precision floating point values, use
  2985. * SDL_ceil for double-precision floats.
  2986. *
  2987. * \param x floating point value.
  2988. * \returns the ceiling of `x`.
  2989. *
  2990. * \threadsafety It is safe to call this function from any thread.
  2991. *
  2992. * \since This function is available since SDL 3.1.3.
  2993. *
  2994. * \sa SDL_ceil
  2995. * \sa SDL_floorf
  2996. * \sa SDL_truncf
  2997. * \sa SDL_roundf
  2998. * \sa SDL_lroundf
  2999. */
  3000. extern SDL_DECLSPEC float SDLCALL SDL_ceilf(float x);
  3001. /**
  3002. * Copy the sign of one floating-point value to another.
  3003. *
  3004. * The definition of copysign is that ``copysign(x, y) = abs(x) * sign(y)``.
  3005. *
  3006. * Domain: `-INF <= x <= INF`, ``-INF <= y <= f``
  3007. *
  3008. * Range: `-INF <= z <= INF`
  3009. *
  3010. * This function operates on double-precision floating point values, use
  3011. * SDL_copysignf for single-precision floats.
  3012. *
  3013. * \param x floating point value to use as the magnitude.
  3014. * \param y floating point value to use as the sign.
  3015. * \returns the floating point value with the sign of y and the magnitude of
  3016. * x.
  3017. *
  3018. * \threadsafety It is safe to call this function from any thread.
  3019. *
  3020. * \since This function is available since SDL 3.1.3.
  3021. *
  3022. * \sa SDL_copysignf
  3023. * \sa SDL_fabs
  3024. */
  3025. extern SDL_DECLSPEC double SDLCALL SDL_copysign(double x, double y);
  3026. /**
  3027. * Copy the sign of one floating-point value to another.
  3028. *
  3029. * The definition of copysign is that ``copysign(x, y) = abs(x) * sign(y)``.
  3030. *
  3031. * Domain: `-INF <= x <= INF`, ``-INF <= y <= f``
  3032. *
  3033. * Range: `-INF <= z <= INF`
  3034. *
  3035. * This function operates on single-precision floating point values, use
  3036. * SDL_copysign for double-precision floats.
  3037. *
  3038. * \param x floating point value to use as the magnitude.
  3039. * \param y floating point value to use as the sign.
  3040. * \returns the floating point value with the sign of y and the magnitude of
  3041. * x.
  3042. *
  3043. * \threadsafety It is safe to call this function from any thread.
  3044. *
  3045. * \since This function is available since SDL 3.1.3.
  3046. *
  3047. * \sa SDL_copysignf
  3048. * \sa SDL_fabsf
  3049. */
  3050. extern SDL_DECLSPEC float SDLCALL SDL_copysignf(float x, float y);
  3051. /**
  3052. * Compute the cosine of `x`.
  3053. *
  3054. * Domain: `-INF <= x <= INF`
  3055. *
  3056. * Range: `-1 <= y <= 1`
  3057. *
  3058. * This function operates on double-precision floating point values, use
  3059. * SDL_cosf for single-precision floats.
  3060. *
  3061. * This function may use a different approximation across different versions,
  3062. * platforms and configurations. i.e, it can return a different value given
  3063. * the same input on different machines or operating systems, or if SDL is
  3064. * updated.
  3065. *
  3066. * \param x floating point value, in radians.
  3067. * \returns cosine of `x`.
  3068. *
  3069. * \threadsafety It is safe to call this function from any thread.
  3070. *
  3071. * \since This function is available since SDL 3.1.3.
  3072. *
  3073. * \sa SDL_cosf
  3074. * \sa SDL_acos
  3075. * \sa SDL_sin
  3076. */
  3077. extern SDL_DECLSPEC double SDLCALL SDL_cos(double x);
  3078. /**
  3079. * Compute the cosine of `x`.
  3080. *
  3081. * Domain: `-INF <= x <= INF`
  3082. *
  3083. * Range: `-1 <= y <= 1`
  3084. *
  3085. * This function operates on single-precision floating point values, use
  3086. * SDL_cos for double-precision floats.
  3087. *
  3088. * This function may use a different approximation across different versions,
  3089. * platforms and configurations. i.e, it can return a different value given
  3090. * the same input on different machines or operating systems, or if SDL is
  3091. * updated.
  3092. *
  3093. * \param x floating point value, in radians.
  3094. * \returns cosine of `x`.
  3095. *
  3096. * \threadsafety It is safe to call this function from any thread.
  3097. *
  3098. * \since This function is available since SDL 3.1.3.
  3099. *
  3100. * \sa SDL_cos
  3101. * \sa SDL_acosf
  3102. * \sa SDL_sinf
  3103. */
  3104. extern SDL_DECLSPEC float SDLCALL SDL_cosf(float x);
  3105. /**
  3106. * Compute the exponential of `x`.
  3107. *
  3108. * The definition of `y = exp(x)` is `y = e^x`, where `e` is the base of the
  3109. * natural logarithm. The inverse is the natural logarithm, SDL_log.
  3110. *
  3111. * Domain: `-INF <= x <= INF`
  3112. *
  3113. * Range: `0 <= y <= INF`
  3114. *
  3115. * The output will overflow if `exp(x)` is too large to be represented.
  3116. *
  3117. * This function operates on double-precision floating point values, use
  3118. * SDL_expf for single-precision floats.
  3119. *
  3120. * This function may use a different approximation across different versions,
  3121. * platforms and configurations. i.e, it can return a different value given
  3122. * the same input on different machines or operating systems, or if SDL is
  3123. * updated.
  3124. *
  3125. * \param x floating point value.
  3126. * \returns value of `e^x`.
  3127. *
  3128. * \threadsafety It is safe to call this function from any thread.
  3129. *
  3130. * \since This function is available since SDL 3.1.3.
  3131. *
  3132. * \sa SDL_expf
  3133. * \sa SDL_log
  3134. */
  3135. extern SDL_DECLSPEC double SDLCALL SDL_exp(double x);
  3136. /**
  3137. * Compute the exponential of `x`.
  3138. *
  3139. * The definition of `y = exp(x)` is `y = e^x`, where `e` is the base of the
  3140. * natural logarithm. The inverse is the natural logarithm, SDL_logf.
  3141. *
  3142. * Domain: `-INF <= x <= INF`
  3143. *
  3144. * Range: `0 <= y <= INF`
  3145. *
  3146. * The output will overflow if `exp(x)` is too large to be represented.
  3147. *
  3148. * This function operates on single-precision floating point values, use
  3149. * SDL_exp for double-precision floats.
  3150. *
  3151. * This function may use a different approximation across different versions,
  3152. * platforms and configurations. i.e, it can return a different value given
  3153. * the same input on different machines or operating systems, or if SDL is
  3154. * updated.
  3155. *
  3156. * \param x floating point value.
  3157. * \returns value of `e^x`.
  3158. *
  3159. * \threadsafety It is safe to call this function from any thread.
  3160. *
  3161. * \since This function is available since SDL 3.1.3.
  3162. *
  3163. * \sa SDL_exp
  3164. * \sa SDL_logf
  3165. */
  3166. extern SDL_DECLSPEC float SDLCALL SDL_expf(float x);
  3167. /**
  3168. * Compute the absolute value of `x`
  3169. *
  3170. * Domain: `-INF <= x <= INF`
  3171. *
  3172. * Range: `0 <= y <= INF`
  3173. *
  3174. * This function operates on double-precision floating point values, use
  3175. * SDL_copysignf for single-precision floats.
  3176. *
  3177. * \param x floating point value to use as the magnitude.
  3178. * \returns the absolute value of `x`.
  3179. *
  3180. * \threadsafety It is safe to call this function from any thread.
  3181. *
  3182. * \since This function is available since SDL 3.1.3.
  3183. *
  3184. * \sa SDL_fabsf
  3185. */
  3186. extern SDL_DECLSPEC double SDLCALL SDL_fabs(double x);
  3187. /**
  3188. * Compute the absolute value of `x`
  3189. *
  3190. * Domain: `-INF <= x <= INF`
  3191. *
  3192. * Range: `0 <= y <= INF`
  3193. *
  3194. * This function operates on single-precision floating point values, use
  3195. * SDL_copysignf for double-precision floats.
  3196. *
  3197. * \param x floating point value to use as the magnitude.
  3198. * \returns the absolute value of `x`.
  3199. *
  3200. * \threadsafety It is safe to call this function from any thread.
  3201. *
  3202. * \since This function is available since SDL 3.1.3.
  3203. *
  3204. * \sa SDL_fabs
  3205. */
  3206. extern SDL_DECLSPEC float SDLCALL SDL_fabsf(float x);
  3207. /**
  3208. * Compute the floor of `x`.
  3209. *
  3210. * The floor of `x` is the largest integer `y` such that `y > x`, i.e `x`
  3211. * rounded down to the nearest integer.
  3212. *
  3213. * Domain: `-INF <= x <= INF`
  3214. *
  3215. * Range: `-INF <= y <= INF`, y integer
  3216. *
  3217. * This function operates on double-precision floating point values, use
  3218. * SDL_floorf for single-precision floats.
  3219. *
  3220. * \param x floating point value.
  3221. * \returns the floor of `x`.
  3222. *
  3223. * \threadsafety It is safe to call this function from any thread.
  3224. *
  3225. * \since This function is available since SDL 3.1.3.
  3226. *
  3227. * \sa SDL_floorf
  3228. * \sa SDL_ceil
  3229. * \sa SDL_trunc
  3230. * \sa SDL_round
  3231. * \sa SDL_lround
  3232. */
  3233. extern SDL_DECLSPEC double SDLCALL SDL_floor(double x);
  3234. /**
  3235. * Compute the floor of `x`.
  3236. *
  3237. * The floor of `x` is the largest integer `y` such that `y > x`, i.e `x`
  3238. * rounded down to the nearest integer.
  3239. *
  3240. * Domain: `-INF <= x <= INF`
  3241. *
  3242. * Range: `-INF <= y <= INF`, y integer
  3243. *
  3244. * This function operates on single-precision floating point values, use
  3245. * SDL_floorf for double-precision floats.
  3246. *
  3247. * \param x floating point value.
  3248. * \returns the floor of `x`.
  3249. *
  3250. * \threadsafety It is safe to call this function from any thread.
  3251. *
  3252. * \since This function is available since SDL 3.1.3.
  3253. *
  3254. * \sa SDL_floor
  3255. * \sa SDL_ceilf
  3256. * \sa SDL_truncf
  3257. * \sa SDL_roundf
  3258. * \sa SDL_lroundf
  3259. */
  3260. extern SDL_DECLSPEC float SDLCALL SDL_floorf(float x);
  3261. /**
  3262. * Truncate `x` to an integer.
  3263. *
  3264. * Rounds `x` to the next closest integer to 0. This is equivalent to removing
  3265. * the fractional part of `x`, leaving only the integer part.
  3266. *
  3267. * Domain: `-INF <= x <= INF`
  3268. *
  3269. * Range: `-INF <= y <= INF`, y integer
  3270. *
  3271. * This function operates on double-precision floating point values, use
  3272. * SDL_truncf for single-precision floats.
  3273. *
  3274. * \param x floating point value.
  3275. * \returns `x` truncated to an integer.
  3276. *
  3277. * \threadsafety It is safe to call this function from any thread.
  3278. *
  3279. * \since This function is available since SDL 3.1.3.
  3280. *
  3281. * \sa SDL_truncf
  3282. * \sa SDL_fmod
  3283. * \sa SDL_ceil
  3284. * \sa SDL_floor
  3285. * \sa SDL_round
  3286. * \sa SDL_lround
  3287. */
  3288. extern SDL_DECLSPEC double SDLCALL SDL_trunc(double x);
  3289. /**
  3290. * Truncate `x` to an integer.
  3291. *
  3292. * Rounds `x` to the next closest integer to 0. This is equivalent to removing
  3293. * the fractional part of `x`, leaving only the integer part.
  3294. *
  3295. * Domain: `-INF <= x <= INF`
  3296. *
  3297. * Range: `-INF <= y <= INF`, y integer
  3298. *
  3299. * This function operates on single-precision floating point values, use
  3300. * SDL_truncf for double-precision floats.
  3301. *
  3302. * \param x floating point value.
  3303. * \returns `x` truncated to an integer.
  3304. *
  3305. * \threadsafety It is safe to call this function from any thread.
  3306. *
  3307. * \since This function is available since SDL 3.1.3.
  3308. *
  3309. * \sa SDL_trunc
  3310. * \sa SDL_fmodf
  3311. * \sa SDL_ceilf
  3312. * \sa SDL_floorf
  3313. * \sa SDL_roundf
  3314. * \sa SDL_lroundf
  3315. */
  3316. extern SDL_DECLSPEC float SDLCALL SDL_truncf(float x);
  3317. /**
  3318. * Return the floating-point remainder of `x / y`
  3319. *
  3320. * Divides `x` by `y`, and returns the remainder.
  3321. *
  3322. * Domain: `-INF <= x <= INF`, `-INF <= y <= INF`, `y != 0`
  3323. *
  3324. * Range: `-y <= z <= y`
  3325. *
  3326. * This function operates on double-precision floating point values, use
  3327. * SDL_fmodf for single-precision floats.
  3328. *
  3329. * \param x the numerator.
  3330. * \param y the denominator. Must not be 0.
  3331. * \returns the remainder of `x / y`.
  3332. *
  3333. * \threadsafety It is safe to call this function from any thread.
  3334. *
  3335. * \since This function is available since SDL 3.1.3.
  3336. *
  3337. * \sa SDL_fmodf
  3338. * \sa SDL_modf
  3339. * \sa SDL_trunc
  3340. * \sa SDL_ceil
  3341. * \sa SDL_floor
  3342. * \sa SDL_round
  3343. * \sa SDL_lround
  3344. */
  3345. extern SDL_DECLSPEC double SDLCALL SDL_fmod(double x, double y);
  3346. /**
  3347. * Return the floating-point remainder of `x / y`
  3348. *
  3349. * Divides `x` by `y`, and returns the remainder.
  3350. *
  3351. * Domain: `-INF <= x <= INF`, `-INF <= y <= INF`, `y != 0`
  3352. *
  3353. * Range: `-y <= z <= y`
  3354. *
  3355. * This function operates on single-precision floating point values, use
  3356. * SDL_fmod for single-precision floats.
  3357. *
  3358. * \param x the numerator.
  3359. * \param y the denominator. Must not be 0.
  3360. * \returns the remainder of `x / y`.
  3361. *
  3362. * \threadsafety It is safe to call this function from any thread.
  3363. *
  3364. * \since This function is available since SDL 3.1.3.
  3365. *
  3366. * \sa SDL_fmod
  3367. * \sa SDL_truncf
  3368. * \sa SDL_modff
  3369. * \sa SDL_ceilf
  3370. * \sa SDL_floorf
  3371. * \sa SDL_roundf
  3372. * \sa SDL_lroundf
  3373. */
  3374. extern SDL_DECLSPEC float SDLCALL SDL_fmodf(float x, float y);
  3375. /**
  3376. * Return whether the value is infinity.
  3377. *
  3378. * \param x double-precision floating point value.
  3379. * \returns non-zero if the value is infinity, 0 otherwise.
  3380. *
  3381. * \threadsafety It is safe to call this function from any thread.
  3382. *
  3383. * \since This function is available since SDL 3.1.3.
  3384. *
  3385. * \sa SDL_isinff
  3386. */
  3387. extern SDL_DECLSPEC int SDLCALL SDL_isinf(double x);
  3388. /**
  3389. * Return whether the value is infinity.
  3390. *
  3391. * \param x floating point value.
  3392. * \returns non-zero if the value is infinity, 0 otherwise.
  3393. *
  3394. * \threadsafety It is safe to call this function from any thread.
  3395. *
  3396. * \since This function is available since SDL 3.1.3.
  3397. *
  3398. * \sa SDL_isinf
  3399. */
  3400. extern SDL_DECLSPEC int SDLCALL SDL_isinff(float x);
  3401. /**
  3402. * Return whether the value is NaN.
  3403. *
  3404. * \param x double-precision floating point value.
  3405. * \returns non-zero if the value is NaN, 0 otherwise.
  3406. *
  3407. * \threadsafety It is safe to call this function from any thread.
  3408. *
  3409. * \since This function is available since SDL 3.1.3.
  3410. *
  3411. * \sa SDL_isnanf
  3412. */
  3413. extern SDL_DECLSPEC int SDLCALL SDL_isnan(double x);
  3414. /**
  3415. * Return whether the value is NaN.
  3416. *
  3417. * \param x floating point value.
  3418. * \returns non-zero if the value is NaN, 0 otherwise.
  3419. *
  3420. * \threadsafety It is safe to call this function from any thread.
  3421. *
  3422. * \since This function is available since SDL 3.1.3.
  3423. *
  3424. * \sa SDL_isnan
  3425. */
  3426. extern SDL_DECLSPEC int SDLCALL SDL_isnanf(float x);
  3427. /**
  3428. * Compute the natural logarithm of `x`.
  3429. *
  3430. * Domain: `0 < x <= INF`
  3431. *
  3432. * Range: `-INF <= y <= INF`
  3433. *
  3434. * It is an error for `x` to be less than or equal to 0.
  3435. *
  3436. * This function operates on double-precision floating point values, use
  3437. * SDL_logf for single-precision floats.
  3438. *
  3439. * This function may use a different approximation across different versions,
  3440. * platforms and configurations. i.e, it can return a different value given
  3441. * the same input on different machines or operating systems, or if SDL is
  3442. * updated.
  3443. *
  3444. * \param x floating point value. Must be greater than 0.
  3445. * \returns the natural logarithm of `x`.
  3446. *
  3447. * \threadsafety It is safe to call this function from any thread.
  3448. *
  3449. * \since This function is available since SDL 3.1.3.
  3450. *
  3451. * \sa SDL_logf
  3452. * \sa SDL_log10
  3453. * \sa SDL_exp
  3454. */
  3455. extern SDL_DECLSPEC double SDLCALL SDL_log(double x);
  3456. /**
  3457. * Compute the natural logarithm of `x`.
  3458. *
  3459. * Domain: `0 < x <= INF`
  3460. *
  3461. * Range: `-INF <= y <= INF`
  3462. *
  3463. * It is an error for `x` to be less than or equal to 0.
  3464. *
  3465. * This function operates on single-precision floating point values, use
  3466. * SDL_log for double-precision floats.
  3467. *
  3468. * This function may use a different approximation across different versions,
  3469. * platforms and configurations. i.e, it can return a different value given
  3470. * the same input on different machines or operating systems, or if SDL is
  3471. * updated.
  3472. *
  3473. * \param x floating point value. Must be greater than 0.
  3474. * \returns the natural logarithm of `x`.
  3475. *
  3476. * \threadsafety It is safe to call this function from any thread.
  3477. *
  3478. * \since This function is available since SDL 3.1.3.
  3479. *
  3480. * \sa SDL_log
  3481. * \sa SDL_expf
  3482. */
  3483. extern SDL_DECLSPEC float SDLCALL SDL_logf(float x);
  3484. /**
  3485. * Compute the base-10 logarithm of `x`.
  3486. *
  3487. * Domain: `0 < x <= INF`
  3488. *
  3489. * Range: `-INF <= y <= INF`
  3490. *
  3491. * It is an error for `x` to be less than or equal to 0.
  3492. *
  3493. * This function operates on double-precision floating point values, use
  3494. * SDL_log10f for single-precision floats.
  3495. *
  3496. * This function may use a different approximation across different versions,
  3497. * platforms and configurations. i.e, it can return a different value given
  3498. * the same input on different machines or operating systems, or if SDL is
  3499. * updated.
  3500. *
  3501. * \param x floating point value. Must be greater than 0.
  3502. * \returns the logarithm of `x`.
  3503. *
  3504. * \threadsafety It is safe to call this function from any thread.
  3505. *
  3506. * \since This function is available since SDL 3.1.3.
  3507. *
  3508. * \sa SDL_log10f
  3509. * \sa SDL_log
  3510. * \sa SDL_pow
  3511. */
  3512. extern SDL_DECLSPEC double SDLCALL SDL_log10(double x);
  3513. /**
  3514. * Compute the base-10 logarithm of `x`.
  3515. *
  3516. * Domain: `0 < x <= INF`
  3517. *
  3518. * Range: `-INF <= y <= INF`
  3519. *
  3520. * It is an error for `x` to be less than or equal to 0.
  3521. *
  3522. * This function operates on single-precision floating point values, use
  3523. * SDL_log10 for double-precision floats.
  3524. *
  3525. * This function may use a different approximation across different versions,
  3526. * platforms and configurations. i.e, it can return a different value given
  3527. * the same input on different machines or operating systems, or if SDL is
  3528. * updated.
  3529. *
  3530. * \param x floating point value. Must be greater than 0.
  3531. * \returns the logarithm of `x`.
  3532. *
  3533. * \threadsafety It is safe to call this function from any thread.
  3534. *
  3535. * \since This function is available since SDL 3.1.3.
  3536. *
  3537. * \sa SDL_log10
  3538. * \sa SDL_logf
  3539. * \sa SDL_powf
  3540. */
  3541. extern SDL_DECLSPEC float SDLCALL SDL_log10f(float x);
  3542. /**
  3543. * Split `x` into integer and fractional parts
  3544. *
  3545. * This function operates on double-precision floating point values, use
  3546. * SDL_modff for single-precision floats.
  3547. *
  3548. * \param x floating point value.
  3549. * \param y output pointer to store the integer part of `x`.
  3550. * \returns the fractional part of `x`.
  3551. *
  3552. * \threadsafety It is safe to call this function from any thread.
  3553. *
  3554. * \since This function is available since SDL 3.1.3.
  3555. *
  3556. * \sa SDL_modff
  3557. * \sa SDL_trunc
  3558. * \sa SDL_fmod
  3559. */
  3560. extern SDL_DECLSPEC double SDLCALL SDL_modf(double x, double *y);
  3561. /**
  3562. * Split `x` into integer and fractional parts
  3563. *
  3564. * This function operates on single-precision floating point values, use
  3565. * SDL_modf for double-precision floats.
  3566. *
  3567. * \param x floating point value.
  3568. * \param y output pointer to store the integer part of `x`.
  3569. * \returns the fractional part of `x`.
  3570. *
  3571. * \threadsafety It is safe to call this function from any thread.
  3572. *
  3573. * \since This function is available since SDL 3.1.3.
  3574. *
  3575. * \sa SDL_modf
  3576. * \sa SDL_truncf
  3577. * \sa SDL_fmodf
  3578. */
  3579. extern SDL_DECLSPEC float SDLCALL SDL_modff(float x, float *y);
  3580. /**
  3581. * Raise `x` to the power `y`
  3582. *
  3583. * Domain: `-INF <= x <= INF`, `-INF <= y <= INF`
  3584. *
  3585. * Range: `-INF <= z <= INF`
  3586. *
  3587. * If `y` is the base of the natural logarithm (e), consider using SDL_exp
  3588. * instead.
  3589. *
  3590. * This function operates on double-precision floating point values, use
  3591. * SDL_powf for single-precision floats.
  3592. *
  3593. * This function may use a different approximation across different versions,
  3594. * platforms and configurations. i.e, it can return a different value given
  3595. * the same input on different machines or operating systems, or if SDL is
  3596. * updated.
  3597. *
  3598. * \param x the base.
  3599. * \param y the exponent.
  3600. * \returns `x` raised to the power `y`.
  3601. *
  3602. * \threadsafety It is safe to call this function from any thread.
  3603. *
  3604. * \since This function is available since SDL 3.1.3.
  3605. *
  3606. * \sa SDL_powf
  3607. * \sa SDL_exp
  3608. * \sa SDL_log
  3609. */
  3610. extern SDL_DECLSPEC double SDLCALL SDL_pow(double x, double y);
  3611. /**
  3612. * Raise `x` to the power `y`
  3613. *
  3614. * Domain: `-INF <= x <= INF`, `-INF <= y <= INF`
  3615. *
  3616. * Range: `-INF <= z <= INF`
  3617. *
  3618. * If `y` is the base of the natural logarithm (e), consider using SDL_exp
  3619. * instead.
  3620. *
  3621. * This function operates on single-precision floating point values, use
  3622. * SDL_powf for double-precision floats.
  3623. *
  3624. * This function may use a different approximation across different versions,
  3625. * platforms and configurations. i.e, it can return a different value given
  3626. * the same input on different machines or operating systems, or if SDL is
  3627. * updated.
  3628. *
  3629. * \param x the base.
  3630. * \param y the exponent.
  3631. * \returns `x` raised to the power `y`.
  3632. *
  3633. * \threadsafety It is safe to call this function from any thread.
  3634. *
  3635. * \since This function is available since SDL 3.1.3.
  3636. *
  3637. * \sa SDL_pow
  3638. * \sa SDL_expf
  3639. * \sa SDL_logf
  3640. */
  3641. extern SDL_DECLSPEC float SDLCALL SDL_powf(float x, float y);
  3642. /**
  3643. * Round `x` to the nearest integer.
  3644. *
  3645. * Rounds `x` to the nearest integer. Values halfway between integers will be
  3646. * rounded away from zero.
  3647. *
  3648. * Domain: `-INF <= x <= INF`
  3649. *
  3650. * Range: `-INF <= y <= INF`, y integer
  3651. *
  3652. * This function operates on double-precision floating point values, use
  3653. * SDL_roundf for single-precision floats. To get the result as an integer
  3654. * type, use SDL_lround.
  3655. *
  3656. * \param x floating point value.
  3657. * \returns the nearest integer to `x`.
  3658. *
  3659. * \threadsafety It is safe to call this function from any thread.
  3660. *
  3661. * \since This function is available since SDL 3.1.3.
  3662. *
  3663. * \sa SDL_roundf
  3664. * \sa SDL_lround
  3665. * \sa SDL_floor
  3666. * \sa SDL_ceil
  3667. * \sa SDL_trunc
  3668. */
  3669. extern SDL_DECLSPEC double SDLCALL SDL_round(double x);
  3670. /**
  3671. * Round `x` to the nearest integer.
  3672. *
  3673. * Rounds `x` to the nearest integer. Values halfway between integers will be
  3674. * rounded away from zero.
  3675. *
  3676. * Domain: `-INF <= x <= INF`
  3677. *
  3678. * Range: `-INF <= y <= INF`, y integer
  3679. *
  3680. * This function operates on double-precision floating point values, use
  3681. * SDL_roundf for single-precision floats. To get the result as an integer
  3682. * type, use SDL_lroundf.
  3683. *
  3684. * \param x floating point value.
  3685. * \returns the nearest integer to `x`.
  3686. *
  3687. * \threadsafety It is safe to call this function from any thread.
  3688. *
  3689. * \since This function is available since SDL 3.1.3.
  3690. *
  3691. * \sa SDL_round
  3692. * \sa SDL_lroundf
  3693. * \sa SDL_floorf
  3694. * \sa SDL_ceilf
  3695. * \sa SDL_truncf
  3696. */
  3697. extern SDL_DECLSPEC float SDLCALL SDL_roundf(float x);
  3698. /**
  3699. * Round `x` to the nearest integer representable as a long
  3700. *
  3701. * Rounds `x` to the nearest integer. Values halfway between integers will be
  3702. * rounded away from zero.
  3703. *
  3704. * Domain: `-INF <= x <= INF`
  3705. *
  3706. * Range: `MIN_LONG <= y <= MAX_LONG`
  3707. *
  3708. * This function operates on double-precision floating point values, use
  3709. * SDL_lround for single-precision floats. To get the result as a
  3710. * floating-point type, use SDL_round.
  3711. *
  3712. * \param x floating point value.
  3713. * \returns the nearest integer to `x`.
  3714. *
  3715. * \threadsafety It is safe to call this function from any thread.
  3716. *
  3717. * \since This function is available since SDL 3.1.3.
  3718. *
  3719. * \sa SDL_lroundf
  3720. * \sa SDL_round
  3721. * \sa SDL_floor
  3722. * \sa SDL_ceil
  3723. * \sa SDL_trunc
  3724. */
  3725. extern SDL_DECLSPEC long SDLCALL SDL_lround(double x);
  3726. /**
  3727. * Round `x` to the nearest integer representable as a long
  3728. *
  3729. * Rounds `x` to the nearest integer. Values halfway between integers will be
  3730. * rounded away from zero.
  3731. *
  3732. * Domain: `-INF <= x <= INF`
  3733. *
  3734. * Range: `MIN_LONG <= y <= MAX_LONG`
  3735. *
  3736. * This function operates on single-precision floating point values, use
  3737. * SDL_lroundf for double-precision floats. To get the result as a
  3738. * floating-point type, use SDL_roundf,
  3739. *
  3740. * \param x floating point value.
  3741. * \returns the nearest integer to `x`.
  3742. *
  3743. * \threadsafety It is safe to call this function from any thread.
  3744. *
  3745. * \since This function is available since SDL 3.1.3.
  3746. *
  3747. * \sa SDL_lround
  3748. * \sa SDL_roundf
  3749. * \sa SDL_floorf
  3750. * \sa SDL_ceilf
  3751. * \sa SDL_truncf
  3752. */
  3753. extern SDL_DECLSPEC long SDLCALL SDL_lroundf(float x);
  3754. /**
  3755. * Scale `x` by an integer power of two.
  3756. *
  3757. * Multiplies `x` by the `n`th power of the floating point radix (always 2).
  3758. *
  3759. * Domain: `-INF <= x <= INF`, `n` integer
  3760. *
  3761. * Range: `-INF <= y <= INF`
  3762. *
  3763. * This function operates on double-precision floating point values, use
  3764. * SDL_scalbnf for single-precision floats.
  3765. *
  3766. * \param x floating point value to be scaled.
  3767. * \param n integer exponent.
  3768. * \returns `x * 2^n`.
  3769. *
  3770. * \threadsafety It is safe to call this function from any thread.
  3771. *
  3772. * \since This function is available since SDL 3.1.3.
  3773. *
  3774. * \sa SDL_scalbnf
  3775. * \sa SDL_pow
  3776. */
  3777. extern SDL_DECLSPEC double SDLCALL SDL_scalbn(double x, int n);
  3778. /**
  3779. * Scale `x` by an integer power of two.
  3780. *
  3781. * Multiplies `x` by the `n`th power of the floating point radix (always 2).
  3782. *
  3783. * Domain: `-INF <= x <= INF`, `n` integer
  3784. *
  3785. * Range: `-INF <= y <= INF`
  3786. *
  3787. * This function operates on single-precision floating point values, use
  3788. * SDL_scalbn for double-precision floats.
  3789. *
  3790. * \param x floating point value to be scaled.
  3791. * \param n integer exponent.
  3792. * \returns `x * 2^n`.
  3793. *
  3794. * \threadsafety It is safe to call this function from any thread.
  3795. *
  3796. * \since This function is available since SDL 3.1.3.
  3797. *
  3798. * \sa SDL_scalbn
  3799. * \sa SDL_powf
  3800. */
  3801. extern SDL_DECLSPEC float SDLCALL SDL_scalbnf(float x, int n);
  3802. /**
  3803. * Compute the sine of `x`.
  3804. *
  3805. * Domain: `-INF <= x <= INF`
  3806. *
  3807. * Range: `-1 <= y <= 1`
  3808. *
  3809. * This function operates on double-precision floating point values, use
  3810. * SDL_sinf for single-precision floats.
  3811. *
  3812. * This function may use a different approximation across different versions,
  3813. * platforms and configurations. i.e, it can return a different value given
  3814. * the same input on different machines or operating systems, or if SDL is
  3815. * updated.
  3816. *
  3817. * \param x floating point value, in radians.
  3818. * \returns sine of `x`.
  3819. *
  3820. * \threadsafety It is safe to call this function from any thread.
  3821. *
  3822. * \since This function is available since SDL 3.1.3.
  3823. *
  3824. * \sa SDL_sinf
  3825. * \sa SDL_asin
  3826. * \sa SDL_cos
  3827. */
  3828. extern SDL_DECLSPEC double SDLCALL SDL_sin(double x);
  3829. /**
  3830. * Compute the sine of `x`.
  3831. *
  3832. * Domain: `-INF <= x <= INF`
  3833. *
  3834. * Range: `-1 <= y <= 1`
  3835. *
  3836. * This function operates on single-precision floating point values, use
  3837. * SDL_sinf for double-precision floats.
  3838. *
  3839. * This function may use a different approximation across different versions,
  3840. * platforms and configurations. i.e, it can return a different value given
  3841. * the same input on different machines or operating systems, or if SDL is
  3842. * updated.
  3843. *
  3844. * \param x floating point value, in radians.
  3845. * \returns sine of `x`.
  3846. *
  3847. * \threadsafety It is safe to call this function from any thread.
  3848. *
  3849. * \since This function is available since SDL 3.1.3.
  3850. *
  3851. * \sa SDL_sin
  3852. * \sa SDL_asinf
  3853. * \sa SDL_cosf
  3854. */
  3855. extern SDL_DECLSPEC float SDLCALL SDL_sinf(float x);
  3856. /**
  3857. * Compute the square root of `x`.
  3858. *
  3859. * Domain: `0 <= x <= INF`
  3860. *
  3861. * Range: `0 <= y <= INF`
  3862. *
  3863. * This function operates on double-precision floating point values, use
  3864. * SDL_sqrtf for single-precision floats.
  3865. *
  3866. * This function may use a different approximation across different versions,
  3867. * platforms and configurations. i.e, it can return a different value given
  3868. * the same input on different machines or operating systems, or if SDL is
  3869. * updated.
  3870. *
  3871. * \param x floating point value. Must be greater than or equal to 0.
  3872. * \returns square root of `x`.
  3873. *
  3874. * \threadsafety It is safe to call this function from any thread.
  3875. *
  3876. * \since This function is available since SDL 3.1.3.
  3877. *
  3878. * \sa SDL_sqrtf
  3879. */
  3880. extern SDL_DECLSPEC double SDLCALL SDL_sqrt(double x);
  3881. /**
  3882. * Compute the square root of `x`.
  3883. *
  3884. * Domain: `0 <= x <= INF`
  3885. *
  3886. * Range: `0 <= y <= INF`
  3887. *
  3888. * This function operates on single-precision floating point values, use
  3889. * SDL_sqrt for double-precision floats.
  3890. *
  3891. * This function may use a different approximation across different versions,
  3892. * platforms and configurations. i.e, it can return a different value given
  3893. * the same input on different machines or operating systems, or if SDL is
  3894. * updated.
  3895. *
  3896. * \param x floating point value. Must be greater than or equal to 0.
  3897. * \returns square root of `x`.
  3898. *
  3899. * \threadsafety It is safe to call this function from any thread.
  3900. *
  3901. * \since This function is available since SDL 3.1.3.
  3902. *
  3903. * \sa SDL_sqrt
  3904. */
  3905. extern SDL_DECLSPEC float SDLCALL SDL_sqrtf(float x);
  3906. /**
  3907. * Compute the tangent of `x`.
  3908. *
  3909. * Domain: `-INF <= x <= INF`
  3910. *
  3911. * Range: `-INF <= y <= INF`
  3912. *
  3913. * This function operates on double-precision floating point values, use
  3914. * SDL_tanf for single-precision floats.
  3915. *
  3916. * This function may use a different approximation across different versions,
  3917. * platforms and configurations. i.e, it can return a different value given
  3918. * the same input on different machines or operating systems, or if SDL is
  3919. * updated.
  3920. *
  3921. * \param x floating point value, in radians.
  3922. * \returns tangent of `x`.
  3923. *
  3924. * \threadsafety It is safe to call this function from any thread.
  3925. *
  3926. * \since This function is available since SDL 3.1.3.
  3927. *
  3928. * \sa SDL_tanf
  3929. * \sa SDL_sin
  3930. * \sa SDL_cos
  3931. * \sa SDL_atan
  3932. * \sa SDL_atan2
  3933. */
  3934. extern SDL_DECLSPEC double SDLCALL SDL_tan(double x);
  3935. /**
  3936. * Compute the tangent of `x`.
  3937. *
  3938. * Domain: `-INF <= x <= INF`
  3939. *
  3940. * Range: `-INF <= y <= INF`
  3941. *
  3942. * This function operates on single-precision floating point values, use
  3943. * SDL_tanf for double-precision floats.
  3944. *
  3945. * This function may use a different approximation across different versions,
  3946. * platforms and configurations. i.e, it can return a different value given
  3947. * the same input on different machines or operating systems, or if SDL is
  3948. * updated.
  3949. *
  3950. * \param x floating point value, in radians.
  3951. * \returns tangent of `x`.
  3952. *
  3953. * \threadsafety It is safe to call this function from any thread.
  3954. *
  3955. * \since This function is available since SDL 3.1.3.
  3956. *
  3957. * \sa SDL_tan
  3958. * \sa SDL_sinf
  3959. * \sa SDL_cosf
  3960. * \sa SDL_atanf
  3961. * \sa SDL_atan2f
  3962. */
  3963. extern SDL_DECLSPEC float SDLCALL SDL_tanf(float x);
  3964. /* The SDL implementation of iconv() returns these error codes */
  3965. #define SDL_ICONV_ERROR (size_t)-1
  3966. #define SDL_ICONV_E2BIG (size_t)-2
  3967. #define SDL_ICONV_EILSEQ (size_t)-3
  3968. #define SDL_ICONV_EINVAL (size_t)-4
  3969. typedef struct SDL_iconv_data_t *SDL_iconv_t;
  3970. /**
  3971. * This function allocates a context for the specified character set
  3972. * conversion.
  3973. *
  3974. * \param tocode The target character encoding, must not be NULL.
  3975. * \param fromcode The source character encoding, must not be NULL.
  3976. * \returns a handle that must be freed with SDL_iconv_close, or
  3977. * SDL_ICONV_ERROR on failure.
  3978. *
  3979. * \since This function is available since SDL 3.1.3.
  3980. *
  3981. * \sa SDL_iconv
  3982. * \sa SDL_iconv_close
  3983. * \sa SDL_iconv_string
  3984. */
  3985. extern SDL_DECLSPEC SDL_iconv_t SDLCALL SDL_iconv_open(const char *tocode,
  3986. const char *fromcode);
  3987. /**
  3988. * This function frees a context used for character set conversion.
  3989. *
  3990. * \param cd The character set conversion handle.
  3991. * \returns 0 on success, or -1 on failure.
  3992. *
  3993. * \since This function is available since SDL 3.1.3.
  3994. *
  3995. * \sa SDL_iconv
  3996. * \sa SDL_iconv_open
  3997. * \sa SDL_iconv_string
  3998. */
  3999. extern SDL_DECLSPEC int SDLCALL SDL_iconv_close(SDL_iconv_t cd);
  4000. /**
  4001. * This function converts text between encodings, reading from and writing to
  4002. * a buffer.
  4003. *
  4004. * It returns the number of succesful conversions.
  4005. *
  4006. * \param cd The character set conversion context, created in
  4007. * SDL_iconv_open().
  4008. * \param inbuf Address of variable that points to the first character of the
  4009. * input sequence.
  4010. * \param inbytesleft The number of bytes in the input buffer.
  4011. * \param outbuf Address of variable that points to the output buffer.
  4012. * \param outbytesleft The number of bytes in the output buffer.
  4013. * \returns the number of conversions on success, else SDL_ICONV_E2BIG is
  4014. * returned when the output buffer is too small, or SDL_ICONV_EILSEQ
  4015. * is returned when an invalid input sequence is encountered, or
  4016. * SDL_ICONV_EINVAL is returned when an incomplete input sequence is
  4017. * encountered.
  4018. *
  4019. * On exit:
  4020. *
  4021. * - inbuf will point to the beginning of the next multibyte
  4022. * sequence. On error, this is the location of the problematic
  4023. * input sequence. On success, this is the end of the input
  4024. * sequence. - inbytesleft will be set to the number of bytes left
  4025. * to convert, which will be 0 on success. - outbuf will point to
  4026. * the location where to store the next output byte. - outbytesleft
  4027. * will be set to the number of bytes left in the output buffer.
  4028. *
  4029. * \since This function is available since SDL 3.1.3.
  4030. *
  4031. * \sa SDL_iconv_open
  4032. * \sa SDL_iconv_close
  4033. * \sa SDL_iconv_string
  4034. */
  4035. extern SDL_DECLSPEC size_t SDLCALL SDL_iconv(SDL_iconv_t cd, const char **inbuf,
  4036. size_t *inbytesleft, char **outbuf,
  4037. size_t *outbytesleft);
  4038. /**
  4039. * Helper function to convert a string's encoding in one call.
  4040. *
  4041. * This function converts a buffer or string between encodings in one pass.
  4042. *
  4043. * The string does not need to be NULL-terminated; this function operates on
  4044. * the number of bytes specified in `inbytesleft` whether there is a NULL
  4045. * character anywhere in the buffer.
  4046. *
  4047. * The returned string is owned by the caller, and should be passed to
  4048. * SDL_free when no longer needed.
  4049. *
  4050. * \param tocode the character encoding of the output string. Examples are
  4051. * "UTF-8", "UCS-4", etc.
  4052. * \param fromcode the character encoding of data in `inbuf`.
  4053. * \param inbuf the string to convert to a different encoding.
  4054. * \param inbytesleft the size of the input string _in bytes_.
  4055. * \returns a new string, converted to the new encoding, or NULL on error.
  4056. *
  4057. * \since This function is available since SDL 3.1.3.
  4058. *
  4059. * \sa SDL_iconv_open
  4060. * \sa SDL_iconv_close
  4061. * \sa SDL_iconv
  4062. */
  4063. extern SDL_DECLSPEC char * SDLCALL SDL_iconv_string(const char *tocode,
  4064. const char *fromcode,
  4065. const char *inbuf,
  4066. size_t inbytesleft);
  4067. /* Some helper macros for common cases... */
  4068. #define SDL_iconv_utf8_locale(S) SDL_iconv_string("", "UTF-8", S, SDL_strlen(S)+1)
  4069. #define SDL_iconv_utf8_ucs2(S) (Uint16 *)SDL_iconv_string("UCS-2", "UTF-8", S, SDL_strlen(S)+1)
  4070. #define SDL_iconv_utf8_ucs4(S) (Uint32 *)SDL_iconv_string("UCS-4", "UTF-8", S, SDL_strlen(S)+1)
  4071. #define SDL_iconv_wchar_utf8(S) SDL_iconv_string("UTF-8", "WCHAR_T", (char *)S, (SDL_wcslen(S)+1)*sizeof(wchar_t))
  4072. /* force builds using Clang's static analysis tools to use literal C runtime
  4073. here, since there are possibly tests that are ineffective otherwise. */
  4074. #if defined(__clang_analyzer__) && !defined(SDL_DISABLE_ANALYZE_MACROS)
  4075. /* The analyzer knows about strlcpy even when the system doesn't provide it */
  4076. #if !defined(HAVE_STRLCPY) && !defined(strlcpy)
  4077. size_t strlcpy(char *dst, const char *src, size_t size);
  4078. #endif
  4079. /* The analyzer knows about strlcat even when the system doesn't provide it */
  4080. #if !defined(HAVE_STRLCAT) && !defined(strlcat)
  4081. size_t strlcat(char *dst, const char *src, size_t size);
  4082. #endif
  4083. #if !defined(HAVE_WCSLCPY) && !defined(wcslcpy)
  4084. size_t wcslcpy(wchar_t *dst, const wchar_t *src, size_t size);
  4085. #endif
  4086. #if !defined(HAVE_WCSLCAT) && !defined(wcslcat)
  4087. size_t wcslcat(wchar_t *dst, const wchar_t *src, size_t size);
  4088. #endif
  4089. /* strdup is not ANSI but POSIX, and its prototype might be hidden... */
  4090. char *strdup(const char *str);
  4091. /* Starting LLVM 16, the analyser errors out if these functions do not have
  4092. their prototype defined (clang-diagnostic-implicit-function-declaration) */
  4093. #include <stdio.h>
  4094. #include <stdlib.h>
  4095. #include <strings.h>
  4096. #define SDL_malloc malloc
  4097. #define SDL_calloc calloc
  4098. #define SDL_realloc realloc
  4099. #define SDL_free free
  4100. #ifndef SDL_memcpy
  4101. #define SDL_memcpy memcpy
  4102. #endif
  4103. #ifndef SDL_memmove
  4104. #define SDL_memmove memmove
  4105. #endif
  4106. #ifndef SDL_memset
  4107. #define SDL_memset memset
  4108. #endif
  4109. #define SDL_memcmp memcmp
  4110. #define SDL_strlcpy strlcpy
  4111. #define SDL_strlcat strlcat
  4112. #define SDL_strlen strlen
  4113. #define SDL_wcslen wcslen
  4114. #define SDL_wcslcpy wcslcpy
  4115. #define SDL_wcslcat wcslcat
  4116. #define SDL_strdup strdup
  4117. #define SDL_wcsdup wcsdup
  4118. #define SDL_strchr strchr
  4119. #define SDL_strrchr strrchr
  4120. #define SDL_strstr strstr
  4121. #define SDL_wcsstr wcsstr
  4122. #define SDL_strtok_r strtok_r
  4123. #define SDL_strcmp strcmp
  4124. #define SDL_wcscmp wcscmp
  4125. #define SDL_strncmp strncmp
  4126. #define SDL_wcsncmp wcsncmp
  4127. #define SDL_strcasecmp strcasecmp
  4128. #define SDL_strncasecmp strncasecmp
  4129. #define SDL_strpbrk strpbrk
  4130. #define SDL_sscanf sscanf
  4131. #define SDL_vsscanf vsscanf
  4132. #define SDL_snprintf snprintf
  4133. #define SDL_vsnprintf vsnprintf
  4134. #endif
  4135. /**
  4136. * Multiply two integers, checking for overflow.
  4137. *
  4138. * If `a * b` would overflow, return false.
  4139. *
  4140. * Otherwise store `a * b` via ret and return true.
  4141. *
  4142. * \param a the multiplicand.
  4143. * \param b the multiplier.
  4144. * \param ret on non-overflow output, stores the multiplication result, may
  4145. * not be NULL.
  4146. * \returns false on overflow, true if result is multiplied without overflow.
  4147. *
  4148. * \threadsafety It is safe to call this function from any thread.
  4149. *
  4150. * \since This function is available since SDL 3.1.3.
  4151. */
  4152. SDL_FORCE_INLINE bool SDL_size_mul_check_overflow(size_t a, size_t b, size_t *ret)
  4153. {
  4154. if (a != 0 && b > SDL_SIZE_MAX / a) {
  4155. return false;
  4156. }
  4157. *ret = a * b;
  4158. return true;
  4159. }
  4160. #ifndef SDL_WIKI_DOCUMENTATION_SECTION
  4161. #if SDL_HAS_BUILTIN(__builtin_mul_overflow)
  4162. /* This needs to be wrapped in an inline rather than being a direct #define,
  4163. * because __builtin_mul_overflow() is type-generic, but we want to be
  4164. * consistent about interpreting a and b as size_t. */
  4165. SDL_FORCE_INLINE bool SDL_size_mul_check_overflow_builtin(size_t a, size_t b, size_t *ret)
  4166. {
  4167. return (__builtin_mul_overflow(a, b, ret) == 0);
  4168. }
  4169. #define SDL_size_mul_check_overflow(a, b, ret) SDL_size_mul_check_overflow_builtin(a, b, ret)
  4170. #endif
  4171. #endif
  4172. /**
  4173. * Add two integers, checking for overflow.
  4174. *
  4175. * If `a + b` would overflow, return -1.
  4176. *
  4177. * Otherwise store `a + b` via ret and return 0.
  4178. *
  4179. * \param a the first addend.
  4180. * \param b the second addend.
  4181. * \param ret on non-overflow output, stores the addition result, may not be
  4182. * NULL.
  4183. * \returns false on overflow, true if result is added without overflow.
  4184. *
  4185. * \threadsafety It is safe to call this function from any thread.
  4186. *
  4187. * \since This function is available since SDL 3.1.3.
  4188. */
  4189. SDL_FORCE_INLINE bool SDL_size_add_check_overflow(size_t a, size_t b, size_t *ret)
  4190. {
  4191. if (b > SDL_SIZE_MAX - a) {
  4192. return false;
  4193. }
  4194. *ret = a + b;
  4195. return true;
  4196. }
  4197. #ifndef SDL_WIKI_DOCUMENTATION_SECTION
  4198. #if SDL_HAS_BUILTIN(__builtin_add_overflow)
  4199. /* This needs to be wrapped in an inline rather than being a direct #define,
  4200. * the same as the call to __builtin_mul_overflow() above. */
  4201. SDL_FORCE_INLINE bool SDL_size_add_check_overflow_builtin(size_t a, size_t b, size_t *ret)
  4202. {
  4203. return (__builtin_add_overflow(a, b, ret) == 0);
  4204. }
  4205. #define SDL_size_add_check_overflow(a, b, ret) SDL_size_add_check_overflow_builtin(a, b, ret)
  4206. #endif
  4207. #endif
  4208. /* This is a generic function pointer which should be cast to the type you expect */
  4209. #ifdef SDL_WIKI_DOCUMENTATION_SECTION
  4210. /**
  4211. * A generic function pointer.
  4212. *
  4213. * In theory, generic function pointers should use this, instead of `void *`,
  4214. * since some platforms could treat code addresses differently than data
  4215. * addresses. Although in current times no popular platforms make this
  4216. * distinction, it is more correct and portable to use the correct type for a
  4217. * generic pointer.
  4218. *
  4219. * If for some reason you need to force this typedef to be an actual `void *`,
  4220. * perhaps to work around a compiler or existing code, you can define
  4221. * `SDL_FUNCTION_POINTER_IS_VOID_POINTER` before including any SDL headers.
  4222. *
  4223. * \since This datatype is available since SDL 3.1.3.
  4224. */
  4225. typedef void (*SDL_FunctionPointer)(void);
  4226. #elif defined(SDL_FUNCTION_POINTER_IS_VOID_POINTER)
  4227. typedef void *SDL_FunctionPointer;
  4228. #else
  4229. typedef void (*SDL_FunctionPointer)(void);
  4230. #endif
  4231. /* Ends C function definitions when using C++ */
  4232. #ifdef __cplusplus
  4233. }
  4234. #endif
  4235. #include <SDL3/SDL_close_code.h>
  4236. #endif /* SDL_stdinc_h_ */