SDL_stdinc.h 197 KB

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