graphics.lua 102 KB

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