SDL_stdinc.h 197 KB

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