SDL_stdinc.h 202 KB

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