1
0

SDL_stdinc.h 199 KB

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