SDL_stdinc.h 201 KB

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