graphics.lua 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092
  1. -- love.graphics
  2. --------------------------------------------------------------------------------
  3. --------------------------------------------------------------------------------
  4. ------------------------------------OBJECTS-------------------------------------
  5. --------------------------------------------------------------------------------
  6. --------------------------------------------------------------------------------
  7. -- Canvas (love.graphics.newCanvas)
  8. love.test.graphics.Canvas = function(test)
  9. -- create canvas with defaults
  10. local canvas = love.graphics.newCanvas(100, 100, {
  11. type = '2d',
  12. format = 'normal',
  13. readable = true,
  14. msaa = 0,
  15. dpiscale = love.graphics.getDPIScale(),
  16. mipmaps = 'auto'
  17. })
  18. test:assertObject(canvas)
  19. -- check texture settings
  20. test:assertEquals(love.graphics.getDPIScale(), canvas:getDPIScale(), 'check dpi scale')
  21. test:assertEquals(1, canvas:getDepth(), 'check depth is 2d')
  22. test:assertEquals(nil, canvas:getDepthSampleMode(), 'check depth sample nil')
  23. local min, mag, ani = canvas:getFilter()
  24. test:assertEquals('nearest', min, 'check filter def min')
  25. test:assertEquals('nearest', mag, 'check filter def mag')
  26. test:assertEquals(1, ani, 'check filter def ani')
  27. canvas:setFilter('linear', 'linear', 2)
  28. min, mag, ani = canvas:getFilter()
  29. test:assertEquals('linear', min, 'check filter changed min')
  30. test:assertEquals('linear', mag, 'check filter changed mag')
  31. test:assertEquals(2, ani, 'check filter changed ani')
  32. test:assertEquals(1, canvas:getLayerCount(), 'check 1 layer for 2d')
  33. test:assertEquals('2d', canvas:getTextureType(), 'check 2d')
  34. local horiz, vert = canvas:getWrap()
  35. test:assertEquals('clamp', horiz, 'check def wrap h')
  36. test:assertEquals('clamp', vert, 'check def wrap v')
  37. canvas:setWrap('repeat', 'repeat')
  38. horiz, vert = canvas:getWrap()
  39. test:assertEquals('repeat', horiz, 'check changed wrap h')
  40. test:assertEquals('repeat', vert, 'check changed wrap v')
  41. test:assertEquals(true, canvas:isReadable(), 'check canvas readable')
  42. test:assertEquals(1, canvas:getMSAA(), 'check samples match')
  43. -- check dimensions
  44. local cw, ch = canvas:getDimensions()
  45. test:assertEquals(100, cw, 'check canvas dim w')
  46. test:assertEquals(100, ch, 'check canvas dim h')
  47. test:assertEquals(cw, canvas:getWidth(), 'check canvas w matches dim')
  48. test:assertEquals(ch, canvas:getHeight(), 'check canvas h matches dim')
  49. local pw, ph = canvas:getPixelDimensions()
  50. test:assertEquals(100*love.graphics.getDPIScale(), pw, 'check pixel dim w')
  51. test:assertEquals(100*love.graphics.getDPIScale(), ph, 'check pixel dim h')
  52. test:assertEquals(pw, canvas:getPixelWidth(), 'check pixel w matches dim')
  53. test:assertEquals(ph, canvas:getPixelHeight(), 'check pixel h matches dim')
  54. -- check mipmaps
  55. local mode, sharpness = canvas:getMipmapFilter()
  56. test:assertEquals('linear', mode, 'check def minmap filter mode')
  57. test:assertEquals(0, sharpness, 'check def minmap filter sharpness')
  58. canvas:setMipmapFilter('nearest', 1)
  59. mode, sharpness = canvas:getMipmapFilter()
  60. test:assertEquals('nearest', mode, 'check changed minmap filter mode')
  61. test:assertEquals(1, sharpness, 'check changed minmap filter sharpness')
  62. test:assertGreaterEqual(2, canvas:getMipmapCount()) -- docs say no mipmaps should return 1
  63. test:assertEquals('auto', canvas:getMipmapMode())
  64. -- check rendering
  65. canvas:renderTo(function()
  66. love.graphics.setColor(1, 0, 0)
  67. love.graphics.rectangle('fill', 0, 0, 200, 200)
  68. love.graphics.setColor(1, 1, 1, 1)
  69. end)
  70. local data = love.graphics.readbackTexture(canvas, {100, 0, 0, 0, 100, 100})
  71. local r, g, b, a = data:getPixel(2, 2)
  72. test:assertEquals(1, r, 'check canvas draw r')
  73. test:assertEquals(0, g, 'check canvas draw g')
  74. test:assertEquals(0, b, 'check canvas draw b')
  75. test:assertEquals(1, a, 'check canvas draw a')
  76. -- check using canvas in love.graphics.draw()
  77. local xcanvas = love.graphics.newCanvas()
  78. love.graphics.setCanvas(xcanvas)
  79. love.graphics.draw(canvas, 0, 0)
  80. love.graphics.setCanvas()
  81. data = love.graphics.readbackTexture(canvas, {100, 0, 0, 0, 100, 100})
  82. r, g, b, a = data:getPixel(2, 2)
  83. test:assertEquals(1, r, 'check canvas draw r')
  84. test:assertEquals(0, g, 'check canvas draw g')
  85. test:assertEquals(0, b, 'check canvas draw b')
  86. test:assertEquals(1, a, 'check canvas draw a')
  87. -- check depth samples
  88. local dcanvas = love.graphics.newCanvas(100, 100, {
  89. type = '2d',
  90. format = 'depth16'
  91. })
  92. test:assertEquals(nil, dcanvas:getDepthSampleMode(), 'check depth sample mode nil by def')
  93. dcanvas:setDepthSampleMode('equal')
  94. test:assertEquals('equal', dcanvas:getDepthSampleMode(), 'check depth sample mode set')
  95. end
  96. -- Font (love.graphics.newFont)
  97. love.test.graphics.Font = function(test)
  98. -- create obj
  99. local font = love.graphics.newFont('resources/font.ttf', 8)
  100. test:assertObject(font)
  101. -- check properties match expected
  102. test:assertEquals(6, font:getAscent(), 'check ascent')
  103. test:assertEquals(6, font:getBaseline(), 'check baseline')
  104. test:assertEquals(1, font:getDPIScale(), 'check dpi')
  105. test:assertEquals(-2, font:getDescent(), 'check descent')
  106. test:assertEquals('nearest', font:getFilter(), 'check filter def')
  107. font:setFilter('linear', 'linear')
  108. test:assertEquals('linear', font:getFilter(), 'check filter change')
  109. font:setFilter('nearest', 'nearest')
  110. test:assertEquals(8, font:getHeight(), 'check height')
  111. test:assertEquals(0, font:getKerning('a', 'b'), 'check kerning')
  112. test:assertEquals(1, font:getLineHeight(), 'check line height')
  113. font:setLineHeight(2)
  114. test:assertEquals(2, font:getLineHeight(), 'check changed line height')
  115. font:setLineHeight(1) -- reset for drawing + wrap later
  116. test:assertEquals(24, font:getWidth('test'), 'check data size')
  117. test:assertEquals(true, font:hasGlyphs('test'), 'check data size')
  118. -- check font wrapping
  119. local width, wrappedtext = font:getWrap('LÖVE is an *awesome* framework you can use to make 2D games in Lua.', 50)
  120. test:assertEquals(48, width, 'check actual wrap width')
  121. test:assertEquals(8, #wrappedtext, 'check wrapped lines')
  122. test:assertEquals('LÖVE is an ', wrappedtext[1], 'check wrapped line')
  123. -- check drawing font
  124. local canvas = love.graphics.newCanvas(16, 16)
  125. love.graphics.setCanvas(canvas)
  126. love.graphics.setFont(font)
  127. love.graphics.print('Aa', 0, 5)
  128. love.graphics.setCanvas()
  129. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  130. test:assertPixels(imgdata, {
  131. white = {{0,3},{4,3},{7,4},{9,4},{10,5},{0,8},{4,8},{10,8}},
  132. }, 'font draw check')
  133. test:exportImg(imgdata)
  134. -- check font substitution
  135. local fontab = love.graphics.newImageFont('resources/font-letters-ab.png', 'AB')
  136. local fontcd = love.graphics.newImageFont('resources/font-letters-cd.png', 'CD')
  137. fontab:setFallbacks(fontcd)
  138. love.graphics.setCanvas(canvas)
  139. love.graphics.clear(0, 0, 0, 0)
  140. love.graphics.setFont(fontab)
  141. love.graphics.print('AB', 0, 0)
  142. love.graphics.print('CD', 0, 9)
  143. love.graphics.setCanvas()
  144. local imgdata2 = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  145. test:assertPixels(imgdata2, {
  146. green = {{1,8},{6,8},{2,10},{5,10},{9,10}},
  147. black = {{9,9},{14,8},{14,10},{14,1},{1,10}}
  148. }, 'font draw check')
  149. test:exportImg(imgdata2)
  150. end
  151. -- Image (love.graphics.newImage)
  152. love.test.graphics.Image = function(test)
  153. -- create object
  154. local image = love.graphics.newImage('resources/love.png', {
  155. dpiscale = 1,
  156. mipmaps = true
  157. })
  158. test:assertObject(image)
  159. -- check texture props
  160. test:assertEquals(love.graphics.getDPIScale(), image:getDPIScale(), 'check dpi scale')
  161. test:assertEquals(1, image:getDepth(), 'check depth is 2d')
  162. test:assertEquals(nil, image:getDepthSampleMode(), 'check depth sample nil')
  163. local min, mag, ani = image:getFilter()
  164. test:assertEquals('nearest', min, 'check filter def min')
  165. test:assertEquals('nearest', mag, 'check filter def mag')
  166. test:assertEquals(1, ani, 'check filter def ani')
  167. image:setFilter('linear', 'linear', 2)
  168. min, mag, ani = image:getFilter()
  169. test:assertEquals('linear', min, 'check filter changed min')
  170. test:assertEquals('linear', mag, 'check filter changed mag')
  171. test:assertEquals(2, ani, 'check filter changed ani')
  172. test:assertEquals(1, image:getLayerCount(), 'check 1 layer for 2d')
  173. test:assertEquals('2d', image:getTextureType(), 'check 2d')
  174. local horiz, vert = image:getWrap()
  175. test:assertEquals('clamp', horiz, 'check def wrap h')
  176. test:assertEquals('clamp', vert, 'check def wrap v')
  177. image:setWrap('repeat', 'repeat')
  178. horiz, vert = image:getWrap()
  179. test:assertEquals('repeat', horiz, 'check changed wrap h')
  180. test:assertEquals('repeat', vert, 'check changed wrap v')
  181. test:assertEquals(true, image:isReadable(), 'check canvas readable')
  182. test:assertEquals(1, image:getMSAA(), 'check samples match')
  183. -- check dimensions
  184. local cw, ch = image:getDimensions()
  185. test:assertEquals(64, cw, 'check canvas dim w')
  186. test:assertEquals(64, ch, 'check canvas dim h')
  187. test:assertEquals(cw, image:getWidth(), 'check canvas w matches dim')
  188. test:assertEquals(ch, image:getHeight(), 'check canvas h matches dim')
  189. local pw, ph = image:getPixelDimensions()
  190. test:assertEquals(64*love.graphics.getDPIScale(), pw, 'check pixel dim w')
  191. test:assertEquals(64*love.graphics.getDPIScale(), ph, 'check pixel dim h')
  192. test:assertEquals(pw, image:getPixelWidth(), 'check pixel w matches dim')
  193. test:assertEquals(ph, image:getPixelHeight(), 'check pixel h matches dim')
  194. -- check mipmaps
  195. local mode, sharpness = image:getMipmapFilter()
  196. test:assertEquals('linear', mode, 'check def minmap filter mode')
  197. test:assertEquals(0, sharpness, 'check def minmap filter sharpness')
  198. image:setMipmapFilter('nearest', 1)
  199. mode, sharpness = image:getMipmapFilter()
  200. test:assertEquals('nearest', mode, 'check changed minmap filter mode')
  201. test:assertEquals(1, sharpness, 'check changed minmap filter sharpness')
  202. test:assertGreaterEqual(2, image:getMipmapCount()) -- docs say no mipmaps should return 1
  203. -- check image properties
  204. test:assertEquals(false, image:isCompressed(), 'check not compressed')
  205. test:assertEquals(false, image:isFormatLinear(), 'check not linear')
  206. local cimage = love.graphics.newImage('resources/love.dxt1')
  207. test:assertObject(cimage)
  208. test:assertEquals(true, cimage:isCompressed(), 'check is compressed')
  209. -- check pixel replacement
  210. local rimage = love.image.newImageData('resources/loveinv.png')
  211. image:replacePixels(rimage)
  212. local canvas = love.graphics.newCanvas(64, 64)
  213. love.graphics.setCanvas(canvas)
  214. love.graphics.draw(image, 0, 0)
  215. love.graphics.setCanvas()
  216. local imgdata = love.graphics.readbackTexture(canvas, {64, 0, 0, 0, 64, 64})
  217. local r1, g1, b1 = imgdata:getPixel(25, 25)
  218. test:assertEquals(3, r1+g1+b1, 'check back to white')
  219. test:exportImg(imgdata)
  220. end
  221. -- Mesh (love.graphics.newMesh)
  222. love.test.graphics.Mesh = function(test)
  223. test:skipTest('test class needs writing')
  224. end
  225. -- ParticleSystem (love.graphics.newParticleSystem)
  226. love.test.graphics.ParticleSystem = function(test)
  227. test:skipTest('test class needs writing')
  228. end
  229. -- Quad (love.graphics.newQuad)
  230. love.test.graphics.Quad = function(test)
  231. local texture = love.graphics.newImage('resources/love.png')
  232. local quad = love.graphics.newQuad(0, 0, 32, 32, texture)
  233. test:assertObject(quad)
  234. -- check properties
  235. test:assertEquals(1, quad:getLayer(), 'check default layer')
  236. quad:setLayer(2)
  237. test:assertEquals(2, quad:getLayer(), 'check changed layer')
  238. local sw, sh = quad:getTextureDimensions()
  239. test:assertEquals(64, sw, 'check texture w')
  240. test:assertEquals(64, sh, 'check texture h')
  241. -- check drawing and viewport changes
  242. local canvas = love.graphics.newCanvas(64, 64)
  243. love.graphics.setCanvas(canvas)
  244. love.graphics.draw(texture, quad, 0, 0)
  245. quad:setViewport(32, 32, 32, 32, 64, 64)
  246. love.graphics.draw(texture, quad, 32, 32)
  247. love.graphics.setCanvas()
  248. local imgdata = love.graphics.readbackTexture(canvas, {64, 0, 0, 0, 64, 64})
  249. test:assertPixels(imgdata, {
  250. white = {{17,31},{31,31},{31,24},{32,32},{46,32},{32,46}},
  251. lovepink = {{2,31},{31,2}},
  252. loveblue = {{32,61},{61,32}}
  253. }, 'check quad drawing')
  254. test:exportImg(imgdata)
  255. end
  256. -- Shader (love.graphics.newShader)
  257. love.test.graphics.Shader = function(test)
  258. -- check valid shader
  259. local pixelcode1 = [[
  260. extern Image tex2;
  261. vec4 effect(vec4 color, Image tex, vec2 texture_coords, vec2 screen_coords) {
  262. vec4 texturecolor = Texel(tex2, texture_coords);
  263. return texturecolor * color;
  264. }
  265. ]]
  266. local vertexcode1 = [[
  267. vec4 position(mat4 transform_projection, vec4 vertex_position) {
  268. return transform_projection * vertex_position;
  269. }
  270. ]]
  271. local shader1 = love.graphics.newShader(pixelcode1, vertexcode1)
  272. test:assertObject(shader1)
  273. test:assertEquals('vertex shader:\npixel shader:\n', shader1:getWarnings(), 'check shader valid')
  274. test:assertEquals(false, shader1:hasUniform('tex1'), 'check invalid uniform')
  275. test:assertEquals(true, shader1:hasUniform('tex2'), 'check valid uniform')
  276. -- check invalid shader
  277. local pixelcode2 = [[
  278. extern float ww;
  279. vec4 effect(vec4 color, Image tex, vec2 texture_coords, vec2 screen_coords) {
  280. vec4 texturecolor = Texel(tex, texture_coords);
  281. float unused = ww * 3 * color;
  282. return texturecolor * color;
  283. }
  284. ]]
  285. local res, err = pcall(love.graphics.newShader, pixelcode2, vertexcode1)
  286. test:assertNotEquals(nil, err, 'check shader compile fails')
  287. -- check using a shader to draw + sending uniforms
  288. -- shader will return a given color if overwrite set to 1, otherwise def. draw
  289. local pixelcode3 = [[
  290. extern vec4 col;
  291. extern float overwrite;
  292. vec4 effect(vec4 color, Image tex, vec2 texture_coords, vec2 screen_coords) {
  293. vec4 texcol = Texel(tex, texture_coords);
  294. if (overwrite == 1) {
  295. return col;
  296. } else {
  297. return texcol * color;
  298. }
  299. }
  300. ]]
  301. local shader3 = love.graphics.newShader(pixelcode3, vertexcode1)
  302. local canvas = love.graphics.newCanvas(16, 16)
  303. love.graphics.setCanvas(canvas)
  304. -- set color to yellow
  305. love.graphics.setColor(1, 1, 0, 1)
  306. -- turn shader 'on' and use red to draw
  307. shader3:send('overwrite', 1)
  308. shader3:sendColor('col', {1, 0, 0, 1})
  309. love.graphics.setShader(shader3)
  310. love.graphics.rectangle('fill', 0, 0, 8, 8)
  311. love.graphics.setShader()
  312. -- turn shader 'off' and draw again
  313. shader3:send('overwrite', 0)
  314. love.graphics.setShader(shader3)
  315. love.graphics.rectangle('fill', 8, 8, 8, 8)
  316. love.graphics.setShader()
  317. love.graphics.setColor(1, 1, 1, 1)
  318. love.graphics.setCanvas()
  319. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  320. test:assertPixels(imgdata, {
  321. red = {{1,1},{1,7},{7,7},{7,1}},
  322. yellow = {{8,8},{8,15},{15,15},{15,8}}
  323. }, 'shader draw check')
  324. test:exportImg(imgdata)
  325. end
  326. -- SpriteBatch (love.graphics.newSpriteBatch)
  327. love.test.graphics.SpriteBatch = function(test)
  328. test:skipTest('test class needs writing')
  329. end
  330. -- Text (love.graphics.newTextBatch)
  331. love.test.graphics.Text = function(test)
  332. local font = love.graphics.newFont('resources/font.ttf', 8)
  333. local plaintext = love.graphics.newTextBatch(font, 'test')
  334. -- check text properties
  335. test:assertObject(plaintext)
  336. test:assertEquals(font:getHeight(), plaintext:getFont():getHeight(), 'check font matches')
  337. local tw, th = plaintext:getDimensions()
  338. test:assertEquals(24, tw, 'check initial dim w')
  339. test:assertEquals(8, th, 'check initial dim h')
  340. test:assertEquals(tw, plaintext:getWidth(), 'check initial dim w')
  341. test:assertEquals(th, plaintext:getHeight(), 'check initial dim h')
  342. -- check changing text
  343. plaintext:add('more text', 100, 0, 0)
  344. test:assertEquals(49, plaintext:getDimensions(), 'check adding text')
  345. plaintext:set('test')
  346. test:assertEquals(24, plaintext:getDimensions(), 'check resetting text')
  347. plaintext:clear()
  348. test:assertEquals(0, plaintext:getDimensions(), 'check clearing text')
  349. -- check drawing + setting more complex text
  350. local colortext = love.graphics.newTextBatch(font, {{1, 0, 0, 1}, 'test'})
  351. test:assertObject(colortext)
  352. colortext:setf('LÖVE is an *awesome* framework you can use to make 2D games in Lua', 60, 'right')
  353. colortext:addf({{1, 1, 0}, 'overlap'}, 1000, 'left')
  354. local font2 = love.graphics.newFont('resources/font.ttf', 8)
  355. colortext:setFont(font2)
  356. local canvas = love.graphics.newCanvas(64, 64)
  357. love.graphics.setCanvas(canvas)
  358. love.graphics.draw(colortext, 0, 10)
  359. love.graphics.setCanvas()
  360. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  361. test:assertPixels(imgdata, {
  362. yellow = {{1,9},{8,13},{16,11},{22,10},{25,7},{29,9},{32,13},{34,15}},
  363. white = {{17,13},{30,12},{38,9},{44,13},{58,13},{8,29},{58,29},{57,37},{5,39},{57,45},{1,55}}
  364. }, 'text draw check')
  365. test:exportImg(imgdata)
  366. end
  367. -- Video (love.graphics.newVideo)
  368. love.test.graphics.Video = function(test)
  369. test:skipTest('test class needs writing')
  370. end
  371. --------------------------------------------------------------------------------
  372. --------------------------------------------------------------------------------
  373. ------------------------------------DRAWING-------------------------------------
  374. --------------------------------------------------------------------------------
  375. --------------------------------------------------------------------------------
  376. -- love.graphics.arc
  377. love.test.graphics.arc = function(test)
  378. -- draw some arcs using pi format
  379. local canvas = love.graphics.newCanvas(32, 32)
  380. love.graphics.setCanvas(canvas)
  381. love.graphics.clear(0, 0, 0, 1)
  382. love.graphics.setColor(1, 1, 1, 1)
  383. love.graphics.arc('line', "pie", 16, 16, 16, 0 * (math.pi/180), 360 * (math.pi/180), 10)
  384. love.graphics.arc('fill', "pie", 16, 16, 16, 270 * (math.pi/180), 45 * (math.pi/180), 10)
  385. love.graphics.setColor(1, 0, 0, 1)
  386. love.graphics.arc('line', "pie", 16, 16, 16, 0 * (math.pi/180), 90 * (math.pi/180), 10)
  387. love.graphics.setColor(1, 1, 0, 1)
  388. love.graphics.arc('line', "pie", 16, 16, 16, 180 * (math.pi/180), 135 * (math.pi/180), 10)
  389. love.graphics.setColor(1, 1, 1, 1)
  390. love.graphics.setCanvas()
  391. local imgdata1 = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  392. test:assertPixels(imgdata1, {
  393. white = {{11,0},{20,0},{0,13},{0,14},{31,13},{31,14},{15,14}},
  394. black = {{16,14},{16,16},{30,14},{30,16}},
  395. yellow = {{15,15},{15,16},{16,15},{0,15},{4,27},{5,26},{14,17}},
  396. red = {{15,17},{15,31},{17,15},{31,15},{28,26},{27,27}}
  397. }, 'arc pi')
  398. -- draw some arcs with open format
  399. love.graphics.setCanvas(canvas)
  400. love.graphics.clear(0, 0, 0, 1)
  401. love.graphics.setColor(1, 1, 1, 1)
  402. love.graphics.arc('line', "open", 16, 16, 16, 0 * (math.pi/180), 315 * (math.pi/180), 10)
  403. love.graphics.setColor(1, 0, 0, 1)
  404. love.graphics.arc('fill', "open", 16, 16, 16, 0 * (math.pi/180), 180 * (math.pi/180), 10)
  405. love.graphics.setColor(1, 1, 0, 1)
  406. love.graphics.arc('fill', "open", 16, 16, 16, 180 * (math.pi/180), 90 * (math.pi/180), 10)
  407. love.graphics.setColor(1, 1, 1, 1)
  408. love.graphics.setCanvas()
  409. local imgdata2 = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  410. test:assertPixels(imgdata2, {
  411. white = {{11,0},{20,0},{26,4},{5,4},{0,15},{19,31},{31,19}},
  412. black = {{27,5},{27,4},{26,5},{1,15},{31,15}},
  413. yellow = {{0,17},{0,19},{12,31},{14,31},{6,23},{7,24}},
  414. red = {{0,16},{31,16},{31,18},{30,21},{18,31},{15,16},{16,16}}
  415. }, 'arc open')
  416. -- draw some arcs with closed format
  417. love.graphics.setCanvas(canvas)
  418. love.graphics.clear(0, 0, 0, 1)
  419. love.graphics.setColor(1, 1, 1, 1)
  420. love.graphics.arc('line', "closed", 16, 16, 16, 0 * (math.pi/180), 315 * (math.pi/180), 10)
  421. love.graphics.setColor(1, 0, 0, 1)
  422. love.graphics.arc('fill', "closed", 16, 16, 16, 0 * (math.pi/180), 180 * (math.pi/180), 10)
  423. love.graphics.setColor(1, 1, 0, 1)
  424. love.graphics.arc('line', "closed", 16, 16, 16, 180 * (math.pi/180), 90 * (math.pi/180), 10)
  425. love.graphics.setColor(1, 1, 1, 1)
  426. love.graphics.setCanvas()
  427. local imgdata3 = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  428. test:assertPixels(imgdata3, {
  429. white = {{11,0},{20,0},{26,4},{5,4},{0,15},{19,31},{31,19}},
  430. yellow = {{0,17},{0,19},{12,31},{14,31}},
  431. red = {{31,16},{31,18},{30,21},{18,31},{15,16},{16,16}}
  432. }, 'arc open')
  433. test:exportImg(imgdata1)
  434. test:exportImg(imgdata2)
  435. test:exportImg(imgdata3)
  436. end
  437. -- love.graphics.circle
  438. love.test.graphics.circle = function(test)
  439. -- draw some circles
  440. local canvas = love.graphics.newCanvas(32, 32)
  441. love.graphics.setCanvas(canvas)
  442. love.graphics.clear(0, 0, 0, 1)
  443. love.graphics.setColor(1, 1, 1, 1)
  444. love.graphics.circle('fill', 16, 16, 16)
  445. love.graphics.setColor(1, 0, 0, 1)
  446. love.graphics.circle('line', 16, 16, 16)
  447. love.graphics.setColor(1, 1, 0, 1)
  448. love.graphics.circle('fill', 16, 16, 8)
  449. love.graphics.setColor(0, 1, 0, 1)
  450. love.graphics.circle('fill', 16, 16, 4)
  451. love.graphics.setColor(1, 1, 1, 1)
  452. love.graphics.setCanvas()
  453. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  454. test:assertPixels(imgdata, {
  455. white = {{13,8},{18,8},{23,13},{23,18}},
  456. green = {
  457. {15,12},{16,12},{13,13},{18,13},{12,15},{12,16},{13,18},{18,18},
  458. {15,19},{16,19},{19,15},{19,16}
  459. },
  460. black = {{10,0},{21,0},{0,10},{0,21},{31,10},{31,21},{10,31},{21,31}},
  461. yellow = {
  462. {11,10},{10,11},{8,14},{8,17},{10,20},{11,21},{14,23},{17,23},{20,21},
  463. {21,20},{23,17},{23,14},{20,10},{21,11},{17,8},{14,8}
  464. },
  465. red = {{11,0},{20,0},{11,31},{20,31},{0,11},{0,20},{31,20},{31,11}}
  466. }, 'circle')
  467. test:exportImg(imgdata)
  468. end
  469. -- love.graphics.clear
  470. love.test.graphics.clear = function(test)
  471. local canvas = love.graphics.newCanvas(16, 16)
  472. love.graphics.setCanvas(canvas)
  473. love.graphics.clear(0, 0, 0, 1)
  474. love.graphics.clear(1, 1, 0, 1)
  475. love.graphics.setCanvas()
  476. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  477. test:assertPixels(imgdata, {
  478. yellow = {{0,0},{15,0},{0,15},{15,15},{8,8}}
  479. }, 'clear')
  480. test:exportImg(imgdata)
  481. end
  482. -- love.graphics.discard
  483. love.test.graphics.discard = function(test)
  484. -- from the docs: "on some desktops this may do nothing"
  485. test:skipTest('cant test this worked')
  486. end
  487. -- love.graphics.draw
  488. love.test.graphics.draw = function(test)
  489. local canvas1 = love.graphics.newCanvas(32, 32)
  490. local canvas2 = love.graphics.newCanvas(32, 32)
  491. local transform = love.math.newTransform( )
  492. transform:translate(16, 0)
  493. transform:scale(0.5, 0.5)
  494. love.graphics.setCanvas(canvas1)
  495. love.graphics.clear(0, 0, 0, 1)
  496. -- img, offset
  497. love.graphics.draw(Logo.texture, Logo.img, 0, 0, 0, 1, 1, 16, 16)
  498. love.graphics.setCanvas()
  499. love.graphics.setCanvas(canvas2)
  500. love.graphics.clear(1, 0, 0, 1)
  501. -- canvas, scale, shear, transform obj
  502. love.graphics.draw(canvas1, 0, 0, 0, 0.5, 0.5, 0, 0, 2, 2)
  503. love.graphics.draw(canvas1, 0, 16, 0, 0.5, 0.5)
  504. love.graphics.draw(canvas1, 16, 16, 0, 0.5, 0.5)
  505. love.graphics.draw(canvas1, transform)
  506. love.graphics.setCanvas()
  507. local imgdata = love.graphics.readbackTexture(canvas2, {16, 0, 0, 0, 16, 16})
  508. test:assertPixels(imgdata, {
  509. lovepink = {{23,3},{23,19},{7,19},{0,0},{16,0},{0,16},{16,16}},
  510. loveblue = {{0,31},{15,17},{15,31},{16,31},{31,17},{31,31},{16,15},{31,15}},
  511. white = {{15, 15},{6,19},{8,19},{22,19},{24,19},{22,3},{24,3}},
  512. red = {{0,1},{1,0},{15,0},{15,7},{0,15},{7,15}}
  513. }, 'drawing')
  514. test:exportImg(imgdata)
  515. end
  516. -- love.graphics.drawInstanced
  517. love.test.graphics.drawInstanced = function(test)
  518. test:skipTest('test class needs writing')
  519. end
  520. -- love.graphics.drawLayer
  521. love.test.graphics.drawLayer = function(test)
  522. local image = love.graphics.newArrayImage({
  523. 'resources/love.png', 'resources/loveinv.png',
  524. 'resources/love.png', 'resources/loveinv.png'
  525. })
  526. local canvas = love.graphics.newCanvas(64, 64)
  527. love.graphics.setCanvas(canvas)
  528. love.graphics.clear(0, 0, 0, 1)
  529. love.graphics.drawLayer(image, 1, 0, 0, 0, 1, 1)
  530. love.graphics.drawLayer(image, 2, 32, 0, 0, 0.5, 0.5)
  531. love.graphics.drawLayer(image, 4, 0, 32, 0, 0.5, 0.5)
  532. love.graphics.drawLayer(image, 3, 32, 32, 0, 2, 2, 16, 16)
  533. love.graphics.setCanvas()
  534. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  535. test:assertPixels(imgdata, {
  536. lovepink = {{30,2},{33,2},{2,30},{2,33},{4,60},{4,63},{60,4},{63,4},{31,23},{32,23}},
  537. loveblue = {{14,33},{17,33},{46,1},{49,1},{1,46},{1,49},{33,14},{33,17}},
  538. black = {{0,0},{63,0},{0,63},{39,6},{40,6},{6,39},{6,40},{6,55},{55,6}},
  539. white = {{46,11},{48,11},{14,43},{16,43},{30,23},{33,23},{34,54},{53,40},{63,63}}
  540. }, 'draw layer')
  541. test:exportImg(imgdata)
  542. end
  543. -- love.graphics.ellipse
  544. love.test.graphics.ellipse = function(test)
  545. local canvas = love.graphics.newCanvas(32, 32)
  546. love.graphics.setCanvas(canvas)
  547. love.graphics.clear(0, 0, 0, 1)
  548. love.graphics.setColor(1, 0, 0, 1)
  549. love.graphics.ellipse('fill', 16, 16, 16, 8)
  550. love.graphics.setColor(1, 1, 0, 1)
  551. love.graphics.ellipse('fill', 24, 24, 10, 24)
  552. love.graphics.setColor(1, 0, 1, 1)
  553. love.graphics.ellipse('fill', 16, 0, 8, 16)
  554. love.graphics.setColor(1, 1, 1, 1)
  555. love.graphics.setCanvas()
  556. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  557. test:assertPixels(imgdata, {
  558. red = {{0,14},{0,17},{7,9},{7,22},{14,15},{14,16}},
  559. pink = {{15,15},{16,15},{8,0},{8,4},{23,0},{23,4},{13,14},{18,14}},
  560. yellow = {{24,0},{25,0},{14,17},{14,30},{15,31},{31,8}}
  561. }, 'ellipses')
  562. test:exportImg(imgdata)
  563. end
  564. -- love.graphics.flushBatch
  565. love.test.graphics.flushBatch = function(test)
  566. love.graphics.flushBatch()
  567. local initial = love.graphics.getStats()['drawcalls']
  568. local canvas = love.graphics.newCanvas(32, 32)
  569. love.graphics.setCanvas(canvas)
  570. love.graphics.clear(0, 0, 0, 1)
  571. love.graphics.rectangle('fill', 0, 0, 32, 32)
  572. love.graphics.setColor(1, 1, 1, 1)
  573. love.graphics.setCanvas()
  574. love.graphics.flushBatch()
  575. local after = love.graphics.getStats()['drawcalls']
  576. test:assertEquals(initial+1, after, 'check drawcalls increased')
  577. end
  578. -- love.graphics.line
  579. love.test.graphics.line = function(test)
  580. local canvas = love.graphics.newCanvas(16, 16)
  581. love.graphics.setCanvas(canvas)
  582. love.graphics.clear(0, 0, 0, 1)
  583. love.graphics.setColor(1, 0, 0, 1)
  584. love.graphics.line(1,1,16,1,16,16,1,16,1,1)
  585. love.graphics.setColor(1, 1, 0, 1)
  586. love.graphics.line({0,0,8,8,16,0,8,8,16,16,8,8,0,16})
  587. love.graphics.setColor(1, 1, 1, 1)
  588. love.graphics.setCanvas()
  589. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  590. test:assertPixels(imgdata, {
  591. yellow = {{0,0},{15,0},{0,15},{15,15},{7,7},{8,7},{8,7},{8,8}},
  592. red = {{1,0},{14,0},{0,1},{0,14},{15,1},{15,14},{1,15},{14,15}}
  593. }, 'lines')
  594. test:exportImg(imgdata)
  595. end
  596. -- love.graphics.points
  597. love.test.graphics.points = function(test)
  598. local canvas = love.graphics.newCanvas(16, 16)
  599. love.graphics.setCanvas(canvas)
  600. love.graphics.clear(0, 0, 0, 1)
  601. love.graphics.setColor(1, 0, 0, 1)
  602. love.graphics.points(1,1,16,1,16,16,1,16,1,1)
  603. love.graphics.setColor(1, 1, 0, 1)
  604. love.graphics.points({2,2,8,8,15,2,8,9,15,15,9,9,2,15,9,8})
  605. love.graphics.setColor(1, 1, 1, 1)
  606. love.graphics.setCanvas()
  607. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  608. test:assertPixels(imgdata, {
  609. yellow = {{1,1},{14,1},{1,14},{14,14},{7,7},{8,7},{8,7},{8,8}},
  610. red = {{0,0},{15,0},{15,15},{0,15}}
  611. }, 'points')
  612. test:exportImg(imgdata)
  613. end
  614. -- love.graphics.polygon
  615. love.test.graphics.polygon = function(test)
  616. local canvas = love.graphics.newCanvas(16, 16)
  617. love.graphics.setCanvas(canvas)
  618. love.graphics.clear(0, 0, 0, 1)
  619. love.graphics.setColor(1, 0, 0, 1)
  620. love.graphics.polygon("fill", 1, 1, 4, 5, 8, 10, 16, 2, 7, 3, 5, 16, 16, 16, 1, 8)
  621. love.graphics.setColor(1, 1, 0, 1)
  622. love.graphics.polygon("line", {2, 2, 4, 5, 3, 7, 8, 15, 12, 4, 5, 10})
  623. love.graphics.setColor(1, 1, 1, 1)
  624. love.graphics.setCanvas()
  625. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  626. test:assertPixels(imgdata, {
  627. yellow = {{1,0},{1,1},{5,9},{7,14},{8,14},{12,3}},
  628. red = {{2,1},{1,2},{1,7},{5,15},{14,15},{8,8},{14,2},{7,1}}
  629. }, 'polygon')
  630. test:exportImg(imgdata)
  631. end
  632. -- love.graphics.print
  633. love.test.graphics.print = function(test)
  634. love.graphics.setFont(Font)
  635. local canvas = love.graphics.newCanvas(16, 16)
  636. love.graphics.setCanvas(canvas)
  637. love.graphics.clear(0, 0, 0, 1)
  638. love.graphics.setColor(1, 0, 0, 1)
  639. love.graphics.print('love', 0, 3, 0, 1, 1, 0, 0)
  640. love.graphics.setColor(0, 1, 0, 1)
  641. love.graphics.print('ooo', 0, 3, 0, 2, 2, 0, 0)
  642. love.graphics.setColor(0, 0, 1, 1)
  643. love.graphics.print('hello', 0, 3, 90*(math.pi/180), 1, 1, 0, 8)
  644. love.graphics.setColor(1, 1, 1, 1)
  645. love.graphics.setCanvas()
  646. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  647. test:assertPixels(imgdata, {
  648. red = {{0,0},{1,0},{1,1},{2,6},{4,4},{7,6},{10,2},{11,5},{14,3},{14,4}},
  649. green = {
  650. {2,1},{2,2},{0,3},{1,3},{1,8},{2,9},{7,10},{8,8},{9,4},{13,3},{14,2},
  651. {13,8},{14,9}
  652. },
  653. blue = {
  654. {4,15},{10,15},{4,12},{6,12},{8,12},{5,9},{7,9},{4,3},{10,3},{8,6},{7,7},
  655. {4,7},{7,13},{8,12}
  656. }
  657. }, 'print')
  658. test:exportImg(imgdata)
  659. end
  660. -- love.graphics.printf
  661. love.test.graphics.printf = function(test)
  662. love.graphics.setFont(Font)
  663. local canvas = love.graphics.newCanvas(32, 32)
  664. love.graphics.setCanvas(canvas)
  665. love.graphics.clear(0, 0, 0, 1)
  666. love.graphics.setColor(1, 0, 0, 1)
  667. love.graphics.printf('love', 0, 0, 8, "left")
  668. love.graphics.setColor(0, 1, 0, 1)
  669. love.graphics.printf('love', 0, 5, 16, "right")
  670. love.graphics.setColor(0, 0, 1, 1)
  671. love.graphics.printf('love', 0, 7, 32, "center")
  672. love.graphics.setColor(1, 1, 1, 1)
  673. love.graphics.setCanvas()
  674. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  675. test:assertPixels(imgdata, {
  676. red = {
  677. {1,0},{1,1},{0,3},{2,3},{2,7},{0,9},{3,11},{4,10},{0,15},{4,15},{2,19},
  678. {0,24},{1,23},{3,23},{4,24},{0,26},{1,27},{2,27},{3,27}
  679. },
  680. green = {
  681. {1,2},{0,8},{1,8},{2,8},{4,7},{5,8},{7,8},{8,7},{10,4},{14,4},{11,7},
  682. {12,8},{10,13},{11,12},{13,12},{14,13},{10,15},{11,16}
  683. },
  684. blue = {{6,4},{6,10},{9,7},{10,6},{16,9},{18,9},{21,8},{25,8}}
  685. }, 'printf')
  686. test:exportImg(imgdata)
  687. end
  688. -- love.graphics.rectangle
  689. love.test.graphics.rectangle = function(test)
  690. -- setup, draw a 16x16 red rectangle with a blue central square
  691. local canvas = love.graphics.newCanvas(16, 16)
  692. love.graphics.setCanvas(canvas)
  693. love.graphics.clear(0, 0, 0, 1)
  694. love.graphics.setColor(1, 0, 0, 1)
  695. love.graphics.rectangle('fill', 0, 0, 16, 16)
  696. love.graphics.setColor(0, 0, 1, 1)
  697. love.graphics.rectangle('fill', 6, 6, 4, 4)
  698. love.graphics.setColor(1, 1, 1, 1)
  699. love.graphics.setCanvas()
  700. local imgdata1 = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  701. -- test, check red bg and blue central square
  702. test:assertPixels(imgdata1, {
  703. red = {{0,0},{15,0},{15,15},{0,15}},
  704. blue = {{6,6},{9,6},{9,9},{6,9}}
  705. }, 'fill')
  706. -- clear canvas to do some line testing
  707. love.graphics.setCanvas(canvas)
  708. love.graphics.clear(0, 0, 0, 1)
  709. love.graphics.setColor(1, 0, 0, 1)
  710. love.graphics.rectangle('line', 1, 1, 15, 15) -- red border
  711. love.graphics.setColor(0, 0, 1, 1)
  712. love.graphics.rectangle('line', 1, 1, 2, 15) -- 3x16 left aligned blue outline
  713. love.graphics.setColor(0, 1, 0, 1)
  714. love.graphics.rectangle('line', 11, 1, 5, 15) -- 6x16 right aligned green outline
  715. love.graphics.setColor(1, 1, 1, 1)
  716. love.graphics.setCanvas()
  717. local imgdata2 = love.graphics.readbackTexture(canvas, {1, 1, 0, 0, 16, 16})
  718. -- -- check corners and inner corners
  719. test:assertPixels(imgdata2, {
  720. red = {{3,0},{9,0},{3,15,9,15}},
  721. blue = {{0,0},{2,0},{0,15},{2,15}},
  722. green = {{10,0},{15,0},{10,15},{15,15}},
  723. black = {
  724. {1,1},{1,14},{3,1},{9,1},{3,14},
  725. {9,14},{11,1},{14,1},{11,14},{14,14}
  726. }
  727. }, 'line')
  728. test:exportImg(imgdata1)
  729. test:exportImg(imgdata2)
  730. end
  731. --------------------------------------------------------------------------------
  732. --------------------------------------------------------------------------------
  733. --------------------------------OBJECT CREATION---------------------------------
  734. --------------------------------------------------------------------------------
  735. --------------------------------------------------------------------------------
  736. -- love.graphics.captureScreenshot
  737. love.test.graphics.captureScreenshot = function(test)
  738. love.graphics.captureScreenshot('example-screenshot.png')
  739. test:waitFrames(10)
  740. -- need to wait until end of the frame for the screenshot
  741. test:assertNotNil(love.filesystem.openFile('example-screenshot.png', 'r'))
  742. love.filesystem.remove('example-screenshot.png')
  743. end
  744. -- love.graphics.newArrayImage
  745. -- @NOTE this is just basic nil checking, objs have their own test method
  746. love.test.graphics.newArrayImage = function(test)
  747. test:assertObject(love.graphics.newArrayImage({
  748. 'resources/love.png', 'resources/love2.png', 'resources/love3.png'
  749. }))
  750. end
  751. -- love.graphics.newCanvas
  752. -- @NOTE this is just basic nil checking, objs have their own test method
  753. love.test.graphics.newCanvas = function(test)
  754. test:assertObject(love.graphics.newCanvas(16, 16, {
  755. type = '2d',
  756. format = 'normal',
  757. readable = true,
  758. msaa = 0,
  759. dpiscale = 1,
  760. mipmaps = 'none'
  761. }))
  762. test:assertObject(love.graphics.newCanvas(1000, 1000))
  763. end
  764. -- love.graphics.newCubeImage
  765. -- @NOTE this is just basic nil checking, objs have their own test method
  766. love.test.graphics.newCubeImage = function(test)
  767. test:assertObject(love.graphics.newCubeImage('resources/cubemap.png', {
  768. mipmaps = false,
  769. linear = false
  770. }))
  771. end
  772. -- love.graphics.newFont
  773. -- @NOTE this is just basic nil checking, objs have their own test method
  774. love.test.graphics.newFont = function(test)
  775. test:assertObject(love.graphics.newFont('resources/font.ttf'))
  776. test:assertObject(love.graphics.newFont('resources/font.ttf', 8, "normal", 1))
  777. end
  778. -- love.graphics.newImage
  779. -- @NOTE this is just basic nil checking, objs have their own test method
  780. love.test.graphics.newImage = function(test)
  781. test:assertObject(love.graphics.newImage('resources/love.png', {
  782. mipmaps = false,
  783. linear = false,
  784. dpiscale = 1
  785. }))
  786. end
  787. -- love.graphics.newImageFont
  788. -- @NOTE this is just basic nil checking, objs have their own test method
  789. love.test.graphics.newImageFont = function(test)
  790. test:assertObject(love.graphics.newImageFont('resources/love.png', 'ABCD', 1))
  791. end
  792. -- love.graphics.newMesh
  793. -- @NOTE this is just basic nil checking, objs have their own test method
  794. love.test.graphics.newMesh = function(test)
  795. test:assertObject(love.graphics.newMesh({{1, 1, 0, 0, 1, 1, 1, 1}}, 'fan', 'dynamic'))
  796. end
  797. -- love.graphics.newParticleSystem
  798. -- @NOTE this is just basic nil checking, objs have their own test method
  799. love.test.graphics.newParticleSystem = function(test)
  800. local imgdata = love.graphics.newImage('resources/love.png')
  801. test:assertObject(love.graphics.newParticleSystem(imgdata, 1000))
  802. end
  803. -- love.graphics.newQuad
  804. -- @NOTE this is just basic nil checking, objs have their own test method
  805. love.test.graphics.newQuad = function(test)
  806. local imgdata = love.graphics.newImage('resources/love.png')
  807. test:assertObject(love.graphics.newQuad(0, 0, 16, 16, imgdata))
  808. end
  809. -- love.graphics.newShader
  810. -- @NOTE this is just basic nil checking, objs have their own test method
  811. love.test.graphics.newShader = function(test)
  812. local pixelcode = [[
  813. vec4 effect(vec4 color, Image tex, vec2 texture_coords, vec2 screen_coords) {
  814. vec4 texturecolor = Texel(tex, texture_coords);
  815. return texturecolor * color;
  816. }
  817. ]]
  818. local vertexcode = [[
  819. vec4 position(mat4 transform_projection, vec4 vertex_position) {
  820. return transform_projection * vertex_position;
  821. }
  822. ]]
  823. test:assertObject(love.graphics.newShader(pixelcode, vertexcode))
  824. end
  825. -- love.graphics.newSpriteBatch
  826. -- @NOTE this is just basic nil checking, objs have their own test method
  827. love.test.graphics.newSpriteBatch = function(test)
  828. local imgdata = love.graphics.newImage('resources/love.png')
  829. test:assertObject(love.graphics.newSpriteBatch(imgdata, 1000))
  830. end
  831. -- love.graphics.newText
  832. -- @NOTE this is just basic nil checking, objs have their own test method
  833. love.test.graphics.newTextBatch = function(test)
  834. local font = love.graphics.newFont('resources/font.ttf')
  835. test:assertObject(love.graphics.newTextBatch(font, 'helloworld'))
  836. end
  837. -- love.graphics.newVideo
  838. -- @NOTE this is just basic nil checking, objs have their own test method
  839. love.test.graphics.newVideo = function(test)
  840. test:assertObject(love.graphics.newVideo('resources/sample.ogv', {
  841. audio = false,
  842. dpiscale = 1
  843. }))
  844. end
  845. -- love.graphics.newVolumeImage
  846. -- @NOTE this is just basic nil checking, objs have their own test method
  847. love.test.graphics.newVolumeImage = function(test)
  848. test:assertObject(love.graphics.newVolumeImage({
  849. 'resources/love.png', 'resources/love2.png', 'resources/love3.png'
  850. }, {
  851. mipmaps = false,
  852. linear = false
  853. }))
  854. end
  855. -- love.graphics.validateShader
  856. love.test.graphics.validateShader = function(test)
  857. local pixelcode = [[
  858. vec4 effect(vec4 color, Image tex, vec2 texture_coords, vec2 screen_coords) {
  859. vec4 texturecolor = Texel(tex, texture_coords);
  860. return texturecolor * color;
  861. }
  862. ]]
  863. local vertexcode = [[
  864. vec4 position(mat4 transform_projection, vec4 vertex_position) {
  865. return transform_projection * vertex_position;
  866. }
  867. ]]
  868. -- check made up code first
  869. local status, _ = love.graphics.validateShader(true, 'nothing here', 'or here')
  870. test:assertEquals(false, status, 'check invalid shader code')
  871. -- check real code
  872. status, _ = love.graphics.validateShader(true, pixelcode, vertexcode)
  873. test:assertEquals(true, status, 'check valid shader code')
  874. end
  875. --------------------------------------------------------------------------------
  876. --------------------------------------------------------------------------------
  877. ---------------------------------GRAPHICS STATE---------------------------------
  878. --------------------------------------------------------------------------------
  879. --------------------------------------------------------------------------------
  880. -- love.graphics.getBackgroundColor
  881. love.test.graphics.getBackgroundColor = function(test)
  882. -- check default bg is black
  883. local r, g, b, a = love.graphics.getBackgroundColor()
  884. test:assertEquals(0, r, 'check default background r')
  885. test:assertEquals(0, g, 'check default background g')
  886. test:assertEquals(0, b, 'check default background b')
  887. test:assertEquals(1, a, 'check default background a')
  888. -- check set value returns correctly
  889. love.graphics.setBackgroundColor(1, 1, 1, 0)
  890. r, g, b, a = love.graphics.getBackgroundColor()
  891. test:assertEquals(1, r, 'check updated background r')
  892. test:assertEquals(1, g, 'check updated background g')
  893. test:assertEquals(1, b, 'check updated background b')
  894. test:assertEquals(0, a, 'check updated background a')
  895. love.graphics.setBackgroundColor(0, 0, 0, 1) -- reset
  896. end
  897. -- love.graphics.getBlendMode
  898. love.test.graphics.getBlendMode = function(test)
  899. -- check default blend mode
  900. local mode, alphamode = love.graphics.getBlendMode()
  901. test:assertEquals('alpha', mode, 'check default blend mode')
  902. test:assertEquals('alphamultiply', alphamode, 'check default alpha blend')
  903. -- check set mode returns correctly
  904. love.graphics.setBlendMode('add', 'premultiplied')
  905. mode, alphamode = love.graphics.getBlendMode()
  906. test:assertEquals('add', mode, 'check changed blend mode')
  907. test:assertEquals('premultiplied', alphamode, 'check changed alpha blend')
  908. love.graphics.setBlendMode('alpha', 'alphamultiply') -- reset
  909. end
  910. -- love.graphics.getCanvas
  911. love.test.graphics.getCanvas = function(test)
  912. -- by default should be nil if drawing to real screen
  913. test:assertEquals(nil, love.graphics.getCanvas(), 'check no canvas set')
  914. -- should return not nil when we target a canvas
  915. local canvas = love.graphics.newCanvas(16, 16)
  916. love.graphics.setCanvas(canvas)
  917. test:assertObject(love.graphics.getCanvas())
  918. love.graphics.setCanvas()
  919. end
  920. -- love.graphics.getColor
  921. love.test.graphics.getColor = function(test)
  922. -- by default should be white
  923. local r, g, b, a = love.graphics.getColor()
  924. test:assertEquals(1, r, 'check default color r')
  925. test:assertEquals(1, g, 'check default color g')
  926. test:assertEquals(1, b, 'check default color b')
  927. test:assertEquals(1, a, 'check default color a')
  928. -- check set color is returned correctly
  929. love.graphics.setColor(0, 0, 0, 0)
  930. r, g, b, a = love.graphics.getColor()
  931. test:assertEquals(0, r, 'check changed color r')
  932. test:assertEquals(0, g, 'check changed color g')
  933. test:assertEquals(0, b, 'check changed color b')
  934. test:assertEquals(0, a, 'check changed color a')
  935. love.graphics.setColor(1, 1, 1, 1) -- reset
  936. end
  937. -- love.graphics.getColorMask
  938. love.test.graphics.getColorMask = function(test)
  939. -- by default should all be active
  940. local r, g, b, a = love.graphics.getColorMask()
  941. test:assertEquals(true, r, 'check default color mask r')
  942. test:assertEquals(true, g, 'check default color mask g')
  943. test:assertEquals(true, b, 'check default color mask b')
  944. test:assertEquals(true, a, 'check default color mask a')
  945. -- check set color mask is returned correctly
  946. love.graphics.setColorMask(false, false, true, false)
  947. r, g, b, a = love.graphics.getColorMask()
  948. test:assertEquals(false, r, 'check changed color mask r')
  949. test:assertEquals(false, g, 'check changed color mask g')
  950. test:assertEquals(true, b, 'check changed color mask b')
  951. test:assertEquals(false, a, 'check changed color mask a')
  952. love.graphics.setColorMask(true, true, true, true) -- reset
  953. end
  954. -- love.graphics.getDefaultFilter
  955. love.test.graphics.getDefaultFilter = function(test)
  956. -- we set this already for testsuite so we know what it should be
  957. local min, mag, anisotropy = love.graphics.getDefaultFilter()
  958. test:assertEquals('nearest', min, 'check default filter min')
  959. test:assertEquals('nearest', mag, 'check default filter mag')
  960. test:assertEquals(1, anisotropy, 'check default filter mag')
  961. end
  962. -- love.graphics.getDepthMode
  963. love.test.graphics.getDepthMode = function(test)
  964. -- by default should be always/write
  965. local comparemode, write = love.graphics.getDepthMode()
  966. test:assertEquals('always', comparemode, 'check default compare depth')
  967. test:assertEquals(false, write, 'check default depth buffer write')
  968. end
  969. -- love.graphics.getFont
  970. love.test.graphics.getFont = function(test)
  971. test:assertObject(love.graphics.getFont())
  972. end
  973. -- love.graphics.getFrontFaceWinding
  974. love.test.graphics.getFrontFaceWinding = function(test)
  975. -- check default winding
  976. test:assertEquals('ccw', love.graphics.getFrontFaceWinding())
  977. -- check setting value changes it correctly
  978. love.graphics.setFrontFaceWinding('cw')
  979. test:assertEquals('cw', love.graphics.getFrontFaceWinding())
  980. love.graphics.setFrontFaceWinding('ccw') -- reset
  981. end
  982. -- love.graphics.getLineJoin
  983. love.test.graphics.getLineJoin = function(test)
  984. -- check default line join
  985. test:assertEquals('miter', love.graphics.getLineJoin())
  986. -- check set value returned correctly
  987. love.graphics.setLineJoin('none')
  988. test:assertEquals('none', love.graphics.getLineJoin())
  989. love.graphics.setLineJoin('miter') -- reset
  990. end
  991. -- love.graphics.getLineStyle
  992. love.test.graphics.getLineStyle = function(test)
  993. -- we know this should be as testsuite sets it!
  994. test:assertEquals('rough', love.graphics.getLineStyle())
  995. -- check set value returned correctly
  996. love.graphics.setLineStyle('smooth')
  997. test:assertEquals('smooth', love.graphics.getLineStyle())
  998. love.graphics.setLineStyle('rough') -- reset
  999. end
  1000. -- love.graphics.getLineWidth
  1001. love.test.graphics.getLineWidth = function(test)
  1002. -- we know this should be as testsuite sets it!
  1003. test:assertEquals(1, love.graphics.getLineWidth())
  1004. -- check set value returned correctly
  1005. love.graphics.setLineWidth(10)
  1006. test:assertEquals(10, love.graphics.getLineWidth())
  1007. love.graphics.setLineWidth(1) -- reset
  1008. end
  1009. -- love.graphics.getMeshCullMode
  1010. love.test.graphics.getMeshCullMode = function(test)
  1011. -- get default mesh culling
  1012. test:assertEquals('none', love.graphics.getMeshCullMode())
  1013. -- check set value returned correctly
  1014. love.graphics.setMeshCullMode('front')
  1015. test:assertEquals('front', love.graphics.getMeshCullMode())
  1016. love.graphics.setMeshCullMode('back') -- reset
  1017. end
  1018. -- love.graphics.getPointSize
  1019. love.test.graphics.getPointSize = function(test)
  1020. -- get default point size
  1021. test:assertEquals(1, love.graphics.getPointSize())
  1022. -- check set value returned correctly
  1023. love.graphics.setPointSize(10)
  1024. test:assertEquals(10, love.graphics.getPointSize())
  1025. love.graphics.setPointSize(1) -- reset
  1026. end
  1027. -- love.graphics.getScissor
  1028. love.test.graphics.getScissor = function(test)
  1029. -- should be no scissor atm
  1030. local x, y, w, h = love.graphics.getScissor()
  1031. test:assertEquals(nil, x, 'check no scissor')
  1032. test:assertEquals(nil, y, 'check no scissor')
  1033. test:assertEquals(nil, w, 'check no scissor')
  1034. test:assertEquals(nil, h, 'check no scissor')
  1035. -- check set value returned correctly
  1036. love.graphics.setScissor(0, 0, 16, 16)
  1037. x, y, w, h = love.graphics.getScissor()
  1038. test:assertEquals(0, x, 'check scissor set')
  1039. test:assertEquals(0, y, 'check scissor set')
  1040. test:assertEquals(16, w, 'check scissor set')
  1041. test:assertEquals(16, h, 'check scissor set')
  1042. love.graphics.setScissor() -- reset
  1043. end
  1044. -- love.graphics.getShader
  1045. love.test.graphics.getShader = function(test)
  1046. -- should be no shader active
  1047. test:assertEquals(nil, love.graphics.getShader(), 'check no active shader')
  1048. end
  1049. -- love.graphics.getStackDepth
  1050. love.test.graphics.getStackDepth = function(test)
  1051. -- by default should be none
  1052. test:assertEquals(0, love.graphics.getStackDepth(), 'check no transforms in stack')
  1053. -- now add 3
  1054. love.graphics.push()
  1055. love.graphics.push()
  1056. love.graphics.push()
  1057. test:assertEquals(3, love.graphics.getStackDepth(), 'check 3 transforms in stack')
  1058. -- now remove 2
  1059. love.graphics.pop()
  1060. love.graphics.pop()
  1061. test:assertEquals(1, love.graphics.getStackDepth(), 'check 1 transforms in stack')
  1062. -- now back to 0
  1063. love.graphics.pop()
  1064. test:assertEquals(0, love.graphics.getStackDepth(), 'check no transforms in stack')
  1065. end
  1066. -- love.graphics.getStencilMode
  1067. love.test.graphics.getStencilMode = function(test)
  1068. -- check default vals
  1069. local action, comparemode, value = love.graphics.getStencilMode( )
  1070. test:assertEquals('keep', action, 'check default stencil action')
  1071. test:assertEquals('always', comparemode, 'check default stencil compare')
  1072. test:assertEquals(0, value, 'check default stencil value')
  1073. -- check set stencil values is returned
  1074. love.graphics.setStencilMode('replace', 'less', 255)
  1075. local action, comparemode, value = love.graphics.getStencilMode()
  1076. test:assertEquals('replace', action, 'check changed stencil action')
  1077. test:assertEquals('less', comparemode, 'check changed stencil compare')
  1078. test:assertEquals(255, value, 'check changed stencil value')
  1079. love.graphics.setStencilMode() -- reset
  1080. end
  1081. -- love.graphics.intersectScissor
  1082. love.test.graphics.intersectScissor = function(test)
  1083. -- make a scissor for the left half, then interset to make the top half
  1084. -- then we should be able to fill the canvas with red and only top 4x4 is filled
  1085. local canvas = love.graphics.newCanvas(16, 16)
  1086. love.graphics.setCanvas(canvas)
  1087. love.graphics.clear(0, 0, 0, 1)
  1088. love.graphics.origin()
  1089. love.graphics.setScissor(0, 0, 8, 16)
  1090. love.graphics.intersectScissor(0, 0, 4, 4)
  1091. love.graphics.setColor(1, 0, 0, 1)
  1092. love.graphics.rectangle('fill', 0, 0, 16, 16)
  1093. love.graphics.setColor(1, 1, 1, 1)
  1094. love.graphics.setScissor()
  1095. love.graphics.setCanvas()
  1096. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1097. test:assertPixels(imgdata, {
  1098. red = {{0,0},{3,3}},
  1099. black ={{4,0},{0,4},{4,4}}
  1100. }, 'intersect scissor')
  1101. test:exportImg(imgdata)
  1102. end
  1103. -- love.graphics.isActive
  1104. love.test.graphics.isActive = function(test)
  1105. local name, version, vendor, device = love.graphics.getRendererInfo()
  1106. if string.find(name, 'Vulkan') ~= nil then
  1107. test:skipTest('love.graphics.isActive() crashes on Vulkan')
  1108. else
  1109. test:assertEquals(true, love.graphics.isActive(), 'check graphics is active') -- i mean if you got this far
  1110. end
  1111. end
  1112. -- love.graphics.isGammaCorrect
  1113. love.test.graphics.isGammaCorrect = function(test)
  1114. -- we know the config so know this is false
  1115. test:assertNotNil(love.graphics.isGammaCorrect())
  1116. end
  1117. -- love.graphics.isWireframe
  1118. love.test.graphics.isWireframe = function(test)
  1119. local name, version, vendor, device = love.graphics.getRendererInfo()
  1120. if string.match(name, 'OpenGL ES') then
  1121. test:skipTest('Wireframe not supported on OpenGL ES')
  1122. else
  1123. -- check off by default
  1124. test:assertEquals(false, love.graphics.isWireframe(), 'check no wireframe by default')
  1125. -- check on when enabled
  1126. love.graphics.setWireframe(true)
  1127. test:assertEquals(true, love.graphics.isWireframe(), 'check wireframe is set')
  1128. love.graphics.setWireframe(false) -- reset
  1129. end
  1130. end
  1131. -- love.graphics.reset
  1132. love.test.graphics.reset = function(test)
  1133. -- reset should reset current canvas and any colors/scissor
  1134. local canvas = love.graphics.newCanvas(16, 16)
  1135. love.graphics.setBackgroundColor(0, 0, 1, 1)
  1136. love.graphics.setColor(0, 1, 0, 1)
  1137. love.graphics.setCanvas(canvas)
  1138. love.graphics.reset()
  1139. local r, g, b, a = love.graphics.getBackgroundColor()
  1140. test:assertEquals(1, r+g+b+a, 'check background reset')
  1141. r, g, b, a = love.graphics.getColor()
  1142. test:assertEquals(4, r+g+b+a, 'check color reset')
  1143. test:assertEquals(nil, love.graphics.getCanvas(), 'check canvas reset')
  1144. end
  1145. -- love.graphics.setBackgroundColor
  1146. love.test.graphics.setBackgroundColor = function(test)
  1147. -- check background is set
  1148. love.graphics.setBackgroundColor(1, 0, 0, 1)
  1149. local r, g, b, a = love.graphics.getBackgroundColor()
  1150. test:assertEquals(1, r, 'check set bg r')
  1151. test:assertEquals(0, g, 'check set bg g')
  1152. test:assertEquals(0, b, 'check set bg b')
  1153. test:assertEquals(1, a, 'check set bg a')
  1154. love.graphics.setBackgroundColor(0, 0, 0, 1)
  1155. end
  1156. -- love.graphics.setBlendMode
  1157. love.test.graphics.setBlendMode = function(test)
  1158. -- create fully white canvas, then draw diff. pixels through blendmodes
  1159. local canvas = love.graphics.newCanvas(16, 16)
  1160. love.graphics.setCanvas(canvas)
  1161. love.graphics.clear(0.5, 0.5, 0.5, 1)
  1162. love.graphics.setBlendMode('add', 'alphamultiply')
  1163. love.graphics.setColor(1, 0, 0, 1)
  1164. love.graphics.points({1,1})
  1165. love.graphics.setBlendMode('subtract', 'alphamultiply')
  1166. love.graphics.setColor(1, 1, 1, 0.5)
  1167. love.graphics.points({16,1})
  1168. love.graphics.setBlendMode('multiply', 'premultiplied')
  1169. love.graphics.setColor(0, 1, 0, 1)
  1170. love.graphics.points({16,16})
  1171. love.graphics.setBlendMode('replace', 'premultiplied')
  1172. love.graphics.setColor(0, 0, 1, 0.5)
  1173. love.graphics.points({1,16})
  1174. love.graphics.setColor(1, 1, 1, 1)
  1175. love.graphics.setCanvas()
  1176. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1177. -- check the 4 corners
  1178. test:assertPixels(imgdata, {
  1179. redpale = {{0,0}},
  1180. black = {{15,0}},
  1181. greenhalf = {{15,15}},
  1182. bluefade = {{0,15}}
  1183. }, 'blend mode')
  1184. love.graphics.setBlendMode('alpha', 'alphamultiply') -- reset
  1185. test:exportImg(imgdata)
  1186. end
  1187. -- love.graphics.setCanvas
  1188. love.test.graphics.setCanvas = function(test)
  1189. -- make 2 canvas, set to each, draw one to the other, check output
  1190. local canvas1 = love.graphics.newCanvas(16, 16)
  1191. local canvas2 = love.graphics.newCanvas(16, 16)
  1192. love.graphics.setCanvas(canvas1)
  1193. test:assertEquals(canvas1, love.graphics.getCanvas(), 'check canvas 1 set')
  1194. love.graphics.clear(1, 0, 0, 1)
  1195. love.graphics.setCanvas(canvas2)
  1196. test:assertEquals(canvas2, love.graphics.getCanvas(), 'check canvas 2 set')
  1197. love.graphics.clear(0, 0, 0, 1)
  1198. love.graphics.draw(canvas1, 0, 0)
  1199. love.graphics.setCanvas()
  1200. test:assertEquals(nil, love.graphics.getCanvas(), 'check no canvas set')
  1201. local imgdata = love.graphics.readbackTexture(canvas2, {16, 0, 0, 0, 16, 16})
  1202. -- check 2nd canvas is red
  1203. test:assertPixels(imgdata, {
  1204. red = {{0,0},{15,0},{15,15},{0,15}}
  1205. }, 'set canvas')
  1206. test:exportImg(imgdata)
  1207. end
  1208. -- love.graphics.setColor
  1209. love.test.graphics.setColor = function(test)
  1210. -- set colors, draw rect, check color
  1211. local canvas = love.graphics.newCanvas(16, 16)
  1212. love.graphics.setCanvas(canvas)
  1213. love.graphics.clear(0, 0, 0, 1)
  1214. love.graphics.setColor(1, 0, 0, 1)
  1215. local r, g, b, a = love.graphics.getColor()
  1216. test:assertEquals(1, r, 'check r set')
  1217. test:assertEquals(0, g, 'check g set')
  1218. test:assertEquals(0, b, 'check b set')
  1219. test:assertEquals(1, a, 'check a set')
  1220. love.graphics.points({{1,1},{6,1},{11,1},{16,1}})
  1221. love.graphics.setColor(1, 1, 0, 1)
  1222. love.graphics.points({{1,2},{6,2},{11,2},{16,2}})
  1223. love.graphics.setColor(0, 1, 0, 0.5)
  1224. love.graphics.points({{1,3},{6,3},{11,3},{16,3}})
  1225. love.graphics.setColor(0, 0, 1, 1)
  1226. love.graphics.points({{1,4},{6,4},{11,4},{16,4}})
  1227. love.graphics.setColor(1, 1, 1, 1)
  1228. love.graphics.setCanvas()
  1229. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1230. test:assertPixels(imgdata, {
  1231. red = {{0,0},{5,0},{10,0},{15,0}},
  1232. yellow = {{0,1},{5,1},{10,1},{15,1}},
  1233. greenhalf = {{0,2},{5,2},{10,2},{15,2}},
  1234. blue = {{0,3},{5,3},{10,3},{15,3}}
  1235. }, 'set color')
  1236. test:exportImg(imgdata)
  1237. end
  1238. -- love.graphics.setColorMask
  1239. love.test.graphics.setColorMask = function(test)
  1240. -- set mask, draw stuff, check output pixels
  1241. local canvas = love.graphics.newCanvas(16, 16)
  1242. love.graphics.setCanvas(canvas)
  1243. love.graphics.clear(0, 0, 0, 1)
  1244. -- mask off blue
  1245. love.graphics.setColorMask(true, true, false, true)
  1246. local r, g, b, a = love.graphics.getColorMask()
  1247. test:assertEquals(r, true, 'check r mask')
  1248. test:assertEquals(g, true, 'check g mask')
  1249. test:assertEquals(b, false, 'check b mask')
  1250. test:assertEquals(a, true, 'check a mask')
  1251. -- draw "black" which should then turn to yellow
  1252. love.graphics.setColor(1, 1, 1, 1)
  1253. love.graphics.rectangle('fill', 0, 0, 16, 16)
  1254. love.graphics.setColorMask(true, true, true, true)
  1255. love.graphics.setCanvas()
  1256. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1257. test:assertPixels(imgdata, {
  1258. yellow = {{0,0},{0,15},{15,15},{15,0}}
  1259. }, 'set color mask')
  1260. test:exportImg(imgdata)
  1261. end
  1262. -- love.graphics.setDefaultFilter
  1263. love.test.graphics.setDefaultFilter = function(test)
  1264. -- check setting filter val works
  1265. love.graphics.setDefaultFilter('linear', 'linear', 1)
  1266. local min, mag, anisotropy = love.graphics.getDefaultFilter()
  1267. test:assertEquals('linear', min, 'check default filter min')
  1268. test:assertEquals('linear', mag, 'check default filter mag')
  1269. test:assertEquals(1, anisotropy, 'check default filter mag')
  1270. love.graphics.setDefaultFilter('nearest', 'nearest', 1) -- reset
  1271. end
  1272. -- love.graphics.setDepthMode
  1273. love.test.graphics.setDepthMode = function(test)
  1274. -- check documented modes are valid
  1275. local comparemode, write = love.graphics.getDepthMode()
  1276. local modes = {
  1277. 'equal', 'notequal', 'less', 'lequal', 'gequal',
  1278. 'greater', 'never', 'always'
  1279. }
  1280. for m=1,#modes do
  1281. love.graphics.setDepthMode(modes[m], true)
  1282. test:assertEquals(modes[m], love.graphics.getDepthMode(), 'check depth mode ' .. modes[m] .. ' set')
  1283. end
  1284. love.graphics.setDepthMode(comparemode, write)
  1285. -- @TODO better graphics drawing specific test
  1286. end
  1287. -- love.graphics.setFont
  1288. love.test.graphics.setFont = function(test)
  1289. -- set font doesnt return anything so draw with the test font
  1290. local canvas = love.graphics.newCanvas(16, 16)
  1291. love.graphics.setFont(Font)
  1292. love.graphics.setCanvas(canvas)
  1293. love.graphics.clear(0, 0, 0, 1)
  1294. love.graphics.setColor(1, 0, 0, 1)
  1295. love.graphics.print('love', 0, 3)
  1296. love.graphics.setColor(1, 1, 1, 1)
  1297. love.graphics.setCanvas()
  1298. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1299. test:assertPixels(imgdata, {
  1300. red = {
  1301. {0,0},{0,6},{2,6},{6,2},
  1302. {4,4},{8,4},{6,6},{10,2},
  1303. {14,2},{12,6}
  1304. }
  1305. }, 'set font for print')
  1306. test:exportImg(imgdata)
  1307. end
  1308. -- love.graphics.setFrontFaceWinding
  1309. love.test.graphics.setFrontFaceWinding = function(test)
  1310. -- check documented modes are valid
  1311. local original = love.graphics.getFrontFaceWinding()
  1312. love.graphics.setFrontFaceWinding('cw')
  1313. test:assertEquals('cw', love.graphics.getFrontFaceWinding(), 'check ffw cw set')
  1314. love.graphics.setFrontFaceWinding('ccw')
  1315. test:assertEquals('ccw', love.graphics.getFrontFaceWinding(), 'check ffw ccw set')
  1316. love.graphics.setFrontFaceWinding(original)
  1317. -- @TODO better graphics drawing specific test
  1318. end
  1319. -- love.graphics.setLineJoin
  1320. love.test.graphics.setLineJoin = function(test)
  1321. local canvas = love.graphics.newCanvas(16, 16)
  1322. love.graphics.setFont(Font)
  1323. love.graphics.setCanvas(canvas)
  1324. love.graphics.clear(0, 0, 0, 1)
  1325. local line = {0,1,8,1,8,8}
  1326. love.graphics.setLineStyle('rough')
  1327. love.graphics.setLineWidth(2)
  1328. love.graphics.setColor(1, 0, 0)
  1329. love.graphics.setLineJoin('bevel')
  1330. love.graphics.line(line)
  1331. love.graphics.translate(0, 4)
  1332. love.graphics.setColor(1, 1, 0)
  1333. love.graphics.setLineJoin('none')
  1334. love.graphics.line(line)
  1335. love.graphics.translate(0, 4)
  1336. love.graphics.setColor(0, 0, 1)
  1337. love.graphics.setLineJoin('miter')
  1338. love.graphics.line(line)
  1339. love.graphics.setColor(1, 1, 1)
  1340. love.graphics.setLineWidth(1)
  1341. love.graphics.origin()
  1342. love.graphics.setCanvas()
  1343. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1344. test:assertPixels(imgdata, {
  1345. black = {{8,0}},
  1346. red = {{8,4}},
  1347. yellow = {{8,7}},
  1348. blue = {{8,8}}
  1349. }, 'set line join')
  1350. test:exportImg(imgdata)
  1351. end
  1352. -- love.graphics.setLineStyle
  1353. love.test.graphics.setLineStyle = function(test)
  1354. local canvas = love.graphics.newCanvas(16, 16)
  1355. love.graphics.setFont(Font)
  1356. love.graphics.setCanvas(canvas)
  1357. love.graphics.clear(0, 0, 0, 1)
  1358. love.graphics.setColor(1, 0, 0)
  1359. local line = {0,1,16,1}
  1360. love.graphics.setLineStyle('rough')
  1361. love.graphics.line(line)
  1362. love.graphics.translate(0, 4)
  1363. love.graphics.setLineStyle('smooth')
  1364. love.graphics.line(line)
  1365. love.graphics.setLineStyle('rough')
  1366. love.graphics.setColor(1, 1, 1)
  1367. love.graphics.origin()
  1368. love.graphics.setCanvas()
  1369. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1370. test:assertPixels(imgdata, {
  1371. red = {{0,0},{7,0},{15,0}},
  1372. red07 = {{0,4},{7,4},{15,4}}
  1373. }, 'set line style')
  1374. test:exportImg(imgdata)
  1375. end
  1376. -- love.graphics.setLineWidth
  1377. love.test.graphics.setLineWidth = function(test)
  1378. local canvas = love.graphics.newCanvas(16, 16)
  1379. love.graphics.setFont(Font)
  1380. love.graphics.setCanvas(canvas)
  1381. love.graphics.clear(0, 0, 0, 1)
  1382. local line = {0,1,8,1,8,8}
  1383. love.graphics.setColor(1, 0, 0)
  1384. love.graphics.setLineWidth(2)
  1385. love.graphics.line(line)
  1386. love.graphics.translate(0, 4)
  1387. love.graphics.setColor(1, 1, 0)
  1388. love.graphics.setLineWidth(3)
  1389. love.graphics.line(line)
  1390. love.graphics.translate(0, 4)
  1391. love.graphics.setColor(0, 0, 1)
  1392. love.graphics.setLineWidth(4)
  1393. love.graphics.line(line)
  1394. love.graphics.setColor(1, 1, 1)
  1395. love.graphics.setLineWidth(1)
  1396. love.graphics.origin()
  1397. love.graphics.setCanvas()
  1398. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1399. test:assertPixels(imgdata, {
  1400. black = {{0,2},{6,2},{0,6},{5,6},{0,11},{5,11}},
  1401. red = {{0,0},{0,1},{7,2},{8,2}},
  1402. yellow = {{0,3},{0,5},{6,6},{8,6}},
  1403. blue = {{0,7},{0,10},{6,15},{9,15}}
  1404. }, 'set line width')
  1405. test:exportImg(imgdata)
  1406. end
  1407. -- love.graphics.setMeshCullMode
  1408. love.test.graphics.setMeshCullMode = function(test)
  1409. -- check documented modes are valid
  1410. local original = love.graphics.getMeshCullMode()
  1411. local modes = {'back', 'front', 'none'}
  1412. for m=1,#modes do
  1413. love.graphics.setMeshCullMode(modes[m])
  1414. test:assertEquals(modes[m], love.graphics.getMeshCullMode(), 'check mesh cull mode ' .. modes[m] .. ' was set')
  1415. end
  1416. love.graphics.setMeshCullMode(original)
  1417. -- @TODO better graphics drawing specific test
  1418. end
  1419. -- love.graphics.setScissor
  1420. love.test.graphics.setScissor = function(test)
  1421. -- make a scissor for the left half
  1422. -- then we should be able to fill the canvas with red and only left is filled
  1423. local canvas = love.graphics.newCanvas(16, 16)
  1424. love.graphics.setCanvas(canvas)
  1425. love.graphics.clear(0, 0, 0, 1)
  1426. love.graphics.origin()
  1427. love.graphics.setScissor(0, 0, 8, 16)
  1428. love.graphics.setColor(1, 0, 0, 1)
  1429. love.graphics.rectangle('fill', 0, 0, 16, 16)
  1430. love.graphics.setColor(1, 1, 1, 1)
  1431. love.graphics.setScissor()
  1432. love.graphics.setCanvas()
  1433. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1434. test:assertPixels(imgdata, {
  1435. red = {{0,0},{7,0},{0,15},{7,15}},
  1436. black ={{8,0},{8,15},{15,0},{15,15}}
  1437. }, 'set scissor')
  1438. test:exportImg(imgdata)
  1439. end
  1440. -- love.graphics.setShader
  1441. love.test.graphics.setShader = function(test)
  1442. -- make a shader that will only ever draw yellow
  1443. local pixelcode = [[
  1444. vec4 effect(vec4 color, Image tex, vec2 texture_coords, vec2 screen_coords) {
  1445. vec4 texturecolor = Texel(tex, texture_coords);
  1446. return vec4(1.0,1.0,0.0,1.0);
  1447. }
  1448. ]]
  1449. local vertexcode = [[
  1450. vec4 position(mat4 transform_projection, vec4 vertex_position) {
  1451. return transform_projection * vertex_position;
  1452. }
  1453. ]]
  1454. local shader = love.graphics.newShader(pixelcode, vertexcode)
  1455. local canvas = love.graphics.newCanvas(16, 16)
  1456. love.graphics.setCanvas(canvas)
  1457. love.graphics.clear(0, 0, 0, 1)
  1458. love.graphics.setShader(shader)
  1459. -- draw red rectangle
  1460. love.graphics.setColor(1, 0, 0, 1)
  1461. love.graphics.rectangle('fill', 0, 0, 16, 16)
  1462. love.graphics.setShader()
  1463. love.graphics.setColor(1, 1, 1, 1)
  1464. love.graphics.setCanvas()
  1465. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1466. test:assertPixels(imgdata, {
  1467. yellow = {{0,0},{15,0},{0,15},{15,15}},
  1468. }, 'check shader set to yellow')
  1469. test:exportImg(imgdata)
  1470. end
  1471. -- love.graphics.setStencilTest
  1472. love.test.graphics.setStencilTest = function(test)
  1473. local canvas = love.graphics.newCanvas(16, 16)
  1474. love.graphics.setCanvas({canvas, stencil=true})
  1475. love.graphics.clear(0, 0, 0, 1)
  1476. love.graphics.stencil(function()
  1477. love.graphics.circle('fill', 8, 8, 6)
  1478. end, 'replace', 1)
  1479. love.graphics.setStencilTest('greater', 0)
  1480. love.graphics.setColor(1, 0, 0, 1)
  1481. love.graphics.rectangle('fill', 0, 0, 16, 16)
  1482. love.graphics.setColor(1, 1, 1, 1)
  1483. love.graphics.setStencilTest()
  1484. love.graphics.setCanvas()
  1485. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1486. test:assertPixels(imgdata, {
  1487. red = {{6,2},{9,2},{2,6},{2,9},{13,6},{9,6},{6,13},{9,13}}
  1488. }, 'check stencil test')
  1489. test:exportImg(imgdata)
  1490. end
  1491. -- love.graphics.setWireframe
  1492. love.test.graphics.setWireframe = function(test)
  1493. local name, version, vendor, device = love.graphics.getRendererInfo()
  1494. if string.match(name, 'OpenGL ES') then
  1495. test:skipTest('Wireframe not supported on OpenGL ES')
  1496. else
  1497. -- check wireframe outlines
  1498. love.graphics.setWireframe(true)
  1499. local canvas = love.graphics.newCanvas(16, 16)
  1500. love.graphics.setCanvas(canvas)
  1501. love.graphics.clear(0, 0, 0, 1)
  1502. love.graphics.setColor(1, 1, 0, 1)
  1503. love.graphics.rectangle('fill', 2, 2, 13, 13)
  1504. love.graphics.setColor(1, 1, 1, 1)
  1505. love.graphics.setWireframe(false)
  1506. love.graphics.setCanvas()
  1507. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1508. test:assertPixels(imgdata, {
  1509. yellow = {{1,14},{14,1},{14,14},{2,2},{13,13}},
  1510. black = {{2,13},{13,2}}
  1511. }, 'set wireframe')
  1512. test:exportImg(imgdata)
  1513. end
  1514. end
  1515. --------------------------------------------------------------------------------
  1516. --------------------------------------------------------------------------------
  1517. -------------------------------COORDINATE SYSTEM--------------------------------
  1518. --------------------------------------------------------------------------------
  1519. --------------------------------------------------------------------------------
  1520. -- love.graphics.applyTransform
  1521. love.test.graphics.applyTransform = function(test)
  1522. -- use transform object to translate the drawn rectangle
  1523. local transform = love.math.newTransform()
  1524. transform:translate(10, 0)
  1525. local canvas = love.graphics.newCanvas(16, 16)
  1526. love.graphics.setCanvas(canvas)
  1527. love.graphics.clear(0, 0, 0, 1)
  1528. love.graphics.setColor(1, 0, 0, 1)
  1529. love.graphics.applyTransform(transform)
  1530. love.graphics.rectangle('fill', 0, 0, 1, 1)
  1531. love.graphics.setColor(1, 1, 1, 1)
  1532. love.graphics.setCanvas()
  1533. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1534. test:assertPixels(imgdata, { red = {{10, 0}} }, 'apply transform 10')
  1535. test:exportImg(imgdata)
  1536. end
  1537. -- love.graphics.inverseTransformPoint
  1538. love.test.graphics.inverseTransformPoint = function(test)
  1539. -- start with 0, 0
  1540. local sx, sy = love.graphics.inverseTransformPoint(0, 0)
  1541. test:assertEquals(0, sx, 'check starting x is 0')
  1542. test:assertEquals(0, sy, 'check starting y is 0')
  1543. -- check translation effects the point
  1544. love.graphics.translate(1, 5)
  1545. sx, sy = love.graphics.inverseTransformPoint(1, 5)
  1546. test:assertEquals(0, sx, 'check transformed x is 0')
  1547. test:assertEquals(0, sy, 'check transformed y is 0')
  1548. love.graphics.origin()
  1549. end
  1550. -- love.graphics.origin
  1551. love.test.graphics.origin = function(test)
  1552. -- if we do some translations and scaling
  1553. -- using .origin() should reset it all and draw the pixel at 0,0
  1554. local canvas = love.graphics.newCanvas(16, 16)
  1555. love.graphics.setCanvas(canvas)
  1556. love.graphics.clear(0, 0, 0, 1)
  1557. love.graphics.origin()
  1558. love.graphics.translate(10, 10)
  1559. love.graphics.scale(1, 1)
  1560. love.graphics.shear(20, 20)
  1561. love.graphics.origin()
  1562. love.graphics.setColor(1, 0, 0, 1)
  1563. love.graphics.rectangle('fill', 0, 0, 1, 1)
  1564. love.graphics.setColor(1, 1, 1, 1)
  1565. love.graphics.setCanvas()
  1566. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1567. test:assertPixels(imgdata, { red = {{0, 0}} }, 'origin check')
  1568. test:exportImg(imgdata)
  1569. end
  1570. -- love.graphics.pop
  1571. love.test.graphics.pop = function(test)
  1572. -- if we push at the start, and then run a pop
  1573. -- it should reset it all and draw the pixel at 0,0
  1574. local canvas = love.graphics.newCanvas(16, 16)
  1575. love.graphics.setCanvas(canvas)
  1576. love.graphics.clear(0, 0, 0, 1)
  1577. love.graphics.push()
  1578. love.graphics.translate(10, 10)
  1579. love.graphics.scale(1, 1)
  1580. love.graphics.shear(20, 20)
  1581. love.graphics.pop()
  1582. love.graphics.setColor(1, 0, 0, 1)
  1583. love.graphics.rectangle('fill', 0, 0, 1, 1)
  1584. love.graphics.setColor(1, 1, 1, 1)
  1585. love.graphics.setCanvas()
  1586. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1587. test:assertPixels(imgdata, { red = {{0, 0}} }, 'pop 1')
  1588. test:exportImg(imgdata)
  1589. end
  1590. -- love.graphics.push
  1591. love.test.graphics.push = function(test)
  1592. -- if we push at the start, do some stuff, then another push
  1593. -- 1 pop should only go back 1 push and draw the pixel at 1, 1
  1594. local canvas = love.graphics.newCanvas(16, 16)
  1595. love.graphics.setCanvas(canvas)
  1596. love.graphics.clear(0, 0, 0, 1)
  1597. love.graphics.push()
  1598. love.graphics.scale(1, 1)
  1599. love.graphics.shear(20, 20)
  1600. love.graphics.push()
  1601. love.graphics.translate(1, 1)
  1602. love.graphics.pop()
  1603. love.graphics.setColor(1, 0, 0, 1)
  1604. love.graphics.rectangle('fill', 0, 0, 1, 1)
  1605. love.graphics.setColor(1, 1, 1, 1)
  1606. love.graphics.setCanvas()
  1607. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1608. test:assertPixels(imgdata, { red = {{1, 1}} }, 'push 1')
  1609. test:exportImg(imgdata)
  1610. end
  1611. -- love.graphics.replaceTransform
  1612. love.test.graphics.replaceTransform = function(test)
  1613. -- if use transform object to translate
  1614. -- set some normal transforms first which should get overwritten
  1615. local transform = love.math.newTransform()
  1616. transform:translate(10, 0)
  1617. local canvas = love.graphics.newCanvas(16, 16)
  1618. love.graphics.setCanvas(canvas)
  1619. love.graphics.clear(0, 0, 0, 1)
  1620. love.graphics.setColor(1, 0, 0, 1)
  1621. love.graphics.scale(2, 2)
  1622. love.graphics.translate(10, 10)
  1623. love.graphics.replaceTransform(transform)
  1624. love.graphics.rectangle('fill', 0, 0, 1, 1)
  1625. love.graphics.setColor(1, 1, 1, 1)
  1626. love.graphics.setCanvas()
  1627. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1628. test:assertPixels(imgdata, { red = {{10, 0}} }, 'replace transform 10')
  1629. test:exportImg(imgdata)
  1630. end
  1631. -- love.graphics.rotate
  1632. love.test.graphics.rotate = function(test)
  1633. -- starting at 0,0, we rotate by 90deg and then draw
  1634. -- we can then check the drawn rectangle is rotated
  1635. local canvas = love.graphics.newCanvas(16, 16)
  1636. love.graphics.setCanvas(canvas)
  1637. love.graphics.clear(0, 0, 0, 1)
  1638. love.graphics.setColor(1, 0, 0, 1)
  1639. love.graphics.translate(4, 0)
  1640. love.graphics.rotate(90 * (math.pi/180))
  1641. love.graphics.rectangle('fill', 0, 0, 4, 4)
  1642. love.graphics.setColor(1, 1, 1, 1)
  1643. love.graphics.setCanvas()
  1644. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1645. test:assertPixels(imgdata, { red = {{0,0},{3,0},{3,3},{0,3}} }, 'rotate 90')
  1646. test:exportImg(imgdata)
  1647. end
  1648. -- love.graphics.scale
  1649. love.test.graphics.scale = function(test)
  1650. -- starting at 0,0, we scale by 4x and then draw
  1651. -- we can then check the drawn rectangle covers the whole canvas
  1652. local canvas = love.graphics.newCanvas(16, 16)
  1653. love.graphics.setCanvas(canvas)
  1654. love.graphics.clear(0, 0, 0, 1)
  1655. love.graphics.setColor(1, 0, 0, 1)
  1656. love.graphics.scale(4, 4)
  1657. love.graphics.rectangle('fill', 0, 0, 4, 4)
  1658. love.graphics.setColor(1, 1, 1, 1)
  1659. love.graphics.setCanvas()
  1660. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1661. test:assertPixels(imgdata, { red = {{1,1},{1,15},{15,1},{15,15}} }, 'scale 4x')
  1662. end
  1663. -- love.graphics.shear
  1664. love.test.graphics.shear = function(test)
  1665. -- starting at 0,0, we shear by 2x and then draw
  1666. -- we can then check the drawn rectangle has moved over
  1667. local canvas = love.graphics.newCanvas(16, 16)
  1668. love.graphics.setCanvas(canvas)
  1669. love.graphics.clear(0, 0, 0, 1)
  1670. love.graphics.origin()
  1671. love.graphics.setColor(1, 0, 0, 1)
  1672. love.graphics.shear(2, 0)
  1673. love.graphics.rectangle('fill', 0, 0, 4, 4)
  1674. love.graphics.setColor(1, 1, 1, 1)
  1675. love.graphics.setCanvas()
  1676. local imgdata1 = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1677. test:assertPixels(imgdata1, { red = {{1,0},{4,0},{7,3},{10,3}} }, 'shear x')
  1678. -- same again at 0,0, we shear by 2y and then draw
  1679. -- we can then check the drawn rectangle has moved down
  1680. love.graphics.setCanvas(canvas)
  1681. love.graphics.clear(0, 0, 0, 1)
  1682. love.graphics.origin()
  1683. love.graphics.setColor(1, 0, 0, 1)
  1684. love.graphics.shear(0, 2)
  1685. love.graphics.rectangle('fill', 0, 0, 4, 4)
  1686. love.graphics.setColor(1, 1, 1, 1)
  1687. love.graphics.setCanvas()
  1688. local imgdata2 = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1689. test:assertPixels(imgdata2, { red = { {0,1},{0,4},{3,7},{3,10}} }, 'shear y')
  1690. test:exportImg(imgdata1)
  1691. test:exportImg(imgdata2)
  1692. end
  1693. -- love.graphics.transformPoint
  1694. love.test.graphics.transformPoint = function(test)
  1695. -- start with 0, 0
  1696. local sx, sy = love.graphics.transformPoint(0, 0)
  1697. test:assertEquals(0, sx, 'check starting x is 0')
  1698. test:assertEquals(0, sy, 'check starting y is 0')
  1699. -- check translation effects the point
  1700. love.graphics.translate(1, 5)
  1701. sx, sy = love.graphics.transformPoint(0, 0)
  1702. test:assertEquals(1, sx, 'check transformed x is 0')
  1703. test:assertEquals(5, sy, 'check transformed y is 10')
  1704. end
  1705. -- love.graphics.translate
  1706. love.test.graphics.translate = function(test)
  1707. -- starting at 0,0, we translate 4 times and draw a pixel at each point
  1708. -- we can then check the 4 points are now red
  1709. local canvas = love.graphics.newCanvas(16, 16)
  1710. love.graphics.setCanvas(canvas)
  1711. love.graphics.clear(0, 0, 0, 1)
  1712. love.graphics.setColor(1, 0, 0, 1)
  1713. love.graphics.translate(5, 0)
  1714. love.graphics.rectangle('fill', 0, 0, 1, 1)
  1715. love.graphics.translate(0, 5)
  1716. love.graphics.rectangle('fill', 0, 0, 1, 1)
  1717. love.graphics.translate(-5, 0)
  1718. love.graphics.rectangle('fill', 0, 0, 1, 1)
  1719. love.graphics.translate(0, -5)
  1720. love.graphics.rectangle('fill', 0, 0, 1, 1)
  1721. love.graphics.setColor(1, 1, 1, 1)
  1722. love.graphics.setCanvas()
  1723. local imgdata = love.graphics.readbackTexture(canvas, {16, 0, 0, 0, 16, 16})
  1724. test:assertPixels(imgdata, { red = {{5,0},{0,5},{5,5},{0,0}} }, 'translate 4x')
  1725. test:exportImg(imgdata)
  1726. end
  1727. --------------------------------------------------------------------------------
  1728. --------------------------------------------------------------------------------
  1729. -------------------------------------WINDOW-------------------------------------
  1730. --------------------------------------------------------------------------------
  1731. --------------------------------------------------------------------------------
  1732. -- love.graphics.getDPIScale
  1733. -- @NOTE hardware dependent so can't check result
  1734. love.test.graphics.getDPIScale = function(test)
  1735. test:assertNotNil(love.graphics.getDPIScale())
  1736. end
  1737. -- love.graphics.getDimensions
  1738. love.test.graphics.getDimensions = function(test)
  1739. -- check graphics dimensions match window dimensions
  1740. local gwidth, gheight = love.graphics.getDimensions()
  1741. local wwidth, wheight, _ = love.window.getMode()
  1742. test:assertEquals(wwidth, gwidth, 'check graphics dimension w matches window w')
  1743. test:assertEquals(wheight, gheight, 'check graphics dimension h matches window h')
  1744. end
  1745. -- love.graphics.getHeight
  1746. love.test.graphics.getHeight = function(test)
  1747. -- check graphics height match window height
  1748. local wwidth, wheight, _ = love.window.getMode()
  1749. test:assertEquals(wheight, love.graphics.getHeight(), 'check graphics h matches window h')
  1750. end
  1751. -- love.graphics.getPixelDimensions
  1752. love.test.graphics.getPixelDimensions = function(test)
  1753. -- check graphics dimensions match window dimensions relative to dpi
  1754. local dpi = love.graphics.getDPIScale()
  1755. local gwidth, gheight = love.graphics.getPixelDimensions()
  1756. local wwidth, wheight, _ = love.window.getMode()
  1757. test:assertEquals(wwidth, gwidth/dpi, 'check graphics pixel dpi w matches window w')
  1758. test:assertEquals(wheight, gheight/dpi, 'check graphics pixel dpi h matches window h')
  1759. end
  1760. -- love.graphics.getPixelHeight
  1761. love.test.graphics.getPixelHeight = function(test)
  1762. -- check graphics height match window height relative to dpi
  1763. local dpi = love.graphics.getDPIScale()
  1764. local wwidth, wheight, _ = love.window.getMode()
  1765. test:assertEquals(wheight,love.graphics.getPixelHeight()/dpi, 'check graphics pixel dpi h matches window h')
  1766. end
  1767. -- love.graphics.getPixelWidth
  1768. love.test.graphics.getPixelWidth = function(test)
  1769. -- check graphics width match window width relative to dpi
  1770. local dpi = love.graphics.getDPIScale()
  1771. local wwidth, wheight, _ = love.window.getMode()
  1772. test:assertEquals(wwidth, love.graphics.getWidth()/dpi, 'check graphics pixel dpi w matches window w')
  1773. end
  1774. -- love.graphics.getWidth
  1775. love.test.graphics.getWidth = function(test)
  1776. -- check graphics width match window width
  1777. local wwidth, wheight, _ = love.window.getMode()
  1778. test:assertEquals(wwidth, love.graphics.getWidth(), 'check graphics w matches window w')
  1779. end
  1780. --------------------------------------------------------------------------------
  1781. --------------------------------------------------------------------------------
  1782. -------------------------------SYSTEM INFORMATION-------------------------------
  1783. --------------------------------------------------------------------------------
  1784. --------------------------------------------------------------------------------
  1785. -- love.graphics.getTextureFormats
  1786. love.test.graphics.getTextureFormats = function(test)
  1787. local formats = {
  1788. 'hdr', 'r8i', 'r8ui', 'r16i', 'r16ui', 'r32i', 'r32ui', 'rg8i', 'rg8ui',
  1789. 'rg16i', 'rg16ui', 'rg32i', 'rg32ui', 'bgra8', 'r8', 'rgba8i', 'rgba8ui',
  1790. 'rgba16i', 'rg8', 'rgba32i', 'rgba32ui', 'rgba8', 'DXT1', 'r16', 'DXT5',
  1791. 'rg16', 'BC4s', 'rgba16', 'BC5s', 'r16f', 'BC6hs', 'BC7', 'PVR1rgb2',
  1792. 'rg16f', 'PVR1rgba2', 'rgba16f', 'ETC1', 'r32f', 'ETC2rgba', 'rg32f',
  1793. 'EACr', 'rgba32f', 'EACrg', 'rgba4', 'ASTC4x4', 'ASTC5x4', 'rgb5a1',
  1794. 'ASTC6x5', 'rgb565', 'ASTC8x5', 'ASTC8x6', 'rgb10a2', 'ASTC10x5',
  1795. 'rg11b10f', 'ASTC10x8', 'ASTC10x10', 'ASTC12x10', 'ASTC12x12', 'normal',
  1796. 'srgba8', 'la8', 'ASTC10x6', 'ASTC8x8', 'ASTC6x6', 'ASTC5x5', 'EACrgs',
  1797. 'EACrs', 'ETC2rgba1', 'ETC2rgb', 'PVR1rgba4', 'PVR1rgb4', 'BC6h',
  1798. 'BC5', 'BC4', 'DXT3', 'stencil8', 'rgba16ui', 'bgra8srgb'
  1799. }
  1800. local supported = love.graphics.getTextureFormats({ canvas = true })
  1801. test:assertNotNil(supported)
  1802. for f=1,#formats do
  1803. test:assertNotEquals(nil, supported[formats[f] ], 'expected a key for format: ' .. formats[f])
  1804. end
  1805. end
  1806. -- love.graphics.getRendererInfo
  1807. -- @NOTE hardware dependent so best can do is nil checking
  1808. love.test.graphics.getRendererInfo = function(test)
  1809. local name, version, vendor, device = love.graphics.getRendererInfo()
  1810. test:assertNotNil(name)
  1811. test:assertNotNil(version)
  1812. test:assertNotNil(vendor)
  1813. test:assertNotNil(device)
  1814. end
  1815. -- love.graphics.getStats
  1816. -- @NOTE cant really predict some of these so just nil check for most
  1817. love.test.graphics.getStats = function(test)
  1818. local stattypes = {
  1819. 'drawcalls', 'canvasswitches', 'texturememory', 'shaderswitches',
  1820. 'drawcallsbatched', 'textures', 'fonts'
  1821. }
  1822. local stats = love.graphics.getStats()
  1823. for s=1,#stattypes do
  1824. test:assertNotEquals(nil, stats[stattypes[s] ], 'expected a key for stat: ' .. stattypes[s])
  1825. end
  1826. end
  1827. -- love.graphics.getSupported
  1828. love.test.graphics.getSupported = function(test)
  1829. -- cant check values as hardware dependent but we can check the keys in the
  1830. -- table match what the documentation lists
  1831. local gfs = {
  1832. 'clampzero', 'lighten', 'glsl3', 'instancing', 'fullnpot',
  1833. 'pixelshaderhighp', 'shaderderivatives', 'indirectdraw', 'mipmaprange',
  1834. 'copyrendertargettobuffer', 'copytexturetobuffer', 'copybuffer',
  1835. 'indexbuffer32bit', 'multirendertargetformats', 'clampone', 'blendminmax',
  1836. 'glsl4'
  1837. }
  1838. local features = love.graphics.getSupported()
  1839. for g=1,#gfs do
  1840. test:assertNotEquals(nil, features[gfs[g] ], 'expected a key for graphic feature: ' .. gfs[g])
  1841. end
  1842. end
  1843. -- love.graphics.getSystemLimits
  1844. love.test.graphics.getSystemLimits = function(test)
  1845. -- cant check values as hardware dependent but we can check the keys in the
  1846. -- table match what the documentation lists
  1847. local glimits = {
  1848. 'texelbuffersize', 'shaderstoragebuffersize', 'threadgroupsx',
  1849. 'threadgroupsy', 'pointsize', 'texturesize', 'texturelayers', 'volumetexturesize',
  1850. 'cubetexturesize', 'anisotropy', 'texturemsaa', 'rendertargets', 'threadgroupsz'
  1851. }
  1852. local limits = love.graphics.getSystemLimits()
  1853. for g=1,#glimits do
  1854. test:assertNotEquals(nil, limits[glimits[g] ], 'expected a key for system limit: ' .. glimits[g])
  1855. end
  1856. end
  1857. -- love.graphics.getTextureTypes
  1858. love.test.graphics.getTextureTypes = function(test)
  1859. -- cant check values as hardware dependent but we can check the keys in the
  1860. -- table match what the documentation lists
  1861. local ttypes = {
  1862. '2d', 'array', 'cube', 'volume'
  1863. }
  1864. local types = love.graphics.getTextureTypes()
  1865. for t=1,#ttypes do
  1866. test:assertNotEquals(nil, types[ttypes[t] ], 'expected a key for texture type: ' .. ttypes[t])
  1867. end
  1868. end