SDL_stdinc.h 197 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /**
  19. * # CategoryStdinc
  20. *
  21. * This is a general header that includes C language support. It implements a
  22. * subset of the C runtime APIs, but with an `SDL_` prefix. For most common
  23. * use cases, these should behave the same way as their C runtime equivalents,
  24. * but they may differ in how or whether they handle certain edge cases. When
  25. * in doubt, consult the documentation for details.
  26. */
  27. #ifndef SDL_stdinc_h_
  28. #define SDL_stdinc_h_
  29. #include <SDL3/SDL_platform_defines.h>
  30. #include <stdarg.h>
  31. #include <stdint.h>
  32. #include <string.h>
  33. #include <wchar.h>
  34. #if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || \
  35. defined(SDL_INCLUDE_INTTYPES_H)
  36. #include <inttypes.h>
  37. #endif
  38. #ifndef __cplusplus
  39. #if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || \
  40. (defined(_MSC_VER) && (_MSC_VER >= 1910 /* Visual Studio 2017 */)) || \
  41. defined(SDL_INCLUDE_STDBOOL_H)
  42. #include <stdbool.h>
  43. #elif !defined(__bool_true_false_are_defined) && !defined(bool)
  44. #define bool unsigned char
  45. #define false 0
  46. #define true 1
  47. #define __bool_true_false_are_defined 1
  48. #endif
  49. #endif /* !__cplusplus */
  50. #ifndef SDL_DISABLE_ALLOCA
  51. # ifndef alloca
  52. # ifdef HAVE_ALLOCA_H
  53. # include <alloca.h>
  54. # elif defined(SDL_PLATFORM_NETBSD)
  55. # if defined(__STRICT_ANSI__)
  56. # define SDL_DISABLE_ALLOCA
  57. # else
  58. # include <stdlib.h>
  59. # endif
  60. # elif defined(__GNUC__)
  61. # define alloca __builtin_alloca
  62. # elif defined(_MSC_VER)
  63. # include <malloc.h>
  64. # define alloca _alloca
  65. # elif defined(__WATCOMC__)
  66. # include <malloc.h>
  67. # elif defined(__BORLANDC__)
  68. # include <malloc.h>
  69. # elif defined(__DMC__)
  70. # include <stdlib.h>
  71. # elif defined(SDL_PLATFORM_AIX)
  72. # pragma alloca
  73. # elif defined(__MRC__)
  74. void *alloca(unsigned);
  75. # else
  76. void *alloca(size_t);
  77. # endif
  78. # endif
  79. #endif
  80. #ifdef SDL_WIKI_DOCUMENTATION_SECTION
  81. /**
  82. * The largest value that a `size_t` can hold for the target platform.
  83. *
  84. * `size_t` is generally the same size as a pointer in modern times, but this
  85. * can get weird on very old and very esoteric machines. For example, on a
  86. * 16-bit Intel 286, you might have a 32-bit "far" pointer (16-bit segment
  87. * plus 16-bit offset), but `size_t` is 16 bits, because it can only deal with
  88. * the offset into an individual segment.
  89. *
  90. * In modern times, it's generally expected to cover an entire linear address
  91. * space. But be careful!
  92. *
  93. * \since This macro is available since SDL 3.1.3.
  94. */
  95. #define SDL_SIZE_MAX SIZE_MAX
  96. #elif defined(SIZE_MAX)
  97. # define SDL_SIZE_MAX SIZE_MAX
  98. #else
  99. # define SDL_SIZE_MAX ((size_t) -1)
  100. #endif
  101. #ifndef SDL_COMPILE_TIME_ASSERT
  102. #ifdef SDL_WIKI_DOCUMENTATION_SECTION
  103. /**
  104. * A compile-time assertion.
  105. *
  106. * This can check constant values _known to the compiler at build time_ for
  107. * correctness, and end the compile with the error if they fail.
  108. *
  109. * Often times these are used to verify basic truths, like the size of a
  110. * datatype is what is expected:
  111. *
  112. * ```c
  113. * SDL_COMPILE_TIME_ASSERT(uint32_size, sizeof(Uint32) == 4);
  114. * ```
  115. *
  116. * The `name` parameter must be a valid C symbol, and must be unique across
  117. * all compile-time asserts in the same compilation unit (one run of the
  118. * compiler), or the build might fail with cryptic errors on some targets.
  119. * This is used with a C language trick that works on older compilers that
  120. * don't support better assertion techniques.
  121. *
  122. * If you need an assertion that operates at runtime, on variable data, you
  123. * should try SDL_assert instead.
  124. *
  125. * \param name a unique identifier for this assertion.
  126. * \param x the value to test. Must be a boolean value.
  127. *
  128. * \threadsafety This macro doesn't generate any code to run.
  129. *
  130. * \since This macro is available since SDL 3.1.3.
  131. *
  132. * \sa SDL_assert
  133. */
  134. #define SDL_COMPILE_TIME_ASSERT(name, x) FailToCompileIf_x_IsFalse(x)
  135. #elif defined(__cplusplus)
  136. /* Keep C++ case alone: Some versions of gcc will define __STDC_VERSION__ even when compiling in C++ mode. */
  137. #if (__cplusplus >= 201103L)
  138. #define SDL_COMPILE_TIME_ASSERT(name, x) static_assert(x, #x)
  139. #endif
  140. #elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 202311L)
  141. #define SDL_COMPILE_TIME_ASSERT(name, x) static_assert(x, #x)
  142. #elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
  143. #define SDL_COMPILE_TIME_ASSERT(name, x) _Static_assert(x, #x)
  144. #endif
  145. #endif /* !SDL_COMPILE_TIME_ASSERT */
  146. #ifndef SDL_COMPILE_TIME_ASSERT
  147. /* universal, but may trigger -Wunused-local-typedefs */
  148. #define SDL_COMPILE_TIME_ASSERT(name, x) \
  149. typedef int SDL_compile_time_assert_ ## name[(x) * 2 - 1]
  150. #endif
  151. /**
  152. * The number of elements in a static array.
  153. *
  154. * This will compile but return incorrect results for a pointer to an array;
  155. * it has to be an array the compiler knows the size of.
  156. *
  157. * This macro looks like it double-evaluates the argument, but it does so
  158. * inside of `sizeof`, so there are no side-effects here, as expressions do
  159. * not actually run any code in these cases.
  160. *
  161. * \since This macro is available since SDL 3.1.3.
  162. */
  163. #define SDL_arraysize(array) (sizeof(array)/sizeof(array[0]))
  164. /**
  165. * Macro useful for building other macros with strings in them.
  166. *
  167. * For example:
  168. *
  169. * ```c
  170. * #define LOG_ERROR(X) OutputDebugString(SDL_STRINGIFY_ARG(__FUNCTION__) ": " X "\n")`
  171. * ```
  172. *
  173. * \param arg the text to turn into a string literal.
  174. *
  175. * \since This macro is available since SDL 3.1.3.
  176. */
  177. #define SDL_STRINGIFY_ARG(arg) #arg
  178. /**
  179. * \name Cast operators
  180. *
  181. * Use proper C++ casts when compiled as C++ to be compatible with the option
  182. * -Wold-style-cast of GCC (and -Werror=old-style-cast in GCC 4.2 and above).
  183. */
  184. /* @{ */
  185. #ifdef SDL_WIKI_DOCUMENTATION_SECTION
  186. /**
  187. * Handle a Reinterpret Cast properly whether using C or C++.
  188. *
  189. * If compiled as C++, this macro offers a proper C++ reinterpret_cast<>.
  190. *
  191. * If compiled as C, this macro does a normal C-style cast.
  192. *
  193. * This is helpful to avoid compiler warnings in C++.
  194. *
  195. * \param type the type to cast the expression to.
  196. * \param expression the expression to cast to a different type.
  197. * \returns `expression`, cast to `type`.
  198. *
  199. * \threadsafety It is safe to call this macro from any thread.
  200. *
  201. * \since This macro is available since SDL 3.1.3.
  202. *
  203. * \sa SDL_static_cast
  204. * \sa SDL_const_cast
  205. */
  206. #define SDL_reinterpret_cast(type, expression) reinterpret_cast<type>(expression) /* or `((type)(expression))` in C */
  207. /**
  208. * Handle a Static Cast properly whether using C or C++.
  209. *
  210. * If compiled as C++, this macro offers a proper C++ static_cast<>.
  211. *
  212. * If compiled as C, this macro does a normal C-style cast.
  213. *
  214. * This is helpful to avoid compiler warnings in C++.
  215. *
  216. * \param type the type to cast the expression to.
  217. * \param expression the expression to cast to a different type.
  218. * \returns `expression`, cast to `type`.
  219. *
  220. * \threadsafety It is safe to call this macro from any thread.
  221. *
  222. * \since This macro is available since SDL 3.1.3.
  223. *
  224. * \sa SDL_reinterpret_cast
  225. * \sa SDL_const_cast
  226. */
  227. #define SDL_static_cast(type, expression) static_cast<type>(expression) /* or `((type)(expression))` in C */
  228. /**
  229. * Handle a Const Cast properly whether using C or C++.
  230. *
  231. * If compiled as C++, this macro offers a proper C++ const_cast<>.
  232. *
  233. * If compiled as C, this macro does a normal C-style cast.
  234. *
  235. * This is helpful to avoid compiler warnings in C++.
  236. *
  237. * \param type the type to cast the expression to.
  238. * \param expression the expression to cast to a different type.
  239. * \returns `expression`, cast to `type`.
  240. *
  241. * \threadsafety It is safe to call this macro from any thread.
  242. *
  243. * \since This macro is available since SDL 3.1.3.
  244. *
  245. * \sa SDL_reinterpret_cast
  246. * \sa SDL_static_cast
  247. */
  248. #define SDL_const_cast(type, expression) const_cast<type>(expression) /* or `((type)(expression))` in C */
  249. #elif defined(__cplusplus)
  250. #define SDL_reinterpret_cast(type, expression) reinterpret_cast<type>(expression)
  251. #define SDL_static_cast(type, expression) static_cast<type>(expression)
  252. #define SDL_const_cast(type, expression) const_cast<type>(expression)
  253. #else
  254. #define SDL_reinterpret_cast(type, expression) ((type)(expression))
  255. #define SDL_static_cast(type, expression) ((type)(expression))
  256. #define SDL_const_cast(type, expression) ((type)(expression))
  257. #endif
  258. /* @} *//* Cast operators */
  259. /**
  260. * Define a four character code as a Uint32.
  261. *
  262. * \param A the first ASCII character.
  263. * \param B the second ASCII character.
  264. * \param C the third ASCII character.
  265. * \param D the fourth ASCII character.
  266. * \returns the four characters converted into a Uint32, one character
  267. * per-byte.
  268. *
  269. * \threadsafety It is safe to call this macro from any thread.
  270. *
  271. * \since This macro is available since SDL 3.1.3.
  272. */
  273. #define SDL_FOURCC(A, B, C, D) \
  274. ((SDL_static_cast(Uint32, SDL_static_cast(Uint8, (A))) << 0) | \
  275. (SDL_static_cast(Uint32, SDL_static_cast(Uint8, (B))) << 8) | \
  276. (SDL_static_cast(Uint32, SDL_static_cast(Uint8, (C))) << 16) | \
  277. (SDL_static_cast(Uint32, SDL_static_cast(Uint8, (D))) << 24))
  278. #ifdef SDL_WIKI_DOCUMENTATION_SECTION
  279. /**
  280. * Append the 64 bit integer suffix to a signed integer literal.
  281. *
  282. * This helps compilers that might believe a integer literal larger than
  283. * 0xFFFFFFFF is overflowing a 32-bit value. Use `SDL_SINT64_C(0xFFFFFFFF1)`
  284. * instead of `0xFFFFFFFF1` by itself.
  285. *
  286. * \since This macro is available since SDL 3.1.3.
  287. *
  288. * \sa SDL_UINT64_C
  289. */
  290. #define SDL_SINT64_C(c) c ## LL /* or whatever the current compiler uses. */
  291. /**
  292. * Append the 64 bit integer suffix to an unsigned integer literal.
  293. *
  294. * This helps compilers that might believe a integer literal larger than
  295. * 0xFFFFFFFF is overflowing a 32-bit value. Use `SDL_UINT64_C(0xFFFFFFFF1)`
  296. * instead of `0xFFFFFFFF1` by itself.
  297. *
  298. * \since This macro is available since SDL 3.1.3.
  299. *
  300. * \sa SDL_SINT64_C
  301. */
  302. #define SDL_UINT64_C(c) c ## ULL /* or whatever the current compiler uses. */
  303. #else /* !SDL_WIKI_DOCUMENTATION_SECTION */
  304. #ifndef SDL_SINT64_C
  305. #if defined(INT64_C)
  306. #define SDL_SINT64_C(c) INT64_C(c)
  307. #elif defined(_MSC_VER)
  308. #define SDL_SINT64_C(c) c ## i64
  309. #elif defined(__LP64__) || defined(_LP64)
  310. #define SDL_SINT64_C(c) c ## L
  311. #else
  312. #define SDL_SINT64_C(c) c ## LL
  313. #endif
  314. #endif /* !SDL_SINT64_C */
  315. #ifndef SDL_UINT64_C
  316. #if defined(UINT64_C)
  317. #define SDL_UINT64_C(c) UINT64_C(c)
  318. #elif defined(_MSC_VER)
  319. #define SDL_UINT64_C(c) c ## ui64
  320. #elif defined(__LP64__) || defined(_LP64)
  321. #define SDL_UINT64_C(c) c ## UL
  322. #else
  323. #define SDL_UINT64_C(c) c ## ULL
  324. #endif
  325. #endif /* !SDL_UINT64_C */
  326. #endif /* !SDL_WIKI_DOCUMENTATION_SECTION */
  327. /**
  328. * \name Basic data types
  329. */
  330. /* @{ */
  331. /**
  332. * A signed 8-bit integer type.
  333. *
  334. * \since This macro is available since SDL 3.1.3.
  335. */
  336. typedef int8_t Sint8;
  337. #define SDL_MAX_SINT8 ((Sint8)0x7F) /* 127 */
  338. #define SDL_MIN_SINT8 ((Sint8)(~0x7F)) /* -128 */
  339. /**
  340. * An unsigned 8-bit integer type.
  341. *
  342. * \since This macro is available since SDL 3.1.3.
  343. */
  344. typedef uint8_t Uint8;
  345. #define SDL_MAX_UINT8 ((Uint8)0xFF) /* 255 */
  346. #define SDL_MIN_UINT8 ((Uint8)0x00) /* 0 */
  347. /**
  348. * A signed 16-bit integer type.
  349. *
  350. * \since This macro is available since SDL 3.1.3.
  351. */
  352. typedef int16_t Sint16;
  353. #define SDL_MAX_SINT16 ((Sint16)0x7FFF) /* 32767 */
  354. #define SDL_MIN_SINT16 ((Sint16)(~0x7FFF)) /* -32768 */
  355. /**
  356. * An unsigned 16-bit integer type.
  357. *
  358. * \since This macro is available since SDL 3.1.3.
  359. */
  360. typedef uint16_t Uint16;
  361. #define SDL_MAX_UINT16 ((Uint16)0xFFFF) /* 65535 */
  362. #define SDL_MIN_UINT16 ((Uint16)0x0000) /* 0 */
  363. /**
  364. * A signed 32-bit integer type.
  365. *
  366. * \since This macro is available since SDL 3.1.3.
  367. */
  368. typedef int32_t Sint32;
  369. #define SDL_MAX_SINT32 ((Sint32)0x7FFFFFFF) /* 2147483647 */
  370. #define SDL_MIN_SINT32 ((Sint32)(~0x7FFFFFFF)) /* -2147483648 */
  371. /**
  372. * An unsigned 32-bit integer type.
  373. *
  374. * \since This macro is available since SDL 3.1.3.
  375. */
  376. typedef uint32_t Uint32;
  377. #define SDL_MAX_UINT32 ((Uint32)0xFFFFFFFFu) /* 4294967295 */
  378. #define SDL_MIN_UINT32 ((Uint32)0x00000000) /* 0 */
  379. /**
  380. * A signed 64-bit integer type.
  381. *
  382. * \since This macro is available since SDL 3.1.3.
  383. *
  384. * \sa SDL_SINT64_C
  385. */
  386. typedef int64_t Sint64;
  387. #define SDL_MAX_SINT64 SDL_SINT64_C(0x7FFFFFFFFFFFFFFF) /* 9223372036854775807 */
  388. #define SDL_MIN_SINT64 ~SDL_SINT64_C(0x7FFFFFFFFFFFFFFF) /* -9223372036854775808 */
  389. /**
  390. * An unsigned 64-bit integer type.
  391. *
  392. * \since This macro is available since SDL 3.1.3.
  393. *
  394. * \sa SDL_UINT64_C
  395. */
  396. typedef uint64_t Uint64;
  397. #define SDL_MAX_UINT64 SDL_UINT64_C(0xFFFFFFFFFFFFFFFF) /* 18446744073709551615 */
  398. #define SDL_MIN_UINT64 SDL_UINT64_C(0x0000000000000000) /* 0 */
  399. /**
  400. * SDL times are signed, 64-bit integers representing nanoseconds since the
  401. * Unix epoch (Jan 1, 1970).
  402. *
  403. * They can be converted between POSIX time_t values with SDL_NS_TO_SECONDS()
  404. * and SDL_SECONDS_TO_NS(), and between Windows FILETIME values with
  405. * SDL_TimeToWindows() and SDL_TimeFromWindows().
  406. *
  407. * \since This macro is available since SDL 3.1.3.
  408. *
  409. * \sa SDL_MAX_SINT64
  410. * \sa SDL_MIN_SINT64
  411. */
  412. typedef Sint64 SDL_Time;
  413. #define SDL_MAX_TIME SDL_MAX_SINT64
  414. #define SDL_MIN_TIME SDL_MIN_SINT64
  415. /* @} *//* Basic data types */
  416. /**
  417. * \name Floating-point constants
  418. */
  419. /* @{ */
  420. #ifdef FLT_EPSILON
  421. #define SDL_FLT_EPSILON FLT_EPSILON
  422. #else
  423. /**
  424. * Epsilon constant, used for comparing floating-point numbers.
  425. *
  426. * Equals by default to platform-defined `FLT_EPSILON`, or
  427. * `1.1920928955078125e-07F` if that's not available.
  428. *
  429. * \since This macro is available since SDL 3.1.3.
  430. */
  431. #define SDL_FLT_EPSILON 1.1920928955078125e-07F /* 0x0.000002p0 */
  432. #endif
  433. /* @} *//* Floating-point constants */
  434. #ifdef SDL_WIKI_DOCUMENTATION_SECTION
  435. /**
  436. * A printf-formatting string for an Sint64 value.
  437. *
  438. * Use it like this:
  439. *
  440. * ```c
  441. * SDL_Log("There are %" SDL_PRIs64 " bottles of beer on the wall.", bottles);
  442. * ```
  443. *
  444. * \since This macro is available since SDL 3.1.3.
  445. */
  446. #define SDL_PRIs64 "lld"
  447. /**
  448. * A printf-formatting string for a Uint64 value.
  449. *
  450. * Use it like this:
  451. *
  452. * ```c
  453. * SDL_Log("There are %" SDL_PRIu64 " bottles of beer on the wall.", bottles);
  454. * ```
  455. *
  456. * \since This macro is available since SDL 3.1.3.
  457. */
  458. #define SDL_PRIu64 "llu"
  459. /**
  460. * A printf-formatting string for a Uint64 value as lower-case hexadecimal.
  461. *
  462. * Use it like this:
  463. *
  464. * ```c
  465. * SDL_Log("There are %" SDL_PRIx64 " bottles of beer on the wall.", bottles);
  466. * ```
  467. *
  468. * \since This macro is available since SDL 3.1.3.
  469. */
  470. #define SDL_PRIx64 "llx"
  471. /**
  472. * A printf-formatting string for a Uint64 value as upper-case hexadecimal.
  473. *
  474. * Use it like this:
  475. *
  476. * ```c
  477. * SDL_Log("There are %" SDL_PRIX64 " bottles of beer on the wall.", bottles);
  478. * ```
  479. *
  480. * \since This macro is available since SDL 3.1.3.
  481. */
  482. #define SDL_PRIX64 "llX"
  483. /**
  484. * A printf-formatting string for an Sint32 value.
  485. *
  486. * Use it like this:
  487. *
  488. * ```c
  489. * SDL_Log("There are %" SDL_PRIs32 " bottles of beer on the wall.", bottles);
  490. * ```
  491. *
  492. * \since This macro is available since SDL 3.1.3.
  493. */
  494. #define SDL_PRIs32 "d"
  495. /**
  496. * A printf-formatting string for a Uint32 value.
  497. *
  498. * Use it like this:
  499. *
  500. * ```c
  501. * SDL_Log("There are %" SDL_PRIu32 " bottles of beer on the wall.", bottles);
  502. * ```
  503. *
  504. * \since This macro is available since SDL 3.1.3.
  505. */
  506. #define SDL_PRIu32 "u"
  507. /**
  508. * A printf-formatting string for a Uint32 value as lower-case hexadecimal.
  509. *
  510. * Use it like this:
  511. *
  512. * ```c
  513. * SDL_Log("There are %" SDL_PRIx32 " bottles of beer on the wall.", bottles);
  514. * ```
  515. *
  516. * \since This macro is available since SDL 3.1.3.
  517. */
  518. #define SDL_PRIx32 "x"
  519. /**
  520. * A printf-formatting string for a Uint32 value as upper-case hexadecimal.
  521. *
  522. * Use it like this:
  523. *
  524. * ```c
  525. * SDL_Log("There are %" SDL_PRIX32 " bottles of beer on the wall.", bottles);
  526. * ```
  527. *
  528. * \since This macro is available since SDL 3.1.3.
  529. */
  530. #define SDL_PRIX32 "X"
  531. /**
  532. * A printf-formatting string prefix for a `long long` value.
  533. *
  534. * This is just the prefix! You probably actually want SDL_PRILLd, SDL_PRILLu,
  535. * SDL_PRILLx, or SDL_PRILLX instead.
  536. *
  537. * Use it like this:
  538. *
  539. * ```c
  540. * SDL_Log("There are %" SDL_PRILL_PREFIX "d bottles of beer on the wall.", bottles);
  541. * ```
  542. *
  543. * \since This macro is available since SDL 3.1.3.
  544. */
  545. #define SDL_PRILL_PREFIX "ll"
  546. /**
  547. * A printf-formatting string for a `long long` value.
  548. *
  549. * Use it like this:
  550. *
  551. * ```c
  552. * SDL_Log("There are %" SDL_PRILLd " bottles of beer on the wall.", bottles);
  553. * ```
  554. *
  555. * \since This macro is available since SDL 3.1.3.
  556. */
  557. #define SDL_PRILLd SDL_PRILL_PREFIX "d"
  558. /**
  559. * A printf-formatting string for a `unsigned long long` value.
  560. *
  561. * Use it like this:
  562. *
  563. * ```c
  564. * SDL_Log("There are %" SDL_PRILLu " bottles of beer on the wall.", bottles);
  565. * ```
  566. *
  567. * \since This macro is available since SDL 3.1.3.
  568. */
  569. #define SDL_PRILLu SDL_PRILL_PREFIX "u"
  570. /**
  571. * A printf-formatting string for an `unsigned long long` value as lower-case
  572. * hexadecimal.
  573. *
  574. * Use it like this:
  575. *
  576. * ```c
  577. * SDL_Log("There are %" SDL_PRILLx " bottles of beer on the wall.", bottles);
  578. * ```
  579. *
  580. * \since This macro is available since SDL 3.1.3.
  581. */
  582. #define SDL_PRILLx SDL_PRILL_PREFIX "x"
  583. /**
  584. * A printf-formatting string for an `unsigned long long` value as upper-case
  585. * hexadecimal.
  586. *
  587. * Use it like this:
  588. *
  589. * ```c
  590. * SDL_Log("There are %" SDL_PRILLX " bottles of beer on the wall.", bottles);
  591. * ```
  592. *
  593. * \since This macro is available since SDL 3.1.3.
  594. */
  595. #define SDL_PRILLX SDL_PRILL_PREFIX "X"
  596. #endif /* SDL_WIKI_DOCUMENTATION_SECTION */
  597. /* Make sure we have macros for printing width-based integers.
  598. * <inttypes.h> should define these but this is not true all platforms.
  599. * (for example win32) */
  600. #ifndef SDL_PRIs64
  601. #if defined(SDL_PLATFORM_WINDOWS)
  602. #define SDL_PRIs64 "I64d"
  603. #elif defined(PRId64)
  604. #define SDL_PRIs64 PRId64
  605. #elif defined(__LP64__) && !defined(SDL_PLATFORM_APPLE) && !defined(__EMSCRIPTEN__)
  606. #define SDL_PRIs64 "ld"
  607. #else
  608. #define SDL_PRIs64 "lld"
  609. #endif
  610. #endif
  611. #ifndef SDL_PRIu64
  612. #if defined(SDL_PLATFORM_WINDOWS)
  613. #define SDL_PRIu64 "I64u"
  614. #elif defined(PRIu64)
  615. #define SDL_PRIu64 PRIu64
  616. #elif defined(__LP64__) && !defined(SDL_PLATFORM_APPLE) && !defined(__EMSCRIPTEN__)
  617. #define SDL_PRIu64 "lu"
  618. #else
  619. #define SDL_PRIu64 "llu"
  620. #endif
  621. #endif
  622. #ifndef SDL_PRIx64
  623. #if defined(SDL_PLATFORM_WINDOWS)
  624. #define SDL_PRIx64 "I64x"
  625. #elif defined(PRIx64)
  626. #define SDL_PRIx64 PRIx64
  627. #elif defined(__LP64__) && !defined(SDL_PLATFORM_APPLE)
  628. #define SDL_PRIx64 "lx"
  629. #else
  630. #define SDL_PRIx64 "llx"
  631. #endif
  632. #endif
  633. #ifndef SDL_PRIX64
  634. #if defined(SDL_PLATFORM_WINDOWS)
  635. #define SDL_PRIX64 "I64X"
  636. #elif defined(PRIX64)
  637. #define SDL_PRIX64 PRIX64
  638. #elif defined(__LP64__) && !defined(SDL_PLATFORM_APPLE)
  639. #define SDL_PRIX64 "lX"
  640. #else
  641. #define SDL_PRIX64 "llX"
  642. #endif
  643. #endif
  644. #ifndef SDL_PRIs32
  645. #ifdef PRId32
  646. #define SDL_PRIs32 PRId32
  647. #else
  648. #define SDL_PRIs32 "d"
  649. #endif
  650. #endif
  651. #ifndef SDL_PRIu32
  652. #ifdef PRIu32
  653. #define SDL_PRIu32 PRIu32
  654. #else
  655. #define SDL_PRIu32 "u"
  656. #endif
  657. #endif
  658. #ifndef SDL_PRIx32
  659. #ifdef PRIx32
  660. #define SDL_PRIx32 PRIx32
  661. #else
  662. #define SDL_PRIx32 "x"
  663. #endif
  664. #endif
  665. #ifndef SDL_PRIX32
  666. #ifdef PRIX32
  667. #define SDL_PRIX32 PRIX32
  668. #else
  669. #define SDL_PRIX32 "X"
  670. #endif
  671. #endif
  672. /* Specifically for the `long long` -- SDL-specific. */
  673. #ifdef SDL_PLATFORM_WINDOWS
  674. SDL_COMPILE_TIME_ASSERT(longlong_size64, sizeof(long long) == 8); /* using I64 for windows - make sure `long long` is 64 bits. */
  675. #define SDL_PRILL_PREFIX "I64"
  676. #else
  677. #define SDL_PRILL_PREFIX "ll"
  678. #endif
  679. #ifndef SDL_PRILLd
  680. #define SDL_PRILLd SDL_PRILL_PREFIX "d"
  681. #endif
  682. #ifndef SDL_PRILLu
  683. #define SDL_PRILLu SDL_PRILL_PREFIX "u"
  684. #endif
  685. #ifndef SDL_PRILLx
  686. #define SDL_PRILLx SDL_PRILL_PREFIX "x"
  687. #endif
  688. #ifndef SDL_PRILLX
  689. #define SDL_PRILLX SDL_PRILL_PREFIX "X"
  690. #endif
  691. /* Annotations to help code analysis tools */
  692. #ifdef SDL_WIKI_DOCUMENTATION_SECTION
  693. /**
  694. * Macro that annotates function params with input buffer size.
  695. *
  696. * If we were to annotate `memcpy`:
  697. *
  698. * ```c
  699. * void *memcpy(void *dst, SDL_IN_BYTECAP(len) const void *src, size_t len);
  700. * ```
  701. *
  702. * This notes that `src` should be `len` bytes in size and is only read by the
  703. * function. The compiler or other analysis tools can warn when this doesn't
  704. * appear to be the case.
  705. *
  706. * On compilers without this annotation mechanism, this is defined to nothing.
  707. *
  708. * \since This macro is available since SDL 3.1.3.
  709. */
  710. #define SDL_IN_BYTECAP(x) _In_bytecount_(x)
  711. /**
  712. * Macro that annotates function params with input/output string buffer size.
  713. *
  714. * If we were to annotate `strlcat`:
  715. *
  716. * ```c
  717. * size_t strlcat(SDL_INOUT_Z_CAP(maxlen) char *dst, const char *src, size_t maxlen);
  718. * ```
  719. *
  720. * This notes that `dst` is a null-terminated C string, should be `maxlen`
  721. * bytes in size, and is both read from and written to by the function. The
  722. * compiler or other analysis tools can warn when this doesn't appear to be
  723. * the case.
  724. *
  725. * On compilers without this annotation mechanism, this is defined to nothing.
  726. *
  727. * \since This macro is available since SDL 3.1.3.
  728. */
  729. #define SDL_INOUT_Z_CAP(x) _Inout_z_cap_(x)
  730. /**
  731. * Macro that annotates function params with output string buffer size.
  732. *
  733. * If we were to annotate `snprintf`:
  734. *
  735. * ```c
  736. * int snprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *fmt, ...);
  737. * ```
  738. *
  739. * This notes that `text` is a null-terminated C string, should be `maxlen`
  740. * bytes in size, and is only written to by the function. The compiler or
  741. * other analysis tools can warn when this doesn't appear to be the case.
  742. *
  743. * On compilers without this annotation mechanism, this is defined to nothing.
  744. *
  745. * \since This macro is available since SDL 3.1.3.
  746. */
  747. #define SDL_OUT_Z_CAP(x) _Out_z_cap_(x)
  748. /**
  749. * Macro that annotates function params with output buffer size.
  750. *
  751. * If we were to annotate `wcsncpy`:
  752. *
  753. * ```c
  754. * char *wcscpy(SDL_OUT_CAP(bufsize) wchar_t *dst, const wchar_t *src, size_t bufsize);
  755. * ```
  756. *
  757. * This notes that `dst` should have a capacity of `bufsize` wchar_t in size,
  758. * and is only written to by the function. The compiler or other analysis
  759. * tools can warn when this doesn't appear to be the case.
  760. *
  761. * This operates on counts of objects, not bytes. Use SDL_OUT_BYTECAP for
  762. * bytes.
  763. *
  764. * On compilers without this annotation mechanism, this is defined to nothing.
  765. *
  766. * \since This macro is available since SDL 3.1.3.
  767. */
  768. #define SDL_OUT_CAP(x) _Out_cap_(x)
  769. /**
  770. * Macro that annotates function params with output buffer size.
  771. *
  772. * If we were to annotate `memcpy`:
  773. *
  774. * ```c
  775. * void *memcpy(SDL_OUT_BYTECAP(bufsize) void *dst, const void *src, size_t bufsize);
  776. * ```
  777. *
  778. * This notes that `dst` should have a capacity of `bufsize` bytes in size,
  779. * and is only written to by the function. The compiler or other analysis
  780. * tools can warn when this doesn't appear to be the case.
  781. *
  782. * On compilers without this annotation mechanism, this is defined to nothing.
  783. *
  784. * \since This macro is available since SDL 3.1.3.
  785. */
  786. #define SDL_OUT_BYTECAP(x) _Out_bytecap_(x)
  787. /**
  788. * Macro that annotates function params with output buffer string size.
  789. *
  790. * If we were to annotate `strcpy`:
  791. *
  792. * ```c
  793. * char *strcpy(SDL_OUT_Z_BYTECAP(bufsize) char *dst, const char *src, size_t bufsize);
  794. * ```
  795. *
  796. * This notes that `dst` should have a capacity of `bufsize` bytes in size,
  797. * and a zero-terminated string is written to it by the function. The compiler
  798. * or other analysis tools can warn when this doesn't appear to be the case.
  799. *
  800. * On compilers without this annotation mechanism, this is defined to nothing.
  801. *
  802. * \since This macro is available since SDL 3.1.3.
  803. */
  804. #define SDL_OUT_Z_BYTECAP(x) _Out_z_bytecap_(x)
  805. /**
  806. * Macro that annotates function params as printf-style format strings.
  807. *
  808. * If we were to annotate `fprintf`:
  809. *
  810. * ```c
  811. * int fprintf(FILE *f, SDL_PRINTF_FORMAT_STRING const char *fmt, ...);
  812. * ```
  813. *
  814. * This notes that `fmt` should be a printf-style format string. The compiler
  815. * or other analysis tools can warn when this doesn't appear to be the case.
  816. *
  817. * On compilers without this annotation mechanism, this is defined to nothing.
  818. *
  819. * \since This macro is available since SDL 3.1.3.
  820. */
  821. #define SDL_PRINTF_FORMAT_STRING _Printf_format_string_
  822. /**
  823. * Macro that annotates function params as scanf-style format strings.
  824. *
  825. * If we were to annotate `fscanf`:
  826. *
  827. * ```c
  828. * int fscanf(FILE *f, SDL_SCANF_FORMAT_STRING const char *fmt, ...);
  829. * ```
  830. *
  831. * This notes that `fmt` should be a scanf-style format string. The compiler
  832. * or other analysis tools can warn when this doesn't appear to be the case.
  833. *
  834. * On compilers without this annotation mechanism, this is defined to nothing.
  835. *
  836. * \since This macro is available since SDL 3.1.3.
  837. */
  838. #define SDL_SCANF_FORMAT_STRING _Scanf_format_string_impl_
  839. /**
  840. * Macro that annotates a vararg function that operates like printf.
  841. *
  842. * If we were to annotate `fprintf`:
  843. *
  844. * ```c
  845. * int fprintf(FILE *f, const char *fmt, ...) SDL_PRINTF_VARARG_FUNC(2);
  846. * ```
  847. *
  848. * This notes that the second parameter should be a printf-style format
  849. * string, followed by `...`. The compiler or other analysis tools can warn
  850. * when this doesn't appear to be the case.
  851. *
  852. * On compilers without this annotation mechanism, this is defined to nothing.
  853. *
  854. * This can (and should) be used with SDL_PRINTF_FORMAT_STRING as well, which
  855. * between them will cover at least Visual Studio, GCC, and Clang.
  856. *
  857. * \since This macro is available since SDL 3.1.3.
  858. */
  859. #define SDL_PRINTF_VARARG_FUNC( fmtargnumber ) __attribute__ (( format( __printf__, fmtargnumber, fmtargnumber+1 )))
  860. /**
  861. * Macro that annotates a va_list function that operates like printf.
  862. *
  863. * If we were to annotate `vfprintf`:
  864. *
  865. * ```c
  866. * int vfprintf(FILE *f, const char *fmt, va_list ap) SDL_PRINTF_VARARG_FUNCV(2);
  867. * ```
  868. *
  869. * This notes that the second parameter should be a printf-style format
  870. * string, followed by a va_list. The compiler or other analysis tools can
  871. * warn when this doesn't appear to be the case.
  872. *
  873. * On compilers without this annotation mechanism, this is defined to nothing.
  874. *
  875. * This can (and should) be used with SDL_PRINTF_FORMAT_STRING as well, which
  876. * between them will cover at least Visual Studio, GCC, and Clang.
  877. *
  878. * \since This macro is available since SDL 3.1.3.
  879. */
  880. #define SDL_PRINTF_VARARG_FUNCV( fmtargnumber ) __attribute__(( format( __printf__, fmtargnumber, 0 )))
  881. /**
  882. * Macro that annotates a vararg function that operates like scanf.
  883. *
  884. * If we were to annotate `fscanf`:
  885. *
  886. * ```c
  887. * int fscanf(FILE *f, const char *fmt, ...) SDL_PRINTF_VARARG_FUNCV(2);
  888. * ```
  889. *
  890. * This notes that the second parameter should be a scanf-style format string,
  891. * followed by `...`. The compiler or other analysis tools can warn when this
  892. * doesn't appear to be the case.
  893. *
  894. * On compilers without this annotation mechanism, this is defined to nothing.
  895. *
  896. * This can (and should) be used with SDL_SCANF_FORMAT_STRING as well, which
  897. * between them will cover at least Visual Studio, GCC, and Clang.
  898. *
  899. * \since This macro is available since SDL 3.1.3.
  900. */
  901. #define SDL_SCANF_VARARG_FUNC( fmtargnumber ) __attribute__ (( format( __scanf__, fmtargnumber, fmtargnumber+1 )))
  902. /**
  903. * Macro that annotates a va_list function that operates like scanf.
  904. *
  905. * If we were to annotate `vfscanf`:
  906. *
  907. * ```c
  908. * int vfscanf(FILE *f, const char *fmt, va_list ap) SDL_PRINTF_VARARG_FUNCV(2);
  909. * ```
  910. *
  911. * This notes that the second parameter should be a scanf-style format string,
  912. * followed by a va_list. The compiler or other analysis tools can warn when
  913. * this doesn't appear to be the case.
  914. *
  915. * On compilers without this annotation mechanism, this is defined to nothing.
  916. *
  917. * This can (and should) be used with SDL_SCANF_FORMAT_STRING as well, which
  918. * between them will cover at least Visual Studio, GCC, and Clang.
  919. *
  920. * \since This macro is available since SDL 3.1.3.
  921. */
  922. #define SDL_SCANF_VARARG_FUNCV( fmtargnumber ) __attribute__(( format( __scanf__, fmtargnumber, 0 )))
  923. /**
  924. * Macro that annotates a vararg function that operates like wprintf.
  925. *
  926. * If we were to annotate `fwprintf`:
  927. *
  928. * ```c
  929. * int fwprintf(FILE *f, const wchar_t *fmt, ...) SDL_WPRINTF_VARARG_FUNC(2);
  930. * ```
  931. *
  932. * This notes that the second parameter should be a wprintf-style format wide
  933. * string, followed by `...`. The compiler or other analysis tools can warn
  934. * when this doesn't appear to be the case.
  935. *
  936. * On compilers without this annotation mechanism, this is defined to nothing.
  937. *
  938. * This can (and should) be used with SDL_PRINTF_FORMAT_STRING as well, which
  939. * between them will cover at least Visual Studio, GCC, and Clang.
  940. *
  941. * \since This macro is available since SDL 3.1.3.
  942. */
  943. #define SDL_WPRINTF_VARARG_FUNC( fmtargnumber ) /* __attribute__ (( format( __wprintf__, fmtargnumber, fmtargnumber+1 ))) */
  944. /**
  945. * Macro that annotates a va_list function that operates like wprintf.
  946. *
  947. * If we were to annotate `vfwprintf`:
  948. *
  949. * ```c
  950. * int vfwprintf(FILE *f, const wchar_t *fmt, va_list ap) SDL_WPRINTF_VARARG_FUNC(2);
  951. * ```
  952. *
  953. * This notes that the second parameter should be a wprintf-style format wide
  954. * string, followed by a va_list. The compiler or other analysis tools can
  955. * warn when this doesn't appear to be the case.
  956. *
  957. * On compilers without this annotation mechanism, this is defined to nothing.
  958. *
  959. * This can (and should) be used with SDL_PRINTF_FORMAT_STRING as well, which
  960. * between them will cover at least Visual Studio, GCC, and Clang.
  961. *
  962. * \since This macro is available since SDL 3.1.3.
  963. */
  964. #define SDL_WPRINTF_VARARG_FUNCV( fmtargnumber ) /* __attribute__ (( format( __wprintf__, fmtargnumber, 0 ))) */
  965. #elif defined(SDL_DISABLE_ANALYZE_MACROS)
  966. #define SDL_IN_BYTECAP(x)
  967. #define SDL_INOUT_Z_CAP(x)
  968. #define SDL_OUT_Z_CAP(x)
  969. #define SDL_OUT_CAP(x)
  970. #define SDL_OUT_BYTECAP(x)
  971. #define SDL_OUT_Z_BYTECAP(x)
  972. #define SDL_PRINTF_FORMAT_STRING
  973. #define SDL_SCANF_FORMAT_STRING
  974. #define SDL_PRINTF_VARARG_FUNC( fmtargnumber )
  975. #define SDL_PRINTF_VARARG_FUNCV( fmtargnumber )
  976. #define SDL_SCANF_VARARG_FUNC( fmtargnumber )
  977. #define SDL_SCANF_VARARG_FUNCV( fmtargnumber )
  978. #define SDL_WPRINTF_VARARG_FUNC( fmtargnumber )
  979. #define SDL_WPRINTF_VARARG_FUNCV( fmtargnumber )
  980. #else
  981. #if defined(_MSC_VER) && (_MSC_VER >= 1600) /* VS 2010 and above */
  982. #include <sal.h>
  983. #define SDL_IN_BYTECAP(x) _In_bytecount_(x)
  984. #define SDL_INOUT_Z_CAP(x) _Inout_z_cap_(x)
  985. #define SDL_OUT_Z_CAP(x) _Out_z_cap_(x)
  986. #define SDL_OUT_CAP(x) _Out_cap_(x)
  987. #define SDL_OUT_BYTECAP(x) _Out_bytecap_(x)
  988. #define SDL_OUT_Z_BYTECAP(x) _Out_z_bytecap_(x)
  989. #define SDL_PRINTF_FORMAT_STRING _Printf_format_string_
  990. #define SDL_SCANF_FORMAT_STRING _Scanf_format_string_impl_
  991. #else
  992. #define SDL_IN_BYTECAP(x)
  993. #define SDL_INOUT_Z_CAP(x)
  994. #define SDL_OUT_Z_CAP(x)
  995. #define SDL_OUT_CAP(x)
  996. #define SDL_OUT_BYTECAP(x)
  997. #define SDL_OUT_Z_BYTECAP(x)
  998. #define SDL_PRINTF_FORMAT_STRING
  999. #define SDL_SCANF_FORMAT_STRING
  1000. #endif
  1001. #if defined(__GNUC__) || defined(__clang__)
  1002. #define SDL_PRINTF_VARARG_FUNC( fmtargnumber ) __attribute__ (( format( __printf__, fmtargnumber, fmtargnumber+1 )))
  1003. #define SDL_PRINTF_VARARG_FUNCV( fmtargnumber ) __attribute__(( format( __printf__, fmtargnumber, 0 )))
  1004. #define SDL_SCANF_VARARG_FUNC( fmtargnumber ) __attribute__ (( format( __scanf__, fmtargnumber, fmtargnumber+1 )))
  1005. #define SDL_SCANF_VARARG_FUNCV( fmtargnumber ) __attribute__(( format( __scanf__, fmtargnumber, 0 )))
  1006. #define SDL_WPRINTF_VARARG_FUNC( fmtargnumber ) /* __attribute__ (( format( __wprintf__, fmtargnumber, fmtargnumber+1 ))) */
  1007. #define SDL_WPRINTF_VARARG_FUNCV( fmtargnumber ) /* __attribute__ (( format( __wprintf__, fmtargnumber, 0 ))) */
  1008. #else
  1009. #define SDL_PRINTF_VARARG_FUNC( fmtargnumber )
  1010. #define SDL_PRINTF_VARARG_FUNCV( fmtargnumber )
  1011. #define SDL_SCANF_VARARG_FUNC( fmtargnumber )
  1012. #define SDL_SCANF_VARARG_FUNCV( fmtargnumber )
  1013. #define SDL_WPRINTF_VARARG_FUNC( fmtargnumber )
  1014. #define SDL_WPRINTF_VARARG_FUNCV( fmtargnumber )
  1015. #endif
  1016. #endif /* SDL_DISABLE_ANALYZE_MACROS */
  1017. /** \cond */
  1018. #ifndef DOXYGEN_SHOULD_IGNORE_THIS
  1019. SDL_COMPILE_TIME_ASSERT(bool_size, sizeof(bool) == 1);
  1020. SDL_COMPILE_TIME_ASSERT(uint8_size, sizeof(Uint8) == 1);
  1021. SDL_COMPILE_TIME_ASSERT(sint8_size, sizeof(Sint8) == 1);
  1022. SDL_COMPILE_TIME_ASSERT(uint16_size, sizeof(Uint16) == 2);
  1023. SDL_COMPILE_TIME_ASSERT(sint16_size, sizeof(Sint16) == 2);
  1024. SDL_COMPILE_TIME_ASSERT(uint32_size, sizeof(Uint32) == 4);
  1025. SDL_COMPILE_TIME_ASSERT(sint32_size, sizeof(Sint32) == 4);
  1026. SDL_COMPILE_TIME_ASSERT(uint64_size, sizeof(Uint64) == 8);
  1027. SDL_COMPILE_TIME_ASSERT(sint64_size, sizeof(Sint64) == 8);
  1028. SDL_COMPILE_TIME_ASSERT(uint64_longlong, sizeof(Uint64) <= sizeof(unsigned long long));
  1029. SDL_COMPILE_TIME_ASSERT(size_t_longlong, sizeof(size_t) <= sizeof(unsigned long long));
  1030. typedef struct SDL_alignment_test
  1031. {
  1032. Uint8 a;
  1033. void *b;
  1034. } SDL_alignment_test;
  1035. SDL_COMPILE_TIME_ASSERT(struct_alignment, sizeof(SDL_alignment_test) == (2 * sizeof(void *)));
  1036. SDL_COMPILE_TIME_ASSERT(two_s_complement, (int)~(int)0 == (int)(-1));
  1037. #endif /* DOXYGEN_SHOULD_IGNORE_THIS */
  1038. /** \endcond */
  1039. /* Check to make sure enums are the size of ints, for structure packing.
  1040. For both Watcom C/C++ and Borland C/C++ the compiler option that makes
  1041. enums having the size of an int must be enabled.
  1042. This is "-b" for Borland C/C++ and "-ei" for Watcom C/C++ (v11).
  1043. */
  1044. /** \cond */
  1045. #ifndef DOXYGEN_SHOULD_IGNORE_THIS
  1046. #if !defined(SDL_PLATFORM_VITA) && !defined(SDL_PLATFORM_3DS)
  1047. /* TODO: include/SDL_stdinc.h:390: error: size of array 'SDL_dummy_enum' is negative */
  1048. typedef enum SDL_DUMMY_ENUM
  1049. {
  1050. DUMMY_ENUM_VALUE
  1051. } SDL_DUMMY_ENUM;
  1052. SDL_COMPILE_TIME_ASSERT(enum, sizeof(SDL_DUMMY_ENUM) == sizeof(int));
  1053. #endif
  1054. #endif /* DOXYGEN_SHOULD_IGNORE_THIS */
  1055. /** \endcond */
  1056. #include <SDL3/SDL_begin_code.h>
  1057. /* Set up for C function definitions, even when using C++ */
  1058. #ifdef __cplusplus
  1059. extern "C" {
  1060. #endif
  1061. /**
  1062. * A macro to initialize an SDL interface.
  1063. *
  1064. * This macro will initialize an SDL interface structure and should be called
  1065. * before you fill out the fields with your implementation.
  1066. *
  1067. * You can use it like this:
  1068. *
  1069. * ```c
  1070. * SDL_IOStreamInterface iface;
  1071. *
  1072. * SDL_INIT_INTERFACE(&iface);
  1073. *
  1074. * // Fill in the interface function pointers with your implementation
  1075. * iface.seek = ...
  1076. *
  1077. * stream = SDL_OpenIO(&iface, NULL);
  1078. * ```
  1079. *
  1080. * If you are using designated initializers, you can use the size of the
  1081. * interface as the version, e.g.
  1082. *
  1083. * ```c
  1084. * SDL_IOStreamInterface iface = {
  1085. * .version = sizeof(iface),
  1086. * .seek = ...
  1087. * };
  1088. * stream = SDL_OpenIO(&iface, NULL);
  1089. * ```
  1090. *
  1091. * \threadsafety It is safe to call this macro from any thread.
  1092. *
  1093. * \since This macro is available since SDL 3.1.3.
  1094. *
  1095. * \sa SDL_IOStreamInterface
  1096. * \sa SDL_StorageInterface
  1097. * \sa SDL_VirtualJoystickDesc
  1098. */
  1099. #define SDL_INIT_INTERFACE(iface) \
  1100. do { \
  1101. SDL_zerop(iface); \
  1102. (iface)->version = sizeof(*(iface)); \
  1103. } while (0)
  1104. #ifdef SDL_WIKI_DOCUMENTATION_SECTION
  1105. /**
  1106. * Allocate memory on the stack (maybe).
  1107. *
  1108. * If SDL knows how to access alloca() on the current platform, it will use it
  1109. * to stack-allocate memory here. If it doesn't, it will use SDL_malloc() to
  1110. * heap-allocate memory.
  1111. *
  1112. * Since this might not be stack memory at all, it's important that you check
  1113. * the returned pointer for NULL, and that you call SDL_stack_free on the
  1114. * memory when done with it. Since this might be stack memory, it's important
  1115. * that you don't allocate large amounts of it, or allocate in a loop without
  1116. * returning from the function, so the stack doesn't overflow.
  1117. *
  1118. * \param type the datatype of the memory to allocate.
  1119. * \param count the number of `type` objects to allocate.
  1120. * \returns newly-allocated memory, or NULL on failure.
  1121. *
  1122. * \threadsafety It is safe to call this macro from any thread.
  1123. *
  1124. * \since This macro is available since SDL 3.1.3.
  1125. *
  1126. * \sa SDL_stack_free
  1127. */
  1128. #define SDL_stack_alloc(type, count) (type*)alloca(sizeof(type)*(count))
  1129. /**
  1130. * Free memory previously allocated with SDL_stack_alloc.
  1131. *
  1132. * If SDL used alloca() to allocate this memory, this macro does nothing and
  1133. * the allocated memory will be automatically released when the function that
  1134. * called SDL_stack_alloc() returns. If SDL used SDL_malloc(), it will
  1135. * SDL_free the memory immediately.
  1136. *
  1137. * \param data the pointer, from SDL_stack_alloc(), to free.
  1138. *
  1139. * \threadsafety It is safe to call this macro from any thread.
  1140. *
  1141. * \since This macro is available since SDL 3.1.3.
  1142. *
  1143. * \sa SDL_stack_alloc
  1144. */
  1145. #define SDL_stack_free(data)
  1146. #elif !defined(SDL_DISABLE_ALLOCA)
  1147. #define SDL_stack_alloc(type, count) (type*)alloca(sizeof(type)*(count))
  1148. #define SDL_stack_free(data)
  1149. #else
  1150. #define SDL_stack_alloc(type, count) (type*)SDL_malloc(sizeof(type)*(count))
  1151. #define SDL_stack_free(data) SDL_free(data)
  1152. #endif
  1153. /**
  1154. * Allocate uninitialized memory.
  1155. *
  1156. * The allocated memory returned by this function must be freed with
  1157. * SDL_free().
  1158. *
  1159. * If `size` is 0, it will be set to 1.
  1160. *
  1161. * If you want to allocate memory aligned to a specific alignment, consider
  1162. * using SDL_aligned_alloc().
  1163. *
  1164. * \param size the size to allocate.
  1165. * \returns a pointer to the allocated memory, or NULL if allocation failed.
  1166. *
  1167. * \threadsafety It is safe to call this function from any thread.
  1168. *
  1169. * \since This function is available since SDL 3.1.3.
  1170. *
  1171. * \sa SDL_free
  1172. * \sa SDL_calloc
  1173. * \sa SDL_realloc
  1174. * \sa SDL_aligned_alloc
  1175. */
  1176. extern SDL_DECLSPEC SDL_MALLOC void * SDLCALL SDL_malloc(size_t size);
  1177. /**
  1178. * Allocate a zero-initialized array.
  1179. *
  1180. * The memory returned by this function must be freed with SDL_free().
  1181. *
  1182. * If either of `nmemb` or `size` is 0, they will both be set to 1.
  1183. *
  1184. * \param nmemb the number of elements in the array.
  1185. * \param size the size of each element of the array.
  1186. * \returns a pointer to the allocated array, or NULL if allocation failed.
  1187. *
  1188. * \threadsafety It is safe to call this function from any thread.
  1189. *
  1190. * \since This function is available since SDL 3.1.3.
  1191. *
  1192. * \sa SDL_free
  1193. * \sa SDL_malloc
  1194. * \sa SDL_realloc
  1195. */
  1196. extern SDL_DECLSPEC SDL_MALLOC SDL_ALLOC_SIZE2(1, 2) void * SDLCALL SDL_calloc(size_t nmemb, size_t size);
  1197. /**
  1198. * Change the size of allocated memory.
  1199. *
  1200. * The memory returned by this function must be freed with SDL_free().
  1201. *
  1202. * If `size` is 0, it will be set to 1. Note that this is unlike some other C
  1203. * runtime `realloc` implementations, which may treat `realloc(mem, 0)` the
  1204. * same way as `free(mem)`.
  1205. *
  1206. * If `mem` is NULL, the behavior of this function is equivalent to
  1207. * SDL_malloc(). Otherwise, the function can have one of three possible
  1208. * outcomes:
  1209. *
  1210. * - If it returns the same pointer as `mem`, it means that `mem` was resized
  1211. * in place without freeing.
  1212. * - If it returns a different non-NULL pointer, it means that `mem` was freed
  1213. * and cannot be dereferenced anymore.
  1214. * - If it returns NULL (indicating failure), then `mem` will remain valid and
  1215. * must still be freed with SDL_free().
  1216. *
  1217. * \param mem a pointer to allocated memory to reallocate, or NULL.
  1218. * \param size the new size of the memory.
  1219. * \returns a pointer to the newly allocated memory, or NULL if allocation
  1220. * failed.
  1221. *
  1222. * \threadsafety It is safe to call this function from any thread.
  1223. *
  1224. * \since This function is available since SDL 3.1.3.
  1225. *
  1226. * \sa SDL_free
  1227. * \sa SDL_malloc
  1228. * \sa SDL_calloc
  1229. */
  1230. extern SDL_DECLSPEC SDL_ALLOC_SIZE(2) void * SDLCALL SDL_realloc(void *mem, size_t size);
  1231. /**
  1232. * Free allocated memory.
  1233. *
  1234. * The pointer is no longer valid after this call and cannot be dereferenced
  1235. * anymore.
  1236. *
  1237. * If `mem` is NULL, this function does nothing.
  1238. *
  1239. * \param mem a pointer to allocated memory, or NULL.
  1240. *
  1241. * \threadsafety It is safe to call this function from any thread.
  1242. *
  1243. * \since This function is available since SDL 3.1.3.
  1244. *
  1245. * \sa SDL_malloc
  1246. * \sa SDL_calloc
  1247. * \sa SDL_realloc
  1248. */
  1249. extern SDL_DECLSPEC void SDLCALL SDL_free(void *mem);
  1250. /**
  1251. * A callback used to implement SDL_malloc().
  1252. *
  1253. * SDL will always ensure that the passed `size` is greater than 0.
  1254. *
  1255. * \param size the size to allocate.
  1256. * \returns a pointer to the allocated memory, or NULL if allocation failed.
  1257. *
  1258. * \threadsafety It should be safe to call this callback from any thread.
  1259. *
  1260. * \since This datatype is available since SDL 3.1.3.
  1261. *
  1262. * \sa SDL_malloc
  1263. * \sa SDL_GetOriginalMemoryFunctions
  1264. * \sa SDL_GetMemoryFunctions
  1265. * \sa SDL_SetMemoryFunctions
  1266. */
  1267. typedef void *(SDLCALL *SDL_malloc_func)(size_t size);
  1268. /**
  1269. * A callback used to implement SDL_calloc().
  1270. *
  1271. * SDL will always ensure that the passed `nmemb` and `size` are both greater
  1272. * than 0.
  1273. *
  1274. * \param nmemb the number of elements in the array.
  1275. * \param size the size of each element of the array.
  1276. * \returns a pointer to the allocated array, or NULL if allocation failed.
  1277. *
  1278. * \threadsafety It should be safe to call this callback from any thread.
  1279. *
  1280. * \since This datatype is available since SDL 3.1.3.
  1281. *
  1282. * \sa SDL_calloc
  1283. * \sa SDL_GetOriginalMemoryFunctions
  1284. * \sa SDL_GetMemoryFunctions
  1285. * \sa SDL_SetMemoryFunctions
  1286. */
  1287. typedef void *(SDLCALL *SDL_calloc_func)(size_t nmemb, size_t size);
  1288. /**
  1289. * A callback used to implement SDL_realloc().
  1290. *
  1291. * SDL will always ensure that the passed `size` is greater than 0.
  1292. *
  1293. * \param mem a pointer to allocated memory to reallocate, or NULL.
  1294. * \param size the new size of the memory.
  1295. * \returns a pointer to the newly allocated memory, or NULL if allocation
  1296. * failed.
  1297. *
  1298. * \threadsafety It should be safe to call this callback from any thread.
  1299. *
  1300. * \since This datatype is available since SDL 3.1.3.
  1301. *
  1302. * \sa SDL_realloc
  1303. * \sa SDL_GetOriginalMemoryFunctions
  1304. * \sa SDL_GetMemoryFunctions
  1305. * \sa SDL_SetMemoryFunctions
  1306. */
  1307. typedef void *(SDLCALL *SDL_realloc_func)(void *mem, size_t size);
  1308. /**
  1309. * A callback used to implement SDL_free().
  1310. *
  1311. * SDL will always ensure that the passed `mem` is a non-NULL pointer.
  1312. *
  1313. * \param mem a pointer to allocated memory.
  1314. *
  1315. * \threadsafety It should be safe to call this callback from any thread.
  1316. *
  1317. * \since This datatype is available since SDL 3.1.3.
  1318. *
  1319. * \sa SDL_free
  1320. * \sa SDL_GetOriginalMemoryFunctions
  1321. * \sa SDL_GetMemoryFunctions
  1322. * \sa SDL_SetMemoryFunctions
  1323. */
  1324. typedef void (SDLCALL *SDL_free_func)(void *mem);
  1325. /**
  1326. * Get the original set of SDL memory functions.
  1327. *
  1328. * This is what SDL_malloc and friends will use by default, if there has been
  1329. * no call to SDL_SetMemoryFunctions. This is not necessarily using the C
  1330. * runtime's `malloc` functions behind the scenes! Different platforms and
  1331. * build configurations might do any number of unexpected things.
  1332. *
  1333. * \param malloc_func filled with malloc function.
  1334. * \param calloc_func filled with calloc function.
  1335. * \param realloc_func filled with realloc function.
  1336. * \param free_func filled with free function.
  1337. *
  1338. * \threadsafety It is safe to call this function from any thread.
  1339. *
  1340. * \since This function is available since SDL 3.1.3.
  1341. */
  1342. extern SDL_DECLSPEC void SDLCALL SDL_GetOriginalMemoryFunctions(SDL_malloc_func *malloc_func,
  1343. SDL_calloc_func *calloc_func,
  1344. SDL_realloc_func *realloc_func,
  1345. SDL_free_func *free_func);
  1346. /**
  1347. * Get the current set of SDL memory functions.
  1348. *
  1349. * \param malloc_func filled with malloc function.
  1350. * \param calloc_func filled with calloc function.
  1351. * \param realloc_func filled with realloc function.
  1352. * \param free_func filled with free function.
  1353. *
  1354. * \threadsafety This does not hold a lock, so do not call this in the
  1355. * unlikely event of a background thread calling
  1356. * SDL_SetMemoryFunctions simultaneously.
  1357. *
  1358. * \since This function is available since SDL 3.1.3.
  1359. *
  1360. * \sa SDL_SetMemoryFunctions
  1361. * \sa SDL_GetOriginalMemoryFunctions
  1362. */
  1363. extern SDL_DECLSPEC void SDLCALL SDL_GetMemoryFunctions(SDL_malloc_func *malloc_func,
  1364. SDL_calloc_func *calloc_func,
  1365. SDL_realloc_func *realloc_func,
  1366. SDL_free_func *free_func);
  1367. /**
  1368. * Replace SDL's memory allocation functions with a custom set.
  1369. *
  1370. * It is not safe to call this function once any allocations have been made,
  1371. * as future calls to SDL_free will use the new allocator, even if they came
  1372. * from an SDL_malloc made with the old one!
  1373. *
  1374. * If used, usually this needs to be the first call made into the SDL library,
  1375. * if not the very first thing done at program startup time.
  1376. *
  1377. * \param malloc_func custom malloc function.
  1378. * \param calloc_func custom calloc function.
  1379. * \param realloc_func custom realloc function.
  1380. * \param free_func custom free function.
  1381. * \returns true on success or false on failure; call SDL_GetError() for more
  1382. * information.
  1383. *
  1384. * \threadsafety It is safe to call this function from any thread, but one
  1385. * should not replace the memory functions once any allocations
  1386. * are made!
  1387. *
  1388. * \since This function is available since SDL 3.1.3.
  1389. *
  1390. * \sa SDL_GetMemoryFunctions
  1391. * \sa SDL_GetOriginalMemoryFunctions
  1392. */
  1393. extern SDL_DECLSPEC bool SDLCALL SDL_SetMemoryFunctions(SDL_malloc_func malloc_func,
  1394. SDL_calloc_func calloc_func,
  1395. SDL_realloc_func realloc_func,
  1396. SDL_free_func free_func);
  1397. /**
  1398. * Allocate memory aligned to a specific alignment.
  1399. *
  1400. * The memory returned by this function must be freed with SDL_aligned_free(),
  1401. * _not_ SDL_free().
  1402. *
  1403. * If `alignment` is less than the size of `void *`, it will be increased to
  1404. * match that.
  1405. *
  1406. * The returned memory address will be a multiple of the alignment value, and
  1407. * the size of the memory allocated will be a multiple of the alignment value.
  1408. *
  1409. * \param alignment the alignment of the memory.
  1410. * \param size the size to allocate.
  1411. * \returns a pointer to the aligned memory, or NULL if allocation failed.
  1412. *
  1413. * \threadsafety It is safe to call this function from any thread.
  1414. *
  1415. * \since This function is available since SDL 3.1.3.
  1416. *
  1417. * \sa SDL_aligned_free
  1418. */
  1419. extern SDL_DECLSPEC SDL_MALLOC void * SDLCALL SDL_aligned_alloc(size_t alignment, size_t size);
  1420. /**
  1421. * Free memory allocated by SDL_aligned_alloc().
  1422. *
  1423. * The pointer is no longer valid after this call and cannot be dereferenced
  1424. * anymore.
  1425. *
  1426. * If `mem` is NULL, this function does nothing.
  1427. *
  1428. * \param mem a pointer previously returned by SDL_aligned_alloc(), or NULL.
  1429. *
  1430. * \threadsafety It is safe to call this function from any thread.
  1431. *
  1432. * \since This function is available since SDL 3.1.3.
  1433. *
  1434. * \sa SDL_aligned_alloc
  1435. */
  1436. extern SDL_DECLSPEC void SDLCALL SDL_aligned_free(void *mem);
  1437. /**
  1438. * Get the number of outstanding (unfreed) allocations.
  1439. *
  1440. * \returns the number of allocations.
  1441. *
  1442. * \threadsafety It is safe to call this function from any thread.
  1443. *
  1444. * \since This function is available since SDL 3.1.3.
  1445. */
  1446. extern SDL_DECLSPEC int SDLCALL SDL_GetNumAllocations(void);
  1447. /**
  1448. * A thread-safe set of environment variables
  1449. *
  1450. * \since This struct is available since SDL 3.1.3.
  1451. *
  1452. * \sa SDL_GetEnvironment
  1453. * \sa SDL_CreateEnvironment
  1454. * \sa SDL_GetEnvironmentVariable
  1455. * \sa SDL_GetEnvironmentVariables
  1456. * \sa SDL_SetEnvironmentVariable
  1457. * \sa SDL_UnsetEnvironmentVariable
  1458. * \sa SDL_DestroyEnvironment
  1459. */
  1460. typedef struct SDL_Environment SDL_Environment;
  1461. /**
  1462. * Get the process environment.
  1463. *
  1464. * This is initialized at application start and is not affected by setenv()
  1465. * and unsetenv() calls after that point. Use SDL_SetEnvironmentVariable() and
  1466. * SDL_UnsetEnvironmentVariable() if you want to modify this environment, or
  1467. * SDL_setenv_unsafe() or SDL_unsetenv_unsafe() if you want changes to persist
  1468. * in the C runtime environment after SDL_Quit().
  1469. *
  1470. * \returns a pointer to the environment for the process or NULL on failure;
  1471. * call SDL_GetError() for more information.
  1472. *
  1473. * \threadsafety It is safe to call this function from any thread.
  1474. *
  1475. * \since This function is available since SDL 3.1.3.
  1476. *
  1477. * \sa SDL_GetEnvironmentVariable
  1478. * \sa SDL_GetEnvironmentVariables
  1479. * \sa SDL_SetEnvironmentVariable
  1480. * \sa SDL_UnsetEnvironmentVariable
  1481. */
  1482. extern SDL_DECLSPEC SDL_Environment * SDLCALL SDL_GetEnvironment(void);
  1483. /**
  1484. * Create a set of environment variables
  1485. *
  1486. * \param populated true to initialize it from the C runtime environment,
  1487. * false to create an empty environment.
  1488. * \returns a pointer to the new environment or NULL on failure; call
  1489. * SDL_GetError() for more information.
  1490. *
  1491. * \threadsafety If `populated` is false, it is safe to call this function
  1492. * from any thread, otherwise it is safe if no other threads are
  1493. * calling setenv() or unsetenv()
  1494. *
  1495. * \since This function is available since SDL 3.1.3.
  1496. *
  1497. * \sa SDL_GetEnvironmentVariable
  1498. * \sa SDL_GetEnvironmentVariables
  1499. * \sa SDL_SetEnvironmentVariable
  1500. * \sa SDL_UnsetEnvironmentVariable
  1501. * \sa SDL_DestroyEnvironment
  1502. */
  1503. extern SDL_DECLSPEC SDL_Environment * SDLCALL SDL_CreateEnvironment(bool populated);
  1504. /**
  1505. * Get the value of a variable in the environment.
  1506. *
  1507. * \param env the environment to query.
  1508. * \param name the name of the variable to get.
  1509. * \returns a pointer to the value of the variable or NULL if it can't be
  1510. * found.
  1511. *
  1512. * \threadsafety It is safe to call this function from any thread.
  1513. *
  1514. * \since This function is available since SDL 3.1.3.
  1515. *
  1516. * \sa SDL_GetEnvironment
  1517. * \sa SDL_CreateEnvironment
  1518. * \sa SDL_GetEnvironmentVariables
  1519. * \sa SDL_SetEnvironmentVariable
  1520. * \sa SDL_UnsetEnvironmentVariable
  1521. */
  1522. extern SDL_DECLSPEC const char * SDLCALL SDL_GetEnvironmentVariable(SDL_Environment *env, const char *name);
  1523. /**
  1524. * Get all variables in the environment.
  1525. *
  1526. * \param env the environment to query.
  1527. * \returns a NULL terminated array of pointers to environment variables in
  1528. * the form "variable=value" or NULL on failure; call SDL_GetError()
  1529. * for more information. This is a single allocation that should be
  1530. * freed with SDL_free() when it is no longer needed.
  1531. *
  1532. * \threadsafety It is safe to call this function from any thread.
  1533. *
  1534. * \since This function is available since SDL 3.1.3.
  1535. *
  1536. * \sa SDL_GetEnvironment
  1537. * \sa SDL_CreateEnvironment
  1538. * \sa SDL_GetEnvironmentVariables
  1539. * \sa SDL_SetEnvironmentVariable
  1540. * \sa SDL_UnsetEnvironmentVariable
  1541. */
  1542. extern SDL_DECLSPEC char ** SDLCALL SDL_GetEnvironmentVariables(SDL_Environment *env);
  1543. /**
  1544. * Set the value of a variable in the environment.
  1545. *
  1546. * \param env the environment to modify.
  1547. * \param name the name of the variable to set.
  1548. * \param value the value of the variable to set.
  1549. * \param overwrite true to overwrite the variable if it exists, false to
  1550. * return success without setting the variable if it already
  1551. * exists.
  1552. * \returns true on success or false on failure; call SDL_GetError() for more
  1553. * information.
  1554. *
  1555. * \threadsafety It is safe to call this function from any thread.
  1556. *
  1557. * \since This function is available since SDL 3.1.3.
  1558. *
  1559. * \sa SDL_GetEnvironment
  1560. * \sa SDL_CreateEnvironment
  1561. * \sa SDL_GetEnvironmentVariable
  1562. * \sa SDL_GetEnvironmentVariables
  1563. * \sa SDL_UnsetEnvironmentVariable
  1564. */
  1565. extern SDL_DECLSPEC bool SDLCALL SDL_SetEnvironmentVariable(SDL_Environment *env, const char *name, const char *value, bool overwrite);
  1566. /**
  1567. * Clear a variable from the environment.
  1568. *
  1569. * \param env the environment to modify.
  1570. * \param name the name of the variable to unset.
  1571. * \returns true on success or false on failure; call SDL_GetError() for more
  1572. * information.
  1573. *
  1574. * \threadsafety It is safe to call this function from any thread.
  1575. *
  1576. * \since This function is available since SDL 3.1.3.
  1577. *
  1578. * \sa SDL_GetEnvironment
  1579. * \sa SDL_CreateEnvironment
  1580. * \sa SDL_GetEnvironmentVariable
  1581. * \sa SDL_GetEnvironmentVariables
  1582. * \sa SDL_SetEnvironmentVariable
  1583. * \sa SDL_UnsetEnvironmentVariable
  1584. */
  1585. extern SDL_DECLSPEC bool SDLCALL SDL_UnsetEnvironmentVariable(SDL_Environment *env, const char *name);
  1586. /**
  1587. * Destroy a set of environment variables.
  1588. *
  1589. * \param env the environment to destroy.
  1590. *
  1591. * \threadsafety It is safe to call this function from any thread, as long as
  1592. * the environment is no longer in use.
  1593. *
  1594. * \since This function is available since SDL 3.1.3.
  1595. *
  1596. * \sa SDL_CreateEnvironment
  1597. */
  1598. extern SDL_DECLSPEC void SDLCALL SDL_DestroyEnvironment(SDL_Environment *env);
  1599. /**
  1600. * Get the value of a variable in the environment.
  1601. *
  1602. * This function uses SDL's cached copy of the environment and is thread-safe.
  1603. *
  1604. * \param name the name of the variable to get.
  1605. * \returns a pointer to the value of the variable or NULL if it can't be
  1606. * found.
  1607. *
  1608. * \threadsafety It is safe to call this function from any thread.
  1609. *
  1610. * \since This function is available since SDL 3.1.3.
  1611. */
  1612. extern SDL_DECLSPEC const char * SDLCALL SDL_getenv(const char *name);
  1613. /**
  1614. * Get the value of a variable in the environment.
  1615. *
  1616. * This function bypasses SDL's cached copy of the environment and is not
  1617. * thread-safe.
  1618. *
  1619. * \param name the name of the variable to get.
  1620. * \returns a pointer to the value of the variable or NULL if it can't be
  1621. * found.
  1622. *
  1623. * \threadsafety This function is not thread safe, consider using SDL_getenv()
  1624. * instead.
  1625. *
  1626. * \since This function is available since SDL 3.1.3.
  1627. *
  1628. * \sa SDL_getenv
  1629. */
  1630. extern SDL_DECLSPEC const char * SDLCALL SDL_getenv_unsafe(const char *name);
  1631. /**
  1632. * Set the value of a variable in the environment.
  1633. *
  1634. * \param name the name of the variable to set.
  1635. * \param value the value of the variable to set.
  1636. * \param overwrite 1 to overwrite the variable if it exists, 0 to return
  1637. * success without setting the variable if it already exists.
  1638. * \returns 0 on success, -1 on error.
  1639. *
  1640. * \threadsafety This function is not thread safe, consider using
  1641. * SDL_SetEnvironmentVariable() instead.
  1642. *
  1643. * \since This function is available since SDL 3.1.3.
  1644. *
  1645. * \sa SDL_SetEnvironmentVariable
  1646. */
  1647. extern SDL_DECLSPEC int SDLCALL SDL_setenv_unsafe(const char *name, const char *value, int overwrite);
  1648. /**
  1649. * Clear a variable from the environment.
  1650. *
  1651. * \param name the name of the variable to unset.
  1652. * \returns 0 on success, -1 on error.
  1653. *
  1654. * \threadsafety This function is not thread safe, consider using
  1655. * SDL_UnsetEnvironmentVariable() instead.
  1656. *
  1657. * \since This function is available since SDL 3.1.3.
  1658. *
  1659. * \sa SDL_UnsetEnvironmentVariable
  1660. */
  1661. extern SDL_DECLSPEC int SDLCALL SDL_unsetenv_unsafe(const char *name);
  1662. /**
  1663. * A callback used with SDL sorting and binary search functions.
  1664. *
  1665. * \param a a pointer to the first element being compared.
  1666. * \param b a pointer to the second element being compared.
  1667. * \returns -1 if `a` should be sorted before `b`, 1 if `b` should be sorted
  1668. * before `a`, 0 if they are equal. If two elements are equal, their
  1669. * order in the sorted array is undefined.
  1670. *
  1671. * \since This callback is available since SDL 3.1.3.
  1672. *
  1673. * \sa SDL_bsearch
  1674. * \sa SDL_qsort
  1675. */
  1676. typedef int (SDLCALL *SDL_CompareCallback)(const void *a, const void *b);
  1677. /**
  1678. * Sort an array.
  1679. *
  1680. * For example:
  1681. *
  1682. * ```c
  1683. * typedef struct {
  1684. * int key;
  1685. * const char *string;
  1686. * } data;
  1687. *
  1688. * int SDLCALL compare(const void *a, const void *b)
  1689. * {
  1690. * const data *A = (const data *)a;
  1691. * const data *B = (const data *)b;
  1692. *
  1693. * if (A->n < B->n) {
  1694. * return -1;
  1695. * } else if (B->n < A->n) {
  1696. * return 1;
  1697. * } else {
  1698. * return 0;
  1699. * }
  1700. * }
  1701. *
  1702. * data values[] = {
  1703. * { 3, "third" }, { 1, "first" }, { 2, "second" }
  1704. * };
  1705. *
  1706. * SDL_qsort(values, SDL_arraysize(values), sizeof(values[0]), compare);
  1707. * ```
  1708. *
  1709. * \param base a pointer to the start of the array.
  1710. * \param nmemb the number of elements in the array.
  1711. * \param size the size of the elements in the array.
  1712. * \param compare a function used to compare elements in the array.
  1713. *
  1714. * \threadsafety It is safe to call this function from any thread.
  1715. *
  1716. * \since This function is available since SDL 3.1.3.
  1717. *
  1718. * \sa SDL_bsearch
  1719. * \sa SDL_qsort_r
  1720. */
  1721. extern SDL_DECLSPEC void SDLCALL SDL_qsort(void *base, size_t nmemb, size_t size, SDL_CompareCallback compare);
  1722. /**
  1723. * Perform a binary search on a previously sorted array.
  1724. *
  1725. * For example:
  1726. *
  1727. * ```c
  1728. * typedef struct {
  1729. * int key;
  1730. * const char *string;
  1731. * } data;
  1732. *
  1733. * int SDLCALL compare(const void *a, const void *b)
  1734. * {
  1735. * const data *A = (const data *)a;
  1736. * const data *B = (const data *)b;
  1737. *
  1738. * if (A->n < B->n) {
  1739. * return -1;
  1740. * } else if (B->n < A->n) {
  1741. * return 1;
  1742. * } else {
  1743. * return 0;
  1744. * }
  1745. * }
  1746. *
  1747. * data values[] = {
  1748. * { 1, "first" }, { 2, "second" }, { 3, "third" }
  1749. * };
  1750. * data key = { 2, NULL };
  1751. *
  1752. * data *result = SDL_bsearch(&key, values, SDL_arraysize(values), sizeof(values[0]), compare);
  1753. * ```
  1754. *
  1755. * \param key a pointer to a key equal to the element being searched for.
  1756. * \param base a pointer to the start of the array.
  1757. * \param nmemb the number of elements in the array.
  1758. * \param size the size of the elements in the array.
  1759. * \param compare a function used to compare elements in the array.
  1760. * \returns a pointer to the matching element in the array, or NULL if not
  1761. * found.
  1762. *
  1763. * \threadsafety It is safe to call this function from any thread.
  1764. *
  1765. * \since This function is available since SDL 3.1.3.
  1766. *
  1767. * \sa SDL_bsearch_r
  1768. * \sa SDL_qsort
  1769. */
  1770. extern SDL_DECLSPEC void * SDLCALL SDL_bsearch(const void *key, const void *base, size_t nmemb, size_t size, SDL_CompareCallback compare);
  1771. /**
  1772. * A callback used with SDL sorting and binary search functions.
  1773. *
  1774. * \param userdata the `userdata` pointer passed to the sort function.
  1775. * \param a a pointer to the first element being compared.
  1776. * \param b a pointer to the second element being compared.
  1777. * \returns -1 if `a` should be sorted before `b`, 1 if `b` should be sorted
  1778. * before `a`, 0 if they are equal. If two elements are equal, their
  1779. * order in the sorted array is undefined.
  1780. *
  1781. * \since This callback is available since SDL 3.1.3.
  1782. *
  1783. * \sa SDL_qsort_r
  1784. * \sa SDL_bsearch_r
  1785. */
  1786. typedef int (SDLCALL *SDL_CompareCallback_r)(void *userdata, const void *a, const void *b);
  1787. /**
  1788. * Sort an array, passing a userdata pointer to the compare function.
  1789. *
  1790. * For example:
  1791. *
  1792. * ```c
  1793. * typedef enum {
  1794. * sort_increasing,
  1795. * sort_decreasing,
  1796. * } sort_method;
  1797. *
  1798. * typedef struct {
  1799. * int key;
  1800. * const char *string;
  1801. * } data;
  1802. *
  1803. * int SDLCALL compare(const void *userdata, const void *a, const void *b)
  1804. * {
  1805. * sort_method method = (sort_method)(uintptr_t)userdata;
  1806. * const data *A = (const data *)a;
  1807. * const data *B = (const data *)b;
  1808. *
  1809. * if (A->key < B->key) {
  1810. * return (method == sort_increasing) ? -1 : 1;
  1811. * } else if (B->key < A->key) {
  1812. * return (method == sort_increasing) ? 1 : -1;
  1813. * } else {
  1814. * return 0;
  1815. * }
  1816. * }
  1817. *
  1818. * data values[] = {
  1819. * { 3, "third" }, { 1, "first" }, { 2, "second" }
  1820. * };
  1821. *
  1822. * SDL_qsort_r(values, SDL_arraysize(values), sizeof(values[0]), compare, (const void *)(uintptr_t)sort_increasing);
  1823. * ```
  1824. *
  1825. * \param base a pointer to the start of the array.
  1826. * \param nmemb the number of elements in the array.
  1827. * \param size the size of the elements in the array.
  1828. * \param compare a function used to compare elements in the array.
  1829. * \param userdata a pointer to pass to the compare function.
  1830. *
  1831. * \threadsafety It is safe to call this function from any thread.
  1832. *
  1833. * \since This function is available since SDL 3.1.3.
  1834. *
  1835. * \sa SDL_bsearch_r
  1836. * \sa SDL_qsort
  1837. */
  1838. extern SDL_DECLSPEC void SDLCALL SDL_qsort_r(void *base, size_t nmemb, size_t size, SDL_CompareCallback_r compare, void *userdata);
  1839. /**
  1840. * Perform a binary search on a previously sorted array, passing a userdata
  1841. * pointer to the compare function.
  1842. *
  1843. * For example:
  1844. *
  1845. * ```c
  1846. * typedef enum {
  1847. * sort_increasing,
  1848. * sort_decreasing,
  1849. * } sort_method;
  1850. *
  1851. * typedef struct {
  1852. * int key;
  1853. * const char *string;
  1854. * } data;
  1855. *
  1856. * int SDLCALL compare(const void *userdata, const void *a, const void *b)
  1857. * {
  1858. * sort_method method = (sort_method)(uintptr_t)userdata;
  1859. * const data *A = (const data *)a;
  1860. * const data *B = (const data *)b;
  1861. *
  1862. * if (A->key < B->key) {
  1863. * return (method == sort_increasing) ? -1 : 1;
  1864. * } else if (B->key < A->key) {
  1865. * return (method == sort_increasing) ? 1 : -1;
  1866. * } else {
  1867. * return 0;
  1868. * }
  1869. * }
  1870. *
  1871. * data values[] = {
  1872. * { 1, "first" }, { 2, "second" }, { 3, "third" }
  1873. * };
  1874. * data key = { 2, NULL };
  1875. *
  1876. * data *result = SDL_bsearch_r(&key, values, SDL_arraysize(values), sizeof(values[0]), compare, (const void *)(uintptr_t)sort_increasing);
  1877. * ```
  1878. *
  1879. * \param key a pointer to a key equal to the element being searched for.
  1880. * \param base a pointer to the start of the array.
  1881. * \param nmemb the number of elements in the array.
  1882. * \param size the size of the elements in the array.
  1883. * \param compare a function used to compare elements in the array.
  1884. * \param userdata a pointer to pass to the compare function.
  1885. * \returns a pointer to the matching element in the array, or NULL if not
  1886. * found.
  1887. *
  1888. * \threadsafety It is safe to call this function from any thread.
  1889. *
  1890. * \since This function is available since SDL 3.1.3.
  1891. *
  1892. * \sa SDL_bsearch
  1893. * \sa SDL_qsort_r
  1894. */
  1895. 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);
  1896. /**
  1897. * Compute the absolute value of `x`.
  1898. *
  1899. * \param x an integer value.
  1900. * \returns the absolute value of x.
  1901. *
  1902. * \threadsafety It is safe to call this function from any thread.
  1903. *
  1904. * \since This function is available since SDL 3.1.3.
  1905. */
  1906. extern SDL_DECLSPEC int SDLCALL SDL_abs(int x);
  1907. /**
  1908. * Return the lesser of two values.
  1909. *
  1910. * This is a helper macro that might be more clear than writing out the
  1911. * comparisons directly, and works with any type that can be compared with the
  1912. * `<` operator. However, it double-evaluates both its parameters, so do not
  1913. * use expressions with side-effects here.
  1914. *
  1915. * \param x the first value to compare.
  1916. * \param y the second value to compare.
  1917. * \returns the lesser of `x` and `y`.
  1918. *
  1919. * \threadsafety It is safe to call this macro from any thread.
  1920. *
  1921. * \since This macro is available since SDL 3.1.3.
  1922. */
  1923. #define SDL_min(x, y) (((x) < (y)) ? (x) : (y))
  1924. /**
  1925. * Return the greater of two values.
  1926. *
  1927. * This is a helper macro that might be more clear than writing out the
  1928. * comparisons directly, and works with any type that can be compared with the
  1929. * `>` operator. However, it double-evaluates both its parameters, so do not
  1930. * use expressions with side-effects here.
  1931. *
  1932. * \param x the first value to compare.
  1933. * \param y the second value to compare.
  1934. * \returns the lesser of `x` and `y`.
  1935. *
  1936. * \threadsafety It is safe to call this macro from any thread.
  1937. *
  1938. * \since This macro is available since SDL 3.1.3.
  1939. */
  1940. #define SDL_max(x, y) (((x) > (y)) ? (x) : (y))
  1941. /**
  1942. * Return a value clamped to a range.
  1943. *
  1944. * If `x` is outside the range a values between `a` and `b`, the returned
  1945. * value will be `a` or `b` as appropriate. Otherwise, `x` is returned.
  1946. *
  1947. * This macro will produce incorrect results if `b` is less than `a`.
  1948. *
  1949. * This is a helper macro that might be more clear than writing out the
  1950. * comparisons directly, and works with any type that can be compared with the
  1951. * `<` and `>` operators. However, it double-evaluates all its parameters, so
  1952. * do not use expressions with side-effects here.
  1953. *
  1954. * \param x the value to compare.
  1955. * \param a the low end value.
  1956. * \param b the high end value.
  1957. * \returns x, clamped between a and b.
  1958. *
  1959. * \threadsafety It is safe to call this macro from any thread.
  1960. *
  1961. * \since This macro is available since SDL 3.1.3.
  1962. */
  1963. #define SDL_clamp(x, a, b) (((x) < (a)) ? (a) : (((x) > (b)) ? (b) : (x)))
  1964. /**
  1965. * Query if a character is alphabetic (a letter).
  1966. *
  1967. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  1968. * for English 'a-z' and 'A-Z' as true.
  1969. *
  1970. * \param x character value to check.
  1971. * \returns non-zero if x falls within the character class, zero otherwise.
  1972. *
  1973. * \threadsafety It is safe to call this function from any thread.
  1974. *
  1975. * \since This function is available since SDL 3.1.3.
  1976. */
  1977. extern SDL_DECLSPEC int SDLCALL SDL_isalpha(int x);
  1978. /**
  1979. * Query if a character is alphabetic (a letter) or a number.
  1980. *
  1981. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  1982. * for English 'a-z', 'A-Z', and '0-9' as true.
  1983. *
  1984. * \param x character value to check.
  1985. * \returns non-zero if x falls within the character class, zero otherwise.
  1986. *
  1987. * \threadsafety It is safe to call this function from any thread.
  1988. *
  1989. * \since This function is available since SDL 3.1.3.
  1990. */
  1991. extern SDL_DECLSPEC int SDLCALL SDL_isalnum(int x);
  1992. /**
  1993. * Report if a character is blank (a space or tab).
  1994. *
  1995. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  1996. * 0x20 (space) or 0x9 (tab) as true.
  1997. *
  1998. * \param x character value to check.
  1999. * \returns non-zero if x falls within the character class, zero otherwise.
  2000. *
  2001. * \threadsafety It is safe to call this function from any thread.
  2002. *
  2003. * \since This function is available since SDL 3.1.3.
  2004. */
  2005. extern SDL_DECLSPEC int SDLCALL SDL_isblank(int x);
  2006. /**
  2007. * Report if a character is a control character.
  2008. *
  2009. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  2010. * 0 through 0x1F, and 0x7F, as true.
  2011. *
  2012. * \param x character value to check.
  2013. * \returns non-zero if x falls within the character class, zero otherwise.
  2014. *
  2015. * \threadsafety It is safe to call this function from any thread.
  2016. *
  2017. * \since This function is available since SDL 3.1.3.
  2018. */
  2019. extern SDL_DECLSPEC int SDLCALL SDL_iscntrl(int x);
  2020. /**
  2021. * Report if a character is a numeric digit.
  2022. *
  2023. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  2024. * '0' (0x30) through '9' (0x39), as true.
  2025. *
  2026. * \param x character value to check.
  2027. * \returns non-zero if x falls within the character class, zero otherwise.
  2028. *
  2029. * \threadsafety It is safe to call this function from any thread.
  2030. *
  2031. * \since This function is available since SDL 3.1.3.
  2032. */
  2033. extern SDL_DECLSPEC int SDLCALL SDL_isdigit(int x);
  2034. /**
  2035. * Report if a character is a hexadecimal digit.
  2036. *
  2037. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  2038. * 'A' through 'F', 'a' through 'f', and '0' through '9', as true.
  2039. *
  2040. * \param x character value to check.
  2041. * \returns non-zero if x falls within the character class, zero otherwise.
  2042. *
  2043. * \threadsafety It is safe to call this function from any thread.
  2044. *
  2045. * \since This function is available since SDL 3.1.3.
  2046. */
  2047. extern SDL_DECLSPEC int SDLCALL SDL_isxdigit(int x);
  2048. /**
  2049. * Report if a character is a punctuation mark.
  2050. *
  2051. * **WARNING**: Regardless of system locale, this is equivalent to
  2052. * `((SDL_isgraph(x)) && (!SDL_isalnum(x)))`.
  2053. *
  2054. * \param x character value to check.
  2055. * \returns non-zero if x falls within the character class, zero otherwise.
  2056. *
  2057. * \threadsafety It is safe to call this function from any thread.
  2058. *
  2059. * \since This function is available since SDL 3.1.3.
  2060. *
  2061. * \sa SDL_isgraph
  2062. * \sa SDL_isalnum
  2063. */
  2064. extern SDL_DECLSPEC int SDLCALL SDL_ispunct(int x);
  2065. /**
  2066. * Report if a character is whitespace.
  2067. *
  2068. * **WARNING**: Regardless of system locale, this will only treat the
  2069. * following ASCII values as true:
  2070. *
  2071. * - space (0x20)
  2072. * - tab (0x09)
  2073. * - newline (0x0A)
  2074. * - vertical tab (0x0B)
  2075. * - form feed (0x0C)
  2076. * - return (0x0D)
  2077. *
  2078. * \param x character value to check.
  2079. * \returns non-zero if x falls within the character class, zero otherwise.
  2080. *
  2081. * \threadsafety It is safe to call this function from any thread.
  2082. *
  2083. * \since This function is available since SDL 3.1.3.
  2084. */
  2085. extern SDL_DECLSPEC int SDLCALL SDL_isspace(int x);
  2086. /**
  2087. * Report if a character is upper case.
  2088. *
  2089. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  2090. * 'A' through 'Z' as true.
  2091. *
  2092. * \param x character value to check.
  2093. * \returns non-zero if x falls within the character class, zero otherwise.
  2094. *
  2095. * \threadsafety It is safe to call this function from any thread.
  2096. *
  2097. * \since This function is available since SDL 3.1.3.
  2098. */
  2099. extern SDL_DECLSPEC int SDLCALL SDL_isupper(int x);
  2100. /**
  2101. * Report if a character is lower case.
  2102. *
  2103. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  2104. * 'a' through 'z' as true.
  2105. *
  2106. * \param x character value to check.
  2107. * \returns non-zero if x falls within the character class, zero otherwise.
  2108. *
  2109. * \threadsafety It is safe to call this function from any thread.
  2110. *
  2111. * \since This function is available since SDL 3.1.3.
  2112. */
  2113. extern SDL_DECLSPEC int SDLCALL SDL_islower(int x);
  2114. /**
  2115. * Report if a character is "printable".
  2116. *
  2117. * Be advised that "printable" has a definition that goes back to text
  2118. * terminals from the dawn of computing, making this a sort of special case
  2119. * function that is not suitable for Unicode (or most any) text management.
  2120. *
  2121. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  2122. * ' ' (0x20) through '~' (0x7E) as true.
  2123. *
  2124. * \param x character value to check.
  2125. * \returns non-zero if x falls within the character class, zero otherwise.
  2126. *
  2127. * \threadsafety It is safe to call this function from any thread.
  2128. *
  2129. * \since This function is available since SDL 3.1.3.
  2130. */
  2131. extern SDL_DECLSPEC int SDLCALL SDL_isprint(int x);
  2132. /**
  2133. * Report if a character is any "printable" except space.
  2134. *
  2135. * Be advised that "printable" has a definition that goes back to text
  2136. * terminals from the dawn of computing, making this a sort of special case
  2137. * function that is not suitable for Unicode (or most any) text management.
  2138. *
  2139. * **WARNING**: Regardless of system locale, this is equivalent to
  2140. * `(SDL_isprint(x)) && ((x) != ' ')`.
  2141. *
  2142. * \param x character value to check.
  2143. * \returns non-zero if x falls within the character class, zero otherwise.
  2144. *
  2145. * \threadsafety It is safe to call this function from any thread.
  2146. *
  2147. * \since This function is available since SDL 3.1.3.
  2148. *
  2149. * \sa SDL_isprint
  2150. */
  2151. extern SDL_DECLSPEC int SDLCALL SDL_isgraph(int x);
  2152. /**
  2153. * Convert low-ASCII English letters to uppercase.
  2154. *
  2155. * **WARNING**: Regardless of system locale, this will only convert ASCII
  2156. * values 'a' through 'z' to uppercase.
  2157. *
  2158. * This function returns the uppercase equivalent of `x`. If a character
  2159. * cannot be converted, or is already uppercase, this function returns `x`.
  2160. *
  2161. * \param x character value to check.
  2162. * \returns capitalized version of x, or x if no conversion available.
  2163. *
  2164. * \threadsafety It is safe to call this function from any thread.
  2165. *
  2166. * \since This function is available since SDL 3.1.3.
  2167. */
  2168. extern SDL_DECLSPEC int SDLCALL SDL_toupper(int x);
  2169. /**
  2170. * Convert low-ASCII English letters to lowercase.
  2171. *
  2172. * **WARNING**: Regardless of system locale, this will only convert ASCII
  2173. * values 'A' through 'Z' to lowercase.
  2174. *
  2175. * This function returns the lowercase equivalent of `x`. If a character
  2176. * cannot be converted, or is already lowercase, this function returns `x`.
  2177. *
  2178. * \param x character value to check.
  2179. * \returns lowercase version of x, or x if no conversion available.
  2180. *
  2181. * \threadsafety It is safe to call this function from any thread.
  2182. *
  2183. * \since This function is available since SDL 3.1.3.
  2184. */
  2185. extern SDL_DECLSPEC int SDLCALL SDL_tolower(int x);
  2186. /**
  2187. * Calculate a CRC-16 value.
  2188. *
  2189. * https://en.wikipedia.org/wiki/Cyclic_redundancy_check
  2190. *
  2191. * This function can be called multiple times, to stream data to be
  2192. * checksummed in blocks. Each call must provide the previous CRC-16 return
  2193. * value to be updated with the next block. The first call to this function
  2194. * for a set of blocks should pass in a zero CRC value.
  2195. *
  2196. * \param crc the current checksum for this data set, or 0 for a new data set.
  2197. * \param data a new block of data to add to the checksum.
  2198. * \param len the size, in bytes, of the new block of data.
  2199. * \returns a CRC-16 checksum value of all blocks in the data set.
  2200. *
  2201. * \threadsafety It is safe to call this function from any thread.
  2202. *
  2203. * \since This function is available since SDL 3.1.3.
  2204. */
  2205. extern SDL_DECLSPEC Uint16 SDLCALL SDL_crc16(Uint16 crc, const void *data, size_t len);
  2206. /**
  2207. * Calculate a CRC-32 value.
  2208. *
  2209. * https://en.wikipedia.org/wiki/Cyclic_redundancy_check
  2210. *
  2211. * This function can be called multiple times, to stream data to be
  2212. * checksummed in blocks. Each call must provide the previous CRC-32 return
  2213. * value to be updated with the next block. The first call to this function
  2214. * for a set of blocks should pass in a zero CRC value.
  2215. *
  2216. * \param crc the current checksum for this data set, or 0 for a new data set.
  2217. * \param data a new block of data to add to the checksum.
  2218. * \param len the size, in bytes, of the new block of data.
  2219. * \returns a CRC-32 checksum value of all blocks in the data set.
  2220. *
  2221. * \threadsafety It is safe to call this function from any thread.
  2222. *
  2223. * \since This function is available since SDL 3.1.3.
  2224. */
  2225. extern SDL_DECLSPEC Uint32 SDLCALL SDL_crc32(Uint32 crc, const void *data, size_t len);
  2226. /**
  2227. * Calculate a 32-bit MurmurHash3 value for a block of data.
  2228. *
  2229. * https://en.wikipedia.org/wiki/MurmurHash
  2230. *
  2231. * A seed may be specified, which changes the final results consistently, but
  2232. * this does not work like SDL_crc16 and SDL_crc32: you can't feed a previous
  2233. * result from this function back into itself as the next seed value to
  2234. * calculate a hash in chunks; it won't produce the same hash as it would if
  2235. * the same data was provided in a single call.
  2236. *
  2237. * If you aren't sure what to provide for a seed, zero is fine. Murmur3 is not
  2238. * cryptographically secure, so it shouldn't be used for hashing top-secret
  2239. * data.
  2240. *
  2241. * \param data the data to be hashed.
  2242. * \param len the size of data, in bytes.
  2243. * \param seed a value that alters the final hash value.
  2244. * \returns a Murmur3 32-bit hash value.
  2245. *
  2246. * \threadsafety It is safe to call this function from any thread.
  2247. *
  2248. * \since This function is available since SDL 3.1.3.
  2249. */
  2250. extern SDL_DECLSPEC Uint32 SDLCALL SDL_murmur3_32(const void *data, size_t len, Uint32 seed);
  2251. /**
  2252. * Copy non-overlapping memory.
  2253. *
  2254. * The memory regions must not overlap. If they do, use SDL_memmove() instead.
  2255. *
  2256. * \param dst The destination memory region. Must not be NULL, and must not
  2257. * overlap with `src`.
  2258. * \param src The source memory region. Must not be NULL, and must not overlap
  2259. * with `dst`.
  2260. * \param len The length in bytes of both `dst` and `src`.
  2261. * \returns `dst`.
  2262. *
  2263. * \threadsafety It is safe to call this function from any thread.
  2264. *
  2265. * \since This function is available since SDL 3.1.3.
  2266. *
  2267. * \sa SDL_memmove
  2268. */
  2269. extern SDL_DECLSPEC void * SDLCALL SDL_memcpy(SDL_OUT_BYTECAP(len) void *dst, SDL_IN_BYTECAP(len) const void *src, size_t len);
  2270. /* Take advantage of compiler optimizations for memcpy */
  2271. #ifndef SDL_SLOW_MEMCPY
  2272. #ifdef SDL_memcpy
  2273. #undef SDL_memcpy
  2274. #endif
  2275. #define SDL_memcpy memcpy
  2276. #endif
  2277. /**
  2278. * A macro to copy memory between objects, with basic type checking.
  2279. *
  2280. * SDL_memcpy and SDL_memmove do not care where you copy memory to and from,
  2281. * which can lead to bugs. This macro aims to avoid most of those bugs by
  2282. * making sure that the source and destination are both pointers to objects
  2283. * that are the same size. It does not check that the objects are the same
  2284. * _type_, just that the copy will not overflow either object.
  2285. *
  2286. * The size check happens at compile time, and the compiler will throw an
  2287. * error if the objects are different sizes.
  2288. *
  2289. * Generally this is intended to copy a single object, not an array.
  2290. *
  2291. * This macro looks like it double-evaluates its parameters, but the extras
  2292. * them are in `sizeof` sections, which generate no code nor side-effects.
  2293. *
  2294. * \param dst a pointer to the destination object. Must not be NULL.
  2295. * \param src a pointer to the source object. Must not be NULL.
  2296. *
  2297. * \threadsafety It is safe to call this function from any thread.
  2298. *
  2299. * \since This function is available since SDL 3.1.3.
  2300. */
  2301. #define SDL_copyp(dst, src) \
  2302. { SDL_COMPILE_TIME_ASSERT(SDL_copyp, sizeof (*(dst)) == sizeof (*(src))); } \
  2303. SDL_memcpy((dst), (src), sizeof(*(src)))
  2304. /**
  2305. * Copy memory ranges that might overlap.
  2306. *
  2307. * It is okay for the memory regions to overlap. If you are confident that the
  2308. * regions never overlap, using SDL_memcpy() may improve performance.
  2309. *
  2310. * \param dst The destination memory region. Must not be NULL.
  2311. * \param src The source memory region. Must not be NULL.
  2312. * \param len The length in bytes of both `dst` and `src`.
  2313. * \returns `dst`.
  2314. *
  2315. * \threadsafety It is safe to call this function from any thread.
  2316. *
  2317. * \since This function is available since SDL 3.1.3.
  2318. *
  2319. * \sa SDL_memcpy
  2320. */
  2321. extern SDL_DECLSPEC void * SDLCALL SDL_memmove(SDL_OUT_BYTECAP(len) void *dst, SDL_IN_BYTECAP(len) const void *src, size_t len);
  2322. /* Take advantage of compiler optimizations for memmove */
  2323. #ifndef SDL_SLOW_MEMMOVE
  2324. #ifdef SDL_memmove
  2325. #undef SDL_memmove
  2326. #endif
  2327. #define SDL_memmove memmove
  2328. #endif
  2329. /**
  2330. * Initialize all bytes of buffer of memory to a specific value.
  2331. *
  2332. * This function will set `len` bytes, pointed to by `dst`, to the value
  2333. * specified in `c`.
  2334. *
  2335. * Despite `c` being an `int` instead of a `char`, this only operates on
  2336. * bytes; `c` must be a value between 0 and 255, inclusive.
  2337. *
  2338. * \param dst the destination memory region. Must not be NULL.
  2339. * \param c the byte value to set.
  2340. * \param len the length, in bytes, to set in `dst`.
  2341. * \returns `dst`.
  2342. *
  2343. * \threadsafety It is safe to call this function from any thread.
  2344. *
  2345. * \since This function is available since SDL 3.1.3.
  2346. */
  2347. extern SDL_DECLSPEC void * SDLCALL SDL_memset(SDL_OUT_BYTECAP(len) void *dst, int c, size_t len);
  2348. /**
  2349. * Initialize all 32-bit words of buffer of memory to a specific value.
  2350. *
  2351. * This function will set a buffer of `dwords` Uint32 values, pointed to by
  2352. * `dst`, to the value specified in `val`.
  2353. *
  2354. * Unlike SDL_memset, this sets 32-bit values, not bytes, so it's not limited
  2355. * to a range of 0-255.
  2356. *
  2357. * \param dst the destination memory region. Must not be NULL.
  2358. * \param val the Uint32 value to set.
  2359. * \param dwords the number of Uint32 values to set in `dst`.
  2360. * \returns `dst`.
  2361. *
  2362. * \threadsafety It is safe to call this function from any thread.
  2363. *
  2364. * \since This function is available since SDL 3.1.3.
  2365. */
  2366. extern SDL_DECLSPEC void * SDLCALL SDL_memset4(void *dst, Uint32 val, size_t dwords);
  2367. /* Take advantage of compiler optimizations for memset */
  2368. #ifndef SDL_SLOW_MEMSET
  2369. #ifdef SDL_memset
  2370. #undef SDL_memset
  2371. #endif
  2372. #define SDL_memset memset
  2373. #endif
  2374. /**
  2375. * Clear an object's memory to zero.
  2376. *
  2377. * This is wrapper over SDL_memset that handles calculating the object size,
  2378. * so there's no chance of copy/paste errors, and the code is cleaner.
  2379. *
  2380. * This requires an object, not a pointer to an object, nor an array.
  2381. *
  2382. * \param x the object to clear.
  2383. *
  2384. * \threadsafety It is safe to call this macro from any thread.
  2385. *
  2386. * \since This macro is available since SDL 3.1.3.
  2387. *
  2388. * \sa SDL_zerop
  2389. * \sa SDL_zeroa
  2390. */
  2391. #define SDL_zero(x) SDL_memset(&(x), 0, sizeof((x)))
  2392. /**
  2393. * Clear an object's memory to zero, using a pointer.
  2394. *
  2395. * This is wrapper over SDL_memset that handles calculating the object size,
  2396. * so there's no chance of copy/paste errors, and the code is cleaner.
  2397. *
  2398. * This requires a pointer to an object, not an object itself, nor an array.
  2399. *
  2400. * \param x a pointer to the object to clear.
  2401. *
  2402. * \threadsafety It is safe to call this macro from any thread.
  2403. *
  2404. * \since This macro is available since SDL 3.1.3.
  2405. *
  2406. * \sa SDL_zero
  2407. * \sa SDL_zeroa
  2408. */
  2409. #define SDL_zerop(x) SDL_memset((x), 0, sizeof(*(x)))
  2410. /**
  2411. * Clear an array's memory to zero.
  2412. *
  2413. * This is wrapper over SDL_memset that handles calculating the array size, so
  2414. * there's no chance of copy/paste errors, and the code is cleaner.
  2415. *
  2416. * This requires an array, not an object, nor a pointer to an object.
  2417. *
  2418. * \param x an array to clear.
  2419. *
  2420. * \threadsafety It is safe to call this macro from any thread.
  2421. *
  2422. * \since This macro is available since SDL 3.1.3.
  2423. *
  2424. * \sa SDL_zero
  2425. * \sa SDL_zeroa
  2426. */
  2427. #define SDL_zeroa(x) SDL_memset((x), 0, sizeof((x)))
  2428. /**
  2429. * Compare two buffers of memory.
  2430. *
  2431. * \param s1 the first buffer to compare. NULL is not permitted!
  2432. * \param s2 the second buffer to compare. NULL is not permitted!
  2433. * \param len the number of bytes to compare between the buffers.
  2434. * \returns less than zero if s1 is "less than" s2, greater than zero if s1 is
  2435. * "greater than" s2, and zero if the buffers match exactly for `len`
  2436. * bytes.
  2437. *
  2438. * \threadsafety It is safe to call this function from any thread.
  2439. *
  2440. * \since This function is available since SDL 3.1.3.
  2441. */
  2442. extern SDL_DECLSPEC int SDLCALL SDL_memcmp(const void *s1, const void *s2, size_t len);
  2443. /**
  2444. * This works exactly like wcslen() but doesn't require access to a C runtime.
  2445. *
  2446. * Counts the number of wchar_t values in `wstr`, excluding the null
  2447. * terminator.
  2448. *
  2449. * Like SDL_strlen only counts bytes and not codepoints in a UTF-8 string,
  2450. * this counts wchar_t values in a string, even if the string's encoding is of
  2451. * variable width, like UTF-16.
  2452. *
  2453. * Also be aware that wchar_t is different sizes on different platforms (4
  2454. * bytes on Linux, 2 on Windows, etc).
  2455. *
  2456. * \param wstr The null-terminated wide string to read. Must not be NULL.
  2457. * \returns the length (in wchar_t values, excluding the null terminator) of
  2458. * `wstr`.
  2459. *
  2460. * \threadsafety It is safe to call this function from any thread.
  2461. *
  2462. * \since This function is available since SDL 3.1.3.
  2463. *
  2464. * \sa SDL_wcsnlen
  2465. * \sa SDL_utf8strlen
  2466. * \sa SDL_utf8strnlen
  2467. */
  2468. extern SDL_DECLSPEC size_t SDLCALL SDL_wcslen(const wchar_t *wstr);
  2469. /**
  2470. * This works exactly like wcsnlen() but doesn't require access to a C
  2471. * runtime.
  2472. *
  2473. * Counts up to a maximum of `maxlen` wchar_t values in `wstr`, excluding the
  2474. * null terminator.
  2475. *
  2476. * Like SDL_strnlen only counts bytes and not codepoints in a UTF-8 string,
  2477. * this counts wchar_t values in a string, even if the string's encoding is of
  2478. * variable width, like UTF-16.
  2479. *
  2480. * Also be aware that wchar_t is different sizes on different platforms (4
  2481. * bytes on Linux, 2 on Windows, etc).
  2482. *
  2483. * Also, `maxlen` is a count of wide characters, not bytes!
  2484. *
  2485. * \param wstr The null-terminated wide string to read. Must not be NULL.
  2486. * \param maxlen The maximum amount of wide characters to count.
  2487. * \returns the length (in wide characters, excluding the null terminator) of
  2488. * `wstr` but never more than `maxlen`.
  2489. *
  2490. * \threadsafety It is safe to call this function from any thread.
  2491. *
  2492. * \since This function is available since SDL 3.1.3.
  2493. *
  2494. * \sa SDL_wcslen
  2495. * \sa SDL_utf8strlen
  2496. * \sa SDL_utf8strnlen
  2497. */
  2498. extern SDL_DECLSPEC size_t SDLCALL SDL_wcsnlen(const wchar_t *wstr, size_t maxlen);
  2499. /**
  2500. * Copy a wide string.
  2501. *
  2502. * This function copies `maxlen` - 1 wide characters from `src` to `dst`, then
  2503. * appends a null terminator.
  2504. *
  2505. * `src` and `dst` must not overlap.
  2506. *
  2507. * If `maxlen` is 0, no wide characters are copied and no null terminator is
  2508. * written.
  2509. *
  2510. * \param dst The destination buffer. Must not be NULL, and must not overlap
  2511. * with `src`.
  2512. * \param src The null-terminated wide string to copy. Must not be NULL, and
  2513. * must not overlap with `dst`.
  2514. * \param maxlen The length (in wide characters) of the destination buffer.
  2515. * \returns the length (in wide characters, excluding the null terminator) of
  2516. * `src`.
  2517. *
  2518. * \threadsafety It is safe to call this function from any thread.
  2519. *
  2520. * \since This function is available since SDL 3.1.3.
  2521. *
  2522. * \sa SDL_wcslcat
  2523. */
  2524. extern SDL_DECLSPEC size_t SDLCALL SDL_wcslcpy(SDL_OUT_Z_CAP(maxlen) wchar_t *dst, const wchar_t *src, size_t maxlen);
  2525. /**
  2526. * Concatenate wide strings.
  2527. *
  2528. * This function appends up to `maxlen` - SDL_wcslen(dst) - 1 wide characters
  2529. * from `src` to the end of the wide string in `dst`, then appends a null
  2530. * terminator.
  2531. *
  2532. * `src` and `dst` must not overlap.
  2533. *
  2534. * If `maxlen` - SDL_wcslen(dst) - 1 is less than or equal to 0, then `dst` is
  2535. * unmodified.
  2536. *
  2537. * \param dst The destination buffer already containing the first
  2538. * null-terminated wide string. Must not be NULL and must not
  2539. * overlap with `src`.
  2540. * \param src The second null-terminated wide string. Must not be NULL, and
  2541. * must not overlap with `dst`.
  2542. * \param maxlen The length (in wide characters) of the destination buffer.
  2543. * \returns the length (in wide characters, excluding the null terminator) of
  2544. * the string in `dst` plus the length of `src`.
  2545. *
  2546. * \threadsafety It is safe to call this function from any thread.
  2547. *
  2548. * \since This function is available since SDL 3.1.3.
  2549. *
  2550. * \sa SDL_wcslcpy
  2551. */
  2552. extern SDL_DECLSPEC size_t SDLCALL SDL_wcslcat(SDL_INOUT_Z_CAP(maxlen) wchar_t *dst, const wchar_t *src, size_t maxlen);
  2553. /**
  2554. * Allocate a copy of a wide string.
  2555. *
  2556. * This allocates enough space for a null-terminated copy of `wstr`, using
  2557. * SDL_malloc, and then makes a copy of the string into this space.
  2558. *
  2559. * The returned string is owned by the caller, and should be passed to
  2560. * SDL_free when no longer needed.
  2561. *
  2562. * \param wstr the string to copy.
  2563. * \returns a pointer to the newly-allocated wide string.
  2564. *
  2565. * \threadsafety It is safe to call this function from any thread.
  2566. *
  2567. * \since This function is available since SDL 3.1.3.
  2568. */
  2569. extern SDL_DECLSPEC wchar_t * SDLCALL SDL_wcsdup(const wchar_t *wstr);
  2570. /**
  2571. * Search a wide string for the first instance of a specific substring.
  2572. *
  2573. * The search ends once it finds the requested substring, or a null terminator
  2574. * byte to end the string.
  2575. *
  2576. * Note that this looks for strings of _wide characters_, not _codepoints_, so
  2577. * it's legal to search for malformed and incomplete UTF-16 sequences.
  2578. *
  2579. * \param haystack the wide string to search. Must not be NULL.
  2580. * \param needle the wide string to search for. Must not be NULL.
  2581. * \returns a pointer to the first instance of `needle` in the string, or NULL
  2582. * if not found.
  2583. *
  2584. * \threadsafety It is safe to call this function from any thread.
  2585. *
  2586. * \since This function is available since SDL 3.1.3.
  2587. */
  2588. extern SDL_DECLSPEC wchar_t * SDLCALL SDL_wcsstr(const wchar_t *haystack, const wchar_t *needle);
  2589. /**
  2590. * Search a wide string, up to n wide chars, for the first instance of a
  2591. * specific substring.
  2592. *
  2593. * The search ends once it finds the requested substring, or a null terminator
  2594. * value to end the string, or `maxlen` wide character have been examined. It
  2595. * is possible to use this function on a wide string without a null
  2596. * terminator.
  2597. *
  2598. * Note that this looks for strings of _wide characters_, not _codepoints_, so
  2599. * it's legal to search for malformed and incomplete UTF-16 sequences.
  2600. *
  2601. * \param haystack the wide string to search. Must not be NULL.
  2602. * \param needle the wide string to search for. Must not be NULL.
  2603. * \param maxlen the maximum number of wide characters to search in
  2604. * `haystack`.
  2605. * \returns a pointer to the first instance of `needle` in the string, or NULL
  2606. * if not found.
  2607. *
  2608. * \threadsafety It is safe to call this function from any thread.
  2609. *
  2610. * \since This function is available since SDL 3.1.3.
  2611. */
  2612. extern SDL_DECLSPEC wchar_t * SDLCALL SDL_wcsnstr(const wchar_t *haystack, const wchar_t *needle, size_t maxlen);
  2613. /**
  2614. * Compare two null-terminated wide strings.
  2615. *
  2616. * This only compares wchar_t values until it hits a null-terminating
  2617. * character; it does not care if the string is well-formed UTF-16 (or UTF-32,
  2618. * depending on your platform's wchar_t size), or uses valid Unicode values.
  2619. *
  2620. * \param str1 the first string to compare. NULL is not permitted!
  2621. * \param str2 the second string to compare. NULL is not permitted!
  2622. * \returns less than zero if str1 is "less than" str2, greater than zero if
  2623. * str1 is "greater than" str2, and zero if the strings match
  2624. * exactly.
  2625. *
  2626. * \threadsafety It is safe to call this function from any thread.
  2627. *
  2628. * \since This function is available since SDL 3.1.3.
  2629. */
  2630. extern SDL_DECLSPEC int SDLCALL SDL_wcscmp(const wchar_t *str1, const wchar_t *str2);
  2631. /**
  2632. * Compare two wide strings up to a number of wchar_t values.
  2633. *
  2634. * This only compares wchar_t values; it does not care if the string is
  2635. * well-formed UTF-16 (or UTF-32, depending on your platform's wchar_t size),
  2636. * or uses valid Unicode values.
  2637. *
  2638. * Note that while this function is intended to be used with UTF-16 (or
  2639. * UTF-32, depending on your platform's definition of wchar_t), it is
  2640. * comparing raw wchar_t values and not Unicode codepoints: `maxlen` specifies
  2641. * a wchar_t limit! If the limit lands in the middle of a multi-wchar UTF-16
  2642. * sequence, it will only compare a portion of the final character.
  2643. *
  2644. * `maxlen` specifies a maximum number of wchar_t to compare; if the strings
  2645. * match to this number of wide chars (or both have matched to a
  2646. * null-terminator character before this count), they will be considered
  2647. * equal.
  2648. *
  2649. * \param str1 the first string to compare. NULL is not permitted!
  2650. * \param str2 the second string to compare. NULL is not permitted!
  2651. * \param maxlen the maximum number of wchar_t to compare.
  2652. * \returns less than zero if str1 is "less than" str2, greater than zero if
  2653. * str1 is "greater than" str2, and zero if the strings match
  2654. * exactly.
  2655. *
  2656. * \threadsafety It is safe to call this function from any thread.
  2657. *
  2658. * \since This function is available since SDL 3.1.3.
  2659. */
  2660. extern SDL_DECLSPEC int SDLCALL SDL_wcsncmp(const wchar_t *str1, const wchar_t *str2, size_t maxlen);
  2661. /**
  2662. * Compare two null-terminated wide strings, case-insensitively.
  2663. *
  2664. * This will work with Unicode strings, using a technique called
  2665. * "case-folding" to handle the vast majority of case-sensitive human
  2666. * languages regardless of system locale. It can deal with expanding values: a
  2667. * German Eszett character can compare against two ASCII 's' chars and be
  2668. * considered a match, for example. A notable exception: it does not handle
  2669. * the Turkish 'i' character; human language is complicated!
  2670. *
  2671. * Depending on your platform, "wchar_t" might be 2 bytes, and expected to be
  2672. * UTF-16 encoded (like Windows), or 4 bytes in UTF-32 format. Since this
  2673. * handles Unicode, it expects the string to be well-formed and not a
  2674. * null-terminated string of arbitrary bytes. Characters that are not valid
  2675. * UTF-16 (or UTF-32) are treated as Unicode character U+FFFD (REPLACEMENT
  2676. * CHARACTER), which is to say two strings of random bits may turn out to
  2677. * match if they convert to the same amount of replacement characters.
  2678. *
  2679. * \param str1 the first string to compare. NULL is not permitted!
  2680. * \param str2 the second string to compare. NULL is not permitted!
  2681. * \returns less than zero if str1 is "less than" str2, greater than zero if
  2682. * str1 is "greater than" str2, and zero if the strings match
  2683. * exactly.
  2684. *
  2685. * \threadsafety It is safe to call this function from any thread.
  2686. *
  2687. * \since This function is available since SDL 3.1.3.
  2688. */
  2689. extern SDL_DECLSPEC int SDLCALL SDL_wcscasecmp(const wchar_t *str1, const wchar_t *str2);
  2690. /**
  2691. * Compare two wide strings, case-insensitively, up to a number of wchar_t.
  2692. *
  2693. * This will work with Unicode strings, using a technique called
  2694. * "case-folding" to handle the vast majority of case-sensitive human
  2695. * languages regardless of system locale. It can deal with expanding values: a
  2696. * German Eszett character can compare against two ASCII 's' chars and be
  2697. * considered a match, for example. A notable exception: it does not handle
  2698. * the Turkish 'i' character; human language is complicated!
  2699. *
  2700. * Depending on your platform, "wchar_t" might be 2 bytes, and expected to be
  2701. * UTF-16 encoded (like Windows), or 4 bytes in UTF-32 format. Since this
  2702. * handles Unicode, it expects the string to be well-formed and not a
  2703. * null-terminated string of arbitrary bytes. Characters that are not valid
  2704. * UTF-16 (or UTF-32) are treated as Unicode character U+FFFD (REPLACEMENT
  2705. * CHARACTER), which is to say two strings of random bits may turn out to
  2706. * match if they convert to the same amount of replacement characters.
  2707. *
  2708. * Note that while this function might deal with variable-sized characters,
  2709. * `maxlen` specifies a _wchar_ limit! If the limit lands in the middle of a
  2710. * multi-byte UTF-16 sequence, it may convert a portion of the final character
  2711. * to one or more Unicode character U+FFFD (REPLACEMENT CHARACTER) so as not
  2712. * to overflow a buffer.
  2713. *
  2714. * `maxlen` specifies a maximum number of wchar_t values to compare; if the
  2715. * strings match to this number of wchar_t (or both have matched to a
  2716. * null-terminator character before this number of bytes), they will be
  2717. * considered equal.
  2718. *
  2719. * \param str1 the first string to compare. NULL is not permitted!
  2720. * \param str2 the second string to compare. NULL is not permitted!
  2721. * \param maxlen the maximum number of wchar_t values to compare.
  2722. * \returns less than zero if str1 is "less than" str2, greater than zero if
  2723. * str1 is "greater than" str2, and zero if the strings match
  2724. * exactly.
  2725. *
  2726. * \threadsafety It is safe to call this function from any thread.
  2727. *
  2728. * \since This function is available since SDL 3.1.3.
  2729. */
  2730. extern SDL_DECLSPEC int SDLCALL SDL_wcsncasecmp(const wchar_t *str1, const wchar_t *str2, size_t maxlen);
  2731. /**
  2732. * Parse a `long` from a wide string.
  2733. *
  2734. * If `str` starts with whitespace, then those whitespace characters are
  2735. * skipped before attempting to parse the number.
  2736. *
  2737. * If the parsed number does not fit inside a `long`, the result is clamped to
  2738. * the minimum and maximum representable `long` values.
  2739. *
  2740. * \param str The null-terminated wide string to read. Must not be NULL.
  2741. * \param endp If not NULL, the address of the first invalid wide character
  2742. * (i.e. the next character after the parsed number) will be
  2743. * written to this pointer.
  2744. * \param base The base of the integer to read. Supported values are 0 and 2
  2745. * to 36 inclusive. If 0, the base will be inferred from the
  2746. * number's prefix (0x for hexadecimal, 0 for octal, decimal
  2747. * otherwise).
  2748. * \returns the parsed `long`, or 0 if no number could be parsed.
  2749. *
  2750. * \threadsafety It is safe to call this function from any thread.
  2751. *
  2752. * \since This function is available since SDL 3.1.3.
  2753. *
  2754. * \sa SDL_strtol
  2755. */
  2756. extern SDL_DECLSPEC long SDLCALL SDL_wcstol(const wchar_t *str, wchar_t **endp, int base);
  2757. /**
  2758. * This works exactly like strlen() but doesn't require access to a C runtime.
  2759. *
  2760. * Counts the bytes in `str`, excluding the null terminator.
  2761. *
  2762. * If you need the length of a UTF-8 string, consider using SDL_utf8strlen().
  2763. *
  2764. * \param str The null-terminated string to read. Must not be NULL.
  2765. * \returns the length (in bytes, excluding the null terminator) of `src`.
  2766. *
  2767. * \threadsafety It is safe to call this function from any thread.
  2768. *
  2769. * \since This function is available since SDL 3.1.3.
  2770. *
  2771. * \sa SDL_strnlen
  2772. * \sa SDL_utf8strlen
  2773. * \sa SDL_utf8strnlen
  2774. */
  2775. extern SDL_DECLSPEC size_t SDLCALL SDL_strlen(const char *str);
  2776. /**
  2777. * This works exactly like strnlen() but doesn't require access to a C
  2778. * runtime.
  2779. *
  2780. * Counts up to a maximum of `maxlen` bytes in `str`, excluding the null
  2781. * terminator.
  2782. *
  2783. * If you need the length of a UTF-8 string, consider using SDL_utf8strnlen().
  2784. *
  2785. * \param str The null-terminated string to read. Must not be NULL.
  2786. * \param maxlen The maximum amount of bytes to count.
  2787. * \returns the length (in bytes, excluding the null terminator) of `src` but
  2788. * never more than `maxlen`.
  2789. *
  2790. * \threadsafety It is safe to call this function from any thread.
  2791. *
  2792. * \since This function is available since SDL 3.1.3.
  2793. *
  2794. * \sa SDL_strlen
  2795. * \sa SDL_utf8strlen
  2796. * \sa SDL_utf8strnlen
  2797. */
  2798. extern SDL_DECLSPEC size_t SDLCALL SDL_strnlen(const char *str, size_t maxlen);
  2799. /**
  2800. * Copy a string.
  2801. *
  2802. * This function copies up to `maxlen` - 1 characters from `src` to `dst`,
  2803. * then appends a null terminator.
  2804. *
  2805. * If `maxlen` is 0, no characters are copied and no null terminator is
  2806. * written.
  2807. *
  2808. * If you want to copy an UTF-8 string but need to ensure that multi-byte
  2809. * sequences are not truncated, consider using SDL_utf8strlcpy().
  2810. *
  2811. * \param dst The destination buffer. Must not be NULL, and must not overlap
  2812. * with `src`.
  2813. * \param src The null-terminated string to copy. Must not be NULL, and must
  2814. * not overlap with `dst`.
  2815. * \param maxlen The length (in characters) of the destination buffer.
  2816. * \returns the length (in characters, excluding the null terminator) of
  2817. * `src`.
  2818. *
  2819. * \threadsafety It is safe to call this function from any thread.
  2820. *
  2821. * \since This function is available since SDL 3.1.3.
  2822. *
  2823. * \sa SDL_strlcat
  2824. * \sa SDL_utf8strlcpy
  2825. */
  2826. extern SDL_DECLSPEC size_t SDLCALL SDL_strlcpy(SDL_OUT_Z_CAP(maxlen) char *dst, const char *src, size_t maxlen);
  2827. /**
  2828. * Copy an UTF-8 string.
  2829. *
  2830. * This function copies up to `dst_bytes` - 1 bytes from `src` to `dst` while
  2831. * also ensuring that the string written to `dst` does not end in a truncated
  2832. * multi-byte sequence. Finally, it appends a null terminator.
  2833. *
  2834. * `src` and `dst` must not overlap.
  2835. *
  2836. * Note that unlike SDL_strlcpy(), this function returns the number of bytes
  2837. * written, not the length of `src`.
  2838. *
  2839. * \param dst The destination buffer. Must not be NULL, and must not overlap
  2840. * with `src`.
  2841. * \param src The null-terminated UTF-8 string to copy. Must not be NULL, and
  2842. * must not overlap with `dst`.
  2843. * \param dst_bytes The length (in bytes) of the destination buffer. Must not
  2844. * be 0.
  2845. * \returns the number of bytes written, excluding the null terminator.
  2846. *
  2847. * \threadsafety It is safe to call this function from any thread.
  2848. *
  2849. * \since This function is available since SDL 3.1.3.
  2850. *
  2851. * \sa SDL_strlcpy
  2852. */
  2853. extern SDL_DECLSPEC size_t SDLCALL SDL_utf8strlcpy(SDL_OUT_Z_CAP(dst_bytes) char *dst, const char *src, size_t dst_bytes);
  2854. /**
  2855. * Concatenate strings.
  2856. *
  2857. * This function appends up to `maxlen` - SDL_strlen(dst) - 1 characters from
  2858. * `src` to the end of the string in `dst`, then appends a null terminator.
  2859. *
  2860. * `src` and `dst` must not overlap.
  2861. *
  2862. * If `maxlen` - SDL_strlen(dst) - 1 is less than or equal to 0, then `dst` is
  2863. * unmodified.
  2864. *
  2865. * \param dst The destination buffer already containing the first
  2866. * null-terminated string. Must not be NULL and must not overlap
  2867. * with `src`.
  2868. * \param src The second null-terminated string. Must not be NULL, and must
  2869. * not overlap with `dst`.
  2870. * \param maxlen The length (in characters) of the destination buffer.
  2871. * \returns the length (in characters, excluding the null terminator) of the
  2872. * string in `dst` plus the length of `src`.
  2873. *
  2874. * \threadsafety It is safe to call this function from any thread.
  2875. *
  2876. * \since This function is available since SDL 3.1.3.
  2877. *
  2878. * \sa SDL_strlcpy
  2879. */
  2880. extern SDL_DECLSPEC size_t SDLCALL SDL_strlcat(SDL_INOUT_Z_CAP(maxlen) char *dst, const char *src, size_t maxlen);
  2881. /**
  2882. * Allocate a copy of a string.
  2883. *
  2884. * This allocates enough space for a null-terminated copy of `str`, using
  2885. * SDL_malloc, and then makes a copy of the string into this space.
  2886. *
  2887. * The returned string is owned by the caller, and should be passed to
  2888. * SDL_free when no longer needed.
  2889. *
  2890. * \param str the string to copy.
  2891. * \returns a pointer to the newly-allocated string.
  2892. *
  2893. * \threadsafety It is safe to call this function from any thread.
  2894. *
  2895. * \since This function is available since SDL 3.1.3.
  2896. */
  2897. extern SDL_DECLSPEC SDL_MALLOC char * SDLCALL SDL_strdup(const char *str);
  2898. /**
  2899. * Allocate a copy of a string, up to n characters.
  2900. *
  2901. * This allocates enough space for a null-terminated copy of `str`, up to
  2902. * `maxlen` bytes, using SDL_malloc, and then makes a copy of the string into
  2903. * this space.
  2904. *
  2905. * If the string is longer than `maxlen` bytes, the returned string will be
  2906. * `maxlen` bytes long, plus a null-terminator character that isn't included
  2907. * in the count.
  2908. *
  2909. * The returned string is owned by the caller, and should be passed to
  2910. * SDL_free when no longer needed.
  2911. *
  2912. * \param str the string to copy.
  2913. * \param maxlen the maximum length of the copied string, not counting the
  2914. * null-terminator character.
  2915. * \returns a pointer to the newly-allocated string.
  2916. *
  2917. * \threadsafety It is safe to call this function from any thread.
  2918. *
  2919. * \since This function is available since SDL 3.1.3.
  2920. */
  2921. extern SDL_DECLSPEC SDL_MALLOC char * SDLCALL SDL_strndup(const char *str, size_t maxlen);
  2922. /**
  2923. * Reverse a string's contents.
  2924. *
  2925. * This reverses a null-terminated string in-place. Only the content of the
  2926. * string is reversed; the null-terminator character remains at the end of the
  2927. * reversed string.
  2928. *
  2929. * **WARNING**: This function reverses the _bytes_ of the string, not the
  2930. * codepoints. If `str` is a UTF-8 string with Unicode codepoints > 127, this
  2931. * will ruin the string data. You should only use this function on strings
  2932. * that are completely comprised of low ASCII characters.
  2933. *
  2934. * \param str the string to reverse.
  2935. * \returns `str`.
  2936. *
  2937. * \threadsafety It is safe to call this function from any thread.
  2938. *
  2939. * \since This function is available since SDL 3.1.3.
  2940. */
  2941. extern SDL_DECLSPEC char * SDLCALL SDL_strrev(char *str);
  2942. /**
  2943. * Convert a string to uppercase.
  2944. *
  2945. * **WARNING**: Regardless of system locale, this will only convert ASCII
  2946. * values 'A' through 'Z' to uppercase.
  2947. *
  2948. * This function operates on a null-terminated string of bytes--even if it is
  2949. * malformed UTF-8!--and converts ASCII characters 'a' through 'z' to their
  2950. * uppercase equivalents in-place, returning the original `str` pointer.
  2951. *
  2952. * \param str the string to convert in-place. Can not be NULL.
  2953. * \returns the `str` pointer passed into this function.
  2954. *
  2955. * \threadsafety It is safe to call this function from any thread.
  2956. *
  2957. * \since This function is available since SDL 3.1.3.
  2958. *
  2959. * \sa SDL_strlwr
  2960. */
  2961. extern SDL_DECLSPEC char * SDLCALL SDL_strupr(char *str);
  2962. /**
  2963. * Convert a string to lowercase.
  2964. *
  2965. * **WARNING**: Regardless of system locale, this will only convert ASCII
  2966. * values 'A' through 'Z' to lowercase.
  2967. *
  2968. * This function operates on a null-terminated string of bytes--even if it is
  2969. * malformed UTF-8!--and converts ASCII characters 'A' through 'Z' to their
  2970. * lowercase equivalents in-place, returning the original `str` pointer.
  2971. *
  2972. * \param str the string to convert in-place. Can not be NULL.
  2973. * \returns the `str` pointer passed into this function.
  2974. *
  2975. * \threadsafety It is safe to call this function from any thread.
  2976. *
  2977. * \since This function is available since SDL 3.1.3.
  2978. *
  2979. * \sa SDL_strupr
  2980. */
  2981. extern SDL_DECLSPEC char * SDLCALL SDL_strlwr(char *str);
  2982. /**
  2983. * Search a string for the first instance of a specific byte.
  2984. *
  2985. * The search ends once it finds the requested byte value, or a null
  2986. * terminator byte to end the string.
  2987. *
  2988. * Note that this looks for _bytes_, not _characters_, so you cannot match
  2989. * against a Unicode codepoint > 255, regardless of character encoding.
  2990. *
  2991. * \param str the string to search. Must not be NULL.
  2992. * \param c the byte value to search for.
  2993. * \returns a pointer to the first instance of `c` in the string, or NULL if
  2994. * not found.
  2995. *
  2996. * \threadsafety It is safe to call this function from any thread.
  2997. *
  2998. * \since This function is available since SDL 3.1.3.
  2999. */
  3000. extern SDL_DECLSPEC char * SDLCALL SDL_strchr(const char *str, int c);
  3001. /**
  3002. * Search a string for the last instance of a specific byte.
  3003. *
  3004. * The search must go until it finds a null terminator byte to end the string.
  3005. *
  3006. * Note that this looks for _bytes_, not _characters_, so you cannot match
  3007. * against a Unicode codepoint > 255, regardless of character encoding.
  3008. *
  3009. * \param str the string to search. Must not be NULL.
  3010. * \param c the byte value to search for.
  3011. * \returns a pointer to the last instance of `c` in the string, or NULL if
  3012. * not found.
  3013. *
  3014. * \threadsafety It is safe to call this function from any thread.
  3015. *
  3016. * \since This function is available since SDL 3.1.3.
  3017. */
  3018. extern SDL_DECLSPEC char * SDLCALL SDL_strrchr(const char *str, int c);
  3019. /**
  3020. * Search a string for the first instance of a specific substring.
  3021. *
  3022. * The search ends once it finds the requested substring, or a null terminator
  3023. * byte to end the string.
  3024. *
  3025. * Note that this looks for strings of _bytes_, not _characters_, so it's
  3026. * legal to search for malformed and incomplete UTF-8 sequences.
  3027. *
  3028. * \param haystack the string to search. Must not be NULL.
  3029. * \param needle the string to search for. Must not be NULL.
  3030. * \returns a pointer to the first instance of `needle` in the string, or NULL
  3031. * if not found.
  3032. *
  3033. * \threadsafety It is safe to call this function from any thread.
  3034. *
  3035. * \since This function is available since SDL 3.1.3.
  3036. */
  3037. extern SDL_DECLSPEC char * SDLCALL SDL_strstr(const char *haystack, const char *needle);
  3038. /**
  3039. * Search a string, up to n bytes, for the first instance of a specific
  3040. * substring.
  3041. *
  3042. * The search ends once it finds the requested substring, or a null terminator
  3043. * byte to end the string, or `maxlen` bytes have been examined. It is
  3044. * possible to use this function on a string without a null terminator.
  3045. *
  3046. * Note that this looks for strings of _bytes_, not _characters_, so it's
  3047. * legal to search for malformed and incomplete UTF-8 sequences.
  3048. *
  3049. * \param haystack the string to search. Must not be NULL.
  3050. * \param needle the string to search for. Must not be NULL.
  3051. * \param maxlen the maximum number of bytes to search in `haystack`.
  3052. * \returns a pointer to the first instance of `needle` in the string, or NULL
  3053. * if not found.
  3054. *
  3055. * \threadsafety It is safe to call this function from any thread.
  3056. *
  3057. * \since This function is available since SDL 3.1.3.
  3058. */
  3059. extern SDL_DECLSPEC char * SDLCALL SDL_strnstr(const char *haystack, const char *needle, size_t maxlen);
  3060. /**
  3061. * Search a UTF-8 string for the first instance of a specific substring,
  3062. * case-insensitively.
  3063. *
  3064. * This will work with Unicode strings, using a technique called
  3065. * "case-folding" to handle the vast majority of case-sensitive human
  3066. * languages regardless of system locale. It can deal with expanding values: a
  3067. * German Eszett character can compare against two ASCII 's' chars and be
  3068. * considered a match, for example. A notable exception: it does not handle
  3069. * the Turkish 'i' character; human language is complicated!
  3070. *
  3071. * Since this handles Unicode, it expects the strings to be well-formed UTF-8
  3072. * and not a null-terminated string of arbitrary bytes. Bytes that are not
  3073. * valid UTF-8 are treated as Unicode character U+FFFD (REPLACEMENT
  3074. * CHARACTER), which is to say two strings of random bits may turn out to
  3075. * match if they convert to the same amount of replacement characters.
  3076. *
  3077. * \param haystack the string to search. Must not be NULL.
  3078. * \param needle the string to search for. Must not be NULL.
  3079. * \returns a pointer to the first instance of `needle` in the string, or NULL
  3080. * if not found.
  3081. *
  3082. * \threadsafety It is safe to call this function from any thread.
  3083. *
  3084. * \since This function is available since SDL 3.1.3.
  3085. */
  3086. extern SDL_DECLSPEC char * SDLCALL SDL_strcasestr(const char *haystack, const char *needle);
  3087. /**
  3088. * This works exactly like strtok_r() but doesn't require access to a C
  3089. * runtime.
  3090. *
  3091. * Break a string up into a series of tokens.
  3092. *
  3093. * To start tokenizing a new string, `str` should be the non-NULL address of
  3094. * the string to start tokenizing. Future calls to get the next token from the
  3095. * same string should specify a NULL.
  3096. *
  3097. * Note that this function will overwrite pieces of `str` with null chars to
  3098. * split it into tokens. This function cannot be used with const/read-only
  3099. * strings!
  3100. *
  3101. * `saveptr` just needs to point to a `char *` that can be overwritten; SDL
  3102. * will use this to save tokenizing state between calls. It is initialized if
  3103. * `str` is non-NULL, and used to resume tokenizing when `str` is NULL.
  3104. *
  3105. * \param str the string to tokenize, or NULL to continue tokenizing.
  3106. * \param delim the delimiter string that separates tokens.
  3107. * \param saveptr pointer to a char *, used for ongoing state.
  3108. * \returns A pointer to the next token, or NULL if no tokens remain.
  3109. *
  3110. * \threadsafety It is safe to call this function from any thread.
  3111. *
  3112. * \since This function is available since SDL 3.1.3.
  3113. */
  3114. extern SDL_DECLSPEC char * SDLCALL SDL_strtok_r(char *str, const char *delim, char **saveptr);
  3115. /**
  3116. * Count the number of codepoints in a UTF-8 string.
  3117. *
  3118. * Counts the _codepoints_, not _bytes_, in `str`, excluding the null
  3119. * terminator.
  3120. *
  3121. * If you need to count the bytes in a string instead, consider using
  3122. * SDL_strlen().
  3123. *
  3124. * Since this handles Unicode, it expects the strings to be well-formed UTF-8
  3125. * and not a null-terminated string of arbitrary bytes. Bytes that are not
  3126. * valid UTF-8 are treated as Unicode character U+FFFD (REPLACEMENT
  3127. * CHARACTER), so a malformed or incomplete UTF-8 sequence might increase the
  3128. * count by several replacement characters.
  3129. *
  3130. * \param str The null-terminated UTF-8 string to read. Must not be NULL.
  3131. * \returns The length (in codepoints, excluding the null terminator) of
  3132. * `src`.
  3133. *
  3134. * \threadsafety It is safe to call this function from any thread.
  3135. *
  3136. * \since This function is available since SDL 3.1.3.
  3137. *
  3138. * \sa SDL_utf8strnlen
  3139. * \sa SDL_strlen
  3140. */
  3141. extern SDL_DECLSPEC size_t SDLCALL SDL_utf8strlen(const char *str);
  3142. /**
  3143. * Count the number of codepoints in a UTF-8 string, up to n bytes.
  3144. *
  3145. * Counts the _codepoints_, not _bytes_, in `str`, excluding the null
  3146. * terminator.
  3147. *
  3148. * If you need to count the bytes in a string instead, consider using
  3149. * SDL_strnlen().
  3150. *
  3151. * The counting stops at `bytes` bytes (not codepoints!). This seems
  3152. * counterintuitive, but makes it easy to express the total size of the
  3153. * string's buffer.
  3154. *
  3155. * Since this handles Unicode, it expects the strings to be well-formed UTF-8
  3156. * and not a null-terminated string of arbitrary bytes. Bytes that are not
  3157. * valid UTF-8 are treated as Unicode character U+FFFD (REPLACEMENT
  3158. * CHARACTER), so a malformed or incomplete UTF-8 sequence might increase the
  3159. * count by several replacement characters.
  3160. *
  3161. * \param str The null-terminated UTF-8 string to read. Must not be NULL.
  3162. * \param bytes The maximum amount of bytes to count.
  3163. * \returns The length (in codepoints, excluding the null terminator) of `src`
  3164. * but never more than `maxlen`.
  3165. *
  3166. * \threadsafety It is safe to call this function from any thread.
  3167. *
  3168. * \since This function is available since SDL 3.1.3.
  3169. *
  3170. * \sa SDL_utf8strlen
  3171. * \sa SDL_strnlen
  3172. */
  3173. extern SDL_DECLSPEC size_t SDLCALL SDL_utf8strnlen(const char *str, size_t bytes);
  3174. /**
  3175. * Convert an integer into a string.
  3176. *
  3177. * This requires a radix to specified for string format. Specifying 10
  3178. * produces a decimal number, 16 hexidecimal, etc. Must be in the range of 2
  3179. * to 36.
  3180. *
  3181. * Note that this function will overflow a buffer if `str` is not large enough
  3182. * to hold the output! It may be safer to use SDL_snprintf to clamp output, or
  3183. * SDL_asprintf to allocate a buffer. Otherwise, it doesn't hurt to allocate
  3184. * much more space than you expect to use (and don't forget possible negative
  3185. * signs, null terminator bytes, etc).
  3186. *
  3187. * \param value the integer to convert.
  3188. * \param str the buffer to write the string into.
  3189. * \param radix the radix to use for string generation.
  3190. * \returns `str`.
  3191. *
  3192. * \threadsafety It is safe to call this function from any thread.
  3193. *
  3194. * \since This function is available since SDL 3.1.3.
  3195. *
  3196. * \sa SDL_uitoa
  3197. * \sa SDL_ltoa
  3198. * \sa SDL_lltoa
  3199. */
  3200. extern SDL_DECLSPEC char * SDLCALL SDL_itoa(int value, char *str, int radix);
  3201. /**
  3202. * Convert an unsigned integer into a string.
  3203. *
  3204. * This requires a radix to specified for string format. Specifying 10
  3205. * produces a decimal number, 16 hexidecimal, etc. Must be in the range of 2
  3206. * to 36.
  3207. *
  3208. * Note that this function will overflow a buffer if `str` is not large enough
  3209. * to hold the output! It may be safer to use SDL_snprintf to clamp output, or
  3210. * SDL_asprintf to allocate a buffer. Otherwise, it doesn't hurt to allocate
  3211. * much more space than you expect to use (and don't forget null terminator
  3212. * bytes, etc).
  3213. *
  3214. * \param value the unsigned integer to convert.
  3215. * \param str the buffer to write the string into.
  3216. * \param radix the radix to use for string generation.
  3217. * \returns `str`.
  3218. *
  3219. * \threadsafety It is safe to call this function from any thread.
  3220. *
  3221. * \since This function is available since SDL 3.1.3.
  3222. *
  3223. * \sa SDL_itoa
  3224. * \sa SDL_ultoa
  3225. * \sa SDL_ulltoa
  3226. */
  3227. extern SDL_DECLSPEC char * SDLCALL SDL_uitoa(unsigned int value, char *str, int radix);
  3228. /**
  3229. * Convert a long integer into a string.
  3230. *
  3231. * This requires a radix to specified for string format. Specifying 10
  3232. * produces a decimal number, 16 hexidecimal, etc. Must be in the range of 2
  3233. * to 36.
  3234. *
  3235. * Note that this function will overflow a buffer if `str` is not large enough
  3236. * to hold the output! It may be safer to use SDL_snprintf to clamp output, or
  3237. * SDL_asprintf to allocate a buffer. Otherwise, it doesn't hurt to allocate
  3238. * much more space than you expect to use (and don't forget possible negative
  3239. * signs, null terminator bytes, etc).
  3240. *
  3241. * \param value the long integer to convert.
  3242. * \param str the buffer to write the string into.
  3243. * \param radix the radix to use for string generation.
  3244. * \returns `str`.
  3245. *
  3246. * \threadsafety It is safe to call this function from any thread.
  3247. *
  3248. * \since This function is available since SDL 3.1.3.
  3249. *
  3250. * \sa SDL_ultoa
  3251. * \sa SDL_itoa
  3252. * \sa SDL_lltoa
  3253. */
  3254. extern SDL_DECLSPEC char * SDLCALL SDL_ltoa(long value, char *str, int radix);
  3255. /**
  3256. * Convert an unsigned long integer into a string.
  3257. *
  3258. * This requires a radix to specified for string format. Specifying 10
  3259. * produces a decimal number, 16 hexidecimal, etc. Must be in the range of 2
  3260. * to 36.
  3261. *
  3262. * Note that this function will overflow a buffer if `str` is not large enough
  3263. * to hold the output! It may be safer to use SDL_snprintf to clamp output, or
  3264. * SDL_asprintf to allocate a buffer. Otherwise, it doesn't hurt to allocate
  3265. * much more space than you expect to use (and don't forget null terminator
  3266. * bytes, etc).
  3267. *
  3268. * \param value the unsigned long integer to convert.
  3269. * \param str the buffer to write the string into.
  3270. * \param radix the radix to use for string generation.
  3271. * \returns `str`.
  3272. *
  3273. * \threadsafety It is safe to call this function from any thread.
  3274. *
  3275. * \since This function is available since SDL 3.1.3.
  3276. *
  3277. * \sa SDL_ltoa
  3278. * \sa SDL_uitoa
  3279. * \sa SDL_ulltoa
  3280. */
  3281. extern SDL_DECLSPEC char * SDLCALL SDL_ultoa(unsigned long value, char *str, int radix);
  3282. /**
  3283. * Convert a long long integer into a string.
  3284. *
  3285. * This requires a radix to specified for string format. Specifying 10
  3286. * produces a decimal number, 16 hexidecimal, etc. Must be in the range of 2
  3287. * to 36.
  3288. *
  3289. * Note that this function will overflow a buffer if `str` is not large enough
  3290. * to hold the output! It may be safer to use SDL_snprintf to clamp output, or
  3291. * SDL_asprintf to allocate a buffer. Otherwise, it doesn't hurt to allocate
  3292. * much more space than you expect to use (and don't forget possible negative
  3293. * signs, null terminator bytes, etc).
  3294. *
  3295. * \param value the long long integer to convert.
  3296. * \param str the buffer to write the string into.
  3297. * \param radix the radix to use for string generation.
  3298. * \returns `str`.
  3299. *
  3300. * \threadsafety It is safe to call this function from any thread.
  3301. *
  3302. * \since This function is available since SDL 3.1.3.
  3303. *
  3304. * \sa SDL_ulltoa
  3305. * \sa SDL_itoa
  3306. * \sa SDL_ltoa
  3307. */
  3308. extern SDL_DECLSPEC char * SDLCALL SDL_lltoa(long long value, char *str, int radix);
  3309. /**
  3310. * Convert an unsigned long long integer into a string.
  3311. *
  3312. * This requires a radix to specified for string format. Specifying 10
  3313. * produces a decimal number, 16 hexidecimal, etc. Must be in the range of 2
  3314. * to 36.
  3315. *
  3316. * Note that this function will overflow a buffer if `str` is not large enough
  3317. * to hold the output! It may be safer to use SDL_snprintf to clamp output, or
  3318. * SDL_asprintf to allocate a buffer. Otherwise, it doesn't hurt to allocate
  3319. * much more space than you expect to use (and don't forget null terminator
  3320. * bytes, etc).
  3321. *
  3322. * \param value the unsigned long long integer to convert.
  3323. * \param str the buffer to write the string into.
  3324. * \param radix the radix to use for string generation.
  3325. * \returns `str`.
  3326. *
  3327. * \threadsafety It is safe to call this function from any thread.
  3328. *
  3329. * \since This function is available since SDL 3.1.3.
  3330. *
  3331. * \sa SDL_lltoa
  3332. * \sa SDL_uitoa
  3333. * \sa SDL_ultoa
  3334. */
  3335. extern SDL_DECLSPEC char * SDLCALL SDL_ulltoa(unsigned long long value, char *str, int radix);
  3336. /**
  3337. * Parse an `int` from a string.
  3338. *
  3339. * The result of calling `SDL_atoi(str)` is equivalent to
  3340. * `(int)SDL_strtol(str, NULL, 10)`.
  3341. *
  3342. * \param str The null-terminated string to read. Must not be NULL.
  3343. * \returns the parsed `int`.
  3344. *
  3345. * \threadsafety It is safe to call this function from any thread.
  3346. *
  3347. * \since This function is available since SDL 3.1.3.
  3348. *
  3349. * \sa SDL_atof
  3350. * \sa SDL_strtol
  3351. * \sa SDL_strtoul
  3352. * \sa SDL_strtoll
  3353. * \sa SDL_strtoull
  3354. * \sa SDL_strtod
  3355. * \sa SDL_itoa
  3356. */
  3357. extern SDL_DECLSPEC int SDLCALL SDL_atoi(const char *str);
  3358. /**
  3359. * Parse a `double` from a string.
  3360. *
  3361. * The result of calling `SDL_atof(str)` is equivalent to `SDL_strtod(str,
  3362. * NULL)`.
  3363. *
  3364. * \param str The null-terminated string to read. Must not be NULL.
  3365. * \returns the parsed `double`.
  3366. *
  3367. * \threadsafety It is safe to call this function from any thread.
  3368. *
  3369. * \since This function is available since SDL 3.1.3.
  3370. *
  3371. * \sa SDL_atoi
  3372. * \sa SDL_strtol
  3373. * \sa SDL_strtoul
  3374. * \sa SDL_strtoll
  3375. * \sa SDL_strtoull
  3376. * \sa SDL_strtod
  3377. */
  3378. extern SDL_DECLSPEC double SDLCALL SDL_atof(const char *str);
  3379. /**
  3380. * Parse a `long` from a string.
  3381. *
  3382. * If `str` starts with whitespace, then those whitespace characters are
  3383. * skipped before attempting to parse the number.
  3384. *
  3385. * If the parsed number does not fit inside a `long`, the result is clamped to
  3386. * the minimum and maximum representable `long` values.
  3387. *
  3388. * \param str The null-terminated string to read. Must not be NULL.
  3389. * \param endp If not NULL, the address of the first invalid character (i.e.
  3390. * the next character after the parsed number) will be written to
  3391. * this pointer.
  3392. * \param base The base of the integer to read. Supported values are 0 and 2
  3393. * to 36 inclusive. If 0, the base will be inferred from the
  3394. * number's prefix (0x for hexadecimal, 0 for octal, decimal
  3395. * otherwise).
  3396. * \returns the parsed `long`, or 0 if no number could be parsed.
  3397. *
  3398. * \threadsafety It is safe to call this function from any thread.
  3399. *
  3400. * \since This function is available since SDL 3.1.3.
  3401. *
  3402. * \sa SDL_atoi
  3403. * \sa SDL_atof
  3404. * \sa SDL_strtoul
  3405. * \sa SDL_strtoll
  3406. * \sa SDL_strtoull
  3407. * \sa SDL_strtod
  3408. * \sa SDL_ltoa
  3409. * \sa SDL_wcstol
  3410. */
  3411. extern SDL_DECLSPEC long SDLCALL SDL_strtol(const char *str, char **endp, int base);
  3412. /**
  3413. * Parse an `unsigned long` from a string.
  3414. *
  3415. * If `str` starts with whitespace, then those whitespace characters are
  3416. * skipped before attempting to parse the number.
  3417. *
  3418. * If the parsed number does not fit inside an `unsigned long`, the result is
  3419. * clamped to the maximum representable `unsigned long` value.
  3420. *
  3421. * \param str The null-terminated string to read. Must not be NULL.
  3422. * \param endp If not NULL, the address of the first invalid character (i.e.
  3423. * the next character after the parsed number) will be written to
  3424. * this pointer.
  3425. * \param base The base of the integer to read. Supported values are 0 and 2
  3426. * to 36 inclusive. If 0, the base will be inferred from the
  3427. * number's prefix (0x for hexadecimal, 0 for octal, decimal
  3428. * otherwise).
  3429. * \returns the parsed `unsigned long`, or 0 if no number could be parsed.
  3430. *
  3431. * \threadsafety It is safe to call this function from any thread.
  3432. *
  3433. * \since This function is available since SDL 3.1.3.
  3434. *
  3435. * \sa SDL_atoi
  3436. * \sa SDL_atof
  3437. * \sa SDL_strtol
  3438. * \sa SDL_strtoll
  3439. * \sa SDL_strtoull
  3440. * \sa SDL_strtod
  3441. * \sa SDL_ultoa
  3442. */
  3443. extern SDL_DECLSPEC unsigned long SDLCALL SDL_strtoul(const char *str, char **endp, int base);
  3444. /**
  3445. * Parse a `long long` from a string.
  3446. *
  3447. * If `str` starts with whitespace, then those whitespace characters are
  3448. * skipped before attempting to parse the number.
  3449. *
  3450. * If the parsed number does not fit inside a `long long`, the result is
  3451. * clamped to the minimum and maximum representable `long long` values.
  3452. *
  3453. * \param str The null-terminated string to read. Must not be NULL.
  3454. * \param endp If not NULL, the address of the first invalid character (i.e.
  3455. * the next character after the parsed number) will be written to
  3456. * this pointer.
  3457. * \param base The base of the integer to read. Supported values are 0 and 2
  3458. * to 36 inclusive. If 0, the base will be inferred from the
  3459. * number's prefix (0x for hexadecimal, 0 for octal, decimal
  3460. * otherwise).
  3461. * \returns the parsed `long long`, or 0 if no number could be parsed.
  3462. *
  3463. * \threadsafety It is safe to call this function from any thread.
  3464. *
  3465. * \since This function is available since SDL 3.1.3.
  3466. *
  3467. * \sa SDL_atoi
  3468. * \sa SDL_atof
  3469. * \sa SDL_strtol
  3470. * \sa SDL_strtoul
  3471. * \sa SDL_strtoull
  3472. * \sa SDL_strtod
  3473. * \sa SDL_lltoa
  3474. */
  3475. extern SDL_DECLSPEC long long SDLCALL SDL_strtoll(const char *str, char **endp, int base);
  3476. /**
  3477. * Parse an `unsigned long long` from a string.
  3478. *
  3479. * If `str` starts with whitespace, then those whitespace characters are
  3480. * skipped before attempting to parse the number.
  3481. *
  3482. * If the parsed number does not fit inside an `unsigned long long`, the
  3483. * result is clamped to the maximum representable `unsigned long long` value.
  3484. *
  3485. * \param str The null-terminated string to read. Must not be NULL.
  3486. * \param endp If not NULL, the address of the first invalid character (i.e.
  3487. * the next character after the parsed number) will be written to
  3488. * this pointer.
  3489. * \param base The base of the integer to read. Supported values are 0 and 2
  3490. * to 36 inclusive. If 0, the base will be inferred from the
  3491. * number's prefix (0x for hexadecimal, 0 for octal, decimal
  3492. * otherwise).
  3493. * \returns the parsed `unsigned long long`, or 0 if no number could be
  3494. * parsed.
  3495. *
  3496. * \threadsafety It is safe to call this function from any thread.
  3497. *
  3498. * \since This function is available since SDL 3.1.3.
  3499. *
  3500. * \sa SDL_atoi
  3501. * \sa SDL_atof
  3502. * \sa SDL_strtol
  3503. * \sa SDL_strtoll
  3504. * \sa SDL_strtoul
  3505. * \sa SDL_strtod
  3506. * \sa SDL_ulltoa
  3507. */
  3508. extern SDL_DECLSPEC unsigned long long SDLCALL SDL_strtoull(const char *str, char **endp, int base);
  3509. /**
  3510. * Parse a `double` from a string.
  3511. *
  3512. * This function makes fewer guarantees than the C runtime `strtod`:
  3513. *
  3514. * - Only decimal notation is guaranteed to be supported. The handling of
  3515. * scientific and hexadecimal notation is unspecified.
  3516. * - Whether or not INF and NAN can be parsed is unspecified.
  3517. * - The precision of the result is unspecified.
  3518. *
  3519. * \param str the null-terminated string to read. Must not be NULL.
  3520. * \param endp if not NULL, the address of the first invalid character (i.e.
  3521. * the next character after the parsed number) will be written to
  3522. * this pointer.
  3523. * \returns the parsed `double`, or 0 if no number could be parsed.
  3524. *
  3525. * \threadsafety It is safe to call this function from any thread.
  3526. *
  3527. * \since This function is available since SDL 3.1.3.
  3528. *
  3529. * \sa SDL_atoi
  3530. * \sa SDL_atof
  3531. * \sa SDL_strtol
  3532. * \sa SDL_strtoll
  3533. * \sa SDL_strtoul
  3534. * \sa SDL_strtoull
  3535. */
  3536. extern SDL_DECLSPEC double SDLCALL SDL_strtod(const char *str, char **endp);
  3537. /**
  3538. * Compare two null-terminated UTF-8 strings.
  3539. *
  3540. * Due to the nature of UTF-8 encoding, this will work with Unicode strings,
  3541. * since effectively this function just compares bytes until it hits a
  3542. * null-terminating character. Also due to the nature of UTF-8, this can be
  3543. * used with SDL_qsort() to put strings in (roughly) alphabetical order.
  3544. *
  3545. * \param str1 the first string to compare. NULL is not permitted!
  3546. * \param str2 the second string to compare. NULL is not permitted!
  3547. * \returns less than zero if str1 is "less than" str2, greater than zero if
  3548. * str1 is "greater than" str2, and zero if the strings match
  3549. * exactly.
  3550. *
  3551. * \threadsafety It is safe to call this function from any thread.
  3552. *
  3553. * \since This function is available since SDL 3.1.3.
  3554. */
  3555. extern SDL_DECLSPEC int SDLCALL SDL_strcmp(const char *str1, const char *str2);
  3556. /**
  3557. * Compare two UTF-8 strings up to a number of bytes.
  3558. *
  3559. * Due to the nature of UTF-8 encoding, this will work with Unicode strings,
  3560. * since effectively this function just compares bytes until it hits a
  3561. * null-terminating character. Also due to the nature of UTF-8, this can be
  3562. * used with SDL_qsort() to put strings in (roughly) alphabetical order.
  3563. *
  3564. * Note that while this function is intended to be used with UTF-8, it is
  3565. * doing a bytewise comparison, and `maxlen` specifies a _byte_ limit! If the
  3566. * limit lands in the middle of a multi-byte UTF-8 sequence, it will only
  3567. * compare a portion of the final character.
  3568. *
  3569. * `maxlen` specifies a maximum number of bytes to compare; if the strings
  3570. * match to this number of bytes (or both have matched to a null-terminator
  3571. * character before this number of bytes), they will be considered equal.
  3572. *
  3573. * \param str1 the first string to compare. NULL is not permitted!
  3574. * \param str2 the second string to compare. NULL is not permitted!
  3575. * \param maxlen the maximum number of _bytes_ to compare.
  3576. * \returns less than zero if str1 is "less than" str2, greater than zero if
  3577. * str1 is "greater than" str2, and zero if the strings match
  3578. * exactly.
  3579. *
  3580. * \threadsafety It is safe to call this function from any thread.
  3581. *
  3582. * \since This function is available since SDL 3.1.3.
  3583. */
  3584. extern SDL_DECLSPEC int SDLCALL SDL_strncmp(const char *str1, const char *str2, size_t maxlen);
  3585. /**
  3586. * Compare two null-terminated UTF-8 strings, case-insensitively.
  3587. *
  3588. * This will work with Unicode strings, using a technique called
  3589. * "case-folding" to handle the vast majority of case-sensitive human
  3590. * languages regardless of system locale. It can deal with expanding values: a
  3591. * German Eszett character can compare against two ASCII 's' chars and be
  3592. * considered a match, for example. A notable exception: it does not handle
  3593. * the Turkish 'i' character; human language is complicated!
  3594. *
  3595. * Since this handles Unicode, it expects the string to be well-formed UTF-8
  3596. * and not a null-terminated string of arbitrary bytes. Bytes that are not
  3597. * valid UTF-8 are treated as Unicode character U+FFFD (REPLACEMENT
  3598. * CHARACTER), which is to say two strings of random bits may turn out to
  3599. * match if they convert to the same amount of replacement characters.
  3600. *
  3601. * \param str1 the first string to compare. NULL is not permitted!
  3602. * \param str2 the second string to compare. NULL is not permitted!
  3603. * \returns less than zero if str1 is "less than" str2, greater than zero if
  3604. * str1 is "greater than" str2, and zero if the strings match
  3605. * exactly.
  3606. *
  3607. * \threadsafety It is safe to call this function from any thread.
  3608. *
  3609. * \since This function is available since SDL 3.1.3.
  3610. */
  3611. extern SDL_DECLSPEC int SDLCALL SDL_strcasecmp(const char *str1, const char *str2);
  3612. /**
  3613. * Compare two UTF-8 strings, case-insensitively, up to a number of bytes.
  3614. *
  3615. * This will work with Unicode strings, using a technique called
  3616. * "case-folding" to handle the vast majority of case-sensitive human
  3617. * languages regardless of system locale. It can deal with expanding values: a
  3618. * German Eszett character can compare against two ASCII 's' chars and be
  3619. * considered a match, for example. A notable exception: it does not handle
  3620. * the Turkish 'i' character; human language is complicated!
  3621. *
  3622. * Since this handles Unicode, it expects the string to be well-formed UTF-8
  3623. * and not a null-terminated string of arbitrary bytes. Bytes that are not
  3624. * valid UTF-8 are treated as Unicode character U+FFFD (REPLACEMENT
  3625. * CHARACTER), which is to say two strings of random bits may turn out to
  3626. * match if they convert to the same amount of replacement characters.
  3627. *
  3628. * Note that while this function is intended to be used with UTF-8, `maxlen`
  3629. * specifies a _byte_ limit! If the limit lands in the middle of a multi-byte
  3630. * UTF-8 sequence, it may convert a portion of the final character to one or
  3631. * more Unicode character U+FFFD (REPLACEMENT CHARACTER) so as not to overflow
  3632. * a buffer.
  3633. *
  3634. * `maxlen` specifies a maximum number of bytes to compare; if the strings
  3635. * match to this number of bytes (or both have matched to a null-terminator
  3636. * character before this number of bytes), they will be considered equal.
  3637. *
  3638. * \param str1 the first string to compare. NULL is not permitted!
  3639. * \param str2 the second string to compare. NULL is not permitted!
  3640. * \param maxlen the maximum number of bytes to compare.
  3641. * \returns less than zero if str1 is "less than" str2, greater than zero if
  3642. * str1 is "greater than" str2, and zero if the strings match
  3643. * exactly.
  3644. *
  3645. * \threadsafety It is safe to call this function from any thread.
  3646. *
  3647. * \since This function is available since SDL 3.1.3.
  3648. */
  3649. extern SDL_DECLSPEC int SDLCALL SDL_strncasecmp(const char *str1, const char *str2, size_t maxlen);
  3650. /**
  3651. * Searches a string for the first occurence of any character contained in a
  3652. * breakset, and returns a pointer from the string to that character.
  3653. *
  3654. * \param str The null-terminated string to be searched. Must not be NULL, and
  3655. * must not overlap with `breakset`.
  3656. * \param breakset A null-terminated string containing the list of characters
  3657. * to look for. Must not be NULL, and must not overlap with
  3658. * `str`.
  3659. * \returns A pointer to the location, in str, of the first occurence of a
  3660. * character present in the breakset, or NULL if none is found.
  3661. *
  3662. * \threadsafety It is safe to call this function from any thread.
  3663. *
  3664. * \since This function is available since SDL 3.1.3.
  3665. */
  3666. extern SDL_DECLSPEC char * SDLCALL SDL_strpbrk(const char *str, const char *breakset);
  3667. /**
  3668. * The Unicode REPLACEMENT CHARACTER codepoint.
  3669. *
  3670. * SDL_StepUTF8() and SDL_StepBackUTF8() report this codepoint when they
  3671. * encounter a UTF-8 string with encoding errors.
  3672. *
  3673. * This tends to render as something like a question mark in most places.
  3674. *
  3675. * \since This macro is available since SDL 3.1.3.
  3676. *
  3677. * \sa SDL_StepBackUTF8
  3678. * \sa SDL_StepUTF8
  3679. */
  3680. #define SDL_INVALID_UNICODE_CODEPOINT 0xFFFD
  3681. /**
  3682. * Decode a UTF-8 string, one Unicode codepoint at a time.
  3683. *
  3684. * This will return the first Unicode codepoint in the UTF-8 encoded string in
  3685. * `*pstr`, and then advance `*pstr` past any consumed bytes before returning.
  3686. *
  3687. * It will not access more than `*pslen` bytes from the string. `*pslen` will
  3688. * be adjusted, as well, subtracting the number of bytes consumed.
  3689. *
  3690. * `pslen` is allowed to be NULL, in which case the string _must_ be
  3691. * NULL-terminated, as the function will blindly read until it sees the NULL
  3692. * char.
  3693. *
  3694. * if `*pslen` is zero, it assumes the end of string is reached and returns a
  3695. * zero codepoint regardless of the contents of the string buffer.
  3696. *
  3697. * If the resulting codepoint is zero (a NULL terminator), or `*pslen` is
  3698. * zero, it will not advance `*pstr` or `*pslen` at all.
  3699. *
  3700. * Generally this function is called in a loop until it returns zero,
  3701. * adjusting its parameters each iteration.
  3702. *
  3703. * If an invalid UTF-8 sequence is encountered, this function returns
  3704. * SDL_INVALID_UNICODE_CODEPOINT and advances the string/length by one byte
  3705. * (which is to say, a multibyte sequence might produce several
  3706. * SDL_INVALID_UNICODE_CODEPOINT returns before it syncs to the next valid
  3707. * UTF-8 sequence).
  3708. *
  3709. * Several things can generate invalid UTF-8 sequences, including overlong
  3710. * encodings, the use of UTF-16 surrogate values, and truncated data. Please
  3711. * refer to
  3712. * [RFC3629](https://www.ietf.org/rfc/rfc3629.txt)
  3713. * for details.
  3714. *
  3715. * \param pstr a pointer to a UTF-8 string pointer to be read and adjusted.
  3716. * \param pslen a pointer to the number of bytes in the string, to be read and
  3717. * adjusted. NULL is allowed.
  3718. * \returns the first Unicode codepoint in the string.
  3719. *
  3720. * \threadsafety It is safe to call this function from any thread.
  3721. *
  3722. * \since This function is available since SDL 3.1.3.
  3723. */
  3724. extern SDL_DECLSPEC Uint32 SDLCALL SDL_StepUTF8(const char **pstr, size_t *pslen);
  3725. /**
  3726. * Decode a UTF-8 string in reverse, one Unicode codepoint at a time.
  3727. *
  3728. * This will go to the start of the previous Unicode codepoint in the string,
  3729. * move `*pstr` to that location and return that codepoint.
  3730. *
  3731. * If `*pstr` is already at the start of the string), it will not advance
  3732. * `*pstr` at all.
  3733. *
  3734. * Generally this function is called in a loop until it returns zero,
  3735. * adjusting its parameter each iteration.
  3736. *
  3737. * If an invalid UTF-8 sequence is encountered, this function returns
  3738. * SDL_INVALID_UNICODE_CODEPOINT.
  3739. *
  3740. * Several things can generate invalid UTF-8 sequences, including overlong
  3741. * encodings, the use of UTF-16 surrogate values, and truncated data. Please
  3742. * refer to
  3743. * [RFC3629](https://www.ietf.org/rfc/rfc3629.txt)
  3744. * for details.
  3745. *
  3746. * \param start a pointer to the beginning of the UTF-8 string.
  3747. * \param pstr a pointer to a UTF-8 string pointer to be read and adjusted.
  3748. * \returns the previous Unicode codepoint in the string.
  3749. *
  3750. * \threadsafety It is safe to call this function from any thread.
  3751. *
  3752. * \since This function is available since SDL 3.1.6.
  3753. */
  3754. extern SDL_DECLSPEC Uint32 SDLCALL SDL_StepBackUTF8(const char *start, const char **pstr);
  3755. /**
  3756. * Convert a single Unicode codepoint to UTF-8.
  3757. *
  3758. * The buffer pointed to by `dst` must be at least 4 bytes long, as this
  3759. * function may generate between 1 and 4 bytes of output.
  3760. *
  3761. * This function returns the first byte _after_ the newly-written UTF-8
  3762. * sequence, which is useful for encoding multiple codepoints in a loop, or
  3763. * knowing where to write a NULL-terminator character to end the string (in
  3764. * either case, plan to have a buffer of _more_ than 4 bytes!).
  3765. *
  3766. * If `codepoint` is an invalid value (outside the Unicode range, or a UTF-16
  3767. * surrogate value, etc), this will use U+FFFD (REPLACEMENT CHARACTER) for the
  3768. * codepoint instead, and not set an error.
  3769. *
  3770. * If `dst` is NULL, this returns NULL immediately without writing to the
  3771. * pointer and without setting an error.
  3772. *
  3773. * \param codepoint a Unicode codepoint to convert to UTF-8.
  3774. * \param dst the location to write the encoded UTF-8. Must point to at least
  3775. * 4 bytes!
  3776. * \returns the first byte past the newly-written UTF-8 sequence.
  3777. *
  3778. * \threadsafety It is safe to call this function from any thread.
  3779. *
  3780. * \since This function is available since SDL 3.1.3.
  3781. */
  3782. extern SDL_DECLSPEC char * SDLCALL SDL_UCS4ToUTF8(Uint32 codepoint, char *dst);
  3783. /**
  3784. * This works exactly like sscanf() but doesn't require access to a C runtime.
  3785. *
  3786. * Scan a string, matching a format string, converting each '%' item and
  3787. * storing it to pointers provided through variable arguments.
  3788. *
  3789. * \param text the string to scan. Must not be NULL.
  3790. * \param fmt a printf-style format string. Must not be NULL.
  3791. * \param ... a list of pointers to values to be filled in with scanned items.
  3792. * \returns the number of items that matched the format string.
  3793. *
  3794. * \threadsafety It is safe to call this function from any thread.
  3795. *
  3796. * \since This function is available since SDL 3.1.3.
  3797. */
  3798. extern SDL_DECLSPEC int SDLCALL SDL_sscanf(const char *text, SDL_SCANF_FORMAT_STRING const char *fmt, ...) SDL_SCANF_VARARG_FUNC(2);
  3799. /**
  3800. * This works exactly like vsscanf() but doesn't require access to a C
  3801. * runtime.
  3802. *
  3803. * Functions identically to SDL_sscanf(), except it takes a `va_list` instead
  3804. * of using `...` variable arguments.
  3805. *
  3806. * \param text the string to scan. Must not be NULL.
  3807. * \param fmt a printf-style format string. Must not be NULL.
  3808. * \param ap a `va_list` of pointers to values to be filled in with scanned
  3809. * items.
  3810. * \returns the number of items that matched the format string.
  3811. *
  3812. * \threadsafety It is safe to call this function from any thread.
  3813. *
  3814. * \since This function is available since SDL 3.1.3.
  3815. */
  3816. 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);
  3817. /**
  3818. * This works exactly like snprintf() but doesn't require access to a C
  3819. * runtime.
  3820. *
  3821. * Format a string of up to `maxlen`-1 bytes, converting each '%' item with
  3822. * values provided through variable arguments.
  3823. *
  3824. * While some C runtimes differ on how to deal with too-large strings, this
  3825. * function null-terminates the output, by treating the null-terminator as
  3826. * part of the `maxlen` count. Note that if `maxlen` is zero, however, no
  3827. * bytes will be written at all.
  3828. *
  3829. * This function returns the number of _bytes_ (not _characters_) that should
  3830. * be written, excluding the null-terminator character. If this returns a
  3831. * number >= `maxlen`, it means the output string was truncated. A negative
  3832. * return value means an error occurred.
  3833. *
  3834. * Referencing the output string's pointer with a format item is undefined
  3835. * behavior.
  3836. *
  3837. * \param text the buffer to write the string into. Must not be NULL.
  3838. * \param maxlen the maximum bytes to write, including the null-terminator.
  3839. * \param fmt a printf-style format string. Must not be NULL.
  3840. * \param ... a list of values to be used with the format string.
  3841. * \returns the number of bytes that should be written, not counting the
  3842. * null-terminator char, or a negative value on error.
  3843. *
  3844. * \threadsafety It is safe to call this function from any thread.
  3845. *
  3846. * \since This function is available since SDL 3.1.3.
  3847. */
  3848. 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);
  3849. /**
  3850. * This works exactly like swprintf() but doesn't require access to a C
  3851. * runtime.
  3852. *
  3853. * Format a wide string of up to `maxlen`-1 wchar_t values, converting each
  3854. * '%' item with values provided through variable arguments.
  3855. *
  3856. * While some C runtimes differ on how to deal with too-large strings, this
  3857. * function null-terminates the output, by treating the null-terminator as
  3858. * part of the `maxlen` count. Note that if `maxlen` is zero, however, no wide
  3859. * characters will be written at all.
  3860. *
  3861. * This function returns the number of _wide characters_ (not _codepoints_)
  3862. * that should be written, excluding the null-terminator character. If this
  3863. * returns a number >= `maxlen`, it means the output string was truncated. A
  3864. * negative return value means an error occurred.
  3865. *
  3866. * Referencing the output string's pointer with a format item is undefined
  3867. * behavior.
  3868. *
  3869. * \param text the buffer to write the wide string into. Must not be NULL.
  3870. * \param maxlen the maximum wchar_t values to write, including the
  3871. * null-terminator.
  3872. * \param fmt a printf-style format string. Must not be NULL.
  3873. * \param ... a list of values to be used with the format string.
  3874. * \returns the number of wide characters that should be written, not counting
  3875. * the null-terminator char, or a negative value on error.
  3876. *
  3877. * \threadsafety It is safe to call this function from any thread.
  3878. *
  3879. * \since This function is available since SDL 3.1.3.
  3880. */
  3881. 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);
  3882. /**
  3883. * This works exactly like vsnprintf() but doesn't require access to a C
  3884. * runtime.
  3885. *
  3886. * Functions identically to SDL_snprintf(), except it takes a `va_list`
  3887. * instead of using `...` variable arguments.
  3888. *
  3889. * \param text the buffer to write the string into. Must not be NULL.
  3890. * \param maxlen the maximum bytes to write, including the null-terminator.
  3891. * \param fmt a printf-style format string. Must not be NULL.
  3892. * \param ap a `va_list` values to be used with the format string.
  3893. * \returns the number of bytes that should be written, not counting the
  3894. * null-terminator char, or a negative value on error.
  3895. *
  3896. * \threadsafety It is safe to call this function from any thread.
  3897. *
  3898. * \since This function is available since SDL 3.1.3.
  3899. */
  3900. 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);
  3901. /**
  3902. * This works exactly like vswprintf() but doesn't require access to a C
  3903. * runtime.
  3904. *
  3905. * Functions identically to SDL_swprintf(), except it takes a `va_list`
  3906. * instead of using `...` variable arguments.
  3907. *
  3908. * \param text the buffer to write the string into. Must not be NULL.
  3909. * \param maxlen the maximum wide characters to write, including the
  3910. * null-terminator.
  3911. * \param fmt a printf-style format wide string. Must not be NULL.
  3912. * \param ap a `va_list` values to be used with the format string.
  3913. * \returns the number of wide characters that should be written, not counting
  3914. * the null-terminator char, or a negative value on error.
  3915. *
  3916. * \threadsafety It is safe to call this function from any thread.
  3917. *
  3918. * \since This function is available since SDL 3.1.3.
  3919. */
  3920. 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);
  3921. /**
  3922. * This works exactly like asprintf() but doesn't require access to a C
  3923. * runtime.
  3924. *
  3925. * Functions identically to SDL_snprintf(), except it allocates a buffer large
  3926. * enough to hold the output string on behalf of the caller.
  3927. *
  3928. * On success, this function returns the number of bytes (not characters)
  3929. * comprising the output string, not counting the null-terminator character,
  3930. * and sets `*strp` to the newly-allocated string.
  3931. *
  3932. * On error, this function returns a negative number, and the value of `*strp`
  3933. * is undefined.
  3934. *
  3935. * The returned string is owned by the caller, and should be passed to
  3936. * SDL_free when no longer needed.
  3937. *
  3938. * \param strp on output, is set to the new string. Must not be NULL.
  3939. * \param fmt a printf-style format string. Must not be NULL.
  3940. * \param ... a list of values to be used with the format string.
  3941. * \returns the number of bytes in the newly-allocated string, not counting
  3942. * the null-terminator char, or a negative value on error.
  3943. *
  3944. * \threadsafety It is safe to call this function from any thread.
  3945. *
  3946. * \since This function is available since SDL 3.1.3.
  3947. */
  3948. extern SDL_DECLSPEC int SDLCALL SDL_asprintf(char **strp, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) SDL_PRINTF_VARARG_FUNC(2);
  3949. /**
  3950. * This works exactly like vasprintf() but doesn't require access to a C
  3951. * runtime.
  3952. *
  3953. * Functions identically to SDL_asprintf(), except it takes a `va_list`
  3954. * instead of using `...` variable arguments.
  3955. *
  3956. * \param strp on output, is set to the new string. Must not be NULL.
  3957. * \param fmt a printf-style format string. Must not be NULL.
  3958. * \param ap a `va_list` values to be used with the format string.
  3959. * \returns the number of bytes in the newly-allocated string, not counting
  3960. * the null-terminator char, or a negative value on error.
  3961. *
  3962. * \threadsafety It is safe to call this function from any thread.
  3963. *
  3964. * \since This function is available since SDL 3.1.3.
  3965. */
  3966. extern SDL_DECLSPEC int SDLCALL SDL_vasprintf(char **strp, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap) SDL_PRINTF_VARARG_FUNCV(2);
  3967. /**
  3968. * Seeds the pseudo-random number generator.
  3969. *
  3970. * Reusing the seed number will cause SDL_rand_*() to repeat the same stream
  3971. * of 'random' numbers.
  3972. *
  3973. * \param seed the value to use as a random number seed, or 0 to use
  3974. * SDL_GetPerformanceCounter().
  3975. *
  3976. * \threadsafety This should be called on the same thread that calls
  3977. * SDL_rand*()
  3978. *
  3979. * \since This function is available since SDL 3.1.3.
  3980. *
  3981. * \sa SDL_rand
  3982. * \sa SDL_rand_bits
  3983. * \sa SDL_randf
  3984. */
  3985. extern SDL_DECLSPEC void SDLCALL SDL_srand(Uint64 seed);
  3986. /**
  3987. * Generate a pseudo-random number less than n for positive n
  3988. *
  3989. * The method used is faster and of better quality than `rand() % n`. Odds are
  3990. * roughly 99.9% even for n = 1 million. Evenness is better for smaller n, and
  3991. * much worse as n gets bigger.
  3992. *
  3993. * Example: to simulate a d6 use `SDL_rand(6) + 1` The +1 converts 0..5 to
  3994. * 1..6
  3995. *
  3996. * If you want to generate a pseudo-random number in the full range of Sint32,
  3997. * you should use: (Sint32)SDL_rand_bits()
  3998. *
  3999. * If you want reproducible output, be sure to initialize with SDL_srand()
  4000. * first.
  4001. *
  4002. * There are no guarantees as to the quality of the random sequence produced,
  4003. * and this should not be used for security (cryptography, passwords) or where
  4004. * money is on the line (loot-boxes, casinos). There are many random number
  4005. * libraries available with different characteristics and you should pick one
  4006. * of those to meet any serious needs.
  4007. *
  4008. * \param n the number of possible outcomes. n must be positive.
  4009. * \returns a random value in the range of [0 .. n-1].
  4010. *
  4011. * \threadsafety All calls should be made from a single thread
  4012. *
  4013. * \since This function is available since SDL 3.1.3.
  4014. *
  4015. * \sa SDL_srand
  4016. * \sa SDL_randf
  4017. */
  4018. extern SDL_DECLSPEC Sint32 SDLCALL SDL_rand(Sint32 n);
  4019. /**
  4020. * Generate a uniform pseudo-random floating point number less than 1.0
  4021. *
  4022. * If you want reproducible output, be sure to initialize with SDL_srand()
  4023. * first.
  4024. *
  4025. * There are no guarantees as to the quality of the random sequence produced,
  4026. * and this should not be used for security (cryptography, passwords) or where
  4027. * money is on the line (loot-boxes, casinos). There are many random number
  4028. * libraries available with different characteristics and you should pick one
  4029. * of those to meet any serious needs.
  4030. *
  4031. * \returns a random value in the range of [0.0, 1.0).
  4032. *
  4033. * \threadsafety All calls should be made from a single thread
  4034. *
  4035. * \since This function is available since SDL 3.1.3.
  4036. *
  4037. * \sa SDL_srand
  4038. * \sa SDL_rand
  4039. */
  4040. extern SDL_DECLSPEC float SDLCALL SDL_randf(void);
  4041. /**
  4042. * Generate 32 pseudo-random bits.
  4043. *
  4044. * You likely want to use SDL_rand() to get a psuedo-random number instead.
  4045. *
  4046. * There are no guarantees as to the quality of the random sequence produced,
  4047. * and this should not be used for security (cryptography, passwords) or where
  4048. * money is on the line (loot-boxes, casinos). There are many random number
  4049. * libraries available with different characteristics and you should pick one
  4050. * of those to meet any serious needs.
  4051. *
  4052. * \returns a random value in the range of [0-SDL_MAX_UINT32].
  4053. *
  4054. * \threadsafety All calls should be made from a single thread
  4055. *
  4056. * \since This function is available since SDL 3.1.3.
  4057. *
  4058. * \sa SDL_rand
  4059. * \sa SDL_randf
  4060. * \sa SDL_srand
  4061. */
  4062. extern SDL_DECLSPEC Uint32 SDLCALL SDL_rand_bits(void);
  4063. /**
  4064. * Generate a pseudo-random number less than n for positive n
  4065. *
  4066. * The method used is faster and of better quality than `rand() % n`. Odds are
  4067. * roughly 99.9% even for n = 1 million. Evenness is better for smaller n, and
  4068. * much worse as n gets bigger.
  4069. *
  4070. * Example: to simulate a d6 use `SDL_rand_r(state, 6) + 1` The +1 converts
  4071. * 0..5 to 1..6
  4072. *
  4073. * If you want to generate a pseudo-random number in the full range of Sint32,
  4074. * you should use: (Sint32)SDL_rand_bits_r(state)
  4075. *
  4076. * There are no guarantees as to the quality of the random sequence produced,
  4077. * and this should not be used for security (cryptography, passwords) or where
  4078. * money is on the line (loot-boxes, casinos). There are many random number
  4079. * libraries available with different characteristics and you should pick one
  4080. * of those to meet any serious needs.
  4081. *
  4082. * \param state a pointer to the current random number state, this may not be
  4083. * NULL.
  4084. * \param n the number of possible outcomes. n must be positive.
  4085. * \returns a random value in the range of [0 .. n-1].
  4086. *
  4087. * \threadsafety This function is thread-safe, as long as the state pointer
  4088. * isn't shared between threads.
  4089. *
  4090. * \since This function is available since SDL 3.1.3.
  4091. *
  4092. * \sa SDL_rand
  4093. * \sa SDL_rand_bits_r
  4094. * \sa SDL_randf_r
  4095. */
  4096. extern SDL_DECLSPEC Sint32 SDLCALL SDL_rand_r(Uint64 *state, Sint32 n);
  4097. /**
  4098. * Generate a uniform pseudo-random floating point number less than 1.0
  4099. *
  4100. * If you want reproducible output, be sure to initialize with SDL_srand()
  4101. * first.
  4102. *
  4103. * There are no guarantees as to the quality of the random sequence produced,
  4104. * and this should not be used for security (cryptography, passwords) or where
  4105. * money is on the line (loot-boxes, casinos). There are many random number
  4106. * libraries available with different characteristics and you should pick one
  4107. * of those to meet any serious needs.
  4108. *
  4109. * \param state a pointer to the current random number state, this may not be
  4110. * NULL.
  4111. * \returns a random value in the range of [0.0, 1.0).
  4112. *
  4113. * \threadsafety This function is thread-safe, as long as the state pointer
  4114. * isn't shared between threads.
  4115. *
  4116. * \since This function is available since SDL 3.1.3.
  4117. *
  4118. * \sa SDL_rand_bits_r
  4119. * \sa SDL_rand_r
  4120. * \sa SDL_randf
  4121. */
  4122. extern SDL_DECLSPEC float SDLCALL SDL_randf_r(Uint64 *state);
  4123. /**
  4124. * Generate 32 pseudo-random bits.
  4125. *
  4126. * You likely want to use SDL_rand_r() to get a psuedo-random number instead.
  4127. *
  4128. * There are no guarantees as to the quality of the random sequence produced,
  4129. * and this should not be used for security (cryptography, passwords) or where
  4130. * money is on the line (loot-boxes, casinos). There are many random number
  4131. * libraries available with different characteristics and you should pick one
  4132. * of those to meet any serious needs.
  4133. *
  4134. * \param state a pointer to the current random number state, this may not be
  4135. * NULL.
  4136. * \returns a random value in the range of [0-SDL_MAX_UINT32].
  4137. *
  4138. * \threadsafety This function is thread-safe, as long as the state pointer
  4139. * isn't shared between threads.
  4140. *
  4141. * \since This function is available since SDL 3.1.3.
  4142. *
  4143. * \sa SDL_rand_r
  4144. * \sa SDL_randf_r
  4145. */
  4146. extern SDL_DECLSPEC Uint32 SDLCALL SDL_rand_bits_r(Uint64 *state);
  4147. #ifndef SDL_PI_D
  4148. /**
  4149. * The value of Pi, as a double-precision floating point literal.
  4150. *
  4151. * \since This macro is available since SDL 3.1.3.
  4152. *
  4153. * \sa SDL_PI_F
  4154. */
  4155. #define SDL_PI_D 3.141592653589793238462643383279502884 /**< pi (double) */
  4156. #endif
  4157. #ifndef SDL_PI_F
  4158. /**
  4159. * The value of Pi, as a single-precision floating point literal.
  4160. *
  4161. * \since This macro is available since SDL 3.1.3.
  4162. *
  4163. * \sa SDL_PI_D
  4164. */
  4165. #define SDL_PI_F 3.141592653589793238462643383279502884F /**< pi (float) */
  4166. #endif
  4167. /**
  4168. * Compute the arc cosine of `x`.
  4169. *
  4170. * The definition of `y = acos(x)` is `x = cos(y)`.
  4171. *
  4172. * Domain: `-1 <= x <= 1`
  4173. *
  4174. * Range: `0 <= y <= Pi`
  4175. *
  4176. * This function operates on double-precision floating point values, use
  4177. * SDL_acosf for single-precision floats.
  4178. *
  4179. * This function may use a different approximation across different versions,
  4180. * platforms and configurations. i.e, it can return a different value given
  4181. * the same input on different machines or operating systems, or if SDL is
  4182. * updated.
  4183. *
  4184. * \param x floating point value.
  4185. * \returns arc cosine of `x`, in radians.
  4186. *
  4187. * \threadsafety It is safe to call this function from any thread.
  4188. *
  4189. * \since This function is available since SDL 3.1.3.
  4190. *
  4191. * \sa SDL_acosf
  4192. * \sa SDL_asin
  4193. * \sa SDL_cos
  4194. */
  4195. extern SDL_DECLSPEC double SDLCALL SDL_acos(double x);
  4196. /**
  4197. * Compute the arc cosine of `x`.
  4198. *
  4199. * The definition of `y = acos(x)` is `x = cos(y)`.
  4200. *
  4201. * Domain: `-1 <= x <= 1`
  4202. *
  4203. * Range: `0 <= y <= Pi`
  4204. *
  4205. * This function operates on single-precision floating point values, use
  4206. * SDL_acos for double-precision floats.
  4207. *
  4208. * This function may use a different approximation across different versions,
  4209. * platforms and configurations. i.e, it can return a different value given
  4210. * the same input on different machines or operating systems, or if SDL is
  4211. * updated.
  4212. *
  4213. * \param x floating point value.
  4214. * \returns arc cosine of `x`, in radians.
  4215. *
  4216. * \threadsafety It is safe to call this function from any thread.
  4217. *
  4218. * \since This function is available since SDL 3.1.3.
  4219. *
  4220. * \sa SDL_acos
  4221. * \sa SDL_asinf
  4222. * \sa SDL_cosf
  4223. */
  4224. extern SDL_DECLSPEC float SDLCALL SDL_acosf(float x);
  4225. /**
  4226. * Compute the arc sine of `x`.
  4227. *
  4228. * The definition of `y = asin(x)` is `x = sin(y)`.
  4229. *
  4230. * Domain: `-1 <= x <= 1`
  4231. *
  4232. * Range: `-Pi/2 <= y <= Pi/2`
  4233. *
  4234. * This function operates on double-precision floating point values, use
  4235. * SDL_asinf for single-precision floats.
  4236. *
  4237. * This function may use a different approximation across different versions,
  4238. * platforms and configurations. i.e, it can return a different value given
  4239. * the same input on different machines or operating systems, or if SDL is
  4240. * updated.
  4241. *
  4242. * \param x floating point value.
  4243. * \returns arc sine of `x`, in radians.
  4244. *
  4245. * \threadsafety It is safe to call this function from any thread.
  4246. *
  4247. * \since This function is available since SDL 3.1.3.
  4248. *
  4249. * \sa SDL_asinf
  4250. * \sa SDL_acos
  4251. * \sa SDL_sin
  4252. */
  4253. extern SDL_DECLSPEC double SDLCALL SDL_asin(double x);
  4254. /**
  4255. * Compute the arc sine of `x`.
  4256. *
  4257. * The definition of `y = asin(x)` is `x = sin(y)`.
  4258. *
  4259. * Domain: `-1 <= x <= 1`
  4260. *
  4261. * Range: `-Pi/2 <= y <= Pi/2`
  4262. *
  4263. * This function operates on single-precision floating point values, use
  4264. * SDL_asin for double-precision floats.
  4265. *
  4266. * This function may use a different approximation across different versions,
  4267. * platforms and configurations. i.e, it can return a different value given
  4268. * the same input on different machines or operating systems, or if SDL is
  4269. * updated.
  4270. *
  4271. * \param x floating point value.
  4272. * \returns arc sine of `x`, in radians.
  4273. *
  4274. * \threadsafety It is safe to call this function from any thread.
  4275. *
  4276. * \since This function is available since SDL 3.1.3.
  4277. *
  4278. * \sa SDL_asin
  4279. * \sa SDL_acosf
  4280. * \sa SDL_sinf
  4281. */
  4282. extern SDL_DECLSPEC float SDLCALL SDL_asinf(float x);
  4283. /**
  4284. * Compute the arc tangent of `x`.
  4285. *
  4286. * The definition of `y = atan(x)` is `x = tan(y)`.
  4287. *
  4288. * Domain: `-INF <= x <= INF`
  4289. *
  4290. * Range: `-Pi/2 <= y <= Pi/2`
  4291. *
  4292. * This function operates on double-precision floating point values, use
  4293. * SDL_atanf for single-precision floats.
  4294. *
  4295. * To calculate the arc tangent of y / x, use SDL_atan2.
  4296. *
  4297. * This function may use a different approximation across different versions,
  4298. * platforms and configurations. i.e, it can return a different value given
  4299. * the same input on different machines or operating systems, or if SDL is
  4300. * updated.
  4301. *
  4302. * \param x floating point value.
  4303. * \returns arc tangent of of `x` in radians, or 0 if `x = 0`.
  4304. *
  4305. * \threadsafety It is safe to call this function from any thread.
  4306. *
  4307. * \since This function is available since SDL 3.1.3.
  4308. *
  4309. * \sa SDL_atanf
  4310. * \sa SDL_atan2
  4311. * \sa SDL_tan
  4312. */
  4313. extern SDL_DECLSPEC double SDLCALL SDL_atan(double x);
  4314. /**
  4315. * Compute the arc tangent of `x`.
  4316. *
  4317. * The definition of `y = atan(x)` is `x = tan(y)`.
  4318. *
  4319. * Domain: `-INF <= x <= INF`
  4320. *
  4321. * Range: `-Pi/2 <= y <= Pi/2`
  4322. *
  4323. * This function operates on single-precision floating point values, use
  4324. * SDL_atan for dboule-precision floats.
  4325. *
  4326. * To calculate the arc tangent of y / x, use SDL_atan2f.
  4327. *
  4328. * This function may use a different approximation across different versions,
  4329. * platforms and configurations. i.e, it can return a different value given
  4330. * the same input on different machines or operating systems, or if SDL is
  4331. * updated.
  4332. *
  4333. * \param x floating point value.
  4334. * \returns arc tangent of of `x` in radians, or 0 if `x = 0`.
  4335. *
  4336. * \threadsafety It is safe to call this function from any thread.
  4337. *
  4338. * \since This function is available since SDL 3.1.3.
  4339. *
  4340. * \sa SDL_atan
  4341. * \sa SDL_atan2f
  4342. * \sa SDL_tanf
  4343. */
  4344. extern SDL_DECLSPEC float SDLCALL SDL_atanf(float x);
  4345. /**
  4346. * Compute the arc tangent of `y / x`, using the signs of x and y to adjust
  4347. * the result's quadrant.
  4348. *
  4349. * The definition of `z = atan2(x, y)` is `y = x tan(z)`, where the quadrant
  4350. * of z is determined based on the signs of x and y.
  4351. *
  4352. * Domain: `-INF <= x <= INF`, `-INF <= y <= INF`
  4353. *
  4354. * Range: `-Pi/2 <= y <= Pi/2`
  4355. *
  4356. * This function operates on double-precision floating point values, use
  4357. * SDL_atan2f for single-precision floats.
  4358. *
  4359. * To calculate the arc tangent of a single value, use SDL_atan.
  4360. *
  4361. * This function may use a different approximation across different versions,
  4362. * platforms and configurations. i.e, it can return a different value given
  4363. * the same input on different machines or operating systems, or if SDL is
  4364. * updated.
  4365. *
  4366. * \param y floating point value of the numerator (y coordinate).
  4367. * \param x floating point value of the denominator (x coordinate).
  4368. * \returns arc tangent of of `y / x` in radians, or, if `x = 0`, either
  4369. * `-Pi/2`, `0`, or `Pi/2`, depending on the value of `y`.
  4370. *
  4371. * \threadsafety It is safe to call this function from any thread.
  4372. *
  4373. * \since This function is available since SDL 3.1.3.
  4374. *
  4375. * \sa SDL_atan2f
  4376. * \sa SDL_atan
  4377. * \sa SDL_tan
  4378. */
  4379. extern SDL_DECLSPEC double SDLCALL SDL_atan2(double y, double x);
  4380. /**
  4381. * Compute the arc tangent of `y / x`, using the signs of x and y to adjust
  4382. * the result's quadrant.
  4383. *
  4384. * The definition of `z = atan2(x, y)` is `y = x tan(z)`, where the quadrant
  4385. * of z is determined based on the signs of x and y.
  4386. *
  4387. * Domain: `-INF <= x <= INF`, `-INF <= y <= INF`
  4388. *
  4389. * Range: `-Pi/2 <= y <= Pi/2`
  4390. *
  4391. * This function operates on single-precision floating point values, use
  4392. * SDL_atan2 for double-precision floats.
  4393. *
  4394. * To calculate the arc tangent of a single value, use SDL_atanf.
  4395. *
  4396. * This function may use a different approximation across different versions,
  4397. * platforms and configurations. i.e, it can return a different value given
  4398. * the same input on different machines or operating systems, or if SDL is
  4399. * updated.
  4400. *
  4401. * \param y floating point value of the numerator (y coordinate).
  4402. * \param x floating point value of the denominator (x coordinate).
  4403. * \returns arc tangent of of `y / x` in radians, or, if `x = 0`, either
  4404. * `-Pi/2`, `0`, or `Pi/2`, depending on the value of `y`.
  4405. *
  4406. * \threadsafety It is safe to call this function from any thread.
  4407. *
  4408. * \since This function is available since SDL 3.1.3.
  4409. *
  4410. * \sa SDL_atan2f
  4411. * \sa SDL_atan
  4412. * \sa SDL_tan
  4413. */
  4414. extern SDL_DECLSPEC float SDLCALL SDL_atan2f(float y, float x);
  4415. /**
  4416. * Compute the ceiling of `x`.
  4417. *
  4418. * The ceiling of `x` is the smallest integer `y` such that `y > x`, i.e `x`
  4419. * rounded up to the nearest integer.
  4420. *
  4421. * Domain: `-INF <= x <= INF`
  4422. *
  4423. * Range: `-INF <= y <= INF`, y integer
  4424. *
  4425. * This function operates on double-precision floating point values, use
  4426. * SDL_ceilf for single-precision floats.
  4427. *
  4428. * \param x floating point value.
  4429. * \returns the ceiling of `x`.
  4430. *
  4431. * \threadsafety It is safe to call this function from any thread.
  4432. *
  4433. * \since This function is available since SDL 3.1.3.
  4434. *
  4435. * \sa SDL_ceilf
  4436. * \sa SDL_floor
  4437. * \sa SDL_trunc
  4438. * \sa SDL_round
  4439. * \sa SDL_lround
  4440. */
  4441. extern SDL_DECLSPEC double SDLCALL SDL_ceil(double x);
  4442. /**
  4443. * Compute the ceiling of `x`.
  4444. *
  4445. * The ceiling of `x` is the smallest integer `y` such that `y > x`, i.e `x`
  4446. * rounded up to the nearest integer.
  4447. *
  4448. * Domain: `-INF <= x <= INF`
  4449. *
  4450. * Range: `-INF <= y <= INF`, y integer
  4451. *
  4452. * This function operates on single-precision floating point values, use
  4453. * SDL_ceil for double-precision floats.
  4454. *
  4455. * \param x floating point value.
  4456. * \returns the ceiling of `x`.
  4457. *
  4458. * \threadsafety It is safe to call this function from any thread.
  4459. *
  4460. * \since This function is available since SDL 3.1.3.
  4461. *
  4462. * \sa SDL_ceil
  4463. * \sa SDL_floorf
  4464. * \sa SDL_truncf
  4465. * \sa SDL_roundf
  4466. * \sa SDL_lroundf
  4467. */
  4468. extern SDL_DECLSPEC float SDLCALL SDL_ceilf(float x);
  4469. /**
  4470. * Copy the sign of one floating-point value to another.
  4471. *
  4472. * The definition of copysign is that ``copysign(x, y) = abs(x) * sign(y)``.
  4473. *
  4474. * Domain: `-INF <= x <= INF`, ``-INF <= y <= f``
  4475. *
  4476. * Range: `-INF <= z <= INF`
  4477. *
  4478. * This function operates on double-precision floating point values, use
  4479. * SDL_copysignf for single-precision floats.
  4480. *
  4481. * \param x floating point value to use as the magnitude.
  4482. * \param y floating point value to use as the sign.
  4483. * \returns the floating point value with the sign of y and the magnitude of
  4484. * x.
  4485. *
  4486. * \threadsafety It is safe to call this function from any thread.
  4487. *
  4488. * \since This function is available since SDL 3.1.3.
  4489. *
  4490. * \sa SDL_copysignf
  4491. * \sa SDL_fabs
  4492. */
  4493. extern SDL_DECLSPEC double SDLCALL SDL_copysign(double x, double y);
  4494. /**
  4495. * Copy the sign of one floating-point value to another.
  4496. *
  4497. * The definition of copysign is that ``copysign(x, y) = abs(x) * sign(y)``.
  4498. *
  4499. * Domain: `-INF <= x <= INF`, ``-INF <= y <= f``
  4500. *
  4501. * Range: `-INF <= z <= INF`
  4502. *
  4503. * This function operates on single-precision floating point values, use
  4504. * SDL_copysign for double-precision floats.
  4505. *
  4506. * \param x floating point value to use as the magnitude.
  4507. * \param y floating point value to use as the sign.
  4508. * \returns the floating point value with the sign of y and the magnitude of
  4509. * x.
  4510. *
  4511. * \threadsafety It is safe to call this function from any thread.
  4512. *
  4513. * \since This function is available since SDL 3.1.3.
  4514. *
  4515. * \sa SDL_copysignf
  4516. * \sa SDL_fabsf
  4517. */
  4518. extern SDL_DECLSPEC float SDLCALL SDL_copysignf(float x, float y);
  4519. /**
  4520. * Compute the cosine of `x`.
  4521. *
  4522. * Domain: `-INF <= x <= INF`
  4523. *
  4524. * Range: `-1 <= y <= 1`
  4525. *
  4526. * This function operates on double-precision floating point values, use
  4527. * SDL_cosf for single-precision floats.
  4528. *
  4529. * This function may use a different approximation across different versions,
  4530. * platforms and configurations. i.e, it can return a different value given
  4531. * the same input on different machines or operating systems, or if SDL is
  4532. * updated.
  4533. *
  4534. * \param x floating point value, in radians.
  4535. * \returns cosine of `x`.
  4536. *
  4537. * \threadsafety It is safe to call this function from any thread.
  4538. *
  4539. * \since This function is available since SDL 3.1.3.
  4540. *
  4541. * \sa SDL_cosf
  4542. * \sa SDL_acos
  4543. * \sa SDL_sin
  4544. */
  4545. extern SDL_DECLSPEC double SDLCALL SDL_cos(double x);
  4546. /**
  4547. * Compute the cosine of `x`.
  4548. *
  4549. * Domain: `-INF <= x <= INF`
  4550. *
  4551. * Range: `-1 <= y <= 1`
  4552. *
  4553. * This function operates on single-precision floating point values, use
  4554. * SDL_cos for double-precision floats.
  4555. *
  4556. * This function may use a different approximation across different versions,
  4557. * platforms and configurations. i.e, it can return a different value given
  4558. * the same input on different machines or operating systems, or if SDL is
  4559. * updated.
  4560. *
  4561. * \param x floating point value, in radians.
  4562. * \returns cosine of `x`.
  4563. *
  4564. * \threadsafety It is safe to call this function from any thread.
  4565. *
  4566. * \since This function is available since SDL 3.1.3.
  4567. *
  4568. * \sa SDL_cos
  4569. * \sa SDL_acosf
  4570. * \sa SDL_sinf
  4571. */
  4572. extern SDL_DECLSPEC float SDLCALL SDL_cosf(float x);
  4573. /**
  4574. * Compute the exponential of `x`.
  4575. *
  4576. * The definition of `y = exp(x)` is `y = e^x`, where `e` is the base of the
  4577. * natural logarithm. The inverse is the natural logarithm, SDL_log.
  4578. *
  4579. * Domain: `-INF <= x <= INF`
  4580. *
  4581. * Range: `0 <= y <= INF`
  4582. *
  4583. * The output will overflow if `exp(x)` is too large to be represented.
  4584. *
  4585. * This function operates on double-precision floating point values, use
  4586. * SDL_expf for single-precision floats.
  4587. *
  4588. * This function may use a different approximation across different versions,
  4589. * platforms and configurations. i.e, it can return a different value given
  4590. * the same input on different machines or operating systems, or if SDL is
  4591. * updated.
  4592. *
  4593. * \param x floating point value.
  4594. * \returns value of `e^x`.
  4595. *
  4596. * \threadsafety It is safe to call this function from any thread.
  4597. *
  4598. * \since This function is available since SDL 3.1.3.
  4599. *
  4600. * \sa SDL_expf
  4601. * \sa SDL_log
  4602. */
  4603. extern SDL_DECLSPEC double SDLCALL SDL_exp(double x);
  4604. /**
  4605. * Compute the exponential of `x`.
  4606. *
  4607. * The definition of `y = exp(x)` is `y = e^x`, where `e` is the base of the
  4608. * natural logarithm. The inverse is the natural logarithm, SDL_logf.
  4609. *
  4610. * Domain: `-INF <= x <= INF`
  4611. *
  4612. * Range: `0 <= y <= INF`
  4613. *
  4614. * The output will overflow if `exp(x)` is too large to be represented.
  4615. *
  4616. * This function operates on single-precision floating point values, use
  4617. * SDL_exp for double-precision floats.
  4618. *
  4619. * This function may use a different approximation across different versions,
  4620. * platforms and configurations. i.e, it can return a different value given
  4621. * the same input on different machines or operating systems, or if SDL is
  4622. * updated.
  4623. *
  4624. * \param x floating point value.
  4625. * \returns value of `e^x`.
  4626. *
  4627. * \threadsafety It is safe to call this function from any thread.
  4628. *
  4629. * \since This function is available since SDL 3.1.3.
  4630. *
  4631. * \sa SDL_exp
  4632. * \sa SDL_logf
  4633. */
  4634. extern SDL_DECLSPEC float SDLCALL SDL_expf(float x);
  4635. /**
  4636. * Compute the absolute value of `x`
  4637. *
  4638. * Domain: `-INF <= x <= INF`
  4639. *
  4640. * Range: `0 <= y <= INF`
  4641. *
  4642. * This function operates on double-precision floating point values, use
  4643. * SDL_copysignf for single-precision floats.
  4644. *
  4645. * \param x floating point value to use as the magnitude.
  4646. * \returns the absolute value of `x`.
  4647. *
  4648. * \threadsafety It is safe to call this function from any thread.
  4649. *
  4650. * \since This function is available since SDL 3.1.3.
  4651. *
  4652. * \sa SDL_fabsf
  4653. */
  4654. extern SDL_DECLSPEC double SDLCALL SDL_fabs(double x);
  4655. /**
  4656. * Compute the absolute value of `x`
  4657. *
  4658. * Domain: `-INF <= x <= INF`
  4659. *
  4660. * Range: `0 <= y <= INF`
  4661. *
  4662. * This function operates on single-precision floating point values, use
  4663. * SDL_copysignf for double-precision floats.
  4664. *
  4665. * \param x floating point value to use as the magnitude.
  4666. * \returns the absolute value of `x`.
  4667. *
  4668. * \threadsafety It is safe to call this function from any thread.
  4669. *
  4670. * \since This function is available since SDL 3.1.3.
  4671. *
  4672. * \sa SDL_fabs
  4673. */
  4674. extern SDL_DECLSPEC float SDLCALL SDL_fabsf(float x);
  4675. /**
  4676. * Compute the floor of `x`.
  4677. *
  4678. * The floor of `x` is the largest integer `y` such that `y > x`, i.e `x`
  4679. * rounded down to the nearest integer.
  4680. *
  4681. * Domain: `-INF <= x <= INF`
  4682. *
  4683. * Range: `-INF <= y <= INF`, y integer
  4684. *
  4685. * This function operates on double-precision floating point values, use
  4686. * SDL_floorf for single-precision floats.
  4687. *
  4688. * \param x floating point value.
  4689. * \returns the floor of `x`.
  4690. *
  4691. * \threadsafety It is safe to call this function from any thread.
  4692. *
  4693. * \since This function is available since SDL 3.1.3.
  4694. *
  4695. * \sa SDL_floorf
  4696. * \sa SDL_ceil
  4697. * \sa SDL_trunc
  4698. * \sa SDL_round
  4699. * \sa SDL_lround
  4700. */
  4701. extern SDL_DECLSPEC double SDLCALL SDL_floor(double x);
  4702. /**
  4703. * Compute the floor of `x`.
  4704. *
  4705. * The floor of `x` is the largest integer `y` such that `y > x`, i.e `x`
  4706. * rounded down to the nearest integer.
  4707. *
  4708. * Domain: `-INF <= x <= INF`
  4709. *
  4710. * Range: `-INF <= y <= INF`, y integer
  4711. *
  4712. * This function operates on single-precision floating point values, use
  4713. * SDL_floorf for double-precision floats.
  4714. *
  4715. * \param x floating point value.
  4716. * \returns the floor of `x`.
  4717. *
  4718. * \threadsafety It is safe to call this function from any thread.
  4719. *
  4720. * \since This function is available since SDL 3.1.3.
  4721. *
  4722. * \sa SDL_floor
  4723. * \sa SDL_ceilf
  4724. * \sa SDL_truncf
  4725. * \sa SDL_roundf
  4726. * \sa SDL_lroundf
  4727. */
  4728. extern SDL_DECLSPEC float SDLCALL SDL_floorf(float x);
  4729. /**
  4730. * Truncate `x` to an integer.
  4731. *
  4732. * Rounds `x` to the next closest integer to 0. This is equivalent to removing
  4733. * the fractional part of `x`, leaving only the integer part.
  4734. *
  4735. * Domain: `-INF <= x <= INF`
  4736. *
  4737. * Range: `-INF <= y <= INF`, y integer
  4738. *
  4739. * This function operates on double-precision floating point values, use
  4740. * SDL_truncf for single-precision floats.
  4741. *
  4742. * \param x floating point value.
  4743. * \returns `x` truncated to an integer.
  4744. *
  4745. * \threadsafety It is safe to call this function from any thread.
  4746. *
  4747. * \since This function is available since SDL 3.1.3.
  4748. *
  4749. * \sa SDL_truncf
  4750. * \sa SDL_fmod
  4751. * \sa SDL_ceil
  4752. * \sa SDL_floor
  4753. * \sa SDL_round
  4754. * \sa SDL_lround
  4755. */
  4756. extern SDL_DECLSPEC double SDLCALL SDL_trunc(double x);
  4757. /**
  4758. * Truncate `x` to an integer.
  4759. *
  4760. * Rounds `x` to the next closest integer to 0. This is equivalent to removing
  4761. * the fractional part of `x`, leaving only the integer part.
  4762. *
  4763. * Domain: `-INF <= x <= INF`
  4764. *
  4765. * Range: `-INF <= y <= INF`, y integer
  4766. *
  4767. * This function operates on single-precision floating point values, use
  4768. * SDL_truncf for double-precision floats.
  4769. *
  4770. * \param x floating point value.
  4771. * \returns `x` truncated to an integer.
  4772. *
  4773. * \threadsafety It is safe to call this function from any thread.
  4774. *
  4775. * \since This function is available since SDL 3.1.3.
  4776. *
  4777. * \sa SDL_trunc
  4778. * \sa SDL_fmodf
  4779. * \sa SDL_ceilf
  4780. * \sa SDL_floorf
  4781. * \sa SDL_roundf
  4782. * \sa SDL_lroundf
  4783. */
  4784. extern SDL_DECLSPEC float SDLCALL SDL_truncf(float x);
  4785. /**
  4786. * Return the floating-point remainder of `x / y`
  4787. *
  4788. * Divides `x` by `y`, and returns the remainder.
  4789. *
  4790. * Domain: `-INF <= x <= INF`, `-INF <= y <= INF`, `y != 0`
  4791. *
  4792. * Range: `-y <= z <= y`
  4793. *
  4794. * This function operates on double-precision floating point values, use
  4795. * SDL_fmodf for single-precision floats.
  4796. *
  4797. * \param x the numerator.
  4798. * \param y the denominator. Must not be 0.
  4799. * \returns the remainder of `x / y`.
  4800. *
  4801. * \threadsafety It is safe to call this function from any thread.
  4802. *
  4803. * \since This function is available since SDL 3.1.3.
  4804. *
  4805. * \sa SDL_fmodf
  4806. * \sa SDL_modf
  4807. * \sa SDL_trunc
  4808. * \sa SDL_ceil
  4809. * \sa SDL_floor
  4810. * \sa SDL_round
  4811. * \sa SDL_lround
  4812. */
  4813. extern SDL_DECLSPEC double SDLCALL SDL_fmod(double x, double y);
  4814. /**
  4815. * Return the floating-point remainder of `x / y`
  4816. *
  4817. * Divides `x` by `y`, and returns the remainder.
  4818. *
  4819. * Domain: `-INF <= x <= INF`, `-INF <= y <= INF`, `y != 0`
  4820. *
  4821. * Range: `-y <= z <= y`
  4822. *
  4823. * This function operates on single-precision floating point values, use
  4824. * SDL_fmod for single-precision floats.
  4825. *
  4826. * \param x the numerator.
  4827. * \param y the denominator. Must not be 0.
  4828. * \returns the remainder of `x / y`.
  4829. *
  4830. * \threadsafety It is safe to call this function from any thread.
  4831. *
  4832. * \since This function is available since SDL 3.1.3.
  4833. *
  4834. * \sa SDL_fmod
  4835. * \sa SDL_truncf
  4836. * \sa SDL_modff
  4837. * \sa SDL_ceilf
  4838. * \sa SDL_floorf
  4839. * \sa SDL_roundf
  4840. * \sa SDL_lroundf
  4841. */
  4842. extern SDL_DECLSPEC float SDLCALL SDL_fmodf(float x, float y);
  4843. /**
  4844. * Return whether the value is infinity.
  4845. *
  4846. * \param x double-precision floating point value.
  4847. * \returns non-zero if the value is infinity, 0 otherwise.
  4848. *
  4849. * \threadsafety It is safe to call this function from any thread.
  4850. *
  4851. * \since This function is available since SDL 3.1.3.
  4852. *
  4853. * \sa SDL_isinff
  4854. */
  4855. extern SDL_DECLSPEC int SDLCALL SDL_isinf(double x);
  4856. /**
  4857. * Return whether the value is infinity.
  4858. *
  4859. * \param x floating point value.
  4860. * \returns non-zero if the value is infinity, 0 otherwise.
  4861. *
  4862. * \threadsafety It is safe to call this function from any thread.
  4863. *
  4864. * \since This function is available since SDL 3.1.3.
  4865. *
  4866. * \sa SDL_isinf
  4867. */
  4868. extern SDL_DECLSPEC int SDLCALL SDL_isinff(float x);
  4869. /**
  4870. * Return whether the value is NaN.
  4871. *
  4872. * \param x double-precision floating point value.
  4873. * \returns non-zero if the value is NaN, 0 otherwise.
  4874. *
  4875. * \threadsafety It is safe to call this function from any thread.
  4876. *
  4877. * \since This function is available since SDL 3.1.3.
  4878. *
  4879. * \sa SDL_isnanf
  4880. */
  4881. extern SDL_DECLSPEC int SDLCALL SDL_isnan(double x);
  4882. /**
  4883. * Return whether the value is NaN.
  4884. *
  4885. * \param x floating point value.
  4886. * \returns non-zero if the value is NaN, 0 otherwise.
  4887. *
  4888. * \threadsafety It is safe to call this function from any thread.
  4889. *
  4890. * \since This function is available since SDL 3.1.3.
  4891. *
  4892. * \sa SDL_isnan
  4893. */
  4894. extern SDL_DECLSPEC int SDLCALL SDL_isnanf(float x);
  4895. /**
  4896. * Compute the natural logarithm of `x`.
  4897. *
  4898. * Domain: `0 < x <= INF`
  4899. *
  4900. * Range: `-INF <= y <= INF`
  4901. *
  4902. * It is an error for `x` to be less than or equal to 0.
  4903. *
  4904. * This function operates on double-precision floating point values, use
  4905. * SDL_logf for single-precision floats.
  4906. *
  4907. * This function may use a different approximation across different versions,
  4908. * platforms and configurations. i.e, it can return a different value given
  4909. * the same input on different machines or operating systems, or if SDL is
  4910. * updated.
  4911. *
  4912. * \param x floating point value. Must be greater than 0.
  4913. * \returns the natural logarithm of `x`.
  4914. *
  4915. * \threadsafety It is safe to call this function from any thread.
  4916. *
  4917. * \since This function is available since SDL 3.1.3.
  4918. *
  4919. * \sa SDL_logf
  4920. * \sa SDL_log10
  4921. * \sa SDL_exp
  4922. */
  4923. extern SDL_DECLSPEC double SDLCALL SDL_log(double x);
  4924. /**
  4925. * Compute the natural logarithm of `x`.
  4926. *
  4927. * Domain: `0 < x <= INF`
  4928. *
  4929. * Range: `-INF <= y <= INF`
  4930. *
  4931. * It is an error for `x` to be less than or equal to 0.
  4932. *
  4933. * This function operates on single-precision floating point values, use
  4934. * SDL_log for double-precision floats.
  4935. *
  4936. * This function may use a different approximation across different versions,
  4937. * platforms and configurations. i.e, it can return a different value given
  4938. * the same input on different machines or operating systems, or if SDL is
  4939. * updated.
  4940. *
  4941. * \param x floating point value. Must be greater than 0.
  4942. * \returns the natural logarithm of `x`.
  4943. *
  4944. * \threadsafety It is safe to call this function from any thread.
  4945. *
  4946. * \since This function is available since SDL 3.1.3.
  4947. *
  4948. * \sa SDL_log
  4949. * \sa SDL_expf
  4950. */
  4951. extern SDL_DECLSPEC float SDLCALL SDL_logf(float x);
  4952. /**
  4953. * Compute the base-10 logarithm of `x`.
  4954. *
  4955. * Domain: `0 < x <= INF`
  4956. *
  4957. * Range: `-INF <= y <= INF`
  4958. *
  4959. * It is an error for `x` to be less than or equal to 0.
  4960. *
  4961. * This function operates on double-precision floating point values, use
  4962. * SDL_log10f for single-precision floats.
  4963. *
  4964. * This function may use a different approximation across different versions,
  4965. * platforms and configurations. i.e, it can return a different value given
  4966. * the same input on different machines or operating systems, or if SDL is
  4967. * updated.
  4968. *
  4969. * \param x floating point value. Must be greater than 0.
  4970. * \returns the logarithm of `x`.
  4971. *
  4972. * \threadsafety It is safe to call this function from any thread.
  4973. *
  4974. * \since This function is available since SDL 3.1.3.
  4975. *
  4976. * \sa SDL_log10f
  4977. * \sa SDL_log
  4978. * \sa SDL_pow
  4979. */
  4980. extern SDL_DECLSPEC double SDLCALL SDL_log10(double x);
  4981. /**
  4982. * Compute the base-10 logarithm of `x`.
  4983. *
  4984. * Domain: `0 < x <= INF`
  4985. *
  4986. * Range: `-INF <= y <= INF`
  4987. *
  4988. * It is an error for `x` to be less than or equal to 0.
  4989. *
  4990. * This function operates on single-precision floating point values, use
  4991. * SDL_log10 for double-precision floats.
  4992. *
  4993. * This function may use a different approximation across different versions,
  4994. * platforms and configurations. i.e, it can return a different value given
  4995. * the same input on different machines or operating systems, or if SDL is
  4996. * updated.
  4997. *
  4998. * \param x floating point value. Must be greater than 0.
  4999. * \returns the logarithm of `x`.
  5000. *
  5001. * \threadsafety It is safe to call this function from any thread.
  5002. *
  5003. * \since This function is available since SDL 3.1.3.
  5004. *
  5005. * \sa SDL_log10
  5006. * \sa SDL_logf
  5007. * \sa SDL_powf
  5008. */
  5009. extern SDL_DECLSPEC float SDLCALL SDL_log10f(float x);
  5010. /**
  5011. * Split `x` into integer and fractional parts
  5012. *
  5013. * This function operates on double-precision floating point values, use
  5014. * SDL_modff for single-precision floats.
  5015. *
  5016. * \param x floating point value.
  5017. * \param y output pointer to store the integer part of `x`.
  5018. * \returns the fractional part of `x`.
  5019. *
  5020. * \threadsafety It is safe to call this function from any thread.
  5021. *
  5022. * \since This function is available since SDL 3.1.3.
  5023. *
  5024. * \sa SDL_modff
  5025. * \sa SDL_trunc
  5026. * \sa SDL_fmod
  5027. */
  5028. extern SDL_DECLSPEC double SDLCALL SDL_modf(double x, double *y);
  5029. /**
  5030. * Split `x` into integer and fractional parts
  5031. *
  5032. * This function operates on single-precision floating point values, use
  5033. * SDL_modf for double-precision floats.
  5034. *
  5035. * \param x floating point value.
  5036. * \param y output pointer to store the integer part of `x`.
  5037. * \returns the fractional part of `x`.
  5038. *
  5039. * \threadsafety It is safe to call this function from any thread.
  5040. *
  5041. * \since This function is available since SDL 3.1.3.
  5042. *
  5043. * \sa SDL_modf
  5044. * \sa SDL_truncf
  5045. * \sa SDL_fmodf
  5046. */
  5047. extern SDL_DECLSPEC float SDLCALL SDL_modff(float x, float *y);
  5048. /**
  5049. * Raise `x` to the power `y`
  5050. *
  5051. * Domain: `-INF <= x <= INF`, `-INF <= y <= INF`
  5052. *
  5053. * Range: `-INF <= z <= INF`
  5054. *
  5055. * If `y` is the base of the natural logarithm (e), consider using SDL_exp
  5056. * instead.
  5057. *
  5058. * This function operates on double-precision floating point values, use
  5059. * SDL_powf for single-precision floats.
  5060. *
  5061. * This function may use a different approximation across different versions,
  5062. * platforms and configurations. i.e, it can return a different value given
  5063. * the same input on different machines or operating systems, or if SDL is
  5064. * updated.
  5065. *
  5066. * \param x the base.
  5067. * \param y the exponent.
  5068. * \returns `x` raised to the power `y`.
  5069. *
  5070. * \threadsafety It is safe to call this function from any thread.
  5071. *
  5072. * \since This function is available since SDL 3.1.3.
  5073. *
  5074. * \sa SDL_powf
  5075. * \sa SDL_exp
  5076. * \sa SDL_log
  5077. */
  5078. extern SDL_DECLSPEC double SDLCALL SDL_pow(double x, double y);
  5079. /**
  5080. * Raise `x` to the power `y`
  5081. *
  5082. * Domain: `-INF <= x <= INF`, `-INF <= y <= INF`
  5083. *
  5084. * Range: `-INF <= z <= INF`
  5085. *
  5086. * If `y` is the base of the natural logarithm (e), consider using SDL_exp
  5087. * instead.
  5088. *
  5089. * This function operates on single-precision floating point values, use
  5090. * SDL_powf for double-precision floats.
  5091. *
  5092. * This function may use a different approximation across different versions,
  5093. * platforms and configurations. i.e, it can return a different value given
  5094. * the same input on different machines or operating systems, or if SDL is
  5095. * updated.
  5096. *
  5097. * \param x the base.
  5098. * \param y the exponent.
  5099. * \returns `x` raised to the power `y`.
  5100. *
  5101. * \threadsafety It is safe to call this function from any thread.
  5102. *
  5103. * \since This function is available since SDL 3.1.3.
  5104. *
  5105. * \sa SDL_pow
  5106. * \sa SDL_expf
  5107. * \sa SDL_logf
  5108. */
  5109. extern SDL_DECLSPEC float SDLCALL SDL_powf(float x, float y);
  5110. /**
  5111. * Round `x` to the nearest integer.
  5112. *
  5113. * Rounds `x` to the nearest integer. Values halfway between integers will be
  5114. * rounded away from zero.
  5115. *
  5116. * Domain: `-INF <= x <= INF`
  5117. *
  5118. * Range: `-INF <= y <= INF`, y integer
  5119. *
  5120. * This function operates on double-precision floating point values, use
  5121. * SDL_roundf for single-precision floats. To get the result as an integer
  5122. * type, use SDL_lround.
  5123. *
  5124. * \param x floating point value.
  5125. * \returns the nearest integer to `x`.
  5126. *
  5127. * \threadsafety It is safe to call this function from any thread.
  5128. *
  5129. * \since This function is available since SDL 3.1.3.
  5130. *
  5131. * \sa SDL_roundf
  5132. * \sa SDL_lround
  5133. * \sa SDL_floor
  5134. * \sa SDL_ceil
  5135. * \sa SDL_trunc
  5136. */
  5137. extern SDL_DECLSPEC double SDLCALL SDL_round(double x);
  5138. /**
  5139. * Round `x` to the nearest integer.
  5140. *
  5141. * Rounds `x` to the nearest integer. Values halfway between integers will be
  5142. * rounded away from zero.
  5143. *
  5144. * Domain: `-INF <= x <= INF`
  5145. *
  5146. * Range: `-INF <= y <= INF`, y integer
  5147. *
  5148. * This function operates on double-precision floating point values, use
  5149. * SDL_roundf for single-precision floats. To get the result as an integer
  5150. * type, use SDL_lroundf.
  5151. *
  5152. * \param x floating point value.
  5153. * \returns the nearest integer to `x`.
  5154. *
  5155. * \threadsafety It is safe to call this function from any thread.
  5156. *
  5157. * \since This function is available since SDL 3.1.3.
  5158. *
  5159. * \sa SDL_round
  5160. * \sa SDL_lroundf
  5161. * \sa SDL_floorf
  5162. * \sa SDL_ceilf
  5163. * \sa SDL_truncf
  5164. */
  5165. extern SDL_DECLSPEC float SDLCALL SDL_roundf(float x);
  5166. /**
  5167. * Round `x` to the nearest integer representable as a long
  5168. *
  5169. * Rounds `x` to the nearest integer. Values halfway between integers will be
  5170. * rounded away from zero.
  5171. *
  5172. * Domain: `-INF <= x <= INF`
  5173. *
  5174. * Range: `MIN_LONG <= y <= MAX_LONG`
  5175. *
  5176. * This function operates on double-precision floating point values, use
  5177. * SDL_lround for single-precision floats. To get the result as a
  5178. * floating-point type, use SDL_round.
  5179. *
  5180. * \param x floating point value.
  5181. * \returns the nearest integer to `x`.
  5182. *
  5183. * \threadsafety It is safe to call this function from any thread.
  5184. *
  5185. * \since This function is available since SDL 3.1.3.
  5186. *
  5187. * \sa SDL_lroundf
  5188. * \sa SDL_round
  5189. * \sa SDL_floor
  5190. * \sa SDL_ceil
  5191. * \sa SDL_trunc
  5192. */
  5193. extern SDL_DECLSPEC long SDLCALL SDL_lround(double x);
  5194. /**
  5195. * Round `x` to the nearest integer representable as a long
  5196. *
  5197. * Rounds `x` to the nearest integer. Values halfway between integers will be
  5198. * rounded away from zero.
  5199. *
  5200. * Domain: `-INF <= x <= INF`
  5201. *
  5202. * Range: `MIN_LONG <= y <= MAX_LONG`
  5203. *
  5204. * This function operates on single-precision floating point values, use
  5205. * SDL_lroundf for double-precision floats. To get the result as a
  5206. * floating-point type, use SDL_roundf,
  5207. *
  5208. * \param x floating point value.
  5209. * \returns the nearest integer to `x`.
  5210. *
  5211. * \threadsafety It is safe to call this function from any thread.
  5212. *
  5213. * \since This function is available since SDL 3.1.3.
  5214. *
  5215. * \sa SDL_lround
  5216. * \sa SDL_roundf
  5217. * \sa SDL_floorf
  5218. * \sa SDL_ceilf
  5219. * \sa SDL_truncf
  5220. */
  5221. extern SDL_DECLSPEC long SDLCALL SDL_lroundf(float x);
  5222. /**
  5223. * Scale `x` by an integer power of two.
  5224. *
  5225. * Multiplies `x` by the `n`th power of the floating point radix (always 2).
  5226. *
  5227. * Domain: `-INF <= x <= INF`, `n` integer
  5228. *
  5229. * Range: `-INF <= y <= INF`
  5230. *
  5231. * This function operates on double-precision floating point values, use
  5232. * SDL_scalbnf for single-precision floats.
  5233. *
  5234. * \param x floating point value to be scaled.
  5235. * \param n integer exponent.
  5236. * \returns `x * 2^n`.
  5237. *
  5238. * \threadsafety It is safe to call this function from any thread.
  5239. *
  5240. * \since This function is available since SDL 3.1.3.
  5241. *
  5242. * \sa SDL_scalbnf
  5243. * \sa SDL_pow
  5244. */
  5245. extern SDL_DECLSPEC double SDLCALL SDL_scalbn(double x, int n);
  5246. /**
  5247. * Scale `x` by an integer power of two.
  5248. *
  5249. * Multiplies `x` by the `n`th power of the floating point radix (always 2).
  5250. *
  5251. * Domain: `-INF <= x <= INF`, `n` integer
  5252. *
  5253. * Range: `-INF <= y <= INF`
  5254. *
  5255. * This function operates on single-precision floating point values, use
  5256. * SDL_scalbn for double-precision floats.
  5257. *
  5258. * \param x floating point value to be scaled.
  5259. * \param n integer exponent.
  5260. * \returns `x * 2^n`.
  5261. *
  5262. * \threadsafety It is safe to call this function from any thread.
  5263. *
  5264. * \since This function is available since SDL 3.1.3.
  5265. *
  5266. * \sa SDL_scalbn
  5267. * \sa SDL_powf
  5268. */
  5269. extern SDL_DECLSPEC float SDLCALL SDL_scalbnf(float x, int n);
  5270. /**
  5271. * Compute the sine of `x`.
  5272. *
  5273. * Domain: `-INF <= x <= INF`
  5274. *
  5275. * Range: `-1 <= y <= 1`
  5276. *
  5277. * This function operates on double-precision floating point values, use
  5278. * SDL_sinf for single-precision floats.
  5279. *
  5280. * This function may use a different approximation across different versions,
  5281. * platforms and configurations. i.e, it can return a different value given
  5282. * the same input on different machines or operating systems, or if SDL is
  5283. * updated.
  5284. *
  5285. * \param x floating point value, in radians.
  5286. * \returns sine of `x`.
  5287. *
  5288. * \threadsafety It is safe to call this function from any thread.
  5289. *
  5290. * \since This function is available since SDL 3.1.3.
  5291. *
  5292. * \sa SDL_sinf
  5293. * \sa SDL_asin
  5294. * \sa SDL_cos
  5295. */
  5296. extern SDL_DECLSPEC double SDLCALL SDL_sin(double x);
  5297. /**
  5298. * Compute the sine of `x`.
  5299. *
  5300. * Domain: `-INF <= x <= INF`
  5301. *
  5302. * Range: `-1 <= y <= 1`
  5303. *
  5304. * This function operates on single-precision floating point values, use
  5305. * SDL_sinf for double-precision floats.
  5306. *
  5307. * This function may use a different approximation across different versions,
  5308. * platforms and configurations. i.e, it can return a different value given
  5309. * the same input on different machines or operating systems, or if SDL is
  5310. * updated.
  5311. *
  5312. * \param x floating point value, in radians.
  5313. * \returns sine of `x`.
  5314. *
  5315. * \threadsafety It is safe to call this function from any thread.
  5316. *
  5317. * \since This function is available since SDL 3.1.3.
  5318. *
  5319. * \sa SDL_sin
  5320. * \sa SDL_asinf
  5321. * \sa SDL_cosf
  5322. */
  5323. extern SDL_DECLSPEC float SDLCALL SDL_sinf(float x);
  5324. /**
  5325. * Compute the square root of `x`.
  5326. *
  5327. * Domain: `0 <= x <= INF`
  5328. *
  5329. * Range: `0 <= y <= INF`
  5330. *
  5331. * This function operates on double-precision floating point values, use
  5332. * SDL_sqrtf for single-precision floats.
  5333. *
  5334. * This function may use a different approximation across different versions,
  5335. * platforms and configurations. i.e, it can return a different value given
  5336. * the same input on different machines or operating systems, or if SDL is
  5337. * updated.
  5338. *
  5339. * \param x floating point value. Must be greater than or equal to 0.
  5340. * \returns square root of `x`.
  5341. *
  5342. * \threadsafety It is safe to call this function from any thread.
  5343. *
  5344. * \since This function is available since SDL 3.1.3.
  5345. *
  5346. * \sa SDL_sqrtf
  5347. */
  5348. extern SDL_DECLSPEC double SDLCALL SDL_sqrt(double x);
  5349. /**
  5350. * Compute the square root of `x`.
  5351. *
  5352. * Domain: `0 <= x <= INF`
  5353. *
  5354. * Range: `0 <= y <= INF`
  5355. *
  5356. * This function operates on single-precision floating point values, use
  5357. * SDL_sqrt for double-precision floats.
  5358. *
  5359. * This function may use a different approximation across different versions,
  5360. * platforms and configurations. i.e, it can return a different value given
  5361. * the same input on different machines or operating systems, or if SDL is
  5362. * updated.
  5363. *
  5364. * \param x floating point value. Must be greater than or equal to 0.
  5365. * \returns square root of `x`.
  5366. *
  5367. * \threadsafety It is safe to call this function from any thread.
  5368. *
  5369. * \since This function is available since SDL 3.1.3.
  5370. *
  5371. * \sa SDL_sqrt
  5372. */
  5373. extern SDL_DECLSPEC float SDLCALL SDL_sqrtf(float x);
  5374. /**
  5375. * Compute the tangent of `x`.
  5376. *
  5377. * Domain: `-INF <= x <= INF`
  5378. *
  5379. * Range: `-INF <= y <= INF`
  5380. *
  5381. * This function operates on double-precision floating point values, use
  5382. * SDL_tanf for single-precision floats.
  5383. *
  5384. * This function may use a different approximation across different versions,
  5385. * platforms and configurations. i.e, it can return a different value given
  5386. * the same input on different machines or operating systems, or if SDL is
  5387. * updated.
  5388. *
  5389. * \param x floating point value, in radians.
  5390. * \returns tangent of `x`.
  5391. *
  5392. * \threadsafety It is safe to call this function from any thread.
  5393. *
  5394. * \since This function is available since SDL 3.1.3.
  5395. *
  5396. * \sa SDL_tanf
  5397. * \sa SDL_sin
  5398. * \sa SDL_cos
  5399. * \sa SDL_atan
  5400. * \sa SDL_atan2
  5401. */
  5402. extern SDL_DECLSPEC double SDLCALL SDL_tan(double x);
  5403. /**
  5404. * Compute the tangent of `x`.
  5405. *
  5406. * Domain: `-INF <= x <= INF`
  5407. *
  5408. * Range: `-INF <= y <= INF`
  5409. *
  5410. * This function operates on single-precision floating point values, use
  5411. * SDL_tanf for double-precision floats.
  5412. *
  5413. * This function may use a different approximation across different versions,
  5414. * platforms and configurations. i.e, it can return a different value given
  5415. * the same input on different machines or operating systems, or if SDL is
  5416. * updated.
  5417. *
  5418. * \param x floating point value, in radians.
  5419. * \returns tangent of `x`.
  5420. *
  5421. * \threadsafety It is safe to call this function from any thread.
  5422. *
  5423. * \since This function is available since SDL 3.1.3.
  5424. *
  5425. * \sa SDL_tan
  5426. * \sa SDL_sinf
  5427. * \sa SDL_cosf
  5428. * \sa SDL_atanf
  5429. * \sa SDL_atan2f
  5430. */
  5431. extern SDL_DECLSPEC float SDLCALL SDL_tanf(float x);
  5432. /**
  5433. * An opaque handle representing string encoding conversion state.
  5434. *
  5435. * \since This datatype is available since SDL 3.1.3.
  5436. *
  5437. * \sa SDL_iconv_open
  5438. */
  5439. typedef struct SDL_iconv_data_t *SDL_iconv_t;
  5440. /**
  5441. * This function allocates a context for the specified character set
  5442. * conversion.
  5443. *
  5444. * \param tocode The target character encoding, must not be NULL.
  5445. * \param fromcode The source character encoding, must not be NULL.
  5446. * \returns a handle that must be freed with SDL_iconv_close, or
  5447. * SDL_ICONV_ERROR on failure.
  5448. *
  5449. * \since This function is available since SDL 3.1.3.
  5450. *
  5451. * \sa SDL_iconv
  5452. * \sa SDL_iconv_close
  5453. * \sa SDL_iconv_string
  5454. */
  5455. extern SDL_DECLSPEC SDL_iconv_t SDLCALL SDL_iconv_open(const char *tocode,
  5456. const char *fromcode);
  5457. /**
  5458. * This function frees a context used for character set conversion.
  5459. *
  5460. * \param cd The character set conversion handle.
  5461. * \returns 0 on success, or -1 on failure.
  5462. *
  5463. * \since This function is available since SDL 3.1.3.
  5464. *
  5465. * \sa SDL_iconv
  5466. * \sa SDL_iconv_open
  5467. * \sa SDL_iconv_string
  5468. */
  5469. extern SDL_DECLSPEC int SDLCALL SDL_iconv_close(SDL_iconv_t cd);
  5470. /**
  5471. * This function converts text between encodings, reading from and writing to
  5472. * a buffer.
  5473. *
  5474. * It returns the number of succesful conversions on success. On error,
  5475. * SDL_ICONV_E2BIG is returned when the output buffer is too small, or
  5476. * SDL_ICONV_EILSEQ is returned when an invalid input sequence is encountered,
  5477. * or SDL_ICONV_EINVAL is returned when an incomplete input sequence is
  5478. * encountered.
  5479. *
  5480. * On exit:
  5481. *
  5482. * - inbuf will point to the beginning of the next multibyte sequence. On
  5483. * error, this is the location of the problematic input sequence. On
  5484. * success, this is the end of the input sequence.
  5485. * - inbytesleft will be set to the number of bytes left to convert, which
  5486. * will be 0 on success.
  5487. * - outbuf will point to the location where to store the next output byte.
  5488. * - outbytesleft will be set to the number of bytes left in the output
  5489. * buffer.
  5490. *
  5491. * \param cd The character set conversion context, created in
  5492. * SDL_iconv_open().
  5493. * \param inbuf Address of variable that points to the first character of the
  5494. * input sequence.
  5495. * \param inbytesleft The number of bytes in the input buffer.
  5496. * \param outbuf Address of variable that points to the output buffer.
  5497. * \param outbytesleft The number of bytes in the output buffer.
  5498. * \returns the number of conversions on success, or a negative error code.
  5499. *
  5500. * \since This function is available since SDL 3.1.3.
  5501. *
  5502. * \sa SDL_iconv_open
  5503. * \sa SDL_iconv_close
  5504. * \sa SDL_iconv_string
  5505. */
  5506. extern SDL_DECLSPEC size_t SDLCALL SDL_iconv(SDL_iconv_t cd, const char **inbuf,
  5507. size_t *inbytesleft, char **outbuf,
  5508. size_t *outbytesleft);
  5509. #define SDL_ICONV_ERROR (size_t)-1 /**< Generic error. Check SDL_GetError()? */
  5510. #define SDL_ICONV_E2BIG (size_t)-2 /**< Output buffer was too small. */
  5511. #define SDL_ICONV_EILSEQ (size_t)-3 /**< Invalid input sequence was encountered. */
  5512. #define SDL_ICONV_EINVAL (size_t)-4 /**< Incomplete input sequence was encountered. */
  5513. /**
  5514. * Helper function to convert a string's encoding in one call.
  5515. *
  5516. * This function converts a buffer or string between encodings in one pass.
  5517. *
  5518. * The string does not need to be NULL-terminated; this function operates on
  5519. * the number of bytes specified in `inbytesleft` whether there is a NULL
  5520. * character anywhere in the buffer.
  5521. *
  5522. * The returned string is owned by the caller, and should be passed to
  5523. * SDL_free when no longer needed.
  5524. *
  5525. * \param tocode the character encoding of the output string. Examples are
  5526. * "UTF-8", "UCS-4", etc.
  5527. * \param fromcode the character encoding of data in `inbuf`.
  5528. * \param inbuf the string to convert to a different encoding.
  5529. * \param inbytesleft the size of the input string _in bytes_.
  5530. * \returns a new string, converted to the new encoding, or NULL on error.
  5531. *
  5532. * \since This function is available since SDL 3.1.3.
  5533. *
  5534. * \sa SDL_iconv_open
  5535. * \sa SDL_iconv_close
  5536. * \sa SDL_iconv
  5537. */
  5538. extern SDL_DECLSPEC char * SDLCALL SDL_iconv_string(const char *tocode,
  5539. const char *fromcode,
  5540. const char *inbuf,
  5541. size_t inbytesleft);
  5542. /* Some helper macros for common SDL_iconv_string cases... */
  5543. /**
  5544. * Convert a UTF-8 string to the current locale's character encoding.
  5545. *
  5546. * This is a helper macro that might be more clear than calling
  5547. * SDL_iconv_string directly. However, it double-evaluates its parameter, so
  5548. * do not use an expression with side-effects here.
  5549. *
  5550. * \param S the string to convert.
  5551. * \returns a new string, converted to the new encoding, or NULL on error.
  5552. *
  5553. * \since This macro is available since SDL 3.1.3.
  5554. */
  5555. #define SDL_iconv_utf8_locale(S) SDL_iconv_string("", "UTF-8", S, SDL_strlen(S)+1)
  5556. /**
  5557. * Convert a UTF-8 string to UCS-2.
  5558. *
  5559. * This is a helper macro that might be more clear than calling
  5560. * SDL_iconv_string directly. However, it double-evaluates its parameter, so
  5561. * do not use an expression with side-effects here.
  5562. *
  5563. * \param S the string to convert.
  5564. * \returns a new string, converted to the new encoding, or NULL on error.
  5565. *
  5566. * \since This macro is available since SDL 3.1.3.
  5567. */
  5568. #define SDL_iconv_utf8_ucs2(S) (Uint16 *)SDL_iconv_string("UCS-2", "UTF-8", S, SDL_strlen(S)+1)
  5569. /**
  5570. * Convert a UTF-8 string to UCS-4.
  5571. *
  5572. * This is a helper macro that might be more clear than calling
  5573. * SDL_iconv_string directly. However, it double-evaluates its parameter, so
  5574. * do not use an expression with side-effects here.
  5575. *
  5576. * \param S the string to convert.
  5577. * \returns a new string, converted to the new encoding, or NULL on error.
  5578. *
  5579. * \since This macro is available since SDL 3.1.3.
  5580. */
  5581. #define SDL_iconv_utf8_ucs4(S) (Uint32 *)SDL_iconv_string("UCS-4", "UTF-8", S, SDL_strlen(S)+1)
  5582. /**
  5583. * Convert a wchar_t string to UTF-8.
  5584. *
  5585. * This is a helper macro that might be more clear than calling
  5586. * SDL_iconv_string directly. However, it double-evaluates its parameter, so
  5587. * do not use an expression with side-effects here.
  5588. *
  5589. * \param S the string to convert.
  5590. * \returns a new string, converted to the new encoding, or NULL on error.
  5591. *
  5592. * \since This macro is available since SDL 3.1.3.
  5593. */
  5594. #define SDL_iconv_wchar_utf8(S) SDL_iconv_string("UTF-8", "WCHAR_T", (char *)S, (SDL_wcslen(S)+1)*sizeof(wchar_t))
  5595. /* force builds using Clang's static analysis tools to use literal C runtime
  5596. here, since there are possibly tests that are ineffective otherwise. */
  5597. #if defined(__clang_analyzer__) && !defined(SDL_DISABLE_ANALYZE_MACROS)
  5598. /* The analyzer knows about strlcpy even when the system doesn't provide it */
  5599. #if !defined(HAVE_STRLCPY) && !defined(strlcpy)
  5600. size_t strlcpy(char *dst, const char *src, size_t size);
  5601. #endif
  5602. /* The analyzer knows about strlcat even when the system doesn't provide it */
  5603. #if !defined(HAVE_STRLCAT) && !defined(strlcat)
  5604. size_t strlcat(char *dst, const char *src, size_t size);
  5605. #endif
  5606. #if !defined(HAVE_WCSLCPY) && !defined(wcslcpy)
  5607. size_t wcslcpy(wchar_t *dst, const wchar_t *src, size_t size);
  5608. #endif
  5609. #if !defined(HAVE_WCSLCAT) && !defined(wcslcat)
  5610. size_t wcslcat(wchar_t *dst, const wchar_t *src, size_t size);
  5611. #endif
  5612. /* strdup is not ANSI but POSIX, and its prototype might be hidden... */
  5613. char *strdup(const char *str);
  5614. /* Starting LLVM 16, the analyser errors out if these functions do not have
  5615. their prototype defined (clang-diagnostic-implicit-function-declaration) */
  5616. #include <stdio.h>
  5617. #include <stdlib.h>
  5618. #include <strings.h>
  5619. #define SDL_malloc malloc
  5620. #define SDL_calloc calloc
  5621. #define SDL_realloc realloc
  5622. #define SDL_free free
  5623. #ifndef SDL_memcpy
  5624. #define SDL_memcpy memcpy
  5625. #endif
  5626. #ifndef SDL_memmove
  5627. #define SDL_memmove memmove
  5628. #endif
  5629. #ifndef SDL_memset
  5630. #define SDL_memset memset
  5631. #endif
  5632. #define SDL_memcmp memcmp
  5633. #define SDL_strlcpy strlcpy
  5634. #define SDL_strlcat strlcat
  5635. #define SDL_strlen strlen
  5636. #define SDL_wcslen wcslen
  5637. #define SDL_wcslcpy wcslcpy
  5638. #define SDL_wcslcat wcslcat
  5639. #define SDL_strdup strdup
  5640. #define SDL_wcsdup wcsdup
  5641. #define SDL_strchr strchr
  5642. #define SDL_strrchr strrchr
  5643. #define SDL_strstr strstr
  5644. #define SDL_wcsstr wcsstr
  5645. #define SDL_strtok_r strtok_r
  5646. #define SDL_strcmp strcmp
  5647. #define SDL_wcscmp wcscmp
  5648. #define SDL_strncmp strncmp
  5649. #define SDL_wcsncmp wcsncmp
  5650. #define SDL_strcasecmp strcasecmp
  5651. #define SDL_strncasecmp strncasecmp
  5652. #define SDL_strpbrk strpbrk
  5653. #define SDL_sscanf sscanf
  5654. #define SDL_vsscanf vsscanf
  5655. #define SDL_snprintf snprintf
  5656. #define SDL_vsnprintf vsnprintf
  5657. #endif
  5658. /**
  5659. * Multiply two integers, checking for overflow.
  5660. *
  5661. * If `a * b` would overflow, return false.
  5662. *
  5663. * Otherwise store `a * b` via ret and return true.
  5664. *
  5665. * \param a the multiplicand.
  5666. * \param b the multiplier.
  5667. * \param ret on non-overflow output, stores the multiplication result, may
  5668. * not be NULL.
  5669. * \returns false on overflow, true if result is multiplied without overflow.
  5670. *
  5671. * \threadsafety It is safe to call this function from any thread.
  5672. *
  5673. * \since This function is available since SDL 3.1.3.
  5674. */
  5675. SDL_FORCE_INLINE bool SDL_size_mul_check_overflow(size_t a, size_t b, size_t *ret)
  5676. {
  5677. if (a != 0 && b > SDL_SIZE_MAX / a) {
  5678. return false;
  5679. }
  5680. *ret = a * b;
  5681. return true;
  5682. }
  5683. #ifndef SDL_WIKI_DOCUMENTATION_SECTION
  5684. #if SDL_HAS_BUILTIN(__builtin_mul_overflow)
  5685. /* This needs to be wrapped in an inline rather than being a direct #define,
  5686. * because __builtin_mul_overflow() is type-generic, but we want to be
  5687. * consistent about interpreting a and b as size_t. */
  5688. SDL_FORCE_INLINE bool SDL_size_mul_check_overflow_builtin(size_t a, size_t b, size_t *ret)
  5689. {
  5690. return (__builtin_mul_overflow(a, b, ret) == 0);
  5691. }
  5692. #define SDL_size_mul_check_overflow(a, b, ret) SDL_size_mul_check_overflow_builtin(a, b, ret)
  5693. #endif
  5694. #endif
  5695. /**
  5696. * Add two integers, checking for overflow.
  5697. *
  5698. * If `a + b` would overflow, return false.
  5699. *
  5700. * Otherwise store `a + b` via ret and return true.
  5701. *
  5702. * \param a the first addend.
  5703. * \param b the second addend.
  5704. * \param ret on non-overflow output, stores the addition result, may not be
  5705. * NULL.
  5706. * \returns false on overflow, true if result is added without overflow.
  5707. *
  5708. * \threadsafety It is safe to call this function from any thread.
  5709. *
  5710. * \since This function is available since SDL 3.1.3.
  5711. */
  5712. SDL_FORCE_INLINE bool SDL_size_add_check_overflow(size_t a, size_t b, size_t *ret)
  5713. {
  5714. if (b > SDL_SIZE_MAX - a) {
  5715. return false;
  5716. }
  5717. *ret = a + b;
  5718. return true;
  5719. }
  5720. #ifndef SDL_WIKI_DOCUMENTATION_SECTION
  5721. #if SDL_HAS_BUILTIN(__builtin_add_overflow)
  5722. /* This needs to be wrapped in an inline rather than being a direct #define,
  5723. * the same as the call to __builtin_mul_overflow() above. */
  5724. SDL_FORCE_INLINE bool SDL_size_add_check_overflow_builtin(size_t a, size_t b, size_t *ret)
  5725. {
  5726. return (__builtin_add_overflow(a, b, ret) == 0);
  5727. }
  5728. #define SDL_size_add_check_overflow(a, b, ret) SDL_size_add_check_overflow_builtin(a, b, ret)
  5729. #endif
  5730. #endif
  5731. /* This is a generic function pointer which should be cast to the type you expect */
  5732. #ifdef SDL_WIKI_DOCUMENTATION_SECTION
  5733. /**
  5734. * A generic function pointer.
  5735. *
  5736. * In theory, generic function pointers should use this, instead of `void *`,
  5737. * since some platforms could treat code addresses differently than data
  5738. * addresses. Although in current times no popular platforms make this
  5739. * distinction, it is more correct and portable to use the correct type for a
  5740. * generic pointer.
  5741. *
  5742. * If for some reason you need to force this typedef to be an actual `void *`,
  5743. * perhaps to work around a compiler or existing code, you can define
  5744. * `SDL_FUNCTION_POINTER_IS_VOID_POINTER` before including any SDL headers.
  5745. *
  5746. * \since This datatype is available since SDL 3.1.3.
  5747. */
  5748. typedef void (*SDL_FunctionPointer)(void);
  5749. #elif defined(SDL_FUNCTION_POINTER_IS_VOID_POINTER)
  5750. typedef void *SDL_FunctionPointer;
  5751. #else
  5752. typedef void (*SDL_FunctionPointer)(void);
  5753. #endif
  5754. /* Ends C function definitions when using C++ */
  5755. #ifdef __cplusplus
  5756. }
  5757. #endif
  5758. #include <SDL3/SDL_close_code.h>
  5759. #endif /* SDL_stdinc_h_ */