SDL_stdinc.h 200 KB

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