SDL_stdinc.h 200 KB

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