graphics.lua 79 KB

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