SDL_stdinc.h 201 KB

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