graphics.lua 102 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743
  1. -- love.graphics
  2. --------------------------------------------------------------------------------
  3. --------------------------------------------------------------------------------
  4. ------------------------------------OBJECTS-------------------------------------
  5. --------------------------------------------------------------------------------
  6. --------------------------------------------------------------------------------
  7. -- Canvas (love.graphics.newCanvas)
  8. love.test.graphics.Canvas = function(test)
  9. -- create canvas with defaults
  10. local canvas = love.graphics.newCanvas(100, 100, {
  11. type = '2d',
  12. format = 'normal',
  13. readable = true,
  14. msaa = 0,
  15. dpiscale = love.graphics.getDPIScale(),
  16. mipmaps = 'auto'
  17. })
  18. test:assertObject(canvas)
  19. -- check dpi
  20. test:assertEquals(love.graphics.getDPIScale(), canvas:getDPIScale(), 'check dpi scale')
  21. -- check depth
  22. test:assertEquals(1, canvas:getDepth(), 'check depth is 2d')
  23. test:assertEquals(nil, canvas:getDepthSampleMode(), 'check depth sample nil')
  24. -- check fliter
  25. local min1, mag1, ani1 = canvas:getFilter()
  26. test:assertEquals('nearest', min1, 'check filter def min')
  27. test:assertEquals('nearest', mag1, 'check filter def mag')
  28. test:assertEquals(1, ani1, 'check filter def ani')
  29. canvas:setFilter('linear', 'linear', 2)
  30. local min2, mag2, ani2 = canvas:getFilter()
  31. test:assertEquals('linear', min2, 'check filter changed min')
  32. test:assertEquals('linear', mag2, 'check filter changed mag')
  33. test:assertEquals(2, ani2, 'check filter changed ani')
  34. -- check layer
  35. test:assertEquals(1, canvas:getLayerCount(), 'check 1 layer for 2d')
  36. -- check texture type
  37. test:assertEquals('2d', canvas:getTextureType(), 'check 2d')
  38. -- check texture wrap
  39. local horiz1, vert1 = canvas:getWrap()
  40. test:assertEquals('clamp', horiz1, 'check def wrap h')
  41. test:assertEquals('clamp', vert1, 'check def wrap v')
  42. canvas:setWrap('repeat', 'repeat')
  43. local horiz2, vert2 = canvas:getWrap()
  44. test:assertEquals('repeat', horiz2, 'check changed wrap h')
  45. test:assertEquals('repeat', vert2, 'check changed wrap v')
  46. -- check readable
  47. test:assertTrue(canvas:isReadable(), 'check canvas readable')
  48. -- check msaa
  49. test:assertEquals(1, canvas:getMSAA(), 'check samples match')
  50. -- check dimensions
  51. local cw, ch = canvas:getDimensions()
  52. test:assertEquals(100, cw, 'check canvas dim w')
  53. test:assertEquals(100, ch, 'check canvas dim h')
  54. test:assertEquals(cw, canvas:getWidth(), 'check canvas w matches dim')
  55. test:assertEquals(ch, canvas:getHeight(), 'check canvas h matches dim')
  56. local pw, ph = canvas:getPixelDimensions()
  57. test:assertEquals(100*love.graphics.getDPIScale(), pw, 'check pixel dim w')
  58. test:assertEquals(100*love.graphics.getDPIScale(), ph, 'check pixel dim h')
  59. test:assertEquals(pw, canvas:getPixelWidth(), 'check pixel w matches dim')
  60. test:assertEquals(ph, canvas:getPixelHeight(), 'check pixel h matches dim')
  61. -- check mipmaps
  62. local mode, sharpness = canvas:getMipmapFilter()
  63. test:assertEquals('linear', mode, 'check def minmap filter mode')
  64. test:assertEquals(0, sharpness, 'check def minmap filter sharpness')
  65. local name, version, vendor, device = love.graphics.getRendererInfo()
  66. canvas:setMipmapFilter('nearest', 1)
  67. mode, sharpness = canvas:getMipmapFilter()
  68. test:assertEquals('nearest', mode, 'check changed minmap filter mode')
  69. -- @NOTE mipmap sharpness wont work on opengl/metal
  70. if string.match(name, 'OpenGL ES') == nil and string.match(name, 'Metal') == nil then
  71. test:assertEquals(1, sharpness, 'check changed minmap filter sharpness')
  72. end
  73. test:assertGreaterEqual(2, canvas:getMipmapCount()) -- docs say no mipmaps should return 1
  74. test:assertEquals('auto', canvas:getMipmapMode())
  75. -- check basic rendering
  76. canvas:renderTo(function()
  77. love.graphics.setColor(1, 0, 0)
  78. love.graphics.rectangle('fill', 0, 0, 200, 200)
  79. love.graphics.setColor(1, 1, 1, 1)
  80. end)
  81. local imgdata1 = love.graphics.readbackTexture(canvas, {100, 0, 0, 0, 100, 100})
  82. test:assertPixels(imgdata1, {
  83. red = {{0, 0},{0,99},{99,0},{99,99}},
  84. }, 'font draw check')
  85. test:compareImg(imgdata1)
  86. -- check using canvas in love.graphics.draw()
  87. local xcanvas = love.graphics.newCanvas()
  88. love.graphics.setCanvas(xcanvas)
  89. love.graphics.draw(canvas, 0, 0)
  90. love.graphics.setCanvas()
  91. local imgdata2 = love.graphics.readbackTexture(canvas, {100, 0, 0, 0, 100, 100})
  92. test:assertPixels(imgdata2, {
  93. red = {{0, 0},{0,99},{99,0},{99,99}},
  94. }, 'font draw check')
  95. test:compareImg(imgdata2)
  96. -- check depth samples
  97. local dcanvas = love.graphics.newCanvas(100, 100, {
  98. type = '2d',
  99. format = 'depth16',
  100. readable = true
  101. })
  102. test:assertEquals(nil, dcanvas:getDepthSampleMode(), 'check depth sample mode nil by def')
  103. dcanvas:setDepthSampleMode('equal')
  104. test:assertEquals('equal', dcanvas:getDepthSampleMode(), 'check depth sample mode set')
  105. end
  106. -- Font (love.graphics.newFont)
  107. love.test.graphics.Font = function(test)
  108. -- create obj
  109. local font = love.graphics.newFont('resources/font.ttf', 8)
  110. test:assertObject(font)
  111. -- check ascent/descent
  112. test:assertEquals(6, font:getAscent(), 'check ascent')
  113. test:assertEquals(-2, font:getDescent(), 'check descent')
  114. -- check baseline
  115. test:assertEquals(6, font:getBaseline(), 'check baseline')
  116. -- check dpi
  117. test:assertEquals(1, font:getDPIScale(), 'check dpi')
  118. -- check filter
  119. test:assertEquals('nearest', font:getFilter(), 'check filter def')
  120. font:setFilter('linear', 'linear')
  121. test:assertEquals('linear', font:getFilter(), 'check filter change')
  122. font:setFilter('nearest', 'nearest')
  123. -- check height + lineheight
  124. test:assertEquals(8, font:getHeight(), 'check height')
  125. test:assertEquals(1, font:getLineHeight(), 'check line height')
  126. font:setLineHeight(2)
  127. test:assertEquals(2, font:getLineHeight(), 'check changed line height')
  128. font:setLineHeight(1) -- reset for drawing + wrap later
  129. -- check width + kerning
  130. test:assertEquals(0, font:getKerning('a', 'b'), 'check kerning')
  131. test:assertEquals(24, font:getWidth('test'), 'check data size')
  132. -- check specific glyphs
  133. test:assertTrue(font:hasGlyphs('test'), 'check data size')
  134. -- check font wrapping
  135. local width, wrappedtext = font:getWrap('LÖVE is an *awesome* framework you can use to make 2D games in Lua.', 50)
  136. test:assertEquals(48, width, 'check actual wrap width')
  137. test:assertEquals(8, #wrappedtext, 'check wrapped lines')
  138. test:assertEquals('LÖVE is an ', wrappedtext[1], 'check wrapped line')
  139. -- check drawing font
  140. local canvas = love.graphics.newCanvas(16, 16)
  141. love.graphics.setCanvas(canvas)
  142. love.graphics.setFont(font)
  143. love.graphics.print('Aa', 0, 5)
  144. love.graphics.setCanvas()
  145. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  146. test:assertPixels(imgdata, {
  147. white = {{0,3},{4,3},{7,4},{9,4},{10,5},{0,8},{4,8},{10,8}},
  148. }, 'font draw check')
  149. test:compareImg(imgdata)
  150. -- check font substitution
  151. local fontab = love.graphics.newImageFont('resources/font-letters-ab.png', 'AB')
  152. local fontcd = love.graphics.newImageFont('resources/font-letters-cd.png', 'CD')
  153. fontab:setFallbacks(fontcd)
  154. love.graphics.setCanvas(canvas)
  155. love.graphics.clear(0, 0, 0, 0)
  156. love.graphics.setFont(fontab)
  157. love.graphics.print('AB', 0, 0) -- should come from fontab
  158. love.graphics.print('CD', 0, 9) -- should come from fontcd
  159. love.graphics.setCanvas()
  160. local imgdata2 = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  161. test:assertPixels(imgdata2, {
  162. green = {{1,8},{6,8},{2,10},{5,10},{9,10}},
  163. black = {{9,9},{14,8},{14,10},{14,1},{1,10}}
  164. }, 'font draw check')
  165. test:compareImg(imgdata2)
  166. end
  167. -- Image (love.graphics.newImage)
  168. love.test.graphics.Image = function(test)
  169. -- create object
  170. local image = love.graphics.newImage('resources/love.png', {
  171. dpiscale = 1,
  172. mipmaps = true
  173. })
  174. test:assertObject(image)
  175. -- check dpi
  176. test:assertEquals(love.graphics.getDPIScale(), image:getDPIScale(), 'check dpi scale')
  177. -- check depth
  178. test:assertEquals(1, image:getDepth(), 'check depth is 2d')
  179. test:assertEquals(nil, image:getDepthSampleMode(), 'check depth sample nil')
  180. -- check filter
  181. local min1, mag1, ani1 = image:getFilter()
  182. test:assertEquals('nearest', min1, 'check filter def min')
  183. test:assertEquals('nearest', mag1, 'check filter def mag')
  184. test:assertEquals(1, ani1, 'check filter def ani')
  185. image:setFilter('linear', 'linear', 2)
  186. local min2, mag2, ani2 = image:getFilter()
  187. test:assertEquals('linear', min2, 'check filter changed min')
  188. test:assertEquals('linear', mag2, 'check filter changed mag')
  189. test:assertEquals(2, ani2, 'check filter changed ani')
  190. image:setFilter('nearest', 'nearest', 1)
  191. -- check layers
  192. test:assertEquals(1, image:getLayerCount(), 'check 1 layer for 2d')
  193. -- check texture type
  194. test:assertEquals('2d', image:getTextureType(), 'check 2d')
  195. -- check texture wrapping
  196. local horiz1, vert1 = image:getWrap()
  197. test:assertEquals('clamp', horiz1, 'check def wrap h')
  198. test:assertEquals('clamp', vert1, 'check def wrap v')
  199. image:setWrap('repeat', 'repeat')
  200. local horiz2, vert2 = image:getWrap()
  201. test:assertEquals('repeat', horiz2, 'check changed wrap h')
  202. test:assertEquals('repeat', vert2, 'check changed wrap v')
  203. -- check readable
  204. test:assertTrue(image:isReadable(), 'check canvas readable')
  205. -- check msaa
  206. test:assertEquals(1, image:getMSAA(), 'check samples match')
  207. -- check dimensions
  208. local cw, ch = image:getDimensions()
  209. test:assertEquals(64, cw, 'check canvas dim w')
  210. test:assertEquals(64, ch, 'check canvas dim h')
  211. test:assertEquals(cw, image:getWidth(), 'check canvas w matches dim')
  212. test:assertEquals(ch, image:getHeight(), 'check canvas h matches dim')
  213. local pw, ph = image:getPixelDimensions()
  214. test:assertEquals(64*love.graphics.getDPIScale(), pw, 'check pixel dim w')
  215. test:assertEquals(64*love.graphics.getDPIScale(), ph, 'check pixel dim h')
  216. test:assertEquals(pw, image:getPixelWidth(), 'check pixel w matches dim')
  217. test:assertEquals(ph, image:getPixelHeight(), 'check pixel h matches dim')
  218. -- check mipmaps
  219. local mode, sharpness = image:getMipmapFilter()
  220. test:assertEquals('linear', mode, 'check def minmap filter mode')
  221. test:assertEquals(0, sharpness, 'check def minmap filter sharpness')
  222. local name, version, vendor, device = love.graphics.getRendererInfo()
  223. -- @note mipmap sharpness wont work on opengl/metal
  224. image:setMipmapFilter('nearest', 1)
  225. mode, sharpness = image:getMipmapFilter()
  226. test:assertEquals('nearest', mode, 'check changed minmap filter mode')
  227. if string.match(name, 'OpenGL ES') == nil and string.match(name, 'Metal') == nil then
  228. test:assertEquals(1, sharpness, 'check changed minmap filter sharpness')
  229. end
  230. test:assertGreaterEqual(2, image:getMipmapCount()) -- docs say no mipmaps should return 1?
  231. -- check image properties
  232. test:assertFalse(image:isCompressed(), 'check not compressed')
  233. test:assertFalse(image:isFormatLinear(), 'check not linear')
  234. local cimage = love.graphics.newImage('resources/love.dxt1')
  235. test:assertObject(cimage)
  236. test:assertTrue(cimage:isCompressed(), 'check is compressed')
  237. -- check pixel replacement
  238. local rimage = love.image.newImageData('resources/loveinv.png')
  239. image:replacePixels(rimage)
  240. local canvas = love.graphics.newCanvas(64, 64)
  241. love.graphics.setCanvas(canvas)
  242. love.graphics.draw(image, 0, 0)
  243. love.graphics.setCanvas()
  244. local imgdata = love.graphics.readbackTexture(canvas, {64, 0, 0, 0, 64, 64})
  245. local r1, g1, b1 = imgdata:getPixel(25, 25)
  246. test:assertEquals(3, r1+g1+b1, 'check back to white')
  247. test:compareImg(imgdata)
  248. end
  249. -- Mesh (love.graphics.newMesh)
  250. love.test.graphics.Mesh = function(test)
  251. -- create 2d mesh with pretty colors
  252. local image = love.graphics.newImage('resources/love.png')
  253. local vertices = {
  254. { 0, 0, 0, 0, 1, 0, 0 },
  255. { image:getWidth(), 0, 1, 0, 0, 1, 0 },
  256. { image:getWidth(), image:getHeight(), 1, 1, 0, 0, 1 },
  257. { 0, image:getHeight(), 0, 1, 1, 1, 0 },
  258. }
  259. local mesh1 = love.graphics.newMesh(vertices, 'fan')
  260. test:assertObject(mesh1)
  261. -- check draw mode
  262. test:assertEquals('fan', mesh1:getDrawMode(), 'check draw mode')
  263. mesh1:setDrawMode('triangles')
  264. test:assertEquals('triangles', mesh1:getDrawMode(), 'check draw mode set')
  265. -- check draw range
  266. local min1, max1 = mesh1:getDrawRange()
  267. test:assertEquals(nil, min1, 'check draw range not set')
  268. mesh1:setDrawRange(1, 10)
  269. local min2, max2 = mesh1:getDrawRange()
  270. test:assertEquals(1, min2, 'check draw range set min')
  271. test:assertEquals(10, max2, 'check draw range set max')
  272. -- check texture pointer
  273. test:assertEquals(nil, mesh1:getTexture(), 'check no texture')
  274. mesh1:setTexture(image)
  275. test:assertEquals(image:getHeight(), mesh1:getTexture():getHeight(), 'check texture match w')
  276. test:assertEquals(image:getWidth(), mesh1:getTexture():getWidth(), 'check texture match h')
  277. -- check vertext count
  278. test:assertEquals(4, mesh1:getVertexCount(), 'check vertex count')
  279. -- check def vertex format
  280. local format = mesh1:getVertexFormat()
  281. test:assertEquals('floatvec2', format[2][2], 'check def vertex format 2')
  282. test:assertEquals('VertexColor', format[3][1], 'check def vertex format 3')
  283. -- check vertext attributes
  284. test:assertTrue(mesh1:isAttributeEnabled('VertexPosition'), 'check def attribute VertexPosition')
  285. test:assertTrue(mesh1:isAttributeEnabled('VertexTexCoord'), 'check def attribute VertexTexCoord')
  286. test:assertTrue(mesh1:isAttributeEnabled('VertexColor'), 'check def attribute VertexColor')
  287. mesh1:setAttributeEnabled('VertexPosition', false)
  288. mesh1:setAttributeEnabled('VertexTexCoord', false)
  289. mesh1:setAttributeEnabled('VertexColor', false)
  290. test:assertFalse(mesh1:isAttributeEnabled('VertexPosition'), 'check disable attribute VertexPosition')
  291. test:assertFalse(mesh1:isAttributeEnabled('VertexTexCoord'), 'check disable attribute VertexTexCoord')
  292. test:assertFalse(mesh1:isAttributeEnabled('VertexColor'), 'check disable attribute VertexColor')
  293. -- check vertex itself
  294. local x1, y1, u1, v1, r1, g1, b1, a1 = mesh1:getVertex(1)
  295. test:assertEquals(0, x1, 'check vertex props x')
  296. test:assertEquals(0, y1, 'check vertex props y')
  297. test:assertEquals(0, u1, 'check vertex props u')
  298. test:assertEquals(0, v1, 'check vertex props v')
  299. test:assertEquals(1, r1, 'check vertex props r')
  300. test:assertEquals(0, g1, 'check vertex props g')
  301. test:assertEquals(0, b1, 'check vertex props b')
  302. test:assertEquals(1, a1, 'check vertex props a')
  303. -- check setting a specific vertex
  304. mesh1:setVertex(2, image:getWidth(), 0, 1, 0, 0, 1, 1, 1)
  305. local x2, y2, u2, v2, r2, g2, b2, a2 = mesh1:getVertex(2)
  306. test:assertEquals(image:getWidth(), x2, 'check changed vertex props x')
  307. test:assertEquals(0, y2, 'check changed vertex props y')
  308. test:assertEquals(1, u2, 'check changed vertex props u')
  309. test:assertEquals(0, v2, 'check changed vertex props v')
  310. test:assertEquals(0, r2, 'check changed vertex props r')
  311. test:assertEquals(1, g2, 'check changed vertex props g')
  312. test:assertEquals(1, b2, 'check changed vertex props b')
  313. test:assertEquals(1, a2, 'check changed vertex props a')
  314. -- check setting a specific vertex attribute
  315. local r3, g3, b3, a3 = mesh1:getVertexAttribute(3, 3)
  316. test:assertEquals(1, b3, 'check specific vertex color')
  317. mesh1:setVertexAttribute(4, 3, 1, 0, 1)
  318. local r4, g4, b4, a4 = mesh1:getVertexAttribute(4, 3)
  319. test:assertEquals(0, g4, 'check changed vertex color')
  320. -- check setting a vertice
  321. mesh1:setVertices(vertices)
  322. local r5, g5, b5, a5 = mesh1:getVertexAttribute(4, 3)
  323. local x6, y6, u6, v6, r6, g6, b6, a6 = mesh1:getVertex(2)
  324. test:assertEquals(1, g5, 'check reset vertex color 1')
  325. test:assertEquals(0, b5, 'check reset vertex color 2')
  326. -- check setting the vertex map
  327. local vmap1 = mesh1:getVertexMap()
  328. test:assertEquals(nil, vmap1, 'check no map by def')
  329. mesh1:setVertexMap({4, 1, 2, 3})
  330. local vmap2 = mesh1:getVertexMap()
  331. test:assertEquals(4, #vmap2, 'check set map len')
  332. test:assertEquals(2, vmap2[3], 'check set map val')
  333. -- check using custom attributes
  334. local mesh2 = love.graphics.newMesh({
  335. { name = 'VertexPosition', format = 'floatvec2'},
  336. { name = 'VertexTexCoord', format = 'floatvec2'},
  337. { name = 'VertexColor', format = 'floatvec4'},
  338. { name = 'CustomValue1', format = 'floatvec2'},
  339. { name = 'CustomValue2', format = 'uint16'}
  340. }, {
  341. { 0, 0, 0, 0, 1, 0, 0, 1, 2, 1, 1005 },
  342. { image:getWidth(), 0, 1, 0, 0, 1, 0, 0, 2, 2, 2005 },
  343. { image:getWidth(), image:getHeight(), 1, 1, 0, 0, 1, 0, 2, 3, 3005 },
  344. { 0, image:getHeight(), 0, 1, 1, 1, 0, 0, 2, 4, 4005 },
  345. }, 'fan')
  346. local c1, c2 = mesh2:getVertexAttribute(1, 4)
  347. local c3 = mesh2:getVertexAttribute(1, 5)
  348. test:assertEquals(2, c1, 'check custom attribute val 1')
  349. test:assertEquals(1, c2, 'check custom attribute val 2')
  350. test:assertEquals(1005, c3, 'check custom attribute val 3')
  351. -- check attaching custom attribute + detaching
  352. mesh1:attachAttribute('CustomValue1', mesh2)
  353. test:assertTrue(mesh1:isAttributeEnabled('CustomValue1'), 'check custom attribute attached')
  354. mesh1:detachAttribute('CustomValue1')
  355. local obj, err = pcall(mesh1.isAttributeEnabled, mesh1, 'CustomValue1')
  356. test:assertNotEquals(nil, err, 'check attribute detached')
  357. mesh1:detachAttribute('VertexPosition')
  358. test:assertTrue(mesh1:isAttributeEnabled('VertexPosition'), 'check cant detach def attribute')
  359. end
  360. -- ParticleSystem (love.graphics.newParticleSystem)
  361. love.test.graphics.ParticleSystem = function(test)
  362. -- create new system
  363. local image = love.graphics.newImage('resources/pixel.png')
  364. local quad1 = love.graphics.newQuad(0, 0, 1, 1, image)
  365. local quad2 = love.graphics.newQuad(0, 0, 1, 1, image)
  366. local psystem = love.graphics.newParticleSystem(image, 1000)
  367. test:assertObject(psystem)
  368. -- check psystem state properties
  369. psystem:start()
  370. psystem:update(1)
  371. test:assertTrue(psystem:isActive(), 'check active')
  372. test:assertFalse(psystem:isPaused(), 'checked not paused by def')
  373. test:assertFalse(psystem:hasRelativeRotation(), 'check rel rot def')
  374. psystem:pause()
  375. test:assertTrue(psystem:isPaused(), 'check now paused')
  376. test:assertFalse(psystem:isStopped(), 'check not stopped by def')
  377. psystem:stop()
  378. test:assertTrue(psystem:isStopped(), 'check now stopped')
  379. psystem:start()
  380. psystem:reset()
  381. -- check emitting some particles
  382. -- need to set a lifespan at minimum or none will be counted
  383. local min, max = psystem:getParticleLifetime()
  384. test:assertEquals(0, min, 'check def lifetime min')
  385. test:assertEquals(0, max, 'check def lifetime max')
  386. psystem:setParticleLifetime(1, 2)
  387. psystem:emit(10)
  388. psystem:update(1)
  389. test:assertEquals(10, psystem:getCount(), 'check added particles')
  390. psystem:reset()
  391. test:assertEquals(0, psystem:getCount(), 'check reset')
  392. -- check setting colors
  393. local colors1 = {psystem:getColors()}
  394. test:assertEquals(1, #colors1, 'check 1 color by def')
  395. psystem:setColors(1, 1, 1, 1, 1, 0, 0, 1)
  396. local colors2 = {psystem:getColors()}
  397. test:assertEquals(2, #colors2, 'check set colors')
  398. test:assertEquals(1, colors2[2][1], 'check set color')
  399. -- check setting direction
  400. test:assertEquals(0, psystem:getDirection(), 'check def direction')
  401. psystem:setDirection(90 * (math.pi/180))
  402. test:assertEquals(math.floor(math.pi/2*100), math.floor(psystem:getDirection()*100), 'check set direction')
  403. -- check emission area options
  404. psystem:setEmissionArea('normal', 100, 50)
  405. psystem:setEmissionArea('ellipse', 100, 50)
  406. psystem:setEmissionArea('borderellipse', 100, 50)
  407. psystem:setEmissionArea('borderrectangle', 100, 50)
  408. psystem:setEmissionArea('none', 100, 50)
  409. psystem:setEmissionArea('uniform', 100, 50)
  410. local dist, dx, dy, angle, rel = psystem:getEmissionArea()
  411. test:assertEquals('uniform', dist, 'check emission area dist')
  412. test:assertEquals(100, dx, 'check emission area dx')
  413. test:assertEquals(50, dy, 'check emission area dy')
  414. test:assertEquals(0, angle, 'check emission area angle')
  415. test:assertFalse(rel, 'check emission area rel')
  416. -- check emission rate
  417. test:assertEquals(0, psystem:getEmissionRate(), 'check def emission rate')
  418. psystem:setEmissionRate(1)
  419. test:assertEquals(1, psystem:getEmissionRate(), 'check changed emission rate')
  420. -- check emission lifetime
  421. test:assertEquals(-1, psystem:getEmitterLifetime(), 'check def emitter life')
  422. psystem:setEmitterLifetime(10)
  423. test:assertEquals(10, psystem:getEmitterLifetime(), 'check changed emitter life')
  424. -- check insert mode
  425. test:assertEquals('top', psystem:getInsertMode(), 'check def insert mode')
  426. psystem:setInsertMode('bottom')
  427. psystem:setInsertMode('random')
  428. test:assertEquals('random', psystem:getInsertMode(), 'check change insert mode')
  429. -- check linear acceleration
  430. local xmin1, ymin1, xmax1, ymax1 = psystem:getLinearAcceleration()
  431. test:assertEquals(0, xmin1, 'check def lin acceleration xmin')
  432. test:assertEquals(0, ymin1, 'check def lin acceleration ymin')
  433. test:assertEquals(0, xmax1, 'check def lin acceleration xmax')
  434. test:assertEquals(0, ymax1, 'check def lin acceleration ymax')
  435. psystem:setLinearAcceleration(1, 2, 3, 4)
  436. local xmin2, ymin2, xmax2, ymax2 = psystem:getLinearAcceleration()
  437. test:assertEquals(1, xmin2, 'check change lin acceleration xmin')
  438. test:assertEquals(2, ymin2, 'check change lin acceleration ymin')
  439. test:assertEquals(3, xmax2, 'check change lin acceleration xmax')
  440. test:assertEquals(4, ymax2, 'check change lin acceleration ymax')
  441. -- check linear damping
  442. local min3, max3 = psystem:getLinearDamping()
  443. test:assertEquals(0, min3, 'check def lin damping min')
  444. test:assertEquals(0, max3, 'check def lin damping max')
  445. psystem:setLinearDamping(1, 2)
  446. local min4, max4 = psystem:getLinearDamping()
  447. test:assertEquals(1, min4, 'check change lin damping min')
  448. test:assertEquals(2, max4, 'check change lin damping max')
  449. -- check offset
  450. local ox1, oy1 = psystem:getOffset()
  451. test:assertEquals(0.5, ox1, 'check def offset x') -- 0.5 cos middle of pixel image which is 1x1
  452. test:assertEquals(0.5, oy1, 'check def offset y')
  453. psystem:setOffset(0, 10)
  454. local ox2, oy2 = psystem:getOffset()
  455. test:assertEquals(0, ox2, 'check change offset x')
  456. test:assertEquals(10, oy2, 'check change offset y')
  457. -- check lifetime (we set it earlier)
  458. local min5, max5 = psystem:getParticleLifetime()
  459. test:assertEquals(1, min5, 'check p lifetime min')
  460. test:assertEquals(2, max5, 'check p lifetime max')
  461. -- check position
  462. local x1, y1 = psystem:getPosition()
  463. test:assertEquals(0, x1, 'check emitter x')
  464. test:assertEquals(0, y1, 'check emitter y')
  465. psystem:setPosition(10, 12)
  466. local x2, y2 = psystem:getPosition()
  467. test:assertEquals(10, x2, 'check set emitter x')
  468. test:assertEquals(12, y2, 'check set emitter y')
  469. -- check quads
  470. test:assertEquals(0, #psystem:getQuads(), 'check def quads')
  471. psystem:setQuads({quad1})
  472. psystem:setQuads(quad1, quad2)
  473. test:assertEquals(2, #psystem:getQuads(), 'check set quads')
  474. -- check radial acceleration
  475. local min6, max6 = psystem:getRadialAcceleration()
  476. test:assertEquals(0, min6, 'check def rad accel min')
  477. test:assertEquals(0, max6, 'check def rad accel max')
  478. psystem:setRadialAcceleration(1, 2)
  479. local min7, max7 = psystem:getRadialAcceleration()
  480. test:assertEquals(1, min7, 'check change rad accel min')
  481. test:assertEquals(2, max7, 'check change rad accel max')
  482. -- check rotation
  483. local min8, max8 = psystem:getRotation()
  484. test:assertEquals(0, min8, 'check def rot min')
  485. test:assertEquals(0, max8, 'check def rot max')
  486. psystem:setRotation(90 * (math.pi/180), 180 * (math.pi/180))
  487. local min8, max8 = psystem:getRotation()
  488. test:assertEquals(math.floor(math.pi/2*100), math.floor(min8*100), 'check set rot min')
  489. test:assertEquals(math.floor(math.pi*100), math.floor(max8*100), 'check set rot max')
  490. -- check variation
  491. test:assertEquals(0, psystem:getSizeVariation(), 'check def variation')
  492. psystem:setSizeVariation(1)
  493. test:assertEquals(1, psystem:getSizeVariation(), 'check change variation')
  494. -- check sizes
  495. test:assertEquals(1, #{psystem:getSizes()}, 'check def size')
  496. psystem:setSizes(1, 2, 4, 1, 3, 2)
  497. local sizes = {psystem:getSizes()}
  498. test:assertEquals(6, #sizes, 'check set sizes')
  499. test:assertEquals(3, sizes[5], 'check set size')
  500. -- check speed
  501. local min9, max9 = psystem:getSpeed()
  502. test:assertEquals(0, min9, 'check def speed min')
  503. test:assertEquals(0, max9, 'check def speed max')
  504. psystem:setSpeed(1, 10)
  505. local min10, max10 = psystem:getSpeed()
  506. test:assertEquals(1, min10, 'check change speed min')
  507. test:assertEquals(10, max10, 'check change speed max')
  508. -- check variation + spin
  509. local variation = psystem:getSpinVariation()
  510. test:assertEquals(0, variation, 'check def spin variation')
  511. psystem:setSpinVariation(1)
  512. test:assertEquals(1, psystem:getSpinVariation(), 'check change spin variation')
  513. psystem:setSpin(1, 2)
  514. local min11, max11 = psystem:getSpin()
  515. test:assertEquals(1, min11, 'check change spin min')
  516. test:assertEquals(2, max11, 'check change spin max')
  517. -- check spread
  518. test:assertEquals(0, psystem:getSpread(), 'check def spread')
  519. psystem:setSpread(90 * (math.pi/180))
  520. test:assertEquals(math.floor(math.pi/2*100), math.floor(psystem:getSpread()*100), 'check change spread')
  521. -- tangential acceleration
  522. local min12, max12 = psystem:getTangentialAcceleration()
  523. test:assertEquals(0, min12, 'check def tan accel min')
  524. test:assertEquals(0, max12, 'check def tan accel max')
  525. psystem:setTangentialAcceleration(1, 2)
  526. local min13, max13 = psystem:getTangentialAcceleration()
  527. test:assertEquals(1, min13, 'check change tan accel min')
  528. test:assertEquals(2, max13, 'check change tan accel max')
  529. -- check texture
  530. test:assertNotEquals(nil, psystem:getTexture(), 'check texture obj')
  531. test:assertObject(psystem:getTexture())
  532. psystem:setTexture(love.graphics.newImage('resources/love.png'))
  533. test:assertObject(psystem:getTexture())
  534. -- try a graphics test!
  535. -- hard to get exactly because of the variation but we can use some pixel
  536. -- tolerance and volume to try and cover the randomness
  537. local psystem2 = love.graphics.newParticleSystem(image, 5000)
  538. psystem2:setEmissionArea('uniform', 2, 64)
  539. psystem2:setColors(1, 0, 0, 1)
  540. psystem2:setDirection(0 * math.pi/180)
  541. psystem2:setEmitterLifetime(100)
  542. psystem2:setEmissionRate(5000)
  543. local psystem3 = psystem2:clone()
  544. psystem3:setPosition(64, 0)
  545. psystem3:setColors(0, 1, 0, 1)
  546. psystem3:setDirection(180 * (math.pi/180))
  547. psystem2:start()
  548. psystem3:start()
  549. psystem2:update(1)
  550. psystem3:update(1)
  551. local canvas = love.graphics.newCanvas(64, 64)
  552. love.graphics.setCanvas(canvas)
  553. love.graphics.clear(0, 0, 0, 1)
  554. love.graphics.draw(psystem2, 0, 0)
  555. love.graphics.draw(psystem3, 0, 0)
  556. love.graphics.setCanvas()
  557. -- this should result in a bunch of red pixels on the left 2px of the canvas
  558. -- and a bunch of green pixels on the right 2px of the canvas
  559. local imgdata = love.graphics.readbackTexture(canvas, {64, 0, 0, 0, 64, 64})
  560. test.pixel_tolerance = 1
  561. test:compareImg(imgdata)
  562. end
  563. -- Quad (love.graphics.newQuad)
  564. love.test.graphics.Quad = function(test)
  565. -- create quad obj
  566. local texture = love.graphics.newImage('resources/love.png')
  567. local quad = love.graphics.newQuad(0, 0, 32, 32, texture)
  568. test:assertObject(quad)
  569. -- check properties
  570. test:assertEquals(1, quad:getLayer(), 'check default layer')
  571. quad:setLayer(2)
  572. test:assertEquals(2, quad:getLayer(), 'check changed layer')
  573. local sw, sh = quad:getTextureDimensions()
  574. test:assertEquals(64, sw, 'check texture w')
  575. test:assertEquals(64, sh, 'check texture h')
  576. -- check drawing and viewport changes
  577. local canvas = love.graphics.newCanvas(64, 64)
  578. love.graphics.setCanvas(canvas)
  579. love.graphics.draw(texture, quad, 0, 0)
  580. quad:setViewport(32, 32, 32, 32, 64, 64)
  581. love.graphics.draw(texture, quad, 32, 32)
  582. love.graphics.setCanvas()
  583. local imgdata = love.graphics.readbackTexture(canvas, {64, 0, 0, 0, 64, 64})
  584. test:assertPixels(imgdata, {
  585. white = {{17,31},{31,31},{31,24},{32,32},{46,32},{32,46}},
  586. lovepink = {{2,31},{31,2}},
  587. loveblue = {{32,61},{61,32}}
  588. }, 'check quad drawing')
  589. test:compareImg(imgdata)
  590. end
  591. -- Shader (love.graphics.newShader)
  592. love.test.graphics.Shader = function(test)
  593. -- check valid shader
  594. local pixelcode1 = [[
  595. extern Image tex2;
  596. vec4 effect(vec4 color, Image tex, vec2 texture_coords, vec2 screen_coords) {
  597. vec4 texturecolor = Texel(tex2, texture_coords);
  598. return texturecolor * color;
  599. }
  600. ]]
  601. local vertexcode1 = [[
  602. vec4 position(mat4 transform_projection, vec4 vertex_position) {
  603. return transform_projection * vertex_position;
  604. }
  605. ]]
  606. local shader1 = love.graphics.newShader(pixelcode1, vertexcode1)
  607. test:assertObject(shader1)
  608. test:assertEquals('vertex shader:\npixel shader:\n', shader1:getWarnings(), 'check shader valid')
  609. test:assertFalse(shader1:hasUniform('tex1'), 'check invalid uniform')
  610. test:assertTrue(shader1:hasUniform('tex2'), 'check valid uniform')
  611. -- check invalid shader
  612. local pixelcode2 = [[
  613. extern float ww;
  614. vec4 effect(vec4 color, Image tex, vec2 texture_coords, vec2 screen_coords) {
  615. vec4 texturecolor = Texel(tex, texture_coords);
  616. float unused = ww * 3 * color;
  617. return texturecolor * color;
  618. }
  619. ]]
  620. local res, err = pcall(love.graphics.newShader, pixelcode2, vertexcode1)
  621. test:assertNotEquals(nil, err, 'check shader compile fails')
  622. -- check using a shader to draw + sending uniforms
  623. -- shader will return a given color if overwrite set to 1, otherwise def. draw
  624. local pixelcode3 = [[
  625. extern vec4 col;
  626. extern float overwrite;
  627. vec4 effect(vec4 color, Image tex, vec2 texture_coords, vec2 screen_coords) {
  628. vec4 texcol = Texel(tex, texture_coords);
  629. if (overwrite == 1.0) {
  630. return col;
  631. } else {
  632. return texcol * color;
  633. }
  634. }
  635. ]]
  636. local shader3 = love.graphics.newShader(pixelcode3, vertexcode1)
  637. local canvas = love.graphics.newCanvas(16, 16)
  638. love.graphics.setCanvas(canvas)
  639. -- set color to yellow
  640. love.graphics.setColor(1, 1, 0, 1)
  641. -- turn shader 'on' and use red to draw
  642. shader3:send('overwrite', 1)
  643. shader3:sendColor('col', {1, 0, 0, 1})
  644. love.graphics.setShader(shader3)
  645. love.graphics.rectangle('fill', 0, 0, 8, 8)
  646. love.graphics.setShader()
  647. -- turn shader 'off' and draw again
  648. shader3:send('overwrite', 0)
  649. love.graphics.setShader(shader3)
  650. love.graphics.rectangle('fill', 8, 8, 8, 8)
  651. love.graphics.setShader()
  652. love.graphics.setColor(1, 1, 1, 1)
  653. love.graphics.setCanvas()
  654. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  655. test:assertPixels(imgdata, {
  656. red = {{1,1},{1,7},{7,7},{7,1}},
  657. yellow = {{8,8},{8,15},{15,15},{15,8}}
  658. }, 'shader draw check')
  659. test:compareImg(imgdata)
  660. end
  661. -- SpriteBatch (love.graphics.newSpriteBatch)
  662. love.test.graphics.SpriteBatch = function(test)
  663. -- create batch
  664. local texture1 = love.graphics.newImage('resources/cubemap.png')
  665. local texture2 = love.graphics.newImage('resources/love.png')
  666. local quad1 = love.graphics.newQuad(32, 12, 1, 1, texture2) -- lovepink
  667. local quad2 = love.graphics.newQuad(32, 32, 1, 1, texture2) -- white
  668. local sbatch = love.graphics.newSpriteBatch(texture1, 5000)
  669. test:assertObject(sbatch)
  670. -- check initial count
  671. test:assertEquals(0, sbatch:getCount(), 'check batch size')
  672. -- check buffer size
  673. test:assertEquals(5000, sbatch:getBufferSize(), 'check batch size')
  674. -- check height/width/texture
  675. test:assertEquals(texture1:getWidth(), sbatch:getTexture():getWidth(), 'check texture match w')
  676. test:assertEquals(texture1:getHeight(), sbatch:getTexture():getHeight(), 'check texture match h')
  677. sbatch:setTexture(texture2)
  678. test:assertEquals(texture2:getWidth(), sbatch:getTexture():getWidth(), 'check texture change w')
  679. test:assertEquals(texture2:getHeight(), sbatch:getTexture():getHeight(), 'check texture change h')
  680. -- check colors
  681. local r1, g1, b1, a1 = sbatch:getColor()
  682. test:assertEquals(1, r1, 'check initial color r')
  683. test:assertEquals(1, g1, 'check initial color g')
  684. test:assertEquals(1, b1, 'check initial color b')
  685. test:assertEquals(1, a1, 'check initial color a')
  686. sbatch:setColor(1, 0, 0, 1)
  687. local r2, g2, b2, a2 = sbatch:getColor()
  688. test:assertEquals(1, r2, 'check set color r')
  689. test:assertEquals(0, g2, 'check set color g')
  690. test:assertEquals(0, b2, 'check set color b')
  691. test:assertEquals(1, a2, 'check set color a')
  692. -- check adding sprites
  693. local offset_x = 0
  694. local offset_y = 0
  695. local color = 'white'
  696. sbatch:setColor(1, 1, 1, 1)
  697. local sprites = {}
  698. for s=1,4096 do
  699. local spr = sbatch:add(quad1, offset_x, offset_y, 0, 1, 1)
  700. table.insert(sprites, {spr, offset_x, offset_y})
  701. offset_x = offset_x + 1
  702. if s % 64 == 0 then
  703. -- alternate row colors
  704. if color == 'white' then
  705. color = 'red'
  706. sbatch:setColor(1, 0, 0, 1)
  707. else
  708. color = 'white'
  709. sbatch:setColor(1, 1, 1, 1)
  710. end
  711. offset_y = offset_y + 1
  712. offset_x = 0
  713. end
  714. end
  715. test:assertEquals(4096, sbatch:getCount())
  716. -- test drawing and setting
  717. local canvas = love.graphics.newCanvas(64, 64)
  718. love.graphics.setCanvas(canvas)
  719. love.graphics.clear(0, 0, 0, 1)
  720. love.graphics.draw(sbatch, 0, 0)
  721. love.graphics.setCanvas()
  722. local imgdata1 = love.graphics.readbackTexture(canvas, {64, 0, 0, 0, 64, 64})
  723. test:assertPixels(imgdata1, {
  724. lovepink = {{0,0},{63,2},{0,32},{63,32},{63,0},{63,2}}
  725. }, 'sbatch draw normal')
  726. test:compareImg(imgdata1)
  727. -- use set to change some sprites
  728. for s=1,2048 do
  729. sbatch:set(sprites[s][1], quad2, sprites[s][2], sprites[s][3]+1, 0, 1, 1)
  730. end
  731. love.graphics.setCanvas(canvas)
  732. love.graphics.clear(0, 0, 0, 1)
  733. love.graphics.draw(sbatch, 0, 0)
  734. love.graphics.setCanvas()
  735. local imgdata2 = love.graphics.readbackTexture(canvas, {64, 0, 0, 0, 64, 64})
  736. test:assertPixels(imgdata2, {
  737. lovepink = {{0,32},{63,32}},
  738. black = {{0,0},{63,0}},
  739. white = {{0,1},{63,1},{0,31},{63,31}}
  740. }, 'sbatch draw set')
  741. test:compareImg(imgdata2)
  742. -- set drawRange and redraw
  743. sbatch:setDrawRange(1025, 2048)
  744. love.graphics.setCanvas(canvas)
  745. love.graphics.clear(0, 0, 0, 1)
  746. love.graphics.draw(sbatch, 0, 0)
  747. love.graphics.setCanvas()
  748. local imgdata3 = love.graphics.readbackTexture(canvas, {64, 0, 0, 0, 64, 64})
  749. test:assertPixels(imgdata3, {
  750. lovepink = {{0,32},{63,32}},
  751. black = {{0,0},{63,0},{0,48},{63,48}},
  752. white = {{0,17},{63,17},{0,31},{63,31}}
  753. }, 'sbatch draw drawrange')
  754. test:compareImg(imgdata3)
  755. -- clear and redraw
  756. sbatch:clear()
  757. love.graphics.setCanvas(canvas)
  758. love.graphics.clear(0, 0, 0, 1)
  759. love.graphics.draw(sbatch, 0, 0)
  760. love.graphics.setCanvas()
  761. local imgdata4 = love.graphics.readbackTexture(canvas, {64, 0, 0, 0, 64, 64})
  762. test:assertPixels(imgdata4, {
  763. black = {{0,0},{63,0},{0,32},{63,32},{0,63},{63,63}},
  764. }, 'sbatch draw clear')
  765. test:compareImg(imgdata4)
  766. -- array texture sbatch
  767. local texture3 = love.graphics.newArrayImage({
  768. 'resources/love.png',
  769. 'resources/loveinv.png'
  770. })
  771. local asbatch = love.graphics.newSpriteBatch(texture3, 4096)
  772. local quad3 = love.graphics.newQuad(32, 52, 1, 1, texture3) -- loveblue
  773. sprites = {}
  774. for s=1,4096 do
  775. local spr = asbatch:addLayer(1, quad3, 0, s, math.floor(s/64), 1, 1)
  776. table.insert(sprites, {spr, s, math.floor(s/64)})
  777. end
  778. test:assertEquals(4096, asbatch:getCount(), 'check max batch size applies')
  779. for s=1,2048 do
  780. asbatch:setLayer(sprites[s][1], 2, sprites[s][2], sprites[s][3], 0, 1, 1)
  781. end
  782. love.graphics.setCanvas(canvas)
  783. love.graphics.clear(0, 0, 0, 1)
  784. love.graphics.draw(asbatch, 0, 0)
  785. love.graphics.setCanvas()
  786. local imgdata5 = love.graphics.readbackTexture(canvas, {64, 0, 0, 0, 64, 64})
  787. test:assertPixels(imgdata5, {
  788. loveblue = {{31,2},{63,2},{3,30},{3,33},{16,47},{63,47}},
  789. lovepink = {{17,48},{63,48},{31,61},{63,61}},
  790. black = {{0,0},{63,0},{63,63},{63,0},{30,2},{30,61}},
  791. }, 'sbatch draw layers')
  792. test:compareImg(imgdata5)
  793. end
  794. -- Text (love.graphics.newTextBatch)
  795. love.test.graphics.Text = function(test)
  796. -- setup text object
  797. local font = love.graphics.newFont('resources/font.ttf', 8)
  798. local plaintext = love.graphics.newTextBatch(font, 'test')
  799. test:assertObject(plaintext)
  800. -- check height/width/dimensions
  801. test:assertEquals(font:getHeight(), plaintext:getFont():getHeight(), 'check font matches')
  802. local tw, th = plaintext:getDimensions()
  803. test:assertEquals(24, tw, 'check initial dim w')
  804. test:assertEquals(8, th, 'check initial dim h')
  805. test:assertEquals(tw, plaintext:getWidth(), 'check initial dim w')
  806. test:assertEquals(th, plaintext:getHeight(), 'check initial dim h')
  807. -- check changing text effects dimensions
  808. plaintext:add('more text', 100, 0, 0)
  809. test:assertEquals(49, plaintext:getDimensions(), 'check adding text')
  810. plaintext:set('test')
  811. test:assertEquals(24, plaintext:getDimensions(), 'check resetting text')
  812. plaintext:clear()
  813. test:assertEquals(0, plaintext:getDimensions(), 'check clearing text')
  814. -- check drawing + setting more complex text
  815. local colortext = love.graphics.newTextBatch(font, {{1, 0, 0, 1}, 'test'})
  816. test:assertObject(colortext)
  817. colortext:setf('LÖVE is an *awesome* framework you can use to make 2D games in Lua', 60, 'right')
  818. colortext:addf({{1, 1, 0}, 'overlap'}, 1000, 'left')
  819. local font2 = love.graphics.newFont('resources/font.ttf', 8)
  820. colortext:setFont(font2)
  821. local canvas = love.graphics.newCanvas(64, 64)
  822. love.graphics.setCanvas(canvas)
  823. love.graphics.draw(colortext, 0, 10)
  824. love.graphics.setCanvas()
  825. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  826. test:assertPixels(imgdata, {
  827. yellow = {{1,9},{8,13},{16,11},{22,10},{25,7},{29,9},{32,13},{34,15}},
  828. white = {{17,13},{30,12},{38,9},{44,13},{58,13},{8,29},{58,29},{57,37},{5,39},{57,45},{1,55}}
  829. }, 'text draw check')
  830. test:compareImg(imgdata)
  831. end
  832. -- Video (love.graphics.newVideo)
  833. love.test.graphics.Video = function(test)
  834. -- create video obj
  835. local video = love.graphics.newVideo('resources/sample.ogv')
  836. test:assertObject(video)
  837. -- check dimensions
  838. local w, h = video:getDimensions()
  839. test:assertEquals(496, w, 'check vid dim w')
  840. test:assertEquals(502, h, 'check vid dim h')
  841. test:assertEquals(w, video:getWidth(), 'check vid width match')
  842. test:assertEquals(h, video:getHeight(), 'check vid height match')
  843. -- check filters
  844. local min1, mag1, ani1 = video:getFilter()
  845. test:assertEquals('nearest', min1, 'check def filter min')
  846. test:assertEquals('nearest', mag1, 'check def filter mag')
  847. test:assertEquals(1, ani1, 'check def filter ani')
  848. video:setFilter('linear', 'linear', 2)
  849. local min2, mag2, ani2 = video:getFilter()
  850. test:assertEquals('linear', min2, 'check changed filter min')
  851. test:assertEquals('linear', mag2, 'check changed filter mag')
  852. test:assertEquals(2, ani2, 'check changed filter ani')
  853. -- check video playing
  854. test:assertFalse(video:isPlaying(), 'check paused by default')
  855. test:assertEquals(0, video:tell(), 'check 0:00 by default')
  856. -- covered by their own obj tests in video but check returns obj
  857. local source = video:getSource()
  858. test:assertObject(source)
  859. local stream = video:getStream()
  860. test:assertObject(stream)
  861. -- check playing / pausing / seeking states
  862. video:play()
  863. test:waitFrames(30) -- 1.5s ish
  864. video:pause()
  865. test:assertEquals(1, math.ceil(video:tell()), 'check video playing for 1s')
  866. video:seek(0.2)
  867. test:assertEquals(0.2, video:tell(), 'check video seeking')
  868. video:rewind()
  869. test:assertEquals(0, video:tell(), 'check video rewind')
  870. video:setFilter('nearest', 'nearest', 1)
  871. -- check actuall drawing with the vid
  872. local canvas = love.graphics.newCanvas(500, 500)
  873. love.graphics.setCanvas(canvas)
  874. love.graphics.clear(1, 0, 0, 1)
  875. love.graphics.draw(video, 0, 0)
  876. love.graphics.setCanvas()
  877. local imgdata = love.graphics.readbackTexture(canvas, {500, 0, 0, 0, 500, 500})
  878. test:assertPixels(imgdata, {
  879. black = {{0,0},{495,0},{495,499},{0,499}},
  880. red = {{499,0},{499,499}}
  881. }, 'video draw')
  882. test:compareImg(imgdata)
  883. end
  884. --------------------------------------------------------------------------------
  885. --------------------------------------------------------------------------------
  886. ------------------------------------DRAWING-------------------------------------
  887. --------------------------------------------------------------------------------
  888. --------------------------------------------------------------------------------
  889. -- love.graphics.arc
  890. love.test.graphics.arc = function(test)
  891. -- draw some arcs using pi format
  892. local canvas = love.graphics.newCanvas(32, 32)
  893. love.graphics.setCanvas(canvas)
  894. love.graphics.clear(0, 0, 0, 1)
  895. love.graphics.setColor(1, 1, 1, 1)
  896. love.graphics.arc('line', "pie", 16, 16, 16, 0 * (math.pi/180), 360 * (math.pi/180), 10)
  897. love.graphics.arc('fill', "pie", 16, 16, 16, 270 * (math.pi/180), 45 * (math.pi/180), 10)
  898. love.graphics.setColor(1, 0, 0, 1)
  899. love.graphics.arc('line', "pie", 16, 16, 16, 0 * (math.pi/180), 90 * (math.pi/180), 10)
  900. love.graphics.setColor(1, 1, 0, 1)
  901. love.graphics.arc('line', "pie", 16, 16, 16, 180 * (math.pi/180), 135 * (math.pi/180), 10)
  902. love.graphics.setColor(1, 1, 1, 1)
  903. love.graphics.setCanvas()
  904. local imgdata1 = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  905. -- draw some arcs with open format
  906. love.graphics.setCanvas(canvas)
  907. love.graphics.clear(0, 0, 0, 1)
  908. love.graphics.setColor(1, 1, 1, 1)
  909. love.graphics.arc('line', "open", 16, 16, 16, 0 * (math.pi/180), 315 * (math.pi/180), 10)
  910. love.graphics.setColor(1, 0, 0, 1)
  911. love.graphics.arc('fill', "open", 16, 16, 16, 0 * (math.pi/180), 180 * (math.pi/180), 10)
  912. love.graphics.setColor(1, 1, 0, 1)
  913. love.graphics.arc('fill', "open", 16, 16, 16, 180 * (math.pi/180), 270 * (math.pi/180), 10)
  914. love.graphics.setColor(1, 1, 1, 1)
  915. love.graphics.setCanvas()
  916. local imgdata2 = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  917. -- draw some arcs with closed format
  918. love.graphics.setCanvas(canvas)
  919. love.graphics.clear(0, 0, 0, 1)
  920. love.graphics.setColor(1, 1, 1, 1)
  921. love.graphics.arc('line', "closed", 16, 16, 16, 0 * (math.pi/180), 315 * (math.pi/180), 10)
  922. love.graphics.setColor(1, 0, 0, 1)
  923. love.graphics.arc('fill', "closed", 16, 16, 16, 0 * (math.pi/180), 180 * (math.pi/180), 10)
  924. love.graphics.setColor(1, 1, 0, 1)
  925. love.graphics.arc('line', "closed", 16, 16, 16, 180 * (math.pi/180), 90 * (math.pi/180), 10)
  926. love.graphics.setColor(1, 1, 1, 1)
  927. love.graphics.setCanvas()
  928. local imgdata3 = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  929. if GITHUB_RUNNER == true and love.system.getOS() == 'OS X' then
  930. -- on macosx runners, the arcs are not drawn as accurately at low res
  931. -- there's a couple pixels different in the curve of the arc but as we
  932. -- are at such a low resolution I think that can be expected
  933. -- on real hardware the test passes fine though
  934. test:assertTrue(true, 'skip test')
  935. else
  936. test:compareImg(imgdata1)
  937. test:compareImg(imgdata2)
  938. test:compareImg(imgdata3)
  939. end
  940. end
  941. -- love.graphics.circle
  942. love.test.graphics.circle = function(test)
  943. -- draw some circles
  944. local canvas = love.graphics.newCanvas(32, 32)
  945. love.graphics.setCanvas(canvas)
  946. love.graphics.clear(0, 0, 0, 1)
  947. love.graphics.setColor(1, 1, 1, 1)
  948. love.graphics.circle('fill', 16, 16, 16)
  949. love.graphics.setColor(1, 0, 0, 1)
  950. love.graphics.circle('line', 16, 16, 16)
  951. love.graphics.setColor(1, 1, 0, 1)
  952. love.graphics.circle('fill', 16, 16, 8)
  953. love.graphics.setColor(0, 1, 0, 1)
  954. love.graphics.circle('fill', 16, 16, 4)
  955. love.graphics.setColor(1, 1, 1, 1)
  956. love.graphics.setCanvas()
  957. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  958. test:assertPixels(imgdata, {
  959. white = {{13,8},{18,8},{23,13},{23,18}},
  960. green = {
  961. {15,12},{16,12},{13,13},{18,13},{12,15},{12,16},{13,18},{18,18},
  962. {15,19},{16,19},{19,15},{19,16}
  963. },
  964. black = {{10,0},{21,0},{0,10},{0,21},{31,10},{31,21},{10,31},{21,31}},
  965. yellow = {
  966. {11,10},{10,11},{8,14},{8,17},{10,20},{11,21},{14,23},{17,23},{20,21},
  967. {21,20},{23,17},{23,14},{20,10},{21,11},{17,8},{14,8}
  968. },
  969. red = {{11,0},{20,0},{11,31},{20,31},{0,11},{0,20},{31,20},{31,11}}
  970. }, 'circle')
  971. test:compareImg(imgdata)
  972. end
  973. -- love.graphics.clear
  974. love.test.graphics.clear = function(test)
  975. local canvas = love.graphics.newCanvas(16, 16)
  976. love.graphics.setCanvas(canvas)
  977. love.graphics.clear(0, 0, 0, 1)
  978. love.graphics.clear(1, 1, 0, 1)
  979. love.graphics.setCanvas()
  980. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  981. test:assertPixels(imgdata, {
  982. yellow = {{0,0},{15,0},{0,15},{15,15},{8,8}}
  983. }, 'clear')
  984. test:compareImg(imgdata)
  985. end
  986. -- love.graphics.discard
  987. love.test.graphics.discard = function(test)
  988. -- from the docs: "on some desktops this may do nothing"
  989. test:skipTest('cant test this worked')
  990. end
  991. -- love.graphics.draw
  992. love.test.graphics.draw = function(test)
  993. local canvas1 = love.graphics.newCanvas(32, 32)
  994. local canvas2 = love.graphics.newCanvas(32, 32)
  995. local transform = love.math.newTransform( )
  996. transform:translate(16, 0)
  997. transform:scale(0.5, 0.5)
  998. love.graphics.setCanvas(canvas1)
  999. love.graphics.clear(0, 0, 0, 1)
  1000. -- img, offset
  1001. love.graphics.draw(Logo.texture, Logo.img, 0, 0, 0, 1, 1, 16, 16)
  1002. love.graphics.setCanvas()
  1003. love.graphics.setCanvas(canvas2)
  1004. love.graphics.clear(1, 0, 0, 1)
  1005. -- canvas, scale, shear, transform obj
  1006. love.graphics.draw(canvas1, 0, 0, 0, 1, 1, 0, 0, 2, 2)
  1007. love.graphics.draw(canvas1, 0, 16, 0, 0.5, 0.5)
  1008. love.graphics.draw(canvas1, 16, 16, 0, 0.5, 0.5)
  1009. love.graphics.draw(canvas1, transform)
  1010. love.graphics.setCanvas()
  1011. local imgdata = love.graphics.readbackTexture(canvas2, {16, 0, 0, 0, 16, 16})
  1012. test:assertPixels(imgdata, {
  1013. lovepink = {{23,3},{23,19},{7,19},{0,0},{16,0},{0,16},{16,16}},
  1014. loveblue = {{0,31},{15,17},{15,31},{16,31},{31,17},{31,31},{16,15},{31,15}},
  1015. white = {{6,19},{8,19},{22,19},{24,19},{22,3},{24,3}},
  1016. red = {{0,1},{1,0},{15,0},{15,7},{0,15},{7,15}}
  1017. }, 'drawing')
  1018. test:compareImg(imgdata)
  1019. end
  1020. -- love.graphics.drawInstanced
  1021. love.test.graphics.drawInstanced = function(test)
  1022. local image = love.graphics.newImage('resources/love.png')
  1023. local vertices = {
  1024. { 0, 0, 0, 0, 1, 0, 0 },
  1025. { image:getWidth(), 0, 1, 0, 0, 1, 0 },
  1026. { image:getWidth(), image:getHeight(), 1, 1, 0, 0, 1 },
  1027. { 0, image:getHeight(), 0, 1, 1, 1, 0 },
  1028. }
  1029. local mesh = love.graphics.newMesh(vertices, 'fan')
  1030. local canvas = love.graphics.newCanvas(64, 64)
  1031. love.graphics.setCanvas(canvas)
  1032. love.graphics.clear(0, 0, 0, 1)
  1033. love.graphics.drawInstanced(mesh, 1000, 0, 0, 0, 1, 1)
  1034. love.graphics.setCanvas()
  1035. local imgdata = love.graphics.readbackTexture(canvas, {64, 0, 0, 0, 64, 64})
  1036. test:assertPixels(imgdata, {
  1037. red = {{0,0}},
  1038. green = {{63,0}},
  1039. blue = {{63,63}},
  1040. yellow = {{0,63}}
  1041. }, 'draw instances')
  1042. -- need 1 tolerance here just cos of the amount of colors
  1043. test.rgba_tolerance = 1
  1044. test:compareImg(imgdata)
  1045. end
  1046. -- love.graphics.drawLayer
  1047. love.test.graphics.drawLayer = function(test)
  1048. local image = love.graphics.newArrayImage({
  1049. 'resources/love.png', 'resources/loveinv.png',
  1050. 'resources/love.png', 'resources/loveinv.png'
  1051. })
  1052. local canvas = love.graphics.newCanvas(64, 64)
  1053. love.graphics.setCanvas(canvas)
  1054. love.graphics.clear(0, 0, 0, 1)
  1055. love.graphics.drawLayer(image, 1, 0, 0, 0, 1, 1)
  1056. love.graphics.drawLayer(image, 2, 32, 0, 0, 0.5, 0.5)
  1057. love.graphics.drawLayer(image, 4, 0, 32, 0, 0.5, 0.5)
  1058. love.graphics.drawLayer(image, 3, 32, 32, 0, 2, 2, 16, 16)
  1059. love.graphics.setCanvas()
  1060. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1061. test:assertPixels(imgdata, {
  1062. lovepink = {{30,2},{33,2},{2,30},{2,33},{4,60},{4,63},{60,4},{63,4},{31,23},{32,23}},
  1063. loveblue = {{14,33},{17,33},{46,1},{49,1},{1,46},{1,49},{33,14},{33,17}},
  1064. black = {{0,0},{63,0},{0,63},{39,6},{40,6},{6,39},{6,40},{6,55},{55,6}},
  1065. white = {{46,11},{48,11},{14,43},{16,43},{30,23},{33,23},{34,54},{53,40},{63,63}}
  1066. }, 'draw layer')
  1067. test:compareImg(imgdata)
  1068. end
  1069. -- love.graphics.ellipse
  1070. love.test.graphics.ellipse = function(test)
  1071. local canvas = love.graphics.newCanvas(32, 32)
  1072. love.graphics.setCanvas(canvas)
  1073. love.graphics.clear(0, 0, 0, 1)
  1074. love.graphics.setColor(1, 0, 0, 1)
  1075. love.graphics.ellipse('fill', 16, 16, 16, 8)
  1076. love.graphics.setColor(1, 1, 0, 1)
  1077. love.graphics.ellipse('fill', 24, 24, 10, 24)
  1078. love.graphics.setColor(1, 0, 1, 1)
  1079. love.graphics.ellipse('fill', 16, 0, 8, 16)
  1080. love.graphics.setColor(1, 1, 1, 1)
  1081. love.graphics.setCanvas()
  1082. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1083. test:assertPixels(imgdata, {
  1084. red = {{0,14},{0,17},{7,9},{7,22},{14,15},{14,16}},
  1085. pink = {{15,15},{16,15},{8,0},{8,4},{23,0},{23,4},{13,14},{18,14}},
  1086. yellow = {{24,0},{25,0},{14,17},{14,30},{15,31},{31,8}}
  1087. }, 'ellipses')
  1088. test:compareImg(imgdata)
  1089. end
  1090. -- love.graphics.flushBatch
  1091. love.test.graphics.flushBatch = function(test)
  1092. love.graphics.flushBatch()
  1093. local initial = love.graphics.getStats()['drawcalls']
  1094. local canvas = love.graphics.newCanvas(32, 32)
  1095. love.graphics.setCanvas(canvas)
  1096. love.graphics.clear(0, 0, 0, 1)
  1097. love.graphics.rectangle('fill', 0, 0, 32, 32)
  1098. love.graphics.setColor(1, 1, 1, 1)
  1099. love.graphics.setCanvas()
  1100. love.graphics.flushBatch()
  1101. local after = love.graphics.getStats()['drawcalls']
  1102. test:assertEquals(initial+1, after, 'check drawcalls increased')
  1103. end
  1104. -- love.graphics.line
  1105. love.test.graphics.line = function(test)
  1106. local canvas = love.graphics.newCanvas(16, 16)
  1107. love.graphics.setCanvas(canvas)
  1108. love.graphics.clear(0, 0, 0, 1)
  1109. love.graphics.setColor(1, 0, 0, 1)
  1110. love.graphics.line(1,1,16,1,16,16,1,16,1,1)
  1111. love.graphics.setColor(1, 1, 0, 1)
  1112. love.graphics.line({0,0,8,8,16,0,8,8,16,16,8,8,0,16})
  1113. love.graphics.setColor(1, 1, 1, 1)
  1114. love.graphics.setCanvas()
  1115. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1116. test:assertPixels(imgdata, {
  1117. yellow = {{0,0},{15,0},{0,15},{15,15},{7,7},{8,7},{8,7},{8,8}},
  1118. red = {{1,0},{14,0},{0,1},{0,14},{15,1},{15,14},{1,15},{14,15}}
  1119. }, 'lines')
  1120. test:compareImg(imgdata)
  1121. end
  1122. -- love.graphics.points
  1123. love.test.graphics.points = function(test)
  1124. local canvas = love.graphics.newCanvas(16, 16)
  1125. love.graphics.setCanvas(canvas)
  1126. love.graphics.clear(0, 0, 0, 1)
  1127. love.graphics.setColor(1, 0, 0, 1)
  1128. love.graphics.points(1,1,16,1,16,16,1,16,1,1)
  1129. love.graphics.setColor(1, 1, 0, 1)
  1130. love.graphics.points({2,2,8,8,15,2,8,9,15,15,9,9,2,15,9,8})
  1131. love.graphics.setColor(1, 1, 1, 1)
  1132. love.graphics.setCanvas()
  1133. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1134. -- on macOS runners points are drawn 1px off from the target
  1135. if GITHUB_RUNNER == true and love.system.getOS() == 'OS X' then
  1136. test.pixel_tolerance = 1
  1137. end
  1138. test:compareImg(imgdata)
  1139. end
  1140. -- love.graphics.polygon
  1141. love.test.graphics.polygon = function(test)
  1142. local canvas = love.graphics.newCanvas(16, 16)
  1143. love.graphics.setCanvas(canvas)
  1144. love.graphics.clear(0, 0, 0, 1)
  1145. love.graphics.setColor(1, 0, 0, 1)
  1146. love.graphics.polygon("fill", 1, 1, 4, 5, 8, 10, 16, 2, 7, 3, 5, 16, 16, 16, 1, 8)
  1147. love.graphics.setColor(1, 1, 0, 1)
  1148. love.graphics.polygon("line", {2, 2, 4, 5, 3, 7, 8, 15, 12, 4, 5, 10})
  1149. love.graphics.setColor(1, 1, 1, 1)
  1150. love.graphics.setCanvas()
  1151. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1152. test:assertPixels(imgdata, {
  1153. yellow = {{1,0},{1,1},{5,9},{7,14},{8,14},{12,3}},
  1154. red = {{2,1},{1,2},{1,7},{5,15},{14,15},{8,8},{14,2},{7,1}}
  1155. }, 'polygon')
  1156. test:compareImg(imgdata)
  1157. end
  1158. -- love.graphics.print
  1159. love.test.graphics.print = function(test)
  1160. love.graphics.setFont(Font)
  1161. local canvas = love.graphics.newCanvas(16, 16)
  1162. love.graphics.setCanvas(canvas)
  1163. love.graphics.clear(0, 0, 0, 1)
  1164. love.graphics.setColor(1, 0, 0, 1)
  1165. love.graphics.print('love', 0, 3, 0, 1, 1, 0, 0)
  1166. love.graphics.setColor(0, 1, 0, 1)
  1167. love.graphics.print('ooo', 0, 3, 0, 2, 2, 0, 0)
  1168. love.graphics.setColor(0, 0, 1, 1)
  1169. love.graphics.print('hello', 0, 3, 90*(math.pi/180), 1, 1, 0, 8)
  1170. love.graphics.setColor(1, 1, 1, 1)
  1171. love.graphics.setCanvas()
  1172. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1173. test:assertPixels(imgdata, {
  1174. red = {{0,0},{1,0},{1,1},{2,6},{4,4},{7,6},{10,2},{11,5},{14,3},{14,4}},
  1175. green = {
  1176. {2,1},{2,2},{0,3},{1,3},{1,8},{2,9},{7,10},{8,8},{9,4},{13,3},{14,2},
  1177. {13,8},{14,9}
  1178. },
  1179. blue = {
  1180. {4,15},{10,15},{4,12},{6,12},{8,12},{5,9},{7,9},{4,3},{10,3},{8,6},{7,7},
  1181. {4,7},{7,13},{8,12}
  1182. }
  1183. }, 'print')
  1184. test:compareImg(imgdata)
  1185. end
  1186. -- love.graphics.printf
  1187. love.test.graphics.printf = function(test)
  1188. love.graphics.setFont(Font)
  1189. local canvas = love.graphics.newCanvas(32, 32)
  1190. love.graphics.setCanvas(canvas)
  1191. love.graphics.clear(0, 0, 0, 1)
  1192. love.graphics.setColor(1, 0, 0, 1)
  1193. love.graphics.printf('love', 0, 0, 8, "left")
  1194. love.graphics.setColor(0, 1, 0, 1)
  1195. love.graphics.printf('love', 0, 5, 16, "right")
  1196. love.graphics.setColor(0, 0, 1, 1)
  1197. love.graphics.printf('love', 0, 7, 32, "center")
  1198. love.graphics.setColor(1, 1, 1, 1)
  1199. love.graphics.setCanvas()
  1200. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1201. test:assertPixels(imgdata, {
  1202. red = {
  1203. {1,0},{1,1},{0,3},{2,3},{2,7},{0,9},{3,11},{4,10},{0,15},{4,15},{2,19},
  1204. {0,24},{1,23},{3,23},{4,24},{0,26},{1,27},{2,27},{3,27}
  1205. },
  1206. green = {
  1207. {1,2},{0,8},{1,8},{2,8},{4,7},{5,8},{7,8},{8,7},{10,4},{14,4},{11,7},
  1208. {12,8},{10,13},{11,12},{13,12},{14,13},{10,15},{11,16}
  1209. },
  1210. blue = {{6,4},{6,10},{9,7},{10,6},{16,9},{18,9},{21,8},{25,8}}
  1211. }, 'printf')
  1212. test:compareImg(imgdata)
  1213. end
  1214. -- love.graphics.rectangle
  1215. love.test.graphics.rectangle = function(test)
  1216. -- setup, draw a 16x16 red rectangle with a blue central square
  1217. local canvas = love.graphics.newCanvas(16, 16)
  1218. love.graphics.setCanvas(canvas)
  1219. love.graphics.clear(0, 0, 0, 1)
  1220. love.graphics.setColor(1, 0, 0, 1)
  1221. love.graphics.rectangle('fill', 0, 0, 16, 16)
  1222. love.graphics.setColor(0, 0, 1, 1)
  1223. love.graphics.rectangle('fill', 6, 6, 4, 4)
  1224. love.graphics.setColor(1, 1, 1, 1)
  1225. love.graphics.setCanvas()
  1226. local imgdata1 = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1227. -- test, check red bg and blue central square
  1228. test:assertPixels(imgdata1, {
  1229. red = {{0,0},{15,0},{15,15},{0,15}},
  1230. blue = {{6,6},{9,6},{9,9},{6,9}}
  1231. }, 'fill')
  1232. -- clear canvas to do some line testing
  1233. love.graphics.setCanvas(canvas)
  1234. love.graphics.clear(0, 0, 0, 1)
  1235. love.graphics.setColor(1, 0, 0, 1)
  1236. love.graphics.rectangle('line', 1, 1, 15, 15) -- red border
  1237. love.graphics.setColor(0, 0, 1, 1)
  1238. love.graphics.rectangle('line', 1, 1, 2, 15) -- 3x16 left aligned blue outline
  1239. love.graphics.setColor(0, 1, 0, 1)
  1240. love.graphics.rectangle('line', 11, 1, 5, 15) -- 6x16 right aligned green outline
  1241. love.graphics.setColor(1, 1, 1, 1)
  1242. love.graphics.setCanvas()
  1243. local imgdata2 = love.graphics.readbackTexture(canvas, {1, 1, 0, 0, 16, 16})
  1244. -- -- check corners and inner corners
  1245. test:assertPixels(imgdata2, {
  1246. red = {{3,0},{9,0},{3,15,9,15}},
  1247. blue = {{0,0},{2,0},{0,15},{2,15}},
  1248. green = {{10,0},{15,0},{10,15},{15,15}},
  1249. black = {
  1250. {1,1},{1,14},{3,1},{9,1},{3,14},
  1251. {9,14},{11,1},{14,1},{11,14},{14,14}
  1252. }
  1253. }, 'line')
  1254. test:compareImg(imgdata1)
  1255. test:compareImg(imgdata2)
  1256. end
  1257. --------------------------------------------------------------------------------
  1258. --------------------------------------------------------------------------------
  1259. --------------------------------OBJECT CREATION---------------------------------
  1260. --------------------------------------------------------------------------------
  1261. --------------------------------------------------------------------------------
  1262. -- love.graphics.captureScreenshot
  1263. love.test.graphics.captureScreenshot = function(test)
  1264. love.graphics.captureScreenshot('example-screenshot.png')
  1265. test:waitFrames(10)
  1266. -- need to wait until end of the frame for the screenshot
  1267. test:assertNotNil(love.filesystem.openFile('example-screenshot.png', 'r'))
  1268. love.filesystem.remove('example-screenshot.png')
  1269. end
  1270. -- love.graphics.newArrayImage
  1271. -- @NOTE this is just basic nil checking, objs have their own test method
  1272. love.test.graphics.newArrayImage = function(test)
  1273. test:assertObject(love.graphics.newArrayImage({
  1274. 'resources/love.png', 'resources/love2.png', 'resources/love3.png'
  1275. }))
  1276. end
  1277. -- love.graphics.newCanvas
  1278. -- @NOTE this is just basic nil checking, objs have their own test method
  1279. love.test.graphics.newCanvas = function(test)
  1280. test:assertObject(love.graphics.newCanvas(16, 16, {
  1281. type = '2d',
  1282. format = 'normal',
  1283. readable = true,
  1284. msaa = 0,
  1285. dpiscale = 1,
  1286. mipmaps = 'none'
  1287. }))
  1288. test:assertObject(love.graphics.newCanvas(1000, 1000))
  1289. end
  1290. -- love.graphics.newCubeImage
  1291. -- @NOTE this is just basic nil checking, objs have their own test method
  1292. love.test.graphics.newCubeImage = function(test)
  1293. test:assertObject(love.graphics.newCubeImage('resources/cubemap.png', {
  1294. mipmaps = false,
  1295. linear = false
  1296. }))
  1297. end
  1298. -- love.graphics.newFont
  1299. -- @NOTE this is just basic nil checking, objs have their own test method
  1300. love.test.graphics.newFont = function(test)
  1301. test:assertObject(love.graphics.newFont('resources/font.ttf'))
  1302. test:assertObject(love.graphics.newFont('resources/font.ttf', 8, "normal", 1))
  1303. end
  1304. -- love.graphics.newImage
  1305. -- @NOTE this is just basic nil checking, objs have their own test method
  1306. love.test.graphics.newImage = function(test)
  1307. test:assertObject(love.graphics.newImage('resources/love.png', {
  1308. mipmaps = false,
  1309. linear = false,
  1310. dpiscale = 1
  1311. }))
  1312. end
  1313. -- love.graphics.newImageFont
  1314. -- @NOTE this is just basic nil checking, objs have their own test method
  1315. love.test.graphics.newImageFont = function(test)
  1316. test:assertObject(love.graphics.newImageFont('resources/love.png', 'ABCD', 1))
  1317. end
  1318. -- love.graphics.newMesh
  1319. -- @NOTE this is just basic nil checking, objs have their own test method
  1320. love.test.graphics.newMesh = function(test)
  1321. test:assertObject(love.graphics.newMesh({{1, 1, 0, 0, 1, 1, 1, 1}}, 'fan', 'dynamic'))
  1322. end
  1323. -- love.graphics.newParticleSystem
  1324. -- @NOTE this is just basic nil checking, objs have their own test method
  1325. love.test.graphics.newParticleSystem = function(test)
  1326. local imgdata = love.graphics.newImage('resources/love.png')
  1327. test:assertObject(love.graphics.newParticleSystem(imgdata, 1000))
  1328. end
  1329. -- love.graphics.newQuad
  1330. -- @NOTE this is just basic nil checking, objs have their own test method
  1331. love.test.graphics.newQuad = function(test)
  1332. local imgdata = love.graphics.newImage('resources/love.png')
  1333. test:assertObject(love.graphics.newQuad(0, 0, 16, 16, imgdata))
  1334. end
  1335. -- love.graphics.newShader
  1336. -- @NOTE this is just basic nil checking, objs have their own test method
  1337. love.test.graphics.newShader = function(test)
  1338. local pixelcode = [[
  1339. vec4 effect(vec4 color, Image tex, vec2 texture_coords, vec2 screen_coords) {
  1340. vec4 texturecolor = Texel(tex, texture_coords);
  1341. return texturecolor * color;
  1342. }
  1343. ]]
  1344. local vertexcode = [[
  1345. vec4 position(mat4 transform_projection, vec4 vertex_position) {
  1346. return transform_projection * vertex_position;
  1347. }
  1348. ]]
  1349. test:assertObject(love.graphics.newShader(pixelcode, vertexcode))
  1350. end
  1351. -- love.graphics.newSpriteBatch
  1352. -- @NOTE this is just basic nil checking, objs have their own test method
  1353. love.test.graphics.newSpriteBatch = function(test)
  1354. local imgdata = love.graphics.newImage('resources/love.png')
  1355. test:assertObject(love.graphics.newSpriteBatch(imgdata, 1000))
  1356. end
  1357. -- love.graphics.newText
  1358. -- @NOTE this is just basic nil checking, objs have their own test method
  1359. love.test.graphics.newTextBatch = function(test)
  1360. local font = love.graphics.newFont('resources/font.ttf')
  1361. test:assertObject(love.graphics.newTextBatch(font, 'helloworld'))
  1362. end
  1363. -- love.graphics.newVideo
  1364. -- @NOTE this is just basic nil checking, objs have their own test method
  1365. love.test.graphics.newVideo = function(test)
  1366. test:assertObject(love.graphics.newVideo('resources/sample.ogv', {
  1367. audio = false,
  1368. dpiscale = 1
  1369. }))
  1370. end
  1371. -- love.graphics.newVolumeImage
  1372. -- @NOTE this is just basic nil checking, objs have their own test method
  1373. love.test.graphics.newVolumeImage = function(test)
  1374. test:assertObject(love.graphics.newVolumeImage({
  1375. 'resources/love.png', 'resources/love2.png', 'resources/love3.png'
  1376. }, {
  1377. mipmaps = false,
  1378. linear = false
  1379. }))
  1380. end
  1381. -- love.graphics.validateShader
  1382. love.test.graphics.validateShader = function(test)
  1383. local pixelcode = [[
  1384. vec4 effect(vec4 color, Image tex, vec2 texture_coords, vec2 screen_coords) {
  1385. vec4 texturecolor = Texel(tex, texture_coords);
  1386. return texturecolor * color;
  1387. }
  1388. ]]
  1389. local vertexcode = [[
  1390. vec4 position(mat4 transform_projection, vec4 vertex_position) {
  1391. return transform_projection * vertex_position;
  1392. }
  1393. ]]
  1394. -- check made up code first
  1395. local status, _ = love.graphics.validateShader(true, 'nothing here', 'or here')
  1396. test:assertFalse(status, 'check invalid shader code')
  1397. -- check real code
  1398. status, _ = love.graphics.validateShader(true, pixelcode, vertexcode)
  1399. test:assertTrue(status, 'check valid shader code')
  1400. end
  1401. --------------------------------------------------------------------------------
  1402. --------------------------------------------------------------------------------
  1403. ---------------------------------GRAPHICS STATE---------------------------------
  1404. --------------------------------------------------------------------------------
  1405. --------------------------------------------------------------------------------
  1406. -- love.graphics.getBackgroundColor
  1407. love.test.graphics.getBackgroundColor = function(test)
  1408. -- check default bg is black
  1409. local r, g, b, a = love.graphics.getBackgroundColor()
  1410. test:assertEquals(0, r, 'check default background r')
  1411. test:assertEquals(0, g, 'check default background g')
  1412. test:assertEquals(0, b, 'check default background b')
  1413. test:assertEquals(1, a, 'check default background a')
  1414. -- check set value returns correctly
  1415. love.graphics.setBackgroundColor(1, 1, 1, 0)
  1416. r, g, b, a = love.graphics.getBackgroundColor()
  1417. test:assertEquals(1, r, 'check updated background r')
  1418. test:assertEquals(1, g, 'check updated background g')
  1419. test:assertEquals(1, b, 'check updated background b')
  1420. test:assertEquals(0, a, 'check updated background a')
  1421. love.graphics.setBackgroundColor(0, 0, 0, 1) -- reset
  1422. end
  1423. -- love.graphics.getBlendMode
  1424. love.test.graphics.getBlendMode = function(test)
  1425. -- check default blend mode
  1426. local mode, alphamode = love.graphics.getBlendMode()
  1427. test:assertEquals('alpha', mode, 'check default blend mode')
  1428. test:assertEquals('alphamultiply', alphamode, 'check default alpha blend')
  1429. -- check set mode returns correctly
  1430. love.graphics.setBlendMode('add', 'premultiplied')
  1431. mode, alphamode = love.graphics.getBlendMode()
  1432. test:assertEquals('add', mode, 'check changed blend mode')
  1433. test:assertEquals('premultiplied', alphamode, 'check changed alpha blend')
  1434. love.graphics.setBlendMode('alpha', 'alphamultiply') -- reset
  1435. end
  1436. -- love.graphics.getCanvas
  1437. love.test.graphics.getCanvas = function(test)
  1438. -- by default should be nil if drawing to real screen
  1439. test:assertEquals(nil, love.graphics.getCanvas(), 'check no canvas set')
  1440. -- should return not nil when we target a canvas
  1441. local canvas = love.graphics.newCanvas(16, 16)
  1442. love.graphics.setCanvas(canvas)
  1443. test:assertObject(love.graphics.getCanvas())
  1444. love.graphics.setCanvas()
  1445. end
  1446. -- love.graphics.getColor
  1447. love.test.graphics.getColor = function(test)
  1448. -- by default should be white
  1449. local r, g, b, a = love.graphics.getColor()
  1450. test:assertEquals(1, r, 'check default color r')
  1451. test:assertEquals(1, g, 'check default color g')
  1452. test:assertEquals(1, b, 'check default color b')
  1453. test:assertEquals(1, a, 'check default color a')
  1454. -- check set color is returned correctly
  1455. love.graphics.setColor(0, 0, 0, 0)
  1456. r, g, b, a = love.graphics.getColor()
  1457. test:assertEquals(0, r, 'check changed color r')
  1458. test:assertEquals(0, g, 'check changed color g')
  1459. test:assertEquals(0, b, 'check changed color b')
  1460. test:assertEquals(0, a, 'check changed color a')
  1461. love.graphics.setColor(1, 1, 1, 1) -- reset
  1462. end
  1463. -- love.graphics.getColorMask
  1464. love.test.graphics.getColorMask = function(test)
  1465. -- by default should all be active
  1466. local r, g, b, a = love.graphics.getColorMask()
  1467. test:assertTrue(r, 'check default color mask r')
  1468. test:assertTrue(g, 'check default color mask g')
  1469. test:assertTrue(b, 'check default color mask b')
  1470. test:assertTrue(a, 'check default color mask a')
  1471. -- check set color mask is returned correctly
  1472. love.graphics.setColorMask(false, false, true, false)
  1473. r, g, b, a = love.graphics.getColorMask()
  1474. test:assertFalse(r, 'check changed color mask r')
  1475. test:assertFalse(g, 'check changed color mask g')
  1476. test:assertTrue( b, 'check changed color mask b')
  1477. test:assertFalse(a, 'check changed color mask a')
  1478. love.graphics.setColorMask(true, true, true, true) -- reset
  1479. end
  1480. -- love.graphics.getDefaultFilter
  1481. love.test.graphics.getDefaultFilter = function(test)
  1482. -- we set this already for testsuite so we know what it should be
  1483. local min, mag, anisotropy = love.graphics.getDefaultFilter()
  1484. test:assertEquals('nearest', min, 'check default filter min')
  1485. test:assertEquals('nearest', mag, 'check default filter mag')
  1486. test:assertEquals(1, anisotropy, 'check default filter mag')
  1487. end
  1488. -- love.graphics.getDepthMode
  1489. love.test.graphics.getDepthMode = function(test)
  1490. -- by default should be always/write
  1491. local comparemode, write = love.graphics.getDepthMode()
  1492. test:assertEquals('always', comparemode, 'check default compare depth')
  1493. test:assertFalse(write, 'check default depth buffer write')
  1494. end
  1495. -- love.graphics.getFont
  1496. love.test.graphics.getFont = function(test)
  1497. test:assertObject(love.graphics.getFont())
  1498. end
  1499. -- love.graphics.getFrontFaceWinding
  1500. love.test.graphics.getFrontFaceWinding = function(test)
  1501. -- check default winding
  1502. test:assertEquals('ccw', love.graphics.getFrontFaceWinding())
  1503. -- check setting value changes it correctly
  1504. love.graphics.setFrontFaceWinding('cw')
  1505. test:assertEquals('cw', love.graphics.getFrontFaceWinding())
  1506. love.graphics.setFrontFaceWinding('ccw') -- reset
  1507. end
  1508. -- love.graphics.getLineJoin
  1509. love.test.graphics.getLineJoin = function(test)
  1510. -- check default line join
  1511. test:assertEquals('miter', love.graphics.getLineJoin())
  1512. -- check set value returned correctly
  1513. love.graphics.setLineJoin('none')
  1514. test:assertEquals('none', love.graphics.getLineJoin())
  1515. love.graphics.setLineJoin('miter') -- reset
  1516. end
  1517. -- love.graphics.getLineStyle
  1518. love.test.graphics.getLineStyle = function(test)
  1519. -- we know this should be as testsuite sets it!
  1520. test:assertEquals('rough', love.graphics.getLineStyle())
  1521. -- check set value returned correctly
  1522. love.graphics.setLineStyle('smooth')
  1523. test:assertEquals('smooth', love.graphics.getLineStyle())
  1524. love.graphics.setLineStyle('rough') -- reset
  1525. end
  1526. -- love.graphics.getLineWidth
  1527. love.test.graphics.getLineWidth = function(test)
  1528. -- we know this should be as testsuite sets it!
  1529. test:assertEquals(1, love.graphics.getLineWidth())
  1530. -- check set value returned correctly
  1531. love.graphics.setLineWidth(10)
  1532. test:assertEquals(10, love.graphics.getLineWidth())
  1533. love.graphics.setLineWidth(1) -- reset
  1534. end
  1535. -- love.graphics.getMeshCullMode
  1536. love.test.graphics.getMeshCullMode = function(test)
  1537. -- get default mesh culling
  1538. test:assertEquals('none', love.graphics.getMeshCullMode())
  1539. -- check set value returned correctly
  1540. love.graphics.setMeshCullMode('front')
  1541. test:assertEquals('front', love.graphics.getMeshCullMode())
  1542. love.graphics.setMeshCullMode('back') -- reset
  1543. end
  1544. -- love.graphics.getPointSize
  1545. love.test.graphics.getPointSize = function(test)
  1546. -- get default point size
  1547. test:assertEquals(1, love.graphics.getPointSize())
  1548. -- check set value returned correctly
  1549. love.graphics.setPointSize(10)
  1550. test:assertEquals(10, love.graphics.getPointSize())
  1551. love.graphics.setPointSize(1) -- reset
  1552. end
  1553. -- love.graphics.getScissor
  1554. love.test.graphics.getScissor = function(test)
  1555. -- should be no scissor atm
  1556. local x, y, w, h = love.graphics.getScissor()
  1557. test:assertEquals(nil, x, 'check no scissor')
  1558. test:assertEquals(nil, y, 'check no scissor')
  1559. test:assertEquals(nil, w, 'check no scissor')
  1560. test:assertEquals(nil, h, 'check no scissor')
  1561. -- check set value returned correctly
  1562. love.graphics.setScissor(0, 0, 16, 16)
  1563. x, y, w, h = love.graphics.getScissor()
  1564. test:assertEquals(0, x, 'check scissor set')
  1565. test:assertEquals(0, y, 'check scissor set')
  1566. test:assertEquals(16, w, 'check scissor set')
  1567. test:assertEquals(16, h, 'check scissor set')
  1568. love.graphics.setScissor() -- reset
  1569. end
  1570. -- love.graphics.getShader
  1571. love.test.graphics.getShader = function(test)
  1572. -- should be no shader active
  1573. test:assertEquals(nil, love.graphics.getShader(), 'check no active shader')
  1574. end
  1575. -- love.graphics.getStackDepth
  1576. love.test.graphics.getStackDepth = function(test)
  1577. -- by default should be none
  1578. test:assertEquals(0, love.graphics.getStackDepth(), 'check no transforms in stack')
  1579. -- now add 3
  1580. love.graphics.push()
  1581. love.graphics.push()
  1582. love.graphics.push()
  1583. test:assertEquals(3, love.graphics.getStackDepth(), 'check 3 transforms in stack')
  1584. -- now remove 2
  1585. love.graphics.pop()
  1586. love.graphics.pop()
  1587. test:assertEquals(1, love.graphics.getStackDepth(), 'check 1 transforms in stack')
  1588. -- now back to 0
  1589. love.graphics.pop()
  1590. test:assertEquals(0, love.graphics.getStackDepth(), 'check no transforms in stack')
  1591. end
  1592. -- love.graphics.getStencilMode
  1593. love.test.graphics.getStencilMode = function(test)
  1594. -- check default vals
  1595. local action, comparemode, value = love.graphics.getStencilMode( )
  1596. test:assertEquals('keep', action, 'check default stencil action')
  1597. test:assertEquals('always', comparemode, 'check default stencil compare')
  1598. test:assertEquals(0, value, 'check default stencil value')
  1599. -- check set stencil values is returned
  1600. love.graphics.setStencilMode('replace', 'less', 255)
  1601. local action, comparemode, value = love.graphics.getStencilMode()
  1602. test:assertEquals('replace', action, 'check changed stencil action')
  1603. test:assertEquals('less', comparemode, 'check changed stencil compare')
  1604. test:assertEquals(255, value, 'check changed stencil value')
  1605. love.graphics.setStencilMode() -- reset
  1606. end
  1607. -- love.graphics.intersectScissor
  1608. love.test.graphics.intersectScissor = function(test)
  1609. -- make a scissor for the left half, then interset to make the top half
  1610. -- then we should be able to fill the canvas with red and only top 4x4 is filled
  1611. local canvas = love.graphics.newCanvas(16, 16)
  1612. love.graphics.setCanvas(canvas)
  1613. love.graphics.clear(0, 0, 0, 1)
  1614. love.graphics.origin()
  1615. love.graphics.setScissor(0, 0, 8, 16)
  1616. love.graphics.intersectScissor(0, 0, 4, 4)
  1617. love.graphics.setColor(1, 0, 0, 1)
  1618. love.graphics.rectangle('fill', 0, 0, 16, 16)
  1619. love.graphics.setColor(1, 1, 1, 1)
  1620. love.graphics.setScissor()
  1621. love.graphics.setCanvas()
  1622. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1623. test:assertPixels(imgdata, {
  1624. red = {{0,0},{3,3}},
  1625. black ={{4,0},{0,4},{4,4}}
  1626. }, 'intersect scissor')
  1627. test:compareImg(imgdata)
  1628. end
  1629. -- love.graphics.isActive
  1630. love.test.graphics.isActive = function(test)
  1631. local name, version, vendor, device = love.graphics.getRendererInfo()
  1632. if string.find(name, 'Vulkan') ~= nil then
  1633. test:skipTest('love.graphics.isActive() crashes on Vulkan')
  1634. else
  1635. test:assertTrue(love.graphics.isActive(), 'check graphics is active') -- i mean if you got this far
  1636. end
  1637. end
  1638. -- love.graphics.isGammaCorrect
  1639. love.test.graphics.isGammaCorrect = function(test)
  1640. -- we know the config so know this is false
  1641. test:assertNotNil(love.graphics.isGammaCorrect())
  1642. end
  1643. -- love.graphics.isWireframe
  1644. love.test.graphics.isWireframe = function(test)
  1645. local name, version, vendor, device = love.graphics.getRendererInfo()
  1646. if string.match(name, 'OpenGL ES') then
  1647. test:skipTest('Wireframe not supported on OpenGL ES')
  1648. else
  1649. -- check off by default
  1650. test:assertFalse(love.graphics.isWireframe(), 'check no wireframe by default')
  1651. -- check on when enabled
  1652. love.graphics.setWireframe(true)
  1653. test:assertTrue(love.graphics.isWireframe(), 'check wireframe is set')
  1654. love.graphics.setWireframe(false) -- reset
  1655. end
  1656. end
  1657. -- love.graphics.reset
  1658. love.test.graphics.reset = function(test)
  1659. -- reset should reset current canvas and any colors/scissor
  1660. local canvas = love.graphics.newCanvas(16, 16)
  1661. love.graphics.setBackgroundColor(0, 0, 1, 1)
  1662. love.graphics.setColor(0, 1, 0, 1)
  1663. love.graphics.setCanvas(canvas)
  1664. love.graphics.reset()
  1665. local r, g, b, a = love.graphics.getBackgroundColor()
  1666. test:assertEquals(1, r+g+b+a, 'check background reset')
  1667. r, g, b, a = love.graphics.getColor()
  1668. test:assertEquals(4, r+g+b+a, 'check color reset')
  1669. test:assertEquals(nil, love.graphics.getCanvas(), 'check canvas reset')
  1670. love.graphics.setDefaultFilter("nearest", "nearest")
  1671. love.graphics.setLineStyle('rough')
  1672. love.graphics.setPointSize(1)
  1673. love.graphics.setLineWidth(1)
  1674. end
  1675. -- love.graphics.setBackgroundColor
  1676. love.test.graphics.setBackgroundColor = function(test)
  1677. -- check background is set
  1678. love.graphics.setBackgroundColor(1, 0, 0, 1)
  1679. local r, g, b, a = love.graphics.getBackgroundColor()
  1680. test:assertEquals(1, r, 'check set bg r')
  1681. test:assertEquals(0, g, 'check set bg g')
  1682. test:assertEquals(0, b, 'check set bg b')
  1683. test:assertEquals(1, a, 'check set bg a')
  1684. love.graphics.setBackgroundColor(0, 0, 0, 1)
  1685. end
  1686. -- love.graphics.setBlendMode
  1687. love.test.graphics.setBlendMode = function(test)
  1688. -- create fully white canvas, then draw diff. pixels through blendmodes
  1689. local canvas = love.graphics.newCanvas(16, 16)
  1690. love.graphics.setCanvas(canvas)
  1691. love.graphics.clear(0.5, 0.5, 0.5, 1)
  1692. love.graphics.setBlendMode('add', 'alphamultiply')
  1693. love.graphics.setColor(1, 0, 0, 1)
  1694. love.graphics.rectangle('fill', 0, 0, 1, 1)
  1695. love.graphics.setBlendMode('subtract', 'alphamultiply')
  1696. love.graphics.setColor(1, 1, 1, 0.5)
  1697. love.graphics.rectangle('fill', 15, 0, 1, 1)
  1698. love.graphics.setBlendMode('multiply', 'premultiplied')
  1699. love.graphics.setColor(0, 1, 0, 1)
  1700. love.graphics.rectangle('fill', 15, 15, 1, 1)
  1701. love.graphics.setBlendMode('replace', 'premultiplied')
  1702. love.graphics.setColor(0, 0, 1, 0.5)
  1703. love.graphics.rectangle('fill', 0, 15, 1, 1)
  1704. love.graphics.setColor(1, 1, 1, 1)
  1705. love.graphics.setCanvas()
  1706. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1707. -- check the 4 corners
  1708. test:assertPixels(imgdata, {
  1709. redpale = {{0,0}},
  1710. black = {{15,0}},
  1711. greenhalf = {{15,15}},
  1712. bluefade = {{0,15}}
  1713. }, 'blend mode')
  1714. love.graphics.setBlendMode('alpha', 'alphamultiply') -- reset
  1715. -- need 1rgba tolerance here on some machines
  1716. test.rgba_tolerance = 1
  1717. test:compareImg(imgdata)
  1718. end
  1719. -- love.graphics.setCanvas
  1720. love.test.graphics.setCanvas = function(test)
  1721. -- make 2 canvas, set to each, draw one to the other, check output
  1722. local canvas1 = love.graphics.newCanvas(16, 16)
  1723. local canvas2 = love.graphics.newCanvas(16, 16)
  1724. love.graphics.setCanvas(canvas1)
  1725. test:assertEquals(canvas1, love.graphics.getCanvas(), 'check canvas 1 set')
  1726. love.graphics.clear(1, 0, 0, 1)
  1727. love.graphics.setCanvas(canvas2)
  1728. test:assertEquals(canvas2, love.graphics.getCanvas(), 'check canvas 2 set')
  1729. love.graphics.clear(0, 0, 0, 1)
  1730. love.graphics.draw(canvas1, 0, 0)
  1731. love.graphics.setCanvas()
  1732. test:assertEquals(nil, love.graphics.getCanvas(), 'check no canvas set')
  1733. local imgdata = love.graphics.readbackTexture(canvas2, {16, 0, 0, 0, 16, 16})
  1734. -- check 2nd canvas is red
  1735. test:assertPixels(imgdata, {
  1736. red = {{0,0},{15,0},{15,15},{0,15}}
  1737. }, 'set canvas')
  1738. test:compareImg(imgdata)
  1739. end
  1740. -- love.graphics.setColor
  1741. love.test.graphics.setColor = function(test)
  1742. -- set colors, draw rect, check color
  1743. local canvas = love.graphics.newCanvas(16, 16)
  1744. love.graphics.setCanvas(canvas)
  1745. love.graphics.clear(0, 0, 0, 1)
  1746. love.graphics.setColor(1, 0, 0, 1)
  1747. local r, g, b, a = love.graphics.getColor()
  1748. test:assertEquals(1, r, 'check r set')
  1749. test:assertEquals(0, g, 'check g set')
  1750. test:assertEquals(0, b, 'check b set')
  1751. test:assertEquals(1, a, 'check a set')
  1752. love.graphics.rectangle('fill', 0, 0, 16, 1)
  1753. love.graphics.setColor(1, 1, 0, 1)
  1754. love.graphics.rectangle('fill', 0, 1, 16, 1)
  1755. love.graphics.setColor(0, 1, 0, 0.5)
  1756. love.graphics.rectangle('fill', 0, 2, 16, 1)
  1757. love.graphics.setColor(0, 0, 1, 1)
  1758. love.graphics.rectangle('fill', 0, 3, 16, 1)
  1759. love.graphics.setColor(1, 1, 1, 1)
  1760. love.graphics.setCanvas()
  1761. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1762. test:assertPixels(imgdata, {
  1763. red = {{0,0},{5,0},{10,0},{15,0}},
  1764. yellow = {{0,1},{5,1},{10,1},{15,1}},
  1765. greenhalf = {{0,2},{5,2},{10,2},{15,2}},
  1766. blue = {{0,3},{5,3},{10,3},{15,3}}
  1767. }, 'set color')
  1768. test:compareImg(imgdata)
  1769. end
  1770. -- love.graphics.setColorMask
  1771. love.test.graphics.setColorMask = function(test)
  1772. -- set mask, draw stuff, check output pixels
  1773. local canvas = love.graphics.newCanvas(16, 16)
  1774. love.graphics.setCanvas(canvas)
  1775. love.graphics.clear(0, 0, 0, 1)
  1776. -- mask off blue
  1777. love.graphics.setColorMask(true, true, false, true)
  1778. local r, g, b, a = love.graphics.getColorMask()
  1779. test:assertEquals(r, true, 'check r mask')
  1780. test:assertEquals(g, true, 'check g mask')
  1781. test:assertEquals(b, false, 'check b mask')
  1782. test:assertEquals(a, true, 'check a mask')
  1783. -- draw "black" which should then turn to yellow
  1784. love.graphics.setColor(1, 1, 1, 1)
  1785. love.graphics.rectangle('fill', 0, 0, 16, 16)
  1786. love.graphics.setColorMask(true, true, true, true)
  1787. love.graphics.setCanvas()
  1788. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1789. test:assertPixels(imgdata, {
  1790. yellow = {{0,0},{0,15},{15,15},{15,0}}
  1791. }, 'set color mask')
  1792. test:compareImg(imgdata)
  1793. end
  1794. -- love.graphics.setDefaultFilter
  1795. love.test.graphics.setDefaultFilter = function(test)
  1796. -- check setting filter val works
  1797. love.graphics.setDefaultFilter('linear', 'linear', 1)
  1798. local min, mag, anisotropy = love.graphics.getDefaultFilter()
  1799. test:assertEquals('linear', min, 'check default filter min')
  1800. test:assertEquals('linear', mag, 'check default filter mag')
  1801. test:assertEquals(1, anisotropy, 'check default filter mag')
  1802. love.graphics.setDefaultFilter('nearest', 'nearest', 1) -- reset
  1803. end
  1804. -- love.graphics.setDepthMode
  1805. love.test.graphics.setDepthMode = function(test)
  1806. -- check documented modes are valid
  1807. local comparemode, write = love.graphics.getDepthMode()
  1808. local modes = {
  1809. 'equal', 'notequal', 'less', 'lequal', 'gequal',
  1810. 'greater', 'never', 'always'
  1811. }
  1812. for m=1,#modes do
  1813. love.graphics.setDepthMode(modes[m], true)
  1814. test:assertEquals(modes[m], love.graphics.getDepthMode(), 'check depth mode ' .. modes[m] .. ' set')
  1815. end
  1816. love.graphics.setDepthMode(comparemode, write)
  1817. -- @TODO better graphics drawing specific test
  1818. end
  1819. -- love.graphics.setFont
  1820. love.test.graphics.setFont = function(test)
  1821. -- set font doesnt return anything so draw with the test font
  1822. local canvas = love.graphics.newCanvas(16, 16)
  1823. love.graphics.setFont(Font)
  1824. love.graphics.setCanvas(canvas)
  1825. love.graphics.clear(0, 0, 0, 1)
  1826. love.graphics.setColor(1, 0, 0, 1)
  1827. love.graphics.print('love', 0, 3)
  1828. love.graphics.setColor(1, 1, 1, 1)
  1829. love.graphics.setCanvas()
  1830. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1831. test:assertPixels(imgdata, {
  1832. red = {
  1833. {0,0},{0,6},{2,6},{6,2},
  1834. {4,4},{8,4},{6,6},{10,2},
  1835. {14,2},{12,6}
  1836. }
  1837. }, 'set font for print')
  1838. test:compareImg(imgdata)
  1839. end
  1840. -- love.graphics.setFrontFaceWinding
  1841. love.test.graphics.setFrontFaceWinding = function(test)
  1842. -- check documented modes are valid
  1843. local original = love.graphics.getFrontFaceWinding()
  1844. love.graphics.setFrontFaceWinding('cw')
  1845. test:assertEquals('cw', love.graphics.getFrontFaceWinding(), 'check ffw cw set')
  1846. love.graphics.setFrontFaceWinding('ccw')
  1847. test:assertEquals('ccw', love.graphics.getFrontFaceWinding(), 'check ffw ccw set')
  1848. love.graphics.setFrontFaceWinding(original)
  1849. -- @TODO better graphics drawing specific test
  1850. end
  1851. -- love.graphics.setLineJoin
  1852. love.test.graphics.setLineJoin = function(test)
  1853. local canvas = love.graphics.newCanvas(16, 16)
  1854. love.graphics.setFont(Font)
  1855. love.graphics.setCanvas(canvas)
  1856. love.graphics.clear(0, 0, 0, 1)
  1857. local line = {0,1,8,1,8,8}
  1858. love.graphics.setLineStyle('rough')
  1859. love.graphics.setLineWidth(2)
  1860. love.graphics.setColor(1, 0, 0)
  1861. love.graphics.setLineJoin('bevel')
  1862. love.graphics.line(line)
  1863. love.graphics.translate(0, 4)
  1864. love.graphics.setColor(1, 1, 0)
  1865. love.graphics.setLineJoin('none')
  1866. love.graphics.line(line)
  1867. love.graphics.translate(0, 4)
  1868. love.graphics.setColor(0, 0, 1)
  1869. love.graphics.setLineJoin('miter')
  1870. love.graphics.line(line)
  1871. love.graphics.setColor(1, 1, 1)
  1872. love.graphics.setLineWidth(1)
  1873. love.graphics.origin()
  1874. love.graphics.setCanvas()
  1875. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1876. test:assertPixels(imgdata, {
  1877. black = {{8,0}},
  1878. red = {{8,4}},
  1879. yellow = {{8,7}},
  1880. blue = {{8,8}}
  1881. }, 'set line join')
  1882. test:compareImg(imgdata)
  1883. end
  1884. -- love.graphics.setLineStyle
  1885. love.test.graphics.setLineStyle = function(test)
  1886. local canvas = love.graphics.newCanvas(16, 16)
  1887. love.graphics.setFont(Font)
  1888. love.graphics.setCanvas(canvas)
  1889. love.graphics.clear(0, 0, 0, 1)
  1890. love.graphics.setColor(1, 0, 0)
  1891. local line = {0,1,16,1}
  1892. love.graphics.setLineStyle('rough')
  1893. love.graphics.line(line)
  1894. love.graphics.translate(0, 4)
  1895. love.graphics.setLineStyle('smooth')
  1896. love.graphics.line(line)
  1897. love.graphics.setLineStyle('rough')
  1898. love.graphics.setColor(1, 1, 1)
  1899. love.graphics.origin()
  1900. love.graphics.setCanvas()
  1901. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1902. test:assertPixels(imgdata, {
  1903. red = {{0,0},{7,0},{15,0}},
  1904. red07 = {{0,4},{7,4},{15,4}}
  1905. }, 'set line style')
  1906. -- linux runner needs a 1/255 tolerance for the blend between a rough line + bg
  1907. if GITHUB_RUNNER == true and love.system.getOS() == 'Linux' then
  1908. test.rgba_tolerance = 1
  1909. end
  1910. test:compareImg(imgdata)
  1911. end
  1912. -- love.graphics.setLineWidth
  1913. love.test.graphics.setLineWidth = function(test)
  1914. local canvas = love.graphics.newCanvas(16, 16)
  1915. love.graphics.setFont(Font)
  1916. love.graphics.setCanvas(canvas)
  1917. love.graphics.clear(0, 0, 0, 1)
  1918. local line = {0,1,8,1,8,8}
  1919. love.graphics.setColor(1, 0, 0)
  1920. love.graphics.setLineWidth(2)
  1921. love.graphics.line(line)
  1922. love.graphics.translate(0, 4)
  1923. love.graphics.setColor(1, 1, 0)
  1924. love.graphics.setLineWidth(3)
  1925. love.graphics.line(line)
  1926. love.graphics.translate(0, 4)
  1927. love.graphics.setColor(0, 0, 1)
  1928. love.graphics.setLineWidth(4)
  1929. love.graphics.line(line)
  1930. love.graphics.setColor(1, 1, 1)
  1931. love.graphics.setLineWidth(1)
  1932. love.graphics.origin()
  1933. love.graphics.setCanvas()
  1934. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1935. test:assertPixels(imgdata, {
  1936. black = {{0,2},{6,2},{0,6},{5,6},{0,11},{5,11}},
  1937. red = {{0,0},{0,1},{7,2},{8,2}},
  1938. yellow = {{0,3},{0,5},{6,6},{8,6}},
  1939. blue = {{0,7},{0,10},{6,15},{9,15}}
  1940. }, 'set line width')
  1941. test:compareImg(imgdata)
  1942. end
  1943. -- love.graphics.setMeshCullMode
  1944. love.test.graphics.setMeshCullMode = function(test)
  1945. -- check documented modes are valid
  1946. local original = love.graphics.getMeshCullMode()
  1947. local modes = {'back', 'front', 'none'}
  1948. for m=1,#modes do
  1949. love.graphics.setMeshCullMode(modes[m])
  1950. test:assertEquals(modes[m], love.graphics.getMeshCullMode(), 'check mesh cull mode ' .. modes[m] .. ' was set')
  1951. end
  1952. love.graphics.setMeshCullMode(original)
  1953. -- @TODO better graphics drawing specific test
  1954. end
  1955. -- love.graphics.setScissor
  1956. love.test.graphics.setScissor = function(test)
  1957. -- make a scissor for the left half
  1958. -- then we should be able to fill the canvas with red and only left is filled
  1959. local canvas = love.graphics.newCanvas(16, 16)
  1960. love.graphics.setCanvas(canvas)
  1961. love.graphics.clear(0, 0, 0, 1)
  1962. love.graphics.origin()
  1963. love.graphics.setScissor(0, 0, 8, 16)
  1964. love.graphics.setColor(1, 0, 0, 1)
  1965. love.graphics.rectangle('fill', 0, 0, 16, 16)
  1966. love.graphics.setColor(1, 1, 1, 1)
  1967. love.graphics.setScissor()
  1968. love.graphics.setCanvas()
  1969. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1970. test:assertPixels(imgdata, {
  1971. red = {{0,0},{7,0},{0,15},{7,15}},
  1972. black ={{8,0},{8,15},{15,0},{15,15}}
  1973. }, 'set scissor')
  1974. test:compareImg(imgdata)
  1975. end
  1976. -- love.graphics.setShader
  1977. love.test.graphics.setShader = function(test)
  1978. -- make a shader that will only ever draw yellow
  1979. local pixelcode = [[
  1980. vec4 effect(vec4 color, Image tex, vec2 texture_coords, vec2 screen_coords) {
  1981. vec4 texturecolor = Texel(tex, texture_coords);
  1982. return vec4(1.0,1.0,0.0,1.0);
  1983. }
  1984. ]]
  1985. local vertexcode = [[
  1986. vec4 position(mat4 transform_projection, vec4 vertex_position) {
  1987. return transform_projection * vertex_position;
  1988. }
  1989. ]]
  1990. local shader = love.graphics.newShader(pixelcode, vertexcode)
  1991. local canvas = love.graphics.newCanvas(16, 16)
  1992. love.graphics.setCanvas(canvas)
  1993. love.graphics.clear(0, 0, 0, 1)
  1994. love.graphics.setShader(shader)
  1995. -- draw red rectangle
  1996. love.graphics.setColor(1, 0, 0, 1)
  1997. love.graphics.rectangle('fill', 0, 0, 16, 16)
  1998. love.graphics.setShader()
  1999. love.graphics.setColor(1, 1, 1, 1)
  2000. love.graphics.setCanvas()
  2001. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  2002. test:assertPixels(imgdata, {
  2003. yellow = {{0,0},{15,0},{0,15},{15,15}},
  2004. }, 'check shader set to yellow')
  2005. test:compareImg(imgdata)
  2006. end
  2007. -- love.graphics.setStencilMode
  2008. love.test.graphics.setStencilMode = function(test)
  2009. local canvas = love.graphics.newCanvas(16, 16)
  2010. love.graphics.setCanvas({canvas, stencil=true})
  2011. love.graphics.clear(0, 0, 0, 1)
  2012. love.graphics.setStencilMode('replace', 'always', 1)
  2013. love.graphics.circle('fill', 8, 8, 6)
  2014. love.graphics.setStencilMode('keep', 'greater', 0)
  2015. love.graphics.setColor(1, 0, 0, 1)
  2016. love.graphics.rectangle('fill', 0, 0, 16, 16)
  2017. love.graphics.setColor(1, 1, 1, 1)
  2018. love.graphics.setStencilMode()
  2019. love.graphics.setCanvas()
  2020. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  2021. test:assertPixels(imgdata, {
  2022. red = {{6,2},{9,2},{2,6},{2,9},{13,6},{9,6},{6,13},{9,13}}
  2023. }, 'check stencil test')
  2024. test:compareImg(imgdata)
  2025. end
  2026. -- love.graphics.setWireframe
  2027. love.test.graphics.setWireframe = function(test)
  2028. local name, version, vendor, device = love.graphics.getRendererInfo()
  2029. if string.match(name, 'OpenGL ES') then
  2030. test:skipTest('Wireframe not supported on OpenGL ES')
  2031. else
  2032. -- check wireframe outlines
  2033. love.graphics.setWireframe(true)
  2034. local canvas = love.graphics.newCanvas(16, 16)
  2035. love.graphics.setCanvas(canvas)
  2036. love.graphics.clear(0, 0, 0, 1)
  2037. love.graphics.setColor(1, 1, 0, 1)
  2038. love.graphics.rectangle('fill', 2, 2, 13, 13)
  2039. love.graphics.setColor(1, 1, 1, 1)
  2040. love.graphics.setCanvas()
  2041. love.graphics.setWireframe(false)
  2042. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  2043. -- on macOS runners wireframes are drawn 1px off from the target
  2044. if GITHUB_RUNNER == true and love.system.getOS() == 'OS X' then
  2045. test.pixel_tolerance = 1
  2046. end
  2047. test:compareImg(imgdata)
  2048. end
  2049. end
  2050. --------------------------------------------------------------------------------
  2051. --------------------------------------------------------------------------------
  2052. -------------------------------COORDINATE SYSTEM--------------------------------
  2053. --------------------------------------------------------------------------------
  2054. --------------------------------------------------------------------------------
  2055. -- love.graphics.applyTransform
  2056. love.test.graphics.applyTransform = function(test)
  2057. -- use transform object to translate the drawn rectangle
  2058. local transform = love.math.newTransform()
  2059. transform:translate(10, 0)
  2060. local canvas = love.graphics.newCanvas(16, 16)
  2061. love.graphics.setCanvas(canvas)
  2062. love.graphics.clear(0, 0, 0, 1)
  2063. love.graphics.setColor(1, 0, 0, 1)
  2064. love.graphics.applyTransform(transform)
  2065. love.graphics.rectangle('fill', 0, 0, 1, 1)
  2066. love.graphics.setColor(1, 1, 1, 1)
  2067. love.graphics.setCanvas()
  2068. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  2069. test:assertPixels(imgdata, { red = {{10, 0}} }, 'apply transform 10')
  2070. test:compareImg(imgdata)
  2071. end
  2072. -- love.graphics.inverseTransformPoint
  2073. love.test.graphics.inverseTransformPoint = function(test)
  2074. -- start with 0, 0
  2075. local sx, sy = love.graphics.inverseTransformPoint(0, 0)
  2076. test:assertEquals(0, sx, 'check starting x is 0')
  2077. test:assertEquals(0, sy, 'check starting y is 0')
  2078. -- check translation effects the point
  2079. love.graphics.translate(1, 5)
  2080. sx, sy = love.graphics.inverseTransformPoint(1, 5)
  2081. test:assertEquals(0, sx, 'check transformed x is 0')
  2082. test:assertEquals(0, sy, 'check transformed y is 0')
  2083. love.graphics.origin()
  2084. end
  2085. -- love.graphics.origin
  2086. love.test.graphics.origin = function(test)
  2087. -- if we do some translations and scaling
  2088. -- using .origin() should reset it all and draw the pixel at 0,0
  2089. local canvas = love.graphics.newCanvas(16, 16)
  2090. love.graphics.setCanvas(canvas)
  2091. love.graphics.clear(0, 0, 0, 1)
  2092. love.graphics.origin()
  2093. love.graphics.translate(10, 10)
  2094. love.graphics.scale(1, 1)
  2095. love.graphics.shear(20, 20)
  2096. love.graphics.origin()
  2097. love.graphics.setColor(1, 0, 0, 1)
  2098. love.graphics.rectangle('fill', 0, 0, 1, 1)
  2099. love.graphics.setColor(1, 1, 1, 1)
  2100. love.graphics.setCanvas()
  2101. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  2102. test:assertPixels(imgdata, { red = {{0, 0}} }, 'origin check')
  2103. test:compareImg(imgdata)
  2104. end
  2105. -- love.graphics.pop
  2106. love.test.graphics.pop = function(test)
  2107. -- if we push at the start, and then run a pop
  2108. -- it should reset it all and draw the pixel at 0,0
  2109. local canvas = love.graphics.newCanvas(16, 16)
  2110. love.graphics.setCanvas(canvas)
  2111. love.graphics.clear(0, 0, 0, 1)
  2112. love.graphics.push()
  2113. love.graphics.translate(10, 10)
  2114. love.graphics.scale(1, 1)
  2115. love.graphics.shear(20, 20)
  2116. love.graphics.pop()
  2117. love.graphics.setColor(1, 0, 0, 1)
  2118. love.graphics.rectangle('fill', 0, 0, 1, 1)
  2119. love.graphics.setColor(1, 1, 1, 1)
  2120. love.graphics.setCanvas()
  2121. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  2122. test:assertPixels(imgdata, { red = {{0, 0}} }, 'pop 1')
  2123. test:compareImg(imgdata)
  2124. end
  2125. -- love.graphics.push
  2126. love.test.graphics.push = function(test)
  2127. -- if we push at the start, do some stuff, then another push
  2128. -- 1 pop should only go back 1 push and draw the pixel at 1, 1
  2129. local canvas = love.graphics.newCanvas(16, 16)
  2130. love.graphics.setCanvas(canvas)
  2131. love.graphics.clear(0, 0, 0, 1)
  2132. love.graphics.push()
  2133. love.graphics.scale(1, 1)
  2134. love.graphics.shear(20, 20)
  2135. love.graphics.push()
  2136. love.graphics.translate(1, 1)
  2137. love.graphics.pop()
  2138. love.graphics.setColor(1, 0, 0, 1)
  2139. love.graphics.rectangle('fill', 0, 0, 1, 1)
  2140. love.graphics.setColor(1, 1, 1, 1)
  2141. love.graphics.setCanvas()
  2142. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  2143. test:assertPixels(imgdata, { red = {{1, 1}} }, 'push 1')
  2144. test:compareImg(imgdata)
  2145. end
  2146. -- love.graphics.replaceTransform
  2147. love.test.graphics.replaceTransform = function(test)
  2148. -- if use transform object to translate
  2149. -- set some normal transforms first which should get overwritten
  2150. local transform = love.math.newTransform()
  2151. transform:translate(10, 0)
  2152. local canvas = love.graphics.newCanvas(16, 16)
  2153. love.graphics.setCanvas(canvas)
  2154. love.graphics.clear(0, 0, 0, 1)
  2155. love.graphics.setColor(1, 0, 0, 1)
  2156. love.graphics.scale(2, 2)
  2157. love.graphics.translate(10, 10)
  2158. love.graphics.replaceTransform(transform)
  2159. love.graphics.rectangle('fill', 0, 0, 1, 1)
  2160. love.graphics.setColor(1, 1, 1, 1)
  2161. love.graphics.setCanvas()
  2162. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  2163. test:assertPixels(imgdata, { red = {{10, 0}} }, 'replace transform 10')
  2164. test:compareImg(imgdata)
  2165. end
  2166. -- love.graphics.rotate
  2167. love.test.graphics.rotate = function(test)
  2168. -- starting at 0,0, we rotate by 90deg and then draw
  2169. -- we can then check the drawn rectangle is rotated
  2170. local canvas = love.graphics.newCanvas(16, 16)
  2171. love.graphics.setCanvas(canvas)
  2172. love.graphics.clear(0, 0, 0, 1)
  2173. love.graphics.setColor(1, 0, 0, 1)
  2174. love.graphics.translate(4, 0)
  2175. love.graphics.rotate(90 * (math.pi/180))
  2176. love.graphics.rectangle('fill', 0, 0, 4, 4)
  2177. love.graphics.setColor(1, 1, 1, 1)
  2178. love.graphics.setCanvas()
  2179. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  2180. test:assertPixels(imgdata, { red = {{0,0},{3,0},{3,3},{0,3}} }, 'rotate 90')
  2181. test:compareImg(imgdata)
  2182. end
  2183. -- love.graphics.scale
  2184. love.test.graphics.scale = function(test)
  2185. -- starting at 0,0, we scale by 4x and then draw
  2186. -- we can then check the drawn rectangle covers the whole canvas
  2187. local canvas = love.graphics.newCanvas(16, 16)
  2188. love.graphics.setCanvas(canvas)
  2189. love.graphics.clear(0, 0, 0, 1)
  2190. love.graphics.setColor(1, 0, 0, 1)
  2191. love.graphics.scale(4, 4)
  2192. love.graphics.rectangle('fill', 0, 0, 4, 4)
  2193. love.graphics.setColor(1, 1, 1, 1)
  2194. love.graphics.setCanvas()
  2195. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  2196. test:assertPixels(imgdata, { red = {{1,1},{1,15},{15,1},{15,15}} }, 'scale 4x')
  2197. end
  2198. -- love.graphics.shear
  2199. love.test.graphics.shear = function(test)
  2200. -- starting at 0,0, we shear by 2x and then draw
  2201. -- we can then check the drawn rectangle has moved over
  2202. local canvas = love.graphics.newCanvas(16, 16)
  2203. love.graphics.setCanvas(canvas)
  2204. love.graphics.clear(0, 0, 0, 1)
  2205. love.graphics.origin()
  2206. love.graphics.setColor(1, 0, 0, 1)
  2207. love.graphics.shear(2, 0)
  2208. love.graphics.rectangle('fill', 0, 0, 4, 4)
  2209. love.graphics.setColor(1, 1, 1, 1)
  2210. love.graphics.setCanvas()
  2211. local imgdata1 = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  2212. test:assertPixels(imgdata1, { red = {{1,0},{4,0},{7,3},{10,3}} }, 'shear x')
  2213. -- same again at 0,0, we shear by 2y and then draw
  2214. -- we can then check the drawn rectangle has moved down
  2215. love.graphics.setCanvas(canvas)
  2216. love.graphics.clear(0, 0, 0, 1)
  2217. love.graphics.origin()
  2218. love.graphics.setColor(1, 0, 0, 1)
  2219. love.graphics.shear(0, 2)
  2220. love.graphics.rectangle('fill', 0, 0, 4, 4)
  2221. love.graphics.setColor(1, 1, 1, 1)
  2222. love.graphics.setCanvas()
  2223. local imgdata2 = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  2224. test:assertPixels(imgdata2, { red = { {0,1},{0,4},{3,7},{3,10}} }, 'shear y')
  2225. test:compareImg(imgdata1)
  2226. test:compareImg(imgdata2)
  2227. end
  2228. -- love.graphics.transformPoint
  2229. love.test.graphics.transformPoint = function(test)
  2230. -- start with 0, 0
  2231. local sx, sy = love.graphics.transformPoint(0, 0)
  2232. test:assertEquals(0, sx, 'check starting x is 0')
  2233. test:assertEquals(0, sy, 'check starting y is 0')
  2234. -- check translation effects the point
  2235. love.graphics.translate(1, 5)
  2236. sx, sy = love.graphics.transformPoint(0, 0)
  2237. test:assertEquals(1, sx, 'check transformed x is 0')
  2238. test:assertEquals(5, sy, 'check transformed y is 10')
  2239. end
  2240. -- love.graphics.translate
  2241. love.test.graphics.translate = function(test)
  2242. -- starting at 0,0, we translate 4 times and draw a pixel at each point
  2243. -- we can then check the 4 points are now red
  2244. local canvas = love.graphics.newCanvas(16, 16)
  2245. love.graphics.setCanvas(canvas)
  2246. love.graphics.clear(0, 0, 0, 1)
  2247. love.graphics.setColor(1, 0, 0, 1)
  2248. love.graphics.translate(5, 0)
  2249. love.graphics.rectangle('fill', 0, 0, 1, 1)
  2250. love.graphics.translate(0, 5)
  2251. love.graphics.rectangle('fill', 0, 0, 1, 1)
  2252. love.graphics.translate(-5, 0)
  2253. love.graphics.rectangle('fill', 0, 0, 1, 1)
  2254. love.graphics.translate(0, -5)
  2255. love.graphics.rectangle('fill', 0, 0, 1, 1)
  2256. love.graphics.setColor(1, 1, 1, 1)
  2257. love.graphics.setCanvas()
  2258. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  2259. test:assertPixels(imgdata, { red = {{5,0},{0,5},{5,5},{0,0}} }, 'translate 4x')
  2260. test:compareImg(imgdata)
  2261. end
  2262. --------------------------------------------------------------------------------
  2263. --------------------------------------------------------------------------------
  2264. -------------------------------------WINDOW-------------------------------------
  2265. --------------------------------------------------------------------------------
  2266. --------------------------------------------------------------------------------
  2267. -- love.graphics.getDPIScale
  2268. -- @NOTE hardware dependent so can't check result
  2269. love.test.graphics.getDPIScale = function(test)
  2270. test:assertNotNil(love.graphics.getDPIScale())
  2271. end
  2272. -- love.graphics.getDimensions
  2273. love.test.graphics.getDimensions = function(test)
  2274. -- check graphics dimensions match window dimensions
  2275. local gwidth, gheight = love.graphics.getDimensions()
  2276. local wwidth, wheight, _ = love.window.getMode()
  2277. test:assertEquals(wwidth, gwidth, 'check graphics dimension w matches window w')
  2278. test:assertEquals(wheight, gheight, 'check graphics dimension h matches window h')
  2279. end
  2280. -- love.graphics.getHeight
  2281. love.test.graphics.getHeight = function(test)
  2282. -- check graphics height match window height
  2283. local wwidth, wheight, _ = love.window.getMode()
  2284. test:assertEquals(wheight, love.graphics.getHeight(), 'check graphics h matches window h')
  2285. end
  2286. -- love.graphics.getPixelDimensions
  2287. love.test.graphics.getPixelDimensions = function(test)
  2288. -- check graphics dimensions match window dimensions relative to dpi
  2289. local dpi = love.graphics.getDPIScale()
  2290. local gwidth, gheight = love.graphics.getPixelDimensions()
  2291. local wwidth, wheight, _ = love.window.getMode()
  2292. test:assertEquals(wwidth, gwidth/dpi, 'check graphics pixel dpi w matches window w')
  2293. test:assertEquals(wheight, gheight/dpi, 'check graphics pixel dpi h matches window h')
  2294. end
  2295. -- love.graphics.getPixelHeight
  2296. love.test.graphics.getPixelHeight = function(test)
  2297. -- check graphics height match window height relative to dpi
  2298. local dpi = love.graphics.getDPIScale()
  2299. local wwidth, wheight, _ = love.window.getMode()
  2300. test:assertEquals(wheight,love.graphics.getPixelHeight()/dpi, 'check graphics pixel dpi h matches window h')
  2301. end
  2302. -- love.graphics.getPixelWidth
  2303. love.test.graphics.getPixelWidth = function(test)
  2304. -- check graphics width match window width relative to dpi
  2305. local dpi = love.graphics.getDPIScale()
  2306. local wwidth, wheight, _ = love.window.getMode()
  2307. test:assertEquals(wwidth, love.graphics.getWidth()/dpi, 'check graphics pixel dpi w matches window w')
  2308. end
  2309. -- love.graphics.getWidth
  2310. love.test.graphics.getWidth = function(test)
  2311. -- check graphics width match window width
  2312. local wwidth, wheight, _ = love.window.getMode()
  2313. test:assertEquals(wwidth, love.graphics.getWidth(), 'check graphics w matches window w')
  2314. end
  2315. --------------------------------------------------------------------------------
  2316. --------------------------------------------------------------------------------
  2317. -------------------------------SYSTEM INFORMATION-------------------------------
  2318. --------------------------------------------------------------------------------
  2319. --------------------------------------------------------------------------------
  2320. -- love.graphics.getTextureFormats
  2321. love.test.graphics.getTextureFormats = function(test)
  2322. local formats = {
  2323. 'hdr', 'r8i', 'r8ui', 'r16i', 'r16ui', 'r32i', 'r32ui', 'rg8i', 'rg8ui',
  2324. 'rg16i', 'rg16ui', 'rg32i', 'rg32ui', 'bgra8', 'r8', 'rgba8i', 'rgba8ui',
  2325. 'rgba16i', 'rg8', 'rgba32i', 'rgba32ui', 'rgba8', 'DXT1', 'r16', 'DXT5',
  2326. 'rg16', 'BC4s', 'rgba16', 'BC5s', 'r16f', 'BC6hs', 'BC7', 'PVR1rgb2',
  2327. 'rg16f', 'PVR1rgba2', 'rgba16f', 'ETC1', 'r32f', 'ETC2rgba', 'rg32f',
  2328. 'EACr', 'rgba32f', 'EACrg', 'rgba4', 'ASTC4x4', 'ASTC5x4', 'rgb5a1',
  2329. 'ASTC6x5', 'rgb565', 'ASTC8x5', 'ASTC8x6', 'rgb10a2', 'ASTC10x5',
  2330. 'rg11b10f', 'ASTC10x8', 'ASTC10x10', 'ASTC12x10', 'ASTC12x12', 'normal',
  2331. 'srgba8', 'la8', 'ASTC10x6', 'ASTC8x8', 'ASTC6x6', 'ASTC5x5', 'EACrgs',
  2332. 'EACrs', 'ETC2rgba1', 'ETC2rgb', 'PVR1rgba4', 'PVR1rgb4', 'BC6h',
  2333. 'BC5', 'BC4', 'DXT3', 'stencil8', 'rgba16ui', 'bgra8srgb'
  2334. }
  2335. local supported = love.graphics.getTextureFormats({ canvas = true })
  2336. test:assertNotNil(supported)
  2337. for f=1,#formats do
  2338. test:assertNotEquals(nil, supported[formats[f] ], 'expected a key for format: ' .. formats[f])
  2339. end
  2340. end
  2341. -- love.graphics.getRendererInfo
  2342. -- @NOTE hardware dependent so best can do is nil checking
  2343. love.test.graphics.getRendererInfo = function(test)
  2344. local name, version, vendor, device = love.graphics.getRendererInfo()
  2345. test:assertNotNil(name)
  2346. test:assertNotNil(version)
  2347. test:assertNotNil(vendor)
  2348. test:assertNotNil(device)
  2349. end
  2350. -- love.graphics.getStats
  2351. -- @NOTE cant really predict some of these so just nil check for most
  2352. love.test.graphics.getStats = function(test)
  2353. local stattypes = {
  2354. 'drawcalls', 'canvasswitches', 'texturememory', 'shaderswitches',
  2355. 'drawcallsbatched', 'textures', 'fonts'
  2356. }
  2357. local stats = love.graphics.getStats()
  2358. for s=1,#stattypes do
  2359. test:assertNotEquals(nil, stats[stattypes[s] ], 'expected a key for stat: ' .. stattypes[s])
  2360. end
  2361. end
  2362. -- love.graphics.getSupported
  2363. love.test.graphics.getSupported = function(test)
  2364. -- cant check values as hardware dependent but we can check the keys in the
  2365. -- table match what the documentation lists
  2366. local gfs = {
  2367. 'clampzero', 'lighten', 'glsl3', 'instancing', 'fullnpot',
  2368. 'pixelshaderhighp', 'shaderderivatives', 'indirectdraw', 'mipmaprange',
  2369. 'copyrendertargettobuffer', 'copytexturetobuffer', 'copybuffer',
  2370. 'indexbuffer32bit', 'multirendertargetformats', 'clampone', 'blendminmax',
  2371. 'glsl4'
  2372. }
  2373. local features = love.graphics.getSupported()
  2374. for g=1,#gfs do
  2375. test:assertNotEquals(nil, features[gfs[g] ], 'expected a key for graphic feature: ' .. gfs[g])
  2376. end
  2377. end
  2378. -- love.graphics.getSystemLimits
  2379. love.test.graphics.getSystemLimits = function(test)
  2380. -- cant check values as hardware dependent but we can check the keys in the
  2381. -- table match what the documentation lists
  2382. local glimits = {
  2383. 'texelbuffersize', 'shaderstoragebuffersize', 'threadgroupsx',
  2384. 'threadgroupsy', 'pointsize', 'texturesize', 'texturelayers', 'volumetexturesize',
  2385. 'cubetexturesize', 'anisotropy', 'texturemsaa', 'rendertargets', 'threadgroupsz'
  2386. }
  2387. local limits = love.graphics.getSystemLimits()
  2388. for g=1,#glimits do
  2389. test:assertNotEquals(nil, limits[glimits[g] ], 'expected a key for system limit: ' .. glimits[g])
  2390. end
  2391. end
  2392. -- love.graphics.getTextureTypes
  2393. love.test.graphics.getTextureTypes = function(test)
  2394. -- cant check values as hardware dependent but we can check the keys in the
  2395. -- table match what the documentation lists
  2396. local ttypes = {
  2397. '2d', 'array', 'cube', 'volume'
  2398. }
  2399. local types = love.graphics.getTextureTypes()
  2400. for t=1,#ttypes do
  2401. test:assertNotEquals(nil, types[ttypes[t] ], 'expected a key for texture type: ' .. ttypes[t])
  2402. end
  2403. end