sdli386utils.pas 143 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236
  1. unit sdli386utils;
  2. {
  3. $Id: sdli386utils.pas,v 1.5 2004/06/02 19:38:53 savage Exp $
  4. }
  5. {******************************************************************************}
  6. { }
  7. { Borland Delphi SDL - Simple DirectMedia Layer }
  8. { SDL Utility functions }
  9. { }
  10. { }
  11. { The initial developer of this Pascal code was : }
  12. { Tom Jones <[email protected]> }
  13. { }
  14. { Portions created by Tom Jones are }
  15. { Copyright (C) 2000 - 2001 Tom Jones. }
  16. { }
  17. { }
  18. { Contributor(s) }
  19. { -------------- }
  20. { Dominique Louis <[email protected]> }
  21. { Róbert Kisnémeth <[email protected]> }
  22. { }
  23. { Obtained through: }
  24. { Joint Endeavour of Delphi Innovators ( Project JEDI ) }
  25. { }
  26. { You may retrieve the latest version of this file at the Project }
  27. { JEDI home page, located at http://delphi-jedi.org }
  28. { }
  29. { The contents of this file are used with permission, subject to }
  30. { the Mozilla Public License Version 1.1 (the "License"); you may }
  31. { not use this file except in compliance with the License. You may }
  32. { obtain a copy of the License at }
  33. { http://www.mozilla.org/MPL/MPL-1.1.html }
  34. { }
  35. { Software distributed under the License is distributed on an }
  36. { "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or }
  37. { implied. See the License for the specific language governing }
  38. { rights and limitations under the License. }
  39. { }
  40. { Description }
  41. { ----------- }
  42. { Helper functions... }
  43. { }
  44. { }
  45. { Requires }
  46. { -------- }
  47. { SDL.dll on Windows platforms }
  48. { libSDL-1.1.so.0 on Linux platform }
  49. { }
  50. { Programming Notes }
  51. { ----------------- }
  52. { }
  53. { }
  54. { }
  55. { }
  56. { Revision History }
  57. { ---------------- }
  58. { 2000 - TJ : Initial creation }
  59. { }
  60. { July 13 2001 - DL : Added PutPixel and GetPixel routines. }
  61. { }
  62. { Sept 14 2001 - RK : Added flipping routines. }
  63. { }
  64. { Sept 19 2001 - RK : Added PutPixel & line drawing & blitting with ADD }
  65. { effect. Fixed a bug in SDL_PutPixel & SDL_GetPixel }
  66. { Added PSDLRect() }
  67. { Sept 22 2001 - DL : Removed need for Windows.pas by defining types here}
  68. { Also removed by poor attempt or a dialog box }
  69. { }
  70. { Sept 25 2001 - RK : Added PixelTest, NewPutPixel, SubPixel, SubLine, }
  71. { SubSurface, MonoSurface & TexturedSurface }
  72. { }
  73. { Sept 26 2001 - DL : Made change so that it refers to native Pascal }
  74. { types rather that Windows types. This makes it more}
  75. { portable to Linix. }
  76. { }
  77. { Sept 27 2001 - RK : SDLUtils now can be compiled with FreePascal }
  78. { }
  79. { Oct 27 2001 - JF : Added ScrollY function }
  80. { }
  81. { Jan 21 2002 - RK : Added SDL_ZoomSurface and SDL_WarpSurface }
  82. { }
  83. { Mar 28 2002 - JF : Added SDL_RotateSurface }
  84. { }
  85. { May 13 2002 - RK : Improved SDL_FillRectAdd & SDL_FillRectSub }
  86. { }
  87. { May 27 2002 - YS : GradientFillRect function }
  88. { }
  89. { May 30 2002 - RK : Added SDL_2xBlit, SDL_Scanline2xBlit }
  90. { & SDL_50Scanline2xBlit }
  91. { }
  92. { June 12 2002 - RK : Added SDL_PixelTestSurfaceVsRect }
  93. { }
  94. { June 12 2002 - JF : Updated SDL_PixelTestSurfaceVsRect }
  95. { }
  96. { November 9 2002 - JF : Added Jason's boolean Surface functions }
  97. { }
  98. { December 10 2002 - DE : Added Dean's SDL_ClipLine function }
  99. { }
  100. {******************************************************************************}
  101. {
  102. $Log: sdli386utils.pas,v $
  103. Revision 1.5 2004/06/02 19:38:53 savage
  104. Changes to SDL_GradientFillRect as suggested by
  105. Ángel Eduardo García Hernández. Many thanks.
  106. Revision 1.4 2004/05/29 23:11:53 savage
  107. Changes to SDL_ScaleSurfaceRect as suggested by
  108. Ángel Eduardo García Hernández to fix a colour issue with the function. Many thanks.
  109. Revision 1.3 2004/02/20 22:04:11 savage
  110. Added Changes as mentioned by Rodrigo "Rui" R. (1/2 RRC2Soft) to facilitate FPC compilation and it also works in Delphi. Also syncronized the funcitons so that they are identical to sdlutils.pas, when no assembly version is available.
  111. Revision 1.2 2004/02/14 00:23:39 savage
  112. As UNIX is defined in jedi-sdl.inc this will be used to check linux compatability as well. Units have been changed to reflect this change.
  113. Revision 1.1 2004/02/05 00:08:20 savage
  114. Module 1.0 release
  115. }
  116. interface
  117. {$i jedi-sdl.inc}
  118. uses
  119. {$IFDEF UNIX}
  120. Types,
  121. Xlib,
  122. {$ENDIF}
  123. SysUtils,
  124. sdl;
  125. type
  126. TGradientStyle = ( gsHorizontal, gsVertical );
  127. // Pixel procedures
  128. function SDL_PixelTest( SrcSurface1 : PSDL_Surface; SrcRect1 : PSDL_Rect; SrcSurface2 :
  129. PSDL_Surface; SrcRect2 : PSDL_Rect; Left1, Top1, Left2, Top2 : integer ) : Boolean;
  130. function SDL_GetPixel( SrcSurface : PSDL_Surface; x : cardinal; y : cardinal ) : Uint32;
  131. procedure SDL_PutPixel( SrcSurface : PSDL_Surface; x : integer; y : integer; Color :
  132. cardinal );
  133. procedure SDL_AddPixel( SrcSurface : PSDL_Surface; x : integer; y : integer; Color :
  134. cardinal );
  135. procedure SDL_SubPixel( SrcSurface : PSDL_Surface; x : integer; y : integer; Color :
  136. cardinal );
  137. // Line procedures
  138. procedure SDL_DrawLine( DstSurface : PSDL_Surface; x1, y1, x2, y2 : integer; Color :
  139. cardinal );overload;
  140. procedure SDL_DrawLine( DstSurface : PSDL_Surface; x1, y1, x2, y2 : integer; Color :
  141. cardinal ; DashLength, DashSpace : byte ); overload;
  142. procedure SDL_AddLine( DstSurface : PSDL_Surface; x1, y1, x2, y2 : integer; Color :
  143. cardinal );
  144. procedure SDL_SubLine( DstSurface : PSDL_Surface; x1, y1, x2, y2 : integer; Color :
  145. cardinal );
  146. // Surface procedures
  147. procedure SDL_AddSurface( SrcSurface : PSDL_Surface; SrcRect : PSDL_Rect;
  148. DstSurface : PSDL_Surface; DestRect : PSDL_Rect );
  149. procedure SDL_SubSurface( SrcSurface : PSDL_Surface; SrcRect : PSDL_Rect;
  150. DstSurface : PSDL_Surface; DestRect : PSDL_Rect );
  151. procedure SDL_MonoSurface( SrcSurface : PSDL_Surface; SrcRect : PSDL_Rect;
  152. DstSurface : PSDL_Surface; DestRect : PSDL_Rect; Color : cardinal );
  153. procedure SDL_TexturedSurface( SrcSurface : PSDL_Surface; SrcRect : PSDL_Rect;
  154. DstSurface : PSDL_Surface; DestRect : PSDL_Rect; Texture : PSDL_Surface;
  155. TextureRect : PSDL_Rect );
  156. procedure SDL_ZoomSurface( SrcSurface : PSDL_Surface; SrcRect : PSDL_Rect; DstSurface : PSDL_Surface; DstRect : PSDL_Rect );
  157. procedure SDL_WarpSurface( SrcSurface : PSDL_Surface; SrcRect : PSDL_Rect; DstSurface : PSDL_Surface; UL, UR, LR, LL : PPoint );
  158. // Flip procedures
  159. procedure SDL_FlipRectH( DstSurface : PSDL_Surface; Rect : PSDL_Rect );
  160. procedure SDL_FlipRectV( DstSurface : PSDL_Surface; Rect : PSDL_Rect );
  161. function PSDLRect( aLeft, aTop, aWidth, aHeight : integer ) : PSDL_Rect;
  162. function SDLRect( aLeft, aTop, aWidth, aHeight : integer ) : TSDL_Rect; overload;
  163. function SDLRect( aRect : TRect ) : TSDL_Rect; overload;
  164. function SDL_ScaleSurfaceRect( SrcSurface : PSDL_Surface; SrcX1, SrcY1, SrcW, SrcH,
  165. Width, Height : integer ) : PSDL_Surface;
  166. procedure SDL_ScrollY( DstSurface : PSDL_Surface; DifY : integer );
  167. procedure SDL_ScrollX( DstSurface : PSDL_Surface; DifX : integer );
  168. procedure SDL_RotateDeg( DstSurface, SrcSurface : PSDL_Surface; SrcRect :
  169. PSDL_Rect; DestX, DestY, OffsetX, OffsetY : Integer; Angle : Integer );
  170. procedure SDL_RotateRad( DstSurface, SrcSurface : PSDL_Surface; SrcRect :
  171. PSDL_Rect; DestX, DestY, OffsetX, OffsetY : Integer; Angle : Single );
  172. function ValidateSurfaceRect( DstSurface : PSDL_Surface; dstrect : PSDL_Rect ) : TSDL_Rect;
  173. // Fill Rect routine
  174. procedure SDL_FillRectAdd( DstSurface : PSDL_Surface; dstrect : PSDL_Rect; color : UInt32 );
  175. procedure SDL_FillRectSub( DstSurface : PSDL_Surface; dstrect : PSDL_Rect; color : UInt32 );
  176. procedure SDL_GradientFillRect( DstSurface : PSDL_Surface; const Rect : PSDL_Rect; const StartColor, EndColor : TSDL_Color; const Style : TGradientStyle );
  177. // NOTE for All SDL_2xblit... function : the dest surface must be 2x of the source surface!
  178. procedure SDL_2xBlit( Src, Dest : PSDL_Surface );
  179. procedure SDL_Scanline2xBlit( Src, Dest : PSDL_Surface );
  180. procedure SDL_50Scanline2xBlit( Src, Dest : PSDL_Surface );
  181. function SDL_PixelTestSurfaceVsRect( SrcSurface1 : PSDL_Surface; SrcRect1 :
  182. PSDL_Rect; SrcRect2 : PSDL_Rect; Left1, Top1, Left2, Top2 : integer ) :
  183. boolean;
  184. // Jason's boolean Surface functions
  185. procedure SDL_ORSurface( SrcSurface : PSDL_Surface; SrcRect : PSDL_Rect;
  186. DestSurface : PSDL_Surface; DestRect : PSDL_Rect );
  187. procedure SDL_ANDSurface( SrcSurface : PSDL_Surface; SrcRect : PSDL_Rect;
  188. DestSurface : PSDL_Surface; DestRect : PSDL_Rect );
  189. procedure SDL_GTSurface( SrcSurface : PSDL_Surface; SrcRect : PSDL_Rect;
  190. DestSurface : PSDL_Surface; DestRect : PSDL_Rect );
  191. procedure SDL_LTSurface( SrcSurface : PSDL_Surface; SrcRect : PSDL_Rect;
  192. DestSurface : PSDL_Surface; DestRect : PSDL_Rect );
  193. function SDL_ClipLine(var x1,y1,x2,y2: Integer; ClipRect: PSDL_Rect) : boolean;
  194. implementation
  195. uses
  196. Math;
  197. function SDL_PixelTest( SrcSurface1 : PSDL_Surface; SrcRect1 : PSDL_Rect; SrcSurface2 :
  198. PSDL_Surface; SrcRect2 : PSDL_Rect; Left1, Top1, Left2, Top2 : integer ) : boolean;
  199. var
  200. Src_Rect1, Src_Rect2 : TSDL_Rect;
  201. right1, bottom1 : integer;
  202. right2, bottom2 : integer;
  203. Scan1Start, Scan2Start, ScanWidth, ScanHeight : cardinal;
  204. Mod1, Mod2 : cardinal;
  205. Addr1, Addr2 : cardinal;
  206. BPP : cardinal;
  207. Pitch1, Pitch2 : cardinal;
  208. TransparentColor1, TransparentColor2 : cardinal;
  209. tx, ty : cardinal;
  210. StartTick : cardinal;
  211. Color1, Color2 : cardinal;
  212. begin
  213. Result := false;
  214. if SrcRect1 = nil then
  215. begin
  216. with Src_Rect1 do
  217. begin
  218. x := 0;
  219. y := 0;
  220. w := SrcSurface1.w;
  221. h := SrcSurface1.h;
  222. end;
  223. end
  224. else
  225. Src_Rect1 := SrcRect1^;
  226. if SrcRect2 = nil then
  227. begin
  228. with Src_Rect2 do
  229. begin
  230. x := 0;
  231. y := 0;
  232. w := SrcSurface2.w;
  233. h := SrcSurface2.h;
  234. end;
  235. end
  236. else
  237. Src_Rect2 := SrcRect2^;
  238. with Src_Rect1 do
  239. begin
  240. Right1 := Left1 + w;
  241. Bottom1 := Top1 + h;
  242. end;
  243. with Src_Rect2 do
  244. begin
  245. Right2 := Left2 + w;
  246. Bottom2 := Top2 + h;
  247. end;
  248. if ( Left1 >= Right2 ) or ( Right1 <= Left2 ) or ( Top1 >= Bottom2 ) or ( Bottom1 <=
  249. Top2 ) then
  250. exit;
  251. if Left1 <= Left2 then
  252. begin
  253. // 1. left, 2. right
  254. Scan1Start := Src_Rect1.x + Left2 - Left1;
  255. Scan2Start := Src_Rect2.x;
  256. ScanWidth := Right1 - Left2;
  257. with Src_Rect2 do
  258. if ScanWidth > w then
  259. ScanWidth := w;
  260. end
  261. else
  262. begin
  263. // 1. right, 2. left
  264. Scan1Start := Src_Rect1.x;
  265. Scan2Start := Src_Rect2.x + Left1 - Left2;
  266. ScanWidth := Right2 - Left1;
  267. with Src_Rect1 do
  268. if ScanWidth > w then
  269. ScanWidth := w;
  270. end;
  271. with SrcSurface1^ do
  272. begin
  273. Pitch1 := Pitch;
  274. Addr1 := cardinal( Pixels );
  275. inc( Addr1, Pitch1 * UInt32( Src_Rect1.y ) );
  276. with format^ do
  277. begin
  278. BPP := BytesPerPixel;
  279. TransparentColor1 := colorkey;
  280. end;
  281. end;
  282. with SrcSurface2^ do
  283. begin
  284. TransparentColor2 := format.colorkey;
  285. Pitch2 := Pitch;
  286. Addr2 := cardinal( Pixels );
  287. inc( Addr2, Pitch2 * UInt32( Src_Rect2.y ) );
  288. end;
  289. Mod1 := Pitch1 - ( ScanWidth * BPP );
  290. Mod2 := Pitch2 - ( ScanWidth * BPP );
  291. inc( Addr1, BPP * Scan1Start );
  292. inc( Addr2, BPP * Scan2Start );
  293. if Top1 <= Top2 then
  294. begin
  295. // 1. up, 2. down
  296. ScanHeight := Bottom1 - Top2;
  297. if ScanHeight > Src_Rect2.h then
  298. ScanHeight := Src_Rect2.h;
  299. inc( Addr1, Pitch1 * UInt32( Top2 - Top1 ) );
  300. end
  301. else
  302. begin
  303. // 1. down, 2. up
  304. ScanHeight := Bottom2 - Top1;
  305. if ScanHeight > Src_Rect1.h then
  306. ScanHeight := Src_Rect1.h;
  307. inc( Addr2, Pitch2 * UInt32( Top1 - Top2 ) );
  308. end;
  309. case BPP of
  310. 1 :
  311. for ty := 1 to ScanHeight do
  312. begin
  313. for tx := 1 to ScanWidth do
  314. begin
  315. if ( PByte( Addr1 )^ <> TransparentColor1 ) and ( PByte( Addr2 )^ <>
  316. TransparentColor2 ) then
  317. begin
  318. Result := true;
  319. exit;
  320. end;
  321. inc( Addr1 );
  322. inc( Addr2 );
  323. end;
  324. inc( Addr1, Mod1 );
  325. inc( Addr2, Mod2 );
  326. end;
  327. 2 :
  328. for ty := 1 to ScanHeight do
  329. begin
  330. for tx := 1 to ScanWidth do
  331. begin
  332. if ( PWord( Addr1 )^ <> TransparentColor1 ) and ( PWord( Addr2 )^ <>
  333. TransparentColor2 ) then
  334. begin
  335. Result := true;
  336. exit;
  337. end;
  338. inc( Addr1, 2 );
  339. inc( Addr2, 2 );
  340. end;
  341. inc( Addr1, Mod1 );
  342. inc( Addr2, Mod2 );
  343. end;
  344. 3 :
  345. for ty := 1 to ScanHeight do
  346. begin
  347. for tx := 1 to ScanWidth do
  348. begin
  349. Color1 := PLongWord( Addr1 )^ and $00FFFFFF;
  350. Color2 := PLongWord( Addr2 )^ and $00FFFFFF;
  351. if ( Color1 <> TransparentColor1 ) and ( Color2 <> TransparentColor2 )
  352. then
  353. begin
  354. Result := true;
  355. exit;
  356. end;
  357. inc( Addr1, 3 );
  358. inc( Addr2, 3 );
  359. end;
  360. inc( Addr1, Mod1 );
  361. inc( Addr2, Mod2 );
  362. end;
  363. 4 :
  364. for ty := 1 to ScanHeight do
  365. begin
  366. for tx := 1 to ScanWidth do
  367. begin
  368. if ( PLongWord( Addr1 )^ <> TransparentColor1 ) and ( PLongWord( Addr2 )^ <>
  369. TransparentColor2 ) then
  370. begin
  371. Result := true;
  372. exit;
  373. end;
  374. inc( Addr1, 4 );
  375. inc( Addr2, 4 );
  376. end;
  377. inc( Addr1, Mod1 );
  378. inc( Addr2, Mod2 );
  379. end;
  380. end;
  381. end;
  382. function SDL_GetPixel( SrcSurface : PSDL_Surface; x : cardinal; y : cardinal ) : Uint32;
  383. var
  384. bpp : UInt32;
  385. p : PInteger;
  386. begin
  387. bpp := SrcSurface.format.BytesPerPixel;
  388. // Here p is the address to the pixel we want to retrieve
  389. p := Pointer( Uint32( SrcSurface.pixels ) + UInt32( y ) * SrcSurface.pitch + UInt32( x ) *
  390. bpp );
  391. case bpp of
  392. 1 : result := PUint8( p )^;
  393. 2 : result := PUint16( p )^;
  394. 3 :
  395. if ( SDL_BYTEORDER = SDL_BIG_ENDIAN ) then
  396. result := PUInt8Array( p )[ 0 ] shl 16 or PUInt8Array( p )[ 1 ] shl 8 or
  397. PUInt8Array( p )[ 2 ]
  398. else
  399. result := PUInt8Array( p )[ 0 ] or PUInt8Array( p )[ 1 ] shl 8 or
  400. PUInt8Array( p )[ 2 ] shl 16;
  401. 4 : result := PUint32( p )^;
  402. else
  403. result := 0; // shouldn't happen, but avoids warnings
  404. end;
  405. end;
  406. procedure SDL_PutPixel( SrcSurface : PSDL_Surface; x : integer; y : integer; Color :
  407. cardinal );
  408. var
  409. Addr, Pitch, BPP : cardinal;
  410. begin
  411. Addr := cardinal( SrcSurface.Pixels );
  412. Pitch := SrcSurface.Pitch;
  413. BPP := SrcSurface.format.BytesPerPixel;
  414. asm
  415. mov eax, y
  416. mul Pitch // EAX := y * Pitch
  417. add Addr, eax // Addr:= Addr + (y * Pitch)
  418. mov eax, x
  419. mov ecx, Color
  420. cmp BPP, 1
  421. jne @Not1BPP
  422. add eax, Addr // Now: EAX:= Addr + (y * Pitch) + x
  423. mov [eax], cl
  424. jmp @Quit
  425. @Not1BPP:
  426. cmp BPP, 2
  427. jne @Not2BPP
  428. mul BPP // EAX := x * BPP
  429. add eax, Addr // Now: EAX:= Addr + (y * Pitch) + x * BPP
  430. mov [eax], cx
  431. jmp @Quit
  432. @Not2BPP:
  433. cmp BPP, 3
  434. jne @Not3BPP
  435. mul BPP // EAX := x * BPP
  436. add eax, Addr // Now: EAX:= Addr + (y * Pitch) + x * BPP
  437. mov edx, [eax]
  438. and edx, $ff000000
  439. or edx, ecx
  440. mov [eax], edx
  441. jmp @Quit
  442. @Not3BPP:
  443. mul BPP // EAX := x * BPP
  444. add eax, Addr // Now: EAX:= Addr + (y * Pitch) + x * BPP
  445. mov [eax], ecx
  446. @Quit:
  447. end;
  448. end;
  449. procedure SDL_AddPixel( SrcSurface : PSDL_Surface; x : integer; y : integer; Color :
  450. cardinal );
  451. var
  452. SrcColor, FinalColor : cardinal;
  453. Addr, Pitch, Bits : cardinal;
  454. begin
  455. if Color = 0 then
  456. exit;
  457. Addr := cardinal( SrcSurface.Pixels );
  458. Pitch := SrcSurface.Pitch;
  459. Bits := SrcSurface.format.BitsPerPixel;
  460. asm
  461. mov eax, y
  462. mul Pitch // EAX := y * Pitch
  463. add Addr, eax // Addr:= Addr + (y * Pitch)
  464. mov eax, x
  465. cmp Bits, 8
  466. jne @Not8bit
  467. add eax, Addr // Now: EAX:= Addr + (y * Pitch) + x
  468. mov cl, [eax]
  469. movzx ecx, cl
  470. mov SrcColor, ecx
  471. mov edx, Color
  472. and ecx, 3
  473. and edx, 3
  474. add ecx, edx
  475. cmp ecx, 3
  476. jbe @Skip1_8bit
  477. mov ecx, 3
  478. @Skip1_8bit:
  479. mov FinalColor, ecx
  480. mov ecx, SrcColor
  481. mov edx, Color
  482. and ecx, $1c
  483. and edx, $1c
  484. add ecx, edx
  485. cmp ecx, $1c
  486. jbe @Skip2_8bit
  487. mov ecx, $1c
  488. @Skip2_8bit:
  489. or FinalColor, ecx
  490. mov ecx, SrcColor
  491. mov edx, Color
  492. and ecx, $e0
  493. and edx, $e0
  494. add ecx, edx
  495. cmp ecx, $e0
  496. jbe @Skip3_8bit
  497. mov ecx, $e0
  498. @Skip3_8bit:
  499. or ecx, FinalColor
  500. mov [eax], cl
  501. jmp @Quit
  502. @Not8bit:
  503. cmp Bits, 15
  504. jne @Not15bit
  505. shl eax, 1
  506. add eax, Addr // Now: EAX:= Addr + (y * Pitch) + x * 2
  507. mov ecx, [eax]
  508. and ecx, $00007fff
  509. mov SrcColor, ecx
  510. mov edx, Color
  511. and ecx, $1f
  512. and edx, $1f
  513. add ecx, edx
  514. cmp ecx, $1f
  515. jbe @Skip1_15bit
  516. mov ecx, $1f
  517. @Skip1_15bit:
  518. mov FinalColor, ecx
  519. mov ecx, SrcColor
  520. mov edx, Color
  521. and ecx, $03e0
  522. and edx, $03e0
  523. add ecx, edx
  524. cmp ecx, $03e0
  525. jbe @Skip2_15bit
  526. mov ecx, $03e0
  527. @Skip2_15bit:
  528. or FinalColor, ecx
  529. mov ecx, SrcColor
  530. mov edx, Color
  531. and ecx, $7c00
  532. and edx, $7c00
  533. add ecx, edx
  534. cmp ecx, $7c00
  535. jbe @Skip3_15bit
  536. mov ecx, $7c00
  537. @Skip3_15bit:
  538. or ecx, FinalColor
  539. mov [eax], cx
  540. jmp @Quit
  541. @Not15Bit:
  542. cmp Bits, 16
  543. jne @Not16bit
  544. shl eax, 1
  545. add eax, Addr // Now: EAX:= Addr + (y * Pitch) + x * 2
  546. mov ecx, [eax]
  547. and ecx, $0000ffff
  548. mov SrcColor, ecx
  549. mov edx, Color
  550. and ecx, $1f
  551. and edx, $1f
  552. add ecx, edx
  553. cmp ecx, $1f
  554. jbe @Skip1_16bit
  555. mov ecx, $1f
  556. @Skip1_16bit:
  557. mov FinalColor, ecx
  558. mov ecx, SrcColor
  559. mov edx, Color
  560. and ecx, $07e0
  561. and edx, $07e0
  562. add ecx, edx
  563. cmp ecx, $07e0
  564. jbe @Skip2_16bit
  565. mov ecx, $07e0
  566. @Skip2_16bit:
  567. or FinalColor, ecx
  568. mov ecx, SrcColor
  569. mov edx, Color
  570. and ecx, $f800
  571. and edx, $f800
  572. add ecx, edx
  573. cmp ecx, $f800
  574. jbe @Skip3_16bit
  575. mov ecx, $f800
  576. @Skip3_16bit:
  577. or ecx, FinalColor
  578. mov [eax], cx
  579. jmp @Quit
  580. @Not16Bit:
  581. cmp Bits, 24
  582. jne @Not24bit
  583. mov ecx, 0
  584. add ecx, eax
  585. shl ecx, 1
  586. add ecx, eax
  587. mov eax, ecx
  588. jmp @32bit
  589. @Not24bit:
  590. shl eax, 2
  591. @32bit:
  592. add eax, Addr // Now: EAX:= Addr + (y * Pitch) + x * 2
  593. mov ecx, [eax]
  594. mov FinalColor, ecx
  595. and FinalColor, $ff000000
  596. and ecx, $00ffffff
  597. mov SrcColor, ecx
  598. mov edx, Color
  599. and ecx, $000000ff
  600. and edx, $000000ff
  601. add ecx, edx
  602. cmp ecx, $000000ff
  603. jbe @Skip1_32bit
  604. mov ecx, $000000ff
  605. @Skip1_32bit:
  606. or FinalColor, ecx
  607. mov ecx, SrcColor
  608. mov edx, Color
  609. and ecx, $0000ff00
  610. and edx, $0000ff00
  611. add ecx, edx
  612. cmp ecx, $0000ff00
  613. jbe @Skip2_32bit
  614. mov ecx, $0000ff00
  615. @Skip2_32bit:
  616. or FinalColor, ecx
  617. mov ecx, SrcColor
  618. mov edx, Color
  619. and ecx, $00ff0000
  620. and edx, $00ff0000
  621. add ecx, edx
  622. cmp ecx, $00ff0000
  623. jbe @Skip3_32bit
  624. mov ecx, $00ff0000
  625. @Skip3_32bit:
  626. or ecx, FinalColor
  627. mov [eax], ecx
  628. @Quit:
  629. end;
  630. end;
  631. procedure SDL_SubPixel( SrcSurface : PSDL_Surface; x : integer; y : integer; Color :
  632. cardinal );
  633. var
  634. SrcColor, FinalColor : cardinal;
  635. Addr, Pitch, Bits : cardinal;
  636. begin
  637. if Color = 0 then
  638. exit;
  639. Addr := cardinal( SrcSurface.Pixels );
  640. Pitch := SrcSurface.Pitch;
  641. Bits := SrcSurface.format.BitsPerPixel;
  642. asm
  643. mov eax, y
  644. mul Pitch // EAX := y * Pitch
  645. add Addr, eax // Addr:= Addr + (y * Pitch)
  646. mov eax, x
  647. cmp Bits, 8
  648. jne @Not8bit
  649. add eax, Addr // Now: EAX:= Addr + (y * Pitch) + x
  650. mov cl, [eax]
  651. movzx ecx, cl
  652. mov SrcColor, ecx
  653. mov edx, Color
  654. and ecx, 3
  655. and edx, 3
  656. sub ecx, edx
  657. jns @Skip1_8bit
  658. mov ecx, 0
  659. @Skip1_8bit:
  660. mov FinalColor, ecx
  661. mov ecx, SrcColor
  662. mov edx, Color
  663. and ecx, $1c
  664. and edx, $1c
  665. sub ecx, edx
  666. jns @Skip2_8bit
  667. mov ecx, 0
  668. @Skip2_8bit:
  669. or FinalColor, ecx
  670. mov ecx, SrcColor
  671. mov edx, Color
  672. and ecx, $e0
  673. and edx, $e0
  674. sub ecx, edx
  675. jns @Skip3_8bit
  676. mov ecx, 0
  677. @Skip3_8bit:
  678. or ecx, FinalColor
  679. mov [eax], cl
  680. jmp @Quit
  681. @Not8bit:
  682. cmp Bits, 15
  683. jne @Not15bit
  684. shl eax, 1
  685. add eax, Addr
  686. mov ecx, [eax]
  687. and ecx, $00007fff
  688. mov SrcColor, ecx
  689. mov edx, Color
  690. and ecx, $1f
  691. and edx, $1f
  692. sub ecx, edx
  693. jns @Skip1_15bit
  694. mov ecx, 0
  695. @Skip1_15bit:
  696. mov FinalColor, ecx
  697. mov ecx, SrcColor
  698. mov edx, Color
  699. and ecx, $03e0
  700. and edx, $03e0
  701. sub ecx, edx
  702. jns @Skip2_15bit
  703. mov ecx, 0
  704. @Skip2_15bit:
  705. or FinalColor, ecx
  706. mov ecx, SrcColor
  707. mov edx, Color
  708. and ecx, $7c00
  709. and edx, $7c00
  710. sub ecx, edx
  711. jns @Skip3_15bit
  712. mov ecx, 0
  713. @Skip3_15bit:
  714. or ecx, FinalColor
  715. mov [eax], cx
  716. jmp @Quit
  717. @Not15Bit:
  718. cmp Bits, 16
  719. jne @Not16bit
  720. shl eax, 1
  721. add eax, Addr
  722. mov ecx, [eax]
  723. and ecx, $0000ffff
  724. mov SrcColor, ecx
  725. mov edx, Color
  726. and ecx, $1f
  727. and edx, $1f
  728. sub ecx, edx
  729. jns @Skip1_16bit
  730. mov ecx, 0
  731. @Skip1_16bit:
  732. mov FinalColor, ecx
  733. mov ecx, SrcColor
  734. mov edx, Color
  735. and ecx, $07e0
  736. and edx, $07e0
  737. sub ecx, edx
  738. jns @Skip2_16bit
  739. mov ecx, 0
  740. @Skip2_16bit:
  741. or FinalColor, ecx
  742. mov ecx, SrcColor
  743. mov edx, Color
  744. and ecx, $f800
  745. and edx, $f800
  746. sub ecx, edx
  747. jns @Skip3_16bit
  748. mov ecx, 0
  749. @Skip3_16bit:
  750. or ecx, FinalColor
  751. mov [eax], cx
  752. jmp @Quit
  753. @Not16Bit:
  754. cmp Bits, 24
  755. jne @Not24bit
  756. mov ecx, 0
  757. add ecx, eax
  758. shl ecx, 1
  759. add ecx, eax
  760. mov eax, ecx
  761. jmp @32bit
  762. @Not24bit:
  763. shl eax, 2
  764. @32bit:
  765. add eax, Addr
  766. mov ecx, [eax]
  767. mov FinalColor, ecx
  768. and FinalColor, $ff000000
  769. and ecx, $00ffffff
  770. mov SrcColor, ecx
  771. mov edx, Color
  772. and ecx, $000000ff
  773. and edx, $000000ff
  774. sub ecx, edx
  775. jns @Skip1_32bit
  776. mov ecx, 0
  777. @Skip1_32bit:
  778. or FinalColor, ecx
  779. mov ecx, SrcColor
  780. mov edx, Color
  781. and ecx, $0000ff00
  782. and edx, $0000ff00
  783. sub ecx, edx
  784. jns @Skip2_32bit
  785. mov ecx, 0
  786. @Skip2_32bit:
  787. or FinalColor, ecx
  788. mov ecx, SrcColor
  789. mov edx, Color
  790. and ecx, $00ff0000
  791. and edx, $00ff0000
  792. sub ecx, edx
  793. jns @Skip3_32bit
  794. mov ecx, 0
  795. @Skip3_32bit:
  796. or ecx, FinalColor
  797. mov [eax], ecx
  798. @Quit:
  799. end;
  800. end;
  801. // Draw a line between x1,y1 and x2,y2 to the given surface
  802. // NOTE: The surface must be locked before calling this!
  803. procedure SDL_DrawLine( DstSurface : PSDL_Surface; x1, y1, x2, y2 : integer; Color :
  804. cardinal );
  805. var
  806. dx, dy, sdx, sdy, x, y, px, py : integer;
  807. begin
  808. dx := x2 - x1;
  809. dy := y2 - y1;
  810. if dx < 0 then
  811. sdx := -1
  812. else
  813. sdx := 1;
  814. if dy < 0 then
  815. sdy := -1
  816. else
  817. sdy := 1;
  818. dx := sdx * dx + 1;
  819. dy := sdy * dy + 1;
  820. x := 0;
  821. y := 0;
  822. px := x1;
  823. py := y1;
  824. if dx >= dy then
  825. begin
  826. for x := 0 to dx - 1 do
  827. begin
  828. SDL_PutPixel( DstSurface, px, py, Color );
  829. y := y + dy;
  830. if y >= dx then
  831. begin
  832. y := y - dx;
  833. py := py + sdy;
  834. end;
  835. px := px + sdx;
  836. end;
  837. end
  838. else
  839. begin
  840. for y := 0 to dy - 1 do
  841. begin
  842. SDL_PutPixel( DstSurface, px, py, Color );
  843. x := x + dx;
  844. if x >= dy then
  845. begin
  846. x := x - dy;
  847. px := px + sdx;
  848. end;
  849. py := py + sdy;
  850. end;
  851. end;
  852. end;
  853. // Draw a dashed line between x1,y1 and x2,y2 to the given surface
  854. // NOTE: The surface must be locked before calling this!
  855. procedure SDL_DrawLine( DstSurface : PSDL_Surface; x1, y1, x2, y2 : integer; Color :
  856. cardinal ; DashLength, DashSpace : byte ); overload;
  857. var
  858. dx, dy, sdx, sdy, x, y, px, py, counter : integer; drawdash : boolean;
  859. begin
  860. counter := 0;
  861. drawdash := true; //begin line drawing with dash
  862. //Avoid invalid user-passed dash parameters
  863. if (DashLength < 1)
  864. then DashLength := 1;
  865. if (DashSpace < 1)
  866. then DashSpace := 0;
  867. dx := x2 - x1;
  868. dy := y2 - y1;
  869. if dx < 0 then
  870. sdx := -1
  871. else
  872. sdx := 1;
  873. if dy < 0 then
  874. sdy := -1
  875. else
  876. sdy := 1;
  877. dx := sdx * dx + 1;
  878. dy := sdy * dy + 1;
  879. x := 0;
  880. y := 0;
  881. px := x1;
  882. py := y1;
  883. if dx >= dy then
  884. begin
  885. for x := 0 to dx - 1 do
  886. begin
  887. //Alternate drawing dashes, or leaving spaces
  888. if drawdash then
  889. begin
  890. SDL_PutPixel( DstSurface, px, py, Color );
  891. inc(counter);
  892. if (counter > DashLength-1) and (DashSpace > 0) then
  893. begin
  894. drawdash := false;
  895. counter := 0;
  896. end;
  897. end
  898. else //space
  899. begin
  900. inc(counter);
  901. if counter > DashSpace-1 then
  902. begin
  903. drawdash := true;
  904. counter := 0;
  905. end;
  906. end;
  907. y := y + dy;
  908. if y >= dx then
  909. begin
  910. y := y - dx;
  911. py := py + sdy;
  912. end;
  913. px := px + sdx;
  914. end;
  915. end
  916. else
  917. begin
  918. for y := 0 to dy - 1 do
  919. begin
  920. //Alternate drawing dashes, or leaving spaces
  921. if drawdash then
  922. begin
  923. SDL_PutPixel( DstSurface, px, py, Color );
  924. inc(counter);
  925. if (counter > DashLength-1) and (DashSpace > 0) then
  926. begin
  927. drawdash := false;
  928. counter := 0;
  929. end;
  930. end
  931. else //space
  932. begin
  933. inc(counter);
  934. if counter > DashSpace-1 then
  935. begin
  936. drawdash := true;
  937. counter := 0;
  938. end;
  939. end;
  940. x := x + dx;
  941. if x >= dy then
  942. begin
  943. x := x - dy;
  944. px := px + sdx;
  945. end;
  946. py := py + sdy;
  947. end;
  948. end;
  949. end;
  950. procedure SDL_AddLine( DstSurface : PSDL_Surface; x1, y1, x2, y2 : integer; Color :
  951. cardinal );
  952. var
  953. dx, dy, sdx, sdy, x, y, px, py : integer;
  954. begin
  955. dx := x2 - x1;
  956. dy := y2 - y1;
  957. if dx < 0 then
  958. sdx := -1
  959. else
  960. sdx := 1;
  961. if dy < 0 then
  962. sdy := -1
  963. else
  964. sdy := 1;
  965. dx := sdx * dx + 1;
  966. dy := sdy * dy + 1;
  967. x := 0;
  968. y := 0;
  969. px := x1;
  970. py := y1;
  971. if dx >= dy then
  972. begin
  973. for x := 0 to dx - 1 do
  974. begin
  975. SDL_AddPixel( DstSurface, px, py, Color );
  976. y := y + dy;
  977. if y >= dx then
  978. begin
  979. y := y - dx;
  980. py := py + sdy;
  981. end;
  982. px := px + sdx;
  983. end;
  984. end
  985. else
  986. begin
  987. for y := 0 to dy - 1 do
  988. begin
  989. SDL_AddPixel( DstSurface, px, py, Color );
  990. x := x + dx;
  991. if x >= dy then
  992. begin
  993. x := x - dy;
  994. px := px + sdx;
  995. end;
  996. py := py + sdy;
  997. end;
  998. end;
  999. end;
  1000. procedure SDL_SubLine( DstSurface : PSDL_Surface; x1, y1, x2, y2 : integer; Color :
  1001. cardinal );
  1002. var
  1003. dx, dy, sdx, sdy, x, y, px, py : integer;
  1004. begin
  1005. dx := x2 - x1;
  1006. dy := y2 - y1;
  1007. if dx < 0 then
  1008. sdx := -1
  1009. else
  1010. sdx := 1;
  1011. if dy < 0 then
  1012. sdy := -1
  1013. else
  1014. sdy := 1;
  1015. dx := sdx * dx + 1;
  1016. dy := sdy * dy + 1;
  1017. x := 0;
  1018. y := 0;
  1019. px := x1;
  1020. py := y1;
  1021. if dx >= dy then
  1022. begin
  1023. for x := 0 to dx - 1 do
  1024. begin
  1025. SDL_SubPixel( DstSurface, px, py, Color );
  1026. y := y + dy;
  1027. if y >= dx then
  1028. begin
  1029. y := y - dx;
  1030. py := py + sdy;
  1031. end;
  1032. px := px + sdx;
  1033. end;
  1034. end
  1035. else
  1036. begin
  1037. for y := 0 to dy - 1 do
  1038. begin
  1039. SDL_SubPixel( DstSurface, px, py, Color );
  1040. x := x + dx;
  1041. if x >= dy then
  1042. begin
  1043. x := x - dy;
  1044. px := px + sdx;
  1045. end;
  1046. py := py + sdy;
  1047. end;
  1048. end;
  1049. end;
  1050. // This procedure works on 8, 15, 16, 24 and 32 bits color depth surfaces.
  1051. // In 8 bit color depth mode the procedure works with the default packed
  1052. // palette (RRRGGGBB). It handles all clipping.
  1053. procedure SDL_AddSurface( SrcSurface : PSDL_Surface; SrcRect : PSDL_Rect;
  1054. DstSurface : PSDL_Surface; DestRect : PSDL_Rect );
  1055. var
  1056. Src, Dest : TSDL_Rect;
  1057. Diff : integer;
  1058. SrcAddr, DestAddr : cardinal;
  1059. // TransparentColor: cardinal;
  1060. _ebx, _esi, _edi, _esp : cardinal;
  1061. WorkX, WorkY : word;
  1062. SrcMod, DestMod : cardinal;
  1063. Bits : cardinal;
  1064. begin
  1065. if ( SrcSurface = nil ) or ( DstSurface = nil ) then
  1066. exit; // Remove this to make it faster
  1067. if ( SrcSurface.Format.BitsPerPixel <> DstSurface.Format.BitsPerPixel ) then
  1068. exit; // Remove this to make it faster
  1069. if SrcRect = nil then
  1070. begin
  1071. with Src do
  1072. begin
  1073. x := 0;
  1074. y := 0;
  1075. w := SrcSurface.w;
  1076. h := SrcSurface.h;
  1077. end;
  1078. end
  1079. else
  1080. Src := SrcRect^;
  1081. if DestRect = nil then
  1082. begin
  1083. Dest.x := 0;
  1084. Dest.y := 0;
  1085. end
  1086. else
  1087. Dest := DestRect^;
  1088. Dest.w := Src.w;
  1089. Dest.h := Src.h;
  1090. with DstSurface.Clip_Rect do
  1091. begin
  1092. // Source's right side is greater than the dest.cliprect
  1093. if Dest.x + Src.w > x + w then
  1094. begin
  1095. smallint( Src.w ) := x + w - Dest.x;
  1096. smallint( Dest.w ) := x + w - Dest.x;
  1097. if smallint( Dest.w ) < 1 then
  1098. exit;
  1099. end;
  1100. // Source's bottom side is greater than the dest.clip
  1101. if Dest.y + Src.h > y + h then
  1102. begin
  1103. smallint( Src.h ) := y + h - Dest.y;
  1104. smallint( Dest.h ) := y + h - Dest.y;
  1105. if smallint( Dest.h ) < 1 then
  1106. exit;
  1107. end;
  1108. // Source's left side is less than the dest.clip
  1109. if Dest.x < x then
  1110. begin
  1111. Diff := x - Dest.x;
  1112. Src.x := Src.x + Diff;
  1113. smallint( Src.w ) := smallint( Src.w ) - Diff;
  1114. Dest.x := x;
  1115. smallint( Dest.w ) := smallint( Dest.w ) - Diff;
  1116. if smallint( Dest.w ) < 1 then
  1117. exit;
  1118. end;
  1119. // Source's Top side is less than the dest.clip
  1120. if Dest.y < y then
  1121. begin
  1122. Diff := y - Dest.y;
  1123. Src.y := Src.y + Diff;
  1124. smallint( Src.h ) := smallint( Src.h ) - Diff;
  1125. Dest.y := y;
  1126. smallint( Dest.h ) := smallint( Dest.h ) - Diff;
  1127. if smallint( Dest.h ) < 1 then
  1128. exit;
  1129. end;
  1130. end;
  1131. with SrcSurface^ do
  1132. begin
  1133. SrcAddr := cardinal( Pixels ) + UInt32( Src.y ) * Pitch + UInt32( Src.x ) *
  1134. Format.BytesPerPixel;
  1135. SrcMod := Pitch - Src.w * Format.BytesPerPixel;
  1136. // TransparentColor := format.ColorKey;
  1137. end;
  1138. with DstSurface^ do
  1139. begin
  1140. DestAddr := cardinal( Pixels ) + UInt32( Dest.y ) * Pitch + UInt32( Dest.x ) *
  1141. Format.BytesPerPixel;
  1142. DestMod := Pitch - Dest.w * Format.BytesPerPixel;
  1143. Bits := Format.BitsPerPixel;
  1144. end;
  1145. SDL_LockSurface( SrcSurface );
  1146. SDL_LockSurface( DstSurface );
  1147. case bits of
  1148. 8 :
  1149. asm
  1150. mov _ebx, ebx
  1151. mov _esi, esi
  1152. mov _edi, edi
  1153. mov _esp, esp
  1154. mov esi, SrcAddr // ESI - Source Offset
  1155. mov edi, DestAddr // EDI - Destination Offset
  1156. mov ax, Src.h // WorkY := Src.h
  1157. mov WorkY, ax
  1158. @LoopY:
  1159. mov ax, Src.w
  1160. mov WorkX, ax // WorkX := Src.w
  1161. @Loopx:
  1162. mov al, [esi] // AL := source color
  1163. cmp al, 0
  1164. je @SkipColor // if AL=0 or AL=transparent color then skip everything
  1165. mov esp, eax // ESP - source color
  1166. mov bl, [edi] // BL := destination color
  1167. mov dl, bl // DL := destination color
  1168. and ax, $03 // Adding BLUE
  1169. and bl, $03
  1170. add al, bl
  1171. cmp al, $03
  1172. jbe @Skip1
  1173. mov al, $03
  1174. @Skip1:
  1175. mov cl, al
  1176. mov eax, esp // Adding GREEN
  1177. mov bl, dl
  1178. and al, $1c
  1179. and bl, $1c
  1180. add al, bl
  1181. cmp al, $1c
  1182. jbe @Skip2
  1183. mov al, $1c
  1184. @Skip2:
  1185. or cl, al
  1186. mov eax, esp // Adding RED
  1187. mov bl, dl
  1188. and ax, $e0
  1189. and bx, $e0
  1190. add ax, bx
  1191. cmp ax, $e0
  1192. jbe @Skip3
  1193. mov al, $e0
  1194. @Skip3:
  1195. or cl, al
  1196. mov [edi], cl
  1197. @SkipColor:
  1198. inc esi
  1199. inc edi
  1200. dec WorkX
  1201. jnz @LoopX
  1202. add esi, SrcMod
  1203. add edi, DestMod
  1204. dec WorkY
  1205. jnz @LoopY
  1206. mov esp,_esp
  1207. mov edi,_edi
  1208. mov esi,_esi
  1209. mov ebx,_ebx
  1210. end;
  1211. 15 :
  1212. asm
  1213. mov _ebx, ebx
  1214. mov _esi, esi
  1215. mov _edi, edi
  1216. mov _esp, esp
  1217. mov esi, SrcAddr // ESI - Source Offset
  1218. mov edi, DestAddr // EDI - Destination Offset
  1219. mov ax, Src.h // WorkY := Src.h
  1220. mov WorkY, ax
  1221. @LoopY:
  1222. mov ax, Src.w
  1223. mov WorkX, ax // WorkX := Src.w
  1224. @Loopx:
  1225. mov ax, [esi] // AX := source color
  1226. cmp ax, 0
  1227. je @SkipColor // if AX=0 then skip everything
  1228. mov esp, eax // ESP - source color
  1229. mov bx, [edi] // BX := destination color
  1230. mov dx, bx // DX := destination color
  1231. and ax, $001F // Adding BLUE
  1232. and bx, $001F
  1233. add ax, bx
  1234. cmp ax, $001F
  1235. jbe @Skip1
  1236. mov ax, $001F
  1237. @Skip1:
  1238. mov cx, ax
  1239. mov eax, esp // Adding GREEN
  1240. mov bx, dx
  1241. and ax, $3E0
  1242. and bx, $3E0
  1243. add ax, bx
  1244. cmp ax, $3E0
  1245. jbe @Skip2
  1246. mov ax, $3E0
  1247. @Skip2:
  1248. or cx, ax
  1249. mov eax, esp // Adding RED
  1250. mov bx, dx
  1251. and ax, $7C00
  1252. and bx, $7C00
  1253. add ax, bx
  1254. cmp ax, $7C00
  1255. jbe @Skip3
  1256. mov ax, $7C00
  1257. @Skip3:
  1258. or cx, ax
  1259. mov [edi], cx
  1260. @SkipColor:
  1261. add esi, 2
  1262. add edi, 2
  1263. dec WorkX
  1264. jnz @LoopX
  1265. add esi, SrcMod
  1266. add edi, DestMod
  1267. dec WorkY
  1268. jnz @LoopY
  1269. mov esp,_esp
  1270. mov edi,_edi
  1271. mov esi,_esi
  1272. mov ebx,_ebx
  1273. end;
  1274. 16 :
  1275. asm
  1276. mov _ebx, ebx
  1277. mov _esi, esi
  1278. mov _edi, edi
  1279. mov _esp, esp
  1280. mov esi, SrcAddr // ESI - Source Offset
  1281. mov edi, DestAddr // EDI - Destination Offset
  1282. mov ax, Src.h // WorkY := Src.h
  1283. mov WorkY, ax
  1284. @LoopY:
  1285. mov ax, Src.w
  1286. mov WorkX, ax // WorkX := Src.w
  1287. @Loopx:
  1288. mov ax, [esi] // AX := source color
  1289. cmp ax, 0
  1290. je @SkipColor // if AX=0 then skip everything
  1291. mov esp, eax // ESP - source color
  1292. mov bx, [edi] // BX := destination color
  1293. mov dx, bx // DX := destination color
  1294. and ax, $1F // Adding BLUE
  1295. and bx, $1F
  1296. add ax, bx
  1297. cmp ax, $1F
  1298. jbe @Skip1
  1299. mov ax, $1F
  1300. @Skip1:
  1301. mov cx, ax
  1302. mov eax, esp // Adding GREEN
  1303. mov bx, dx
  1304. and ax, $7E0
  1305. and bx, $7E0
  1306. add ax, bx
  1307. cmp ax, $7E0
  1308. jbe @Skip2
  1309. mov ax, $7E0
  1310. @Skip2:
  1311. or cx, ax
  1312. mov eax, esp // Adding RED
  1313. mov bx, dx
  1314. and eax, $F800
  1315. and ebx, $F800
  1316. add eax, ebx
  1317. cmp eax, $F800
  1318. jbe @Skip3
  1319. mov ax, $F800
  1320. @Skip3:
  1321. or cx, ax
  1322. mov [edi], cx
  1323. @SkipColor:
  1324. add esi, 2
  1325. add edi, 2
  1326. dec WorkX
  1327. jnz @LoopX
  1328. add esi, SrcMod
  1329. add edi, DestMod
  1330. dec WorkY
  1331. jnz @LoopY
  1332. mov esp,_esp
  1333. mov edi,_edi
  1334. mov esi,_esi
  1335. mov ebx,_ebx
  1336. end;
  1337. 24 :
  1338. asm
  1339. mov _ebx, ebx
  1340. mov _esi, esi
  1341. mov _edi, edi
  1342. mov _esp, esp
  1343. mov esi, SrcAddr // ESI - Source Offset
  1344. mov edi, DestAddr // EDI - Destination Offset
  1345. mov ax, Src.h // WorkY := Src.h
  1346. mov WorkY, ax
  1347. @LoopY:
  1348. mov ax, Src.w
  1349. mov WorkX, ax // WorkX := Src.w
  1350. add WorkX, ax // WorkX := Src.w * 2
  1351. add WorkX, ax // WorkX := Src.w * 3
  1352. @Loopx:
  1353. mov bl, [edi] // BX := destination color
  1354. mov al, [esi] // AX := source color
  1355. cmp al, 0
  1356. je @Skip // if AL=0 then skip COMPONENT
  1357. mov ah, 0 // AX := COLOR COMPONENT
  1358. mov bh, 0
  1359. add bx, ax
  1360. cmp bx, $00ff
  1361. jb @Skip
  1362. mov bl, $ff
  1363. @Skip:
  1364. mov [edi], bl
  1365. inc esi
  1366. inc edi
  1367. dec WorkX
  1368. jnz @LoopX
  1369. add esi, SrcMod
  1370. add edi, DestMod
  1371. dec WorkY
  1372. jnz @LoopY
  1373. mov esp,_esp
  1374. mov edi,_edi
  1375. mov esi,_esi
  1376. mov ebx,_ebx
  1377. end;
  1378. 32 :
  1379. asm
  1380. mov _ebx, ebx
  1381. mov _esi, esi
  1382. mov _edi, edi
  1383. mov _esp, esp
  1384. mov esi, SrcAddr // ESI - Source Offset
  1385. mov edi, DestAddr // EDI - Destination Offset
  1386. mov ax, Src.h // WorkY := Src.h
  1387. mov WorkY, ax
  1388. @LoopY:
  1389. mov ax, Src.w
  1390. shl ax, 2
  1391. mov WorkX, ax // WorkX := Src.w * 4
  1392. @Loopx:
  1393. mov bl, [edi] // BX := destination color
  1394. mov al, [esi] // AX := source color
  1395. cmp al, 0
  1396. je @Skip // if AL=0 then skip COMPONENT
  1397. mov ah, 0 // AX := COLOR COMPONENT
  1398. mov bh, 0
  1399. add bx, ax
  1400. cmp bx, $00ff
  1401. jb @Skip
  1402. mov bl, $ff
  1403. @Skip:
  1404. mov [edi], bl
  1405. inc esi
  1406. inc edi
  1407. dec WorkX
  1408. jnz @LoopX
  1409. add esi, SrcMod
  1410. add edi, DestMod
  1411. dec WorkY
  1412. jnz @LoopY
  1413. mov esp,_esp
  1414. mov edi,_edi
  1415. mov esi,_esi
  1416. mov ebx,_ebx
  1417. end;
  1418. end;
  1419. SDL_UnlockSurface( SrcSurface );
  1420. SDL_UnlockSurface( DstSurface );
  1421. end;
  1422. procedure SDL_SubSurface( SrcSurface : PSDL_Surface; SrcRect : PSDL_Rect;
  1423. DstSurface : PSDL_Surface; DestRect : PSDL_Rect );
  1424. var
  1425. Src, Dest : TSDL_Rect;
  1426. Diff : integer;
  1427. SrcAddr, DestAddr : cardinal;
  1428. _ebx, _esi, _edi, _esp : cardinal;
  1429. WorkX, WorkY : word;
  1430. SrcMod, DestMod : cardinal;
  1431. Bits : cardinal;
  1432. begin
  1433. if ( SrcSurface = nil ) or ( DstSurface = nil ) then
  1434. exit; // Remove this to make it faster
  1435. if ( SrcSurface.Format.BitsPerPixel <> DstSurface.Format.BitsPerPixel ) then
  1436. exit; // Remove this to make it faster
  1437. if SrcRect = nil then
  1438. begin
  1439. with Src do
  1440. begin
  1441. x := 0;
  1442. y := 0;
  1443. w := SrcSurface.w;
  1444. h := SrcSurface.h;
  1445. end;
  1446. end
  1447. else
  1448. Src := SrcRect^;
  1449. if DestRect = nil then
  1450. begin
  1451. Dest.x := 0;
  1452. Dest.y := 0;
  1453. end
  1454. else
  1455. Dest := DestRect^;
  1456. Dest.w := Src.w;
  1457. Dest.h := Src.h;
  1458. with DstSurface.Clip_Rect do
  1459. begin
  1460. // Source's right side is greater than the dest.cliprect
  1461. if Dest.x + Src.w > x + w then
  1462. begin
  1463. smallint( Src.w ) := x + w - Dest.x;
  1464. smallint( Dest.w ) := x + w - Dest.x;
  1465. if smallint( Dest.w ) < 1 then
  1466. exit;
  1467. end;
  1468. // Source's bottom side is greater than the dest.clip
  1469. if Dest.y + Src.h > y + h then
  1470. begin
  1471. smallint( Src.h ) := y + h - Dest.y;
  1472. smallint( Dest.h ) := y + h - Dest.y;
  1473. if smallint( Dest.h ) < 1 then
  1474. exit;
  1475. end;
  1476. // Source's left side is less than the dest.clip
  1477. if Dest.x < x then
  1478. begin
  1479. Diff := x - Dest.x;
  1480. Src.x := Src.x + Diff;
  1481. smallint( Src.w ) := smallint( Src.w ) - Diff;
  1482. Dest.x := x;
  1483. smallint( Dest.w ) := smallint( Dest.w ) - Diff;
  1484. if smallint( Dest.w ) < 1 then
  1485. exit;
  1486. end;
  1487. // Source's Top side is less than the dest.clip
  1488. if Dest.y < y then
  1489. begin
  1490. Diff := y - Dest.y;
  1491. Src.y := Src.y + Diff;
  1492. smallint( Src.h ) := smallint( Src.h ) - Diff;
  1493. Dest.y := y;
  1494. smallint( Dest.h ) := smallint( Dest.h ) - Diff;
  1495. if smallint( Dest.h ) < 1 then
  1496. exit;
  1497. end;
  1498. end;
  1499. with SrcSurface^ do
  1500. begin
  1501. SrcAddr := cardinal( Pixels ) + UInt32( Src.y ) * Pitch + UInt32( Src.x ) *
  1502. Format.BytesPerPixel;
  1503. SrcMod := Pitch - Src.w * Format.BytesPerPixel;
  1504. end;
  1505. with DstSurface^ do
  1506. begin
  1507. DestAddr := cardinal( Pixels ) + UInt32( Dest.y ) * Pitch + UInt32( Dest.x ) *
  1508. Format.BytesPerPixel;
  1509. DestMod := Pitch - Dest.w * Format.BytesPerPixel;
  1510. Bits := DstSurface.Format.BitsPerPixel;
  1511. end;
  1512. SDL_LockSurface( SrcSurface );
  1513. SDL_LockSurface( DstSurface );
  1514. case bits of
  1515. 8 :
  1516. asm
  1517. mov _ebx, ebx
  1518. mov _esi, esi
  1519. mov _edi, edi
  1520. mov _esp, esp
  1521. mov esi, SrcAddr // ESI - Source Offset
  1522. mov edi, DestAddr // EDI - Destination Offset
  1523. mov ax, Src.h // WorkY := Src.h
  1524. mov WorkY, ax
  1525. @LoopY:
  1526. mov ax, Src.w
  1527. mov WorkX, ax // WorkX := Src.w
  1528. @Loopx:
  1529. mov al, [esi] // AL := source color
  1530. cmp al, 0
  1531. je @SkipColor // if AL=0 then skip everything
  1532. mov esp, eax // ESP - source color
  1533. mov bl, [edi] // BL := destination color
  1534. mov dl, bl // DL := destination color
  1535. and al, $03 // Subtract BLUE
  1536. and bl, $03
  1537. sub bl, al
  1538. jns @Skip1
  1539. mov bl, 0
  1540. @Skip1:
  1541. mov cl, bl
  1542. mov eax, esp // Subtract GREEN
  1543. mov bl, dl
  1544. and al, $1c
  1545. and bl, $1c
  1546. sub bl, al
  1547. jns @Skip2
  1548. mov bl, 0
  1549. @Skip2:
  1550. or cl, bl
  1551. mov eax, esp // Subtract RED
  1552. mov bl, dl
  1553. and ax, $e0
  1554. and bx, $e0
  1555. sub bx, ax
  1556. jns @Skip3
  1557. mov bl, 0
  1558. @Skip3:
  1559. or cl, bl
  1560. mov [edi], cl
  1561. @SkipColor:
  1562. inc esi
  1563. inc edi
  1564. dec WorkX
  1565. jnz @LoopX
  1566. add esi, SrcMod
  1567. add edi, DestMod
  1568. dec WorkY
  1569. jnz @LoopY
  1570. mov esp,_esp
  1571. mov edi,_edi
  1572. mov esi,_esi
  1573. mov ebx,_ebx
  1574. end;
  1575. 15 :
  1576. asm
  1577. mov _ebx, ebx
  1578. mov _esi, esi
  1579. mov _edi, edi
  1580. mov _esp, esp
  1581. mov esi, SrcAddr // ESI - Source Offset
  1582. mov edi, DestAddr // EDI - Destination Offset
  1583. mov ax, Src.h // WorkY := Src.h
  1584. mov WorkY, ax
  1585. @LoopY:
  1586. mov ax, Src.w
  1587. mov WorkX, ax // WorkX := Src.w
  1588. @Loopx:
  1589. mov ax, [esi] // AX := source color
  1590. cmp ax, 0
  1591. je @SkipColor // if AX=0 then skip everything
  1592. mov esp, eax // ESP - source color
  1593. mov bx, [edi] // BX := destination color
  1594. mov dx, bx // DX := destination color
  1595. and ax, $001F // Subtract BLUE
  1596. and bx, $001F
  1597. sub bx, ax
  1598. jns @Skip1
  1599. mov bx, 0
  1600. @Skip1:
  1601. mov cx, bx
  1602. mov eax, esp // Subtract GREEN
  1603. mov bx, dx
  1604. and ax, $3E0
  1605. and bx, $3E0
  1606. sub bx, ax
  1607. jns @Skip2
  1608. mov bx, 0
  1609. @Skip2:
  1610. or cx, bx
  1611. mov eax, esp // Subtract RED
  1612. mov bx, dx
  1613. and ax, $7C00
  1614. and bx, $7C00
  1615. sub bx, ax
  1616. jns @Skip3
  1617. mov bx, 0
  1618. @Skip3:
  1619. or cx, bx
  1620. mov [edi], cx
  1621. @SkipColor:
  1622. add esi, 2
  1623. add edi, 2
  1624. dec WorkX
  1625. jnz @LoopX
  1626. add esi, SrcMod
  1627. add edi, DestMod
  1628. dec WorkY
  1629. jnz @LoopY
  1630. mov esp,_esp
  1631. mov edi,_edi
  1632. mov esi,_esi
  1633. mov ebx,_ebx
  1634. end;
  1635. 16 :
  1636. asm
  1637. mov _ebx, ebx
  1638. mov _esi, esi
  1639. mov _edi, edi
  1640. mov _esp, esp
  1641. mov esi, SrcAddr // ESI - Source Offset
  1642. mov edi, DestAddr // EDI - Destination Offset
  1643. mov ax, Src.h // WorkY := Src.h
  1644. mov WorkY, ax
  1645. @LoopY:
  1646. mov ax, Src.w
  1647. mov WorkX, ax // WorkX := Src.w
  1648. @Loopx:
  1649. mov ax, [esi] // AX := source color
  1650. cmp ax, 0
  1651. je @SkipColor // if AX=0 then skip everything
  1652. mov esp, eax // ESP - source color
  1653. mov bx, [edi] // BX := destination color
  1654. mov dx, bx // DX := destination color
  1655. and ax, $1F // Subtracting BLUE
  1656. and bx, $1F
  1657. sub bx, ax
  1658. jns @Skip1
  1659. mov bx, 0
  1660. @Skip1:
  1661. mov cx, bx
  1662. mov eax, esp // Adding GREEN
  1663. mov bx, dx
  1664. and ax, $7E0
  1665. and bx, $7E0
  1666. sub bx, ax
  1667. jns @Skip2
  1668. mov bx, 0
  1669. @Skip2:
  1670. or cx, bx
  1671. mov eax, esp // Adding RED
  1672. mov bx, dx
  1673. and eax, $F800
  1674. and ebx, $F800
  1675. sub ebx, eax
  1676. jns @Skip3
  1677. mov bx, 0
  1678. @Skip3:
  1679. or cx, bx
  1680. mov [edi], cx
  1681. @SkipColor:
  1682. add esi, 2
  1683. add edi, 2
  1684. dec WorkX
  1685. jnz @LoopX
  1686. add esi, SrcMod
  1687. add edi, DestMod
  1688. dec WorkY
  1689. jnz @LoopY
  1690. mov esp,_esp
  1691. mov edi,_edi
  1692. mov esi,_esi
  1693. mov ebx,_ebx
  1694. end;
  1695. 24 :
  1696. asm
  1697. mov _ebx, ebx
  1698. mov _esi, esi
  1699. mov _edi, edi
  1700. mov _esp, esp
  1701. mov esi, SrcAddr // ESI - Source Offset
  1702. mov edi, DestAddr // EDI - Destination Offset
  1703. mov ax, Src.h // WorkY := Src.h
  1704. mov WorkY, ax
  1705. @LoopY:
  1706. mov ax, Src.w
  1707. mov WorkX, ax // WorkX := Src.w
  1708. add WorkX, ax // WorkX := Src.w * 2
  1709. add WorkX, ax // WorkX := Src.w * 3
  1710. @Loopx:
  1711. mov bl, [edi] // BX := destination color
  1712. mov al, [esi] // AX := source color
  1713. cmp al, 0
  1714. je @Skip // if AL=0 then skip COMPONENT
  1715. mov ah, 0 // AX := COLOR COMPONENT
  1716. mov bh, 0
  1717. sub bx, ax
  1718. jns @Skip
  1719. mov bl, 0
  1720. @Skip:
  1721. mov [edi], bl
  1722. inc esi
  1723. inc edi
  1724. dec WorkX
  1725. jnz @LoopX
  1726. add esi, SrcMod
  1727. add edi, DestMod
  1728. dec WorkY
  1729. jnz @LoopY
  1730. mov esp,_esp
  1731. mov edi,_edi
  1732. mov esi,_esi
  1733. mov ebx,_ebx
  1734. end;
  1735. 32 :
  1736. asm
  1737. mov _ebx, ebx
  1738. mov _esi, esi
  1739. mov _edi, edi
  1740. mov _esp, esp
  1741. mov esi, SrcAddr // ESI - Source Offset
  1742. mov edi, DestAddr // EDI - Destination Offset
  1743. mov ax, Src.h // WorkY := Src.h
  1744. mov WorkY, ax
  1745. @LoopY:
  1746. mov ax, Src.w
  1747. shl ax, 2
  1748. mov WorkX, ax // WorkX := Src.w * 4
  1749. @Loopx:
  1750. mov bl, [edi] // BX := destination color
  1751. mov al, [esi] // AX := source color
  1752. cmp al, 0
  1753. je @Skip // if AL=0 then skip COMPONENT
  1754. mov ah, 0 // AX := COLOR COMPONENT
  1755. mov bh, 0
  1756. sub bx, ax
  1757. jns @Skip
  1758. mov bl, 0
  1759. @Skip:
  1760. mov [edi], bl
  1761. inc esi
  1762. inc edi
  1763. dec WorkX
  1764. jnz @LoopX
  1765. add esi, SrcMod
  1766. add edi, DestMod
  1767. dec WorkY
  1768. jnz @LoopY
  1769. mov esp,_esp
  1770. mov edi,_edi
  1771. mov esi,_esi
  1772. mov ebx,_ebx
  1773. end;
  1774. end;
  1775. SDL_UnlockSurface( SrcSurface );
  1776. SDL_UnlockSurface( DstSurface );
  1777. end;
  1778. procedure SDL_MonoSurface( SrcSurface : PSDL_Surface; SrcRect : PSDL_Rect;
  1779. DstSurface : PSDL_Surface; DestRect : PSDL_Rect; Color : cardinal );
  1780. var
  1781. Src, Dest : TSDL_Rect;
  1782. Diff : integer;
  1783. SrcAddr, DestAddr : cardinal;
  1784. _ebx, _esi, _edi, _esp : cardinal;
  1785. WorkX, WorkY : word;
  1786. SrcMod, DestMod : cardinal;
  1787. SrcTransparentColor : cardinal;
  1788. Bits : cardinal;
  1789. begin
  1790. if ( SrcSurface = nil ) or ( DstSurface = nil ) then
  1791. exit; // Remove this to make it faster
  1792. if ( SrcSurface.Format.BitsPerPixel <> DstSurface.Format.BitsPerPixel ) then
  1793. exit; // Remove this to make it faster
  1794. if SrcRect = nil then
  1795. begin
  1796. with Src do
  1797. begin
  1798. x := 0;
  1799. y := 0;
  1800. w := SrcSurface.w;
  1801. h := SrcSurface.h;
  1802. end;
  1803. end
  1804. else
  1805. Src := SrcRect^;
  1806. if DestRect = nil then
  1807. begin
  1808. Dest.x := 0;
  1809. Dest.y := 0;
  1810. end
  1811. else
  1812. Dest := DestRect^;
  1813. Dest.w := Src.w;
  1814. Dest.h := Src.h;
  1815. with DstSurface.Clip_Rect do
  1816. begin
  1817. // Source's right side is greater than the dest.cliprect
  1818. if Dest.x + Src.w > x + w then
  1819. begin
  1820. smallint( Src.w ) := x + w - Dest.x;
  1821. smallint( Dest.w ) := x + w - Dest.x;
  1822. if smallint( Dest.w ) < 1 then
  1823. exit;
  1824. end;
  1825. // Source's bottom side is greater than the dest.clip
  1826. if Dest.y + Src.h > y + h then
  1827. begin
  1828. smallint( Src.h ) := y + h - Dest.y;
  1829. smallint( Dest.h ) := y + h - Dest.y;
  1830. if smallint( Dest.h ) < 1 then
  1831. exit;
  1832. end;
  1833. // Source's left side is less than the dest.clip
  1834. if Dest.x < x then
  1835. begin
  1836. Diff := x - Dest.x;
  1837. Src.x := Src.x + Diff;
  1838. smallint( Src.w ) := smallint( Src.w ) - Diff;
  1839. Dest.x := x;
  1840. smallint( Dest.w ) := smallint( Dest.w ) - Diff;
  1841. if smallint( Dest.w ) < 1 then
  1842. exit;
  1843. end;
  1844. // Source's Top side is less than the dest.clip
  1845. if Dest.y < y then
  1846. begin
  1847. Diff := y - Dest.y;
  1848. Src.y := Src.y + Diff;
  1849. smallint( Src.h ) := smallint( Src.h ) - Diff;
  1850. Dest.y := y;
  1851. smallint( Dest.h ) := smallint( Dest.h ) - Diff;
  1852. if smallint( Dest.h ) < 1 then
  1853. exit;
  1854. end;
  1855. end;
  1856. with SrcSurface^ do
  1857. begin
  1858. SrcAddr := cardinal( Pixels ) + UInt32( Src.y ) * Pitch + UInt32( Src.x ) *
  1859. Format.BytesPerPixel;
  1860. SrcMod := Pitch - Src.w * Format.BytesPerPixel;
  1861. SrcTransparentColor := format.colorkey;
  1862. end;
  1863. with DstSurface^ do
  1864. begin
  1865. DestAddr := cardinal( Pixels ) + UInt32( Dest.y ) * Pitch + UInt32( Dest.x ) *
  1866. Format.BytesPerPixel;
  1867. DestMod := Pitch - Dest.w * Format.BytesPerPixel;
  1868. Bits := DstSurface.Format.BitsPerPixel;
  1869. end;
  1870. SDL_LockSurface( SrcSurface );
  1871. SDL_LockSurface( DstSurface );
  1872. case bits of
  1873. 8 :
  1874. asm
  1875. mov _esi, esi
  1876. mov _edi, edi
  1877. mov _esp, esp
  1878. mov esi, SrcAddr // ESI - Source Offset
  1879. mov edi, DestAddr // EDI - Destination Offset
  1880. mov ax, Src.h // WorkY := Src.h
  1881. mov WorkY, ax
  1882. mov ecx, Color
  1883. @LoopY:
  1884. mov ax, Src.w
  1885. mov WorkX, ax // WorkX := Src.w
  1886. @Loopx:
  1887. mov al, [esi] // AL := source color
  1888. movzx eax, al
  1889. cmp eax, SrcTransparentColor
  1890. je @SkipColor // if AL=Transparent color then skip everything
  1891. mov [edi], cl
  1892. @SkipColor:
  1893. inc esi
  1894. inc edi
  1895. dec WorkX
  1896. jnz @LoopX
  1897. add esi, SrcMod
  1898. add edi, DestMod
  1899. dec WorkY
  1900. jnz @LoopY
  1901. mov esp,_esp
  1902. mov edi,_edi
  1903. mov esi,_esi
  1904. end;
  1905. 15, 16 :
  1906. asm
  1907. mov _esi, esi
  1908. mov _edi, edi
  1909. mov _esp, esp
  1910. mov esi, SrcAddr // ESI - Source Offset
  1911. mov edi, DestAddr // EDI - Destination Offset
  1912. mov ax, Src.h // WorkY := Src.h
  1913. mov WorkY, ax
  1914. mov ecx, Color
  1915. @LoopY:
  1916. mov ax, Src.w
  1917. mov WorkX, ax // WorkX := Src.w
  1918. @Loopx:
  1919. mov ax, [esi] // AX := source color
  1920. movzx eax, ax
  1921. cmp eax, SrcTransparentColor
  1922. je @SkipColor // if AX=Transparent color then skip everything
  1923. mov [edi], cx
  1924. @SkipColor:
  1925. inc esi
  1926. inc esi
  1927. inc edi
  1928. inc edi
  1929. dec WorkX
  1930. jnz @LoopX
  1931. add esi, SrcMod
  1932. add edi, DestMod
  1933. dec WorkY
  1934. jnz @LoopY
  1935. mov esp,_esp
  1936. mov edi,_edi
  1937. mov esi,_esi
  1938. end;
  1939. 24 :
  1940. asm
  1941. mov _esi, esi
  1942. mov _edi, edi
  1943. mov _esp, esp
  1944. mov _ebx, ebx
  1945. mov esi, SrcAddr // ESI - Source Offset
  1946. mov edi, DestAddr // EDI - Destination Offset
  1947. mov ax, Src.h // WorkY := Src.h
  1948. mov WorkY, ax
  1949. mov ecx, Color
  1950. and ecx, $00ffffff
  1951. @LoopY:
  1952. mov ax, Src.w
  1953. mov WorkX, ax // WorkX := Src.w
  1954. @Loopx:
  1955. mov eax, [esi] // EAX := source color
  1956. and eax, $00ffffff
  1957. cmp eax, SrcTransparentColor
  1958. je @SkipColor // if EAX=Transparent color then skip everything
  1959. mov ebx, [edi]
  1960. and ebx, $ff000000
  1961. or ebx, ecx
  1962. mov [edi], ecx
  1963. @SkipColor:
  1964. add esi, 3
  1965. add edi, 3
  1966. dec WorkX
  1967. jnz @LoopX
  1968. add esi, SrcMod
  1969. add edi, DestMod
  1970. dec WorkY
  1971. jnz @LoopY
  1972. mov esp, _esp
  1973. mov edi, _edi
  1974. mov esi, _esi
  1975. mov ebx, _ebx
  1976. end;
  1977. 32 :
  1978. asm
  1979. mov _esi, esi
  1980. mov _edi, edi
  1981. mov _esp, esp
  1982. mov esi, SrcAddr // ESI - Source Offset
  1983. mov edi, DestAddr // EDI - Destination Offset
  1984. mov ax, Src.h // WorkY := Src.h
  1985. mov WorkY, ax
  1986. mov ecx, Color
  1987. @LoopY:
  1988. mov ax, Src.w
  1989. mov WorkX, ax // WorkX := Src.w
  1990. @Loopx:
  1991. mov eax, [esi] // EAX := source color
  1992. cmp eax, SrcTransparentColor
  1993. je @SkipColor // if EAX=Transparent color then skip everything
  1994. mov [edi], ecx
  1995. @SkipColor:
  1996. add esi, 4
  1997. add edi, 4
  1998. dec WorkX
  1999. jnz @LoopX
  2000. add esi, SrcMod
  2001. add edi, DestMod
  2002. dec WorkY
  2003. jnz @LoopY
  2004. mov esp, _esp
  2005. mov edi, _edi
  2006. mov esi, _esi
  2007. end;
  2008. end;
  2009. SDL_UnlockSurface( SrcSurface );
  2010. SDL_UnlockSurface( DstSurface );
  2011. end;
  2012. // TextureRect.w and TextureRect.h are not used.
  2013. // The TextureSurface's size MUST larger than the drawing rectangle!!!
  2014. procedure SDL_TexturedSurface( SrcSurface : PSDL_Surface; SrcRect : PSDL_Rect;
  2015. DstSurface : PSDL_Surface; DestRect : PSDL_Rect; Texture : PSDL_Surface;
  2016. TextureRect : PSDL_Rect );
  2017. var
  2018. Src, Dest : TSDL_Rect;
  2019. Diff : integer;
  2020. SrcAddr, DestAddr, TextAddr : cardinal;
  2021. _ebx, _esi, _edi, _esp : cardinal;
  2022. WorkX, WorkY : word;
  2023. SrcMod, DestMod, TextMod : cardinal;
  2024. SrcTransparentColor : cardinal;
  2025. Bits : cardinal;
  2026. begin
  2027. if ( SrcSurface = nil ) or ( DstSurface = nil ) then
  2028. exit; // Remove this to make it faster
  2029. if ( SrcSurface.Format.BitsPerPixel <> DstSurface.Format.BitsPerPixel ) then
  2030. exit; // Remove this to make it faster
  2031. if SrcRect = nil then
  2032. begin
  2033. with Src do
  2034. begin
  2035. x := 0;
  2036. y := 0;
  2037. w := SrcSurface.w;
  2038. h := SrcSurface.h;
  2039. end;
  2040. end
  2041. else
  2042. Src := SrcRect^;
  2043. if DestRect = nil then
  2044. begin
  2045. Dest.x := 0;
  2046. Dest.y := 0;
  2047. end
  2048. else
  2049. Dest := DestRect^;
  2050. Dest.w := Src.w;
  2051. Dest.h := Src.h;
  2052. with DstSurface.Clip_Rect do
  2053. begin
  2054. // Source's right side is greater than the dest.cliprect
  2055. if Dest.x + Src.w > x + w then
  2056. begin
  2057. smallint( Src.w ) := x + w - Dest.x;
  2058. smallint( Dest.w ) := x + w - Dest.x;
  2059. if smallint( Dest.w ) < 1 then
  2060. exit;
  2061. end;
  2062. // Source's bottom side is greater than the dest.clip
  2063. if Dest.y + Src.h > y + h then
  2064. begin
  2065. smallint( Src.h ) := y + h - Dest.y;
  2066. smallint( Dest.h ) := y + h - Dest.y;
  2067. if smallint( Dest.h ) < 1 then
  2068. exit;
  2069. end;
  2070. // Source's left side is less than the dest.clip
  2071. if Dest.x < x then
  2072. begin
  2073. Diff := x - Dest.x;
  2074. Src.x := Src.x + Diff;
  2075. smallint( Src.w ) := smallint( Src.w ) - Diff;
  2076. Dest.x := x;
  2077. smallint( Dest.w ) := smallint( Dest.w ) - Diff;
  2078. if smallint( Dest.w ) < 1 then
  2079. exit;
  2080. end;
  2081. // Source's Top side is less than the dest.clip
  2082. if Dest.y < y then
  2083. begin
  2084. Diff := y - Dest.y;
  2085. Src.y := Src.y + Diff;
  2086. smallint( Src.h ) := smallint( Src.h ) - Diff;
  2087. Dest.y := y;
  2088. smallint( Dest.h ) := smallint( Dest.h ) - Diff;
  2089. if smallint( Dest.h ) < 1 then
  2090. exit;
  2091. end;
  2092. end;
  2093. with SrcSurface^ do
  2094. begin
  2095. SrcAddr := cardinal( Pixels ) + UInt32( Src.y ) * Pitch + UInt32( Src.x ) *
  2096. Format.BytesPerPixel;
  2097. SrcMod := Pitch - Src.w * Format.BytesPerPixel;
  2098. SrcTransparentColor := format.colorkey;
  2099. end;
  2100. with DstSurface^ do
  2101. begin
  2102. DestAddr := cardinal( Pixels ) + UInt32( Dest.y ) * Pitch + UInt32( Dest.x ) *
  2103. Format.BytesPerPixel;
  2104. DestMod := Pitch - Dest.w * Format.BytesPerPixel;
  2105. Bits := DstSurface.Format.BitsPerPixel;
  2106. end;
  2107. with Texture^ do
  2108. begin
  2109. TextAddr := cardinal( Pixels ) + UInt32( TextureRect.y ) * Pitch +
  2110. UInt32( TextureRect.x ) * Format.BytesPerPixel;
  2111. TextMod := Pitch - Src.w * Format.BytesPerPixel;
  2112. end;
  2113. SDL_LockSurface( SrcSurface );
  2114. SDL_LockSurface( DstSurface );
  2115. SDL_LockSurface( Texture );
  2116. case bits of
  2117. 8 :
  2118. asm
  2119. mov _esi, esi
  2120. mov _edi, edi
  2121. mov _esp, esp
  2122. mov _ebx, ebx
  2123. mov esi, SrcAddr // ESI - Source Offset
  2124. mov edi, DestAddr // EDI - Destination Offset
  2125. mov ebx, TextAddr
  2126. mov ax, Src.h // WorkY := Src.h
  2127. mov WorkY, ax
  2128. @LoopY:
  2129. mov ax, Src.w
  2130. mov WorkX, ax // WorkX := Src.w
  2131. @Loopx:
  2132. mov al, [esi] // AL := source color
  2133. movzx eax, al
  2134. cmp eax, SrcTransparentColor
  2135. je @SkipColor // if AL=Transparent color then skip everything
  2136. mov al, [ebx]
  2137. mov [edi], al
  2138. @SkipColor:
  2139. inc esi
  2140. inc edi
  2141. inc ebx
  2142. dec WorkX
  2143. jnz @LoopX
  2144. add esi, SrcMod
  2145. add edi, DestMod
  2146. add ebx, TextMod
  2147. dec WorkY
  2148. jnz @LoopY
  2149. mov esp,_esp
  2150. mov edi,_edi
  2151. mov esi,_esi
  2152. mov ebx, _ebx
  2153. end;
  2154. 15, 16 :
  2155. asm
  2156. mov _esi, esi
  2157. mov _edi, edi
  2158. mov _esp, esp
  2159. mov esi, SrcAddr // ESI - Source Offset
  2160. mov edi, DestAddr // EDI - Destination Offset
  2161. mov ecx, TextAddr
  2162. mov ax, Src.h // WorkY := Src.h
  2163. mov WorkY, ax
  2164. @LoopY:
  2165. mov ax, Src.w
  2166. mov WorkX, ax // WorkX := Src.w
  2167. @Loopx:
  2168. mov ax, [esi] // AL := source color
  2169. movzx eax, ax
  2170. cmp eax, SrcTransparentColor
  2171. je @SkipColor // if AL=Transparent color then skip everything
  2172. mov ax, [ecx]
  2173. mov [edi], ax
  2174. @SkipColor:
  2175. inc esi
  2176. inc esi
  2177. inc edi
  2178. inc edi
  2179. inc ecx
  2180. inc ecx
  2181. dec WorkX
  2182. jnz @LoopX
  2183. add esi, SrcMod
  2184. add edi, DestMod
  2185. add ecx, TextMod
  2186. dec WorkY
  2187. jnz @LoopY
  2188. mov esp,_esp
  2189. mov edi,_edi
  2190. mov esi,_esi
  2191. end;
  2192. 24 :
  2193. asm
  2194. mov _esi, esi
  2195. mov _edi, edi
  2196. mov _esp, esp
  2197. mov _ebx, ebx
  2198. mov esi, SrcAddr // ESI - Source Offset
  2199. mov edi, DestAddr // EDI - Destination Offset
  2200. mov ebx, TextAddr
  2201. mov ax, Src.h // WorkY := Src.h
  2202. mov WorkY, ax
  2203. @LoopY:
  2204. mov ax, Src.w
  2205. mov WorkX, ax // WorkX := Src.w
  2206. @Loopx:
  2207. mov eax, [esi] // AL := source color
  2208. and eax, $00ffffff
  2209. cmp eax, SrcTransparentColor
  2210. je @SkipColor // if AL=Transparent color then skip everything
  2211. mov eax, [ebx]
  2212. and eax, $00ffffff
  2213. mov ecx, [edi]
  2214. and ecx, $ff000000
  2215. or ecx, eax
  2216. mov [edi], eax
  2217. @SkipColor:
  2218. add esi, 3
  2219. add edi, 3
  2220. add ebx, 3
  2221. dec WorkX
  2222. jnz @LoopX
  2223. add esi, SrcMod
  2224. add edi, DestMod
  2225. add ebx, TextMod
  2226. dec WorkY
  2227. jnz @LoopY
  2228. mov esp,_esp
  2229. mov edi,_edi
  2230. mov esi,_esi
  2231. mov ebx, _ebx
  2232. end;
  2233. 32 :
  2234. asm
  2235. mov _esi, esi
  2236. mov _edi, edi
  2237. mov _esp, esp
  2238. mov esi, SrcAddr // ESI - Source Offset
  2239. mov edi, DestAddr // EDI - Destination Offset
  2240. mov ecx, TextAddr
  2241. mov ax, Src.h // WorkY := Src.h
  2242. mov WorkY, ax
  2243. @LoopY:
  2244. mov ax, Src.w
  2245. mov WorkX, ax // WorkX := Src.w
  2246. @Loopx:
  2247. mov eax, [esi] // AL := source color
  2248. cmp eax, SrcTransparentColor
  2249. je @SkipColor // if AL=Transparent color then skip everything
  2250. mov eax, [ecx]
  2251. mov [edi], eax
  2252. @SkipColor:
  2253. add esi, 4
  2254. add edi, 4
  2255. add ecx, 4
  2256. dec WorkX
  2257. jnz @LoopX
  2258. add esi, SrcMod
  2259. add edi, DestMod
  2260. add ecx, TextMod
  2261. dec WorkY
  2262. jnz @LoopY
  2263. mov esp,_esp
  2264. mov edi,_edi
  2265. mov esi,_esi
  2266. end;
  2267. end;
  2268. SDL_UnlockSurface( SrcSurface );
  2269. SDL_UnlockSurface( DstSurface );
  2270. SDL_UnlockSurface( Texture );
  2271. end;
  2272. procedure SDL_ZoomSurface( SrcSurface : PSDL_Surface; SrcRect : PSDL_Rect; DstSurface : PSDL_Surface; DstRect : PSDL_Rect );
  2273. var
  2274. xc, yc : cardinal;
  2275. rx, wx, ry, wy, ry16 : cardinal;
  2276. color : cardinal;
  2277. modx, mody : cardinal;
  2278. begin
  2279. // Warning! No checks for surface pointers!!!
  2280. if srcrect = nil then
  2281. srcrect := @SrcSurface.clip_rect;
  2282. if dstrect = nil then
  2283. dstrect := @DstSurface.clip_rect;
  2284. if SDL_MustLock( SrcSurface ) then
  2285. SDL_LockSurface( SrcSurface );
  2286. if SDL_MustLock( DstSurface ) then
  2287. SDL_LockSurface( DstSurface );
  2288. modx := trunc( ( srcrect.w / dstrect.w ) * 65536 );
  2289. mody := trunc( ( srcrect.h / dstrect.h ) * 65536 );
  2290. //rx := srcrect.x * 65536;
  2291. ry := srcrect.y * 65536;
  2292. wy := dstrect.y;
  2293. for yc := 0 to dstrect.h - 1 do
  2294. begin
  2295. rx := srcrect.x * 65536;
  2296. wx := dstrect.x;
  2297. ry16 := ry shr 16;
  2298. for xc := 0 to dstrect.w - 1 do
  2299. begin
  2300. color := SDL_GetPixel( SrcSurface, rx shr 16, ry16 );
  2301. SDL_PutPixel( DstSurface, wx, wy, color );
  2302. rx := rx + modx;
  2303. inc( wx );
  2304. end;
  2305. ry := ry + mody;
  2306. inc( wy );
  2307. end;
  2308. if SDL_MustLock( SrcSurface ) then
  2309. SDL_UnlockSurface( SrcSurface );
  2310. if SDL_MustLock( DstSurface ) then
  2311. SDL_UnlockSurface( DstSurface );
  2312. end;
  2313. // Re-map a rectangular area into an area defined by four vertices
  2314. // Converted from C to Pascal by KiCHY
  2315. procedure SDL_WarpSurface( SrcSurface : PSDL_Surface; SrcRect : PSDL_Rect; DstSurface : PSDL_Surface; UL, UR, LR, LL : PPoint );
  2316. const
  2317. SHIFTS = 15; // Extend ints to limit round-off error (try 2 - 20)
  2318. THRESH = 1 shl SHIFTS; // Threshold for pixel size value
  2319. procedure CopySourceToDest( UL, UR, LR, LL : TPoint; x1, y1, x2, y2 : cardinal );
  2320. var
  2321. tm, lm, rm, bm, m : TPoint;
  2322. mx, my : cardinal;
  2323. cr : cardinal;
  2324. begin
  2325. // Does the destination area specify a single pixel?
  2326. if ( ( abs( ul.x - ur.x ) < THRESH ) and
  2327. ( abs( ul.x - lr.x ) < THRESH ) and
  2328. ( abs( ul.x - ll.x ) < THRESH ) and
  2329. ( abs( ul.y - ur.y ) < THRESH ) and
  2330. ( abs( ul.y - lr.y ) < THRESH ) and
  2331. ( abs( ul.y - ll.y ) < THRESH ) ) then
  2332. begin // Yes
  2333. cr := SDL_GetPixel( SrcSurface, ( x1 shr SHIFTS ), ( y1 shr SHIFTS ) );
  2334. SDL_PutPixel( DstSurface, ( ul.x shr SHIFTS ), ( ul.y shr SHIFTS ), cr );
  2335. end
  2336. else
  2337. begin // No
  2338. // Quarter the source and the destination, and then recurse
  2339. tm.x := ( ul.x + ur.x ) shr 1;
  2340. tm.y := ( ul.y + ur.y ) shr 1;
  2341. bm.x := ( ll.x + lr.x ) shr 1;
  2342. bm.y := ( ll.y + lr.y ) shr 1;
  2343. lm.x := ( ul.x + ll.x ) shr 1;
  2344. lm.y := ( ul.y + ll.y ) shr 1;
  2345. rm.x := ( ur.x + lr.x ) shr 1;
  2346. rm.y := ( ur.y + lr.y ) shr 1;
  2347. m.x := ( tm.x + bm.x ) shr 1;
  2348. m.y := ( tm.y + bm.y ) shr 1;
  2349. mx := ( x1 + x2 ) shr 1;
  2350. my := ( y1 + y2 ) shr 1;
  2351. CopySourceToDest( ul, tm, m, lm, x1, y1, mx, my );
  2352. CopySourceToDest( tm, ur, rm, m, mx, y1, x2, my );
  2353. CopySourceToDest( m, rm, lr, bm, mx, my, x2, y2 );
  2354. CopySourceToDest( lm, m, bm, ll, x1, my, mx, y2 );
  2355. end;
  2356. end;
  2357. var
  2358. _UL, _UR, _LR, _LL : TPoint;
  2359. Rect_x, Rect_y, Rect_w, Rect_h : integer;
  2360. begin
  2361. if SDL_MustLock( SrcSurface ) then
  2362. SDL_LockSurface( SrcSurface );
  2363. if SDL_MustLock( DstSurface ) then
  2364. SDL_LockSurface( DstSurface );
  2365. if SrcRect = nil then
  2366. begin
  2367. Rect_x := 0;
  2368. Rect_y := 0;
  2369. Rect_w := ( SrcSurface.w - 1 ) shl SHIFTS;
  2370. Rect_h := ( SrcSurface.h - 1 ) shl SHIFTS;
  2371. end
  2372. else
  2373. begin
  2374. Rect_x := SrcRect.x;
  2375. Rect_y := SrcRect.y;
  2376. Rect_w := ( SrcRect.w - 1 ) shl SHIFTS;
  2377. Rect_h := ( SrcRect.h - 1 ) shl SHIFTS;
  2378. end;
  2379. // Shift all values to help reduce round-off error.
  2380. _ul.x := ul.x shl SHIFTS;
  2381. _ul.y := ul.y shl SHIFTS;
  2382. _ur.x := ur.x shl SHIFTS;
  2383. _ur.y := ur.y shl SHIFTS;
  2384. _lr.x := lr.x shl SHIFTS;
  2385. _lr.y := lr.y shl SHIFTS;
  2386. _ll.x := ll.x shl SHIFTS;
  2387. _ll.y := ll.y shl SHIFTS;
  2388. CopySourceToDest( _ul, _ur, _lr, _ll, Rect_x, Rect_y, Rect_w, Rect_h );
  2389. if SDL_MustLock( SrcSurface ) then
  2390. SDL_UnlockSurface( SrcSurface );
  2391. if SDL_MustLock( DstSurface ) then
  2392. SDL_UnlockSurface( DstSurface );
  2393. end;
  2394. // flips a rectangle vertically on given surface
  2395. procedure SDL_FlipRectV( DstSurface : PSDL_Surface; Rect : PSDL_Rect );
  2396. var
  2397. TmpRect : TSDL_Rect;
  2398. Locked : boolean;
  2399. y, FlipLength, RowLength : integer;
  2400. Row1, Row2 : Pointer;
  2401. OneRow : TByteArray; // Optimize it if you wish
  2402. begin
  2403. if DstSurface <> nil then
  2404. begin
  2405. if Rect = nil then
  2406. begin // if Rect=nil then we flip the whole surface
  2407. TmpRect := SDLRect( 0, 0, DstSurface.w, DstSurface.h );
  2408. Rect := @TmpRect;
  2409. end;
  2410. FlipLength := Rect^.h shr 1 - 1;
  2411. RowLength := Rect^.w * DstSurface^.format.BytesPerPixel;
  2412. if SDL_MustLock( DstSurface ) then
  2413. begin
  2414. Locked := true;
  2415. SDL_LockSurface( DstSurface );
  2416. end
  2417. else
  2418. Locked := false;
  2419. Row1 := pointer( cardinal( DstSurface^.Pixels ) + UInt32( Rect^.y ) *
  2420. DstSurface^.Pitch );
  2421. Row2 := pointer( cardinal( DstSurface^.Pixels ) + ( UInt32( Rect^.y ) + Rect^.h - 1 )
  2422. * DstSurface^.Pitch );
  2423. for y := 0 to FlipLength do
  2424. begin
  2425. Move( Row1^, OneRow, RowLength );
  2426. Move( Row2^, Row1^, RowLength );
  2427. Move( OneRow, Row2^, RowLength );
  2428. inc( cardinal( Row1 ), DstSurface^.Pitch );
  2429. dec( cardinal( Row2 ), DstSurface^.Pitch );
  2430. end;
  2431. if Locked then
  2432. SDL_UnlockSurface( DstSurface );
  2433. end;
  2434. end;
  2435. // flips a rectangle horizontally on given surface
  2436. procedure SDL_FlipRectH( DstSurface : PSDL_Surface; Rect : PSDL_Rect );
  2437. type
  2438. T24bit = packed array[ 0..2 ] of byte;
  2439. T24bitArray = packed array[ 0..8191 ] of T24bit;
  2440. P24bitArray = ^T24bitArray;
  2441. TLongWordArray = array[ 0..8191 ] of LongWord;
  2442. PLongWordArray = ^TLongWordArray;
  2443. var
  2444. TmpRect : TSDL_Rect;
  2445. Row8bit : PByteArray;
  2446. Row16bit : PWordArray;
  2447. Row24bit : P24bitArray;
  2448. Row32bit : PLongWordArray;
  2449. y, x, RightSide, FlipLength : integer;
  2450. Pixel : cardinal;
  2451. Pixel24 : T24bit;
  2452. Locked : boolean;
  2453. begin
  2454. if DstSurface <> nil then
  2455. begin
  2456. if Rect = nil then
  2457. begin
  2458. TmpRect := SDLRect( 0, 0, DstSurface.w, DstSurface.h );
  2459. Rect := @TmpRect;
  2460. end;
  2461. FlipLength := Rect^.w shr 1 - 1;
  2462. if SDL_MustLock( DstSurface ) then
  2463. begin
  2464. Locked := true;
  2465. SDL_LockSurface( DstSurface );
  2466. end
  2467. else
  2468. Locked := false;
  2469. case DstSurface^.format.BytesPerPixel of
  2470. 1 :
  2471. begin
  2472. Row8Bit := pointer( cardinal( DstSurface^.pixels ) + UInt32( Rect^.y ) *
  2473. DstSurface^.pitch );
  2474. for y := 1 to Rect^.h do
  2475. begin
  2476. RightSide := Rect^.w - 1;
  2477. for x := 0 to FlipLength do
  2478. begin
  2479. Pixel := Row8Bit^[ x ];
  2480. Row8Bit^[ x ] := Row8Bit^[ RightSide ];
  2481. Row8Bit^[ RightSide ] := Pixel;
  2482. dec( RightSide );
  2483. end;
  2484. inc( cardinal( Row8Bit ), DstSurface^.pitch );
  2485. end;
  2486. end;
  2487. 2 :
  2488. begin
  2489. Row16Bit := pointer( cardinal( DstSurface^.pixels ) + UInt32( Rect^.y ) *
  2490. DstSurface^.pitch );
  2491. for y := 1 to Rect^.h do
  2492. begin
  2493. RightSide := Rect^.w - 1;
  2494. for x := 0 to FlipLength do
  2495. begin
  2496. Pixel := Row16Bit^[ x ];
  2497. Row16Bit^[ x ] := Row16Bit^[ RightSide ];
  2498. Row16Bit^[ RightSide ] := Pixel;
  2499. dec( RightSide );
  2500. end;
  2501. inc( cardinal( Row16Bit ), DstSurface^.pitch );
  2502. end;
  2503. end;
  2504. 3 :
  2505. begin
  2506. Row24Bit := pointer( cardinal( DstSurface^.pixels ) + UInt32( Rect^.y ) *
  2507. DstSurface^.pitch );
  2508. for y := 1 to Rect^.h do
  2509. begin
  2510. RightSide := Rect^.w - 1;
  2511. for x := 0 to FlipLength do
  2512. begin
  2513. Pixel24 := Row24Bit^[ x ];
  2514. Row24Bit^[ x ] := Row24Bit^[ RightSide ];
  2515. Row24Bit^[ RightSide ] := Pixel24;
  2516. dec( RightSide );
  2517. end;
  2518. inc( cardinal( Row24Bit ), DstSurface^.pitch );
  2519. end;
  2520. end;
  2521. 4 :
  2522. begin
  2523. Row32Bit := pointer( cardinal( DstSurface^.pixels ) + UInt32( Rect^.y ) *
  2524. DstSurface^.pitch );
  2525. for y := 1 to Rect^.h do
  2526. begin
  2527. RightSide := Rect^.w - 1;
  2528. for x := 0 to FlipLength do
  2529. begin
  2530. Pixel := Row32Bit^[ x ];
  2531. Row32Bit^[ x ] := Row32Bit^[ RightSide ];
  2532. Row32Bit^[ RightSide ] := Pixel;
  2533. dec( RightSide );
  2534. end;
  2535. inc( cardinal( Row32Bit ), DstSurface^.pitch );
  2536. end;
  2537. end;
  2538. end;
  2539. if Locked then
  2540. SDL_UnlockSurface( DstSurface );
  2541. end;
  2542. end;
  2543. // Use with caution! The procedure allocates memory for TSDL_Rect and return with its pointer.
  2544. // But you MUST free it after you don't need it anymore!!!
  2545. function PSDLRect( aLeft, aTop, aWidth, aHeight : integer ) : PSDL_Rect;
  2546. var
  2547. Rect : PSDL_Rect;
  2548. begin
  2549. New( Rect );
  2550. with Rect^ do
  2551. begin
  2552. x := aLeft;
  2553. y := aTop;
  2554. w := aWidth;
  2555. h := aHeight;
  2556. end;
  2557. Result := Rect;
  2558. end;
  2559. function SDLRect( aLeft, aTop, aWidth, aHeight : integer ) : TSDL_Rect;
  2560. begin
  2561. with result do
  2562. begin
  2563. x := aLeft;
  2564. y := aTop;
  2565. w := aWidth;
  2566. h := aHeight;
  2567. end;
  2568. end;
  2569. function SDLRect( aRect : TRect ) : TSDL_Rect;
  2570. begin
  2571. with aRect do
  2572. result := SDLRect( Left, Top, Right - Left, Bottom - Top );
  2573. end;
  2574. procedure SDL_Stretch8( Surface, Dst_Surface : PSDL_Surface; x1, x2, y1, y2, yr, yw,
  2575. depth : integer );
  2576. var
  2577. dx, dy, e, d, dx2 : integer;
  2578. src_pitch, dst_pitch : uint16;
  2579. src_pixels, dst_pixels : PUint8;
  2580. begin
  2581. if ( yw >= dst_surface^.h ) then
  2582. exit;
  2583. dx := ( x2 - x1 );
  2584. dy := ( y2 - y1 );
  2585. dy := dy shl 1;
  2586. e := dy - dx;
  2587. dx2 := dx shl 1;
  2588. src_pitch := Surface^.pitch;
  2589. dst_pitch := dst_surface^.pitch;
  2590. src_pixels := PUint8( integer( Surface^.pixels ) + yr * src_pitch + y1 * depth );
  2591. dst_pixels := PUint8( integer( dst_surface^.pixels ) + yw * dst_pitch + x1 *
  2592. depth );
  2593. for d := 0 to dx - 1 do
  2594. begin
  2595. move( src_pixels^, dst_pixels^, depth );
  2596. while ( e >= 0 ) do
  2597. begin
  2598. inc( src_pixels, depth );
  2599. e := e - dx2;
  2600. end;
  2601. inc( dst_pixels, depth );
  2602. e := e + dy;
  2603. end;
  2604. end;
  2605. function sign( x : integer ) : integer;
  2606. begin
  2607. if x > 0 then
  2608. result := 1
  2609. else
  2610. result := -1;
  2611. end;
  2612. // Stretches a part of a surface
  2613. function SDL_ScaleSurfaceRect( SrcSurface : PSDL_Surface; SrcX1, SrcY1, SrcW, SrcH,
  2614. Width, Height : integer ) : PSDL_Surface;
  2615. var
  2616. dst_surface : PSDL_Surface;
  2617. dx, dy, e, d, dx2, srcx2, srcy2 : integer;
  2618. destx1, desty1 : integer;
  2619. begin
  2620. srcx2 := srcx1 + SrcW;
  2621. srcy2 := srcy1 + SrcH;
  2622. result := nil;
  2623. destx1 := 0;
  2624. desty1 := 0;
  2625. dx := abs( integer( Height - desty1 ) );
  2626. dy := abs( integer( SrcY2 - SrcY1 ) );
  2627. e := ( dy shl 1 ) - dx;
  2628. dx2 := dx shl 1;
  2629. dy := dy shl 1;
  2630. dst_surface := SDL_CreateRGBSurface( SDL_HWPALETTE, width - destx1, Height -
  2631. desty1,
  2632. SrcSurface^.Format^.BitsPerPixel,
  2633. SrcSurface^.Format^.RMask,
  2634. SrcSurface^.Format^.GMask,
  2635. SrcSurface^.Format^.BMask,
  2636. SrcSurface^.Format^.AMask );
  2637. if ( dst_surface^.format^.BytesPerPixel = 1 ) then
  2638. SDL_SetColors( dst_surface, @SrcSurface^.format^.palette^.colors^[ 0 ], 0, 256 );
  2639. SDL_SetColorKey( dst_surface, sdl_srccolorkey, SrcSurface^.format^.colorkey );
  2640. if ( SDL_MustLock( dst_surface ) ) then
  2641. if ( SDL_LockSurface( dst_surface ) < 0 ) then
  2642. exit;
  2643. for d := 0 to dx - 1 do
  2644. begin
  2645. SDL_Stretch8( SrcSurface, dst_surface, destx1, Width, SrcX1, SrcX2, SrcY1, desty1,
  2646. SrcSurface^.format^.BytesPerPixel );
  2647. while e >= 0 do
  2648. begin
  2649. inc( SrcY1 );
  2650. e := e - dx2;
  2651. end;
  2652. inc( desty1 );
  2653. e := e + dy;
  2654. end;
  2655. if SDL_MUSTLOCK( dst_surface ) then
  2656. SDL_UnlockSurface( dst_surface );
  2657. result := dst_surface;
  2658. end;
  2659. procedure SDL_ScrollY( DstSurface : PSDL_Surface; DifY : integer );
  2660. var
  2661. r1, r2 : TSDL_Rect;
  2662. //buffer: PSDL_Surface;
  2663. YPos : Integer;
  2664. begin
  2665. if ( DstSurface <> nil ) and ( DifY <> 0 ) then
  2666. begin
  2667. //if DifY > 0 then // going up
  2668. //begin
  2669. ypos := 0;
  2670. r1.x := 0;
  2671. r2.x := 0;
  2672. r1.w := DstSurface.w;
  2673. r2.w := DstSurface.w;
  2674. r1.h := DifY;
  2675. r2.h := DifY;
  2676. while ypos < DstSurface.h do
  2677. begin
  2678. r1.y := ypos;
  2679. r2.y := ypos + DifY;
  2680. SDL_BlitSurface( DstSurface, @r2, DstSurface, @r1 );
  2681. ypos := ypos + DifY;
  2682. end;
  2683. //end
  2684. //else
  2685. //begin // Going Down
  2686. //end;
  2687. end;
  2688. end;
  2689. procedure SDL_ScrollX( DstSurface : PSDL_Surface; DifX : integer );
  2690. var
  2691. r1, r2 : TSDL_Rect;
  2692. buffer : PSDL_Surface;
  2693. begin
  2694. if ( DstSurface <> nil ) and ( DifX <> 0 ) then
  2695. begin
  2696. buffer := SDL_CreateRGBSurface( SDL_HWSURFACE, ( DstSurface^.w - DifX ) * 2,
  2697. DstSurface^.h * 2,
  2698. DstSurface^.Format^.BitsPerPixel,
  2699. DstSurface^.Format^.RMask,
  2700. DstSurface^.Format^.GMask,
  2701. DstSurface^.Format^.BMask,
  2702. DstSurface^.Format^.AMask );
  2703. if buffer <> nil then
  2704. begin
  2705. if ( buffer^.format^.BytesPerPixel = 1 ) then
  2706. SDL_SetColors( buffer, @DstSurface^.format^.palette^.colors^[ 0 ], 0, 256 );
  2707. r1 := SDLRect( DifX, 0, buffer^.w, buffer^.h );
  2708. r2 := SDLRect( 0, 0, buffer^.w, buffer^.h );
  2709. SDL_BlitSurface( DstSurface, @r1, buffer, @r2 );
  2710. SDL_BlitSurface( buffer, @r2, DstSurface, @r2 );
  2711. SDL_FreeSurface( buffer );
  2712. end;
  2713. end;
  2714. end;
  2715. procedure SDL_RotateRad( DstSurface, SrcSurface : PSDL_Surface; SrcRect :
  2716. PSDL_Rect; DestX, DestY, OffsetX, OffsetY : Integer; Angle : Single );
  2717. var
  2718. aSin, aCos : Single;
  2719. MX, MY, DX, DY, NX, NY, SX, SY, OX, OY, Width, Height, TX, TY, RX, RY, ROX, ROY : Integer;
  2720. Colour, TempTransparentColour : UInt32;
  2721. MAXX, MAXY : Integer;
  2722. begin
  2723. // Rotate the surface to the target surface.
  2724. TempTransparentColour := SrcSurface.format.colorkey;
  2725. if srcRect.w > srcRect.h then
  2726. begin
  2727. Width := srcRect.w;
  2728. Height := srcRect.w;
  2729. end
  2730. else
  2731. begin
  2732. Width := srcRect.h;
  2733. Height := srcRect.h;
  2734. end;
  2735. maxx := DstSurface.w;
  2736. maxy := DstSurface.h;
  2737. aCos := cos( Angle );
  2738. aSin := sin( Angle );
  2739. Width := round( abs( srcrect.h * acos ) + abs( srcrect.w * asin ) );
  2740. Height := round( abs( srcrect.h * asin ) + abs( srcrect.w * acos ) );
  2741. OX := Width div 2;
  2742. OY := Height div 2; ;
  2743. MX := ( srcRect.x + ( srcRect.x + srcRect.w ) ) div 2;
  2744. MY := ( srcRect.y + ( srcRect.y + srcRect.h ) ) div 2;
  2745. ROX := ( -( srcRect.w div 2 ) ) + Offsetx;
  2746. ROY := ( -( srcRect.h div 2 ) ) + OffsetY;
  2747. Tx := ox + round( ROX * aSin - ROY * aCos );
  2748. Ty := oy + round( ROY * aSin + ROX * aCos );
  2749. SX := 0;
  2750. for DX := DestX - TX to DestX - TX + ( width ) do
  2751. begin
  2752. Inc( SX );
  2753. SY := 0;
  2754. for DY := DestY - TY to DestY - TY + ( Height ) do
  2755. begin
  2756. RX := SX - OX;
  2757. RY := SY - OY;
  2758. NX := round( mx + RX * aSin + RY * aCos ); //
  2759. NY := round( my + RY * aSin - RX * aCos ); //
  2760. // Used for testing only
  2761. //SDL_PutPixel(DstSurface.SDLSurfacePointer,DX,DY,0);
  2762. if ( ( DX > 0 ) and ( DX < MAXX ) ) and ( ( DY > 0 ) and ( DY < MAXY ) ) then
  2763. begin
  2764. if ( NX >= srcRect.x ) and ( NX <= srcRect.x + srcRect.w ) then
  2765. begin
  2766. if ( NY >= srcRect.y ) and ( NY <= srcRect.y + srcRect.h ) then
  2767. begin
  2768. Colour := SDL_GetPixel( SrcSurface, NX, NY );
  2769. if Colour <> TempTransparentColour then
  2770. begin
  2771. SDL_PutPixel( DstSurface, DX, DY, Colour );
  2772. end;
  2773. end;
  2774. end;
  2775. end;
  2776. inc( SY );
  2777. end;
  2778. end;
  2779. end;
  2780. procedure SDL_RotateDeg( DstSurface, SrcSurface : PSDL_Surface; SrcRect :
  2781. PSDL_Rect; DestX, DestY, OffsetX, OffsetY : Integer; Angle : Integer );
  2782. begin
  2783. SDL_RotateRad( DstSurface, SrcSurface, SrcRect, DestX, DestY, OffsetX, OffsetY, DegToRad( Angle ) );
  2784. end;
  2785. function ValidateSurfaceRect( DstSurface : PSDL_Surface; dstrect : PSDL_Rect ) : TSDL_Rect;
  2786. var
  2787. RealRect : TSDL_Rect;
  2788. OutOfRange : Boolean;
  2789. begin
  2790. OutOfRange := false;
  2791. if dstrect = nil then
  2792. begin
  2793. RealRect.x := 0;
  2794. RealRect.y := 0;
  2795. RealRect.w := DstSurface.w;
  2796. RealRect.h := DstSurface.h;
  2797. end
  2798. else
  2799. begin
  2800. if dstrect.x < DstSurface.w then
  2801. begin
  2802. RealRect.x := dstrect.x;
  2803. end
  2804. else if dstrect.x < 0 then
  2805. begin
  2806. realrect.x := 0;
  2807. end
  2808. else
  2809. begin
  2810. OutOfRange := True;
  2811. end;
  2812. if dstrect.y < DstSurface.h then
  2813. begin
  2814. RealRect.y := dstrect.y;
  2815. end
  2816. else if dstrect.y < 0 then
  2817. begin
  2818. realrect.y := 0;
  2819. end
  2820. else
  2821. begin
  2822. OutOfRange := True;
  2823. end;
  2824. if OutOfRange = False then
  2825. begin
  2826. if realrect.x + dstrect.w <= DstSurface.w then
  2827. begin
  2828. RealRect.w := dstrect.w;
  2829. end
  2830. else
  2831. begin
  2832. RealRect.w := dstrect.w - realrect.x;
  2833. end;
  2834. if realrect.y + dstrect.h <= DstSurface.h then
  2835. begin
  2836. RealRect.h := dstrect.h;
  2837. end
  2838. else
  2839. begin
  2840. RealRect.h := dstrect.h - realrect.y;
  2841. end;
  2842. end;
  2843. end;
  2844. if OutOfRange = False then
  2845. begin
  2846. result := realrect;
  2847. end
  2848. else
  2849. begin
  2850. realrect.w := 0;
  2851. realrect.h := 0;
  2852. realrect.x := 0;
  2853. realrect.y := 0;
  2854. result := realrect;
  2855. end;
  2856. end;
  2857. procedure SDL_FillRectAdd( DstSurface : PSDL_Surface; dstrect : PSDL_Rect; color : UInt32 );
  2858. var
  2859. RealRect : TSDL_Rect;
  2860. Addr : pointer;
  2861. ModX, BPP : cardinal;
  2862. x, y, R, G, B, SrcColor : cardinal;
  2863. begin
  2864. RealRect := ValidateSurfaceRect( DstSurface, DstRect );
  2865. if ( RealRect.w > 0 ) and ( RealRect.h > 0 ) then
  2866. begin
  2867. SDL_LockSurface( DstSurface );
  2868. BPP := DstSurface.format.BytesPerPixel;
  2869. with DstSurface^ do
  2870. begin
  2871. Addr := pointer( UInt32( pixels ) + UInt32( RealRect.y ) * pitch + UInt32( RealRect.x ) * BPP );
  2872. ModX := Pitch - UInt32( RealRect.w ) * BPP;
  2873. end;
  2874. case DstSurface.format.BitsPerPixel of
  2875. 8 :
  2876. begin
  2877. for y := 0 to RealRect.h - 1 do
  2878. begin
  2879. for x := 0 to RealRect.w - 1 do
  2880. begin
  2881. SrcColor := PUInt32( Addr )^;
  2882. R := SrcColor and $E0 + Color and $E0;
  2883. G := SrcColor and $1C + Color and $1C;
  2884. B := SrcColor and $03 + Color and $03;
  2885. if R > $E0 then
  2886. R := $E0;
  2887. if G > $1C then
  2888. G := $1C;
  2889. if B > $03 then
  2890. B := $03;
  2891. PUInt8( Addr )^ := R or G or B;
  2892. inc( UInt32( Addr ), BPP );
  2893. end;
  2894. inc( UInt32( Addr ), ModX );
  2895. end;
  2896. end;
  2897. 15 :
  2898. begin
  2899. for y := 0 to RealRect.h - 1 do
  2900. begin
  2901. for x := 0 to RealRect.w - 1 do
  2902. begin
  2903. SrcColor := PUInt32( Addr )^;
  2904. R := SrcColor and $7C00 + Color and $7C00;
  2905. G := SrcColor and $03E0 + Color and $03E0;
  2906. B := SrcColor and $001F + Color and $001F;
  2907. if R > $7C00 then
  2908. R := $7C00;
  2909. if G > $03E0 then
  2910. G := $03E0;
  2911. if B > $001F then
  2912. B := $001F;
  2913. PUInt16( Addr )^ := R or G or B;
  2914. inc( UInt32( Addr ), BPP );
  2915. end;
  2916. inc( UInt32( Addr ), ModX );
  2917. end;
  2918. end;
  2919. 16 :
  2920. begin
  2921. for y := 0 to RealRect.h - 1 do
  2922. begin
  2923. for x := 0 to RealRect.w - 1 do
  2924. begin
  2925. SrcColor := PUInt32( Addr )^;
  2926. R := SrcColor and $F800 + Color and $F800;
  2927. G := SrcColor and $07C0 + Color and $07C0;
  2928. B := SrcColor and $001F + Color and $001F;
  2929. if R > $F800 then
  2930. R := $F800;
  2931. if G > $07C0 then
  2932. G := $07C0;
  2933. if B > $001F then
  2934. B := $001F;
  2935. PUInt16( Addr )^ := R or G or B;
  2936. inc( UInt32( Addr ), BPP );
  2937. end;
  2938. inc( UInt32( Addr ), ModX );
  2939. end;
  2940. end;
  2941. 24 :
  2942. begin
  2943. for y := 0 to RealRect.h - 1 do
  2944. begin
  2945. for x := 0 to RealRect.w - 1 do
  2946. begin
  2947. SrcColor := PUInt32( Addr )^;
  2948. R := SrcColor and $00FF0000 + Color and $00FF0000;
  2949. G := SrcColor and $0000FF00 + Color and $0000FF00;
  2950. B := SrcColor and $000000FF + Color and $000000FF;
  2951. if R > $FF0000 then
  2952. R := $FF0000;
  2953. if G > $00FF00 then
  2954. G := $00FF00;
  2955. if B > $0000FF then
  2956. B := $0000FF;
  2957. PUInt32( Addr )^ := SrcColor and $FF000000 or R or G or B;
  2958. inc( UInt32( Addr ), BPP );
  2959. end;
  2960. inc( UInt32( Addr ), ModX );
  2961. end;
  2962. end;
  2963. 32 :
  2964. begin
  2965. for y := 0 to RealRect.h - 1 do
  2966. begin
  2967. for x := 0 to RealRect.w - 1 do
  2968. begin
  2969. SrcColor := PUInt32( Addr )^;
  2970. R := SrcColor and $00FF0000 + Color and $00FF0000;
  2971. G := SrcColor and $0000FF00 + Color and $0000FF00;
  2972. B := SrcColor and $000000FF + Color and $000000FF;
  2973. if R > $FF0000 then
  2974. R := $FF0000;
  2975. if G > $00FF00 then
  2976. G := $00FF00;
  2977. if B > $0000FF then
  2978. B := $0000FF;
  2979. PUInt32( Addr )^ := R or G or B;
  2980. inc( UInt32( Addr ), BPP );
  2981. end;
  2982. inc( UInt32( Addr ), ModX );
  2983. end;
  2984. end;
  2985. end;
  2986. SDL_UnlockSurface( DstSurface );
  2987. end;
  2988. end;
  2989. procedure SDL_FillRectSub( DstSurface : PSDL_Surface; dstrect : PSDL_Rect; color : UInt32 );
  2990. var
  2991. RealRect : TSDL_Rect;
  2992. Addr : pointer;
  2993. ModX, BPP : cardinal;
  2994. x, y, R, G, B, SrcColor : cardinal;
  2995. begin
  2996. RealRect := ValidateSurfaceRect( DstSurface, DstRect );
  2997. if ( RealRect.w > 0 ) and ( RealRect.h > 0 ) then
  2998. begin
  2999. SDL_LockSurface( DstSurface );
  3000. BPP := DstSurface.format.BytesPerPixel;
  3001. with DstSurface^ do
  3002. begin
  3003. Addr := pointer( UInt32( pixels ) + UInt32( RealRect.y ) * pitch + UInt32( RealRect.x ) * BPP );
  3004. ModX := Pitch - UInt32( RealRect.w ) * BPP;
  3005. end;
  3006. case DstSurface.format.BitsPerPixel of
  3007. 8 :
  3008. begin
  3009. for y := 0 to RealRect.h - 1 do
  3010. begin
  3011. for x := 0 to RealRect.w - 1 do
  3012. begin
  3013. SrcColor := PUInt32( Addr )^;
  3014. R := SrcColor and $E0 - Color and $E0;
  3015. G := SrcColor and $1C - Color and $1C;
  3016. B := SrcColor and $03 - Color and $03;
  3017. if R > $E0 then
  3018. R := 0;
  3019. if G > $1C then
  3020. G := 0;
  3021. if B > $03 then
  3022. B := 0;
  3023. PUInt8( Addr )^ := R or G or B;
  3024. inc( UInt32( Addr ), BPP );
  3025. end;
  3026. inc( UInt32( Addr ), ModX );
  3027. end;
  3028. end;
  3029. 15 :
  3030. begin
  3031. for y := 0 to RealRect.h - 1 do
  3032. begin
  3033. for x := 0 to RealRect.w - 1 do
  3034. begin
  3035. SrcColor := PUInt32( Addr )^;
  3036. R := SrcColor and $7C00 - Color and $7C00;
  3037. G := SrcColor and $03E0 - Color and $03E0;
  3038. B := SrcColor and $001F - Color and $001F;
  3039. if R > $7C00 then
  3040. R := 0;
  3041. if G > $03E0 then
  3042. G := 0;
  3043. if B > $001F then
  3044. B := 0;
  3045. PUInt16( Addr )^ := R or G or B;
  3046. inc( UInt32( Addr ), BPP );
  3047. end;
  3048. inc( UInt32( Addr ), ModX );
  3049. end;
  3050. end;
  3051. 16 :
  3052. begin
  3053. for y := 0 to RealRect.h - 1 do
  3054. begin
  3055. for x := 0 to RealRect.w - 1 do
  3056. begin
  3057. SrcColor := PUInt32( Addr )^;
  3058. R := SrcColor and $F800 - Color and $F800;
  3059. G := SrcColor and $07C0 - Color and $07C0;
  3060. B := SrcColor and $001F - Color and $001F;
  3061. if R > $F800 then
  3062. R := 0;
  3063. if G > $07C0 then
  3064. G := 0;
  3065. if B > $001F then
  3066. B := 0;
  3067. PUInt16( Addr )^ := R or G or B;
  3068. inc( UInt32( Addr ), BPP );
  3069. end;
  3070. inc( UInt32( Addr ), ModX );
  3071. end;
  3072. end;
  3073. 24 :
  3074. begin
  3075. for y := 0 to RealRect.h - 1 do
  3076. begin
  3077. for x := 0 to RealRect.w - 1 do
  3078. begin
  3079. SrcColor := PUInt32( Addr )^;
  3080. R := SrcColor and $00FF0000 - Color and $00FF0000;
  3081. G := SrcColor and $0000FF00 - Color and $0000FF00;
  3082. B := SrcColor and $000000FF - Color and $000000FF;
  3083. if R > $FF0000 then
  3084. R := 0;
  3085. if G > $00FF00 then
  3086. G := 0;
  3087. if B > $0000FF then
  3088. B := 0;
  3089. PUInt32( Addr )^ := SrcColor and $FF000000 or R or G or B;
  3090. inc( UInt32( Addr ), BPP );
  3091. end;
  3092. inc( UInt32( Addr ), ModX );
  3093. end;
  3094. end;
  3095. 32 :
  3096. begin
  3097. for y := 0 to RealRect.h - 1 do
  3098. begin
  3099. for x := 0 to RealRect.w - 1 do
  3100. begin
  3101. SrcColor := PUInt32( Addr )^;
  3102. R := SrcColor and $00FF0000 - Color and $00FF0000;
  3103. G := SrcColor and $0000FF00 - Color and $0000FF00;
  3104. B := SrcColor and $000000FF - Color and $000000FF;
  3105. if R > $FF0000 then
  3106. R := 0;
  3107. if G > $00FF00 then
  3108. G := 0;
  3109. if B > $0000FF then
  3110. B := 0;
  3111. PUInt32( Addr )^ := R or G or B;
  3112. inc( UInt32( Addr ), BPP );
  3113. end;
  3114. inc( UInt32( Addr ), ModX );
  3115. end;
  3116. end;
  3117. end;
  3118. SDL_UnlockSurface( DstSurface );
  3119. end;
  3120. end;
  3121. procedure SDL_GradientFillRect( DstSurface : PSDL_Surface; const Rect : PSDL_Rect; const StartColor, EndColor : TSDL_Color; const Style : TGradientStyle );
  3122. var
  3123. FBC : array[ 0..255 ] of Cardinal;
  3124. // temp vars
  3125. i, YR, YG, YB, SR, SG, SB, DR, DG, DB : Integer;
  3126. TempStepV, TempStepH : Single;
  3127. TempLeft, TempTop, TempHeight, TempWidth : integer;
  3128. TempRect : TSDL_Rect;
  3129. begin
  3130. // calc FBC
  3131. YR := StartColor.r;
  3132. YG := StartColor.g;
  3133. YB := StartColor.b;
  3134. SR := YR;
  3135. SG := YG;
  3136. SB := YB;
  3137. DR := EndColor.r - SR;
  3138. DG := EndColor.g - SG;
  3139. DB := EndColor.b - SB;
  3140. for i := 0 to 255 do
  3141. begin
  3142. FBC[ i ] := SDL_MapRGB( DstSurface.format, YR, YG, YB );
  3143. YR := SR + round( DR / 255 * i );
  3144. YG := SG + round( DG / 255 * i );
  3145. YB := SB + round( DB / 255 * i );
  3146. end;
  3147. // if aStyle = 1 then begin
  3148. TempStepH := Rect.w / 255;
  3149. TempStepV := Rect.h / 255;
  3150. TempHeight := Trunc( TempStepV + 1 );
  3151. TempWidth := Trunc( TempStepH + 1 );
  3152. TempTop := 0;
  3153. TempLeft := 0;
  3154. TempRect.x := Rect.x;
  3155. TempRect.y := Rect.y;
  3156. TempRect.h := Rect.h;
  3157. TempRect.w := Rect.w;
  3158. case Style of
  3159. gsHorizontal :
  3160. begin
  3161. TempRect.h := TempHeight;
  3162. for i := 0 to 255 do
  3163. begin
  3164. TempRect.y := Rect.y + TempTop;
  3165. SDL_FillRect( DstSurface, @TempRect, FBC[ i ] );
  3166. TempTop := Trunc( TempStepV * i );
  3167. end;
  3168. end;
  3169. gsVertical :
  3170. begin
  3171. TempRect.w := TempWidth;
  3172. for i := 0 to 255 do
  3173. begin
  3174. TempRect.x := Rect.x + TempLeft;
  3175. SDL_FillRect( DstSurface, @TempRect, FBC[ i ] );
  3176. TempLeft := Trunc( TempStepH * i );
  3177. end;
  3178. end;
  3179. end;
  3180. end;
  3181. procedure SDL_2xBlit( Src, Dest : PSDL_Surface );
  3182. var
  3183. ReadAddr, WriteAddr, ReadRow, WriteRow : UInt32;
  3184. SrcPitch, DestPitch, x, y, w, h : UInt32;
  3185. begin
  3186. if ( Src = nil ) or ( Dest = nil ) then
  3187. exit;
  3188. if ( Src.w shl 1 ) < Dest.w then
  3189. exit;
  3190. if ( Src.h shl 1 ) < Dest.h then
  3191. exit;
  3192. if SDL_MustLock( Src ) then
  3193. SDL_LockSurface( Src );
  3194. if SDL_MustLock( Dest ) then
  3195. SDL_LockSurface( Dest );
  3196. ReadRow := UInt32( Src.Pixels );
  3197. WriteRow := UInt32( Dest.Pixels );
  3198. SrcPitch := Src.pitch;
  3199. DestPitch := Dest.pitch;
  3200. w := Src.w;
  3201. h := Src.h;
  3202. case Src.format.BytesPerPixel of
  3203. 1 :
  3204. asm
  3205. push ebx
  3206. mov eax, h // for y := 1 to Src.h do
  3207. mov y, eax
  3208. @LoopY:
  3209. mov eax, ReadRow // ReadAddr := ReadRow;
  3210. mov ReadAddr, eax
  3211. mov eax, WriteRow // WriteAddr := WriteRow;
  3212. mov WriteAddr, eax
  3213. mov eax, w // for x := 1 to Src.w do
  3214. mov x, eax
  3215. mov ecx, ReadAddr
  3216. mov edx, WriteAddr
  3217. mov ebx, DestPitch
  3218. @LoopX:
  3219. mov al, [ecx] // PUInt8(WriteAddr)^ := PUInt8(ReadAddr)^;
  3220. mov [edx], al
  3221. mov [edx + 1], al // PUInt8(WriteAddr + 1)^ := PUInt8(ReadAddr)^;
  3222. mov [edx + ebx], al // PUInt8(WriteAddr + DestPitch)^ := PUInt8(ReadAddr)^;
  3223. mov [edx + ebx + 1], al // PUInt8(WriteAddr + DestPitch + 1)^ := PUInt8(ReadAddr)^;
  3224. inc ecx // inc(ReadAddr);
  3225. add edx, 2 // inc(WriteAddr, 2);
  3226. dec x
  3227. jnz @LoopX
  3228. mov eax, SrcPitch // inc(UInt32(ReadRow), SrcPitch);
  3229. add ReadRow, eax
  3230. mov eax, DestPitch // inc(UInt32(WriteRow), DestPitch * 2);
  3231. add WriteRow, eax
  3232. add WriteRow, eax
  3233. dec y
  3234. jnz @LoopY
  3235. pop ebx
  3236. end;
  3237. 2 :
  3238. asm
  3239. push ebx
  3240. mov eax, h // for y := 1 to Src.h do
  3241. mov y, eax
  3242. @LoopY:
  3243. mov eax, ReadRow // ReadAddr := ReadRow;
  3244. mov ReadAddr, eax
  3245. mov eax, WriteRow // WriteAddr := WriteRow;
  3246. mov WriteAddr, eax
  3247. mov eax, w // for x := 1 to Src.w do
  3248. mov x, eax
  3249. mov ecx, ReadAddr
  3250. mov edx, WriteAddr
  3251. mov ebx, DestPitch
  3252. @LoopX:
  3253. mov ax, [ecx] // PUInt16(WriteAddr)^ := PUInt16(ReadAddr)^;
  3254. mov [edx], ax
  3255. mov [edx + 2], ax // PUInt16(WriteAddr + 2)^ := PUInt16(ReadAddr)^;
  3256. mov [edx + ebx], ax // PUInt16(WriteAddr + DestPitch)^ := PUInt16(ReadAddr)^;
  3257. mov [edx + ebx + 2], ax // PUInt16(WriteAddr + DestPitch + 2)^ := PUInt16(ReadAddr)^;
  3258. add ecx, 2 // inc(ReadAddr, 2);
  3259. add edx, 4 // inc(WriteAddr, 4);
  3260. dec x
  3261. jnz @LoopX
  3262. mov eax, SrcPitch // inc(UInt32(ReadRow), SrcPitch);
  3263. add ReadRow, eax
  3264. mov eax, DestPitch // inc(UInt32(WriteRow), DestPitch * 2);
  3265. add WriteRow, eax
  3266. add WriteRow, eax
  3267. dec y
  3268. jnz @LoopY
  3269. pop ebx
  3270. end;
  3271. 3 :
  3272. asm
  3273. push ebx
  3274. mov eax, h // for y := 1 to Src.h do
  3275. mov y, eax
  3276. @LoopY:
  3277. mov eax, ReadRow // ReadAddr := ReadRow;
  3278. mov ReadAddr, eax
  3279. mov eax, WriteRow // WriteAddr := WriteRow;
  3280. mov WriteAddr, eax
  3281. mov eax, w // for x := 1 to Src.w do
  3282. mov x, eax
  3283. mov ecx, ReadAddr
  3284. mov edx, WriteAddr
  3285. mov ebx, DestPitch
  3286. @LoopX:
  3287. mov eax, [ecx] // (PUInt32(WriteAddr)^ and $ff000000) or (PUInt32(ReadAddr)^ and $00ffffff);
  3288. and eax, $00ffffff
  3289. and dword ptr [edx], $ff000000
  3290. or [edx], eax
  3291. and dword ptr [edx + 3], $00ffffff // (PUInt32(WriteAddr + 3)^ and $ff000000) or (PUInt32(ReadAddr)^ and $00ffffff);
  3292. or [edx + 3], eax
  3293. and dword ptr [edx + ebx], $00ffffff // (PUInt32(WriteAddr + DestPitch)^ and $ff000000) or (PUInt32(ReadAddr)^ and $00ffffff);
  3294. or [edx + ebx], eax
  3295. and dword ptr [edx + ebx + 3], $00ffffff // (PUInt32(WriteAddr + DestPitch + 3)^ and $ff000000) or (PUInt32(ReadAddr)^ and $00ffffff);
  3296. or [edx + ebx + 3], eax
  3297. add ecx, 3 // inc(ReadAddr, 3);
  3298. add edx, 6 // inc(WriteAddr, 6);
  3299. dec x
  3300. jnz @LoopX
  3301. mov eax, SrcPitch // inc(UInt32(ReadRow), SrcPitch);
  3302. add ReadRow, eax
  3303. mov eax, DestPitch // inc(UInt32(WriteRow), DestPitch * 2);
  3304. add WriteRow, eax
  3305. add WriteRow, eax
  3306. dec y
  3307. jnz @LoopY
  3308. pop ebx
  3309. end;
  3310. 4 :
  3311. asm
  3312. push ebx
  3313. mov eax, h // for y := 1 to Src.h do
  3314. mov y, eax
  3315. @LoopY:
  3316. mov eax, ReadRow // ReadAddr := ReadRow;
  3317. mov ReadAddr, eax
  3318. mov eax, WriteRow // WriteAddr := WriteRow;
  3319. mov WriteAddr, eax
  3320. mov eax, w // for x := 1 to Src.w do
  3321. mov x, eax
  3322. mov ecx, ReadAddr
  3323. mov edx, WriteAddr
  3324. mov ebx, DestPitch
  3325. @LoopX:
  3326. mov eax, [ecx] // PUInt32(WriteAddr)^ := PUInt32(ReadAddr)^;
  3327. mov [edx], eax
  3328. mov [edx + 4], eax // PUInt32(WriteAddr + 4)^ := PUInt32(ReadAddr)^;
  3329. mov [edx + ebx], eax // PUInt32(WriteAddr + DestPitch)^ := PUInt32(ReadAddr)^;
  3330. mov [edx + ebx + 4], eax // PUInt32(WriteAddr + DestPitch + 4)^ := PUInt32(ReadAddr)^;
  3331. add ecx, 4 // inc(ReadAddr, 4);
  3332. add edx, 8 // inc(WriteAddr, 8);
  3333. dec x
  3334. jnz @LoopX
  3335. mov eax, SrcPitch // inc(UInt32(ReadRow), SrcPitch);
  3336. add ReadRow, eax
  3337. mov eax, DestPitch // inc(UInt32(WriteRow), DestPitch * 2);
  3338. add WriteRow, eax
  3339. add WriteRow, eax
  3340. dec y
  3341. jnz @LoopY
  3342. pop ebx
  3343. end;
  3344. end;
  3345. if SDL_MustLock( Src ) then
  3346. SDL_UnlockSurface( Src );
  3347. if SDL_MustLock( Dest ) then
  3348. SDL_UnlockSurface( Dest );
  3349. end;
  3350. procedure SDL_Scanline2xBlit( Src, Dest : PSDL_Surface );
  3351. var
  3352. ReadAddr, WriteAddr, ReadRow, WriteRow : UInt32;
  3353. SrcPitch, DestPitch, x, y, w, h : UInt32;
  3354. begin
  3355. if ( Src = nil ) or ( Dest = nil ) then
  3356. exit;
  3357. if ( Src.w shl 1 ) < Dest.w then
  3358. exit;
  3359. if ( Src.h shl 1 ) < Dest.h then
  3360. exit;
  3361. if SDL_MustLock( Src ) then
  3362. SDL_LockSurface( Src );
  3363. if SDL_MustLock( Dest ) then
  3364. SDL_LockSurface( Dest );
  3365. ReadRow := UInt32( Src.Pixels );
  3366. WriteRow := UInt32( Dest.Pixels );
  3367. SrcPitch := Src.pitch;
  3368. DestPitch := Dest.pitch;
  3369. w := Src.w;
  3370. h := Src.h;
  3371. case Src.format.BytesPerPixel of
  3372. 1 :
  3373. asm
  3374. push ebx
  3375. mov eax, h // for y := 1 to Src.h do
  3376. mov y, eax
  3377. @LoopY:
  3378. mov eax, ReadRow // ReadAddr := ReadRow;
  3379. mov ReadAddr, eax
  3380. mov eax, WriteRow // WriteAddr := WriteRow;
  3381. mov WriteAddr, eax
  3382. mov eax, w // for x := 1 to Src.w do
  3383. mov x, eax
  3384. mov ecx, ReadAddr
  3385. mov edx, WriteAddr
  3386. @LoopX:
  3387. mov al, [ecx] // PUInt8(WriteAddr)^ := PUInt8(ReadAddr)^;
  3388. mov [edx], al
  3389. mov [edx + 1], al // PUInt8(WriteAddr + 1)^ := PUInt8(ReadAddr)^;
  3390. inc ecx // inc(ReadAddr);
  3391. add edx, 2 // inc(WriteAddr, 2);
  3392. dec x
  3393. jnz @LoopX
  3394. mov eax, SrcPitch // inc(UInt32(ReadRow), SrcPitch);
  3395. add ReadRow, eax
  3396. mov eax, DestPitch // inc(UInt32(WriteRow), DestPitch * 2);
  3397. add WriteRow, eax
  3398. add WriteRow, eax
  3399. dec y
  3400. jnz @LoopY
  3401. pop ebx
  3402. end;
  3403. 2 :
  3404. asm
  3405. push ebx
  3406. mov eax, h // for y := 1 to Src.h do
  3407. mov y, eax
  3408. @LoopY:
  3409. mov eax, ReadRow // ReadAddr := ReadRow;
  3410. mov ReadAddr, eax
  3411. mov eax, WriteRow // WriteAddr := WriteRow;
  3412. mov WriteAddr, eax
  3413. mov eax, w // for x := 1 to Src.w do
  3414. mov x, eax
  3415. mov ecx, ReadAddr
  3416. mov edx, WriteAddr
  3417. @LoopX:
  3418. mov ax, [ecx] // PUInt16(WriteAddr)^ := PUInt16(ReadAddr)^;
  3419. mov [edx], ax
  3420. mov [edx + 2], eax // PUInt16(WriteAddr + 2)^ := PUInt16(ReadAddr)^;
  3421. add ecx, 2 // inc(ReadAddr, 2);
  3422. add edx, 4 // inc(WriteAddr, 4);
  3423. dec x
  3424. jnz @LoopX
  3425. mov eax, SrcPitch // inc(UInt32(ReadRow), SrcPitch);
  3426. add ReadRow, eax
  3427. mov eax, DestPitch // inc(UInt32(WriteRow), DestPitch * 2);
  3428. add WriteRow, eax
  3429. add WriteRow, eax
  3430. dec y
  3431. jnz @LoopY
  3432. pop ebx
  3433. end;
  3434. 3 :
  3435. asm
  3436. push ebx
  3437. mov eax, h // for y := 1 to Src.h do
  3438. mov y, eax
  3439. @LoopY:
  3440. mov eax, ReadRow // ReadAddr := ReadRow;
  3441. mov ReadAddr, eax
  3442. mov eax, WriteRow // WriteAddr := WriteRow;
  3443. mov WriteAddr, eax
  3444. mov eax, w // for x := 1 to Src.w do
  3445. mov x, eax
  3446. mov ecx, ReadAddr
  3447. mov edx, WriteAddr
  3448. @LoopX:
  3449. mov eax, [ecx] // (PUInt32(WriteAddr)^ and $ff000000) or (PUInt32(ReadAddr)^ and $00ffffff);
  3450. and eax, $00ffffff
  3451. and dword ptr [edx], $ff000000
  3452. or [edx], eax
  3453. and dword ptr [edx + 3], $00ffffff // (PUInt32(WriteAddr + 3)^ and $ff000000) or (PUInt32(ReadAddr)^ and $00ffffff);
  3454. or [edx + 3], eax
  3455. add ecx, 3 // inc(ReadAddr, 3);
  3456. add edx, 6 // inc(WriteAddr, 6);
  3457. dec x
  3458. jnz @LoopX
  3459. mov eax, SrcPitch // inc(UInt32(ReadRow), SrcPitch);
  3460. add ReadRow, eax
  3461. mov eax, DestPitch // inc(UInt32(WriteRow), DestPitch * 2);
  3462. add WriteRow, eax
  3463. add WriteRow, eax
  3464. dec y
  3465. jnz @LoopY
  3466. pop ebx
  3467. end;
  3468. 4 :
  3469. asm
  3470. push ebx
  3471. mov eax, h // for y := 1 to Src.h do
  3472. mov y, eax
  3473. @LoopY:
  3474. mov eax, ReadRow // ReadAddr := ReadRow;
  3475. mov ReadAddr, eax
  3476. mov eax, WriteRow // WriteAddr := WriteRow;
  3477. mov WriteAddr, eax
  3478. mov eax, w // for x := 1 to Src.w do
  3479. mov x, eax
  3480. mov ecx, ReadAddr
  3481. mov edx, WriteAddr
  3482. @LoopX:
  3483. mov eax, [ecx] // PUInt32(WriteAddr)^ := PUInt32(ReadAddr)^;
  3484. mov [edx], eax
  3485. mov [edx + 4], eax // PUInt32(WriteAddr + 4)^ := PUInt32(ReadAddr)^;
  3486. add ecx, 4 // inc(ReadAddr, 4);
  3487. add edx, 8 // inc(WriteAddr, 8);
  3488. dec x
  3489. jnz @LoopX
  3490. mov eax, SrcPitch // inc(UInt32(ReadRow), SrcPitch);
  3491. add ReadRow, eax
  3492. mov eax, DestPitch // inc(UInt32(WriteRow), DestPitch * 2);
  3493. add WriteRow, eax
  3494. add WriteRow, eax
  3495. dec y
  3496. jnz @LoopY
  3497. pop ebx
  3498. end;
  3499. end;
  3500. if SDL_MustLock( Src ) then
  3501. SDL_UnlockSurface( Src );
  3502. if SDL_MustLock( Dest ) then
  3503. SDL_UnlockSurface( Dest );
  3504. end;
  3505. procedure SDL_50Scanline2xBlit( Src, Dest : PSDL_Surface );
  3506. var
  3507. ReadAddr, WriteAddr, ReadRow, WriteRow : UInt32;
  3508. SrcPitch, DestPitch, x, y, w, h : UInt32;
  3509. begin
  3510. if ( Src = nil ) or ( Dest = nil ) then
  3511. exit;
  3512. if ( Src.w shl 1 ) < Dest.w then
  3513. exit;
  3514. if ( Src.h shl 1 ) < Dest.h then
  3515. exit;
  3516. if SDL_MustLock( Src ) then
  3517. SDL_LockSurface( Src );
  3518. if SDL_MustLock( Dest ) then
  3519. SDL_LockSurface( Dest );
  3520. ReadRow := UInt32( Src.Pixels );
  3521. WriteRow := UInt32( Dest.Pixels );
  3522. SrcPitch := Src.pitch;
  3523. DestPitch := Dest.pitch;
  3524. w := Src.w;
  3525. h := Src.h;
  3526. case Src.format.BitsPerPixel of
  3527. 8 :
  3528. asm
  3529. push ebx
  3530. mov eax, h // for y := 1 to Src.h do
  3531. mov y, eax
  3532. @LoopY:
  3533. mov eax, ReadRow // ReadAddr := ReadRow;
  3534. mov ReadAddr, eax
  3535. mov eax, WriteRow // WriteAddr := WriteRow;
  3536. mov WriteAddr, eax
  3537. mov eax, w // for x := 1 to Src.w do
  3538. mov x, eax
  3539. mov ecx, ReadAddr
  3540. mov edx, WriteAddr
  3541. mov ebx, DestPitch
  3542. @LoopX:
  3543. mov al, [ecx] // PUInt8(WriteAddr)^ := PUInt8(ReadAddr)^;
  3544. mov [edx], al
  3545. mov [edx + 1], al // PUInt8(WriteAddr + 1)^ := PUInt8(ReadAddr)^;
  3546. shr al, 1
  3547. and al, $6d
  3548. mov [edx + ebx], al // PUInt8(WriteAddr + DestPitch)^ := PUInt8(ReadAddr)^;
  3549. mov [edx + ebx + 1], al // PUInt8(WriteAddr + DestPitch + 1)^ := PUInt8(ReadAddr)^;
  3550. inc ecx // inc(ReadAddr);
  3551. add edx, 2 // inc(WriteAddr, 2);
  3552. dec x
  3553. jnz @LoopX
  3554. mov eax, SrcPitch // inc(UInt32(ReadRow), SrcPitch);
  3555. add ReadRow, eax
  3556. mov eax, DestPitch // inc(UInt32(WriteRow), DestPitch * 2);
  3557. add WriteRow, eax
  3558. add WriteRow, eax
  3559. dec y
  3560. jnz @LoopY
  3561. pop ebx
  3562. end;
  3563. 15 :
  3564. asm
  3565. push ebx
  3566. mov eax, h // for y := 1 to Src.h do
  3567. mov y, eax
  3568. @LoopY:
  3569. mov eax, ReadRow // ReadAddr := ReadRow;
  3570. mov ReadAddr, eax
  3571. mov eax, WriteRow // WriteAddr := WriteRow;
  3572. mov WriteAddr, eax
  3573. mov eax, w // for x := 1 to Src.w do
  3574. mov x, eax
  3575. mov ecx, ReadAddr
  3576. mov edx, WriteAddr
  3577. mov ebx, DestPitch
  3578. @LoopX:
  3579. mov ax, [ecx] // PUInt16(WriteAddr)^ := PUInt16(ReadAddr)^;
  3580. mov [edx], ax
  3581. mov [edx + 2], ax // PUInt16(WriteAddr + 2)^ := PUInt16(ReadAddr)^;
  3582. shr ax, 1
  3583. and ax, $3def
  3584. mov [edx + ebx], ax // PUInt16(WriteAddr + DestPitch)^ := PUInt16(ReadAddr)^;
  3585. mov [edx + ebx + 2], ax // PUInt16(WriteAddr + DestPitch + 2)^ := PUInt16(ReadAddr)^;
  3586. add ecx, 2 // inc(ReadAddr, 2);
  3587. add edx, 4 // inc(WriteAddr, 4);
  3588. dec x
  3589. jnz @LoopX
  3590. mov eax, SrcPitch // inc(UInt32(ReadRow), SrcPitch);
  3591. add ReadRow, eax
  3592. mov eax, DestPitch // inc(UInt32(WriteRow), DestPitch * 2);
  3593. add WriteRow, eax
  3594. add WriteRow, eax
  3595. dec y
  3596. jnz @LoopY
  3597. pop ebx
  3598. end;
  3599. 16 :
  3600. asm
  3601. push ebx
  3602. mov eax, h // for y := 1 to Src.h do
  3603. mov y, eax
  3604. @LoopY:
  3605. mov eax, ReadRow // ReadAddr := ReadRow;
  3606. mov ReadAddr, eax
  3607. mov eax, WriteRow // WriteAddr := WriteRow;
  3608. mov WriteAddr, eax
  3609. mov eax, w // for x := 1 to Src.w do
  3610. mov x, eax
  3611. mov ecx, ReadAddr
  3612. mov edx, WriteAddr
  3613. mov ebx, DestPitch
  3614. @LoopX:
  3615. mov ax, [ecx] // PUInt16(WriteAddr)^ := PUInt16(ReadAddr)^;
  3616. mov [edx], ax
  3617. mov [edx + 2], ax // PUInt16(WriteAddr + 2)^ := PUInt16(ReadAddr)^;
  3618. shr ax, 1
  3619. and ax, $7bef
  3620. mov [edx + ebx], ax // PUInt16(WriteAddr + DestPitch)^ := PUInt16(ReadAddr)^;
  3621. mov [edx + ebx + 2], ax // PUInt16(WriteAddr + DestPitch + 2)^ := PUInt16(ReadAddr)^;
  3622. add ecx, 2 // inc(ReadAddr, 2);
  3623. add edx, 4 // inc(WriteAddr, 4);
  3624. dec x
  3625. jnz @LoopX
  3626. mov eax, SrcPitch // inc(UInt32(ReadRow), SrcPitch);
  3627. add ReadRow, eax
  3628. mov eax, DestPitch // inc(UInt32(WriteRow), DestPitch * 2);
  3629. add WriteRow, eax
  3630. add WriteRow, eax
  3631. dec y
  3632. jnz @LoopY
  3633. pop ebx
  3634. end;
  3635. 24 :
  3636. asm
  3637. push ebx
  3638. mov eax, h // for y := 1 to Src.h do
  3639. mov y, eax
  3640. @LoopY:
  3641. mov eax, ReadRow // ReadAddr := ReadRow;
  3642. mov ReadAddr, eax
  3643. mov eax, WriteRow // WriteAddr := WriteRow;
  3644. mov WriteAddr, eax
  3645. mov eax, w // for x := 1 to Src.w do
  3646. mov x, eax
  3647. mov ecx, ReadAddr
  3648. mov edx, WriteAddr
  3649. mov ebx, DestPitch
  3650. @LoopX:
  3651. mov eax, [ecx] // (PUInt32(WriteAddr)^ and $ff000000) or (PUInt32(ReadAddr)^ and $00ffffff);
  3652. and eax, $00ffffff
  3653. and dword ptr [edx], $ff000000
  3654. or [edx], eax
  3655. and dword ptr [edx + 3], $00ffffff // (PUInt32(WriteAddr + 3)^ and $ff000000) or (PUInt32(ReadAddr)^ and $00ffffff);
  3656. or [edx + 3], eax
  3657. shr eax, 1
  3658. and eax, $007f7f7f
  3659. and dword ptr [edx + ebx], $00ffffff // (PUInt32(WriteAddr + DestPitch)^ and $ff000000) or (PUInt32(ReadAddr)^ and $00ffffff);
  3660. or [edx + ebx], eax
  3661. and dword ptr [edx + ebx + 3], $00ffffff // (PUInt32(WriteAddr + DestPitch + 3)^ and $ff000000) or (PUInt32(ReadAddr)^ and $00ffffff);
  3662. or [edx + ebx + 3], eax
  3663. add ecx, 3 // inc(ReadAddr, 3);
  3664. add edx, 6 // inc(WriteAddr, 6);
  3665. dec x
  3666. jnz @LoopX
  3667. mov eax, SrcPitch // inc(UInt32(ReadRow), SrcPitch);
  3668. add ReadRow, eax
  3669. mov eax, DestPitch // inc(UInt32(WriteRow), DestPitch * 2);
  3670. add WriteRow, eax
  3671. add WriteRow, eax
  3672. dec y
  3673. jnz @LoopY
  3674. pop ebx
  3675. end;
  3676. 32 :
  3677. asm
  3678. push ebx
  3679. mov eax, h // for y := 1 to Src.h do
  3680. mov y, eax
  3681. @LoopY:
  3682. mov eax, ReadRow // ReadAddr := ReadRow;
  3683. mov ReadAddr, eax
  3684. mov eax, WriteRow // WriteAddr := WriteRow;
  3685. mov WriteAddr, eax
  3686. mov eax, w // for x := 1 to Src.w do
  3687. mov x, eax
  3688. mov ecx, ReadAddr
  3689. mov edx, WriteAddr
  3690. mov ebx, DestPitch
  3691. @LoopX:
  3692. mov eax, [ecx] // PUInt32(WriteAddr)^ := PUInt32(ReadAddr)^;
  3693. mov [edx], eax
  3694. mov [edx + 4], eax // PUInt32(WriteAddr + 4)^ := PUInt32(ReadAddr)^;
  3695. shr eax, 1
  3696. and eax, $7f7f7f7f
  3697. mov [edx + ebx], eax // PUInt32(WriteAddr + DestPitch)^ := PUInt32(ReadAddr)^;
  3698. mov [edx + ebx + 4], eax // PUInt32(WriteAddr + DestPitch + 4)^ := PUInt32(ReadAddr)^;
  3699. add ecx, 4 // inc(ReadAddr, 4);
  3700. add edx, 8 // inc(WriteAddr, 8);
  3701. dec x
  3702. jnz @LoopX
  3703. mov eax, SrcPitch // inc(UInt32(ReadRow), SrcPitch);
  3704. add ReadRow, eax
  3705. mov eax, DestPitch // inc(UInt32(WriteRow), DestPitch * 2);
  3706. add WriteRow, eax
  3707. add WriteRow, eax
  3708. dec y
  3709. jnz @LoopY
  3710. pop ebx
  3711. end;
  3712. end;
  3713. if SDL_MustLock( Src ) then
  3714. SDL_UnlockSurface( Src );
  3715. if SDL_MustLock( Dest ) then
  3716. SDL_UnlockSurface( Dest );
  3717. end;
  3718. function SDL_PixelTestSurfaceVsRect( SrcSurface1 : PSDL_Surface; SrcRect1 : PSDL_Rect; SrcRect2 : PSDL_Rect; Left1, Top1, Left2, Top2 : integer ) :
  3719. boolean;
  3720. var
  3721. Src_Rect1, Src_Rect2 : TSDL_Rect;
  3722. right1, bottom1 : integer;
  3723. right2, bottom2 : integer;
  3724. Scan1Start, Scan2Start, ScanWidth, ScanHeight : cardinal;
  3725. Mod1: cardinal;
  3726. Addr1 : cardinal;
  3727. BPP : cardinal;
  3728. Pitch1 : cardinal;
  3729. TransparentColor1 : cardinal;
  3730. tx, ty : cardinal;
  3731. StartTick : cardinal;
  3732. Color1 : cardinal;
  3733. begin
  3734. Result := false;
  3735. if SrcRect1 = nil then
  3736. begin
  3737. with Src_Rect1 do
  3738. begin
  3739. x := 0;
  3740. y := 0;
  3741. w := SrcSurface1.w;
  3742. h := SrcSurface1.h;
  3743. end;
  3744. end
  3745. else
  3746. Src_Rect1 := SrcRect1^;
  3747. Src_Rect2 := SrcRect2^;
  3748. with Src_Rect1 do
  3749. begin
  3750. Right1 := Left1 + w;
  3751. Bottom1 := Top1 + h;
  3752. end;
  3753. with Src_Rect2 do
  3754. begin
  3755. Right2 := Left2 + w;
  3756. Bottom2 := Top2 + h;
  3757. end;
  3758. if ( Left1 >= Right2 ) or ( Right1 <= Left2 ) or ( Top1 >= Bottom2 ) or (
  3759. Bottom1 <=
  3760. Top2 ) then
  3761. exit;
  3762. if Left1 <= Left2 then
  3763. begin
  3764. // 1. left, 2. right
  3765. Scan1Start := Src_Rect1.x + Left2 - Left1;
  3766. Scan2Start := Src_Rect2.x;
  3767. ScanWidth := Right1 - Left2;
  3768. with Src_Rect2 do
  3769. if ScanWidth > w then
  3770. ScanWidth := w;
  3771. end
  3772. else
  3773. begin
  3774. // 1. right, 2. left
  3775. Scan1Start := Src_Rect1.x;
  3776. Scan2Start := Src_Rect2.x + Left1 - Left2;
  3777. ScanWidth := Right2 - Left1;
  3778. with Src_Rect1 do
  3779. if ScanWidth > w then
  3780. ScanWidth := w;
  3781. end;
  3782. with SrcSurface1^ do
  3783. begin
  3784. Pitch1 := Pitch;
  3785. Addr1 := cardinal( Pixels );
  3786. inc( Addr1, Pitch1 * UInt32( Src_Rect1.y ) );
  3787. with format^ do
  3788. begin
  3789. BPP := BytesPerPixel;
  3790. TransparentColor1 := colorkey;
  3791. end;
  3792. end;
  3793. Mod1 := Pitch1 - ( ScanWidth * BPP );
  3794. inc( Addr1, BPP * Scan1Start );
  3795. if Top1 <= Top2 then
  3796. begin
  3797. // 1. up, 2. down
  3798. ScanHeight := Bottom1 - Top2;
  3799. if ScanHeight > Src_Rect2.h then
  3800. ScanHeight := Src_Rect2.h;
  3801. inc( Addr1, Pitch1 * UInt32( Top2 - Top1 ) );
  3802. end
  3803. else
  3804. begin
  3805. // 1. down, 2. up
  3806. ScanHeight := Bottom2 - Top1;
  3807. if ScanHeight > Src_Rect1.h then
  3808. ScanHeight := Src_Rect1.h;
  3809. end;
  3810. case BPP of
  3811. 1 :
  3812. for ty := 1 to ScanHeight do
  3813. begin
  3814. for tx := 1 to ScanWidth do
  3815. begin
  3816. if ( PByte( Addr1 )^ <> TransparentColor1 ) then
  3817. begin
  3818. Result := true;
  3819. exit;
  3820. end;
  3821. inc( Addr1 );
  3822. end;
  3823. inc( Addr1, Mod1 );
  3824. end;
  3825. 2 :
  3826. for ty := 1 to ScanHeight do
  3827. begin
  3828. for tx := 1 to ScanWidth do
  3829. begin
  3830. if ( PWord( Addr1 )^ <> TransparentColor1 ) then
  3831. begin
  3832. Result := true;
  3833. exit;
  3834. end;
  3835. inc( Addr1, 2 );
  3836. end;
  3837. inc( Addr1, Mod1 );
  3838. end;
  3839. 3 :
  3840. for ty := 1 to ScanHeight do
  3841. begin
  3842. for tx := 1 to ScanWidth do
  3843. begin
  3844. Color1 := PLongWord( Addr1 )^ and $00FFFFFF;
  3845. if ( Color1 <> TransparentColor1 )
  3846. then
  3847. begin
  3848. Result := true;
  3849. exit;
  3850. end;
  3851. inc( Addr1, 3 );
  3852. end;
  3853. inc( Addr1, Mod1 );
  3854. end;
  3855. 4 :
  3856. for ty := 1 to ScanHeight do
  3857. begin
  3858. for tx := 1 to ScanWidth do
  3859. begin
  3860. if ( PLongWord( Addr1 )^ <> TransparentColor1 ) then
  3861. begin
  3862. Result := true;
  3863. exit;
  3864. end;
  3865. inc( Addr1, 4 );
  3866. end;
  3867. inc( Addr1, Mod1 );
  3868. end;
  3869. end;
  3870. end;
  3871. procedure SDL_ORSurface( SrcSurface : PSDL_Surface; SrcRect : PSDL_Rect;
  3872. DestSurface : PSDL_Surface; DestRect : PSDL_Rect );
  3873. var
  3874. Src, Dest : TSDL_Rect;
  3875. Diff : integer;
  3876. SrcAddr, DestAddr, TransparentColor : cardinal;
  3877. // TransparentColor: cardinal;
  3878. _ebx, _esi, _edi, _esp : cardinal;
  3879. WorkX, WorkY : word;
  3880. SrcMod, DestMod : cardinal;
  3881. Bits : cardinal;
  3882. begin
  3883. if ( SrcSurface = nil ) or ( DestSurface = nil ) then
  3884. exit; // Remove this to make it faster
  3885. if ( SrcSurface.Format.BitsPerPixel <> DestSurface.Format.BitsPerPixel ) then
  3886. exit; // Remove this to make it faster
  3887. if SrcRect = nil then
  3888. begin
  3889. with Src do
  3890. begin
  3891. x := 0;
  3892. y := 0;
  3893. w := SrcSurface.w;
  3894. h := SrcSurface.h;
  3895. end;
  3896. end
  3897. else
  3898. Src := SrcRect^;
  3899. if DestRect = nil then
  3900. begin
  3901. Dest.x := 0;
  3902. Dest.y := 0;
  3903. end
  3904. else
  3905. Dest := DestRect^;
  3906. Dest.w := Src.w;
  3907. Dest.h := Src.h;
  3908. with DestSurface.Clip_Rect do
  3909. begin
  3910. // Source's right side is greater than the dest.cliprect
  3911. if Dest.x + Src.w > x + w then
  3912. begin
  3913. smallint( Src.w ) := x + w - Dest.x;
  3914. smallint( Dest.w ) := x + w - Dest.x;
  3915. if smallint( Dest.w ) < 1 then
  3916. exit;
  3917. end;
  3918. // Source's bottom side is greater than the dest.clip
  3919. if Dest.y + Src.h > y + h then
  3920. begin
  3921. smallint( Src.h ) := y + h - Dest.y;
  3922. smallint( Dest.h ) := y + h - Dest.y;
  3923. if smallint( Dest.h ) < 1 then
  3924. exit;
  3925. end;
  3926. // Source's left side is less than the dest.clip
  3927. if Dest.x < x then
  3928. begin
  3929. Diff := x - Dest.x;
  3930. Src.x := Src.x + Diff;
  3931. smallint( Src.w ) := smallint( Src.w ) - Diff;
  3932. Dest.x := x;
  3933. smallint( Dest.w ) := smallint( Dest.w ) - Diff;
  3934. if smallint( Dest.w ) < 1 then
  3935. exit;
  3936. end;
  3937. // Source's Top side is less than the dest.clip
  3938. if Dest.y < y then
  3939. begin
  3940. Diff := y - Dest.y;
  3941. Src.y := Src.y + Diff;
  3942. smallint( Src.h ) := smallint( Src.h ) - Diff;
  3943. Dest.y := y;
  3944. smallint( Dest.h ) := smallint( Dest.h ) - Diff;
  3945. if smallint( Dest.h ) < 1 then
  3946. exit;
  3947. end;
  3948. end;
  3949. with SrcSurface^ do
  3950. begin
  3951. SrcAddr := cardinal( Pixels ) + UInt32( Src.y ) * Pitch + UInt32( Src.x ) *
  3952. Format.BytesPerPixel;
  3953. SrcMod := Pitch - Src.w * Format.BytesPerPixel;
  3954. TransparentColor := Format.colorkey;
  3955. end;
  3956. with DestSurface^ do
  3957. begin
  3958. DestAddr := cardinal( Pixels ) + UInt32( Dest.y ) * Pitch + UInt32( Dest.x ) *
  3959. Format.BytesPerPixel;
  3960. DestMod := Pitch - Dest.w * Format.BytesPerPixel;
  3961. Bits := Format.BitsPerPixel;
  3962. end;
  3963. SDL_LockSurface( SrcSurface );
  3964. SDL_LockSurface( DestSurface );
  3965. WorkY := Src.h;
  3966. case bits of
  3967. 8 :
  3968. asm
  3969. mov _ebx, ebx
  3970. mov _esi, esi
  3971. mov _edi, edi
  3972. mov _esp, esp
  3973. mov esi, SrcAddr // ESI - Source Offset
  3974. mov edi, DestAddr // EDI - Destination Offset
  3975. mov ax, Src.h // WorkY := Src.h
  3976. mov WorkY, ax
  3977. @LoopY:
  3978. mov ax, Src.w
  3979. mov WorkX, ax // WorkX := Src.w
  3980. @Loopx:
  3981. mov al, [esi] // AL := source color
  3982. cmp al, 0
  3983. je @SkipColor // if AL=0 or AL=transparent color then skip everything
  3984. cmp al, byte ptr TransparentColor
  3985. je @SkipColor
  3986. or al, [edi]
  3987. mov [edi], al
  3988. @SkipColor:
  3989. inc esi
  3990. inc edi
  3991. dec WorkX
  3992. jnz @LoopX
  3993. add esi, SrcMod
  3994. add edi, DestMod
  3995. dec WorkY
  3996. jnz @LoopY
  3997. mov esp,_esp
  3998. mov edi,_edi
  3999. mov esi,_esi
  4000. mov ebx,_ebx
  4001. end;
  4002. 15 :
  4003. asm
  4004. mov _ebx, ebx
  4005. mov _esi, esi
  4006. mov _edi, edi
  4007. mov _esp, esp
  4008. mov esi, SrcAddr // ESI - Source Offset
  4009. mov edi, DestAddr // EDI - Destination Offset
  4010. mov ax, Src.h // WorkY := Src.h
  4011. mov WorkY, ax
  4012. @LoopY:
  4013. mov ax, Src.w
  4014. mov WorkX, ax // WorkX := Src.w
  4015. @Loopx:
  4016. mov ax, [esi] // AX := source color
  4017. cmp ax, 0
  4018. je @SkipColor // if AX=0 then skip everything
  4019. cmp ax, word ptr TransparentColor
  4020. je @SkipColor
  4021. or ax, [edi]
  4022. mov [edi], ax
  4023. @SkipColor:
  4024. add esi, 2
  4025. add edi, 2
  4026. dec WorkX
  4027. jnz @LoopX
  4028. add esi, SrcMod
  4029. add edi, DestMod
  4030. dec WorkY
  4031. jnz @LoopY
  4032. mov esp,_esp
  4033. mov edi,_edi
  4034. mov esi,_esi
  4035. mov ebx,_ebx
  4036. end;
  4037. 16 :
  4038. asm
  4039. mov _ebx, ebx
  4040. mov _esi, esi
  4041. mov _edi, edi
  4042. mov _esp, esp
  4043. mov esi, SrcAddr // ESI - Source Offset
  4044. mov edi, DestAddr // EDI - Destination Offset
  4045. mov ax, Src.h // WorkY := Src.h
  4046. mov WorkY, ax
  4047. @LoopY:
  4048. mov ax, Src.w
  4049. mov WorkX, ax // WorkX := Src.w
  4050. @Loopx:
  4051. mov ax, [esi] // AX := source color
  4052. cmp ax, 0
  4053. je @SkipColor // if AX=0 then skip everything
  4054. cmp ax, word ptr TransparentColor
  4055. je @SkipColor
  4056. or ax, [edi]
  4057. mov [edi], ax
  4058. @SkipColor:
  4059. add esi, 2
  4060. add edi, 2
  4061. dec WorkX
  4062. jnz @LoopX
  4063. add esi, SrcMod
  4064. add edi, DestMod
  4065. dec WorkY
  4066. jnz @LoopY
  4067. mov esp,_esp
  4068. mov edi,_edi
  4069. mov esi,_esi
  4070. mov ebx,_ebx
  4071. end;
  4072. 24 :
  4073. asm
  4074. mov _ebx, ebx
  4075. mov _esi, esi
  4076. mov _edi, edi
  4077. mov _esp, esp
  4078. mov esi, SrcAddr // ESI - Source Offset
  4079. mov edi, DestAddr // EDI - Destination Offset
  4080. mov ax, Src.h // WorkY := Src.h
  4081. mov WorkY, ax
  4082. @LoopY:
  4083. mov ax, Src.w
  4084. mov WorkX, ax // WorkX := Src.w
  4085. add WorkX, ax // WorkX := Src.w * 2
  4086. add WorkX, ax // WorkX := Src.w * 3
  4087. @Loopx:
  4088. mov al, [esi] // AL := source color
  4089. or al, [edi]
  4090. mov [edi], al
  4091. inc esi
  4092. inc edi
  4093. dec WorkX
  4094. jnz @LoopX
  4095. add esi, SrcMod
  4096. add edi, DestMod
  4097. dec WorkY
  4098. jnz @LoopY
  4099. mov esp,_esp
  4100. mov edi,_edi
  4101. mov esi,_esi
  4102. mov ebx,_ebx
  4103. end;
  4104. 32 :
  4105. asm
  4106. mov _ebx, ebx
  4107. mov _esi, esi
  4108. mov _edi, edi
  4109. mov _esp, esp
  4110. mov esi, SrcAddr // ESI - Source Offset
  4111. mov edi, DestAddr // EDI - Destination Offset
  4112. mov ax, Src.h // WorkY := Src.h
  4113. mov WorkY, ax
  4114. @LoopY:
  4115. mov ax, Src.w
  4116. shl ax, 2
  4117. mov WorkX, ax // WorkX := Src.w * 4
  4118. @Loopx:
  4119. mov al, [esi] // AL := source color
  4120. or al, [edi]
  4121. mov [edi], al
  4122. inc esi
  4123. inc edi
  4124. dec WorkX
  4125. jnz @LoopX
  4126. add esi, SrcMod
  4127. add edi, DestMod
  4128. dec WorkY
  4129. jnz @LoopY
  4130. mov esp,_esp
  4131. mov edi,_edi
  4132. mov esi,_esi
  4133. mov ebx,_ebx
  4134. end;
  4135. end;
  4136. SDL_UnlockSurface( SrcSurface );
  4137. SDL_UnlockSurface( DestSurface );
  4138. end;
  4139. procedure SDL_ANDSurface( SrcSurface : PSDL_Surface; SrcRect : PSDL_Rect;
  4140. DestSurface : PSDL_Surface; DestRect : PSDL_Rect );
  4141. var
  4142. Src, Dest : TSDL_Rect;
  4143. Diff : integer;
  4144. SrcAddr, DestAddr, TransparentColor : cardinal;
  4145. // TransparentColor: cardinal;
  4146. _ebx, _esi, _edi, _esp : cardinal;
  4147. WorkX, WorkY : word;
  4148. SrcMod, DestMod : cardinal;
  4149. Bits : cardinal;
  4150. begin
  4151. if ( SrcSurface = nil ) or ( DestSurface = nil ) then
  4152. exit; // Remove this to make it faster
  4153. if ( SrcSurface.Format.BitsPerPixel <> DestSurface.Format.BitsPerPixel ) then
  4154. exit; // Remove this to make it faster
  4155. if SrcRect = nil then
  4156. begin
  4157. with Src do
  4158. begin
  4159. x := 0;
  4160. y := 0;
  4161. w := SrcSurface.w;
  4162. h := SrcSurface.h;
  4163. end;
  4164. end
  4165. else
  4166. Src := SrcRect^;
  4167. if DestRect = nil then
  4168. begin
  4169. Dest.x := 0;
  4170. Dest.y := 0;
  4171. end
  4172. else
  4173. Dest := DestRect^;
  4174. Dest.w := Src.w;
  4175. Dest.h := Src.h;
  4176. with DestSurface.Clip_Rect do
  4177. begin
  4178. // Source's right side is greater than the dest.cliprect
  4179. if Dest.x + Src.w > x + w then
  4180. begin
  4181. smallint( Src.w ) := x + w - Dest.x;
  4182. smallint( Dest.w ) := x + w - Dest.x;
  4183. if smallint( Dest.w ) < 1 then
  4184. exit;
  4185. end;
  4186. // Source's bottom side is greater than the dest.clip
  4187. if Dest.y + Src.h > y + h then
  4188. begin
  4189. smallint( Src.h ) := y + h - Dest.y;
  4190. smallint( Dest.h ) := y + h - Dest.y;
  4191. if smallint( Dest.h ) < 1 then
  4192. exit;
  4193. end;
  4194. // Source's left side is less than the dest.clip
  4195. if Dest.x < x then
  4196. begin
  4197. Diff := x - Dest.x;
  4198. Src.x := Src.x + Diff;
  4199. smallint( Src.w ) := smallint( Src.w ) - Diff;
  4200. Dest.x := x;
  4201. smallint( Dest.w ) := smallint( Dest.w ) - Diff;
  4202. if smallint( Dest.w ) < 1 then
  4203. exit;
  4204. end;
  4205. // Source's Top side is less than the dest.clip
  4206. if Dest.y < y then
  4207. begin
  4208. Diff := y - Dest.y;
  4209. Src.y := Src.y + Diff;
  4210. smallint( Src.h ) := smallint( Src.h ) - Diff;
  4211. Dest.y := y;
  4212. smallint( Dest.h ) := smallint( Dest.h ) - Diff;
  4213. if smallint( Dest.h ) < 1 then
  4214. exit;
  4215. end;
  4216. end;
  4217. with SrcSurface^ do
  4218. begin
  4219. SrcAddr := cardinal( Pixels ) + UInt32( Src.y ) * Pitch + UInt32( Src.x ) *
  4220. Format.BytesPerPixel;
  4221. SrcMod := Pitch - Src.w * Format.BytesPerPixel;
  4222. TransparentColor := Format.colorkey;
  4223. end;
  4224. with DestSurface^ do
  4225. begin
  4226. DestAddr := cardinal( Pixels ) + UInt32( Dest.y ) * Pitch + UInt32( Dest.x ) *
  4227. Format.BytesPerPixel;
  4228. DestMod := Pitch - Dest.w * Format.BytesPerPixel;
  4229. Bits := Format.BitsPerPixel;
  4230. end;
  4231. SDL_LockSurface( SrcSurface );
  4232. SDL_LockSurface( DestSurface );
  4233. WorkY := Src.h;
  4234. case bits of
  4235. 8 :
  4236. asm
  4237. mov _ebx, ebx
  4238. mov _esi, esi
  4239. mov _edi, edi
  4240. mov _esp, esp
  4241. mov esi, SrcAddr // ESI - Source Offset
  4242. mov edi, DestAddr // EDI - Destination Offset
  4243. mov ax, Src.h // WorkY := Src.h
  4244. mov WorkY, ax
  4245. @LoopY:
  4246. mov ax, Src.w
  4247. mov WorkX, ax // WorkX := Src.w
  4248. @Loopx:
  4249. mov al, [esi] // AL := source color
  4250. cmp al, 0
  4251. je @SkipColor // if AL=0 or AL=transparent color then skip everything
  4252. cmp al, byte ptr TransparentColor
  4253. je @SkipColor
  4254. and al, [edi]
  4255. mov [edi], al
  4256. @SkipColor:
  4257. inc esi
  4258. inc edi
  4259. dec WorkX
  4260. jnz @LoopX
  4261. add esi, SrcMod
  4262. add edi, DestMod
  4263. dec WorkY
  4264. jnz @LoopY
  4265. mov esp,_esp
  4266. mov edi,_edi
  4267. mov esi,_esi
  4268. mov ebx,_ebx
  4269. end;
  4270. 15 :
  4271. asm
  4272. mov _ebx, ebx
  4273. mov _esi, esi
  4274. mov _edi, edi
  4275. mov _esp, esp
  4276. mov esi, SrcAddr // ESI - Source Offset
  4277. mov edi, DestAddr // EDI - Destination Offset
  4278. mov ax, Src.h // WorkY := Src.h
  4279. mov WorkY, ax
  4280. @LoopY:
  4281. mov ax, Src.w
  4282. mov WorkX, ax // WorkX := Src.w
  4283. @Loopx:
  4284. mov ax, [esi] // AX := source color
  4285. cmp ax, 0
  4286. je @SkipColor // if AX=0 then skip everything
  4287. cmp ax, word ptr TransparentColor
  4288. je @SkipColor
  4289. and ax, [edi]
  4290. mov [edi], ax
  4291. @SkipColor:
  4292. add esi, 2
  4293. add edi, 2
  4294. dec WorkX
  4295. jnz @LoopX
  4296. add esi, SrcMod
  4297. add edi, DestMod
  4298. dec WorkY
  4299. jnz @LoopY
  4300. mov esp,_esp
  4301. mov edi,_edi
  4302. mov esi,_esi
  4303. mov ebx,_ebx
  4304. end;
  4305. 16 :
  4306. asm
  4307. mov _ebx, ebx
  4308. mov _esi, esi
  4309. mov _edi, edi
  4310. mov _esp, esp
  4311. mov esi, SrcAddr // ESI - Source Offset
  4312. mov edi, DestAddr // EDI - Destination Offset
  4313. mov ax, Src.h // WorkY := Src.h
  4314. mov WorkY, ax
  4315. @LoopY:
  4316. mov ax, Src.w
  4317. mov WorkX, ax // WorkX := Src.w
  4318. @Loopx:
  4319. mov ax, [esi] // AX := source color
  4320. cmp ax, 0
  4321. je @SkipColor // if AX=0 then skip everything
  4322. cmp ax, word ptr TransparentColor
  4323. je @SkipColor
  4324. and ax, [edi]
  4325. mov [edi], ax
  4326. @SkipColor:
  4327. add esi, 2
  4328. add edi, 2
  4329. dec WorkX
  4330. jnz @LoopX
  4331. add esi, SrcMod
  4332. add edi, DestMod
  4333. dec WorkY
  4334. jnz @LoopY
  4335. mov esp,_esp
  4336. mov edi,_edi
  4337. mov esi,_esi
  4338. mov ebx,_ebx
  4339. end;
  4340. 24 :
  4341. asm
  4342. mov _ebx, ebx
  4343. mov _esi, esi
  4344. mov _edi, edi
  4345. mov _esp, esp
  4346. mov esi, SrcAddr // ESI - Source Offset
  4347. mov edi, DestAddr // EDI - Destination Offset
  4348. mov ax, Src.h // WorkY := Src.h
  4349. mov WorkY, ax
  4350. @LoopY:
  4351. mov ax, Src.w
  4352. mov WorkX, ax // WorkX := Src.w
  4353. add WorkX, ax // WorkX := Src.w * 2
  4354. add WorkX, ax // WorkX := Src.w * 3
  4355. @Loopx:
  4356. mov al, [esi] // AL := source color
  4357. and al, [edi]
  4358. mov [edi], al
  4359. inc esi
  4360. inc edi
  4361. dec WorkX
  4362. jnz @LoopX
  4363. add esi, SrcMod
  4364. add edi, DestMod
  4365. dec WorkY
  4366. jnz @LoopY
  4367. mov esp,_esp
  4368. mov edi,_edi
  4369. mov esi,_esi
  4370. mov ebx,_ebx
  4371. end;
  4372. 32 :
  4373. asm
  4374. mov _ebx, ebx
  4375. mov _esi, esi
  4376. mov _edi, edi
  4377. mov _esp, esp
  4378. mov esi, SrcAddr // ESI - Source Offset
  4379. mov edi, DestAddr // EDI - Destination Offset
  4380. mov ax, Src.h // WorkY := Src.h
  4381. mov WorkY, ax
  4382. @LoopY:
  4383. mov ax, Src.w
  4384. shl ax, 2
  4385. mov WorkX, ax // WorkX := Src.w * 4
  4386. @Loopx:
  4387. mov al, [esi] // AL := source color
  4388. and al, [edi]
  4389. mov [edi], al
  4390. inc esi
  4391. inc edi
  4392. dec WorkX
  4393. jnz @LoopX
  4394. add esi, SrcMod
  4395. add edi, DestMod
  4396. dec WorkY
  4397. jnz @LoopY
  4398. mov esp,_esp
  4399. mov edi,_edi
  4400. mov esi,_esi
  4401. mov ebx,_ebx
  4402. end;
  4403. end;
  4404. SDL_UnlockSurface( SrcSurface );
  4405. SDL_UnlockSurface( DestSurface );
  4406. end;
  4407. procedure SDL_GTSurface( SrcSurface : PSDL_Surface; SrcRect : PSDL_Rect;
  4408. DestSurface : PSDL_Surface; DestRect : PSDL_Rect );
  4409. var
  4410. R, G, B, Pixel1, Pixel2, TransparentColor : cardinal;
  4411. Src, Dest : TSDL_Rect;
  4412. Diff : integer;
  4413. SrcAddr, DestAddr : cardinal;
  4414. WorkX, WorkY : word;
  4415. SrcMod, DestMod : cardinal;
  4416. Bits : cardinal;
  4417. begin
  4418. if ( SrcSurface = nil ) or ( DestSurface = nil ) then
  4419. exit; // Remove this to make it faster
  4420. if ( SrcSurface.Format.BitsPerPixel <> DestSurface.Format.BitsPerPixel ) then
  4421. exit; // Remove this to make it faster
  4422. if SrcRect = nil then
  4423. begin
  4424. with Src do
  4425. begin
  4426. x := 0;
  4427. y := 0;
  4428. w := SrcSurface.w;
  4429. h := SrcSurface.h;
  4430. end;
  4431. end
  4432. else
  4433. Src := SrcRect^;
  4434. if DestRect = nil then
  4435. begin
  4436. Dest.x := 0;
  4437. Dest.y := 0;
  4438. end
  4439. else
  4440. Dest := DestRect^;
  4441. Dest.w := Src.w;
  4442. Dest.h := Src.h;
  4443. with DestSurface.Clip_Rect do
  4444. begin
  4445. // Source's right side is greater than the dest.cliprect
  4446. if Dest.x + Src.w > x + w then
  4447. begin
  4448. smallint( Src.w ) := x + w - Dest.x;
  4449. smallint( Dest.w ) := x + w - Dest.x;
  4450. if smallint( Dest.w ) < 1 then
  4451. exit;
  4452. end;
  4453. // Source's bottom side is greater than the dest.clip
  4454. if Dest.y + Src.h > y + h then
  4455. begin
  4456. smallint( Src.h ) := y + h - Dest.y;
  4457. smallint( Dest.h ) := y + h - Dest.y;
  4458. if smallint( Dest.h ) < 1 then
  4459. exit;
  4460. end;
  4461. // Source's left side is less than the dest.clip
  4462. if Dest.x < x then
  4463. begin
  4464. Diff := x - Dest.x;
  4465. Src.x := Src.x + Diff;
  4466. smallint( Src.w ) := smallint( Src.w ) - Diff;
  4467. Dest.x := x;
  4468. smallint( Dest.w ) := smallint( Dest.w ) - Diff;
  4469. if smallint( Dest.w ) < 1 then
  4470. exit;
  4471. end;
  4472. // Source's Top side is less than the dest.clip
  4473. if Dest.y < y then
  4474. begin
  4475. Diff := y - Dest.y;
  4476. Src.y := Src.y + Diff;
  4477. smallint( Src.h ) := smallint( Src.h ) - Diff;
  4478. Dest.y := y;
  4479. smallint( Dest.h ) := smallint( Dest.h ) - Diff;
  4480. if smallint( Dest.h ) < 1 then
  4481. exit;
  4482. end;
  4483. end;
  4484. with SrcSurface^ do
  4485. begin
  4486. SrcAddr := cardinal( Pixels ) + UInt32( Src.y ) * Pitch + UInt32( Src.x ) *
  4487. Format.BytesPerPixel;
  4488. SrcMod := Pitch - Src.w * Format.BytesPerPixel;
  4489. TransparentColor := Format.colorkey;
  4490. end;
  4491. with DestSurface^ do
  4492. begin
  4493. DestAddr := cardinal( Pixels ) + UInt32( Dest.y ) * Pitch + UInt32( Dest.x ) *
  4494. Format.BytesPerPixel;
  4495. DestMod := Pitch - Dest.w * Format.BytesPerPixel;
  4496. Bits := Format.BitsPerPixel;
  4497. end;
  4498. SDL_LockSurface( SrcSurface );
  4499. SDL_LockSurface( DestSurface );
  4500. WorkY := Src.h;
  4501. case bits of
  4502. 8 :
  4503. begin
  4504. repeat
  4505. WorkX := Src.w;
  4506. repeat
  4507. Pixel1 := PUInt8( SrcAddr )^;
  4508. if ( Pixel1 <> TransparentColor ) and ( Pixel1 <> 0 ) then
  4509. begin
  4510. Pixel2 := PUInt8( DestAddr )^;
  4511. if Pixel2 > 0 then
  4512. begin
  4513. if Pixel2 and $E0 > Pixel1 and $E0 then R := Pixel2 and $E0 else R := Pixel1 and $E0;
  4514. if Pixel2 and $1C > Pixel1 and $1C then G := Pixel2 and $1C else G := Pixel1 and $1C;
  4515. if Pixel2 and $03 > Pixel1 and $03 then B := Pixel2 and $03 else B := Pixel1 and $03;
  4516. if R > $E0 then
  4517. R := $E0;
  4518. if G > $1C then
  4519. G := $1C;
  4520. if B > $03 then
  4521. B := $03;
  4522. PUInt8( DestAddr )^ := R or G or B;
  4523. end
  4524. else
  4525. PUInt8( DestAddr )^ := Pixel1;
  4526. end;
  4527. inc( SrcAddr );
  4528. inc( DestAddr );
  4529. dec( WorkX );
  4530. until WorkX = 0;
  4531. inc( SrcAddr, SrcMod );
  4532. inc( DestAddr, DestMod );
  4533. dec( WorkY );
  4534. until WorkY = 0;
  4535. end;
  4536. 15 :
  4537. begin
  4538. repeat
  4539. WorkX := Src.w;
  4540. repeat
  4541. Pixel1 := PUInt16( SrcAddr )^;
  4542. if ( Pixel1 <> TransparentColor ) and ( Pixel1 <> 0 ) then
  4543. begin
  4544. Pixel2 := PUInt16( DestAddr )^;
  4545. if Pixel2 > 0 then
  4546. begin
  4547. if Pixel2 and $7C00 > Pixel1 and $7C00 then R := Pixel2 and $7C00 else R := Pixel1 and $7C00;
  4548. if Pixel2 and $03E0 > Pixel1 and $03E0 then G := Pixel2 and $03E0 else G := Pixel1 and $03E0;
  4549. if Pixel2 and $001F > Pixel1 and $001F then B := Pixel2 and $001F else B := Pixel1 and $001F;
  4550. PUInt16( DestAddr )^ := R or G or B;
  4551. end
  4552. else
  4553. PUInt16( DestAddr )^ := Pixel1;
  4554. end;
  4555. inc( SrcAddr, 2 );
  4556. inc( DestAddr, 2 );
  4557. dec( WorkX );
  4558. until WorkX = 0;
  4559. inc( SrcAddr, SrcMod );
  4560. inc( DestAddr, DestMod );
  4561. dec( WorkY );
  4562. until WorkY = 0;
  4563. end;
  4564. 16 :
  4565. begin
  4566. repeat
  4567. WorkX := Src.w;
  4568. repeat
  4569. Pixel1 := PUInt16( SrcAddr )^;
  4570. if ( Pixel1 <> TransparentColor ) and ( Pixel1 <> 0 ) then
  4571. begin
  4572. Pixel2 := PUInt16( DestAddr )^;
  4573. if Pixel2 > 0 then
  4574. begin
  4575. if Pixel2 and $F800 > Pixel1 and $F800 then R := Pixel2 and $F800 else R := Pixel1 and $F800;
  4576. if Pixel2 and $07E0 > Pixel1 and $07E0 then G := Pixel2 and $07E0 else G := Pixel1 and $07E0;
  4577. if Pixel2 and $001F > Pixel1 and $001F then B := Pixel2 and $001F else B := Pixel1 and $001F;
  4578. PUInt16( DestAddr )^ := R or G or B;
  4579. end
  4580. else
  4581. PUInt16( DestAddr )^ := Pixel1;
  4582. end;
  4583. inc( SrcAddr, 2 );
  4584. inc( DestAddr, 2 );
  4585. dec( WorkX );
  4586. until WorkX = 0;
  4587. inc( SrcAddr, SrcMod );
  4588. inc( DestAddr, DestMod );
  4589. dec( WorkY );
  4590. until WorkY = 0;
  4591. end;
  4592. 24 :
  4593. begin
  4594. repeat
  4595. WorkX := Src.w;
  4596. repeat
  4597. Pixel1 := PUInt32( SrcAddr )^ and $00FFFFFF;
  4598. if ( Pixel1 <> TransparentColor ) and ( Pixel1 <> 0 ) then
  4599. begin
  4600. Pixel2 := PUInt32( DestAddr )^ and $00FFFFFF;
  4601. if Pixel2 > 0 then
  4602. begin
  4603. if Pixel2 and $FF0000 > Pixel1 and $FF0000 then R := Pixel2 and $FF0000 else R := Pixel1 and $FF0000;
  4604. if Pixel2 and $00FF00 > Pixel1 and $00FF00 then G := Pixel2 and $00FF00 else G := Pixel1 and $00FF00;
  4605. if Pixel2 and $0000FF > Pixel1 and $0000FF then B := Pixel2 and $0000FF else B := Pixel1 and $0000FF;
  4606. PUInt32( DestAddr )^ := PUInt32( DestAddr )^ and $FF000000 or ( R or G or B );
  4607. end
  4608. else
  4609. PUInt32( DestAddr )^ := PUInt32( DestAddr )^ and $FF000000 or Pixel1;
  4610. end;
  4611. inc( SrcAddr, 3 );
  4612. inc( DestAddr, 3 );
  4613. dec( WorkX );
  4614. until WorkX = 0;
  4615. inc( SrcAddr, SrcMod );
  4616. inc( DestAddr, DestMod );
  4617. dec( WorkY );
  4618. until WorkY = 0;
  4619. end;
  4620. 32 :
  4621. begin
  4622. repeat
  4623. WorkX := Src.w;
  4624. repeat
  4625. Pixel1 := PUInt32( SrcAddr )^;
  4626. if ( Pixel1 <> TransparentColor ) and ( Pixel1 <> 0 ) then
  4627. begin
  4628. Pixel2 := PUInt32( DestAddr )^;
  4629. if Pixel2 > 0 then
  4630. begin
  4631. if Pixel2 and $FF0000 > Pixel1 and $FF0000 then R := Pixel2 and $FF0000 else R := Pixel1 and $FF0000;
  4632. if Pixel2 and $00FF00 > Pixel1 and $00FF00 then G := Pixel2 and $00FF00 else G := Pixel1 and $00FF00;
  4633. if Pixel2 and $0000FF > Pixel1 and $0000FF then B := Pixel2 and $0000FF else B := Pixel1 and $0000FF;
  4634. PUInt32( DestAddr )^ := R or G or B;
  4635. end
  4636. else
  4637. PUInt32( DestAddr )^ := Pixel1;
  4638. end;
  4639. inc( SrcAddr, 4 );
  4640. inc( DestAddr, 4 );
  4641. dec( WorkX );
  4642. until WorkX = 0;
  4643. inc( SrcAddr, SrcMod );
  4644. inc( DestAddr, DestMod );
  4645. dec( WorkY );
  4646. until WorkY = 0;
  4647. end;
  4648. end;
  4649. SDL_UnlockSurface( SrcSurface );
  4650. SDL_UnlockSurface( DestSurface );
  4651. end;
  4652. procedure SDL_LTSurface( SrcSurface : PSDL_Surface; SrcRect : PSDL_Rect;
  4653. DestSurface : PSDL_Surface; DestRect : PSDL_Rect );
  4654. var
  4655. R, G, B, Pixel1, Pixel2, TransparentColor : cardinal;
  4656. Src, Dest : TSDL_Rect;
  4657. Diff : integer;
  4658. SrcAddr, DestAddr : cardinal;
  4659. WorkX, WorkY : word;
  4660. SrcMod, DestMod : cardinal;
  4661. Bits : cardinal;
  4662. begin
  4663. if ( SrcSurface = nil ) or ( DestSurface = nil ) then
  4664. exit; // Remove this to make it faster
  4665. if ( SrcSurface.Format.BitsPerPixel <> DestSurface.Format.BitsPerPixel ) then
  4666. exit; // Remove this to make it faster
  4667. if SrcRect = nil then
  4668. begin
  4669. with Src do
  4670. begin
  4671. x := 0;
  4672. y := 0;
  4673. w := SrcSurface.w;
  4674. h := SrcSurface.h;
  4675. end;
  4676. end
  4677. else
  4678. Src := SrcRect^;
  4679. if DestRect = nil then
  4680. begin
  4681. Dest.x := 0;
  4682. Dest.y := 0;
  4683. end
  4684. else
  4685. Dest := DestRect^;
  4686. Dest.w := Src.w;
  4687. Dest.h := Src.h;
  4688. with DestSurface.Clip_Rect do
  4689. begin
  4690. // Source's right side is greater than the dest.cliprect
  4691. if Dest.x + Src.w > x + w then
  4692. begin
  4693. smallint( Src.w ) := x + w - Dest.x;
  4694. smallint( Dest.w ) := x + w - Dest.x;
  4695. if smallint( Dest.w ) < 1 then
  4696. exit;
  4697. end;
  4698. // Source's bottom side is greater than the dest.clip
  4699. if Dest.y + Src.h > y + h then
  4700. begin
  4701. smallint( Src.h ) := y + h - Dest.y;
  4702. smallint( Dest.h ) := y + h - Dest.y;
  4703. if smallint( Dest.h ) < 1 then
  4704. exit;
  4705. end;
  4706. // Source's left side is less than the dest.clip
  4707. if Dest.x < x then
  4708. begin
  4709. Diff := x - Dest.x;
  4710. Src.x := Src.x + Diff;
  4711. smallint( Src.w ) := smallint( Src.w ) - Diff;
  4712. Dest.x := x;
  4713. smallint( Dest.w ) := smallint( Dest.w ) - Diff;
  4714. if smallint( Dest.w ) < 1 then
  4715. exit;
  4716. end;
  4717. // Source's Top side is less than the dest.clip
  4718. if Dest.y < y then
  4719. begin
  4720. Diff := y - Dest.y;
  4721. Src.y := Src.y + Diff;
  4722. smallint( Src.h ) := smallint( Src.h ) - Diff;
  4723. Dest.y := y;
  4724. smallint( Dest.h ) := smallint( Dest.h ) - Diff;
  4725. if smallint( Dest.h ) < 1 then
  4726. exit;
  4727. end;
  4728. end;
  4729. with SrcSurface^ do
  4730. begin
  4731. SrcAddr := cardinal( Pixels ) + UInt32( Src.y ) * Pitch + UInt32( Src.x ) *
  4732. Format.BytesPerPixel;
  4733. SrcMod := Pitch - Src.w * Format.BytesPerPixel;
  4734. TransparentColor := Format.colorkey;
  4735. end;
  4736. with DestSurface^ do
  4737. begin
  4738. DestAddr := cardinal( Pixels ) + UInt32( Dest.y ) * Pitch + UInt32( Dest.x ) *
  4739. Format.BytesPerPixel;
  4740. DestMod := Pitch - Dest.w * Format.BytesPerPixel;
  4741. Bits := Format.BitsPerPixel;
  4742. end;
  4743. SDL_LockSurface( SrcSurface );
  4744. SDL_LockSurface( DestSurface );
  4745. WorkY := Src.h;
  4746. case bits of
  4747. 8 :
  4748. begin
  4749. repeat
  4750. WorkX := Src.w;
  4751. repeat
  4752. Pixel1 := PUInt8( SrcAddr )^;
  4753. if ( Pixel1 <> TransparentColor ) and ( Pixel1 <> 0 ) then
  4754. begin
  4755. Pixel2 := PUInt8( DestAddr )^;
  4756. if Pixel2 > 0 then
  4757. begin
  4758. if Pixel2 and $E0 < Pixel1 and $E0 then R := Pixel2 and $E0 else R := Pixel1 and $E0;
  4759. if Pixel2 and $1C < Pixel1 and $1C then G := Pixel2 and $1C else G := Pixel1 and $1C;
  4760. if Pixel2 and $03 < Pixel1 and $03 then B := Pixel2 and $03 else B := Pixel1 and $03;
  4761. if R > $E0 then
  4762. R := $E0;
  4763. if G > $1C then
  4764. G := $1C;
  4765. if B > $03 then
  4766. B := $03;
  4767. PUInt8( DestAddr )^ := R or G or B;
  4768. end
  4769. else
  4770. PUInt8( DestAddr )^ := Pixel1;
  4771. end;
  4772. inc( SrcAddr );
  4773. inc( DestAddr );
  4774. dec( WorkX );
  4775. until WorkX = 0;
  4776. inc( SrcAddr, SrcMod );
  4777. inc( DestAddr, DestMod );
  4778. dec( WorkY );
  4779. until WorkY = 0;
  4780. end;
  4781. 15 :
  4782. begin
  4783. repeat
  4784. WorkX := Src.w;
  4785. repeat
  4786. Pixel1 := PUInt16( SrcAddr )^;
  4787. if ( Pixel1 <> TransparentColor ) and ( Pixel1 <> 0 ) then
  4788. begin
  4789. Pixel2 := PUInt16( DestAddr )^;
  4790. if Pixel2 > 0 then
  4791. begin
  4792. if Pixel2 and $7C00 < Pixel1 and $7C00 then R := Pixel2 and $7C00 else R := Pixel1 and $7C00;
  4793. if Pixel2 and $03E0 < Pixel1 and $03E0 then G := Pixel2 and $03E0 else G := Pixel1 and $03E0;
  4794. if Pixel2 and $001F < Pixel1 and $001F then B := Pixel2 and $001F else B := Pixel1 and $001F;
  4795. PUInt16( DestAddr )^ := R or G or B;
  4796. end
  4797. else
  4798. PUInt16( DestAddr )^ := Pixel1;
  4799. end;
  4800. inc( SrcAddr, 2 );
  4801. inc( DestAddr, 2 );
  4802. dec( WorkX );
  4803. until WorkX = 0;
  4804. inc( SrcAddr, SrcMod );
  4805. inc( DestAddr, DestMod );
  4806. dec( WorkY );
  4807. until WorkY = 0;
  4808. end;
  4809. 16 :
  4810. begin
  4811. repeat
  4812. WorkX := Src.w;
  4813. repeat
  4814. Pixel1 := PUInt16( SrcAddr )^;
  4815. if ( Pixel1 <> TransparentColor ) and ( Pixel1 <> 0 ) then
  4816. begin
  4817. Pixel2 := PUInt16( DestAddr )^;
  4818. if Pixel2 > 0 then
  4819. begin
  4820. if Pixel2 and $F800 < Pixel1 and $F800 then R := Pixel2 and $F800 else R := Pixel1 and $F800;
  4821. if Pixel2 and $07E0 < Pixel1 and $07E0 then G := Pixel2 and $07E0 else G := Pixel1 and $07E0;
  4822. if Pixel2 and $001F < Pixel1 and $001F then B := Pixel2 and $001F else B := Pixel1 and $001F;
  4823. PUInt16( DestAddr )^ := R or G or B;
  4824. end
  4825. else
  4826. PUInt16( DestAddr )^ := Pixel1;
  4827. end;
  4828. inc( SrcAddr, 2 );
  4829. inc( DestAddr, 2 );
  4830. dec( WorkX );
  4831. until WorkX = 0;
  4832. inc( SrcAddr, SrcMod );
  4833. inc( DestAddr, DestMod );
  4834. dec( WorkY );
  4835. until WorkY = 0;
  4836. end;
  4837. 24 :
  4838. begin
  4839. repeat
  4840. WorkX := Src.w;
  4841. repeat
  4842. Pixel1 := PUInt32( SrcAddr )^ and $00FFFFFF;
  4843. if ( Pixel1 <> TransparentColor ) and ( Pixel1 <> 0 ) then
  4844. begin
  4845. Pixel2 := PUInt32( DestAddr )^ and $00FFFFFF;
  4846. if Pixel2 > 0 then
  4847. begin
  4848. if Pixel2 and $FF0000 < Pixel1 and $FF0000 then R := Pixel2 and $FF0000 else R := Pixel1 and $FF0000;
  4849. if Pixel2 and $00FF00 < Pixel1 and $00FF00 then G := Pixel2 and $00FF00 else G := Pixel1 and $00FF00;
  4850. if Pixel2 and $0000FF < Pixel1 and $0000FF then B := Pixel2 and $0000FF else B := Pixel1 and $0000FF;
  4851. PUInt32( DestAddr )^ := PUInt32( DestAddr )^ and $FF000000 or ( R or G or B );
  4852. end
  4853. else
  4854. PUInt32( DestAddr )^ := PUInt32( DestAddr )^ and $FF000000 or Pixel1;
  4855. end;
  4856. inc( SrcAddr, 3 );
  4857. inc( DestAddr, 3 );
  4858. dec( WorkX );
  4859. until WorkX = 0;
  4860. inc( SrcAddr, SrcMod );
  4861. inc( DestAddr, DestMod );
  4862. dec( WorkY );
  4863. until WorkY = 0;
  4864. end;
  4865. 32 :
  4866. begin
  4867. repeat
  4868. WorkX := Src.w;
  4869. repeat
  4870. Pixel1 := PUInt32( SrcAddr )^;
  4871. if ( Pixel1 <> TransparentColor ) and ( Pixel1 <> 0 ) then
  4872. begin
  4873. Pixel2 := PUInt32( DestAddr )^;
  4874. if Pixel2 > 0 then
  4875. begin
  4876. if Pixel2 and $FF0000 < Pixel1 and $FF0000 then R := Pixel2 and $FF0000 else R := Pixel1 and $FF0000;
  4877. if Pixel2 and $00FF00 < Pixel1 and $00FF00 then G := Pixel2 and $00FF00 else G := Pixel1 and $00FF00;
  4878. if Pixel2 and $0000FF < Pixel1 and $0000FF then B := Pixel2 and $0000FF else B := Pixel1 and $0000FF;
  4879. PUInt32( DestAddr )^ := R or G or B;
  4880. end
  4881. else
  4882. PUInt32( DestAddr )^ := Pixel1;
  4883. end;
  4884. inc( SrcAddr, 4 );
  4885. inc( DestAddr, 4 );
  4886. dec( WorkX );
  4887. until WorkX = 0;
  4888. inc( SrcAddr, SrcMod );
  4889. inc( DestAddr, DestMod );
  4890. dec( WorkY );
  4891. until WorkY = 0;
  4892. end;
  4893. end;
  4894. SDL_UnlockSurface( SrcSurface );
  4895. SDL_UnlockSurface( DestSurface );
  4896. end;
  4897. function SDL_ClipLine(var x1,y1,x2,y2: Integer; ClipRect: PSDL_Rect) : boolean;
  4898. var tflag, flag1, flag2: word;
  4899. txy, xedge, yedge: Integer;
  4900. slope: single;
  4901. function ClipCode(x,y: Integer): word;
  4902. begin
  4903. Result := 0;
  4904. if x < ClipRect.x then Result := 1;
  4905. if x >= ClipRect.w + ClipRect.x then Result := Result or 2;
  4906. if y < ClipRect.y then Result := Result or 4;
  4907. if y >= ClipRect.h + ClipRect.y then Result := Result or 8;
  4908. end;
  4909. begin
  4910. flag1 := ClipCode(x1,y1);
  4911. flag2 := ClipCode(x2,y2);
  4912. result := true;
  4913. while true do
  4914. begin
  4915. if (flag1 or flag2) = 0 then Exit; // all in
  4916. if (flag1 and flag2) <> 0 then
  4917. begin
  4918. result := false;
  4919. Exit; // all out
  4920. end;
  4921. if flag2 = 0 then
  4922. begin
  4923. txy := x1; x1 := x2; x2 := txy;
  4924. txy := y1; y1 := y2; y2 := txy;
  4925. tflag := flag1; flag1 := flag2; flag2 := tflag;
  4926. end;
  4927. if (flag2 and 3) <> 0 then
  4928. begin
  4929. if (flag2 and 1) <> 0 then
  4930. xedge := ClipRect.x
  4931. else
  4932. xedge := ClipRect.w + ClipRect.x -1; // back 1 pixel otherwise we end up in a loop
  4933. slope := (y2 - y1) / (x2 - x1);
  4934. y2 := y1 + Round(slope * (xedge - x1));
  4935. x2 := xedge;
  4936. end
  4937. else
  4938. begin
  4939. if (flag2 and 4) <> 0 then
  4940. yedge := ClipRect.y
  4941. else
  4942. yedge := ClipRect.h + ClipRect.y -1; // up 1 pixel otherwise we end up in a loop
  4943. slope := (x2 - x1) / (y2 - y1);
  4944. x2 := x1 + Round(slope * (yedge - y1));
  4945. y2 := yedge;
  4946. end;
  4947. flag2 := ClipCode(x2, y2);
  4948. end;
  4949. end;
  4950. end.