graphics.lua 101 KB

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