SDL_stdinc.h 197 KB

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