SDL_stdinc.h 140 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369
  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
  1226. * found.
  1227. *
  1228. * \since This function is available since SDL 3.0.0.
  1229. *
  1230. * \sa SDL_bsearch_r
  1231. * \sa SDL_qsort
  1232. */
  1233. extern SDL_DECLSPEC void * SDLCALL SDL_bsearch(const void *key, const void *base, size_t nmemb, size_t size, SDL_CompareCallback compare);
  1234. /**
  1235. * A callback used with SDL sorting and binary search functions.
  1236. *
  1237. * \param userdata the `userdata` pointer passed to the sort function.
  1238. * \param a a pointer to the first element being compared.
  1239. * \param b a pointer to the second element being compared.
  1240. * \returns -1 if `a` should be sorted before `b`, 1 if `b` should be sorted
  1241. * before `a`, 0 if they are equal. If two elements are equal, their
  1242. * order in the sorted array is undefined.
  1243. *
  1244. * \since This callback is available since SDL 3.0.0.
  1245. *
  1246. * \sa SDL_qsort_r
  1247. * \sa SDL_bsearch_r
  1248. */
  1249. typedef int (SDLCALL *SDL_CompareCallback_r)(void *userdata, const void *a, const void *b);
  1250. /**
  1251. * Sort an array, passing a userdata pointer to the compare function.
  1252. *
  1253. * For example:
  1254. *
  1255. * ```c
  1256. * typedef enum {
  1257. * sort_increasing,
  1258. * sort_decreasing,
  1259. * } sort_method;
  1260. *
  1261. * typedef struct {
  1262. * int key;
  1263. * const char *string;
  1264. * } data;
  1265. *
  1266. * int SDLCALL compare(const void *userdata, const void *a, const void *b)
  1267. * {
  1268. * sort_method method = (sort_method)(uintptr_t)userdata;
  1269. * const data *A = (const data *)a;
  1270. * const data *B = (const data *)b;
  1271. *
  1272. * if (A->n < B->n) {
  1273. * return (method == sort_increasing) ? -1 : 1;
  1274. * } else if (B->n < A->n) {
  1275. * return (method == sort_increasing) ? 1 : -1;
  1276. * } else {
  1277. * return 0;
  1278. * }
  1279. * }
  1280. *
  1281. * data values[] = {
  1282. * { 3, "third" }, { 1, "first" }, { 2, "second" }
  1283. * };
  1284. *
  1285. * SDL_qsort_r(values, SDL_arraysize(values), sizeof(values[0]), compare, (const void *)(uintptr_t)sort_increasing);
  1286. * ```
  1287. *
  1288. * \param base a pointer to the start of the array.
  1289. * \param nmemb the number of elements in the array.
  1290. * \param size the size of the elements in the array.
  1291. * \param compare a function used to compare elements in the array.
  1292. * \param userdata a pointer to pass to the compare function.
  1293. *
  1294. * \since This function is available since SDL 3.0.0.
  1295. *
  1296. * \sa SDL_bsearch_r
  1297. * \sa SDL_qsort
  1298. */
  1299. extern SDL_DECLSPEC void SDLCALL SDL_qsort_r(void *base, size_t nmemb, size_t size, SDL_CompareCallback_r compare, void *userdata);
  1300. /**
  1301. * Perform a binary search on a previously sorted array, passing a userdata
  1302. * pointer to the compare function.
  1303. *
  1304. * For example:
  1305. *
  1306. * ```c
  1307. * typedef enum {
  1308. * sort_increasing,
  1309. * sort_decreasing,
  1310. * } sort_method;
  1311. *
  1312. * typedef struct {
  1313. * int key;
  1314. * const char *string;
  1315. * } data;
  1316. *
  1317. * int SDLCALL compare(const void *userdata, const void *a, const void *b)
  1318. * {
  1319. * sort_method method = (sort_method)(uintptr_t)userdata;
  1320. * const data *A = (const data *)a;
  1321. * const data *B = (const data *)b;
  1322. *
  1323. * if (A->n < B->n) {
  1324. * return (method == sort_increasing) ? -1 : 1;
  1325. * } else if (B->n < A->n) {
  1326. * return (method == sort_increasing) ? 1 : -1;
  1327. * } else {
  1328. * return 0;
  1329. * }
  1330. * }
  1331. *
  1332. * data values[] = {
  1333. * { 1, "first" }, { 2, "second" }, { 3, "third" }
  1334. * };
  1335. * data key = { 2, NULL };
  1336. *
  1337. * data *result = SDL_bsearch_r(&key, values, SDL_arraysize(values), sizeof(values[0]), compare, (const void *)(uintptr_t)sort_increasing);
  1338. * ```
  1339. *
  1340. * \param key a pointer to a key equal to the element being searched for.
  1341. * \param base a pointer to the start of the array.
  1342. * \param nmemb the number of elements in the array.
  1343. * \param size the size of the elements in the array.
  1344. * \param compare a function used to compare elements in the array.
  1345. * \param userdata a pointer to pass to the compare function.
  1346. * \returns a pointer to the matching element in the array, or NULL if not
  1347. * found.
  1348. *
  1349. * \since This function is available since SDL 3.0.0.
  1350. *
  1351. * \sa SDL_bsearch
  1352. * \sa SDL_qsort_r
  1353. */
  1354. 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);
  1355. extern SDL_DECLSPEC int SDLCALL SDL_abs(int x);
  1356. /* NOTE: these double-evaluate their arguments, so you should never have side effects in the parameters */
  1357. #define SDL_min(x, y) (((x) < (y)) ? (x) : (y))
  1358. #define SDL_max(x, y) (((x) > (y)) ? (x) : (y))
  1359. #define SDL_clamp(x, a, b) (((x) < (a)) ? (a) : (((x) > (b)) ? (b) : (x)))
  1360. /**
  1361. * Query if a character is alphabetic (a letter).
  1362. *
  1363. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  1364. * for English 'a-z' and 'A-Z' as true.
  1365. *
  1366. * \param x character value to check.
  1367. * \returns non-zero if x falls within the character class, zero otherwise.
  1368. *
  1369. * \threadsafety It is safe to call this function from any thread.
  1370. *
  1371. * \since This function is available since SDL 3.0.0.
  1372. */
  1373. extern SDL_DECLSPEC int SDLCALL SDL_isalpha(int x);
  1374. /**
  1375. * Query if a character is alphabetic (a letter) or a number.
  1376. *
  1377. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  1378. * for English 'a-z', 'A-Z', and '0-9' as true.
  1379. *
  1380. * \param x character value to check.
  1381. * \returns non-zero if x falls within the character class, zero otherwise.
  1382. *
  1383. * \threadsafety It is safe to call this function from any thread.
  1384. *
  1385. * \since This function is available since SDL 3.0.0.
  1386. */
  1387. extern SDL_DECLSPEC int SDLCALL SDL_isalnum(int x);
  1388. /**
  1389. * Report if a character is blank (a space or tab).
  1390. *
  1391. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  1392. * 0x20 (space) or 0x9 (tab) as true.
  1393. *
  1394. * \param x character value to check.
  1395. * \returns non-zero if x falls within the character class, zero otherwise.
  1396. *
  1397. * \threadsafety It is safe to call this function from any thread.
  1398. *
  1399. * \since This function is available since SDL 3.0.0.
  1400. */
  1401. extern SDL_DECLSPEC int SDLCALL SDL_isblank(int x);
  1402. /**
  1403. * Report if a character is a control character.
  1404. *
  1405. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  1406. * 0 through 0x1F, and 0x7F, as true.
  1407. *
  1408. * \param x character value to check.
  1409. * \returns non-zero if x falls within the character class, zero otherwise.
  1410. *
  1411. * \threadsafety It is safe to call this function from any thread.
  1412. *
  1413. * \since This function is available since SDL 3.0.0.
  1414. */
  1415. extern SDL_DECLSPEC int SDLCALL SDL_iscntrl(int x);
  1416. /**
  1417. * Report if a character is a numeric digit.
  1418. *
  1419. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  1420. * '0' (0x30) through '9' (0x39), as true.
  1421. *
  1422. * \param x character value to check.
  1423. * \returns non-zero if x falls within the character class, zero otherwise.
  1424. *
  1425. * \threadsafety It is safe to call this function from any thread.
  1426. *
  1427. * \since This function is available since SDL 3.0.0.
  1428. */
  1429. extern SDL_DECLSPEC int SDLCALL SDL_isdigit(int x);
  1430. /**
  1431. * Report if a character is a hexadecimal digit.
  1432. *
  1433. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  1434. * 'A' through 'F', 'a' through 'f', and '0' through '9', as true.
  1435. *
  1436. * \param x character value to check.
  1437. * \returns non-zero if x falls within the character class, zero otherwise.
  1438. *
  1439. * \threadsafety It is safe to call this function from any thread.
  1440. *
  1441. * \since This function is available since SDL 3.0.0.
  1442. */
  1443. extern SDL_DECLSPEC int SDLCALL SDL_isxdigit(int x);
  1444. /**
  1445. * Report if a character is a punctuation mark.
  1446. *
  1447. * **WARNING**: Regardless of system locale, this is equivalent to
  1448. * `((SDL_isgraph(x)) && (!SDL_isalnum(x)))`.
  1449. *
  1450. * \param x character value to check.
  1451. * \returns non-zero if x falls within the character class, zero otherwise.
  1452. *
  1453. * \threadsafety It is safe to call this function from any thread.
  1454. *
  1455. * \since This function is available since SDL 3.0.0.
  1456. *
  1457. * \sa SDL_isgraph
  1458. * \sa SDL_isalnum
  1459. */
  1460. extern SDL_DECLSPEC int SDLCALL SDL_ispunct(int x);
  1461. /**
  1462. * Report if a character is whitespace.
  1463. *
  1464. * **WARNING**: Regardless of system locale, this will only treat the
  1465. * following ASCII values as true:
  1466. *
  1467. * - space (0x20)
  1468. * - tab (0x09)
  1469. * - newline (0x0A)
  1470. * - vertical tab (0x0B)
  1471. * - form feed (0x0C)
  1472. * - return (0x0D)
  1473. *
  1474. * \param x character value to check.
  1475. * \returns non-zero if x falls within the character class, zero otherwise.
  1476. *
  1477. * \threadsafety It is safe to call this function from any thread.
  1478. *
  1479. * \since This function is available since SDL 3.0.0.
  1480. */
  1481. extern SDL_DECLSPEC int SDLCALL SDL_isspace(int x);
  1482. /**
  1483. * Report if a character is upper case.
  1484. *
  1485. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  1486. * 'A' through 'Z' as true.
  1487. *
  1488. * \param x character value to check.
  1489. * \returns non-zero if x falls within the character class, zero otherwise.
  1490. *
  1491. * \threadsafety It is safe to call this function from any thread.
  1492. *
  1493. * \since This function is available since SDL 3.0.0.
  1494. */
  1495. extern SDL_DECLSPEC int SDLCALL SDL_isupper(int x);
  1496. /**
  1497. * Report if a character is lower case.
  1498. *
  1499. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  1500. * 'a' through 'z' as true.
  1501. *
  1502. * \param x character value to check.
  1503. * \returns non-zero if x falls within the character class, zero otherwise.
  1504. *
  1505. * \threadsafety It is safe to call this function from any thread.
  1506. *
  1507. * \since This function is available since SDL 3.0.0.
  1508. */
  1509. extern SDL_DECLSPEC int SDLCALL SDL_islower(int x);
  1510. /**
  1511. * Report if a character is "printable".
  1512. *
  1513. * Be advised that "printable" has a definition that goes back to text
  1514. * terminals from the dawn of computing, making this a sort of special case
  1515. * function that is not suitable for Unicode (or most any) text management.
  1516. *
  1517. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  1518. * ' ' (0x20) through '~' (0x7E) as true.
  1519. *
  1520. * \param x character value to check.
  1521. * \returns non-zero if x falls within the character class, zero otherwise.
  1522. *
  1523. * \threadsafety It is safe to call this function from any thread.
  1524. *
  1525. * \since This function is available since SDL 3.0.0.
  1526. */
  1527. extern SDL_DECLSPEC int SDLCALL SDL_isprint(int x);
  1528. /**
  1529. * Report if a character is any "printable" except space.
  1530. *
  1531. * Be advised that "printable" has a definition that goes back to text
  1532. * terminals from the dawn of computing, making this a sort of special case
  1533. * function that is not suitable for Unicode (or most any) text management.
  1534. *
  1535. * **WARNING**: Regardless of system locale, this is equivalent to
  1536. * `(SDL_isprint(x)) && ((x) != ' ')`.
  1537. *
  1538. * \param x character value to check.
  1539. * \returns non-zero if x falls within the character class, zero otherwise.
  1540. *
  1541. * \threadsafety It is safe to call this function from any thread.
  1542. *
  1543. * \since This function is available since SDL 3.0.0.
  1544. *
  1545. * \sa SDL_isprint
  1546. */
  1547. extern SDL_DECLSPEC int SDLCALL SDL_isgraph(int x);
  1548. /**
  1549. * Convert low-ASCII English letters to uppercase.
  1550. *
  1551. * **WARNING**: Regardless of system locale, this will only convert ASCII
  1552. * values 'a' through 'z' to uppercase.
  1553. *
  1554. * This function returns the uppercase equivalent of `x`. If a character
  1555. * cannot be converted, or is already uppercase, this function returns `x`.
  1556. *
  1557. * \param x character value to check.
  1558. * \returns capitalized version of x, or x if no conversion available.
  1559. *
  1560. * \threadsafety It is safe to call this function from any thread.
  1561. *
  1562. * \since This function is available since SDL 3.0.0.
  1563. */
  1564. extern SDL_DECLSPEC int SDLCALL SDL_toupper(int x);
  1565. /**
  1566. * Convert low-ASCII English letters to lowercase.
  1567. *
  1568. * **WARNING**: Regardless of system locale, this will only convert ASCII
  1569. * values 'A' through 'Z' to lowercase.
  1570. *
  1571. * This function returns the lowercase equivalent of `x`. If a character
  1572. * cannot be converted, or is already lowercase, this function returns `x`.
  1573. *
  1574. * \param x character value to check.
  1575. * \returns lowercase version of x, or x if no conversion available.
  1576. *
  1577. * \threadsafety It is safe to call this function from any thread.
  1578. *
  1579. * \since This function is available since SDL 3.0.0.
  1580. */
  1581. extern SDL_DECLSPEC int SDLCALL SDL_tolower(int x);
  1582. extern SDL_DECLSPEC Uint16 SDLCALL SDL_crc16(Uint16 crc, const void *data, size_t len);
  1583. extern SDL_DECLSPEC Uint32 SDLCALL SDL_crc32(Uint32 crc, const void *data, size_t len);
  1584. extern SDL_DECLSPEC Uint32 SDLCALL SDL_murmur3_32(const void *data, size_t len, Uint32 seed);
  1585. /**
  1586. * Copy non-overlapping memory.
  1587. *
  1588. * The memory regions must not overlap. If they do, use SDL_memmove() instead.
  1589. *
  1590. * \param dst The destination memory region. Must not be NULL, and must not
  1591. * overlap with `src`.
  1592. * \param src The source memory region. Must not be NULL, and must not overlap
  1593. * with `dst`.
  1594. * \param len The length in bytes of both `dst` and `src`.
  1595. * \returns `dst`.
  1596. *
  1597. * \threadsafety It is safe to call this function from any thread.
  1598. *
  1599. * \since This function is available since SDL 3.0.0.
  1600. *
  1601. * \sa SDL_memmove
  1602. */
  1603. extern SDL_DECLSPEC void * SDLCALL SDL_memcpy(SDL_OUT_BYTECAP(len) void *dst, SDL_IN_BYTECAP(len) const void *src, size_t len);
  1604. /* Take advantage of compiler optimizations for memcpy */
  1605. #ifndef SDL_SLOW_MEMCPY
  1606. #ifdef SDL_memcpy
  1607. #undef SDL_memcpy
  1608. #endif
  1609. #define SDL_memcpy memcpy
  1610. #endif
  1611. #define SDL_copyp(dst, src) \
  1612. { SDL_COMPILE_TIME_ASSERT(SDL_copyp, sizeof (*(dst)) == sizeof (*(src))); } \
  1613. SDL_memcpy((dst), (src), sizeof(*(src)))
  1614. /**
  1615. * Copy memory.
  1616. *
  1617. * It is okay for the memory regions to overlap. If you are confident that the
  1618. * regions never overlap, using SDL_memcpy() may improve performance.
  1619. *
  1620. * \param dst The destination memory region. Must not be NULL.
  1621. * \param src The source memory region. Must not be NULL.
  1622. * \param len The length in bytes of both `dst` and `src`.
  1623. * \returns `dst`.
  1624. *
  1625. * \threadsafety It is safe to call this function from any thread.
  1626. *
  1627. * \since This function is available since SDL 3.0.0.
  1628. *
  1629. * \sa SDL_memcpy
  1630. */
  1631. extern SDL_DECLSPEC void * SDLCALL SDL_memmove(SDL_OUT_BYTECAP(len) void *dst, SDL_IN_BYTECAP(len) const void *src, size_t len);
  1632. /* Take advantage of compiler optimizations for memmove */
  1633. #ifndef SDL_SLOW_MEMMOVE
  1634. #ifdef SDL_memmove
  1635. #undef SDL_memmove
  1636. #endif
  1637. #define SDL_memmove memmove
  1638. #endif
  1639. extern SDL_DECLSPEC void * SDLCALL SDL_memset(SDL_OUT_BYTECAP(len) void *dst, int c, size_t len);
  1640. extern SDL_DECLSPEC void * SDLCALL SDL_memset4(void *dst, Uint32 val, size_t dwords);
  1641. /* Take advantage of compiler optimizations for memset */
  1642. #ifndef SDL_SLOW_MEMSET
  1643. #ifdef SDL_memset
  1644. #undef SDL_memset
  1645. #endif
  1646. #define SDL_memset memset
  1647. #endif
  1648. #define SDL_zero(x) SDL_memset(&(x), 0, sizeof((x)))
  1649. #define SDL_zerop(x) SDL_memset((x), 0, sizeof(*(x)))
  1650. #define SDL_zeroa(x) SDL_memset((x), 0, sizeof((x)))
  1651. extern SDL_DECLSPEC int SDLCALL SDL_memcmp(const void *s1, const void *s2, size_t len);
  1652. extern SDL_DECLSPEC size_t SDLCALL SDL_wcslen(const wchar_t *wstr);
  1653. extern SDL_DECLSPEC size_t SDLCALL SDL_wcsnlen(const wchar_t *wstr, size_t maxlen);
  1654. /**
  1655. * Copy a wide string.
  1656. *
  1657. * This function copies `maxlen` - 1 wide characters from `src` to `dst`, then
  1658. * appends a null terminator.
  1659. *
  1660. * `src` and `dst` must not overlap.
  1661. *
  1662. * If `maxlen` is 0, no wide characters are copied and no null terminator is
  1663. * written.
  1664. *
  1665. * \param dst The destination buffer. Must not be NULL, and must not overlap
  1666. * with `src`.
  1667. * \param src The null-terminated wide string to copy. Must not be NULL, and
  1668. * must not overlap with `dst`.
  1669. * \param maxlen The length (in wide characters) of the destination buffer.
  1670. * \returns The length (in wide characters, excluding the null terminator) of
  1671. * `src`.
  1672. *
  1673. * \threadsafety It is safe to call this function from any thread.
  1674. *
  1675. * \since This function is available since SDL 3.0.0.
  1676. *
  1677. * \sa SDL_wcslcat
  1678. */
  1679. extern SDL_DECLSPEC size_t SDLCALL SDL_wcslcpy(SDL_OUT_Z_CAP(maxlen) wchar_t *dst, const wchar_t *src, size_t maxlen);
  1680. /**
  1681. * Concatenate wide strings.
  1682. *
  1683. * This function appends up to `maxlen` - SDL_wcslen(dst) - 1 wide characters
  1684. * from `src` to the end of the wide string in `dst`, then appends a null
  1685. * terminator.
  1686. *
  1687. * `src` and `dst` must not overlap.
  1688. *
  1689. * If `maxlen` - SDL_wcslen(dst) - 1 is less than or equal to 0, then `dst` is
  1690. * unmodified.
  1691. *
  1692. * \param dst The destination buffer already containing the first
  1693. * null-terminated wide string. Must not be NULL and must not
  1694. * overlap with `src`.
  1695. * \param src The second null-terminated wide string. Must not be NULL, and
  1696. * must not overlap with `dst`.
  1697. * \param maxlen The length (in wide characters) of the destination buffer.
  1698. * \returns The length (in wide characters, excluding the null terminator) of
  1699. * the string in `dst` plus the length of `src`.
  1700. *
  1701. * \threadsafety It is safe to call this function from any thread.
  1702. *
  1703. * \since This function is available since SDL 3.0.0.
  1704. *
  1705. * \sa SDL_wcslcpy
  1706. */
  1707. extern SDL_DECLSPEC size_t SDLCALL SDL_wcslcat(SDL_INOUT_Z_CAP(maxlen) wchar_t *dst, const wchar_t *src, size_t maxlen);
  1708. extern SDL_DECLSPEC wchar_t * SDLCALL SDL_wcsdup(const wchar_t *wstr);
  1709. extern SDL_DECLSPEC wchar_t * SDLCALL SDL_wcsstr(const wchar_t *haystack, const wchar_t *needle);
  1710. extern SDL_DECLSPEC wchar_t * SDLCALL SDL_wcsnstr(const wchar_t *haystack, const wchar_t *needle, size_t maxlen);
  1711. /**
  1712. * Compare two null-terminated wide strings.
  1713. *
  1714. * This only compares wchar_t values until it hits a null-terminating
  1715. * character; it does not care if the string is well-formed UTF-16 (or UTF-32,
  1716. * depending on your platform's wchar_t size), or uses valid Unicode values.
  1717. *
  1718. * \param str1 the first string to compare. NULL is not permitted!
  1719. * \param str2 the second string to compare. NULL is not permitted!
  1720. * \returns less than zero if str1 is "less than" str2, greater than zero if
  1721. * str1 is "greater than" str2, and zero if the strings match
  1722. * exactly.
  1723. *
  1724. * \threadsafety It is safe to call this function from any thread.
  1725. *
  1726. * \since This function is available since SDL 3.0.0.
  1727. */
  1728. extern SDL_DECLSPEC int SDLCALL SDL_wcscmp(const wchar_t *str1, const wchar_t *str2);
  1729. /**
  1730. * Compare two wide strings up to a number of wchar_t values.
  1731. *
  1732. * This only compares wchar_t values; it does not care if the string is
  1733. * well-formed UTF-16 (or UTF-32, depending on your platform's wchar_t size),
  1734. * or uses valid Unicode values.
  1735. *
  1736. * Note that while this function is intended to be used with UTF-16 (or
  1737. * UTF-32, depending on your platform's definition of wchar_t), it is
  1738. * comparing raw wchar_t values and not Unicode codepoints: `maxlen` specifies
  1739. * a wchar_t limit! If the limit lands in the middle of a multi-wchar UTF-16
  1740. * sequence, it will only compare a portion of the final character.
  1741. *
  1742. * `maxlen` specifies a maximum number of wchar_t to compare; if the strings
  1743. * match to this number of wide chars (or both have matched to a
  1744. * null-terminator character before this count), they will be considered
  1745. * equal.
  1746. *
  1747. * \param str1 the first string to compare. NULL is not permitted!
  1748. * \param str2 the second string to compare. NULL is not permitted!
  1749. * \param maxlen the maximum number of wchar_t to compare.
  1750. * \returns less than zero if str1 is "less than" str2, greater than zero if
  1751. * str1 is "greater than" str2, and zero if the strings match
  1752. * exactly.
  1753. *
  1754. * \threadsafety It is safe to call this function from any thread.
  1755. *
  1756. * \since This function is available since SDL 3.0.0.
  1757. */
  1758. extern SDL_DECLSPEC int SDLCALL SDL_wcsncmp(const wchar_t *str1, const wchar_t *str2, size_t maxlen);
  1759. /**
  1760. * Compare two null-terminated wide strings, case-insensitively.
  1761. *
  1762. * This will work with Unicode strings, using a technique called
  1763. * "case-folding" to handle the vast majority of case-sensitive human
  1764. * languages regardless of system locale. It can deal with expanding values: a
  1765. * German Eszett character can compare against two ASCII 's' chars and be
  1766. * considered a match, for example. A notable exception: it does not handle
  1767. * the Turkish 'i' character; human language is complicated!
  1768. *
  1769. * Depending on your platform, "wchar_t" might be 2 bytes, and expected to be
  1770. * UTF-16 encoded (like Windows), or 4 bytes in UTF-32 format. Since this
  1771. * handles Unicode, it expects the string to be well-formed and not a
  1772. * null-terminated string of arbitrary bytes. Characters that are not valid
  1773. * UTF-16 (or UTF-32) are treated as Unicode character U+FFFD (REPLACEMENT
  1774. * CHARACTER), which is to say two strings of random bits may turn out to
  1775. * match if they convert to the same amount of replacement characters.
  1776. *
  1777. * \param str1 the first string to compare. NULL is not permitted!
  1778. * \param str2 the second string to compare. NULL is not permitted!
  1779. * \returns less than zero if str1 is "less than" str2, greater than zero if
  1780. * str1 is "greater than" str2, and zero if the strings match
  1781. * exactly.
  1782. *
  1783. * \threadsafety It is safe to call this function from any thread.
  1784. *
  1785. * \since This function is available since SDL 3.0.0.
  1786. */
  1787. extern SDL_DECLSPEC int SDLCALL SDL_wcscasecmp(const wchar_t *str1, const wchar_t *str2);
  1788. /**
  1789. * Compare two wide strings, case-insensitively, up to a number of wchar_t.
  1790. *
  1791. * This will work with Unicode strings, using a technique called
  1792. * "case-folding" to handle the vast majority of case-sensitive human
  1793. * languages regardless of system locale. It can deal with expanding values: a
  1794. * German Eszett character can compare against two ASCII 's' chars and be
  1795. * considered a match, for example. A notable exception: it does not handle
  1796. * the Turkish 'i' character; human language is complicated!
  1797. *
  1798. * Depending on your platform, "wchar_t" might be 2 bytes, and expected to be
  1799. * UTF-16 encoded (like Windows), or 4 bytes in UTF-32 format. Since this
  1800. * handles Unicode, it expects the string to be well-formed and not a
  1801. * null-terminated string of arbitrary bytes. Characters that are not valid
  1802. * UTF-16 (or UTF-32) are treated as Unicode character U+FFFD (REPLACEMENT
  1803. * CHARACTER), which is to say two strings of random bits may turn out to
  1804. * match if they convert to the same amount of replacement characters.
  1805. *
  1806. * Note that while this function might deal with variable-sized characters,
  1807. * `maxlen` specifies a _wchar_ limit! If the limit lands in the middle of a
  1808. * multi-byte UTF-16 sequence, it may convert a portion of the final character
  1809. * to one or more Unicode character U+FFFD (REPLACEMENT CHARACTER) so as not
  1810. * to overflow a buffer.
  1811. *
  1812. * `maxlen` specifies a maximum number of wchar_t values to compare; if the
  1813. * strings match to this number of wchar_t (or both have matched to a
  1814. * null-terminator character before this number of bytes), they will be
  1815. * considered equal.
  1816. *
  1817. * \param str1 the first string to compare. NULL is not permitted!
  1818. * \param str2 the second string to compare. NULL is not permitted!
  1819. * \param maxlen the maximum number of wchar_t values to compare.
  1820. * \returns less than zero if str1 is "less than" str2, greater than zero if
  1821. * str1 is "greater than" str2, and zero if the strings match
  1822. * exactly.
  1823. *
  1824. * \threadsafety It is safe to call this function from any thread.
  1825. *
  1826. * \since This function is available since SDL 3.0.0.
  1827. */
  1828. extern SDL_DECLSPEC int SDLCALL SDL_wcsncasecmp(const wchar_t *str1, const wchar_t *str2, size_t maxlen);
  1829. /**
  1830. * Parse a `long` from a wide string.
  1831. *
  1832. * If `str` starts with whitespace, then those whitespace characters are
  1833. * skipped before attempting to parse the number.
  1834. *
  1835. * If the parsed number does not fit inside a `long`, the result is clamped to
  1836. * the minimum and maximum representable `long` values.
  1837. *
  1838. * \param str The null-terminated wide string to read. Must not be NULL.
  1839. * \param endp If not NULL, the address of the first invalid wide character
  1840. * (i.e. the next character after the parsed number) will be
  1841. * written to this pointer.
  1842. * \param base The base of the integer to read. Supported values are 0 and 2
  1843. * to 36 inclusive. If 0, the base will be inferred from the
  1844. * number's prefix (0x for hexadecimal, 0 for octal, decimal
  1845. * otherwise).
  1846. * \returns The parsed `long`, or 0 if no number could be parsed.
  1847. *
  1848. * \threadsafety It is safe to call this function from any thread.
  1849. *
  1850. * \since This function is available since SDL 3.0.0.
  1851. *
  1852. * \sa SDL_strtol
  1853. */
  1854. extern SDL_DECLSPEC long SDLCALL SDL_wcstol(const wchar_t *str, wchar_t **endp, int base);
  1855. extern SDL_DECLSPEC size_t SDLCALL SDL_strlen(const char *str);
  1856. extern SDL_DECLSPEC size_t SDLCALL SDL_strnlen(const char *str, size_t maxlen);
  1857. /**
  1858. * Copy a string.
  1859. *
  1860. * This function copies up to `maxlen` - 1 characters from `src` to `dst`,
  1861. * then appends a null terminator.
  1862. *
  1863. * If `maxlen` is 0, no characters are copied and no null terminator is
  1864. * written.
  1865. *
  1866. * If you want to copy an UTF-8 string but need to ensure that multi-byte
  1867. * sequences are not truncated, consider using SDL_utf8strlcpy().
  1868. *
  1869. * \param dst The destination buffer. Must not be NULL, and must not overlap
  1870. * with `src`.
  1871. * \param src The null-terminated string to copy. Must not be NULL, and must
  1872. * not overlap with `dst`.
  1873. * \param maxlen The length (in characters) of the destination buffer.
  1874. * \returns The length (in characters, excluding the null terminator) of
  1875. * `src`.
  1876. *
  1877. * \threadsafety It is safe to call this function from any thread.
  1878. *
  1879. * \since This function is available since SDL 3.0.0.
  1880. *
  1881. * \sa SDL_strlcat
  1882. * \sa SDL_utf8strlcpy
  1883. */
  1884. extern SDL_DECLSPEC size_t SDLCALL SDL_strlcpy(SDL_OUT_Z_CAP(maxlen) char *dst, const char *src, size_t maxlen);
  1885. /**
  1886. * Copy an UTF-8 string.
  1887. *
  1888. * This function copies up to `dst_bytes` - 1 bytes from `src` to `dst` while
  1889. * also ensuring that the string written to `dst` does not end in a truncated
  1890. * multi-byte sequence. Finally, it appends a null terminator.
  1891. *
  1892. * `src` and `dst` must not overlap.
  1893. *
  1894. * Note that unlike SDL_strlcpy(), this function returns the number of bytes
  1895. * written, not the length of `src`.
  1896. *
  1897. * \param dst The destination buffer. Must not be NULL, and must not overlap
  1898. * with `src`.
  1899. * \param src The null-terminated UTF-8 string to copy. Must not be NULL, and
  1900. * must not overlap with `dst`.
  1901. * \param dst_bytes The length (in bytes) of the destination buffer. Must not
  1902. * be 0.
  1903. * \returns The number of bytes written, excluding the null terminator.
  1904. *
  1905. * \threadsafety It is safe to call this function from any thread.
  1906. *
  1907. * \since This function is available since SDL 3.0.0.
  1908. *
  1909. * \sa SDL_strlcpy
  1910. */
  1911. extern SDL_DECLSPEC size_t SDLCALL SDL_utf8strlcpy(SDL_OUT_Z_CAP(dst_bytes) char *dst, const char *src, size_t dst_bytes);
  1912. /**
  1913. * Concatenate strings.
  1914. *
  1915. * This function appends up to `maxlen` - SDL_strlen(dst) - 1 characters from
  1916. * `src` to the end of the string in `dst`, then appends a null terminator.
  1917. *
  1918. * `src` and `dst` must not overlap.
  1919. *
  1920. * If `maxlen` - SDL_strlen(dst) - 1 is less than or equal to 0, then `dst` is
  1921. * unmodified.
  1922. *
  1923. * \param dst The destination buffer already containing the first
  1924. * null-terminated string. Must not be NULL and must not overlap
  1925. * with `src`.
  1926. * \param src The second null-terminated string. Must not be NULL, and must
  1927. * not overlap with `dst`.
  1928. * \param maxlen The length (in characters) of the destination buffer.
  1929. * \returns The length (in characters, excluding the null terminator) of the
  1930. * string in `dst` plus the length of `src`.
  1931. *
  1932. * \threadsafety It is safe to call this function from any thread.
  1933. *
  1934. * \since This function is available since SDL 3.0.0.
  1935. *
  1936. * \sa SDL_strlcpy
  1937. */
  1938. extern SDL_DECLSPEC size_t SDLCALL SDL_strlcat(SDL_INOUT_Z_CAP(maxlen) char *dst, const char *src, size_t maxlen);
  1939. extern SDL_DECLSPEC SDL_MALLOC char * SDLCALL SDL_strdup(const char *str);
  1940. extern SDL_DECLSPEC SDL_MALLOC char * SDLCALL SDL_strndup(const char *str, size_t maxlen);
  1941. extern SDL_DECLSPEC char * SDLCALL SDL_strrev(char *str);
  1942. /**
  1943. * Convert a string to uppercase.
  1944. *
  1945. * **WARNING**: Regardless of system locale, this will only convert ASCII
  1946. * values 'A' through 'Z' to uppercase.
  1947. *
  1948. * This function operates on a null-terminated string of bytes--even if it is
  1949. * malformed UTF-8!--and converts ASCII characters 'a' through 'z' to their
  1950. * uppercase equivalents in-place, returning the original `str` pointer.
  1951. *
  1952. * \param str the string to convert in-place. Can not be NULL.
  1953. * \returns the `str` pointer passed into this function.
  1954. *
  1955. * \threadsafety It is safe to call this function from any thread.
  1956. *
  1957. * \since This function is available since SDL 3.0.0.
  1958. *
  1959. * \sa SDL_strlwr
  1960. */
  1961. extern SDL_DECLSPEC char * SDLCALL SDL_strupr(char *str);
  1962. /**
  1963. * Convert a string to lowercase.
  1964. *
  1965. * **WARNING**: Regardless of system locale, this will only convert ASCII
  1966. * values 'A' through 'Z' to lowercase.
  1967. *
  1968. * This function operates on a null-terminated string of bytes--even if it is
  1969. * malformed UTF-8!--and converts ASCII characters 'A' through 'Z' to their
  1970. * lowercase equivalents in-place, returning the original `str` pointer.
  1971. *
  1972. * \param str the string to convert in-place. Can not be NULL.
  1973. * \returns the `str` pointer passed into this function.
  1974. *
  1975. * \threadsafety It is safe to call this function from any thread.
  1976. *
  1977. * \since This function is available since SDL 3.0.0.
  1978. *
  1979. * \sa SDL_strupr
  1980. */
  1981. extern SDL_DECLSPEC char * SDLCALL SDL_strlwr(char *str);
  1982. extern SDL_DECLSPEC char * SDLCALL SDL_strchr(const char *str, int c);
  1983. extern SDL_DECLSPEC char * SDLCALL SDL_strrchr(const char *str, int c);
  1984. extern SDL_DECLSPEC char * SDLCALL SDL_strstr(const char *haystack, const char *needle);
  1985. extern SDL_DECLSPEC char * SDLCALL SDL_strnstr(const char *haystack, const char *needle, size_t maxlen);
  1986. extern SDL_DECLSPEC char * SDLCALL SDL_strcasestr(const char *haystack, const char *needle);
  1987. extern SDL_DECLSPEC char * SDLCALL SDL_strtok_r(char *s1, const char *s2, char **saveptr);
  1988. extern SDL_DECLSPEC size_t SDLCALL SDL_utf8strlen(const char *str);
  1989. extern SDL_DECLSPEC size_t SDLCALL SDL_utf8strnlen(const char *str, size_t bytes);
  1990. extern SDL_DECLSPEC char * SDLCALL SDL_itoa(int value, char *str, int radix);
  1991. extern SDL_DECLSPEC char * SDLCALL SDL_uitoa(unsigned int value, char *str, int radix);
  1992. extern SDL_DECLSPEC char * SDLCALL SDL_ltoa(long value, char *str, int radix);
  1993. extern SDL_DECLSPEC char * SDLCALL SDL_ultoa(unsigned long value, char *str, int radix);
  1994. extern SDL_DECLSPEC char * SDLCALL SDL_lltoa(long long value, char *str, int radix);
  1995. extern SDL_DECLSPEC char * SDLCALL SDL_ulltoa(unsigned long long value, char *str, int radix);
  1996. /**
  1997. * Parse an `int` from a string.
  1998. *
  1999. * The result of calling `SDL_atoi(str)` is equivalent to
  2000. * `(int)SDL_strtol(str, NULL, 10)`.
  2001. *
  2002. * \param str The null-terminated string to read. Must not be NULL.
  2003. * \returns The parsed `int`.
  2004. *
  2005. * \threadsafety It is safe to call this function from any thread.
  2006. *
  2007. * \since This function is available since SDL 3.0.0.
  2008. *
  2009. * \sa SDL_atof
  2010. * \sa SDL_strtol
  2011. * \sa SDL_strtoul
  2012. * \sa SDL_strtoll
  2013. * \sa SDL_strtoull
  2014. * \sa SDL_strtod
  2015. * \sa SDL_itoa
  2016. */
  2017. extern SDL_DECLSPEC int SDLCALL SDL_atoi(const char *str);
  2018. /**
  2019. * Parse a `double` from a string.
  2020. *
  2021. * The result of calling `SDL_atof(str)` is equivalent to `SDL_strtod(str,
  2022. * NULL)`.
  2023. *
  2024. * \param str The null-terminated string to read. Must not be NULL.
  2025. * \returns The parsed `double`.
  2026. *
  2027. * \threadsafety It is safe to call this function from any thread.
  2028. *
  2029. * \since This function is available since SDL 3.0.0.
  2030. *
  2031. * \sa SDL_atoi
  2032. * \sa SDL_strtol
  2033. * \sa SDL_strtoul
  2034. * \sa SDL_strtoll
  2035. * \sa SDL_strtoull
  2036. * \sa SDL_strtod
  2037. */
  2038. extern SDL_DECLSPEC double SDLCALL SDL_atof(const char *str);
  2039. /**
  2040. * Parse a `long` from a string.
  2041. *
  2042. * If `str` starts with whitespace, then those whitespace characters are
  2043. * skipped before attempting to parse the number.
  2044. *
  2045. * If the parsed number does not fit inside a `long`, the result is clamped to
  2046. * the minimum and maximum representable `long` values.
  2047. *
  2048. * \param str The null-terminated string to read. Must not be NULL.
  2049. * \param endp If not NULL, the address of the first invalid character (i.e.
  2050. * the next character after the parsed number) will be written to
  2051. * this pointer.
  2052. * \param base The base of the integer to read. Supported values are 0 and 2
  2053. * to 36 inclusive. If 0, the base will be inferred from the
  2054. * number's prefix (0x for hexadecimal, 0 for octal, decimal
  2055. * otherwise).
  2056. * \returns The parsed `long`, or 0 if no number could be parsed.
  2057. *
  2058. * \threadsafety It is safe to call this function from any thread.
  2059. *
  2060. * \since This function is available since SDL 3.0.0.
  2061. *
  2062. * \sa SDL_atoi
  2063. * \sa SDL_atof
  2064. * \sa SDL_strtoul
  2065. * \sa SDL_strtoll
  2066. * \sa SDL_strtoull
  2067. * \sa SDL_strtod
  2068. * \sa SDL_ltoa
  2069. * \sa SDL_wcstol
  2070. */
  2071. extern SDL_DECLSPEC long SDLCALL SDL_strtol(const char *str, char **endp, int base);
  2072. /**
  2073. * Parse an `unsigned long` from a string.
  2074. *
  2075. * If `str` starts with whitespace, then those whitespace characters are
  2076. * skipped before attempting to parse the number.
  2077. *
  2078. * If the parsed number does not fit inside an `unsigned long`, the result is
  2079. * clamped to the maximum representable `unsigned long` value.
  2080. *
  2081. * \param str The null-terminated string to read. Must not be NULL.
  2082. * \param endp If not NULL, the address of the first invalid character (i.e.
  2083. * the next character after the parsed number) will be written to
  2084. * this pointer.
  2085. * \param base The base of the integer to read. Supported values are 0 and 2
  2086. * to 36 inclusive. If 0, the base will be inferred from the
  2087. * number's prefix (0x for hexadecimal, 0 for octal, decimal
  2088. * otherwise).
  2089. * \returns The parsed `unsigned long`, or 0 if no number could be parsed.
  2090. *
  2091. * \threadsafety It is safe to call this function from any thread.
  2092. *
  2093. * \since This function is available since SDL 3.0.0.
  2094. *
  2095. * \sa SDL_atoi
  2096. * \sa SDL_atof
  2097. * \sa SDL_strtol
  2098. * \sa SDL_strtoll
  2099. * \sa SDL_strtoull
  2100. * \sa SDL_strtod
  2101. * \sa SDL_ultoa
  2102. */
  2103. extern SDL_DECLSPEC unsigned long SDLCALL SDL_strtoul(const char *str, char **endp, int base);
  2104. /**
  2105. * Parse a `long long` from a string.
  2106. *
  2107. * If `str` starts with whitespace, then those whitespace characters are
  2108. * skipped before attempting to parse the number.
  2109. *
  2110. * If the parsed number does not fit inside a `long long`, the result is
  2111. * clamped to the minimum and maximum representable `long long` values.
  2112. *
  2113. * \param str The null-terminated string to read. Must not be NULL.
  2114. * \param endp If not NULL, the address of the first invalid character (i.e.
  2115. * the next character after the parsed number) will be written to
  2116. * this pointer.
  2117. * \param base The base of the integer to read. Supported values are 0 and 2
  2118. * to 36 inclusive. If 0, the base will be inferred from the
  2119. * number's prefix (0x for hexadecimal, 0 for octal, decimal
  2120. * otherwise).
  2121. * \returns The parsed `long long`, or 0 if no number could be parsed.
  2122. *
  2123. * \threadsafety It is safe to call this function from any thread.
  2124. *
  2125. * \since This function is available since SDL 3.0.0.
  2126. *
  2127. * \sa SDL_atoi
  2128. * \sa SDL_atof
  2129. * \sa SDL_strtol
  2130. * \sa SDL_strtoul
  2131. * \sa SDL_strtoull
  2132. * \sa SDL_strtod
  2133. * \sa SDL_lltoa
  2134. */
  2135. extern SDL_DECLSPEC long long SDLCALL SDL_strtoll(const char *str, char **endp, int base);
  2136. /**
  2137. * Parse an `unsigned long long` from a string.
  2138. *
  2139. * If `str` starts with whitespace, then those whitespace characters are
  2140. * skipped before attempting to parse the number.
  2141. *
  2142. * If the parsed number does not fit inside an `unsigned long long`, the
  2143. * result is clamped to the maximum representable `unsigned long long` value.
  2144. *
  2145. * \param str The null-terminated string to read. Must not be NULL.
  2146. * \param endp If not NULL, the address of the first invalid character (i.e.
  2147. * the next character after the parsed number) will be written to
  2148. * this pointer.
  2149. * \param base The base of the integer to read. Supported values are 0 and 2
  2150. * to 36 inclusive. If 0, the base will be inferred from the
  2151. * number's prefix (0x for hexadecimal, 0 for octal, decimal
  2152. * otherwise).
  2153. * \returns The parsed `unsigned long long`, or 0 if no number could be
  2154. * parsed.
  2155. *
  2156. * \threadsafety It is safe to call this function from any thread.
  2157. *
  2158. * \since This function is available since SDL 3.0.0.
  2159. *
  2160. * \sa SDL_atoi
  2161. * \sa SDL_atof
  2162. * \sa SDL_strtol
  2163. * \sa SDL_strtoll
  2164. * \sa SDL_strtoul
  2165. * \sa SDL_strtod
  2166. * \sa SDL_ulltoa
  2167. */
  2168. extern SDL_DECLSPEC unsigned long long SDLCALL SDL_strtoull(const char *str, char **endp, int base);
  2169. /**
  2170. * Parse a `double` from a string.
  2171. *
  2172. * This function makes fewer guarantees than the C runtime `strtod`:
  2173. *
  2174. * - Only decimal notation is guaranteed to be supported. The handling of
  2175. * scientific and hexadecimal notation is unspecified.
  2176. * - Whether or not INF and NAN can be parsed is unspecified.
  2177. * - The precision of the result is unspecified.
  2178. *
  2179. * \param str The null-terminated string to read. Must not be NULL.
  2180. * \param endp If not NULL, the address of the first invalid character (i.e.
  2181. * the next character after the parsed number) will be written to
  2182. * this pointer.
  2183. * \returns The parsed `double`, or 0 if no number could be parsed.
  2184. *
  2185. * \threadsafety It is safe to call this function from any thread.
  2186. *
  2187. * \since This function is available since SDL 3.0.0.
  2188. *
  2189. * \sa SDL_atoi
  2190. * \sa SDL_atof
  2191. * \sa SDL_strtol
  2192. * \sa SDL_strtoll
  2193. * \sa SDL_strtoul
  2194. * \sa SDL_strtoull
  2195. */
  2196. extern SDL_DECLSPEC double SDLCALL SDL_strtod(const char *str, char **endp);
  2197. /**
  2198. * Compare two null-terminated UTF-8 strings.
  2199. *
  2200. * Due to the nature of UTF-8 encoding, this will work with Unicode strings,
  2201. * since effectively this function just compares bytes until it hits a
  2202. * null-terminating character. Also due to the nature of UTF-8, this can be
  2203. * used with SDL_qsort() to put strings in (roughly) alphabetical order.
  2204. *
  2205. * \param str1 the first string to compare. NULL is not permitted!
  2206. * \param str2 the second string to compare. NULL is not permitted!
  2207. * \returns less than zero if str1 is "less than" str2, greater than zero if
  2208. * str1 is "greater than" str2, and zero if the strings match
  2209. * exactly.
  2210. *
  2211. * \threadsafety It is safe to call this function from any thread.
  2212. *
  2213. * \since This function is available since SDL 3.0.0.
  2214. */
  2215. extern SDL_DECLSPEC int SDLCALL SDL_strcmp(const char *str1, const char *str2);
  2216. /**
  2217. * Compare two UTF-8 strings up to a number of bytes.
  2218. *
  2219. * Due to the nature of UTF-8 encoding, this will work with Unicode strings,
  2220. * since effectively this function just compares bytes until it hits a
  2221. * null-terminating character. Also due to the nature of UTF-8, this can be
  2222. * used with SDL_qsort() to put strings in (roughly) alphabetical order.
  2223. *
  2224. * Note that while this function is intended to be used with UTF-8, it is
  2225. * doing a bytewise comparison, and `maxlen` specifies a _byte_ limit! If the
  2226. * limit lands in the middle of a multi-byte UTF-8 sequence, it will only
  2227. * compare a portion of the final character.
  2228. *
  2229. * `maxlen` specifies a maximum number of bytes to compare; if the strings
  2230. * match to this number of bytes (or both have matched to a null-terminator
  2231. * character before this number of bytes), they will be considered equal.
  2232. *
  2233. * \param str1 the first string to compare. NULL is not permitted!
  2234. * \param str2 the second string to compare. NULL is not permitted!
  2235. * \param maxlen the maximum number of _bytes_ to compare.
  2236. * \returns less than zero if str1 is "less than" str2, greater than zero if
  2237. * str1 is "greater than" str2, and zero if the strings match
  2238. * exactly.
  2239. *
  2240. * \threadsafety It is safe to call this function from any thread.
  2241. *
  2242. * \since This function is available since SDL 3.0.0.
  2243. */
  2244. extern SDL_DECLSPEC int SDLCALL SDL_strncmp(const char *str1, const char *str2, size_t maxlen);
  2245. /**
  2246. * Compare two null-terminated UTF-8 strings, case-insensitively.
  2247. *
  2248. * This will work with Unicode strings, using a technique called
  2249. * "case-folding" to handle the vast majority of case-sensitive human
  2250. * languages regardless of system locale. It can deal with expanding values: a
  2251. * German Eszett character can compare against two ASCII 's' chars and be
  2252. * considered a match, for example. A notable exception: it does not handle
  2253. * the Turkish 'i' character; human language is complicated!
  2254. *
  2255. * Since this handles Unicode, it expects the string to be well-formed UTF-8
  2256. * and not a null-terminated string of arbitrary bytes. Bytes that are not
  2257. * valid UTF-8 are treated as Unicode character U+FFFD (REPLACEMENT
  2258. * CHARACTER), which is to say two strings of random bits may turn out to
  2259. * match if they convert to the same amount of replacement characters.
  2260. *
  2261. * \param str1 the first string to compare. NULL is not permitted!
  2262. * \param str2 the second string to compare. NULL is not permitted!
  2263. * \returns less than zero if str1 is "less than" str2, greater than zero if
  2264. * str1 is "greater than" str2, and zero if the strings match
  2265. * exactly.
  2266. *
  2267. * \threadsafety It is safe to call this function from any thread.
  2268. *
  2269. * \since This function is available since SDL 3.0.0.
  2270. */
  2271. extern SDL_DECLSPEC int SDLCALL SDL_strcasecmp(const char *str1, const char *str2);
  2272. /**
  2273. * Compare two UTF-8 strings, case-insensitively, up to a number of bytes.
  2274. *
  2275. * This will work with Unicode strings, using a technique called
  2276. * "case-folding" to handle the vast majority of case-sensitive human
  2277. * languages regardless of system locale. It can deal with expanding values: a
  2278. * German Eszett character can compare against two ASCII 's' chars and be
  2279. * considered a match, for example. A notable exception: it does not handle
  2280. * the Turkish 'i' character; human language is complicated!
  2281. *
  2282. * Since this handles Unicode, it expects the string to be well-formed UTF-8
  2283. * and not a null-terminated string of arbitrary bytes. Bytes that are not
  2284. * valid UTF-8 are treated as Unicode character U+FFFD (REPLACEMENT
  2285. * CHARACTER), which is to say two strings of random bits may turn out to
  2286. * match if they convert to the same amount of replacement characters.
  2287. *
  2288. * Note that while this function is intended to be used with UTF-8, `maxlen`
  2289. * specifies a _byte_ limit! If the limit lands in the middle of a multi-byte
  2290. * UTF-8 sequence, it may convert a portion of the final character to one or
  2291. * more Unicode character U+FFFD (REPLACEMENT CHARACTER) so as not to overflow
  2292. * a buffer.
  2293. *
  2294. * `maxlen` specifies a maximum number of bytes to compare; if the strings
  2295. * match to this number of bytes (or both have matched to a null-terminator
  2296. * character before this number of bytes), they will be considered equal.
  2297. *
  2298. * \param str1 the first string to compare. NULL is not permitted!
  2299. * \param str2 the second string to compare. NULL is not permitted!
  2300. * \param maxlen the maximum number of bytes to compare.
  2301. * \returns less than zero if str1 is "less than" str2, greater than zero if
  2302. * str1 is "greater than" str2, and zero if the strings match
  2303. * exactly.
  2304. *
  2305. * \threadsafety It is safe to call this function from any thread.
  2306. *
  2307. * \since This function is available since SDL 3.0.0.
  2308. */
  2309. extern SDL_DECLSPEC int SDLCALL SDL_strncasecmp(const char *str1, const char *str2, size_t maxlen);
  2310. /**
  2311. * Searches a string for the first occurence of any character contained in a
  2312. * breakset, and returns a pointer from the string to that character.
  2313. *
  2314. * \param str The null-terminated string to be searched. Must not be NULL, and
  2315. * must not overlap with `breakset`.
  2316. * \param breakset A null-terminated string containing the list of characters
  2317. * to look for. Must not be NULL, and must not overlap with
  2318. * `str`.
  2319. * \returns A pointer to the location, in str, of the first occurence of a
  2320. * character present in the breakset, or NULL if none is found.
  2321. *
  2322. * \threadsafety It is safe to call this function from any thread.
  2323. *
  2324. * \since This function is available since SDL 3.0.0.
  2325. */
  2326. extern SDL_DECLSPEC char * SDLCALL SDL_strpbrk(const char *str, const char *breakset);
  2327. /**
  2328. * The Unicode REPLACEMENT CHARACTER codepoint.
  2329. *
  2330. * SDL_StepUTF8() reports this codepoint when it encounters a UTF-8 string
  2331. * with encoding errors.
  2332. *
  2333. * This tends to render as something like a question mark in most places.
  2334. *
  2335. * \since This macro is available since SDL 3.0.0.
  2336. *
  2337. * \sa SDL_StepUTF8
  2338. */
  2339. #define SDL_INVALID_UNICODE_CODEPOINT 0xFFFD
  2340. /**
  2341. * Decode a UTF-8 string, one Unicode codepoint at a time.
  2342. *
  2343. * This will return the first Unicode codepoint in the UTF-8 encoded string in
  2344. * `*pstr`, and then advance `*pstr` past any consumed bytes before returning.
  2345. *
  2346. * It will not access more than `*pslen` bytes from the string. `*pslen` will
  2347. * be adjusted, as well, subtracting the number of bytes consumed.
  2348. *
  2349. * `pslen` is allowed to be NULL, in which case the string _must_ be
  2350. * NULL-terminated, as the function will blindly read until it sees the NULL
  2351. * char.
  2352. *
  2353. * if `*pslen` is zero, it assumes the end of string is reached and returns a
  2354. * zero codepoint regardless of the contents of the string buffer.
  2355. *
  2356. * If the resulting codepoint is zero (a NULL terminator), or `*pslen` is
  2357. * zero, it will not advance `*pstr` or `*pslen` at all.
  2358. *
  2359. * Generally this function is called in a loop until it returns zero,
  2360. * adjusting its parameters each iteration.
  2361. *
  2362. * If an invalid UTF-8 sequence is encountered, this function returns
  2363. * SDL_INVALID_UNICODE_CODEPOINT and advances the string/length by one byte
  2364. * (which is to say, a multibyte sequence might produce several
  2365. * SDL_INVALID_UNICODE_CODEPOINT returns before it syncs to the next valid
  2366. * UTF-8 sequence).
  2367. *
  2368. * Several things can generate invalid UTF-8 sequences, including overlong
  2369. * encodings, the use of UTF-16 surrogate values, and truncated data. Please
  2370. * refer to
  2371. * [RFC3629](https://www.ietf.org/rfc/rfc3629.txt)
  2372. * for details.
  2373. *
  2374. * \param pstr a pointer to a UTF-8 string pointer to be read and adjusted.
  2375. * \param pslen a pointer to the number of bytes in the string, to be read and
  2376. * adjusted. NULL is allowed.
  2377. * \returns the first Unicode codepoint in the string.
  2378. *
  2379. * \threadsafety It is safe to call this function from any thread.
  2380. *
  2381. * \since This function is available since SDL 3.0.0.
  2382. */
  2383. extern SDL_DECLSPEC Uint32 SDLCALL SDL_StepUTF8(const char **pstr, size_t *pslen);
  2384. /**
  2385. * Convert a single Unicode codepoint to UTF-8.
  2386. *
  2387. * The buffer pointed to by `dst` must be at least 4 bytes long, as this
  2388. * function may generate between 1 and 4 bytes of output.
  2389. *
  2390. * This function returns the first byte _after_ the newly-written UTF-8
  2391. * sequence, which is useful for encoding multiple codepoints in a loop, or
  2392. * knowing where to write a NULL-terminator character to end the string (in
  2393. * either case, plan to have a buffer of _more_ than 4 bytes!).
  2394. *
  2395. * If `codepoint` is an invalid value (outside the Unicode range, or a UTF-16
  2396. * surrogate value, etc), this will use U+FFFD (REPLACEMENT CHARACTER) for the
  2397. * codepoint instead, and not set an error.
  2398. *
  2399. * If `dst` is NULL, this returns NULL immediately without writing to the
  2400. * pointer and without setting an error.
  2401. *
  2402. * \param codepoint a Unicode codepoint to convert to UTF-8.
  2403. * \param dst the location to write the encoded UTF-8. Must point to at least
  2404. * 4 bytes!
  2405. * \returns the first byte past the newly-written UTF-8 sequence.
  2406. *
  2407. * \threadsafety It is safe to call this function from any thread.
  2408. *
  2409. * \since This function is available since SDL 3.0.0.
  2410. */
  2411. extern SDL_DECLSPEC char * SDLCALL SDL_UCS4ToUTF8(Uint32 codepoint, char *dst);
  2412. extern SDL_DECLSPEC int SDLCALL SDL_sscanf(const char *text, SDL_SCANF_FORMAT_STRING const char *fmt, ...) SDL_SCANF_VARARG_FUNC(2);
  2413. 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);
  2414. 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);
  2415. 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);
  2416. 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);
  2417. 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);
  2418. extern SDL_DECLSPEC int SDLCALL SDL_asprintf(char **strp, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) SDL_PRINTF_VARARG_FUNC(2);
  2419. extern SDL_DECLSPEC int SDLCALL SDL_vasprintf(char **strp, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap) SDL_PRINTF_VARARG_FUNCV(2);
  2420. /**
  2421. * Seeds the pseudo-random number generator.
  2422. *
  2423. * Reusing the seed number will cause SDL_rand_*() to repeat the same stream
  2424. * of 'random' numbers.
  2425. *
  2426. * \param seed the value to use as a random number seed, or 0 to use
  2427. * SDL_GetPerformanceCounter().
  2428. *
  2429. * \threadsafety This should be called on the same thread that calls
  2430. * SDL_rand*()
  2431. *
  2432. * \since This function is available since SDL 3.0.0.
  2433. *
  2434. * \sa SDL_rand
  2435. * \sa SDL_rand_bits
  2436. * \sa SDL_randf
  2437. */
  2438. extern SDL_DECLSPEC void SDLCALL SDL_srand(Uint64 seed);
  2439. /**
  2440. * Generate a pseudo-random number less than n for positive n
  2441. *
  2442. * The method used is faster and of better quality than `rand() % n`. Odds are
  2443. * roughly 99.9% even for n = 1 million. Evenness is better for smaller n, and
  2444. * much worse as n gets bigger.
  2445. *
  2446. * Example: to simulate a d6 use `SDL_rand(6) + 1` The +1 converts 0..5 to
  2447. * 1..6
  2448. *
  2449. * If you want to generate a pseudo-random number in the full range of Sint32,
  2450. * you should use: (Sint32)SDL_rand_bits()
  2451. *
  2452. * If you want reproducible output, be sure to initialize with SDL_srand()
  2453. * first.
  2454. *
  2455. * There are no guarantees as to the quality of the random sequence produced,
  2456. * and this should not be used for security (cryptography, passwords) or where
  2457. * money is on the line (loot-boxes, casinos). There are many random number
  2458. * libraries available with different characteristics and you should pick one
  2459. * of those to meet any serious needs.
  2460. *
  2461. * \param n the number of possible outcomes. n must be positive.
  2462. * \returns a random value in the range of [0 .. n-1].
  2463. *
  2464. * \threadsafety All calls should be made from a single thread
  2465. *
  2466. * \since This function is available since SDL 3.0.0.
  2467. *
  2468. * \sa SDL_srand
  2469. * \sa SDL_randf
  2470. */
  2471. extern SDL_DECLSPEC Sint32 SDLCALL SDL_rand(Sint32 n);
  2472. /**
  2473. * Generate a uniform pseudo-random floating point number less than 1.0
  2474. *
  2475. * If you want reproducible output, be sure to initialize with SDL_srand()
  2476. * first.
  2477. *
  2478. * There are no guarantees as to the quality of the random sequence produced,
  2479. * and this should not be used for security (cryptography, passwords) or where
  2480. * money is on the line (loot-boxes, casinos). There are many random number
  2481. * libraries available with different characteristics and you should pick one
  2482. * of those to meet any serious needs.
  2483. *
  2484. * \returns a random value in the range of [0.0, 1.0).
  2485. *
  2486. * \threadsafety All calls should be made from a single thread
  2487. *
  2488. * \since This function is available since SDL 3.0.0.
  2489. *
  2490. * \sa SDL_srand
  2491. * \sa SDL_rand
  2492. */
  2493. extern SDL_DECLSPEC float SDLCALL SDL_randf(void);
  2494. /**
  2495. * Generate 32 pseudo-random bits.
  2496. *
  2497. * You likely want to use SDL_rand() to get a psuedo-random number instead.
  2498. *
  2499. * There are no guarantees as to the quality of the random sequence produced,
  2500. * and this should not be used for security (cryptography, passwords) or where
  2501. * money is on the line (loot-boxes, casinos). There are many random number
  2502. * libraries available with different characteristics and you should pick one
  2503. * of those to meet any serious needs.
  2504. *
  2505. * \returns a random value in the range of [0-SDL_MAX_UINT32].
  2506. *
  2507. * \threadsafety All calls should be made from a single thread
  2508. *
  2509. * \since This function is available since SDL 3.0.0.
  2510. *
  2511. * \sa SDL_rand
  2512. * \sa SDL_randf
  2513. * \sa SDL_srand
  2514. */
  2515. extern SDL_DECLSPEC Uint32 SDLCALL SDL_rand_bits(void);
  2516. /**
  2517. * Generate a pseudo-random number less than n for positive n
  2518. *
  2519. * The method used is faster and of better quality than `rand() % n`. Odds are
  2520. * roughly 99.9% even for n = 1 million. Evenness is better for smaller n, and
  2521. * much worse as n gets bigger.
  2522. *
  2523. * Example: to simulate a d6 use `SDL_rand_r(state, 6) + 1` The +1 converts
  2524. * 0..5 to 1..6
  2525. *
  2526. * If you want to generate a pseudo-random number in the full range of Sint32,
  2527. * you should use: (Sint32)SDL_rand_bits_r(state)
  2528. *
  2529. * There are no guarantees as to the quality of the random sequence produced,
  2530. * and this should not be used for security (cryptography, passwords) or where
  2531. * money is on the line (loot-boxes, casinos). There are many random number
  2532. * libraries available with different characteristics and you should pick one
  2533. * of those to meet any serious needs.
  2534. *
  2535. * \param state a pointer to the current random number state, this may not be
  2536. * NULL.
  2537. * \param n the number of possible outcomes. n must be positive.
  2538. * \returns a random value in the range of [0 .. n-1].
  2539. *
  2540. * \threadsafety This function is thread-safe, as long as the state pointer
  2541. * isn't shared between threads.
  2542. *
  2543. * \since This function is available since SDL 3.0.0.
  2544. *
  2545. * \sa SDL_rand
  2546. * \sa SDL_rand_bits_r
  2547. * \sa SDL_randf_r
  2548. */
  2549. extern SDL_DECLSPEC Sint32 SDLCALL SDL_rand_r(Uint64 *state, Sint32 n);
  2550. /**
  2551. * Generate a uniform pseudo-random floating point number less than 1.0
  2552. *
  2553. * If you want reproducible output, be sure to initialize with SDL_srand()
  2554. * first.
  2555. *
  2556. * There are no guarantees as to the quality of the random sequence produced,
  2557. * and this should not be used for security (cryptography, passwords) or where
  2558. * money is on the line (loot-boxes, casinos). There are many random number
  2559. * libraries available with different characteristics and you should pick one
  2560. * of those to meet any serious needs.
  2561. *
  2562. * \param state a pointer to the current random number state, this may not be
  2563. * NULL.
  2564. * \returns a random value in the range of [0.0, 1.0).
  2565. *
  2566. * \threadsafety This function is thread-safe, as long as the state pointer
  2567. * isn't shared between threads.
  2568. *
  2569. * \since This function is available since SDL 3.0.0.
  2570. *
  2571. * \sa SDL_rand_bits_r
  2572. * \sa SDL_rand_r
  2573. * \sa SDL_randf
  2574. */
  2575. extern SDL_DECLSPEC float SDLCALL SDL_randf_r(Uint64 *state);
  2576. /**
  2577. * Generate 32 pseudo-random bits.
  2578. *
  2579. * You likely want to use SDL_rand_r() to get a psuedo-random number instead.
  2580. *
  2581. * There are no guarantees as to the quality of the random sequence produced,
  2582. * and this should not be used for security (cryptography, passwords) or where
  2583. * money is on the line (loot-boxes, casinos). There are many random number
  2584. * libraries available with different characteristics and you should pick one
  2585. * of those to meet any serious needs.
  2586. *
  2587. * \param state a pointer to the current random number state, this may not be
  2588. * NULL.
  2589. * \returns a random value in the range of [0-SDL_MAX_UINT32].
  2590. *
  2591. * \threadsafety This function is thread-safe, as long as the state pointer
  2592. * isn't shared between threads.
  2593. *
  2594. * \since This function is available since SDL 3.0.0.
  2595. *
  2596. * \sa SDL_rand_r
  2597. * \sa SDL_randf_r
  2598. */
  2599. extern SDL_DECLSPEC Uint32 SDLCALL SDL_rand_bits_r(Uint64 *state);
  2600. #ifndef SDL_PI_D
  2601. #define SDL_PI_D 3.141592653589793238462643383279502884 /**< pi (double) */
  2602. #endif
  2603. #ifndef SDL_PI_F
  2604. #define SDL_PI_F 3.141592653589793238462643383279502884F /**< pi (float) */
  2605. #endif
  2606. /**
  2607. * Compute the arc cosine of `x`.
  2608. *
  2609. * The definition of `y = acos(x)` is `x = cos(y)`.
  2610. *
  2611. * Domain: `-1 <= x <= 1`
  2612. *
  2613. * Range: `0 <= y <= Pi`
  2614. *
  2615. * This function operates on double-precision floating point values, use
  2616. * SDL_acosf for single-precision floats.
  2617. *
  2618. * This function may use a different approximation across different versions,
  2619. * platforms and configurations. i.e, it can return a different value given
  2620. * the same input on different machines or operating systems, or if SDL is
  2621. * updated.
  2622. *
  2623. * \param x floating point value.
  2624. * \returns arc cosine of `x`, in radians.
  2625. *
  2626. * \threadsafety It is safe to call this function from any thread.
  2627. *
  2628. * \since This function is available since SDL 3.0.0.
  2629. *
  2630. * \sa SDL_acosf
  2631. * \sa SDL_asin
  2632. * \sa SDL_cos
  2633. */
  2634. extern SDL_DECLSPEC double SDLCALL SDL_acos(double x);
  2635. /**
  2636. * Compute the arc cosine of `x`.
  2637. *
  2638. * The definition of `y = acos(x)` is `x = cos(y)`.
  2639. *
  2640. * Domain: `-1 <= x <= 1`
  2641. *
  2642. * Range: `0 <= y <= Pi`
  2643. *
  2644. * This function operates on single-precision floating point values, use
  2645. * SDL_acos for double-precision floats.
  2646. *
  2647. * This function may use a different approximation across different versions,
  2648. * platforms and configurations. i.e, it can return a different value given
  2649. * the same input on different machines or operating systems, or if SDL is
  2650. * updated.
  2651. *
  2652. * \param x floating point value.
  2653. * \returns arc cosine of `x`, in radians.
  2654. *
  2655. * \threadsafety It is safe to call this function from any thread.
  2656. *
  2657. * \since This function is available since SDL 3.0.0.
  2658. *
  2659. * \sa SDL_acos
  2660. * \sa SDL_asinf
  2661. * \sa SDL_cosf
  2662. */
  2663. extern SDL_DECLSPEC float SDLCALL SDL_acosf(float x);
  2664. /**
  2665. * Compute the arc sine of `x`.
  2666. *
  2667. * The definition of `y = asin(x)` is `x = sin(y)`.
  2668. *
  2669. * Domain: `-1 <= x <= 1`
  2670. *
  2671. * Range: `-Pi/2 <= y <= Pi/2`
  2672. *
  2673. * This function operates on double-precision floating point values, use
  2674. * SDL_asinf for single-precision floats.
  2675. *
  2676. * This function may use a different approximation across different versions,
  2677. * platforms and configurations. i.e, it can return a different value given
  2678. * the same input on different machines or operating systems, or if SDL is
  2679. * updated.
  2680. *
  2681. * \param x floating point value.
  2682. * \returns arc sine of `x`, in radians.
  2683. *
  2684. * \threadsafety It is safe to call this function from any thread.
  2685. *
  2686. * \since This function is available since SDL 3.0.0.
  2687. *
  2688. * \sa SDL_asinf
  2689. * \sa SDL_acos
  2690. * \sa SDL_sin
  2691. */
  2692. extern SDL_DECLSPEC double SDLCALL SDL_asin(double x);
  2693. /**
  2694. * Compute the arc sine of `x`.
  2695. *
  2696. * The definition of `y = asin(x)` is `x = sin(y)`.
  2697. *
  2698. * Domain: `-1 <= x <= 1`
  2699. *
  2700. * Range: `-Pi/2 <= y <= Pi/2`
  2701. *
  2702. * This function operates on single-precision floating point values, use
  2703. * SDL_asin for double-precision floats.
  2704. *
  2705. * This function may use a different approximation across different versions,
  2706. * platforms and configurations. i.e, it can return a different value given
  2707. * the same input on different machines or operating systems, or if SDL is
  2708. * updated.
  2709. *
  2710. * \param x floating point value.
  2711. * \returns arc sine of `x`, in radians.
  2712. *
  2713. * \threadsafety It is safe to call this function from any thread.
  2714. *
  2715. * \since This function is available since SDL 3.0.0.
  2716. *
  2717. * \sa SDL_asin
  2718. * \sa SDL_acosf
  2719. * \sa SDL_sinf
  2720. */
  2721. extern SDL_DECLSPEC float SDLCALL SDL_asinf(float x);
  2722. /**
  2723. * Compute the arc tangent of `x`.
  2724. *
  2725. * The definition of `y = atan(x)` is `x = tan(y)`.
  2726. *
  2727. * Domain: `-INF <= x <= INF`
  2728. *
  2729. * Range: `-Pi/2 <= y <= Pi/2`
  2730. *
  2731. * This function operates on double-precision floating point values, use
  2732. * SDL_atanf for single-precision floats.
  2733. *
  2734. * To calculate the arc tangent of y / x, use SDL_atan2.
  2735. *
  2736. * This function may use a different approximation across different versions,
  2737. * platforms and configurations. i.e, it can return a different value given
  2738. * the same input on different machines or operating systems, or if SDL is
  2739. * updated.
  2740. *
  2741. * \param x floating point value.
  2742. * \returns arc tangent of of `x` in radians, or 0 if `x = 0`.
  2743. *
  2744. * \threadsafety It is safe to call this function from any thread.
  2745. *
  2746. * \since This function is available since SDL 3.0.0.
  2747. *
  2748. * \sa SDL_atanf
  2749. * \sa SDL_atan2
  2750. * \sa SDL_tan
  2751. */
  2752. extern SDL_DECLSPEC double SDLCALL SDL_atan(double x);
  2753. /**
  2754. * Compute the arc tangent of `x`.
  2755. *
  2756. * The definition of `y = atan(x)` is `x = tan(y)`.
  2757. *
  2758. * Domain: `-INF <= x <= INF`
  2759. *
  2760. * Range: `-Pi/2 <= y <= Pi/2`
  2761. *
  2762. * This function operates on single-precision floating point values, use
  2763. * SDL_atan for dboule-precision floats.
  2764. *
  2765. * To calculate the arc tangent of y / x, use SDL_atan2f.
  2766. *
  2767. * This function may use a different approximation across different versions,
  2768. * platforms and configurations. i.e, it can return a different value given
  2769. * the same input on different machines or operating systems, or if SDL is
  2770. * updated.
  2771. *
  2772. * \param x floating point value.
  2773. * \returns arc tangent of of `x` in radians, or 0 if `x = 0`.
  2774. *
  2775. * \threadsafety It is safe to call this function from any thread.
  2776. *
  2777. * \since This function is available since SDL 3.0.0.
  2778. *
  2779. * \sa SDL_atan
  2780. * \sa SDL_atan2f
  2781. * \sa SDL_tanf
  2782. */
  2783. extern SDL_DECLSPEC float SDLCALL SDL_atanf(float x);
  2784. /**
  2785. * Compute the arc tangent of `y / x`, using the signs of x and y to adjust
  2786. * the result's quadrant.
  2787. *
  2788. * The definition of `z = atan2(x, y)` is `y = x tan(z)`, where the quadrant
  2789. * of z is determined based on the signs of x and y.
  2790. *
  2791. * Domain: `-INF <= x <= INF`, `-INF <= y <= INF`
  2792. *
  2793. * Range: `-Pi/2 <= y <= Pi/2`
  2794. *
  2795. * This function operates on double-precision floating point values, use
  2796. * SDL_atan2f for single-precision floats.
  2797. *
  2798. * To calculate the arc tangent of a single value, use SDL_atan.
  2799. *
  2800. * This function may use a different approximation across different versions,
  2801. * platforms and configurations. i.e, it can return a different value given
  2802. * the same input on different machines or operating systems, or if SDL is
  2803. * updated.
  2804. *
  2805. * \param y floating point value of the numerator (y coordinate).
  2806. * \param x floating point value of the denominator (x coordinate).
  2807. * \returns arc tangent of of `y / x` in radians, or, if `x = 0`, either
  2808. * `-Pi/2`, `0`, or `Pi/2`, depending on the value of `y`.
  2809. *
  2810. * \threadsafety It is safe to call this function from any thread.
  2811. *
  2812. * \since This function is available since SDL 3.0.0.
  2813. *
  2814. * \sa SDL_atan2f
  2815. * \sa SDL_atan
  2816. * \sa SDL_tan
  2817. */
  2818. extern SDL_DECLSPEC double SDLCALL SDL_atan2(double y, double x);
  2819. /**
  2820. * Compute the arc tangent of `y / x`, using the signs of x and y to adjust
  2821. * the result's quadrant.
  2822. *
  2823. * The definition of `z = atan2(x, y)` is `y = x tan(z)`, where the quadrant
  2824. * of z is determined based on the signs of x and y.
  2825. *
  2826. * Domain: `-INF <= x <= INF`, `-INF <= y <= INF`
  2827. *
  2828. * Range: `-Pi/2 <= y <= Pi/2`
  2829. *
  2830. * This function operates on single-precision floating point values, use
  2831. * SDL_atan2 for double-precision floats.
  2832. *
  2833. * To calculate the arc tangent of a single value, use SDL_atanf.
  2834. *
  2835. * This function may use a different approximation across different versions,
  2836. * platforms and configurations. i.e, it can return a different value given
  2837. * the same input on different machines or operating systems, or if SDL is
  2838. * updated.
  2839. *
  2840. * \param y floating point value of the numerator (y coordinate).
  2841. * \param x floating point value of the denominator (x coordinate).
  2842. * \returns arc tangent of of `y / x` in radians, or, if `x = 0`, either
  2843. * `-Pi/2`, `0`, or `Pi/2`, depending on the value of `y`.
  2844. *
  2845. * \threadsafety It is safe to call this function from any thread.
  2846. *
  2847. * \since This function is available since SDL 3.0.0.
  2848. *
  2849. * \sa SDL_atan2f
  2850. * \sa SDL_atan
  2851. * \sa SDL_tan
  2852. */
  2853. extern SDL_DECLSPEC float SDLCALL SDL_atan2f(float y, float x);
  2854. /**
  2855. * Compute the ceiling of `x`.
  2856. *
  2857. * The ceiling of `x` is the smallest integer `y` such that `y > x`, i.e `x`
  2858. * rounded up to the nearest integer.
  2859. *
  2860. * Domain: `-INF <= x <= INF`
  2861. *
  2862. * Range: `-INF <= y <= INF`, y integer
  2863. *
  2864. * This function operates on double-precision floating point values, use
  2865. * SDL_ceilf for single-precision floats.
  2866. *
  2867. * \param x floating point value.
  2868. * \returns the ceiling of `x`.
  2869. *
  2870. * \threadsafety It is safe to call this function from any thread.
  2871. *
  2872. * \since This function is available since SDL 3.0.0.
  2873. *
  2874. * \sa SDL_ceilf
  2875. * \sa SDL_floor
  2876. * \sa SDL_trunc
  2877. * \sa SDL_round
  2878. * \sa SDL_lround
  2879. */
  2880. extern SDL_DECLSPEC double SDLCALL SDL_ceil(double x);
  2881. /**
  2882. * Compute the ceiling of `x`.
  2883. *
  2884. * The ceiling of `x` is the smallest integer `y` such that `y > x`, i.e `x`
  2885. * rounded up to the nearest integer.
  2886. *
  2887. * Domain: `-INF <= x <= INF`
  2888. *
  2889. * Range: `-INF <= y <= INF`, y integer
  2890. *
  2891. * This function operates on single-precision floating point values, use
  2892. * SDL_ceil for double-precision floats.
  2893. *
  2894. * \param x floating point value.
  2895. * \returns the ceiling of `x`.
  2896. *
  2897. * \threadsafety It is safe to call this function from any thread.
  2898. *
  2899. * \since This function is available since SDL 3.0.0.
  2900. *
  2901. * \sa SDL_ceil
  2902. * \sa SDL_floorf
  2903. * \sa SDL_truncf
  2904. * \sa SDL_roundf
  2905. * \sa SDL_lroundf
  2906. */
  2907. extern SDL_DECLSPEC float SDLCALL SDL_ceilf(float x);
  2908. /**
  2909. * Copy the sign of one floating-point value to another.
  2910. *
  2911. * The definition of copysign is that ``copysign(x, y) = abs(x) * sign(y)``.
  2912. *
  2913. * Domain: `-INF <= x <= INF`, ``-INF <= y <= f``
  2914. *
  2915. * Range: `-INF <= z <= INF`
  2916. *
  2917. * This function operates on double-precision floating point values, use
  2918. * SDL_copysignf for single-precision floats.
  2919. *
  2920. * \param x floating point value to use as the magnitude.
  2921. * \param y floating point value to use as the sign.
  2922. * \returns the floating point value with the sign of y and the magnitude of
  2923. * x.
  2924. *
  2925. * \threadsafety It is safe to call this function from any thread.
  2926. *
  2927. * \since This function is available since SDL 3.0.0.
  2928. *
  2929. * \sa SDL_copysignf
  2930. * \sa SDL_fabs
  2931. */
  2932. extern SDL_DECLSPEC double SDLCALL SDL_copysign(double x, double y);
  2933. /**
  2934. * Copy the sign of one floating-point value to another.
  2935. *
  2936. * The definition of copysign is that ``copysign(x, y) = abs(x) * sign(y)``.
  2937. *
  2938. * Domain: `-INF <= x <= INF`, ``-INF <= y <= f``
  2939. *
  2940. * Range: `-INF <= z <= INF`
  2941. *
  2942. * This function operates on single-precision floating point values, use
  2943. * SDL_copysign for double-precision floats.
  2944. *
  2945. * \param x floating point value to use as the magnitude.
  2946. * \param y floating point value to use as the sign.
  2947. * \returns the floating point value with the sign of y and the magnitude of
  2948. * x.
  2949. *
  2950. * \threadsafety It is safe to call this function from any thread.
  2951. *
  2952. * \since This function is available since SDL 3.0.0.
  2953. *
  2954. * \sa SDL_copysignf
  2955. * \sa SDL_fabsf
  2956. */
  2957. extern SDL_DECLSPEC float SDLCALL SDL_copysignf(float x, float y);
  2958. /**
  2959. * Compute the cosine of `x`.
  2960. *
  2961. * Domain: `-INF <= x <= INF`
  2962. *
  2963. * Range: `-1 <= y <= 1`
  2964. *
  2965. * This function operates on double-precision floating point values, use
  2966. * SDL_cosf for single-precision floats.
  2967. *
  2968. * This function may use a different approximation across different versions,
  2969. * platforms and configurations. i.e, it can return a different value given
  2970. * the same input on different machines or operating systems, or if SDL is
  2971. * updated.
  2972. *
  2973. * \param x floating point value, in radians.
  2974. * \returns cosine of `x`.
  2975. *
  2976. * \threadsafety It is safe to call this function from any thread.
  2977. *
  2978. * \since This function is available since SDL 3.0.0.
  2979. *
  2980. * \sa SDL_cosf
  2981. * \sa SDL_acos
  2982. * \sa SDL_sin
  2983. */
  2984. extern SDL_DECLSPEC double SDLCALL SDL_cos(double x);
  2985. /**
  2986. * Compute the cosine of `x`.
  2987. *
  2988. * Domain: `-INF <= x <= INF`
  2989. *
  2990. * Range: `-1 <= y <= 1`
  2991. *
  2992. * This function operates on single-precision floating point values, use
  2993. * SDL_cos for double-precision floats.
  2994. *
  2995. * This function may use a different approximation across different versions,
  2996. * platforms and configurations. i.e, it can return a different value given
  2997. * the same input on different machines or operating systems, or if SDL is
  2998. * updated.
  2999. *
  3000. * \param x floating point value, in radians.
  3001. * \returns cosine of `x`.
  3002. *
  3003. * \threadsafety It is safe to call this function from any thread.
  3004. *
  3005. * \since This function is available since SDL 3.0.0.
  3006. *
  3007. * \sa SDL_cos
  3008. * \sa SDL_acosf
  3009. * \sa SDL_sinf
  3010. */
  3011. extern SDL_DECLSPEC float SDLCALL SDL_cosf(float x);
  3012. /**
  3013. * Compute the exponential of `x`.
  3014. *
  3015. * The definition of `y = exp(x)` is `y = e^x`, where `e` is the base of the
  3016. * natural logarithm. The inverse is the natural logarithm, SDL_log.
  3017. *
  3018. * Domain: `-INF <= x <= INF`
  3019. *
  3020. * Range: `0 <= y <= INF`
  3021. *
  3022. * The output will overflow if `exp(x)` is too large to be represented.
  3023. *
  3024. * This function operates on double-precision floating point values, use
  3025. * SDL_expf for single-precision floats.
  3026. *
  3027. * This function may use a different approximation across different versions,
  3028. * platforms and configurations. i.e, it can return a different value given
  3029. * the same input on different machines or operating systems, or if SDL is
  3030. * updated.
  3031. *
  3032. * \param x floating point value.
  3033. * \returns value of `e^x`.
  3034. *
  3035. * \threadsafety It is safe to call this function from any thread.
  3036. *
  3037. * \since This function is available since SDL 3.0.0.
  3038. *
  3039. * \sa SDL_expf
  3040. * \sa SDL_log
  3041. */
  3042. extern SDL_DECLSPEC double SDLCALL SDL_exp(double x);
  3043. /**
  3044. * Compute the exponential of `x`.
  3045. *
  3046. * The definition of `y = exp(x)` is `y = e^x`, where `e` is the base of the
  3047. * natural logarithm. The inverse is the natural logarithm, SDL_logf.
  3048. *
  3049. * Domain: `-INF <= x <= INF`
  3050. *
  3051. * Range: `0 <= y <= INF`
  3052. *
  3053. * The output will overflow if `exp(x)` is too large to be represented.
  3054. *
  3055. * This function operates on single-precision floating point values, use
  3056. * SDL_exp for double-precision floats.
  3057. *
  3058. * This function may use a different approximation across different versions,
  3059. * platforms and configurations. i.e, it can return a different value given
  3060. * the same input on different machines or operating systems, or if SDL is
  3061. * updated.
  3062. *
  3063. * \param x floating point value.
  3064. * \returns value of `e^x`.
  3065. *
  3066. * \threadsafety It is safe to call this function from any thread.
  3067. *
  3068. * \since This function is available since SDL 3.0.0.
  3069. *
  3070. * \sa SDL_exp
  3071. * \sa SDL_logf
  3072. */
  3073. extern SDL_DECLSPEC float SDLCALL SDL_expf(float x);
  3074. /**
  3075. * Compute the absolute value of `x`
  3076. *
  3077. * Domain: `-INF <= x <= INF`
  3078. *
  3079. * Range: `0 <= y <= INF`
  3080. *
  3081. * This function operates on double-precision floating point values, use
  3082. * SDL_copysignf for single-precision floats.
  3083. *
  3084. * \param x floating point value to use as the magnitude.
  3085. * \returns the absolute value of `x`.
  3086. *
  3087. * \threadsafety It is safe to call this function from any thread.
  3088. *
  3089. * \since This function is available since SDL 3.0.0.
  3090. *
  3091. * \sa SDL_fabsf
  3092. */
  3093. extern SDL_DECLSPEC double SDLCALL SDL_fabs(double x);
  3094. /**
  3095. * Compute the absolute value of `x`
  3096. *
  3097. * Domain: `-INF <= x <= INF`
  3098. *
  3099. * Range: `0 <= y <= INF`
  3100. *
  3101. * This function operates on single-precision floating point values, use
  3102. * SDL_copysignf for double-precision floats.
  3103. *
  3104. * \param x floating point value to use as the magnitude.
  3105. * \returns the absolute value of `x`.
  3106. *
  3107. * \threadsafety It is safe to call this function from any thread.
  3108. *
  3109. * \since This function is available since SDL 3.0.0.
  3110. *
  3111. * \sa SDL_fabs
  3112. */
  3113. extern SDL_DECLSPEC float SDLCALL SDL_fabsf(float x);
  3114. /**
  3115. * Compute the floor of `x`.
  3116. *
  3117. * The floor of `x` is the largest integer `y` such that `y > x`, i.e `x`
  3118. * rounded down to the nearest integer.
  3119. *
  3120. * Domain: `-INF <= x <= INF`
  3121. *
  3122. * Range: `-INF <= y <= INF`, y integer
  3123. *
  3124. * This function operates on double-precision floating point values, use
  3125. * SDL_floorf for single-precision floats.
  3126. *
  3127. * \param x floating point value.
  3128. * \returns the floor of `x`.
  3129. *
  3130. * \threadsafety It is safe to call this function from any thread.
  3131. *
  3132. * \since This function is available since SDL 3.0.0.
  3133. *
  3134. * \sa SDL_floorf
  3135. * \sa SDL_ceil
  3136. * \sa SDL_trunc
  3137. * \sa SDL_round
  3138. * \sa SDL_lround
  3139. */
  3140. extern SDL_DECLSPEC double SDLCALL SDL_floor(double x);
  3141. /**
  3142. * Compute the floor of `x`.
  3143. *
  3144. * The floor of `x` is the largest integer `y` such that `y > x`, i.e `x`
  3145. * rounded down to the nearest integer.
  3146. *
  3147. * Domain: `-INF <= x <= INF`
  3148. *
  3149. * Range: `-INF <= y <= INF`, y integer
  3150. *
  3151. * This function operates on single-precision floating point values, use
  3152. * SDL_floorf for double-precision floats.
  3153. *
  3154. * \param x floating point value.
  3155. * \returns the floor of `x`.
  3156. *
  3157. * \threadsafety It is safe to call this function from any thread.
  3158. *
  3159. * \since This function is available since SDL 3.0.0.
  3160. *
  3161. * \sa SDL_floor
  3162. * \sa SDL_ceilf
  3163. * \sa SDL_truncf
  3164. * \sa SDL_roundf
  3165. * \sa SDL_lroundf
  3166. */
  3167. extern SDL_DECLSPEC float SDLCALL SDL_floorf(float x);
  3168. /**
  3169. * Truncate `x` to an integer.
  3170. *
  3171. * Rounds `x` to the next closest integer to 0. This is equivalent to removing
  3172. * the fractional part of `x`, leaving only the integer part.
  3173. *
  3174. * Domain: `-INF <= x <= INF`
  3175. *
  3176. * Range: `-INF <= y <= INF`, y integer
  3177. *
  3178. * This function operates on double-precision floating point values, use
  3179. * SDL_truncf for single-precision floats.
  3180. *
  3181. * \param x floating point value.
  3182. * \returns `x` truncated to an integer.
  3183. *
  3184. * \threadsafety It is safe to call this function from any thread.
  3185. *
  3186. * \since This function is available since SDL 3.0.0.
  3187. *
  3188. * \sa SDL_truncf
  3189. * \sa SDL_fmod
  3190. * \sa SDL_ceil
  3191. * \sa SDL_floor
  3192. * \sa SDL_round
  3193. * \sa SDL_lround
  3194. */
  3195. extern SDL_DECLSPEC double SDLCALL SDL_trunc(double x);
  3196. /**
  3197. * Truncate `x` to an integer.
  3198. *
  3199. * Rounds `x` to the next closest integer to 0. This is equivalent to removing
  3200. * the fractional part of `x`, leaving only the integer part.
  3201. *
  3202. * Domain: `-INF <= x <= INF`
  3203. *
  3204. * Range: `-INF <= y <= INF`, y integer
  3205. *
  3206. * This function operates on single-precision floating point values, use
  3207. * SDL_truncf for double-precision floats.
  3208. *
  3209. * \param x floating point value.
  3210. * \returns `x` truncated to an integer.
  3211. *
  3212. * \threadsafety It is safe to call this function from any thread.
  3213. *
  3214. * \since This function is available since SDL 3.0.0.
  3215. *
  3216. * \sa SDL_trunc
  3217. * \sa SDL_fmodf
  3218. * \sa SDL_ceilf
  3219. * \sa SDL_floorf
  3220. * \sa SDL_roundf
  3221. * \sa SDL_lroundf
  3222. */
  3223. extern SDL_DECLSPEC float SDLCALL SDL_truncf(float x);
  3224. /**
  3225. * Return the floating-point remainder of `x / y`
  3226. *
  3227. * Divides `x` by `y`, and returns the remainder.
  3228. *
  3229. * Domain: `-INF <= x <= INF`, `-INF <= y <= INF`, `y != 0`
  3230. *
  3231. * Range: `-y <= z <= y`
  3232. *
  3233. * This function operates on double-precision floating point values, use
  3234. * SDL_fmodf for single-precision floats.
  3235. *
  3236. * \param x the numerator.
  3237. * \param y the denominator. Must not be 0.
  3238. * \returns the remainder of `x / y`.
  3239. *
  3240. * \threadsafety It is safe to call this function from any thread.
  3241. *
  3242. * \since This function is available since SDL 3.0.0.
  3243. *
  3244. * \sa SDL_fmodf
  3245. * \sa SDL_modf
  3246. * \sa SDL_trunc
  3247. * \sa SDL_ceil
  3248. * \sa SDL_floor
  3249. * \sa SDL_round
  3250. * \sa SDL_lround
  3251. */
  3252. extern SDL_DECLSPEC double SDLCALL SDL_fmod(double x, double y);
  3253. /**
  3254. * Return the floating-point remainder of `x / y`
  3255. *
  3256. * Divides `x` by `y`, and returns the remainder.
  3257. *
  3258. * Domain: `-INF <= x <= INF`, `-INF <= y <= INF`, `y != 0`
  3259. *
  3260. * Range: `-y <= z <= y`
  3261. *
  3262. * This function operates on single-precision floating point values, use
  3263. * SDL_fmod for single-precision floats.
  3264. *
  3265. * \param x the numerator.
  3266. * \param y the denominator. Must not be 0.
  3267. * \returns the remainder of `x / y`.
  3268. *
  3269. * \threadsafety It is safe to call this function from any thread.
  3270. *
  3271. * \since This function is available since SDL 3.0.0.
  3272. *
  3273. * \sa SDL_fmod
  3274. * \sa SDL_truncf
  3275. * \sa SDL_modff
  3276. * \sa SDL_ceilf
  3277. * \sa SDL_floorf
  3278. * \sa SDL_roundf
  3279. * \sa SDL_lroundf
  3280. */
  3281. extern SDL_DECLSPEC float SDLCALL SDL_fmodf(float x, float y);
  3282. /**
  3283. * Return whether the value is infinity.
  3284. *
  3285. * \param x double-precision floating point value.
  3286. * \returns non-zero if the value is infinity, 0 otherwise.
  3287. *
  3288. * \threadsafety It is safe to call this function from any thread.
  3289. *
  3290. * \since This function is available since SDL 3.0.0.
  3291. *
  3292. * \sa SDL_isinff
  3293. */
  3294. extern SDL_DECLSPEC int SDLCALL SDL_isinf(double x);
  3295. /**
  3296. * Return whether the value is infinity.
  3297. *
  3298. * \param x floating point value.
  3299. * \returns non-zero if the value is infinity, 0 otherwise.
  3300. *
  3301. * \threadsafety It is safe to call this function from any thread.
  3302. *
  3303. * \since This function is available since SDL 3.0.0.
  3304. *
  3305. * \sa SDL_isinf
  3306. */
  3307. extern SDL_DECLSPEC int SDLCALL SDL_isinff(float x);
  3308. /**
  3309. * Return whether the value is NaN.
  3310. *
  3311. * \param x double-precision floating point value.
  3312. * \returns non-zero if the value is NaN, 0 otherwise.
  3313. *
  3314. * \threadsafety It is safe to call this function from any thread.
  3315. *
  3316. * \since This function is available since SDL 3.0.0.
  3317. *
  3318. * \sa SDL_isnanf
  3319. */
  3320. extern SDL_DECLSPEC int SDLCALL SDL_isnan(double x);
  3321. /**
  3322. * Return whether the value is NaN.
  3323. *
  3324. * \param x floating point value.
  3325. * \returns non-zero if the value is NaN, 0 otherwise.
  3326. *
  3327. * \threadsafety It is safe to call this function from any thread.
  3328. *
  3329. * \since This function is available since SDL 3.0.0.
  3330. *
  3331. * \sa SDL_isnan
  3332. */
  3333. extern SDL_DECLSPEC int SDLCALL SDL_isnanf(float x);
  3334. /**
  3335. * Compute the natural logarithm of `x`.
  3336. *
  3337. * Domain: `0 < x <= INF`
  3338. *
  3339. * Range: `-INF <= y <= INF`
  3340. *
  3341. * It is an error for `x` to be less than or equal to 0.
  3342. *
  3343. * This function operates on double-precision floating point values, use
  3344. * SDL_logf for single-precision floats.
  3345. *
  3346. * This function may use a different approximation across different versions,
  3347. * platforms and configurations. i.e, it can return a different value given
  3348. * the same input on different machines or operating systems, or if SDL is
  3349. * updated.
  3350. *
  3351. * \param x floating point value. Must be greater than 0.
  3352. * \returns the natural logarithm of `x`.
  3353. *
  3354. * \threadsafety It is safe to call this function from any thread.
  3355. *
  3356. * \since This function is available since SDL 3.0.0.
  3357. *
  3358. * \sa SDL_logf
  3359. * \sa SDL_log10
  3360. * \sa SDL_exp
  3361. */
  3362. extern SDL_DECLSPEC double SDLCALL SDL_log(double x);
  3363. /**
  3364. * Compute the natural logarithm of `x`.
  3365. *
  3366. * Domain: `0 < x <= INF`
  3367. *
  3368. * Range: `-INF <= y <= INF`
  3369. *
  3370. * It is an error for `x` to be less than or equal to 0.
  3371. *
  3372. * This function operates on single-precision floating point values, use
  3373. * SDL_log for double-precision floats.
  3374. *
  3375. * This function may use a different approximation across different versions,
  3376. * platforms and configurations. i.e, it can return a different value given
  3377. * the same input on different machines or operating systems, or if SDL is
  3378. * updated.
  3379. *
  3380. * \param x floating point value. Must be greater than 0.
  3381. * \returns the natural logarithm of `x`.
  3382. *
  3383. * \threadsafety It is safe to call this function from any thread.
  3384. *
  3385. * \since This function is available since SDL 3.0.0.
  3386. *
  3387. * \sa SDL_log
  3388. * \sa SDL_expf
  3389. */
  3390. extern SDL_DECLSPEC float SDLCALL SDL_logf(float x);
  3391. /**
  3392. * Compute the base-10 logarithm of `x`.
  3393. *
  3394. * Domain: `0 < x <= INF`
  3395. *
  3396. * Range: `-INF <= y <= INF`
  3397. *
  3398. * It is an error for `x` to be less than or equal to 0.
  3399. *
  3400. * This function operates on double-precision floating point values, use
  3401. * SDL_log10f for single-precision floats.
  3402. *
  3403. * This function may use a different approximation across different versions,
  3404. * platforms and configurations. i.e, it can return a different value given
  3405. * the same input on different machines or operating systems, or if SDL is
  3406. * updated.
  3407. *
  3408. * \param x floating point value. Must be greater than 0.
  3409. * \returns the logarithm of `x`.
  3410. *
  3411. * \threadsafety It is safe to call this function from any thread.
  3412. *
  3413. * \since This function is available since SDL 3.0.0.
  3414. *
  3415. * \sa SDL_log10f
  3416. * \sa SDL_log
  3417. * \sa SDL_pow
  3418. */
  3419. extern SDL_DECLSPEC double SDLCALL SDL_log10(double x);
  3420. /**
  3421. * Compute the base-10 logarithm of `x`.
  3422. *
  3423. * Domain: `0 < x <= INF`
  3424. *
  3425. * Range: `-INF <= y <= INF`
  3426. *
  3427. * It is an error for `x` to be less than or equal to 0.
  3428. *
  3429. * This function operates on single-precision floating point values, use
  3430. * SDL_log10 for double-precision floats.
  3431. *
  3432. * This function may use a different approximation across different versions,
  3433. * platforms and configurations. i.e, it can return a different value given
  3434. * the same input on different machines or operating systems, or if SDL is
  3435. * updated.
  3436. *
  3437. * \param x floating point value. Must be greater than 0.
  3438. * \returns the logarithm of `x`.
  3439. *
  3440. * \threadsafety It is safe to call this function from any thread.
  3441. *
  3442. * \since This function is available since SDL 3.0.0.
  3443. *
  3444. * \sa SDL_log10
  3445. * \sa SDL_logf
  3446. * \sa SDL_powf
  3447. */
  3448. extern SDL_DECLSPEC float SDLCALL SDL_log10f(float x);
  3449. /**
  3450. * Split `x` into integer and fractional parts
  3451. *
  3452. * This function operates on double-precision floating point values, use
  3453. * SDL_modff for single-precision floats.
  3454. *
  3455. * \param x floating point value.
  3456. * \param y output pointer to store the integer part of `x`.
  3457. * \returns the fractional part of `x`.
  3458. *
  3459. * \threadsafety It is safe to call this function from any thread.
  3460. *
  3461. * \since This function is available since SDL 3.0.0.
  3462. *
  3463. * \sa SDL_modff
  3464. * \sa SDL_trunc
  3465. * \sa SDL_fmod
  3466. */
  3467. extern SDL_DECLSPEC double SDLCALL SDL_modf(double x, double *y);
  3468. /**
  3469. * Split `x` into integer and fractional parts
  3470. *
  3471. * This function operates on single-precision floating point values, use
  3472. * SDL_modf for double-precision floats.
  3473. *
  3474. * \param x floating point value.
  3475. * \param y output pointer to store the integer part of `x`.
  3476. * \returns the fractional part of `x`.
  3477. *
  3478. * \threadsafety It is safe to call this function from any thread.
  3479. *
  3480. * \since This function is available since SDL 3.0.0.
  3481. *
  3482. * \sa SDL_modf
  3483. * \sa SDL_truncf
  3484. * \sa SDL_fmodf
  3485. */
  3486. extern SDL_DECLSPEC float SDLCALL SDL_modff(float x, float *y);
  3487. /**
  3488. * Raise `x` to the power `y`
  3489. *
  3490. * Domain: `-INF <= x <= INF`, `-INF <= y <= INF`
  3491. *
  3492. * Range: `-INF <= z <= INF`
  3493. *
  3494. * If `y` is the base of the natural logarithm (e), consider using SDL_exp
  3495. * instead.
  3496. *
  3497. * This function operates on double-precision floating point values, use
  3498. * SDL_powf for single-precision floats.
  3499. *
  3500. * This function may use a different approximation across different versions,
  3501. * platforms and configurations. i.e, it can return a different value given
  3502. * the same input on different machines or operating systems, or if SDL is
  3503. * updated.
  3504. *
  3505. * \param x the base.
  3506. * \param y the exponent.
  3507. * \returns `x` raised to the power `y`.
  3508. *
  3509. * \threadsafety It is safe to call this function from any thread.
  3510. *
  3511. * \since This function is available since SDL 3.0.0.
  3512. *
  3513. * \sa SDL_powf
  3514. * \sa SDL_exp
  3515. * \sa SDL_log
  3516. */
  3517. extern SDL_DECLSPEC double SDLCALL SDL_pow(double x, double y);
  3518. /**
  3519. * Raise `x` to the power `y`
  3520. *
  3521. * Domain: `-INF <= x <= INF`, `-INF <= y <= INF`
  3522. *
  3523. * Range: `-INF <= z <= INF`
  3524. *
  3525. * If `y` is the base of the natural logarithm (e), consider using SDL_exp
  3526. * instead.
  3527. *
  3528. * This function operates on single-precision floating point values, use
  3529. * SDL_powf for double-precision floats.
  3530. *
  3531. * This function may use a different approximation across different versions,
  3532. * platforms and configurations. i.e, it can return a different value given
  3533. * the same input on different machines or operating systems, or if SDL is
  3534. * updated.
  3535. *
  3536. * \param x the base.
  3537. * \param y the exponent.
  3538. * \returns `x` raised to the power `y`.
  3539. *
  3540. * \threadsafety It is safe to call this function from any thread.
  3541. *
  3542. * \since This function is available since SDL 3.0.0.
  3543. *
  3544. * \sa SDL_pow
  3545. * \sa SDL_expf
  3546. * \sa SDL_logf
  3547. */
  3548. extern SDL_DECLSPEC float SDLCALL SDL_powf(float x, float y);
  3549. /**
  3550. * Round `x` to the nearest integer.
  3551. *
  3552. * Rounds `x` to the nearest integer. Values halfway between integers will be
  3553. * rounded away from zero.
  3554. *
  3555. * Domain: `-INF <= x <= INF`
  3556. *
  3557. * Range: `-INF <= y <= INF`, y integer
  3558. *
  3559. * This function operates on double-precision floating point values, use
  3560. * SDL_roundf for single-precision floats. To get the result as an integer
  3561. * type, use SDL_lround.
  3562. *
  3563. * \param x floating point value.
  3564. * \returns the nearest integer to `x`.
  3565. *
  3566. * \threadsafety It is safe to call this function from any thread.
  3567. *
  3568. * \since This function is available since SDL 3.0.0.
  3569. *
  3570. * \sa SDL_roundf
  3571. * \sa SDL_lround
  3572. * \sa SDL_floor
  3573. * \sa SDL_ceil
  3574. * \sa SDL_trunc
  3575. */
  3576. extern SDL_DECLSPEC double SDLCALL SDL_round(double x);
  3577. /**
  3578. * Round `x` to the nearest integer.
  3579. *
  3580. * Rounds `x` to the nearest integer. Values halfway between integers will be
  3581. * rounded away from zero.
  3582. *
  3583. * Domain: `-INF <= x <= INF`
  3584. *
  3585. * Range: `-INF <= y <= INF`, y integer
  3586. *
  3587. * This function operates on double-precision floating point values, use
  3588. * SDL_roundf for single-precision floats. To get the result as an integer
  3589. * type, use SDL_lroundf.
  3590. *
  3591. * \param x floating point value.
  3592. * \returns the nearest integer to `x`.
  3593. *
  3594. * \threadsafety It is safe to call this function from any thread.
  3595. *
  3596. * \since This function is available since SDL 3.0.0.
  3597. *
  3598. * \sa SDL_round
  3599. * \sa SDL_lroundf
  3600. * \sa SDL_floorf
  3601. * \sa SDL_ceilf
  3602. * \sa SDL_truncf
  3603. */
  3604. extern SDL_DECLSPEC float SDLCALL SDL_roundf(float x);
  3605. /**
  3606. * Round `x` to the nearest integer representable as a long
  3607. *
  3608. * Rounds `x` to the nearest integer. Values halfway between integers will be
  3609. * rounded away from zero.
  3610. *
  3611. * Domain: `-INF <= x <= INF`
  3612. *
  3613. * Range: `MIN_LONG <= y <= MAX_LONG`
  3614. *
  3615. * This function operates on double-precision floating point values, use
  3616. * SDL_lround for single-precision floats. To get the result as a
  3617. * floating-point type, use SDL_round.
  3618. *
  3619. * \param x floating point value.
  3620. * \returns the nearest integer to `x`.
  3621. *
  3622. * \threadsafety It is safe to call this function from any thread.
  3623. *
  3624. * \since This function is available since SDL 3.0.0.
  3625. *
  3626. * \sa SDL_lroundf
  3627. * \sa SDL_round
  3628. * \sa SDL_floor
  3629. * \sa SDL_ceil
  3630. * \sa SDL_trunc
  3631. */
  3632. extern SDL_DECLSPEC long SDLCALL SDL_lround(double x);
  3633. /**
  3634. * Round `x` to the nearest integer representable as a long
  3635. *
  3636. * Rounds `x` to the nearest integer. Values halfway between integers will be
  3637. * rounded away from zero.
  3638. *
  3639. * Domain: `-INF <= x <= INF`
  3640. *
  3641. * Range: `MIN_LONG <= y <= MAX_LONG`
  3642. *
  3643. * This function operates on single-precision floating point values, use
  3644. * SDL_lroundf for double-precision floats. To get the result as a
  3645. * floating-point type, use SDL_roundf,
  3646. *
  3647. * \param x floating point value.
  3648. * \returns the nearest integer to `x`.
  3649. *
  3650. * \threadsafety It is safe to call this function from any thread.
  3651. *
  3652. * \since This function is available since SDL 3.0.0.
  3653. *
  3654. * \sa SDL_lround
  3655. * \sa SDL_roundf
  3656. * \sa SDL_floorf
  3657. * \sa SDL_ceilf
  3658. * \sa SDL_truncf
  3659. */
  3660. extern SDL_DECLSPEC long SDLCALL SDL_lroundf(float x);
  3661. /**
  3662. * Scale `x` by an integer power of two.
  3663. *
  3664. * Multiplies `x` by the `n`th power of the floating point radix (always 2).
  3665. *
  3666. * Domain: `-INF <= x <= INF`, `n` integer
  3667. *
  3668. * Range: `-INF <= y <= INF`
  3669. *
  3670. * This function operates on double-precision floating point values, use
  3671. * SDL_scalbnf for single-precision floats.
  3672. *
  3673. * \param x floating point value to be scaled.
  3674. * \param n integer exponent.
  3675. * \returns `x * 2^n`.
  3676. *
  3677. * \threadsafety It is safe to call this function from any thread.
  3678. *
  3679. * \since This function is available since SDL 3.0.0.
  3680. *
  3681. * \sa SDL_scalbnf
  3682. * \sa SDL_pow
  3683. */
  3684. extern SDL_DECLSPEC double SDLCALL SDL_scalbn(double x, int n);
  3685. /**
  3686. * Scale `x` by an integer power of two.
  3687. *
  3688. * Multiplies `x` by the `n`th power of the floating point radix (always 2).
  3689. *
  3690. * Domain: `-INF <= x <= INF`, `n` integer
  3691. *
  3692. * Range: `-INF <= y <= INF`
  3693. *
  3694. * This function operates on single-precision floating point values, use
  3695. * SDL_scalbn for double-precision floats.
  3696. *
  3697. * \param x floating point value to be scaled.
  3698. * \param n integer exponent.
  3699. * \returns `x * 2^n`.
  3700. *
  3701. * \threadsafety It is safe to call this function from any thread.
  3702. *
  3703. * \since This function is available since SDL 3.0.0.
  3704. *
  3705. * \sa SDL_scalbn
  3706. * \sa SDL_powf
  3707. */
  3708. extern SDL_DECLSPEC float SDLCALL SDL_scalbnf(float x, int n);
  3709. /**
  3710. * Compute the sine of `x`.
  3711. *
  3712. * Domain: `-INF <= x <= INF`
  3713. *
  3714. * Range: `-1 <= y <= 1`
  3715. *
  3716. * This function operates on double-precision floating point values, use
  3717. * SDL_sinf for single-precision floats.
  3718. *
  3719. * This function may use a different approximation across different versions,
  3720. * platforms and configurations. i.e, it can return a different value given
  3721. * the same input on different machines or operating systems, or if SDL is
  3722. * updated.
  3723. *
  3724. * \param x floating point value, in radians.
  3725. * \returns sine of `x`.
  3726. *
  3727. * \threadsafety It is safe to call this function from any thread.
  3728. *
  3729. * \since This function is available since SDL 3.0.0.
  3730. *
  3731. * \sa SDL_sinf
  3732. * \sa SDL_asin
  3733. * \sa SDL_cos
  3734. */
  3735. extern SDL_DECLSPEC double SDLCALL SDL_sin(double x);
  3736. /**
  3737. * Compute the sine of `x`.
  3738. *
  3739. * Domain: `-INF <= x <= INF`
  3740. *
  3741. * Range: `-1 <= y <= 1`
  3742. *
  3743. * This function operates on single-precision floating point values, use
  3744. * SDL_sinf for double-precision floats.
  3745. *
  3746. * This function may use a different approximation across different versions,
  3747. * platforms and configurations. i.e, it can return a different value given
  3748. * the same input on different machines or operating systems, or if SDL is
  3749. * updated.
  3750. *
  3751. * \param x floating point value, in radians.
  3752. * \returns sine of `x`.
  3753. *
  3754. * \threadsafety It is safe to call this function from any thread.
  3755. *
  3756. * \since This function is available since SDL 3.0.0.
  3757. *
  3758. * \sa SDL_sin
  3759. * \sa SDL_asinf
  3760. * \sa SDL_cosf
  3761. */
  3762. extern SDL_DECLSPEC float SDLCALL SDL_sinf(float x);
  3763. /**
  3764. * Compute the square root of `x`.
  3765. *
  3766. * Domain: `0 <= x <= INF`
  3767. *
  3768. * Range: `0 <= y <= INF`
  3769. *
  3770. * This function operates on double-precision floating point values, use
  3771. * SDL_sqrtf for single-precision floats.
  3772. *
  3773. * This function may use a different approximation across different versions,
  3774. * platforms and configurations. i.e, it can return a different value given
  3775. * the same input on different machines or operating systems, or if SDL is
  3776. * updated.
  3777. *
  3778. * \param x floating point value. Must be greater than or equal to 0.
  3779. * \returns square root of `x`.
  3780. *
  3781. * \threadsafety It is safe to call this function from any thread.
  3782. *
  3783. * \since This function is available since SDL 3.0.0.
  3784. *
  3785. * \sa SDL_sqrtf
  3786. */
  3787. extern SDL_DECLSPEC double SDLCALL SDL_sqrt(double x);
  3788. /**
  3789. * Compute the square root of `x`.
  3790. *
  3791. * Domain: `0 <= x <= INF`
  3792. *
  3793. * Range: `0 <= y <= INF`
  3794. *
  3795. * This function operates on single-precision floating point values, use
  3796. * SDL_sqrt for double-precision floats.
  3797. *
  3798. * This function may use a different approximation across different versions,
  3799. * platforms and configurations. i.e, it can return a different value given
  3800. * the same input on different machines or operating systems, or if SDL is
  3801. * updated.
  3802. *
  3803. * \param x floating point value. Must be greater than or equal to 0.
  3804. * \returns square root of `x`.
  3805. *
  3806. * \threadsafety It is safe to call this function from any thread.
  3807. *
  3808. * \since This function is available since SDL 3.0.0.
  3809. *
  3810. * \sa SDL_sqrt
  3811. */
  3812. extern SDL_DECLSPEC float SDLCALL SDL_sqrtf(float x);
  3813. /**
  3814. * Compute the tangent of `x`.
  3815. *
  3816. * Domain: `-INF <= x <= INF`
  3817. *
  3818. * Range: `-INF <= y <= INF`
  3819. *
  3820. * This function operates on double-precision floating point values, use
  3821. * SDL_tanf for single-precision floats.
  3822. *
  3823. * This function may use a different approximation across different versions,
  3824. * platforms and configurations. i.e, it can return a different value given
  3825. * the same input on different machines or operating systems, or if SDL is
  3826. * updated.
  3827. *
  3828. * \param x floating point value, in radians.
  3829. * \returns tangent of `x`.
  3830. *
  3831. * \threadsafety It is safe to call this function from any thread.
  3832. *
  3833. * \since This function is available since SDL 3.0.0.
  3834. *
  3835. * \sa SDL_tanf
  3836. * \sa SDL_sin
  3837. * \sa SDL_cos
  3838. * \sa SDL_atan
  3839. * \sa SDL_atan2
  3840. */
  3841. extern SDL_DECLSPEC double SDLCALL SDL_tan(double x);
  3842. /**
  3843. * Compute the tangent of `x`.
  3844. *
  3845. * Domain: `-INF <= x <= INF`
  3846. *
  3847. * Range: `-INF <= y <= INF`
  3848. *
  3849. * This function operates on single-precision floating point values, use
  3850. * SDL_tanf for double-precision floats.
  3851. *
  3852. * This function may use a different approximation across different versions,
  3853. * platforms and configurations. i.e, it can return a different value given
  3854. * the same input on different machines or operating systems, or if SDL is
  3855. * updated.
  3856. *
  3857. * \param x floating point value, in radians.
  3858. * \returns tangent of `x`.
  3859. *
  3860. * \threadsafety It is safe to call this function from any thread.
  3861. *
  3862. * \since This function is available since SDL 3.0.0.
  3863. *
  3864. * \sa SDL_tan
  3865. * \sa SDL_sinf
  3866. * \sa SDL_cosf
  3867. * \sa SDL_atanf
  3868. * \sa SDL_atan2f
  3869. */
  3870. extern SDL_DECLSPEC float SDLCALL SDL_tanf(float x);
  3871. /* The SDL implementation of iconv() returns these error codes */
  3872. #define SDL_ICONV_ERROR (size_t)-1
  3873. #define SDL_ICONV_E2BIG (size_t)-2
  3874. #define SDL_ICONV_EILSEQ (size_t)-3
  3875. #define SDL_ICONV_EINVAL (size_t)-4
  3876. typedef struct SDL_iconv_data_t *SDL_iconv_t;
  3877. /**
  3878. * This function allocates a context for the specified character set
  3879. * conversion.
  3880. *
  3881. * \param tocode The target character encoding, must not be NULL.
  3882. * \param fromcode The source character encoding, must not be NULL.
  3883. * \returns a handle that must be freed with SDL_iconv_close, or
  3884. * SDL_ICONV_ERROR on failure.
  3885. *
  3886. * \since This function is available since SDL 3.0.0.
  3887. *
  3888. * \sa SDL_iconv
  3889. * \sa SDL_iconv_close
  3890. * \sa SDL_iconv_string
  3891. */
  3892. extern SDL_DECLSPEC SDL_iconv_t SDLCALL SDL_iconv_open(const char *tocode,
  3893. const char *fromcode);
  3894. /**
  3895. * This function frees a context used for character set conversion.
  3896. *
  3897. * \param cd The character set conversion handle.
  3898. * \returns 0 on success, or -1 on failure.
  3899. *
  3900. * \since This function is available since SDL 3.0.0.
  3901. *
  3902. * \sa SDL_iconv
  3903. * \sa SDL_iconv_open
  3904. * \sa SDL_iconv_string
  3905. */
  3906. extern SDL_DECLSPEC int SDLCALL SDL_iconv_close(SDL_iconv_t cd);
  3907. /**
  3908. * This function converts text between encodings, reading from and writing to
  3909. * a buffer.
  3910. *
  3911. * It returns the number of succesful conversions.
  3912. *
  3913. * \param cd The character set conversion context, created in
  3914. * SDL_iconv_open().
  3915. * \param inbuf Address of variable that points to the first character of the
  3916. * input sequence.
  3917. * \param inbytesleft The number of bytes in the input buffer.
  3918. * \param outbuf Address of variable that points to the output buffer.
  3919. * \param outbytesleft The number of bytes in the output buffer.
  3920. * \returns the number of conversions on success, else SDL_ICONV_E2BIG is
  3921. * returned when the output buffer is too small, or SDL_ICONV_EILSEQ
  3922. * is returned when an invalid input sequence is encountered, or
  3923. * SDL_ICONV_EINVAL is returned when an incomplete input sequence is
  3924. * encountered.
  3925. *
  3926. * On exit:
  3927. *
  3928. * - inbuf will point to the beginning of the next multibyte
  3929. * sequence. On error, this is the location of the problematic
  3930. * input sequence. On success, this is the end of the input
  3931. * sequence. - inbytesleft will be set to the number of bytes left
  3932. * to convert, which will be 0 on success. - outbuf will point to
  3933. * the location where to store the next output byte. - outbytesleft
  3934. * will be set to the number of bytes left in the output buffer.
  3935. *
  3936. * \since This function is available since SDL 3.0.0.
  3937. *
  3938. * \sa SDL_iconv_open
  3939. * \sa SDL_iconv_close
  3940. * \sa SDL_iconv_string
  3941. */
  3942. extern SDL_DECLSPEC size_t SDLCALL SDL_iconv(SDL_iconv_t cd, const char **inbuf,
  3943. size_t *inbytesleft, char **outbuf,
  3944. size_t *outbytesleft);
  3945. /**
  3946. * Helper function to convert a string's encoding in one call.
  3947. *
  3948. * This function converts a buffer or string between encodings in one pass.
  3949. *
  3950. * The string does not need to be NULL-terminated; this function operates on
  3951. * the number of bytes specified in `inbytesleft` whether there is a NULL
  3952. * character anywhere in the buffer.
  3953. *
  3954. * The returned string is owned by the caller, and should be passed to
  3955. * SDL_free when no longer needed.
  3956. *
  3957. * \param tocode the character encoding of the output string. Examples are
  3958. * "UTF-8", "UCS-4", etc.
  3959. * \param fromcode the character encoding of data in `inbuf`.
  3960. * \param inbuf the string to convert to a different encoding.
  3961. * \param inbytesleft the size of the input string _in bytes_.
  3962. * \returns a new string, converted to the new encoding, or NULL on error.
  3963. *
  3964. * \since This function is available since SDL 3.0.0.
  3965. *
  3966. * \sa SDL_iconv_open
  3967. * \sa SDL_iconv_close
  3968. * \sa SDL_iconv
  3969. */
  3970. extern SDL_DECLSPEC char * SDLCALL SDL_iconv_string(const char *tocode,
  3971. const char *fromcode,
  3972. const char *inbuf,
  3973. size_t inbytesleft);
  3974. /* Some helper macros for common cases... */
  3975. #define SDL_iconv_utf8_locale(S) SDL_iconv_string("", "UTF-8", S, SDL_strlen(S)+1)
  3976. #define SDL_iconv_utf8_ucs2(S) (Uint16 *)SDL_iconv_string("UCS-2", "UTF-8", S, SDL_strlen(S)+1)
  3977. #define SDL_iconv_utf8_ucs4(S) (Uint32 *)SDL_iconv_string("UCS-4", "UTF-8", S, SDL_strlen(S)+1)
  3978. #define SDL_iconv_wchar_utf8(S) SDL_iconv_string("UTF-8", "WCHAR_T", (char *)S, (SDL_wcslen(S)+1)*sizeof(wchar_t))
  3979. /* force builds using Clang's static analysis tools to use literal C runtime
  3980. here, since there are possibly tests that are ineffective otherwise. */
  3981. #if defined(__clang_analyzer__) && !defined(SDL_DISABLE_ANALYZE_MACROS)
  3982. /* The analyzer knows about strlcpy even when the system doesn't provide it */
  3983. #if !defined(HAVE_STRLCPY) && !defined(strlcpy)
  3984. size_t strlcpy(char *dst, const char *src, size_t size);
  3985. #endif
  3986. /* The analyzer knows about strlcat even when the system doesn't provide it */
  3987. #if !defined(HAVE_STRLCAT) && !defined(strlcat)
  3988. size_t strlcat(char *dst, const char *src, size_t size);
  3989. #endif
  3990. #if !defined(HAVE_WCSLCPY) && !defined(wcslcpy)
  3991. size_t wcslcpy(wchar_t *dst, const wchar_t *src, size_t size);
  3992. #endif
  3993. #if !defined(HAVE_WCSLCAT) && !defined(wcslcat)
  3994. size_t wcslcat(wchar_t *dst, const wchar_t *src, size_t size);
  3995. #endif
  3996. /* Starting LLVM 16, the analyser errors out if these functions do not have
  3997. their prototype defined (clang-diagnostic-implicit-function-declaration) */
  3998. #include <stdio.h>
  3999. #include <stdlib.h>
  4000. #include <strings.h>
  4001. #define SDL_malloc malloc
  4002. #define SDL_calloc calloc
  4003. #define SDL_realloc realloc
  4004. #define SDL_free free
  4005. #ifndef SDL_memcpy
  4006. #define SDL_memcpy memcpy
  4007. #endif
  4008. #ifndef SDL_memmove
  4009. #define SDL_memmove memmove
  4010. #endif
  4011. #ifndef SDL_memset
  4012. #define SDL_memset memset
  4013. #endif
  4014. #define SDL_memcmp memcmp
  4015. #define SDL_strlcpy strlcpy
  4016. #define SDL_strlcat strlcat
  4017. #define SDL_strlen strlen
  4018. #define SDL_wcslen wcslen
  4019. #define SDL_wcslcpy wcslcpy
  4020. #define SDL_wcslcat wcslcat
  4021. #define SDL_strdup strdup
  4022. #define SDL_wcsdup wcsdup
  4023. #define SDL_strchr strchr
  4024. #define SDL_strrchr strrchr
  4025. #define SDL_strstr strstr
  4026. #define SDL_wcsstr wcsstr
  4027. #define SDL_strtok_r strtok_r
  4028. #define SDL_strcmp strcmp
  4029. #define SDL_wcscmp wcscmp
  4030. #define SDL_strncmp strncmp
  4031. #define SDL_wcsncmp wcsncmp
  4032. #define SDL_strcasecmp strcasecmp
  4033. #define SDL_strncasecmp strncasecmp
  4034. #define SDL_strpbrk strpbrk
  4035. #define SDL_sscanf sscanf
  4036. #define SDL_vsscanf vsscanf
  4037. #define SDL_snprintf snprintf
  4038. #define SDL_vsnprintf vsnprintf
  4039. #endif
  4040. /**
  4041. * Multiply two integers, checking for overflow.
  4042. *
  4043. * If `a * b` would overflow, return false.
  4044. *
  4045. * Otherwise store `a * b` via ret and return true.
  4046. *
  4047. * \param a the multiplicand.
  4048. * \param b the multiplier.
  4049. * \param ret on non-overflow output, stores the multiplication result, may
  4050. * not be NULL.
  4051. * \returns false on overflow, true if result is multiplied without overflow.
  4052. *
  4053. * \threadsafety It is safe to call this function from any thread.
  4054. *
  4055. * \since This function is available since SDL 3.0.0.
  4056. */
  4057. SDL_FORCE_INLINE bool SDL_size_mul_check_overflow(size_t a, size_t b, size_t *ret)
  4058. {
  4059. if (a != 0 && b > SDL_SIZE_MAX / a) {
  4060. return false;
  4061. }
  4062. *ret = a * b;
  4063. return true;
  4064. }
  4065. #ifndef SDL_WIKI_DOCUMENTATION_SECTION
  4066. #if SDL_HAS_BUILTIN(__builtin_mul_overflow)
  4067. /* This needs to be wrapped in an inline rather than being a direct #define,
  4068. * because __builtin_mul_overflow() is type-generic, but we want to be
  4069. * consistent about interpreting a and b as size_t. */
  4070. SDL_FORCE_INLINE bool SDL_size_mul_check_overflow_builtin(size_t a, size_t b, size_t *ret)
  4071. {
  4072. return (__builtin_mul_overflow(a, b, ret) == 0);
  4073. }
  4074. #define SDL_size_mul_check_overflow(a, b, ret) SDL_size_mul_check_overflow_builtin(a, b, ret)
  4075. #endif
  4076. #endif
  4077. /**
  4078. * Add two integers, checking for overflow.
  4079. *
  4080. * If `a + b` would overflow, return -1.
  4081. *
  4082. * Otherwise store `a + b` via ret and return 0.
  4083. *
  4084. * \param a the first addend.
  4085. * \param b the second addend.
  4086. * \param ret on non-overflow output, stores the addition result, may not be
  4087. * NULL.
  4088. * \returns false on overflow, true if result is added without overflow.
  4089. *
  4090. * \threadsafety It is safe to call this function from any thread.
  4091. *
  4092. * \since This function is available since SDL 3.0.0.
  4093. */
  4094. SDL_FORCE_INLINE bool SDL_size_add_check_overflow(size_t a, size_t b, size_t *ret)
  4095. {
  4096. if (b > SDL_SIZE_MAX - a) {
  4097. return false;
  4098. }
  4099. *ret = a + b;
  4100. return true;
  4101. }
  4102. #ifndef SDL_WIKI_DOCUMENTATION_SECTION
  4103. #if SDL_HAS_BUILTIN(__builtin_add_overflow)
  4104. /* This needs to be wrapped in an inline rather than being a direct #define,
  4105. * the same as the call to __builtin_mul_overflow() above. */
  4106. SDL_FORCE_INLINE bool SDL_size_add_check_overflow_builtin(size_t a, size_t b, size_t *ret)
  4107. {
  4108. return (__builtin_add_overflow(a, b, ret) == 0);
  4109. }
  4110. #define SDL_size_add_check_overflow(a, b, ret) SDL_size_add_check_overflow_builtin(a, b, ret)
  4111. #endif
  4112. #endif
  4113. /* This is a generic function pointer which should be cast to the type you expect */
  4114. #ifdef SDL_WIKI_DOCUMENTATION_SECTION
  4115. /**
  4116. * A generic function pointer.
  4117. *
  4118. * In theory, generic function pointers should use this, instead of `void *`,
  4119. * since some platforms could treat code addresses differently than data
  4120. * addresses. Although in current times no popular platforms make this
  4121. * distinction, it is more correct and portable to use the correct type for a
  4122. * generic pointer.
  4123. *
  4124. * If for some reason you need to force this typedef to be an actual `void *`,
  4125. * perhaps to work around a compiler or existing code, you can define
  4126. * `SDL_FUNCTION_POINTER_IS_VOID_POINTER` before including any SDL headers.
  4127. *
  4128. * \since This datatype is available since SDL 3.0.0.
  4129. */
  4130. typedef void (*SDL_FunctionPointer)(void);
  4131. #elif defined(SDL_FUNCTION_POINTER_IS_VOID_POINTER)
  4132. typedef void *SDL_FunctionPointer;
  4133. #else
  4134. typedef void (*SDL_FunctionPointer)(void);
  4135. #endif
  4136. /* Ends C function definitions when using C++ */
  4137. #ifdef __cplusplus
  4138. }
  4139. #endif
  4140. #include <SDL3/SDL_close_code.h>
  4141. #endif /* SDL_stdinc_h_ */