SDL_stdinc.h 140 KB

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