graphics.lua 104 KB

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