graphics.lua 64 KB

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